xref: /freebsd/contrib/libpcap/fmtutils.c (revision 6f9cba8f)
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 #ifdef HAVE_CONFIG_H
39b00ab754SHans Petter Selasky #include <config.h>
40b00ab754SHans Petter Selasky #endif
41b00ab754SHans Petter Selasky 
42b00ab754SHans Petter Selasky #include "ftmacros.h"
43b00ab754SHans Petter Selasky 
44b00ab754SHans Petter Selasky #include <stddef.h>
45b00ab754SHans Petter Selasky #include <stdarg.h>
46b00ab754SHans Petter Selasky #include <stdio.h>
47b00ab754SHans Petter Selasky #include <string.h>
48b00ab754SHans Petter Selasky #include <errno.h>
49b00ab754SHans Petter Selasky 
50*6f9cba8fSJoseph Mingrone #include "pcap-int.h"
51b00ab754SHans Petter Selasky 
52b00ab754SHans Petter Selasky #include "portability.h"
53b00ab754SHans Petter Selasky 
54b00ab754SHans Petter Selasky #include "fmtutils.h"
55b00ab754SHans Petter Selasky 
56*6f9cba8fSJoseph Mingrone #ifdef _WIN32
57*6f9cba8fSJoseph Mingrone #include "charconv.h"
58*6f9cba8fSJoseph Mingrone #endif
59*6f9cba8fSJoseph Mingrone 
60*6f9cba8fSJoseph Mingrone /*
61*6f9cba8fSJoseph Mingrone  * Set the encoding.
62*6f9cba8fSJoseph Mingrone  */
63*6f9cba8fSJoseph Mingrone #ifdef _WIN32
64*6f9cba8fSJoseph Mingrone /*
65*6f9cba8fSJoseph Mingrone  * True if we shouold use UTF-8.
66*6f9cba8fSJoseph Mingrone  */
67*6f9cba8fSJoseph Mingrone static int use_utf_8;
68*6f9cba8fSJoseph Mingrone 
69*6f9cba8fSJoseph Mingrone void
pcap_fmt_set_encoding(unsigned int opts)70*6f9cba8fSJoseph Mingrone pcap_fmt_set_encoding(unsigned int opts)
71*6f9cba8fSJoseph Mingrone {
72*6f9cba8fSJoseph Mingrone 	if (opts == PCAP_CHAR_ENC_UTF_8)
73*6f9cba8fSJoseph Mingrone 		use_utf_8 = 1;
74*6f9cba8fSJoseph Mingrone }
75*6f9cba8fSJoseph Mingrone #else
76*6f9cba8fSJoseph Mingrone void
pcap_fmt_set_encoding(unsigned int opts _U_)77*6f9cba8fSJoseph Mingrone pcap_fmt_set_encoding(unsigned int opts _U_)
78*6f9cba8fSJoseph Mingrone {
79*6f9cba8fSJoseph Mingrone 	/*
80*6f9cba8fSJoseph Mingrone 	 * Nothing to do here.
81*6f9cba8fSJoseph Mingrone 	 */
82*6f9cba8fSJoseph Mingrone }
83*6f9cba8fSJoseph Mingrone #endif
84*6f9cba8fSJoseph Mingrone 
85*6f9cba8fSJoseph Mingrone #ifdef _WIN32
86*6f9cba8fSJoseph Mingrone /*
87*6f9cba8fSJoseph Mingrone  * Convert a null-terminated UTF-16LE string to UTF-8, putting it into
88*6f9cba8fSJoseph Mingrone  * a buffer starting at the specified location and stopping if we go
89*6f9cba8fSJoseph Mingrone  * past the specified size.  This will only put out complete UTF-8
90*6f9cba8fSJoseph Mingrone  * sequences.
91*6f9cba8fSJoseph Mingrone  *
92*6f9cba8fSJoseph Mingrone  * We do this ourselves because Microsoft doesn't offer a "convert and
93*6f9cba8fSJoseph Mingrone  * stop at a UTF-8 character boundary if we run out of space" routine.
94*6f9cba8fSJoseph Mingrone  */
95*6f9cba8fSJoseph Mingrone #define IS_LEADING_SURROGATE(c) \
96*6f9cba8fSJoseph Mingrone 	((c) >= 0xd800 && (c) < 0xdc00)
97*6f9cba8fSJoseph Mingrone #define IS_TRAILING_SURROGATE(c) \
98*6f9cba8fSJoseph Mingrone 	((c) >= 0xdc00 && (c) < 0xe000)
99*6f9cba8fSJoseph Mingrone #define SURROGATE_VALUE(leading, trailing) \
100*6f9cba8fSJoseph Mingrone 	(((((leading) - 0xd800) << 10) | ((trailing) - 0xdc00)) + 0x10000)
101*6f9cba8fSJoseph Mingrone #define REPLACEMENT_CHARACTER	0x0FFFD
102*6f9cba8fSJoseph Mingrone 
103*6f9cba8fSJoseph Mingrone static char *
utf_16le_to_utf_8_truncated(const wchar_t * utf_16,char * utf_8,size_t utf_8_len)104*6f9cba8fSJoseph Mingrone utf_16le_to_utf_8_truncated(const wchar_t *utf_16, char *utf_8,
105*6f9cba8fSJoseph Mingrone     size_t utf_8_len)
106*6f9cba8fSJoseph Mingrone {
107*6f9cba8fSJoseph Mingrone 	wchar_t c, c2;
108*6f9cba8fSJoseph Mingrone 	uint32_t uc;
109*6f9cba8fSJoseph Mingrone 
110*6f9cba8fSJoseph Mingrone 	if (utf_8_len == 0) {
111*6f9cba8fSJoseph Mingrone 		/*
112*6f9cba8fSJoseph Mingrone 		 * Not even enough room for a trailing '\0'.
113*6f9cba8fSJoseph Mingrone 		 * Don't put anything into the buffer.
114*6f9cba8fSJoseph Mingrone 		 */
115*6f9cba8fSJoseph Mingrone 		return (utf_8);
116*6f9cba8fSJoseph Mingrone 	}
117*6f9cba8fSJoseph Mingrone 
118*6f9cba8fSJoseph Mingrone 	while ((c = *utf_16++) != '\0') {
119*6f9cba8fSJoseph Mingrone 		if (IS_LEADING_SURROGATE(c)) {
120*6f9cba8fSJoseph Mingrone 			/*
121*6f9cba8fSJoseph Mingrone 			 * Leading surrogate.  Must be followed by
122*6f9cba8fSJoseph Mingrone 			 * a trailing surrogate.
123*6f9cba8fSJoseph Mingrone 			 */
124*6f9cba8fSJoseph Mingrone 			c2 = *utf_16;
125*6f9cba8fSJoseph Mingrone 			if (c2 == '\0') {
126*6f9cba8fSJoseph Mingrone 				/*
127*6f9cba8fSJoseph Mingrone 				 * Oops, string ends with a lead
128*6f9cba8fSJoseph Mingrone 				 * surrogate.  Try to drop in
129*6f9cba8fSJoseph Mingrone 				 * a REPLACEMENT CHARACTER, and
130*6f9cba8fSJoseph Mingrone 				 * don't move the string pointer,
131*6f9cba8fSJoseph Mingrone 				 * so on the next trip through
132*6f9cba8fSJoseph Mingrone 				 * the loop we grab the terminating
133*6f9cba8fSJoseph Mingrone 				 * '\0' and quit.
134*6f9cba8fSJoseph Mingrone 				 */
135*6f9cba8fSJoseph Mingrone 				uc = REPLACEMENT_CHARACTER;
136*6f9cba8fSJoseph Mingrone 			} else {
137*6f9cba8fSJoseph Mingrone 				/*
138*6f9cba8fSJoseph Mingrone 				 * OK, we can consume this 2-octet
139*6f9cba8fSJoseph Mingrone 				 * value.
140*6f9cba8fSJoseph Mingrone 				 */
141*6f9cba8fSJoseph Mingrone 				utf_16++;
142*6f9cba8fSJoseph Mingrone 				if (IS_TRAILING_SURROGATE(c2)) {
143*6f9cba8fSJoseph Mingrone 					/*
144*6f9cba8fSJoseph Mingrone 					 * Trailing surrogate.
145*6f9cba8fSJoseph Mingrone 					 * This calculation will,
146*6f9cba8fSJoseph Mingrone 					 * for c being a leading
147*6f9cba8fSJoseph Mingrone 					 * surrogate and c2 being
148*6f9cba8fSJoseph Mingrone 					 * a trailing surrogate,
149*6f9cba8fSJoseph Mingrone 					 * produce a value between
150*6f9cba8fSJoseph Mingrone 					 * 0x100000 and 0x10ffff,
151*6f9cba8fSJoseph Mingrone 					 * so it's always going to be
152*6f9cba8fSJoseph Mingrone 					 * a valid Unicode code point.
153*6f9cba8fSJoseph Mingrone 					 */
154*6f9cba8fSJoseph Mingrone 					uc = SURROGATE_VALUE(c, c2);
155*6f9cba8fSJoseph Mingrone 				} else {
156*6f9cba8fSJoseph Mingrone 					/*
157*6f9cba8fSJoseph Mingrone 					 * Not a trailing surroage;
158*6f9cba8fSJoseph Mingrone 					 * try to drop in a
159*6f9cba8fSJoseph Mingrone 					 * REPLACEMENT CHARACTER.
160*6f9cba8fSJoseph Mingrone 					 */
161*6f9cba8fSJoseph Mingrone 					uc = REPLACEMENT_CHARACTER;
162*6f9cba8fSJoseph Mingrone 				}
163*6f9cba8fSJoseph Mingrone 			}
164*6f9cba8fSJoseph Mingrone 		} else {
165*6f9cba8fSJoseph Mingrone 			/*
166*6f9cba8fSJoseph Mingrone 			 * Not a leading surrogate.
167*6f9cba8fSJoseph Mingrone 			 */
168*6f9cba8fSJoseph Mingrone 			if (IS_TRAILING_SURROGATE(c)) {
169*6f9cba8fSJoseph Mingrone 				/*
170*6f9cba8fSJoseph Mingrone 				 * Trailing surrogate without
171*6f9cba8fSJoseph Mingrone 				 * a preceding leading surrogate.
172*6f9cba8fSJoseph Mingrone 				 * Try to drop in a REPLACEMENT
173*6f9cba8fSJoseph Mingrone 				 * CHARACTER.
174*6f9cba8fSJoseph Mingrone 				 */
175*6f9cba8fSJoseph Mingrone 				uc = REPLACEMENT_CHARACTER;
176*6f9cba8fSJoseph Mingrone 			} else {
177*6f9cba8fSJoseph Mingrone 				/*
178*6f9cba8fSJoseph Mingrone 				 * This is a valid BMP character;
179*6f9cba8fSJoseph Mingrone 				 * drop it in.
180*6f9cba8fSJoseph Mingrone 				 */
181*6f9cba8fSJoseph Mingrone 				uc = c;
182*6f9cba8fSJoseph Mingrone 			}
183*6f9cba8fSJoseph Mingrone 		}
184*6f9cba8fSJoseph Mingrone 
185*6f9cba8fSJoseph Mingrone 		/*
186*6f9cba8fSJoseph Mingrone 		 * OK, uc is a valid Unicode character; how
187*6f9cba8fSJoseph Mingrone 		 * many bytes worth of UTF-8 does it require?
188*6f9cba8fSJoseph Mingrone 		 */
189*6f9cba8fSJoseph Mingrone 		if (uc < 0x0080) {
190*6f9cba8fSJoseph Mingrone 			/* 1 byte. */
191*6f9cba8fSJoseph Mingrone 			if (utf_8_len < 2) {
192*6f9cba8fSJoseph Mingrone 				/*
193*6f9cba8fSJoseph Mingrone 				 * Not enough room for that byte
194*6f9cba8fSJoseph Mingrone 				 * plus a trailing '\0'.
195*6f9cba8fSJoseph Mingrone 				 */
196*6f9cba8fSJoseph Mingrone 				break;
197*6f9cba8fSJoseph Mingrone 			}
198*6f9cba8fSJoseph Mingrone 			*utf_8++ = (char)uc;
199*6f9cba8fSJoseph Mingrone 			utf_8_len--;
200*6f9cba8fSJoseph Mingrone 		} else if (uc < 0x0800) {
201*6f9cba8fSJoseph Mingrone 			/* 2 bytes. */
202*6f9cba8fSJoseph Mingrone 			if (utf_8_len < 3) {
203*6f9cba8fSJoseph Mingrone 				/*
204*6f9cba8fSJoseph Mingrone 				 * Not enough room for those bytes
205*6f9cba8fSJoseph Mingrone 				 * plus a trailing '\0'.
206*6f9cba8fSJoseph Mingrone 				 */
207*6f9cba8fSJoseph Mingrone 				break;
208*6f9cba8fSJoseph Mingrone 			}
209*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 6) & 0x3F) | 0xC0;
210*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
211*6f9cba8fSJoseph Mingrone 			utf_8_len -= 2;
212*6f9cba8fSJoseph Mingrone 		} else if (uc < 0x010000) {
213*6f9cba8fSJoseph Mingrone 			/* 3 bytes. */
214*6f9cba8fSJoseph Mingrone 			if (utf_8_len < 4) {
215*6f9cba8fSJoseph Mingrone 				/*
216*6f9cba8fSJoseph Mingrone 				 * Not enough room for those bytes
217*6f9cba8fSJoseph Mingrone 				 * plus a trailing '\0'.
218*6f9cba8fSJoseph Mingrone 				 */
219*6f9cba8fSJoseph Mingrone 				break;
220*6f9cba8fSJoseph Mingrone 			}
221*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 12) & 0x0F) | 0xE0;
222*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 6) & 0x3F) | 0x80;
223*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
224*6f9cba8fSJoseph Mingrone 			utf_8_len -= 3;
225*6f9cba8fSJoseph Mingrone 		} else {
226*6f9cba8fSJoseph Mingrone 			/* 4 bytes. */
227*6f9cba8fSJoseph Mingrone 			if (utf_8_len < 5) {
228*6f9cba8fSJoseph Mingrone 				/*
229*6f9cba8fSJoseph Mingrone 				 * Not enough room for those bytes
230*6f9cba8fSJoseph Mingrone 				 * plus a trailing '\0'.
231*6f9cba8fSJoseph Mingrone 				 */
232*6f9cba8fSJoseph Mingrone 				break;
233*6f9cba8fSJoseph Mingrone 			}
234*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 18) & 0x03) | 0xF0;
235*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 12) & 0x3F) | 0x80;
236*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 6) & 0x3F) | 0x80;
237*6f9cba8fSJoseph Mingrone 			*utf_8++ = ((uc >> 0) & 0x3F) | 0x80;
238*6f9cba8fSJoseph Mingrone 			utf_8_len -= 3;
239*6f9cba8fSJoseph Mingrone 		}
240*6f9cba8fSJoseph Mingrone 	}
241*6f9cba8fSJoseph Mingrone 
242*6f9cba8fSJoseph Mingrone 	/*
243*6f9cba8fSJoseph Mingrone 	 * OK, we have enough room for (at least) a trailing '\0'.
244*6f9cba8fSJoseph Mingrone 	 * (We started out with enough room, thanks to the test
245*6f9cba8fSJoseph Mingrone 	 * for a zero-length buffer at the beginning, and if
246*6f9cba8fSJoseph Mingrone 	 * there wasn't enough room for any character we wanted
247*6f9cba8fSJoseph Mingrone 	 * to put into the buffer *plus* a trailing '\0',
248*6f9cba8fSJoseph Mingrone 	 * we'd have quit before putting it into the buffer,
249*6f9cba8fSJoseph Mingrone 	 * and thus would have left enough room for the trailing
250*6f9cba8fSJoseph Mingrone 	 * '\0'.)
251*6f9cba8fSJoseph Mingrone 	 *
252*6f9cba8fSJoseph Mingrone 	 * Drop it in.
253*6f9cba8fSJoseph Mingrone 	 */
254*6f9cba8fSJoseph Mingrone 	*utf_8 = '\0';
255*6f9cba8fSJoseph Mingrone 
256*6f9cba8fSJoseph Mingrone 	/*
257*6f9cba8fSJoseph Mingrone 	 * Return a pointer to the terminating '\0', in case we
258*6f9cba8fSJoseph Mingrone 	 * want to drop something in after that.
259*6f9cba8fSJoseph Mingrone 	 */
260*6f9cba8fSJoseph Mingrone 	return (utf_8);
261*6f9cba8fSJoseph Mingrone }
262*6f9cba8fSJoseph Mingrone #endif /* _WIN32 */
263*6f9cba8fSJoseph Mingrone 
264b00ab754SHans Petter Selasky /*
265b00ab754SHans Petter Selasky  * Generate an error message based on a format, arguments, and an
266b00ab754SHans Petter Selasky  * errno, with a message for the errno after the formatted output.
267b00ab754SHans Petter Selasky  */
268b00ab754SHans Petter Selasky void
pcap_fmt_errmsg_for_errno(char * errbuf,size_t errbuflen,int errnum,const char * fmt,...)269b00ab754SHans Petter Selasky pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
270b00ab754SHans Petter Selasky     const char *fmt, ...)
271b00ab754SHans Petter Selasky {
272b00ab754SHans Petter Selasky 	va_list ap;
273*6f9cba8fSJoseph Mingrone 
274*6f9cba8fSJoseph Mingrone 	va_start(ap, fmt);
275*6f9cba8fSJoseph Mingrone 	pcap_vfmt_errmsg_for_errno(errbuf, errbuflen, errnum, fmt, ap);
276*6f9cba8fSJoseph Mingrone 	va_end(ap);
277*6f9cba8fSJoseph Mingrone }
278*6f9cba8fSJoseph Mingrone 
279*6f9cba8fSJoseph Mingrone void
pcap_vfmt_errmsg_for_errno(char * errbuf,size_t errbuflen,int errnum,const char * fmt,va_list ap)280*6f9cba8fSJoseph Mingrone pcap_vfmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
281*6f9cba8fSJoseph Mingrone     const char *fmt, va_list ap)
282*6f9cba8fSJoseph Mingrone {
283b00ab754SHans Petter Selasky 	size_t msglen;
284b00ab754SHans Petter Selasky 	char *p;
285b00ab754SHans Petter Selasky 	size_t errbuflen_remaining;
286b00ab754SHans Petter Selasky 
287*6f9cba8fSJoseph Mingrone 	(void)vsnprintf(errbuf, errbuflen, fmt, ap);
288b00ab754SHans Petter Selasky 	msglen = strlen(errbuf);
289b00ab754SHans Petter Selasky 
290b00ab754SHans Petter Selasky 	/*
291b00ab754SHans Petter Selasky 	 * Do we have enough space to append ": "?
292b00ab754SHans Petter Selasky 	 * Including the terminating '\0', that's 3 bytes.
293b00ab754SHans Petter Selasky 	 */
294b00ab754SHans Petter Selasky 	if (msglen + 3 > errbuflen) {
295b00ab754SHans Petter Selasky 		/* No - just give them what we've produced. */
296b00ab754SHans Petter Selasky 		return;
297b00ab754SHans Petter Selasky 	}
298b00ab754SHans Petter Selasky 	p = errbuf + msglen;
299b00ab754SHans Petter Selasky 	errbuflen_remaining = errbuflen - msglen;
300b00ab754SHans Petter Selasky 	*p++ = ':';
301b00ab754SHans Petter Selasky 	*p++ = ' ';
302b00ab754SHans Petter Selasky 	*p = '\0';
303b00ab754SHans Petter Selasky 	errbuflen_remaining -= 2;
304b00ab754SHans Petter Selasky 
305b00ab754SHans Petter Selasky 	/*
306b00ab754SHans Petter Selasky 	 * Now append the string for the error code.
307b00ab754SHans Petter Selasky 	 */
308*6f9cba8fSJoseph Mingrone #if defined(HAVE__WCSERROR_S)
30957e22627SCy Schubert 	/*
310*6f9cba8fSJoseph Mingrone 	 * We have a Windows-style _wcserror_s().
311*6f9cba8fSJoseph Mingrone 	 * Generate a UTF-16LE error message.
31257e22627SCy Schubert 	 */
313*6f9cba8fSJoseph Mingrone 	wchar_t utf_16_errbuf[PCAP_ERRBUF_SIZE];
314*6f9cba8fSJoseph Mingrone 	errno_t err = _wcserror_s(utf_16_errbuf, PCAP_ERRBUF_SIZE, errnum);
315b00ab754SHans Petter Selasky 	if (err != 0) {
316b00ab754SHans Petter Selasky 		/*
317b00ab754SHans Petter Selasky 		 * It doesn't appear to be documented anywhere obvious
318*6f9cba8fSJoseph Mingrone 		 * what the error returns from _wcserror_s().
319b00ab754SHans Petter Selasky 		 */
320*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining, "Error %d", errnum);
321*6f9cba8fSJoseph Mingrone 		return;
322b00ab754SHans Petter Selasky 	}
323*6f9cba8fSJoseph Mingrone 
324*6f9cba8fSJoseph Mingrone 	/*
325*6f9cba8fSJoseph Mingrone 	 * Now convert it from UTF-16LE to UTF-8, dropping it in the
326*6f9cba8fSJoseph Mingrone 	 * remaining space in the buffer, and truncating it - cleanly,
327*6f9cba8fSJoseph Mingrone 	 * on a UTF-8 character boundary - if it doesn't fit.
328*6f9cba8fSJoseph Mingrone 	 */
329*6f9cba8fSJoseph Mingrone 	utf_16le_to_utf_8_truncated(utf_16_errbuf, p, errbuflen_remaining);
330*6f9cba8fSJoseph Mingrone 
331*6f9cba8fSJoseph Mingrone 	/*
332*6f9cba8fSJoseph Mingrone 	 * Now, if we're not in UTF-8 mode, convert errbuf to the
333*6f9cba8fSJoseph Mingrone 	 * local code page.
334*6f9cba8fSJoseph Mingrone 	 */
335*6f9cba8fSJoseph Mingrone 	if (!use_utf_8)
336*6f9cba8fSJoseph Mingrone 		utf_8_to_acp_truncated(errbuf);
33757e22627SCy Schubert #elif defined(HAVE_GNU_STRERROR_R)
33857e22627SCy Schubert 	/*
33957e22627SCy Schubert 	 * We have a GNU-style strerror_r(), which is *not* guaranteed to
34057e22627SCy Schubert 	 * do anything to the buffer handed to it, and which returns a
34157e22627SCy Schubert 	 * pointer to the error string, which may or may not be in
34257e22627SCy Schubert 	 * the buffer.
34357e22627SCy Schubert 	 *
34457e22627SCy Schubert 	 * It is, however, guaranteed to succeed.
34557e22627SCy Schubert 	 */
34657e22627SCy Schubert 	char strerror_buf[PCAP_ERRBUF_SIZE];
34757e22627SCy Schubert 	char *errstring = strerror_r(errnum, strerror_buf, PCAP_ERRBUF_SIZE);
348*6f9cba8fSJoseph Mingrone 	snprintf(p, errbuflen_remaining, "%s", errstring);
34957e22627SCy Schubert #elif defined(HAVE_POSIX_STRERROR_R)
35057e22627SCy Schubert 	/*
35157e22627SCy Schubert 	 * We have a POSIX-style strerror_r(), which is guaranteed to fill
35257e22627SCy Schubert 	 * in the buffer, but is not guaranteed to succeed.
35357e22627SCy Schubert 	 */
35457e22627SCy Schubert 	int err = strerror_r(errnum, p, errbuflen_remaining);
355b00ab754SHans Petter Selasky 	if (err == EINVAL) {
356b00ab754SHans Petter Selasky 		/*
357b00ab754SHans Petter Selasky 		 * UNIX 03 says this isn't guaranteed to produce a
358b00ab754SHans Petter Selasky 		 * fallback error message.
359b00ab754SHans Petter Selasky 		 */
360*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining, "Unknown error: %d",
361b00ab754SHans Petter Selasky 		    errnum);
362b00ab754SHans Petter Selasky 	} else if (err == ERANGE) {
363b00ab754SHans Petter Selasky 		/*
364b00ab754SHans Petter Selasky 		 * UNIX 03 says this isn't guaranteed to produce a
365b00ab754SHans Petter Selasky 		 * fallback error message.
366b00ab754SHans Petter Selasky 		 */
367*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining,
368b00ab754SHans Petter Selasky 		    "Message for error %d is too long", errnum);
369b00ab754SHans Petter Selasky 	}
370b00ab754SHans Petter Selasky #else
371b00ab754SHans Petter Selasky 	/*
372*6f9cba8fSJoseph Mingrone 	 * We have neither _wcserror_s() nor strerror_r(), so we're
373b00ab754SHans Petter Selasky 	 * stuck with using pcap_strerror().
374b00ab754SHans Petter Selasky 	 */
375*6f9cba8fSJoseph Mingrone 	snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum));
376b00ab754SHans Petter Selasky #endif
377b00ab754SHans Petter Selasky }
37857e22627SCy Schubert 
37957e22627SCy Schubert #ifdef _WIN32
38057e22627SCy Schubert /*
38157e22627SCy Schubert  * Generate an error message based on a format, arguments, and a
38257e22627SCy Schubert  * Win32 error, with a message for the Win32 error after the formatted output.
38357e22627SCy Schubert  */
38457e22627SCy Schubert void
pcap_fmt_errmsg_for_win32_err(char * errbuf,size_t errbuflen,DWORD errnum,const char * fmt,...)38557e22627SCy Schubert pcap_fmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
38657e22627SCy Schubert     const char *fmt, ...)
38757e22627SCy Schubert {
38857e22627SCy Schubert 	va_list ap;
389*6f9cba8fSJoseph Mingrone 
390*6f9cba8fSJoseph Mingrone 	va_start(ap, fmt);
391*6f9cba8fSJoseph Mingrone 	pcap_vfmt_errmsg_for_win32_err(errbuf, errbuflen, errnum, fmt, ap);
392*6f9cba8fSJoseph Mingrone 	va_end(ap);
393*6f9cba8fSJoseph Mingrone }
394*6f9cba8fSJoseph Mingrone 
395*6f9cba8fSJoseph Mingrone void
pcap_vfmt_errmsg_for_win32_err(char * errbuf,size_t errbuflen,DWORD errnum,const char * fmt,va_list ap)396*6f9cba8fSJoseph Mingrone pcap_vfmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
397*6f9cba8fSJoseph Mingrone     const char *fmt, va_list ap)
398*6f9cba8fSJoseph Mingrone {
39957e22627SCy Schubert 	size_t msglen;
40057e22627SCy Schubert 	char *p;
40157e22627SCy Schubert 	size_t errbuflen_remaining;
40257e22627SCy Schubert 	DWORD retval;
403*6f9cba8fSJoseph Mingrone 	wchar_t utf_16_errbuf[PCAP_ERRBUF_SIZE];
404*6f9cba8fSJoseph Mingrone 	size_t utf_8_len;
40557e22627SCy Schubert 
406*6f9cba8fSJoseph Mingrone 	vsnprintf(errbuf, errbuflen, fmt, ap);
40757e22627SCy Schubert 	msglen = strlen(errbuf);
40857e22627SCy Schubert 
40957e22627SCy Schubert 	/*
41057e22627SCy Schubert 	 * Do we have enough space to append ": "?
41157e22627SCy Schubert 	 * Including the terminating '\0', that's 3 bytes.
41257e22627SCy Schubert 	 */
41357e22627SCy Schubert 	if (msglen + 3 > errbuflen) {
41457e22627SCy Schubert 		/* No - just give them what we've produced. */
41557e22627SCy Schubert 		return;
41657e22627SCy Schubert 	}
41757e22627SCy Schubert 	p = errbuf + msglen;
41857e22627SCy Schubert 	errbuflen_remaining = errbuflen - msglen;
41957e22627SCy Schubert 	*p++ = ':';
42057e22627SCy Schubert 	*p++ = ' ';
42157e22627SCy Schubert 	*p = '\0';
42257e22627SCy Schubert 	msglen += 2;
42357e22627SCy Schubert 	errbuflen_remaining -= 2;
42457e22627SCy Schubert 
42557e22627SCy Schubert 	/*
42657e22627SCy Schubert 	 * Now append the string for the error code.
42757e22627SCy Schubert 	 *
42857e22627SCy Schubert 	 * XXX - what language ID to use?
42957e22627SCy Schubert 	 *
43057e22627SCy Schubert 	 * For UN*Xes, pcap_strerror() may or may not return localized
43157e22627SCy Schubert 	 * strings.
43257e22627SCy Schubert 	 *
43357e22627SCy Schubert 	 * We currently don't have localized messages for libpcap, but
43457e22627SCy Schubert 	 * we might want to do so.  On the other hand, if most of these
43557e22627SCy Schubert 	 * messages are going to be read by libpcap developers and
43657e22627SCy Schubert 	 * perhaps by developers of libpcap-based applications, English
43757e22627SCy Schubert 	 * might be a better choice, so the developer doesn't have to
43857e22627SCy Schubert 	 * get the message translated if it's in a language they don't
43957e22627SCy Schubert 	 * happen to understand.
44057e22627SCy Schubert 	 */
441*6f9cba8fSJoseph Mingrone 	retval = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
44257e22627SCy Schubert 	    NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
443*6f9cba8fSJoseph Mingrone 	    utf_16_errbuf, PCAP_ERRBUF_SIZE, NULL);
44457e22627SCy Schubert 	if (retval == 0) {
44557e22627SCy Schubert 		/*
44657e22627SCy Schubert 		 * Failed.
44757e22627SCy Schubert 		 */
448*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining,
44957e22627SCy Schubert 		    "Couldn't get error message for error (%lu)", errnum);
45057e22627SCy Schubert 		return;
45157e22627SCy Schubert 	}
45257e22627SCy Schubert 
453*6f9cba8fSJoseph Mingrone 	/*
454*6f9cba8fSJoseph Mingrone 	 * Now convert it from UTF-16LE to UTF-8.
455*6f9cba8fSJoseph Mingrone 	 */
456*6f9cba8fSJoseph Mingrone 	p = utf_16le_to_utf_8_truncated(utf_16_errbuf, p, errbuflen_remaining);
457*6f9cba8fSJoseph Mingrone 
458*6f9cba8fSJoseph Mingrone 	/*
459*6f9cba8fSJoseph Mingrone 	 * Now append the error number, if it fits.
460*6f9cba8fSJoseph Mingrone 	 */
461*6f9cba8fSJoseph Mingrone 	utf_8_len = p - errbuf;
462*6f9cba8fSJoseph Mingrone 	errbuflen_remaining -= utf_8_len;
463*6f9cba8fSJoseph Mingrone 	if (utf_8_len == 0) {
464*6f9cba8fSJoseph Mingrone 		/* The message was empty. */
465*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining, "(%lu)", errnum);
466*6f9cba8fSJoseph Mingrone 	} else
467*6f9cba8fSJoseph Mingrone 		snprintf(p, errbuflen_remaining, " (%lu)", errnum);
468*6f9cba8fSJoseph Mingrone 
469*6f9cba8fSJoseph Mingrone 	/*
470*6f9cba8fSJoseph Mingrone 	 * Now, if we're not in UTF-8 mode, convert errbuf to the
471*6f9cba8fSJoseph Mingrone 	 * local code page.
472*6f9cba8fSJoseph Mingrone 	 */
473*6f9cba8fSJoseph Mingrone 	if (!use_utf_8)
474*6f9cba8fSJoseph Mingrone 		utf_8_to_acp_truncated(errbuf);
47557e22627SCy Schubert }
47657e22627SCy Schubert #endif
477