2 * Unicode utility routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2006 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "unicode-utils.h"
16 ws_utf8_char_len(guint8 ch)
18 if (ch >= 0xfe) return -1;
19 if (ch >= 0xfc) return 6;
20 if (ch >= 0xf8) return 5;
21 if (ch >= 0xf0) return 4;
22 if (ch >= 0xe0) return 3;
23 if (ch >= 0xc0) return 2;
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 StringCchPrintf(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? */
165 * Editor modelines - http://www.wireshark.org/tools/modelines.html
170 * indent-tabs-mode: nil
173 * ex: set shiftwidth=2 tabstop=8 expandtab:
174 * :indentSize=2:tabSize=8:noTabs=true: