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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #error "This is only for Windows"
29 #include "unicode-utils.h"
32 * Unicode utilities (internal interface)
34 * We define UNICODE and _UNICODE under Windows. This means that
35 * Windows SDK routines expect UTF-16 strings, in contrast to newer
36 * versions of Glib and GTK+ which expect UTF-8. This module provides
37 * convenience routines for converting between UTF-8 and UTF-16.
40 #define INITIAL_UTFBUF_SIZE 128
43 * XXX - Should we use g_utf8_to_utf16() and g_utf16_to_utf8()
44 * instead? The goal of the functions below was to provide simple
45 * wrappers for UTF-8 <-> UTF-16 conversion without making the
46 * caller worry about freeing up memory afterward.
49 /* Convert from UTF-8 to UTF-16. */
51 utf_8to16(const char *utf8str)
53 static wchar_t *utf16buf[3];
54 static int utf16buf_len[3];
63 * Allocate the buffer if it's not already allocated.
65 if (utf16buf[idx] == NULL) {
66 utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
67 utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
70 while (MultiByteToWideChar(CP_UTF8, 0, utf8str,
71 -1, NULL, 0) >= utf16buf_len[idx]) {
73 * Double the buffer's size if it's not big enough.
74 * The size of the buffer starts at 128, so doubling its size
75 * adds at least another 128 bytes, which is more than enough
76 * for one more character plus a terminating '\0'.
78 utf16buf_len[idx] *= 2;
79 utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
82 if (MultiByteToWideChar(CP_UTF8, 0, utf8str,
83 -1, utf16buf[idx], utf16buf_len[idx]) == 0)
90 utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt, ...)
96 dst = g_strdup_vprintf(fmt, ap);
99 _snwprintf(utf16buf, utf16buf_len, _T("%s"), utf_8to16(dst));
104 /* Convert from UTF-16 to UTF-8. */
106 utf_16to8(const wchar_t *utf16str)
108 static gchar *utf8buf[3];
109 static int utf8buf_len[3];
112 if (utf16str == NULL)
118 * Allocate the buffer if it's not already allocated.
120 if (utf8buf[idx] == NULL) {
121 utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
122 utf8buf[idx] = g_malloc(utf8buf_len[idx]);
125 while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
126 NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
128 * Double the buffer's size if it's not big enough.
129 * The size of the buffer starts at 128, so doubling its size
130 * adds at least another 128 bytes, which is more than enough
131 * for one more character plus a terminating '\0'.
133 utf8buf_len[idx] *= 2;
134 utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
137 if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
138 utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)