Fix some Dead Store (Dead assignement/Dead increment) Warning found by Clang
[obnox/wireshark/wip.git] / wsutil / unicode-utils.c
1 /* unicode-utils.c
2  * Unicode utility routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2006 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef _WIN32
26 #error "This is only for Windows"
27 #endif
28
29 #include "unicode-utils.h"
30
31 #include <shellapi.h>
32
33 /** @file
34  * Unicode utilities (internal interface)
35  *
36  * We define UNICODE and _UNICODE under Windows.  This means that
37  * Windows SDK routines expect UTF-16 strings, in contrast to newer
38  * versions of Glib and GTK+ which expect UTF-8.  This module provides
39  * convenience routines for converting between UTF-8 and UTF-16.
40  */
41
42 #define INITIAL_UTFBUF_SIZE 128
43
44 /*
45  * XXX - Should we use g_utf8_to_utf16() and g_utf16_to_utf8()
46  * instead?  The goal of the functions below was to provide simple
47  * wrappers for UTF-8 <-> UTF-16 conversion without making the
48  * caller worry about freeing up memory afterward.
49  */
50
51 /* Convert from UTF-8 to UTF-16. */
52 wchar_t *
53 utf_8to16(const char *utf8str)
54 {
55   static wchar_t *utf16buf[3];
56   static int utf16buf_len[3];
57   static int idx;
58
59   if (utf8str == NULL)
60     return NULL;
61
62   idx = (idx + 1) % 3;
63
64   /*
65    * Allocate the buffer if it's not already allocated.
66    */
67   if (utf16buf[idx] == NULL) {
68     utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
69     utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
70   }
71
72   while (MultiByteToWideChar(CP_UTF8, 0, utf8str,
73       -1, NULL, 0) >= utf16buf_len[idx]) {
74     /*
75      * Double the buffer's size if it's not big enough.
76      * The size of the buffer starts at 128, so doubling its size
77      * adds at least another 128 bytes, which is more than enough
78      * for one more character plus a terminating '\0'.
79      */
80     utf16buf_len[idx] *= 2;
81     utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
82   }
83
84   if (MultiByteToWideChar(CP_UTF8, 0, utf8str,
85       -1, utf16buf[idx], utf16buf_len[idx]) == 0)
86     return NULL;
87
88   return utf16buf[idx];
89 }
90
91 void
92 utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt, ...)
93 {
94     va_list ap;
95     gchar* dst;
96
97     va_start(ap,fmt);
98     dst = g_strdup_vprintf(fmt, ap);
99     va_end(ap);
100
101     _snwprintf(utf16buf, utf16buf_len, _T("%s"), utf_8to16(dst));
102
103     g_free(dst);
104 }
105
106 /* Convert from UTF-16 to UTF-8. */
107 gchar *
108 utf_16to8(const wchar_t *utf16str)
109 {
110   static gchar *utf8buf[3];
111   static int utf8buf_len[3];
112   static int idx;
113
114   if (utf16str == NULL)
115     return NULL;
116
117   idx = (idx + 1) % 3;
118
119   /*
120    * Allocate the buffer if it's not already allocated.
121    */
122   if (utf8buf[idx] == NULL) {
123     utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
124     utf8buf[idx] = g_malloc(utf8buf_len[idx]);
125   }
126
127   while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
128       NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
129     /*
130      * Double the buffer's size if it's not big enough.
131      * The size of the buffer starts at 128, so doubling its size
132      * adds at least another 128 bytes, which is more than enough
133      * for one more character plus a terminating '\0'.
134      */
135     utf8buf_len[idx] *= 2;
136     utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
137   }
138
139   if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
140       utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
141     return NULL;
142
143   return utf8buf[idx];
144 }
145
146 /* Convert our argument list from UTF-16 to UTF-8. */
147 void
148 arg_list_utf_16to8(int argc, char *argv[]) {
149   LPWSTR              *wc_argv;
150   int                  wc_argc, i;
151
152   /* Convert our arg list to UTF-8. */
153   wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc);
154   if (wc_argv && wc_argc == argc) {
155     for (i = 0; i < argc; i++) {
156       argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
157     }
158   } /* XXX else bail because something is horribly, horribly wrong? */
159 }