Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / epan / 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 #ifdef _WIN32
26
27 #include <glib.h>
28 #include "unicode-utils.h"
29
30 #include <windows.h>
31 #include <tchar.h>
32 #include <wchar.h>
33
34 /** @file
35  * Unicode utilities (internal interface)
36  *
37  * We define UNICODE and _UNICODE under Windows.  This means that
38  * Windows SDK routines expect UTF-16 strings, in contrast to newer
39  * versions of Glib and GTK+ which expect UTF-8.  This module provides
40  * convenience routines for converting between UTF-8 and UTF-16.
41  */
42
43 #define INITIAL_UTFBUF_SIZE 128
44
45 /*
46  * XXX - Should we use g_utf8_to_utf16() and g_utf16_to_utf8()
47  * instead?  The goal of the functions below was to provide simple
48  * wrappers for UTF-8 <-> UTF-16 conversion without making the
49  * caller worry about freeing up memory afterward.
50  */
51
52 /* Convert from UTF-8 to UTF-16. */
53 wchar_t * utf_8to16(const char *utf8str) {
54   static wchar_t *utf16buf[3];
55   static int utf16buf_len[3];
56   static int idx;
57
58   if (utf8str == NULL)
59     return NULL;
60
61   idx = (idx + 1) % 3;
62
63   /*
64    * Allocate the buffer if it's not already allocated.
65    */
66   if (utf16buf[idx] == NULL) {
67     utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
68     utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
69   }
70
71   while (MultiByteToWideChar(CP_UTF8, 0, utf8str,
72       -1, NULL, 0) >= utf16buf_len[idx]) {
73     /*
74      * Double the buffer's size if it's not big enough.
75      * The size of the buffer starts at 128, so doubling its size
76      * adds at least another 128 bytes, which is more than enough
77      * for one more character plus a terminating '\0'.
78      */
79     utf16buf_len[idx] *= 2;
80     utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
81   }
82
83   if (MultiByteToWideChar(CP_UTF8, 0, utf8str,
84       -1, utf16buf[idx], utf16buf_len[idx]) == 0)
85     return NULL;
86
87   return utf16buf[idx];
88 }
89
90 /* Convert from UTF-16 to UTF-8. */
91 gchar * utf_16to8(const wchar_t *utf16str) {
92   static gchar *utf8buf[3];
93   static int utf8buf_len[3];
94   static int idx;
95
96   if (utf16str == NULL)
97     return NULL;
98
99   idx = (idx + 1) % 3;
100
101   /*
102    * Allocate the buffer if it's not already allocated.
103    */
104   if (utf8buf[idx] == NULL) {
105     utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
106     utf8buf[idx] = g_malloc(utf8buf_len[idx]);
107   }
108
109   while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
110       NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
111     /*
112      * Double the buffer's size if it's not big enough.
113      * The size of the buffer starts at 128, so doubling its size
114      * adds at least another 128 bytes, which is more than enough
115      * for one more character plus a terminating '\0'.
116      */
117     utf8buf_len[idx] *= 2;
118     utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
119   }
120
121   if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
122       utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
123     return NULL;
124
125   return utf8buf[idx];
126 }
127
128 #endif