1*8b26181fSAndroid Build Coastguard Worker /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2*8b26181fSAndroid Build Coastguard Worker /*
3*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1993, 1994, 1995, 1996, 1997
4*8b26181fSAndroid Build Coastguard Worker * The Regents of the University of California. All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker *
6*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker * are met:
9*8b26181fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
10*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
11*8b26181fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
12*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
13*8b26181fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
14*8b26181fSAndroid Build Coastguard Worker * 3. All advertising materials mentioning features or use of this software
15*8b26181fSAndroid Build Coastguard Worker * must display the following acknowledgement:
16*8b26181fSAndroid Build Coastguard Worker * This product includes software developed by the Computer Systems
17*8b26181fSAndroid Build Coastguard Worker * Engineering Group at Lawrence Berkeley Laboratory.
18*8b26181fSAndroid Build Coastguard Worker * 4. Neither the name of the University nor of the Laboratory may be used
19*8b26181fSAndroid Build Coastguard Worker * to endorse or promote products derived from this software without
20*8b26181fSAndroid Build Coastguard Worker * specific prior written permission.
21*8b26181fSAndroid Build Coastguard Worker *
22*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*8b26181fSAndroid Build Coastguard Worker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*8b26181fSAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*8b26181fSAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*8b26181fSAndroid Build Coastguard Worker * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*8b26181fSAndroid Build Coastguard Worker * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*8b26181fSAndroid Build Coastguard Worker * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*8b26181fSAndroid Build Coastguard Worker * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*8b26181fSAndroid Build Coastguard Worker * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*8b26181fSAndroid Build Coastguard Worker * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*8b26181fSAndroid Build Coastguard Worker * SUCH DAMAGE.
33*8b26181fSAndroid Build Coastguard Worker */
34*8b26181fSAndroid Build Coastguard Worker
35*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
36*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
37*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
38*8b26181fSAndroid Build Coastguard Worker
39*8b26181fSAndroid Build Coastguard Worker #include <pcap/pcap.h> /* Needed for PCAP_ERRBUF_SIZE */
40*8b26181fSAndroid Build Coastguard Worker
41*8b26181fSAndroid Build Coastguard Worker #include "charconv.h"
42*8b26181fSAndroid Build Coastguard Worker
43*8b26181fSAndroid Build Coastguard Worker wchar_t *
cp_to_utf_16le(UINT codepage,const char * cp_string,DWORD flags)44*8b26181fSAndroid Build Coastguard Worker cp_to_utf_16le(UINT codepage, const char *cp_string, DWORD flags)
45*8b26181fSAndroid Build Coastguard Worker {
46*8b26181fSAndroid Build Coastguard Worker int utf16le_len;
47*8b26181fSAndroid Build Coastguard Worker wchar_t *utf16le_string;
48*8b26181fSAndroid Build Coastguard Worker
49*8b26181fSAndroid Build Coastguard Worker /*
50*8b26181fSAndroid Build Coastguard Worker * Map from the specified code page to UTF-16LE.
51*8b26181fSAndroid Build Coastguard Worker * First, find out how big a buffer we'll need.
52*8b26181fSAndroid Build Coastguard Worker */
53*8b26181fSAndroid Build Coastguard Worker utf16le_len = MultiByteToWideChar(codepage, flags, cp_string, -1,
54*8b26181fSAndroid Build Coastguard Worker NULL, 0);
55*8b26181fSAndroid Build Coastguard Worker if (utf16le_len == 0) {
56*8b26181fSAndroid Build Coastguard Worker /*
57*8b26181fSAndroid Build Coastguard Worker * Error. Fail with EINVAL.
58*8b26181fSAndroid Build Coastguard Worker */
59*8b26181fSAndroid Build Coastguard Worker errno = EINVAL;
60*8b26181fSAndroid Build Coastguard Worker return (NULL);
61*8b26181fSAndroid Build Coastguard Worker }
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker /*
64*8b26181fSAndroid Build Coastguard Worker * Now attempt to allocate a buffer for that.
65*8b26181fSAndroid Build Coastguard Worker */
66*8b26181fSAndroid Build Coastguard Worker utf16le_string = malloc(utf16le_len * sizeof (wchar_t));
67*8b26181fSAndroid Build Coastguard Worker if (utf16le_string == NULL) {
68*8b26181fSAndroid Build Coastguard Worker /*
69*8b26181fSAndroid Build Coastguard Worker * Not enough memory; assume errno has been
70*8b26181fSAndroid Build Coastguard Worker * set, and fail.
71*8b26181fSAndroid Build Coastguard Worker */
72*8b26181fSAndroid Build Coastguard Worker return (NULL);
73*8b26181fSAndroid Build Coastguard Worker }
74*8b26181fSAndroid Build Coastguard Worker
75*8b26181fSAndroid Build Coastguard Worker /*
76*8b26181fSAndroid Build Coastguard Worker * Now convert.
77*8b26181fSAndroid Build Coastguard Worker */
78*8b26181fSAndroid Build Coastguard Worker utf16le_len = MultiByteToWideChar(codepage, flags, cp_string, -1,
79*8b26181fSAndroid Build Coastguard Worker utf16le_string, utf16le_len);
80*8b26181fSAndroid Build Coastguard Worker if (utf16le_len == 0) {
81*8b26181fSAndroid Build Coastguard Worker /*
82*8b26181fSAndroid Build Coastguard Worker * Error. Fail with EINVAL.
83*8b26181fSAndroid Build Coastguard Worker * XXX - should this ever happen, given that
84*8b26181fSAndroid Build Coastguard Worker * we already ran the string through
85*8b26181fSAndroid Build Coastguard Worker * MultiByteToWideChar() to find out how big
86*8b26181fSAndroid Build Coastguard Worker * a buffer we needed?
87*8b26181fSAndroid Build Coastguard Worker */
88*8b26181fSAndroid Build Coastguard Worker free(utf16le_string);
89*8b26181fSAndroid Build Coastguard Worker errno = EINVAL;
90*8b26181fSAndroid Build Coastguard Worker return (NULL);
91*8b26181fSAndroid Build Coastguard Worker }
92*8b26181fSAndroid Build Coastguard Worker return (utf16le_string);
93*8b26181fSAndroid Build Coastguard Worker }
94*8b26181fSAndroid Build Coastguard Worker
95*8b26181fSAndroid Build Coastguard Worker char *
utf_16le_to_cp(UINT codepage,const wchar_t * utf16le_string)96*8b26181fSAndroid Build Coastguard Worker utf_16le_to_cp(UINT codepage, const wchar_t *utf16le_string)
97*8b26181fSAndroid Build Coastguard Worker {
98*8b26181fSAndroid Build Coastguard Worker int cp_len;
99*8b26181fSAndroid Build Coastguard Worker char *cp_string;
100*8b26181fSAndroid Build Coastguard Worker
101*8b26181fSAndroid Build Coastguard Worker /*
102*8b26181fSAndroid Build Coastguard Worker * Map from UTF-16LE to the specified code page.
103*8b26181fSAndroid Build Coastguard Worker * First, find out how big a buffer we'll need.
104*8b26181fSAndroid Build Coastguard Worker * We convert composite characters to precomposed characters,
105*8b26181fSAndroid Build Coastguard Worker * as that's what Windows expects.
106*8b26181fSAndroid Build Coastguard Worker */
107*8b26181fSAndroid Build Coastguard Worker cp_len = WideCharToMultiByte(codepage, WC_COMPOSITECHECK,
108*8b26181fSAndroid Build Coastguard Worker utf16le_string, -1, NULL, 0, NULL, NULL);
109*8b26181fSAndroid Build Coastguard Worker if (cp_len == 0) {
110*8b26181fSAndroid Build Coastguard Worker /*
111*8b26181fSAndroid Build Coastguard Worker * Error. Fail with EINVAL.
112*8b26181fSAndroid Build Coastguard Worker */
113*8b26181fSAndroid Build Coastguard Worker errno = EINVAL;
114*8b26181fSAndroid Build Coastguard Worker return (NULL);
115*8b26181fSAndroid Build Coastguard Worker }
116*8b26181fSAndroid Build Coastguard Worker
117*8b26181fSAndroid Build Coastguard Worker /*
118*8b26181fSAndroid Build Coastguard Worker * Now attempt to allocate a buffer for that.
119*8b26181fSAndroid Build Coastguard Worker */
120*8b26181fSAndroid Build Coastguard Worker cp_string = malloc(cp_len * sizeof (char));
121*8b26181fSAndroid Build Coastguard Worker if (cp_string == NULL) {
122*8b26181fSAndroid Build Coastguard Worker /*
123*8b26181fSAndroid Build Coastguard Worker * Not enough memory; assume errno has been
124*8b26181fSAndroid Build Coastguard Worker * set, and fail.
125*8b26181fSAndroid Build Coastguard Worker */
126*8b26181fSAndroid Build Coastguard Worker return (NULL);
127*8b26181fSAndroid Build Coastguard Worker }
128*8b26181fSAndroid Build Coastguard Worker
129*8b26181fSAndroid Build Coastguard Worker /*
130*8b26181fSAndroid Build Coastguard Worker * Now convert.
131*8b26181fSAndroid Build Coastguard Worker */
132*8b26181fSAndroid Build Coastguard Worker cp_len = WideCharToMultiByte(codepage, WC_COMPOSITECHECK,
133*8b26181fSAndroid Build Coastguard Worker utf16le_string, -1, cp_string, cp_len, NULL, NULL);
134*8b26181fSAndroid Build Coastguard Worker if (cp_len == 0) {
135*8b26181fSAndroid Build Coastguard Worker /*
136*8b26181fSAndroid Build Coastguard Worker * Error. Fail with EINVAL.
137*8b26181fSAndroid Build Coastguard Worker * XXX - should this ever happen, given that
138*8b26181fSAndroid Build Coastguard Worker * we already ran the string through
139*8b26181fSAndroid Build Coastguard Worker * WideCharToMultiByte() to find out how big
140*8b26181fSAndroid Build Coastguard Worker * a buffer we needed?
141*8b26181fSAndroid Build Coastguard Worker */
142*8b26181fSAndroid Build Coastguard Worker free(cp_string);
143*8b26181fSAndroid Build Coastguard Worker errno = EINVAL;
144*8b26181fSAndroid Build Coastguard Worker return (NULL);
145*8b26181fSAndroid Build Coastguard Worker }
146*8b26181fSAndroid Build Coastguard Worker return (cp_string);
147*8b26181fSAndroid Build Coastguard Worker }
148*8b26181fSAndroid Build Coastguard Worker
149*8b26181fSAndroid Build Coastguard Worker /*
150*8b26181fSAndroid Build Coastguard Worker * Convert an error message string from UTF-8 to the local code page, as
151*8b26181fSAndroid Build Coastguard Worker * best we can.
152*8b26181fSAndroid Build Coastguard Worker *
153*8b26181fSAndroid Build Coastguard Worker * The buffer is assumed to be PCAP_ERRBUF_SIZE bytes long; we truncate
154*8b26181fSAndroid Build Coastguard Worker * if it doesn't fit.
155*8b26181fSAndroid Build Coastguard Worker */
156*8b26181fSAndroid Build Coastguard Worker void
utf_8_to_acp_truncated(char * errbuf)157*8b26181fSAndroid Build Coastguard Worker utf_8_to_acp_truncated(char *errbuf)
158*8b26181fSAndroid Build Coastguard Worker {
159*8b26181fSAndroid Build Coastguard Worker wchar_t *utf_16_errbuf;
160*8b26181fSAndroid Build Coastguard Worker int retval;
161*8b26181fSAndroid Build Coastguard Worker DWORD err;
162*8b26181fSAndroid Build Coastguard Worker
163*8b26181fSAndroid Build Coastguard Worker /*
164*8b26181fSAndroid Build Coastguard Worker * Do this by converting to UTF-16LE and then to the local
165*8b26181fSAndroid Build Coastguard Worker * code page. That means we get to use Microsoft's
166*8b26181fSAndroid Build Coastguard Worker * conversion routines, rather than having to understand
167*8b26181fSAndroid Build Coastguard Worker * all the code pages ourselves, *and* that this routine
168*8b26181fSAndroid Build Coastguard Worker * can convert in place.
169*8b26181fSAndroid Build Coastguard Worker */
170*8b26181fSAndroid Build Coastguard Worker
171*8b26181fSAndroid Build Coastguard Worker /*
172*8b26181fSAndroid Build Coastguard Worker * Map from UTF-8 to UTF-16LE.
173*8b26181fSAndroid Build Coastguard Worker * First, find out how big a buffer we'll need.
174*8b26181fSAndroid Build Coastguard Worker * Convert any invalid characters to REPLACEMENT CHARACTER.
175*8b26181fSAndroid Build Coastguard Worker */
176*8b26181fSAndroid Build Coastguard Worker utf_16_errbuf = cp_to_utf_16le(CP_UTF8, errbuf, 0);
177*8b26181fSAndroid Build Coastguard Worker if (utf_16_errbuf == NULL) {
178*8b26181fSAndroid Build Coastguard Worker /*
179*8b26181fSAndroid Build Coastguard Worker * Error. Give up.
180*8b26181fSAndroid Build Coastguard Worker */
181*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
182*8b26181fSAndroid Build Coastguard Worker "Can't convert error string to the local code page");
183*8b26181fSAndroid Build Coastguard Worker return;
184*8b26181fSAndroid Build Coastguard Worker }
185*8b26181fSAndroid Build Coastguard Worker
186*8b26181fSAndroid Build Coastguard Worker /*
187*8b26181fSAndroid Build Coastguard Worker * Now, convert that to the local code page.
188*8b26181fSAndroid Build Coastguard Worker * Use the current thread's code page. For unconvertable
189*8b26181fSAndroid Build Coastguard Worker * characters, let it pick the "best fit" character.
190*8b26181fSAndroid Build Coastguard Worker *
191*8b26181fSAndroid Build Coastguard Worker * XXX - we'd like some way to do what utf_16le_to_utf_8_truncated()
192*8b26181fSAndroid Build Coastguard Worker * does if the buffer isn't big enough, but we don't want to have
193*8b26181fSAndroid Build Coastguard Worker * to handle all local code pages ourselves; doing so requires
194*8b26181fSAndroid Build Coastguard Worker * knowledge of all those code pages, including knowledge of how
195*8b26181fSAndroid Build Coastguard Worker * characters are formed in thoe code pages so that we can avoid
196*8b26181fSAndroid Build Coastguard Worker * cutting a multi-byte character into pieces.
197*8b26181fSAndroid Build Coastguard Worker *
198*8b26181fSAndroid Build Coastguard Worker * Converting to an un-truncated string using Windows APIs, and
199*8b26181fSAndroid Build Coastguard Worker * then copying to the buffer, still requires knowledge of how
200*8b26181fSAndroid Build Coastguard Worker * characters are formed in the target code page.
201*8b26181fSAndroid Build Coastguard Worker */
202*8b26181fSAndroid Build Coastguard Worker retval = WideCharToMultiByte(CP_THREAD_ACP, 0, utf_16_errbuf, -1,
203*8b26181fSAndroid Build Coastguard Worker errbuf, PCAP_ERRBUF_SIZE, NULL, NULL);
204*8b26181fSAndroid Build Coastguard Worker if (retval == 0) {
205*8b26181fSAndroid Build Coastguard Worker err = GetLastError();
206*8b26181fSAndroid Build Coastguard Worker free(utf_16_errbuf);
207*8b26181fSAndroid Build Coastguard Worker if (err == ERROR_INSUFFICIENT_BUFFER)
208*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
209*8b26181fSAndroid Build Coastguard Worker "The error string, in the local code page, didn't fit in the buffer");
210*8b26181fSAndroid Build Coastguard Worker else
211*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
212*8b26181fSAndroid Build Coastguard Worker "Can't convert error string to the local code page");
213*8b26181fSAndroid Build Coastguard Worker return;
214*8b26181fSAndroid Build Coastguard Worker }
215*8b26181fSAndroid Build Coastguard Worker free(utf_16_errbuf);
216*8b26181fSAndroid Build Coastguard Worker }
217*8b26181fSAndroid Build Coastguard Worker #endif
218