1335640Shselasky/*
2335640Shselasky * Copyright (c) 1994, 1995, 1996
3335640Shselasky *	The Regents of the University of California.  All rights reserved.
4335640Shselasky *
5335640Shselasky * Redistribution and use in source and binary forms, with or without
6335640Shselasky * modification, are permitted provided that the following conditions
7335640Shselasky * are met:
8335640Shselasky * 1. Redistributions of source code must retain the above copyright
9335640Shselasky *    notice, this list of conditions and the following disclaimer.
10335640Shselasky * 2. Redistributions in binary form must reproduce the above copyright
11335640Shselasky *    notice, this list of conditions and the following disclaimer in the
12335640Shselasky *    documentation and/or other materials provided with the distribution.
13335640Shselasky * 3. All advertising materials mentioning features or use of this software
14335640Shselasky *    must display the following acknowledgement:
15335640Shselasky *	This product includes software developed by the Computer Systems
16335640Shselasky *	Engineering Group at Lawrence Berkeley Laboratory.
17335640Shselasky * 4. Neither the name of the University nor of the Laboratory may be used
18335640Shselasky *    to endorse or promote products derived from this software without
19335640Shselasky *    specific prior written permission.
20335640Shselasky *
21335640Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22335640Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23335640Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24335640Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25335640Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26335640Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27335640Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28335640Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29335640Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30335640Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31335640Shselasky * SUCH DAMAGE.
32335640Shselasky */
33335640Shselasky
34335640Shselasky#ifndef portability_h
35335640Shselasky#define	portability_h
36335640Shselasky
37335640Shselasky/*
38335640Shselasky * Helpers for portability between Windows and UN*X and between different
39335640Shselasky * flavors of UN*X.
40335640Shselasky */
41356341Scy#include <stdarg.h>	/* we declare varargs functions on some platforms */
42335640Shselasky
43335640Shselasky#include "pcap/funcattrs.h"
44335640Shselasky
45335640Shselasky#ifdef __cplusplus
46335640Shselaskyextern "C" {
47335640Shselasky#endif
48335640Shselasky
49356341Scy#ifdef HAVE_STRLCAT
50356341Scy  #define pcap_strlcat	strlcat
51356341Scy#else
52356341Scy  #if defined(_MSC_VER) || defined(__MINGW32__)
53356341Scy    /*
54356341Scy     * strncat_s() is supported at least back to Visual
55356341Scy     * Studio 2005.
56356341Scy     */
57356341Scy    #define pcap_strlcat(x, y, z) \
58356341Scy	strncat_s((x), (z), (y), _TRUNCATE)
59356341Scy  #else
60356341Scy    /*
61356341Scy     * Define it ourselves.
62356341Scy     */
63356341Scy    extern size_t pcap_strlcat(char * restrict dst, const char * restrict src, size_t dstsize);
64356341Scy  #endif
65335640Shselasky#endif
66335640Shselasky
67356341Scy#ifdef HAVE_STRLCPY
68356341Scy  #define pcap_strlcpy	strlcpy
69356341Scy#else
70356341Scy  #if defined(_MSC_VER) || defined(__MINGW32__)
71356341Scy    /*
72356341Scy     * strncpy_s() is supported at least back to Visual
73356341Scy     * Studio 2005.
74356341Scy     */
75356341Scy    #define pcap_strlcpy(x, y, z) \
76356341Scy	strncpy_s((x), (z), (y), _TRUNCATE)
77356341Scy  #else
78356341Scy    /*
79356341Scy     * Define it ourselves.
80356341Scy     */
81356341Scy    extern size_t pcap_strlcpy(char * restrict dst, const char * restrict src, size_t dstsize);
82356341Scy  #endif
83335640Shselasky#endif
84335640Shselasky
85335640Shselasky#ifdef _MSC_VER
86356341Scy  #define isascii	__isascii
87356341Scy
88335640Shselasky  /*
89335640Shselasky   * If <crtdbg.h> has been included, and _DEBUG is defined, and
90335640Shselasky   * __STDC__ is zero, <crtdbg.h> will define strdup() to call
91335640Shselasky   * _strdup_dbg().  So if it's already defined, don't redefine
92335640Shselasky   * it.
93335640Shselasky   */
94335640Shselasky  #ifndef strdup
95335640Shselasky  #define strdup	_strdup
96335640Shselasky  #endif
97335640Shselasky#endif
98335640Shselasky
99335640Shselasky/*
100335640Shselasky * On Windows, snprintf(), with that name and with C99 behavior - i.e.,
101335640Shselasky * guaranteeing that the formatted string is null-terminated - didn't
102335640Shselasky * appear until Visual Studio 2015.  Prior to that, the C runtime had
103335640Shselasky * only _snprintf(), which *doesn't* guarantee that the string is
104335640Shselasky * null-terminated if it is truncated due to the buffer being too
105335640Shselasky * small.  We therefore can't just define snprintf to be _snprintf
106335640Shselasky * and define vsnprintf to be _vsnprintf, as we're relying on null-
107335640Shselasky * termination of strings in all cases.
108335640Shselasky *
109335640Shselasky * We also want to allow this to be built with versions of Visual Studio
110335640Shselasky * prior to VS 2015, so we can't rely on snprintf() being present.
111335640Shselasky *
112335640Shselasky * And we want to make sure that, if we support plugins in the future,
113335640Shselasky * a routine with C99 snprintf() behavior will be available to them.
114335640Shselasky * We also don't want it to collide with the C library snprintf() if
115335640Shselasky * there is one.
116335640Shselasky *
117335640Shselasky * So we make pcap_snprintf() and pcap_vsnprintf() available, either by
118335640Shselasky * #defining them to be snprintf or vsnprintf, respectively, or by
119335640Shselasky * defining our own versions and exporting them.
120335640Shselasky */
121335640Shselasky#ifdef HAVE_SNPRINTF
122335640Shselasky#define pcap_snprintf snprintf
123335640Shselasky#else
124335640Shselaskyextern int pcap_snprintf(char *, size_t, PCAP_FORMAT_STRING(const char *), ...)
125335640Shselasky    PCAP_PRINTFLIKE(3, 4);
126335640Shselasky#endif
127335640Shselasky
128335640Shselasky#ifdef HAVE_VSNPRINTF
129335640Shselasky#define pcap_vsnprintf vsnprintf
130335640Shselasky#else
131335640Shselaskyextern int pcap_vsnprintf(char *, size_t, const char *, va_list ap);
132335640Shselasky#endif
133335640Shselasky
134356341Scy/*
135356341Scy * We also want asprintf(), for some cases where we use it to construct
136356341Scy * dynamically-allocated variable-length strings.
137356341Scy */
138356341Scy#ifdef HAVE_ASPRINTF
139356341Scy#define pcap_asprintf asprintf
140356341Scy#else
141356341Scyextern int pcap_asprintf(char **, PCAP_FORMAT_STRING(const char *), ...)
142356341Scy    PCAP_PRINTFLIKE(2, 3);
143356341Scy#endif
144356341Scy
145356341Scy#ifdef HAVE_VASPRINTF
146356341Scy#define pcap_vasprintf vasprintf
147356341Scy#else
148356341Scyextern int pcap_vasprintf(char **, const char *, va_list ap);
149356341Scy#endif
150356341Scy
151335640Shselasky#ifdef HAVE_STRTOK_R
152335640Shselasky  #define pcap_strtok_r	strtok_r
153335640Shselasky#else
154335640Shselasky  #ifdef _WIN32
155335640Shselasky    /*
156335640Shselasky     * Microsoft gives it a different name.
157335640Shselasky     */
158335640Shselasky    #define pcap_strtok_r	strtok_s
159335640Shselasky  #else
160335640Shselasky    /*
161335640Shselasky     * Define it ourselves.
162335640Shselasky     */
163335640Shselasky    extern char *pcap_strtok_r(char *, const char *, char **);
164335640Shselasky  #endif
165335640Shselasky#endif /* HAVE_STRTOK_R */
166335640Shselasky
167335640Shselasky#ifdef _WIN32
168335640Shselasky  #if !defined(__cplusplus)
169335640Shselasky    #define inline __inline
170335640Shselasky  #endif
171335640Shselasky#endif /* _WIN32 */
172335640Shselasky
173335640Shselasky#ifdef __cplusplus
174335640Shselasky}
175335640Shselasky#endif
176335640Shselasky
177335640Shselasky#endif
178