1b00ab754SHans Petter Selasky /*
2b00ab754SHans Petter Selasky * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3b00ab754SHans Petter Selasky * The Regents of the University of California. All rights reserved.
4b00ab754SHans Petter Selasky *
5b00ab754SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
6b00ab754SHans Petter Selasky * modification, are permitted provided that the following conditions
7b00ab754SHans Petter Selasky * are met:
8b00ab754SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
9b00ab754SHans Petter Selasky * notice, this list of conditions and the following disclaimer.
10b00ab754SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
11b00ab754SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
12b00ab754SHans Petter Selasky * documentation and/or other materials provided with the distribution.
13b00ab754SHans Petter Selasky * 3. All advertising materials mentioning features or use of this software
14b00ab754SHans Petter Selasky * must display the following acknowledgement:
15b00ab754SHans Petter Selasky * This product includes software developed by the Computer Systems
16b00ab754SHans Petter Selasky * Engineering Group at Lawrence Berkeley Laboratory.
17b00ab754SHans Petter Selasky * 4. Neither the name of the University nor of the Laboratory may be used
18b00ab754SHans Petter Selasky * to endorse or promote products derived from this software without
19b00ab754SHans Petter Selasky * specific prior written permission.
20b00ab754SHans Petter Selasky *
21b00ab754SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22b00ab754SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b00ab754SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b00ab754SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25b00ab754SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b00ab754SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b00ab754SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b00ab754SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b00ab754SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b00ab754SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b00ab754SHans Petter Selasky * SUCH DAMAGE.
32b00ab754SHans Petter Selasky */
33b00ab754SHans Petter Selasky
34b00ab754SHans Petter Selasky /*
35b00ab754SHans Petter Selasky * Utilities for message formatting used both by libpcap and rpcapd.
36b00ab754SHans Petter Selasky */
37b00ab754SHans Petter Selasky
38b00ab754SHans Petter Selasky #include <config.h>
39b00ab754SHans Petter Selasky
40b00ab754SHans Petter Selasky #include "ftmacros.h"
41b00ab754SHans Petter Selasky
42b00ab754SHans Petter Selasky #include <stddef.h>
43b00ab754SHans Petter Selasky #include <stdarg.h>
44b00ab754SHans Petter Selasky #include <stdio.h>
45b00ab754SHans Petter Selasky #include <string.h>
46b00ab754SHans Petter Selasky #include <errno.h>
47b00ab754SHans Petter Selasky
486f9cba8fSJoseph Mingrone #include "pcap-int.h"
49b00ab754SHans Petter Selasky
50b00ab754SHans Petter Selasky #include "portability.h"
51b00ab754SHans Petter Selasky
52b00ab754SHans Petter Selasky #include "fmtutils.h"
53b00ab754SHans Petter Selasky
546f9cba8fSJoseph Mingrone #ifdef _WIN32
556f9cba8fSJoseph Mingrone #include "charconv.h"
566f9cba8fSJoseph Mingrone #endif
576f9cba8fSJoseph Mingrone
586f9cba8fSJoseph Mingrone /*
596f9cba8fSJoseph Mingrone * Set the encoding.
606f9cba8fSJoseph Mingrone */
616f9cba8fSJoseph Mingrone #ifdef _WIN32
626f9cba8fSJoseph Mingrone /*
63*afdbf109SJoseph Mingrone * True if we should use UTF-8.
646f9cba8fSJoseph Mingrone */
656f9cba8fSJoseph Mingrone static int use_utf_8;
666f9cba8fSJoseph Mingrone
676f9cba8fSJoseph Mingrone void
pcapint_fmt_set_encoding(unsigned int opts)68*afdbf109SJoseph Mingrone pcapint_fmt_set_encoding(unsigned int opts)
696f9cba8fSJoseph Mingrone {
706f9cba8fSJoseph Mingrone if (opts == PCAP_CHAR_ENC_UTF_8)
716f9cba8fSJoseph Mingrone use_utf_8 = 1;
726f9cba8fSJoseph Mingrone }
736f9cba8fSJoseph Mingrone #else
746f9cba8fSJoseph Mingrone void
pcapint_fmt_set_encoding(unsigned int opts _U_)75*afdbf109SJoseph Mingrone pcapint_fmt_set_encoding(unsigned int opts _U_)
766f9cba8fSJoseph Mingrone {
776f9cba8fSJoseph Mingrone /*
786f9cba8fSJoseph Mingrone * Nothing to do here.
796f9cba8fSJoseph Mingrone */
806f9cba8fSJoseph Mingrone }
816f9cba8fSJoseph Mingrone #endif
826f9cba8fSJoseph Mingrone
836f9cba8fSJoseph Mingrone #ifdef _WIN32
846f9cba8fSJoseph Mingrone /*
856f9cba8fSJoseph Mingrone * Convert a null-terminated UTF-16LE string to UTF-8, putting it into
866f9cba8fSJoseph Mingrone * a buffer starting at the specified location and stopping if we go
876f9cba8fSJoseph Mingrone * past the specified size. This will only put out complete UTF-8
886f9cba8fSJoseph Mingrone * sequences.
896f9cba8fSJoseph Mingrone *
906f9cba8fSJoseph Mingrone * We do this ourselves because Microsoft doesn't offer a "convert and
916f9cba8fSJoseph Mingrone * stop at a UTF-8 character boundary if we run out of space" routine.
926f9cba8fSJoseph Mingrone */
936f9cba8fSJoseph Mingrone #define IS_LEADING_SURROGATE(c) \
946f9cba8fSJoseph Mingrone ((c) >= 0xd800 && (c) < 0xdc00)
956f9cba8fSJoseph Mingrone #define IS_TRAILING_SURROGATE(c) \
966f9cba8fSJoseph Mingrone ((c) >= 0xdc00 && (c) < 0xe000)
976f9cba8fSJoseph Mingrone #define SURROGATE_VALUE(leading, trailing) \
986f9cba8fSJoseph Mingrone (((((leading) - 0xd800) << 10) | ((trailing) - 0xdc00)) + 0x10000)
996f9cba8fSJoseph Mingrone #define REPLACEMENT_CHARACTER 0x0FFFD
1006f9cba8fSJoseph Mingrone
1016f9cba8fSJoseph Mingrone static char *
utf_16le_to_utf_8_truncated(const wchar_t * utf_16,char * utf_8,size_t utf_8_len)1026f9cba8fSJoseph Mingrone utf_16le_to_utf_8_truncated(const wchar_t *utf_16, char *utf_8,
1036f9cba8fSJoseph Mingrone size_t utf_8_len)
1046f9cba8fSJoseph Mingrone {
1056f9cba8fSJoseph Mingrone wchar_t c, c2;
1066f9cba8fSJoseph Mingrone uint32_t uc;
1076f9cba8fSJoseph Mingrone
1086f9cba8fSJoseph Mingrone if (utf_8_len == 0) {
1096f9cba8fSJoseph Mingrone /*
1106f9cba8fSJoseph Mingrone * Not even enough room for a trailing '\0'.
1116f9cba8fSJoseph Mingrone * Don't put anything into the buffer.
1126f9cba8fSJoseph Mingrone */
1136f9cba8fSJoseph Mingrone return (utf_8);
1146f9cba8fSJoseph Mingrone }
1156f9cba8fSJoseph Mingrone
1166f9cba8fSJoseph Mingrone while ((c = *utf_16++) != '\0') {
1176f9cba8fSJoseph Mingrone if (IS_LEADING_SURROGATE(c)) {
1186f9cba8fSJoseph Mingrone /*
1196f9cba8fSJoseph Mingrone * Leading surrogate. Must be followed by
1206f9cba8fSJoseph Mingrone * a trailing surrogate.
1216f9cba8fSJoseph Mingrone */
1226f9cba8fSJoseph Mingrone c2 = *utf_16;
1236f9cba8fSJoseph Mingrone if (c2 == '\0') {
1246f9cba8fSJoseph Mingrone /*
1256f9cba8fSJoseph Mingrone * Oops, string ends with a lead
1266f9cba8fSJoseph Mingrone * surrogate. Try to drop in
1276f9cba8fSJoseph Mingrone * a REPLACEMENT CHARACTER, and
1286f9cba8fSJoseph Mingrone * don't move the string pointer,
1296f9cba8fSJoseph Mingrone * so on the next trip through
1306f9cba8fSJoseph Mingrone * the loop we grab the terminating
1316f9cba8fSJoseph Mingrone * '\0' and quit.
1326f9cba8fSJoseph Mingrone */
1336f9cba8fSJoseph Mingrone uc = REPLACEMENT_CHARACTER;
1346f9cba8fSJoseph Mingrone } else {
1356f9cba8fSJoseph Mingrone /*
1366f9cba8fSJoseph Mingrone * OK, we can consume this 2-octet
1376f9cba8fSJoseph Mingrone * value.
1386f9cba8fSJoseph Mingrone */
1396f9cba8fSJoseph Mingrone utf_16++;
1406f9cba8fSJoseph Mingrone if (IS_TRAILING_SURROGATE(c2)) {
1416f9cba8fSJoseph Mingrone /*
1426f9cba8fSJoseph Mingrone * Trailing surrogate.
1436f9cba8fSJoseph Mingrone * This calculation will,
1446f9cba8fSJoseph Mingrone * for c being a leading
1456f9cba8fSJoseph Mingrone * surrogate and c2 being
1466f9cba8fSJoseph Mingrone * a trailing surrogate,
1476f9cba8fSJoseph Mingrone * produce a value between
1486f9cba8fSJoseph Mingrone * 0x100000 and 0x10ffff,
1496f9cba8fSJoseph Mingrone * so it's always going to be
1506f9cba8fSJoseph Mingrone * a valid Unicode code point.
1516f9cba8fSJoseph Mingrone */
1526f9cba8fSJoseph Mingrone uc = SURROGATE_VALUE(c, c2);
1536f9cba8fSJoseph Mingrone } else {
1546f9cba8fSJoseph Mingrone /*
155*afdbf109SJoseph Mingrone * Not a trailing surrogate;
1566f9cba8fSJoseph Mingrone * try to drop in a
1576f9cba8fSJoseph Mingrone * REPLACEMENT CHARACTER.
1586f9cba8fSJoseph Mingrone */
1596f9cba8fSJoseph Mingrone uc = REPLACEMENT_CHARACTER;
1606f9cba8fSJoseph Mingrone }
1616f9cba8fSJoseph Mingrone }
1626f9cba8fSJoseph Mingrone } else {
1636f9cba8fSJoseph Mingrone /*
1646f9cba8fSJoseph Mingrone * Not a leading surrogate.
1656f9cba8fSJoseph Mingrone */
1666f9cba8fSJoseph Mingrone if (IS_TRAILING_SURROGATE(c)) {
1676f9cba8fSJoseph Mingrone /*
1686f9cba8fSJoseph Mingrone * Trailing surrogate without
1696f9cba8fSJoseph Mingrone * a preceding leading surrogate.
1706f9cba8fSJoseph Mingrone * Try to drop in a REPLACEMENT
1716f9cba8fSJoseph Mingrone * CHARACTER.
1726f9cba8fSJoseph Mingrone */
1736f9cba8fSJoseph Mingrone uc = REPLACEMENT_CHARACTER;
1746f9cba8fSJoseph Mingrone } else {
1756f9cba8fSJoseph Mingrone /*
1766f9cba8fSJoseph Mingrone * This is a valid BMP character;
1776f9cba8fSJoseph Mingrone * drop it in.
1786f9cba8fSJoseph Mingrone */
1796f9cba8fSJoseph Mingrone uc = c;
1806f9cba8fSJoseph Mingrone }
1816f9cba8fSJoseph Mingrone }
1826f9cba8fSJoseph Mingrone
1836f9cba8fSJoseph Mingrone /*
1846f9cba8fSJoseph Mingrone * OK, uc is a valid Unicode character; how
1856f9cba8fSJoseph Mingrone * many bytes worth of UTF-8 does it require?
1866f9cba8fSJoseph Mingrone */
1876f9cba8fSJoseph Mingrone if (uc < 0x0080) {
1886f9cba8fSJoseph Mingrone /* 1 byte. */
1896f9cba8fSJoseph Mingrone if (utf_8_len < 2) {
1906f9cba8fSJoseph Mingrone /*
1916f9cba8fSJoseph Mingrone * Not enough room for that byte
1926f9cba8fSJoseph Mingrone * plus a trailing '\0'.
1936f9cba8fSJoseph Mingrone */
1946f9cba8fSJoseph Mingrone break;
1956f9cba8fSJoseph Mingrone }
1966f9cba8fSJoseph Mingrone *utf_8++ = (char)uc;
1976f9cba8fSJoseph Mingrone utf_8_len--;
1986f9cba8fSJoseph Mingrone } else if (uc < 0x0800) {
1996f9cba8fSJoseph Mingrone /* 2 bytes. */
2006f9cba8fSJoseph Mingrone if (utf_8_len < 3) {
2016f9cba8fSJoseph Mingrone /*
2026f9cba8fSJoseph Mingrone * Not enough room for those bytes
2036f9cba8fSJoseph Mingrone * plus a trailing '\0'.
2046f9cba8fSJoseph Mingrone */
2056f9cba8fSJoseph Mingrone break;
2066f9cba8fSJoseph Mingrone }
2076f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 6) & 0x3F) | 0xC0;
2086f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
2096f9cba8fSJoseph Mingrone utf_8_len -= 2;
2106f9cba8fSJoseph Mingrone } else if (uc < 0x010000) {
2116f9cba8fSJoseph Mingrone /* 3 bytes. */
2126f9cba8fSJoseph Mingrone if (utf_8_len < 4) {
2136f9cba8fSJoseph Mingrone /*
2146f9cba8fSJoseph Mingrone * Not enough room for those bytes
2156f9cba8fSJoseph Mingrone * plus a trailing '\0'.
2166f9cba8fSJoseph Mingrone */
2176f9cba8fSJoseph Mingrone break;
2186f9cba8fSJoseph Mingrone }
2196f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 12) & 0x0F) | 0xE0;
2206f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 6) & 0x3F) | 0x80;
2216f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
2226f9cba8fSJoseph Mingrone utf_8_len -= 3;
2236f9cba8fSJoseph Mingrone } else {
2246f9cba8fSJoseph Mingrone /* 4 bytes. */
2256f9cba8fSJoseph Mingrone if (utf_8_len < 5) {
2266f9cba8fSJoseph Mingrone /*
2276f9cba8fSJoseph Mingrone * Not enough room for those bytes
2286f9cba8fSJoseph Mingrone * plus a trailing '\0'.
2296f9cba8fSJoseph Mingrone */
2306f9cba8fSJoseph Mingrone break;
2316f9cba8fSJoseph Mingrone }
2326f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 18) & 0x03) | 0xF0;
2336f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 12) & 0x3F) | 0x80;
2346f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 6) & 0x3F) | 0x80;
2356f9cba8fSJoseph Mingrone *utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
2366f9cba8fSJoseph Mingrone utf_8_len -= 3;
2376f9cba8fSJoseph Mingrone }
2386f9cba8fSJoseph Mingrone }
2396f9cba8fSJoseph Mingrone
2406f9cba8fSJoseph Mingrone /*
2416f9cba8fSJoseph Mingrone * OK, we have enough room for (at least) a trailing '\0'.
2426f9cba8fSJoseph Mingrone * (We started out with enough room, thanks to the test
2436f9cba8fSJoseph Mingrone * for a zero-length buffer at the beginning, and if
2446f9cba8fSJoseph Mingrone * there wasn't enough room for any character we wanted
2456f9cba8fSJoseph Mingrone * to put into the buffer *plus* a trailing '\0',
2466f9cba8fSJoseph Mingrone * we'd have quit before putting it into the buffer,
2476f9cba8fSJoseph Mingrone * and thus would have left enough room for the trailing
2486f9cba8fSJoseph Mingrone * '\0'.)
2496f9cba8fSJoseph Mingrone *
2506f9cba8fSJoseph Mingrone * Drop it in.
2516f9cba8fSJoseph Mingrone */
2526f9cba8fSJoseph Mingrone *utf_8 = '\0';
2536f9cba8fSJoseph Mingrone
2546f9cba8fSJoseph Mingrone /*
2556f9cba8fSJoseph Mingrone * Return a pointer to the terminating '\0', in case we
2566f9cba8fSJoseph Mingrone * want to drop something in after that.
2576f9cba8fSJoseph Mingrone */
2586f9cba8fSJoseph Mingrone return (utf_8);
2596f9cba8fSJoseph Mingrone }
2606f9cba8fSJoseph Mingrone #endif /* _WIN32 */
2616f9cba8fSJoseph Mingrone
262b00ab754SHans Petter Selasky /*
263b00ab754SHans Petter Selasky * Generate an error message based on a format, arguments, and an
264b00ab754SHans Petter Selasky * errno, with a message for the errno after the formatted output.
265b00ab754SHans Petter Selasky */
266b00ab754SHans Petter Selasky void
pcapint_fmt_errmsg_for_errno(char * errbuf,size_t errbuflen,int errnum,const char * fmt,...)267*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
268b00ab754SHans Petter Selasky const char *fmt, ...)
269b00ab754SHans Petter Selasky {
270b00ab754SHans Petter Selasky va_list ap;
2716f9cba8fSJoseph Mingrone
2726f9cba8fSJoseph Mingrone va_start(ap, fmt);
273*afdbf109SJoseph Mingrone pcapint_vfmt_errmsg_for_errno(errbuf, errbuflen, errnum, fmt, ap);
2746f9cba8fSJoseph Mingrone va_end(ap);
2756f9cba8fSJoseph Mingrone }
2766f9cba8fSJoseph Mingrone
2776f9cba8fSJoseph Mingrone void
pcapint_vfmt_errmsg_for_errno(char * errbuf,size_t errbuflen,int errnum,const char * fmt,va_list ap)278*afdbf109SJoseph Mingrone pcapint_vfmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
2796f9cba8fSJoseph Mingrone const char *fmt, va_list ap)
2806f9cba8fSJoseph Mingrone {
281b00ab754SHans Petter Selasky size_t msglen;
282b00ab754SHans Petter Selasky char *p;
283b00ab754SHans Petter Selasky size_t errbuflen_remaining;
284b00ab754SHans Petter Selasky
2856f9cba8fSJoseph Mingrone (void)vsnprintf(errbuf, errbuflen, fmt, ap);
286b00ab754SHans Petter Selasky msglen = strlen(errbuf);
287b00ab754SHans Petter Selasky
288b00ab754SHans Petter Selasky /*
289b00ab754SHans Petter Selasky * Do we have enough space to append ": "?
290b00ab754SHans Petter Selasky * Including the terminating '\0', that's 3 bytes.
291b00ab754SHans Petter Selasky */
292b00ab754SHans Petter Selasky if (msglen + 3 > errbuflen) {
293b00ab754SHans Petter Selasky /* No - just give them what we've produced. */
294b00ab754SHans Petter Selasky return;
295b00ab754SHans Petter Selasky }
296b00ab754SHans Petter Selasky p = errbuf + msglen;
297b00ab754SHans Petter Selasky errbuflen_remaining = errbuflen - msglen;
298b00ab754SHans Petter Selasky *p++ = ':';
299b00ab754SHans Petter Selasky *p++ = ' ';
300b00ab754SHans Petter Selasky *p = '\0';
301b00ab754SHans Petter Selasky errbuflen_remaining -= 2;
302b00ab754SHans Petter Selasky
303b00ab754SHans Petter Selasky /*
304b00ab754SHans Petter Selasky * Now append the string for the error code.
305b00ab754SHans Petter Selasky */
3066f9cba8fSJoseph Mingrone #if defined(HAVE__WCSERROR_S)
30757e22627SCy Schubert /*
3086f9cba8fSJoseph Mingrone * We have a Windows-style _wcserror_s().
3096f9cba8fSJoseph Mingrone * Generate a UTF-16LE error message.
31057e22627SCy Schubert */
3116f9cba8fSJoseph Mingrone wchar_t utf_16_errbuf[PCAP_ERRBUF_SIZE];
3126f9cba8fSJoseph Mingrone errno_t err = _wcserror_s(utf_16_errbuf, PCAP_ERRBUF_SIZE, errnum);
313b00ab754SHans Petter Selasky if (err != 0) {
314b00ab754SHans Petter Selasky /*
315b00ab754SHans Petter Selasky * It doesn't appear to be documented anywhere obvious
3166f9cba8fSJoseph Mingrone * what the error returns from _wcserror_s().
317b00ab754SHans Petter Selasky */
3186f9cba8fSJoseph Mingrone snprintf(p, errbuflen_remaining, "Error %d", errnum);
3196f9cba8fSJoseph Mingrone return;
320b00ab754SHans Petter Selasky }
3216f9cba8fSJoseph Mingrone
3226f9cba8fSJoseph Mingrone /*
3236f9cba8fSJoseph Mingrone * Now convert it from UTF-16LE to UTF-8, dropping it in the
3246f9cba8fSJoseph Mingrone * remaining space in the buffer, and truncating it - cleanly,
3256f9cba8fSJoseph Mingrone * on a UTF-8 character boundary - if it doesn't fit.
3266f9cba8fSJoseph Mingrone */
3276f9cba8fSJoseph Mingrone utf_16le_to_utf_8_truncated(utf_16_errbuf, p, errbuflen_remaining);
3286f9cba8fSJoseph Mingrone
3296f9cba8fSJoseph Mingrone /*
3306f9cba8fSJoseph Mingrone * Now, if we're not in UTF-8 mode, convert errbuf to the
3316f9cba8fSJoseph Mingrone * local code page.
3326f9cba8fSJoseph Mingrone */
3336f9cba8fSJoseph Mingrone if (!use_utf_8)
3346f9cba8fSJoseph Mingrone utf_8_to_acp_truncated(errbuf);
335b00ab754SHans Petter Selasky #else
336b00ab754SHans Petter Selasky /*
337*afdbf109SJoseph Mingrone * Either Windows without _wcserror_s() or not Windows. Let pcap_strerror()
338*afdbf109SJoseph Mingrone * solve the non-UTF-16 part of this problem space.
339b00ab754SHans Petter Selasky */
3406f9cba8fSJoseph Mingrone snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum));
341b00ab754SHans Petter Selasky #endif
342b00ab754SHans Petter Selasky }
34357e22627SCy Schubert
34457e22627SCy Schubert #ifdef _WIN32
34557e22627SCy Schubert /*
34657e22627SCy Schubert * Generate an error message based on a format, arguments, and a
34757e22627SCy Schubert * Win32 error, with a message for the Win32 error after the formatted output.
34857e22627SCy Schubert */
34957e22627SCy Schubert void
pcapint_fmt_errmsg_for_win32_err(char * errbuf,size_t errbuflen,DWORD errnum,const char * fmt,...)350*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
35157e22627SCy Schubert const char *fmt, ...)
35257e22627SCy Schubert {
35357e22627SCy Schubert va_list ap;
3546f9cba8fSJoseph Mingrone
3556f9cba8fSJoseph Mingrone va_start(ap, fmt);
356*afdbf109SJoseph Mingrone pcapint_vfmt_errmsg_for_win32_err(errbuf, errbuflen, errnum, fmt, ap);
3576f9cba8fSJoseph Mingrone va_end(ap);
3586f9cba8fSJoseph Mingrone }
3596f9cba8fSJoseph Mingrone
3606f9cba8fSJoseph Mingrone void
pcapint_vfmt_errmsg_for_win32_err(char * errbuf,size_t errbuflen,DWORD errnum,const char * fmt,va_list ap)361*afdbf109SJoseph Mingrone pcapint_vfmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
3626f9cba8fSJoseph Mingrone const char *fmt, va_list ap)
3636f9cba8fSJoseph Mingrone {
36457e22627SCy Schubert size_t msglen;
36557e22627SCy Schubert char *p;
36657e22627SCy Schubert size_t errbuflen_remaining;
36757e22627SCy Schubert DWORD retval;
3686f9cba8fSJoseph Mingrone wchar_t utf_16_errbuf[PCAP_ERRBUF_SIZE];
3696f9cba8fSJoseph Mingrone size_t utf_8_len;
37057e22627SCy Schubert
3716f9cba8fSJoseph Mingrone vsnprintf(errbuf, errbuflen, fmt, ap);
37257e22627SCy Schubert msglen = strlen(errbuf);
37357e22627SCy Schubert
37457e22627SCy Schubert /*
37557e22627SCy Schubert * Do we have enough space to append ": "?
37657e22627SCy Schubert * Including the terminating '\0', that's 3 bytes.
37757e22627SCy Schubert */
37857e22627SCy Schubert if (msglen + 3 > errbuflen) {
37957e22627SCy Schubert /* No - just give them what we've produced. */
38057e22627SCy Schubert return;
38157e22627SCy Schubert }
38257e22627SCy Schubert p = errbuf + msglen;
38357e22627SCy Schubert errbuflen_remaining = errbuflen - msglen;
38457e22627SCy Schubert *p++ = ':';
38557e22627SCy Schubert *p++ = ' ';
38657e22627SCy Schubert *p = '\0';
38757e22627SCy Schubert msglen += 2;
38857e22627SCy Schubert errbuflen_remaining -= 2;
38957e22627SCy Schubert
39057e22627SCy Schubert /*
39157e22627SCy Schubert * Now append the string for the error code.
39257e22627SCy Schubert *
39357e22627SCy Schubert * XXX - what language ID to use?
39457e22627SCy Schubert *
39557e22627SCy Schubert * For UN*Xes, pcap_strerror() may or may not return localized
39657e22627SCy Schubert * strings.
39757e22627SCy Schubert *
39857e22627SCy Schubert * We currently don't have localized messages for libpcap, but
39957e22627SCy Schubert * we might want to do so. On the other hand, if most of these
40057e22627SCy Schubert * messages are going to be read by libpcap developers and
40157e22627SCy Schubert * perhaps by developers of libpcap-based applications, English
40257e22627SCy Schubert * might be a better choice, so the developer doesn't have to
40357e22627SCy Schubert * get the message translated if it's in a language they don't
40457e22627SCy Schubert * happen to understand.
40557e22627SCy Schubert */
4066f9cba8fSJoseph Mingrone retval = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
40757e22627SCy Schubert NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
4086f9cba8fSJoseph Mingrone utf_16_errbuf, PCAP_ERRBUF_SIZE, NULL);
40957e22627SCy Schubert if (retval == 0) {
41057e22627SCy Schubert /*
41157e22627SCy Schubert * Failed.
41257e22627SCy Schubert */
4136f9cba8fSJoseph Mingrone snprintf(p, errbuflen_remaining,
41457e22627SCy Schubert "Couldn't get error message for error (%lu)", errnum);
41557e22627SCy Schubert return;
41657e22627SCy Schubert }
41757e22627SCy Schubert
4186f9cba8fSJoseph Mingrone /*
4196f9cba8fSJoseph Mingrone * Now convert it from UTF-16LE to UTF-8.
4206f9cba8fSJoseph Mingrone */
4216f9cba8fSJoseph Mingrone p = utf_16le_to_utf_8_truncated(utf_16_errbuf, p, errbuflen_remaining);
4226f9cba8fSJoseph Mingrone
4236f9cba8fSJoseph Mingrone /*
4246f9cba8fSJoseph Mingrone * Now append the error number, if it fits.
4256f9cba8fSJoseph Mingrone */
4266f9cba8fSJoseph Mingrone utf_8_len = p - errbuf;
4276f9cba8fSJoseph Mingrone errbuflen_remaining -= utf_8_len;
4286f9cba8fSJoseph Mingrone if (utf_8_len == 0) {
4296f9cba8fSJoseph Mingrone /* The message was empty. */
4306f9cba8fSJoseph Mingrone snprintf(p, errbuflen_remaining, "(%lu)", errnum);
4316f9cba8fSJoseph Mingrone } else
4326f9cba8fSJoseph Mingrone snprintf(p, errbuflen_remaining, " (%lu)", errnum);
4336f9cba8fSJoseph Mingrone
4346f9cba8fSJoseph Mingrone /*
4356f9cba8fSJoseph Mingrone * Now, if we're not in UTF-8 mode, convert errbuf to the
4366f9cba8fSJoseph Mingrone * local code page.
4376f9cba8fSJoseph Mingrone */
4386f9cba8fSJoseph Mingrone if (!use_utf_8)
4396f9cba8fSJoseph Mingrone utf_8_to_acp_truncated(errbuf);
44057e22627SCy Schubert }
44157e22627SCy Schubert #endif
442