80323a53aa2a1b3b1c782b5a3500530e0d2c0a9a
[metze/wireshark/wip.git] / wsutil / unicode-utils.c
1 /* unicode-utils.c
2  * Unicode utility routines
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2006 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <config.h>
12
13 #include "unicode-utils.h"
14
15 int
16 ws_utf8_char_len(guint8 ch)
17 {
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;
24   else            return  1;
25 }
26
27
28 #ifdef _WIN32
29
30 #include <shellapi.h>
31 #include <strsafe.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 const 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   StringCchPrintf(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   LocalFree(wc_argv);
160 }
161
162 #endif
163
164 /*
165  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
166  *
167  * Local Variables:
168  * c-basic-offset: 2
169  * tab-width: 8
170  * indent-tabs-mode: nil
171  * End:
172  *
173  * ex: set shiftwidth=2 tabstop=8 expandtab:
174  * :indentSize=2:tabSize=8:noTabs=true:
175  */