Coverage Report

Created: 2023-06-07 06:03

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