2 * Unicode utility routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2006 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #error "This is only for Windows"
29 #include "unicode-utils.h"
34 * Unicode utilities (internal interface)
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.
42 #define INITIAL_UTFBUF_SIZE 128
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.
51 /* Convert from UTF-8 to UTF-16. */
53 utf_8to16(const char *utf8str
)
55 static wchar_t *utf16buf
[3];
56 static int utf16buf_len
[3];
65 * Allocate the buffer if it's not already allocated.
67 if (utf16buf
[idx
] == NULL
) {
68 utf16buf_len
[idx
] = INITIAL_UTFBUF_SIZE
;
69 utf16buf
[idx
] = g_malloc(utf16buf_len
[idx
] * sizeof(wchar_t));
72 while (MultiByteToWideChar(CP_UTF8
, 0, utf8str
,
73 -1, NULL
, 0) >= utf16buf_len
[idx
]) {
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'.
80 utf16buf_len
[idx
] *= 2;
81 utf16buf
[idx
] = g_realloc(utf16buf
[idx
], utf16buf_len
[idx
] * sizeof(wchar_t));
84 if (MultiByteToWideChar(CP_UTF8
, 0, utf8str
,
85 -1, utf16buf
[idx
], utf16buf_len
[idx
]) == 0)
92 utf_8to16_snprintf(TCHAR
*utf16buf
, gint utf16buf_len
, const gchar
* fmt
, ...)
98 dst
= g_strdup_vprintf(fmt
, ap
);
101 _snwprintf(utf16buf
, utf16buf_len
, _T("%s"), utf_8to16(dst
));
106 /* Convert from UTF-16 to UTF-8. */
108 utf_16to8(const wchar_t *utf16str
)
110 static gchar
*utf8buf
[3];
111 static int utf8buf_len
[3];
114 if (utf16str
== NULL
)
120 * Allocate the buffer if it's not already allocated.
122 if (utf8buf
[idx
] == NULL
) {
123 utf8buf_len
[idx
] = INITIAL_UTFBUF_SIZE
;
124 utf8buf
[idx
] = g_malloc(utf8buf_len
[idx
]);
127 while (WideCharToMultiByte(CP_UTF8
, 0, utf16str
, -1,
128 NULL
, 0, NULL
, NULL
) >= utf8buf_len
[idx
]) {
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'.
135 utf8buf_len
[idx
] *= 2;
136 utf8buf
[idx
] = g_realloc(utf8buf
[idx
], utf8buf_len
[idx
]);
139 if (WideCharToMultiByte(CP_UTF8
, 0, utf16str
, -1,
140 utf8buf
[idx
], utf8buf_len
[idx
], NULL
, NULL
) == 0)
146 /* Convert our argument list from UTF-16 to UTF-8. */
148 arg_list_utf_16to8(int argc
, char *argv
[]) {
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
);
158 } /* XXX else bail because something is horribly, horribly wrong? */