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