/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */1/*2* Copyright (c) 1993, 1994, 1995, 1996, 19973* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. All advertising materials mentioning features or use of this software14* must display the following acknowledgement:15* This product includes software developed by the Computer Systems16* Engineering Group at Lawrence Berkeley Laboratory.17* 4. Neither the name of the University nor of the Laboratory may be used18* to endorse or promote products derived from this software without19* specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#ifdef _WIN3235#include <stdio.h>36#include <errno.h>3738#include <pcap/pcap.h> /* Needed for PCAP_ERRBUF_SIZE */3940#include "charconv.h"4142wchar_t *43cp_to_utf_16le(UINT codepage, const char *cp_string, DWORD flags)44{45int utf16le_len;46wchar_t *utf16le_string;4748/*49* Map from the specified code page to UTF-16LE.50* First, find out how big a buffer we'll need.51*/52utf16le_len = MultiByteToWideChar(codepage, flags, cp_string, -1,53NULL, 0);54if (utf16le_len == 0) {55/*56* Error. Fail with EINVAL.57*/58errno = EINVAL;59return (NULL);60}6162/*63* Now attempt to allocate a buffer for that.64*/65utf16le_string = malloc(utf16le_len * sizeof (wchar_t));66if (utf16le_string == NULL) {67/*68* Not enough memory; assume errno has been69* set, and fail.70*/71return (NULL);72}7374/*75* Now convert.76*/77utf16le_len = MultiByteToWideChar(codepage, flags, cp_string, -1,78utf16le_string, utf16le_len);79if (utf16le_len == 0) {80/*81* Error. Fail with EINVAL.82* XXX - should this ever happen, given that83* we already ran the string through84* MultiByteToWideChar() to find out how big85* a buffer we needed?86*/87free(utf16le_string);88errno = EINVAL;89return (NULL);90}91return (utf16le_string);92}9394char *95utf_16le_to_cp(UINT codepage, const wchar_t *utf16le_string)96{97int cp_len;98char *cp_string;99100/*101* Map from UTF-16LE to the specified code page.102* First, find out how big a buffer we'll need.103* We convert composite characters to precomposed characters,104* as that's what Windows expects.105*/106cp_len = WideCharToMultiByte(codepage, WC_COMPOSITECHECK,107utf16le_string, -1, NULL, 0, NULL, NULL);108if (cp_len == 0) {109/*110* Error. Fail with EINVAL.111*/112errno = EINVAL;113return (NULL);114}115116/*117* Now attempt to allocate a buffer for that.118*/119cp_string = malloc(cp_len * sizeof (char));120if (cp_string == NULL) {121/*122* Not enough memory; assume errno has been123* set, and fail.124*/125return (NULL);126}127128/*129* Now convert.130*/131cp_len = WideCharToMultiByte(codepage, WC_COMPOSITECHECK,132utf16le_string, -1, cp_string, cp_len, NULL, NULL);133if (cp_len == 0) {134/*135* Error. Fail with EINVAL.136* XXX - should this ever happen, given that137* we already ran the string through138* WideCharToMultiByte() to find out how big139* a buffer we needed?140*/141free(cp_string);142errno = EINVAL;143return (NULL);144}145return (cp_string);146}147148/*149* Convert an error message string from UTF-8 to the local code page, as150* best we can.151*152* The buffer is assumed to be PCAP_ERRBUF_SIZE bytes long; we truncate153* if it doesn't fit.154*/155void156utf_8_to_acp_truncated(char *errbuf)157{158wchar_t *utf_16_errbuf;159int retval;160DWORD err;161162/*163* Do this by converting to UTF-16LE and then to the local164* code page. That means we get to use Microsoft's165* conversion routines, rather than having to understand166* all the code pages ourselves, *and* that this routine167* can convert in place.168*/169170/*171* Map from UTF-8 to UTF-16LE.172* First, find out how big a buffer we'll need.173* Convert any invalid characters to REPLACEMENT CHARACTER.174*/175utf_16_errbuf = cp_to_utf_16le(CP_UTF8, errbuf, 0);176if (utf_16_errbuf == NULL) {177/*178* Error. Give up.179*/180snprintf(errbuf, PCAP_ERRBUF_SIZE,181"Can't convert error string to the local code page");182return;183}184185/*186* Now, convert that to the local code page.187* Use the current thread's code page. For unconvertible188* characters, let it pick the "best fit" character.189*190* XXX - we'd like some way to do what utf_16le_to_utf_8_truncated()191* does if the buffer isn't big enough, but we don't want to have192* to handle all local code pages ourselves; doing so requires193* knowledge of all those code pages, including knowledge of how194* characters are formed in those code pages so that we can avoid195* cutting a multi-byte character into pieces.196*197* Converting to an un-truncated string using Windows APIs, and198* then copying to the buffer, still requires knowledge of how199* characters are formed in the target code page.200*/201retval = WideCharToMultiByte(CP_THREAD_ACP, 0, utf_16_errbuf, -1,202errbuf, PCAP_ERRBUF_SIZE, NULL, NULL);203if (retval == 0) {204err = GetLastError();205free(utf_16_errbuf);206if (err == ERROR_INSUFFICIENT_BUFFER)207snprintf(errbuf, PCAP_ERRBUF_SIZE,208"The error string, in the local code page, didn't fit in the buffer");209else210snprintf(errbuf, PCAP_ERRBUF_SIZE,211"Can't convert error string to the local code page");212return;213}214free(utf_16_errbuf);215}216#endif217218219