Coverage Report

Created: 2026-05-24 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/resiprocate/rutil/compat.hxx
Line
Count
Source
1
#if !defined(RESIP_COMPAT_HXX)
2
#define RESIP_COMPAT_HXX 
3
4
#ifdef HAVE_CONFIG_H
5
#include "config.h"
6
#else
7
#define GPERF_SIZE_TYPE size_t
8
#endif
9
10
11
/**
12
   @file
13
   This file is used to handle compatibility fixes/tweaks so reSIProcate can 
14
   function on multiple platforms.
15
*/
16
17
#if defined(__INTEL_COMPILER ) && defined( __OPTIMIZE__ )
18
#  undef __OPTIMIZE__ // weird intel bug with ntohs and htons macros
19
#endif
20
21
//#if defined(HAVE_SYS_INT_TYPES_H)
22
//#include <sys/int_types.h>
23
//#endif
24
25
#include <cstring>
26
#include <string>
27
28
#ifndef WIN32
29
#  include <netdb.h>
30
#  include <sys/types.h>
31
#  include <sys/time.h>
32
#  include <sys/socket.h>
33
#  include <sys/select.h>
34
#  include <netinet/in.h>
35
#  include <arpa/inet.h>
36
#  include <unistd.h>
37
#  include <pthread.h>
38
#  include <limits.h>
39
#endif
40
41
#ifdef WIN32
42
#ifndef WIN32_LEAN_AND_MEAN
43
#define WIN32_LEAN_AND_MEAN
44
#endif
45
#  include <windows.h>
46
#  include <winsock2.h>
47
#  include <cstdint>
48
#undef WIN32_LEAN_AND_MEAN
49
#  include <errno.h>
50
#  include <io.h>
51
#ifdef UNDER_CE
52
#include "wince/WceCompat.hxx"
53
#endif // UNDER_CE
54
55
#if defined(_MSC_VER) && _MSC_VER < 1900
56
#include <stdio.h>
57
#ifndef snprintf
58
#define snprintf c99_snprintf
59
60
inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
61
{
62
    int count = -1;
63
64
    if (size != 0)
65
        count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
66
    if (count == -1)
67
        count = _vscprintf(format, ap);
68
69
    return count;
70
}
71
72
inline int c99_snprintf(char* str, size_t size, const char* format, ...)
73
{
74
    int count;
75
    va_list ap;
76
77
    va_start(ap, format);
78
    count = c99_vsnprintf(str, size, format, ap);
79
    va_end(ap);
80
81
    return count;
82
}
83
#endif
84
#endif // _MSC_VER
85
86
#endif
87
88
#define RESIP_MAX_SOCKADDR_SIZE 28
89
90
#if defined(__APPLE__)
91
   // .amr. If you get linker or type conflicts around UInt32, then use this define
92
#  if defined(RESIP_APPLE_USE_SYSTEM_TYPES)
93
#     include <TargetConditionals.h>
94
#     include <CoreServices/CoreServices.h>
95
#  endif
96
#  if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_2
97
      // you don't include the SDK or you're running 10.3 or above
98
      // note: this will fail on 10.2 if you don't include the SDK
99
#     include <arpa/nameser_compat.h>
100
#  else
101
      // you include the SDK and you're running Mac OS 10.2 or below
102
      typedef int socklen_t;
103
#  endif
104
#  ifdef __MWERKS__ /* this is a <limits.h> bug filed with Apple, Radar# 3657629. */
105
#    ifndef __SCHAR_MAX__ 
106
#      define __SCHAR_MAX__ 127
107
#    endif
108
#  endif
109
#endif
110
111
#if defined(__SUNPRO_CC)
112
#  if defined(_TIME_T)
113
    using std::time_t;
114
#  endif
115
#  include <time.h>
116
#  include <memory.h>
117
#  include <string.h>
118
#endif
119
120
#if !defined(T_NAPTR)
121
#  define T_NAPTR 35
122
#endif
123
124
#if !defined(T_SRV)
125
#  define T_SRV 33
126
#endif
127
128
#if !defined(T_AAAA)
129
#  define T_AAAA 28
130
#endif
131
132
#if !defined(T_A)
133
#  define T_A 1
134
#endif
135
136
#if defined( TARGET_OS_IPHONE )
137
// TARGET_OS_IPHONE can be 0 or 1, so must also check the value
138
#if TARGET_OS_IPHONE
139
#define REQUIRE_SO_NOSIGPIPE
140
#endif
141
#endif
142
143
//typedef struct { unsigned char octet[16]; }  UInt128;
144
145
namespace resip
146
{
147
148
#if defined(WIN32) || defined(__QNX__)
149
#ifndef strcasecmp
150
#  define strcasecmp(a,b)    _stricmp(a,b)
151
#endif
152
#ifndef strncasecmp
153
#  define strncasecmp(a,b,c) _strnicmp(a,b,c)
154
#endif
155
#endif
156
157
#if defined(__QNX__) || defined(__sun) || defined(WIN32)
158
  typedef unsigned int u_int32_t;
159
#endif
160
161
template<typename _Tp>
162
inline const _Tp&
163
resipMin(const _Tp& __a, const _Tp& __b)
164
12
{
165
12
   if (__b < __a)
166
0
   {
167
0
      return __b;
168
0
   }
169
170
12
   return __a;
171
12
}
172
173
template<typename _Tp>
174
inline const _Tp&
175
resipMax(const _Tp& __a, const _Tp& __b) 
176
0
{
177
0
   if (__a < __b)
178
0
   {
179
0
      return __b;
180
0
   }
181
182
0
   return __a;
183
0
}
184
185
template<typename _Tp1, typename _Tp2>
186
inline const _Tp1
187
resipIntDiv(const _Tp1& __a, const _Tp2& __b)
188
{
189
   // .bwc. Divide-round-nearest without using any floating-point.
190
   if(__a%__b > __b/2)
191
   {
192
      return __a/__b+1;
193
   }
194
195
   return __a/__b;
196
}
197
198
#if defined(WORDS_BIGENDIAN) || defined(_BIG_ENDIAN) || defined( __BIG_ENDIAN__ ) || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(RESIP_BIG_ENDIAN)
199
200
inline uint64_t
201
ntoh64(const uint64_t input)
202
{
203
   return input;
204
}
205
206
inline uint64_t
207
hton64(const uint64_t input)
208
{
209
   return input;
210
}
211
212
#else
213
214
inline uint64_t
215
ntoh64(const uint64_t input)
216
0
{
217
0
   uint64_t rval;
218
0
   uint8_t *data = (uint8_t *)&rval;
219
0
220
0
   data[0] = (uint8_t)((input >> 56) & 0xFF);
221
0
   data[1] = (uint8_t)((input >> 48) & 0xFF);
222
0
   data[2] = (uint8_t)((input >> 40) & 0xFF);
223
0
   data[3] = (uint8_t)((input >> 32) & 0xFF);
224
0
   data[4] = (uint8_t)((input >> 24) & 0xFF);
225
0
   data[5] = (uint8_t)((input >> 16) & 0xFF);
226
0
   data[6] = (uint8_t)((input >> 8) & 0xFF);
227
0
   data[7] = (uint8_t)((input >> 0) & 0xFF);
228
0
229
0
   return rval;
230
0
}
231
232
inline uint64_t
233
hton64(const uint64_t input)
234
0
{
235
0
   return (ntoh64(input));
236
0
}
237
238
#endif
239
240
inline std::string strError(int err)
241
0
{
242
0
   char buf[256];
243
#ifdef _MSC_VER
244
   strerror_s(buf, sizeof(buf), err);
245
   return buf;
246
#else
247
0
   char* res = strerror_r(err, buf, sizeof(buf));
248
0
   return std::string(res);
249
0
#endif
250
0
}
251
252
}
253
254
//template "levels; ie REASONABLE and COMPLETE
255
//reasonable allows most things such as partial template specialization,
256
//etc...like most compilers and VC++2003+.
257
//COMPLETE would allow template metaprogramming, template< template< > > tricks,
258
//etc...REASONABLE should always be defined when COMPLETE is defined.
259
260
//#if !defined(__SUNPRO_CC) && !defined(__INTEL_COMPILER)
261
#if !defined(__INTEL_COMPILER)
262
#  define REASONABLE_TEMPLATES
263
#endif
264
265
// .bwc. This is the only place we check for USE_IPV6 in a header file. This 
266
// code has no effect if USE_IPV6 is not set, so this should only kick in when
267
// we're building the resip libs. If someone sets USE_IPV6 while building
268
// against the resip libs, no resip header file will care. 
269
#ifdef USE_IPV6
270
#ifndef IPPROTO_IPV6
271
#if(_WIN32_WINNT >= 0x0501)   // Some versions of the windows SDK define IPPROTO_IPV6 differently - always enable IP v6 if USE_IPV6 and _WIN32_WINNT >= 0x0501
272
#define IPPROTO_IPV6 ::IPPROTO_IPV6  
273
#else
274
#ifdef _MSC_VER
275
#define __STR2__(x) #x
276
#define __STR1__(x) __STR2__(x)
277
#define __LOC__ __FILE__ "("__STR1__(__LINE__)"): "
278
#pragma message (__LOC__ " IPv6 support requested, but IPPROTO_V6 undefined; this platform does not appear to support IPv6 ")
279
#else
280
#warning IPv6 support requested, but IPPROTO_IPV6 undefined; this platform does not appear to support IPv6
281
#endif
282
// .bwc. Don't do this; someone might have defined it for their own code.
283
// #undef USE_IPV6
284
#endif
285
#endif
286
#endif
287
288
#ifndef IPPROTO_SCTP
289
#define IPPROTO_SCTP 132
290
#endif
291
292
// !bwc! Some poking around seems to indicate that icc supports gcc's function 
293
// attributes, at least as far back as version 8. I have no idea what support is 
294
// like prior to that. As for SUNPRO, it uses gcc's frontend, so I would expect 
295
// gnu-c function attributes to work, but does it define __GNUC__?
296
#if defined(__GNUC__) || (__INTEL_COMPILER > 800)
297
#define RESIP_DEPRECATED(x) x __attribute__ ((deprecated))
298
#elif defined(_MSC_VER)
299
#define RESIP_DEPRECATED(x) __declspec(deprecated) x
300
#else
301
#define RESIP_DEPRECATED(x) x
302
#endif
303
304
// These used to live in resipfaststreams.hxx and may only be used there
305
#if (defined(WIN32) || defined(_WIN32_WCE))
306
307
#if (defined(_MSC_VER) && _MSC_VER >= 1400 )
308
#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf_s(buffer,sizeofBuffer,_TRUNCATE,format,var1)
309
#define LTOA(value,string,sizeofstring,radix) _ltoa_s(value,string,sizeofstring,radix)
310
#define ULTOA(value,string,sizeofstring,radix) _ultoa_s(value,string,sizeofstring,radix)
311
#define I64TOA(value,string,sizeofstring,radix) _i64toa_s(value,string,sizeofstring,radix)
312
#define UI64TOA(value,string,sizeofstring,radix) _ui64toa_s(value,string,sizeofstring,radix)
313
#define GCVT(val,num,buffer,buffersize) _gcvt_s(buffer,buffersize,val,num)
314
#else
315
#define _TRUNCATE -1
316
#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf(buffer,count,format,var1)
317
#define LTOA(value,string,sizeofstring,radix) _ltoa(value,string,radix)
318
#define ULTOA(value,string,sizeofstring,radix) _ultoa(value,string,radix)
319
#define I64TOA(value,string,sizeofstring,radix) _i64toa(value,string,radix)
320
#define UI64TOA(value,string,sizeofstring,radix) _ui64toa(value,string,radix)
321
#define GCVT(val,sigdigits,buffer,buffersize) _gcvt(val,sigdigits,buffer)
322
#endif
323
324
#else //non-windows
325
#define _TRUNCATE -1
326
#define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) snprintf(buffer,sizeofBuffer,format,var1)
327
#define LTOA(l,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%li",l)
328
#define ULTOA(ul,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%lu",ul)
329
#define I64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%lli",value)
330
#define UI64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%llu",value)
331
#define GCVT(f,sigdigits,buffer,bufferlen) SNPRINTF_1(buffer,bufferlen,bufferlen,"%f",f)
332
#define _CVTBUFSIZE 309+40
333
#endif
334
335
#endif
336
337
/* ====================================================================
338
 * The Vovida Software License, Version 1.0 
339
 * 
340
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
341
 * 
342
 * Redistribution and use in source and binary forms, with or without
343
 * modification, are permitted provided that the following conditions
344
 * are met:
345
 * 
346
 * 1. Redistributions of source code must retain the above copyright
347
 *    notice, this list of conditions and the following disclaimer.
348
 * 
349
 * 2. Redistributions in binary form must reproduce the above copyright
350
 *    notice, this list of conditions and the following disclaimer in
351
 *    the documentation and/or other materials provided with the
352
 *    distribution.
353
 * 
354
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
355
 *    and "Vovida Open Communication Application Library (VOCAL)" must
356
 *    not be used to endorse or promote products derived from this
357
 *    software without prior written permission. For written
358
 *    permission, please contact vocal@vovida.org.
359
 *
360
 * 4. Products derived from this software may not be called "VOCAL", nor
361
 *    may "VOCAL" appear in their name, without prior written
362
 *    permission of Vovida Networks, Inc.
363
 * 
364
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
365
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
366
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
367
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
368
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
369
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
370
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
371
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
372
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
373
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
374
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
375
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
376
 * DAMAGE.
377
 * 
378
 * ====================================================================
379
 */