Coverage Report

Created: 2026-02-09 06:28

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