1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1993, 1994, 1995, 1996, 1997
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <pcap/pcap.h> /* Needed for PCAP_ERRBUF_SIZE */
44 cp_to_utf_16le(UINT codepage
, const char *cp_string
, DWORD flags
)
47 wchar_t *utf16le_string
;
50 * Map from the specified code page to UTF-16LE.
51 * First, find out how big a buffer we'll need.
53 utf16le_len
= MultiByteToWideChar(codepage
, flags
, cp_string
, -1,
55 if (utf16le_len
== 0) {
57 * Error. Fail with EINVAL.
64 * Now attempt to allocate a buffer for that.
66 utf16le_string
= malloc(utf16le_len
* sizeof (wchar_t));
67 if (utf16le_string
== NULL
) {
69 * Not enough memory; assume errno has been
78 utf16le_len
= MultiByteToWideChar(codepage
, flags
, cp_string
, -1,
79 utf16le_string
, utf16le_len
);
80 if (utf16le_len
== 0) {
82 * Error. Fail with EINVAL.
83 * XXX - should this ever happen, given that
84 * we already ran the string through
85 * MultiByteToWideChar() to find out how big
92 return (utf16le_string
);
96 utf_16le_to_cp(UINT codepage
, const wchar_t *utf16le_string
)
102 * Map from UTF-16LE to the specified code page.
103 * First, find out how big a buffer we'll need.
104 * We convert composite characters to precomposed characters,
105 * as that's what Windows expects.
107 cp_len
= WideCharToMultiByte(codepage
, WC_COMPOSITECHECK
,
108 utf16le_string
, -1, NULL
, 0, NULL
, NULL
);
111 * Error. Fail with EINVAL.
118 * Now attempt to allocate a buffer for that.
120 cp_string
= malloc(cp_len
* sizeof (char));
121 if (cp_string
== NULL
) {
123 * Not enough memory; assume errno has been
132 cp_len
= WideCharToMultiByte(codepage
, WC_COMPOSITECHECK
,
133 utf16le_string
, -1, cp_string
, cp_len
, NULL
, NULL
);
136 * Error. Fail with EINVAL.
137 * XXX - should this ever happen, given that
138 * we already ran the string through
139 * WideCharToMultiByte() to find out how big
140 * a buffer we needed?
150 * Convert an error message string from UTF-8 to the local code page, as
153 * The buffer is assumed to be PCAP_ERRBUF_SIZE bytes long; we truncate
157 utf_8_to_acp_truncated(char *errbuf
)
159 wchar_t *utf_16_errbuf
;
164 * Do this by converting to UTF-16LE and then to the local
165 * code page. That means we get to use Microsoft's
166 * conversion routines, rather than having to understand
167 * all the code pages ourselves, *and* that this routine
168 * can convert in place.
172 * Map from UTF-8 to UTF-16LE.
173 * First, find out how big a buffer we'll need.
174 * Convert any invalid characters to REPLACEMENT CHARACTER.
176 utf_16_errbuf
= cp_to_utf_16le(CP_UTF8
, errbuf
, 0);
177 if (utf_16_errbuf
== NULL
) {
181 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
182 "Can't convert error string to the local code page");
187 * Now, convert that to the local code page.
188 * Use the current thread's code page. For unconvertible
189 * characters, let it pick the "best fit" character.
191 * XXX - we'd like some way to do what utf_16le_to_utf_8_truncated()
192 * does if the buffer isn't big enough, but we don't want to have
193 * to handle all local code pages ourselves; doing so requires
194 * knowledge of all those code pages, including knowledge of how
195 * characters are formed in those code pages so that we can avoid
196 * cutting a multi-byte character into pieces.
198 * Converting to an un-truncated string using Windows APIs, and
199 * then copying to the buffer, still requires knowledge of how
200 * characters are formed in the target code page.
202 retval
= WideCharToMultiByte(CP_THREAD_ACP
, 0, utf_16_errbuf
, -1,
203 errbuf
, PCAP_ERRBUF_SIZE
, NULL
, NULL
);
205 err
= GetLastError();
207 if (err
== ERROR_INSUFFICIENT_BUFFER
)
208 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
209 "The error string, in the local code page, didn't fit in the buffer");
211 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
212 "Can't convert error string to the local code page");