1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16 
17     Change History (most recent first):
18 
19 Log: CommonServices.h,v $
20 Revision 1.11  2009/03/30 19:51:29  herscher
21 <rdar://problem/5925472> Current Bonjour code does not compile on Windows
22 <rdar://problem/5187308> Move build train to Visual Studio 2005
23 
24 Revision 1.10  2009/01/11 03:20:06  mkrochma
25 <rdar://problem/5797526> Fixes from Igor Seleznev to get mdnsd working on Solaris
26 
27 Revision 1.9  2009/01/10 22:03:43  mkrochma
28 <rdar://problem/5797507> dnsextd fails to build on Linux
29 
30 Revision 1.8  2007/01/17 19:16:59  cheshire
31 Only define ssize_t if it's not already defined
32 
33 Revision 1.7  2007/01/16 23:00:45  cheshire
34 Don't need to include CoreServices.h
35 
36 Revision 1.6  2006/08/24 22:41:53  herscher
37 <rdar://problem/4580067> POSIX: dnsextd_parser doesn't compile on Linux
38 
39 Revision 1.5  2006/08/14 23:24:56  cheshire
40 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
41 
42 Revision 1.4  2006/07/05 22:43:21  cheshire
43 <rdar://problem/4472014> Add Private DNS client functionality to mDNSResponder
44 
45 Revision 1.3  2004/04/08 09:27:12  bradley
46 Added macro for portable specification of callback calling conventions.
47 
48 Revision 1.2  2004/03/07 05:53:39  bradley
49 Fixed NumVersion extraction macros. Updated error code mappings to match latest internal version.
50 
51 Revision 1.1  2004/01/30 02:25:59  bradley
52 Common Services and portability support for various platforms.
53 
54 */
55 
56 //---------------------------------------------------------------------------------------------------------------------------
57 /*!	@header		CommonServices
58 
59 	Common Services for Mac OS X, Linux, Palm, VxWorks, Windows, and Windows CE.
60 */
61 
62 #ifndef	__COMMON_SERVICES__
63 #define	__COMMON_SERVICES__
64 
65 #ifdef	__cplusplus
66 	extern "C" {
67 #endif
68 
69 #if 0
70 #pragma mark == Target ==
71 #endif
72 
73 //===========================================================================================================================
74 //	 Target
75 //===========================================================================================================================
76 
77 // Macintosh
78 
79 #if( !defined( TARGET_OS_MAC ) )
80 	#if( ( macintosh || __MACH__ ) && !KERNEL )
81 		// ConditionalMacros.h in CoreServices will define this TARGET_* flag.
82 	#else
83 		#define	TARGET_OS_MAC			0
84 	#endif
85 #endif
86 
87 #if( !defined( TARGET_API_MAC_OSX_KERNEL ) )
88 	#if( __MACH__ && KERNEL )
89 		#define	TARGET_API_MAC_OSX_KERNEL		1
90 	#else
91 		#define	TARGET_API_MAC_OSX_KERNEL		0
92 	#endif
93 #endif
94 
95 // Linux
96 
97 #if( !defined( TARGET_OS_LINUX ) )
98 	#if( defined( __linux__ ) )
99 		#define	TARGET_OS_LINUX			1
100 	#else
101 		#define	TARGET_OS_LINUX			0
102 	#endif
103 #endif
104 
105 // Solaris
106 
107 #if( !defined( TARGET_OS_SOLARIS ) )
108 	#if( defined(solaris) || (defined(__SVR4) && defined(sun)) )
109 		#define	TARGET_OS_SOLARIS		1
110 	#else
111 		#define	TARGET_OS_SOLARIS		0
112 	#endif
113 #endif
114 
115 // Palm
116 
117 #if( !defined( TARGET_OS_PALM ) )
118 	#if( defined( __PALMOS_TRAPS__ ) || defined( __PALMOS_ARMLET__ ) )
119 		#define	TARGET_OS_PALM			1
120 	#else
121 		#define	TARGET_OS_PALM			0
122 	#endif
123 #endif
124 
125 // VxWorks
126 
127 #if( !defined( TARGET_OS_VXWORKS ) )
128 
129 	// No predefined macro for VxWorks so just assume VxWorks if nothing else is set.
130 
131 	#if( !macintosh && !__MACH__  && !defined( __linux__ ) && !defined ( __SVR4 ) && !defined ( __sun ) && !defined( __PALMOS_TRAPS__ ) && !defined( __PALMOS_ARMLET__ ) && !defined( _WIN32 ) )
132 		#define	TARGET_OS_VXWORKS		1
133 	#else
134 		#define	TARGET_OS_VXWORKS		0
135 	#endif
136 #endif
137 
138 // Windows
139 
140 #if( !defined( TARGET_OS_WIN32 ) )
141 	#if( macintosh || __MACH__ )
142 		// ConditionalMacros.h in CoreServices will define this TARGET_* flag.
143 	#else
144 		#if( defined( _WIN32 ) )
145 			#define	TARGET_OS_WIN32		1
146 		#else
147 			#define	TARGET_OS_WIN32		0
148 		#endif
149 	#endif
150 #endif
151 
152 // Windows CE
153 
154 #if( !defined( TARGET_OS_WINDOWS_CE ) )
155 	#if( defined( _WIN32_WCE ) )
156 		#define	TARGET_OS_WINDOWS_CE	1
157 	#else
158 		#define	TARGET_OS_WINDOWS_CE	0
159 	#endif
160 #endif
161 
162 #if 0
163 #pragma mark == Includes ==
164 #endif
165 
166 //===========================================================================================================================
167 //	 Includes
168 //===========================================================================================================================
169 
170 #if( !KERNEL )
171 	#if defined(WIN32) && !defined(_WSPIAPI_COUNTOF)
172 		#define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
173 	#endif
174 	#include	<stddef.h>
175 #endif
176 
177 #if( ( macintosh || __MACH__ ) && !KERNEL )
178 
179 	#if( defined( __MWERKS__ ) )
180 		#if( __option( c9x ) )
181 			#include	<stdbool.h>
182 		#endif
183 	#else
184 		#include	<stdbool.h>
185 	#endif
186 
187 	#include	<stdint.h>
188 
189 	#if( __MACH__ )
190 
191 		// Mac OS X
192 
193 		#include	<sys/types.h>
194 		#include	<netinet/in.h>
195 		#include	<arpa/inet.h>
196 		#include	<fcntl.h>
197 		#include	<pthread.h>
198 		#include	<sys/ioctl.h>
199 		#include	<sys/socket.h>
200 		#include	<unistd.h>
201 
202 	#else
203 
204 		// Classic Mac OS
205 
206 		#include	<ConditionalMacros.h>
207 		#include	<MacTypes.h>
208 
209 	#endif
210 
211 #elif( KERNEL )
212 
213 	// Mac OS X Kernel
214 
215 	#include	<stdint.h>
216 
217 	#include	<libkern/OSTypes.h>
218 	#include	<sys/types.h>
219 
220 #elif( TARGET_OS_LINUX )
221 
222 	// Linux
223 
224 	#include	<stdint.h>
225 	#include	<arpa/inet.h>
226 
227 #elif( TARGET_OS_SOLARIS )
228 
229 	// Solaris
230 
231 	#include	<stdint.h>
232 
233 	#include	<arpa/inet.h>
234 	#include	<arpa/nameser.h>
235 
236 	#if ( defined( BYTE_ORDER ) && defined( LITTLE_ENDIAN ) && ( BYTE_ORDER == LITTLE_ENDIAN ) )
237 		#define TARGET_RT_LITTLE_ENDIAN		1
238 	#endif
239 	#if ( defined( BYTE_ORDER ) && defined( BIG_ENDIAN ) && ( BYTE_ORDER == BIG_ENDIAN ) )
240 		#define TARGET_RT_BIG_ENDIAN		1
241 	#endif
242 
243 #elif( TARGET_OS_PALM )
244 
245 	// Palm (no special includes yet).
246 
247 #elif( TARGET_OS_VXWORKS )
248 
249 	// VxWorks
250 
251 	#include	"vxWorks.h"
252 
253 #elif( TARGET_OS_WIN32 )
254 
255 	// Windows
256 
257 	#if( !defined( WIN32_WINDOWS ) )
258 		#define	WIN32_WINDOWS		0x0401
259 	#endif
260 
261 	#if( !defined( _WIN32_WINDOWS ) )
262 		#define	_WIN32_WINDOWS		0x0401
263 	#endif
264 
265 	#if( !defined( WIN32_LEAN_AND_MEAN ) )
266 		#define	WIN32_LEAN_AND_MEAN			// Needed to avoid redefinitions by Windows interfaces.
267 	#endif
268 
269 	#if( defined( __MWERKS__ ) )
270 
271 		#if( __option( c9x ) )
272 			#include	<stdbool.h>
273 		#endif
274 
275 		#include	<stdint.h>
276 
277 	#elif( defined( _MSC_VER ) )
278 
279 		#pragma warning( disable:4127 )		// Disable "conditional expression is constant" warning for debug macros.
280 		#pragma warning( disable:4706 )		// Disable "assignment within conditional expression" for Microsoft headers.
281 
282 	#endif
283 
284 	#include	<windows.h>
285 	#include	<winsock2.h>
286 	#include	<Ws2tcpip.h>
287 
288 	#if( defined( _MSC_VER ) )
289 		#pragma warning( default:4706 )
290 	#endif
291 
292 #else
293 	#error unknown OS - update this file to support your OS
294 #endif
295 
296 #if( !defined( TARGET_BUILD_MAIN ) )
297 	#if( !TARGET_OS_VXWORKS )
298 		#define	TARGET_BUILD_MAIN		1
299 	#endif
300 #endif
301 
302 #if( __GNUC__ || !TARGET_OS_VXWORKS )
303 	#define	TARGET_LANGUAGE_C_LIKE		1
304 #else
305 	#define	TARGET_LANGUAGE_C_LIKE		0
306 #endif
307 
308 #if 0
309 #pragma mark == CPU ==
310 #endif
311 
312 //===========================================================================================================================
313 //	CPU
314 //===========================================================================================================================
315 
316 // PowerPC
317 
318 #if( !defined( TARGET_CPU_PPC ) )
319 	#if( defined( __ppc__ ) || defined( __PPC__ ) || defined( powerpc ) || defined( ppc ) || defined( _M_MPPC ) )
320 		#define	TARGET_CPU_PPC				1
321 	#else
322 		#define	TARGET_CPU_PPC				0
323 	#endif
324 #endif
325 
326 // x86
327 
328 #if( !defined( TARGET_CPU_X86 ) )
329 	#if( __INTEL__ || defined( __i386__ ) || defined( i386 ) || defined( intel ) || defined( _M_IX86 ) )
330 		#define	TARGET_CPU_X86				1
331 	#else
332 		#define	TARGET_CPU_X86				0
333 	#endif
334 #endif
335 
336 // MIPS
337 
338 #if( !defined( TARGET_CPU_MIPS ) )
339 	#if( __MIPS__ || defined( MIPS32 ) || defined( R3000 ) || defined( R4000 ) || defined( R4650 ) || defined( _M_MRX000 ) )
340 		#define	TARGET_CPU_MIPS				1
341 	#else
342 		#define	TARGET_CPU_MIPS				0
343 	#endif
344 #endif
345 
346 #if( !defined( TARGET_CPU_PPC ) && !defined( TARGET_CPU_X86 ) && !defined( TARGET_CPU_MIPS ) )
347 	#error unknown CPU - update this file to support your CPU
348 #endif
349 
350 #if 0
351 #pragma mark == Byte Order ==
352 #endif
353 
354 //===========================================================================================================================
355 //	Byte Order
356 //===========================================================================================================================
357 
358 // TARGET_RT_LITTLE_ENDIAN
359 
360 #if( !defined( TARGET_RT_LITTLE_ENDIAN ) )
361 	#if( MIPSEL || IL_LITTLE_ENDIAN || defined( __LITTLE_ENDIAN__ ) 										|| \
362 		 ( defined(   BYTE_ORDER ) && defined(   LITTLE_ENDIAN ) && (   BYTE_ORDER ==   LITTLE_ENDIAN ) )	|| \
363 		 ( defined(  _BYTE_ORDER ) && defined(  _LITTLE_ENDIAN ) && (  _BYTE_ORDER ==  _LITTLE_ENDIAN ) )	|| \
364 		 ( defined( __BYTE_ORDER ) && defined( __LITTLE_ENDIAN ) && ( __BYTE_ORDER == __LITTLE_ENDIAN ) )	|| \
365 		 TARGET_CPU_X86 || ( defined( TARGET_RT_BIG_ENDIAN ) && !TARGET_RT_BIG_ENDIAN ) )
366 		#define	TARGET_RT_LITTLE_ENDIAN		1
367 	#else
368 		#define	TARGET_RT_LITTLE_ENDIAN		0
369 	#endif
370 #endif
371 
372 // TARGET_RT_BIG_ENDIAN
373 
374 #if( !defined( TARGET_RT_BIG_ENDIAN ) )
375 	#if( MIPSEB || IL_BIG_ENDIAN || defined( __BIG_ENDIAN__ ) 										|| \
376 		 ( defined(   BYTE_ORDER ) && defined(   BIG_ENDIAN ) && (   BYTE_ORDER ==   BIG_ENDIAN ) )	|| \
377 		 ( defined(  _BYTE_ORDER ) && defined(  _BIG_ENDIAN ) && (  _BYTE_ORDER ==  _BIG_ENDIAN ) )	|| \
378 		 ( defined( __BYTE_ORDER ) && defined( __BIG_ENDIAN ) && ( __BYTE_ORDER == __BIG_ENDIAN ) )	|| \
379 		( defined( TARGET_RT_LITTLE_ENDIAN ) && !TARGET_RT_LITTLE_ENDIAN ) )
380 		#define	TARGET_RT_BIG_ENDIAN		1
381 	#else
382 		#define	TARGET_RT_BIG_ENDIAN		0
383 	#endif
384 #endif
385 
386 #if( defined( TARGET_RT_LITTLE_ENDIAN ) && !defined( TARGET_RT_BIG_ENDIAN ) )
387 	#if( TARGET_RT_LITTLE_ENDIAN )
388 		#define	TARGET_RT_BIG_ENDIAN		0
389 	#else
390 		#define	TARGET_RT_BIG_ENDIAN		1
391 	#endif
392 #endif
393 
394 #if( defined( TARGET_RT_BIG_ENDIAN ) && !defined( TARGET_RT_LITTLE_ENDIAN ) )
395 	#if( TARGET_RT_BIG_ENDIAN )
396 		#define	TARGET_RT_LITTLE_ENDIAN		0
397 	#else
398 		#define	TARGET_RT_LITTLE_ENDIAN		1
399 	#endif
400 #endif
401 
402 #if( !defined( TARGET_RT_LITTLE_ENDIAN ) || !defined( TARGET_RT_BIG_ENDIAN ) )
403 	#error unknown byte order - update this file to support your byte order
404 #endif
405 
406 // TARGET_RT_BYTE_ORDER
407 
408 #if( !defined( TARGET_RT_BYTE_ORDER_BIG_ENDIAN ) )
409 	#define	TARGET_RT_BYTE_ORDER_BIG_ENDIAN			1234
410 #endif
411 
412 #if( !defined( TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN ) )
413 	#define	TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN		4321
414 #endif
415 
416 #if( !defined( TARGET_RT_BYTE_ORDER ) )
417 	#if( TARGET_RT_LITTLE_ENDIAN )
418 		#define	TARGET_RT_BYTE_ORDER				TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN
419 	#else
420 		#define	TARGET_RT_BYTE_ORDER				TARGET_RT_BYTE_ORDER_BIG_ENDIAN
421 	#endif
422 #endif
423 
424 #if 0
425 #pragma mark == Constants ==
426 #endif
427 
428 //===========================================================================================================================
429 //	Constants
430 //===========================================================================================================================
431 
432 #if( !TARGET_OS_MAC )
433 	#define CR		'\r'
434 #endif
435 
436 #define LF			'\n'
437 #define	CRSTR		"\r"
438 #define	LFSTR		"\n"
439 #define CRLF		"\r\n"
440 #define CRCR 		"\r\r"
441 
442 #if 0
443 #pragma mark == Compatibility ==
444 #endif
445 
446 //===========================================================================================================================
447 //	Compatibility
448 //===========================================================================================================================
449 
450 // Macros to allow the same code to work on Windows and other sockets API-compatible platforms.
451 
452 #if( TARGET_OS_WIN32 )
453 	#define	close_compat( X )		closesocket( X )
454 	#define	errno_compat()			(int) GetLastError()
455 	#define	set_errno_compat( X )	SetLastError( X )
456 	#define	EWOULDBLOCK_compat		WSAEWOULDBLOCK
457 	#define	ETIMEDOUT_compat		WSAETIMEDOUT
458 	#define	ENOTCONN_compat			WSAENOTCONN
459 	#define	IsValidSocket( X )		( ( X ) != INVALID_SOCKET )
460 	#define	kInvalidSocketRef		INVALID_SOCKET
461 	#if( TARGET_LANGUAGE_C_LIKE )
462 		typedef SOCKET				SocketRef;
463 	#endif
464 #else
465 	#define	close_compat( X )		close( X )
466 	#define	errno_compat()			errno
467 	#define	set_errno_compat( X )	do { errno = ( X ); } while( 0 )
468 	#define	EWOULDBLOCK_compat		EWOULDBLOCK
469 	#define	ETIMEDOUT_compat		ETIMEDOUT
470 	#define	ENOTCONN_compat			ENOTCONN
471 	#define	IsValidSocket( X )		( ( X ) >= 0 )
472 	#define	kInvalidSocketRef		-1
473 	#if( TARGET_LANGUAGE_C_LIKE )
474 		typedef int					SocketRef;
475 	#endif
476 #endif
477 
478 // socklen_t is not defined on the following platforms so emulate it if not defined:
479 //
480 // - Pre-Panther Mac OS X. Panther defines SO_NOADDRERR so trigger off that.
481 // - Windows SDK prior to 2003. 2003+ SDK's define EAI_AGAIN so trigger off that.
482 // - VxWorks
483 
484 #if( TARGET_LANGUAGE_C_LIKE )
485 	#if( ( TARGET_OS_MAC && !defined( SO_NOADDRERR ) ) || ( TARGET_OS_WIN32 && !defined( EAI_AGAIN ) ) || TARGET_OS_VXWORKS )
486 		typedef int						socklen_t;
487 	#endif
488 #endif
489 
490 // ssize_t is not defined on the following platforms so emulate it if not defined:
491 //
492 // - Mac OS X when not building with BSD headers
493 // - Windows
494 
495 #if( TARGET_LANGUAGE_C_LIKE )
496 	#if( !defined(_SSIZE_T) && ( TARGET_OS_WIN32 || !defined( _BSD_SSIZE_T_DEFINED_ ) ) && !TARGET_OS_LINUX && !TARGET_OS_VXWORKS && !TARGET_OS_MAC)
497 		typedef int						ssize_t;
498 	#endif
499 #endif
500 
501 // sockaddr_storage is not supported on non-IPv6 machines so alias it to an IPv4-compatible structure.
502 
503 #if( TARGET_LANGUAGE_C_LIKE )
504 	#if( !defined( AF_INET6 ) )
505 		#define	sockaddr_storage		sockaddr_in
506 		#define	ss_family				sin_family
507 	#endif
508 #endif
509 
510 //---------------------------------------------------------------------------------------------------------------------------
511 /*!	@defined	SOCKADDR_IS_IP_LOOPBACK
512 
513 	@abstract	Determines if a sockaddr is an IPv4 or IPv6 loopback address (if IPv6 is supported).
514 */
515 
516 #if( defined( AF_INET6 ) )
517 	#define	SOCKADDR_IS_IP_LOOPBACK( SA )															\
518 		( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET )   							\
519 		? ( ( (const struct sockaddr_in *)( SA ) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) )	\
520 		: ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET6 ) 							\
521 			? IN6_IS_ADDR_LOOPBACK( &( (const struct sockaddr_in6 *)( SA ) )->sin6_addr ) 			\
522 			: 0
523 #else
524 	#define	SOCKADDR_IS_IP_LOOPBACK( SA )															\
525 		( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET )  								\
526 		? ( ( (const struct sockaddr_in *)( SA ) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) 	\
527 		: 0
528 #endif
529 
530 //---------------------------------------------------------------------------------------------------------------------------
531 /*!	@defined	SOCKADDR_IS_IP_LINK_LOCAL
532 
533 	@abstract	Determines if a sockaddr is an IPv4 or IPv6 link-local address (if IPv6 is supported).
534 */
535 
536 #if( defined( AF_INET6 ) )
537 	#define	SOCKADDR_IS_IP_LINK_LOCAL( SA )																\
538 		( ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET )   								\
539 		  ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 0 ] == 169 ) && 	\
540 			  ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 1 ] == 254 ) )	\
541 		  : IN6_IS_ADDR_LOOPBACK( &( (const struct sockaddr_in6 *)( SA ) )->sin6_addr ) )
542 #else
543 	#define	SOCKADDR_IS_IP_LINK_LOCAL( SA )																\
544 		( ( ( (const struct sockaddr *)( SA ) )->sa_family == AF_INET )   								\
545 		  ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 0 ] == 169 ) && 	\
546 			  ( ( (uint8_t *)( &( (const struct sockaddr_in *)( SA ) )->sin_addr ) )[ 1 ] == 254 ) )	\
547 		  : 0 )
548 #endif
549 
550 // _beginthreadex and _endthreadex are not supported on Windows CE 2.1 or later (the C runtime issues with leaking
551 // resources have apparently been resolved and they seem to have just ripped out support for the API) so map it to
552 // CreateThread on Windows CE.
553 
554 #if( TARGET_OS_WINDOWS_CE )
555 	#define	_beginthreadex_compat( SECURITY_PTR, STACK_SIZE, START_ADDRESS, ARG_LIST, FLAGS, THREAD_ID_PTR )			\
556 		(uintptr_t) CreateThread( SECURITY_PTR, STACK_SIZE, (LPTHREAD_START_ROUTINE) START_ADDRESS, ARG_LIST, FLAGS, 	\
557 					  (LPDWORD) THREAD_ID_PTR )
558 
559 	#define	_endthreadex_compat( RESULT )		ExitThread( (DWORD) RESULT )
560 #elif( TARGET_OS_WIN32 )
561 	#define	_beginthreadex_compat				_beginthreadex
562 	#define	_endthreadex_compat					_endthreadex
563 #endif
564 
565 // The C99 "inline" keyword is not supported by Microsoft compilers, but they do support __inline so map it when needed.
566 
567 #if( defined( _MSC_VER ) )
568 	#define	inline_compat		__inline
569 #else
570 	#define	inline_compat		inline
571 #endif
572 
573 // Calling conventions
574 
575 #if( !defined( CALLBACK_COMPAT ) )
576 	#if( TARGET_OS_WIN32 || TARGET_OS_WINDOWS_CE )
577 		#define	CALLBACK_COMPAT		CALLBACK
578 	#else
579 		#define	CALLBACK_COMPAT
580 	#endif
581 #endif
582 
583 #if 0
584 #pragma mark == Macros ==
585 #endif
586 
587 //---------------------------------------------------------------------------------------------------------------------------
588 /*!	@defined	kSizeCString
589 
590 	@abstract	A meta-value to pass to supported routines to indicate the size should be calculated with strlen.
591 */
592 
593 #define	kSizeCString		( (size_t) -1 )
594 
595 //---------------------------------------------------------------------------------------------------------------------------
596 /*!	@defined	sizeof_array
597 
598 	@abstract	Determines the number of elements in an array.
599 */
600 
601 #define	sizeof_array( X )		( sizeof( X ) / sizeof( X[ 0 ] ) )
602 
603 //---------------------------------------------------------------------------------------------------------------------------
604 /*!	@defined	sizeof_element
605 
606 	@abstract	Determines the size of an array element.
607 */
608 
609 #define	sizeof_element( X )		sizeof( X[ 0 ] )
610 
611 //---------------------------------------------------------------------------------------------------------------------------
612 /*!	@defined	sizeof_string
613 
614 	@abstract	Determines the size of a constant C string, excluding the null terminator.
615 */
616 
617 #define	sizeof_string( X )		( sizeof( ( X ) ) - 1 )
618 
619 //---------------------------------------------------------------------------------------------------------------------------
620 /*!	@defined	sizeof_field
621 
622 	@abstract	Determines the size of a field of a type.
623 */
624 
625 #define	sizeof_field( TYPE, FIELD )		sizeof( ( ( (TYPE *) 0 )->FIELD ) )
626 
627 //---------------------------------------------------------------------------------------------------------------------------
628 /*!	@function	RoundUp
629 
630 	@abstract	Rounds X up to a multiple of Y.
631 */
632 
633 #define	RoundUp( X, Y )		( ( X ) + ( ( Y ) - ( ( X ) % ( Y ) ) ) )
634 
635 //---------------------------------------------------------------------------------------------------------------------------
636 /*!	@function	IsAligned
637 
638 	@abstract	Returns non-zero if X is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
639 */
640 
641 #define	IsAligned( X, Y )		( ( ( X ) & ( ( Y ) - 1 ) ) == 0 )
642 
643 //---------------------------------------------------------------------------------------------------------------------------
644 /*!	@function	IsFieldAligned
645 
646 	@abstract	Returns non-zero if FIELD of type TYPE is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
647 */
648 
649 #define	IsFieldAligned( X, TYPE, FIELD, Y )		IsAligned( ( (uintptr_t)( X ) ) + offsetof( TYPE, FIELD ), ( Y ) )
650 
651 //---------------------------------------------------------------------------------------------------------------------------
652 /*!	@function	AlignDown
653 
654 	@abstract	Aligns X down to a Y byte boundary. Y must be a power of 2.
655 */
656 
657 #define	AlignDown( X, Y )		( ( X ) & ~( ( Y ) - 1 ) )
658 
659 //---------------------------------------------------------------------------------------------------------------------------
660 /*!	@function	AlignUp
661 
662 	@abstract	Aligns X up to a Y byte boundary. Y must be a power of 2.
663 */
664 
665 #define	AlignUp( X, Y )		( ( ( X ) + ( ( Y ) - 1 ) ) & ~( ( Y ) - 1 ) )
666 
667 //---------------------------------------------------------------------------------------------------------------------------
668 /*!	@function	Min
669 
670 	@abstract	Returns the lesser of X and Y.
671 */
672 
673 #if( !defined( Min ) )
674 	#define	Min( X, Y )		( ( ( X ) < ( Y ) ) ? ( X ) : ( Y ) )
675 #endif
676 
677 //---------------------------------------------------------------------------------------------------------------------------
678 /*!	@function	Max
679 
680 	@abstract	Returns the greater of X and Y.
681 */
682 
683 #if( !defined( Max ) )
684 	#define	Max( X, Y )		( ( ( X ) > ( Y ) ) ? ( X ) : ( Y ) )
685 #endif
686 
687 //---------------------------------------------------------------------------------------------------------------------------
688 /*!	@function	InsertBits
689 
690 	@abstract	Inserts BITS (both 0 and 1 bits) into X, controlled by MASK and SHIFT, and returns the result.
691 
692 	@discussion
693 
694 	MASK is the bitmask of the bits in the final position.
695 	SHIFT is the number of bits to shift left for 1 to reach the first bit position of MASK.
696 
697 	For example, if you wanted to insert 0x3 into the leftmost 4 bits of a 32-bit value:
698 
699 	InsertBits( 0, 0x3, 0xF0000000U, 28 ) == 0x30000000
700 */
701 
702 #define	InsertBits( X, BITS, MASK, SHIFT )		( ( ( X ) & ~( MASK ) ) | ( ( ( BITS ) << ( SHIFT ) ) & ( MASK ) ) )
703 
704 //---------------------------------------------------------------------------------------------------------------------------
705 /*!	@function	ExtractBits
706 
707 	@abstract	Extracts bits from X, controlled by MASK and SHIFT, and returns the result.
708 
709 	@discussion
710 
711 	MASK is the bitmask of the bits in the final position.
712 	SHIFT is the number of bits to shift right to right justify MASK.
713 
714 	For example, if you had a 32-bit value (e.g. 0x30000000) wanted the left-most 4 bits (e.g. 3 in this example):
715 
716 	ExtractBits( 0x30000000U, 0xF0000000U, 28 ) == 0x3
717 */
718 
719 #define	ExtractBits( X, MASK, SHIFT )			( ( ( X ) >> ( SHIFT ) ) & ( ( MASK ) >> ( SHIFT ) ) )
720 
721 //---------------------------------------------------------------------------------------------------------------------------
722 /*!	@function	Stringify
723 
724 	@abstract	Stringify's an expression.
725 
726 	@discussion
727 
728 	Stringify macros to process raw text passed via -D options to C string constants. The double-wrapping is necessary
729 	because the C preprocessor doesn't perform its normal argument expansion pre-scan with stringified macros so the
730 	-D macro needs to be expanded once via the wrapper macro then stringified so the raw text is stringified. Otherwise,
731 	the replacement value would be used instead of the symbolic name (only for preprocessor symbols like #defines).
732 
733 	For example:
734 
735 		#define	kMyConstant		1
736 
737 		printf( "%s", Stringify( kMyConstant ) );			// Prints "kMyConstant"
738 		printf( "%s", StringifyExpansion( kMyConstant ) );	// Prints "1"
739 
740 	Non-preprocessor symbols do not have this issue. For example:
741 
742 		enum
743 		{
744 			kMyConstant = 1
745 		};
746 
747 		printf( "%s", Stringify( kMyConstant ) );			// Prints "kMyConstant"
748 		printf( "%s", StringifyExpansion( kMyConstant ) );	// Prints "kMyConstant"
749 
750 	See <http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html> for more info on C preprocessor pre-scanning.
751 */
752 
753 #define	Stringify( X )				# X
754 #define	StringifyExpansion( X )		Stringify( X )
755 
756 #if 0
757 #pragma mark == Types ==
758 #endif
759 
760 #if( TARGET_LANGUAGE_C_LIKE )
761 //===========================================================================================================================
762 //	 Standard Types
763 //===========================================================================================================================
764 
765 #if( !defined( INT8_MIN ) )
766 
767 	#define INT8_MIN					SCHAR_MIN
768 
769 	#if( defined( _MSC_VER ) )
770 
771 		// C99 stdint.h not supported in VC++/VS.NET yet.
772 
773 		typedef INT8					int8_t;
774 		typedef UINT8					uint8_t;
775 		typedef INT16					int16_t;
776 		typedef UINT16					uint16_t;
777 		typedef INT32					int32_t;
778 		typedef UINT32					uint32_t;
779 		typedef __int64					int64_t;
780 		typedef unsigned __int64		uint64_t;
781 
782 	#elif( TARGET_OS_VXWORKS && ( TORNADO_VERSION < 220 ) )
783 		typedef long long				int64_t;
784 		typedef unsigned long long		uint64_t;
785 	#endif
786 
787 	typedef int8_t						int_least8_t;
788 	typedef int16_t						int_least16_t;
789 	typedef int32_t						int_least32_t;
790 	typedef int64_t						int_least64_t;
791 
792 	typedef uint8_t						uint_least8_t;
793 	typedef uint16_t					uint_least16_t;
794 	typedef uint32_t					uint_least32_t;
795 	typedef uint64_t					uint_least64_t;
796 
797 	typedef int8_t						int_fast8_t;
798 	typedef int16_t						int_fast16_t;
799 	typedef int32_t						int_fast32_t;
800 	typedef int64_t						int_fast64_t;
801 
802 	typedef uint8_t						uint_fast8_t;
803 	typedef uint16_t					uint_fast16_t;
804 	typedef uint32_t					uint_fast32_t;
805 	typedef uint64_t					uint_fast64_t;
806 
807 	#if( !defined( _MSC_VER ) || TARGET_OS_WINDOWS_CE )
808 		typedef long int				intptr_t;
809 		typedef unsigned long int		uintptr_t;
810 	#endif
811 
812 #endif
813 
814 // Macros for minimum-width integer constants
815 
816 #if( !defined( INT8_C ) )
817 	#define INT8_C( value )			value
818 #endif
819 
820 #if( !defined( INT16_C ) )
821 	#define INT16_C( value )		value
822 #endif
823 
824 #if( !defined( INT32_C ) )
825 	#define INT32_C( value )		value ## L
826 #endif
827 
828 #if( !defined( INT64_C ) )
829 	#if( defined( _MSC_VER ) )
830 		#define INT64_C( value )	value ## i64
831 	#else
832 		#define INT64_C( value )	value ## LL
833 	#endif
834 #endif
835 
836 #if( !defined( UINT8_C ) )
837 	#define UINT8_C( value )		value ## U
838 #endif
839 
840 #if( !defined( UINT16_C ) )
841 	#define UINT16_C( value )		value ## U
842 #endif
843 
844 #if( !defined( UINT32_C ) )
845 	#define UINT32_C( value )		value ## UL
846 #endif
847 
848 #if( !defined( UINT64_C ) )
849 	#if( defined( _MSC_VER ) )
850 		#define UINT64_C( value )	value ## UI64
851 	#else
852 		#define UINT64_C( value )	value ## ULL
853 	#endif
854 #endif
855 
856 #if 0
857 #pragma mark == bool ==
858 #endif
859 
860 //===========================================================================================================================
861 //	 Boolean Constants and Types
862 //===========================================================================================================================
863 
864 // C++ defines bool, true, and false. Metrowerks allows this to be controlled by the "bool" option though.
865 // C99 defines __bool_true_false_are_defined when bool, true, and false are defined.
866 // MacTypes.h defines true and false (Mac builds only).
867 //
868 // Note: The Metrowerks has to be in its own block because Microsoft Visual Studio .NET does not completely
869 // short-circuit and gets confused by the option( bool ) portion of the conditional.
870 
871 #if( defined( __MWERKS__ ) )
872 
873 	// Note: The following test is done on separate lines because CodeWarrior doesn't like it all on one line.
874 
875 	#if( !__bool_true_false_are_defined && ( !defined( __cplusplus ) || !__option( bool ) ) )
876 		#define	COMMON_SERVICES_NEEDS_BOOL		1
877 	#else
878 		#define	COMMON_SERVICES_NEEDS_BOOL		0
879 	#endif
880 
881 	// Workaround when building with CodeWarrior, but using the Apple stdbool.h header, which uses _Bool.
882 
883 	#if( __bool_true_false_are_defined && !defined( __cplusplus ) && !__option( c9x ) )
884 		#define _Bool	int
885 	#endif
886 
887 	// Workaround when building with CodeWarrior for C++ with bool disabled and using the Apple stdbool.h header,
888 	// which defines true and false to map to C++ true and false (which are not enabled). Serenity Now!
889 
890 	#if( __bool_true_false_are_defined && defined( __cplusplus ) && !__option( bool ) )
891 		#define	true	1
892 		#define	false	0
893 	#endif
894 #else
895 	#define	COMMON_SERVICES_NEEDS_BOOL			( !defined( __cplusplus ) && !__bool_true_false_are_defined )
896 #endif
897 
898 #if( COMMON_SERVICES_NEEDS_BOOL )
899 
900 	typedef int		bool;
901 
902 	#define	bool	bool
903 
904 	#if( !defined( __MACTYPES__ ) && !defined( true ) && !defined( false ) )
905 		#define true	1
906 		#define false	0
907 	#endif
908 
909 	#define __bool_true_false_are_defined		1
910 #endif
911 
912 // IOKit IOTypes.h typedef's bool if TYPE_BOOL is not defined so define it here to prevent redefinition by IOTypes.h.
913 
914 #if( TARGET_API_MAC_OSX_KERNEL )
915 	#define TYPE_BOOL		1
916 #endif
917 
918 //---------------------------------------------------------------------------------------------------------------------------
919 /*!	@typedef	CStr255
920 
921 	@abstract	255 character null-terminated (C-style) string.
922 */
923 
924 #if( TARGET_LANGUAGE_C_LIKE )
925 	typedef char	CStr255[ 256 ];
926 #endif
927 
928 #endif	// TARGET_LANGUAGE_C_LIKE
929 
930 //---------------------------------------------------------------------------------------------------------------------------
931 /*!	@defined	TYPE_LONGLONG_NATIVE
932 
933 	@abstract	Defines whether long long (or its equivalent) is natively supported or requires special libraries.
934 */
935 
936 #if( !defined( TYPE_LONGLONG_NATIVE ) )
937 	#if( !TARGET_OS_VXWORKS )
938 		#define	TYPE_LONGLONG_NATIVE			1
939 	#else
940 		#define	TYPE_LONGLONG_NATIVE			0
941 	#endif
942 #endif
943 
944 //---------------------------------------------------------------------------------------------------------------------------
945 /*!	@defined	long_long_compat
946 
947 	@abstract	Compatibility type to map to the closest thing to long long and unsigned long long.
948 
949 	@discussion
950 
951 	Neither long long nor unsigned long long are supported by Microsoft compilers, but they do support proprietary
952 	"__int64" and "unsigned __int64" equivalents so map to those types if the real long long is not supported.
953 */
954 
955 #if( TARGET_LANGUAGE_C_LIKE )
956 	#if( TARGET_OS_WIN32 )
957 		typedef __int64					long_long_compat;
958 		typedef unsigned __int64		unsigned_long_long_compat;
959 	#else
960 		typedef signed long long		long_long_compat;
961 		typedef unsigned long long		unsigned_long_long_compat;
962 	#endif
963 #endif
964 
965 #if 0
966 #pragma mark == Errors ==
967 #endif
968 
969 //---------------------------------------------------------------------------------------------------------------------------
970 /*!	@enum		OSStatus
971 
972 	@abstract	Status Code
973 
974 	@constant	kNoErr						    0 No error occurred.
975 	@constant	kInProgressErr				    1 Operation in progress.
976 	@constant	kUnknownErr					-6700 Unknown error occurred.
977 	@constant	kOptionErr					-6701 Option was not acceptable.
978 	@constant	kSelectorErr				-6702 Selector passed in is invalid or unknown.
979 	@constant	kExecutionStateErr			-6703 Call made in the wrong execution state (e.g. called at interrupt time).
980 	@constant	kPathErr					-6704 Path is invalid, too long, or otherwise not usable.
981 	@constant	kParamErr					-6705 Parameter is incorrect, missing, or not appropriate.
982 	@constant	kParamCountErr				-6706 Incorrect or unsupported number of parameters.
983 	@constant	kCommandErr					-6707 Command invalid or not supported.
984 	@constant	kIDErr						-6708 Unknown, invalid, or inappropriate identifier.
985 	@constant	kStateErr					-6709 Not in appropriate state to perform operation.
986 	@constant	kRangeErr					-6710 Index is out of range or not valid.
987 	@constant	kRequestErr					-6711 Request was improperly formed or not appropriate.
988 	@constant	kResponseErr				-6712 Response was incorrect or out of sequence.
989 	@constant	kChecksumErr				-6713 Checksum does not match the actual data.
990 	@constant	kNotHandledErr				-6714 Operation was not handled (or not handled completely).
991 	@constant	kVersionErr					-6715 Version is not incorrect or not compatibile.
992 	@constant	kSignatureErr				-6716 Signature did not match what was expected.
993 	@constant	kFormatErr					-6717 Unknown, invalid, or inappropriate file/data format.
994 	@constant	kNotInitializedErr			-6718 Action request before needed services were initialized.
995 	@constant	kAlreadyInitializedErr		-6719 Attempt made to initialize when already initialized.
996 	@constant	kNotInUseErr				-6720 Object not in use (e.g. cannot abort if not already in use).
997 	@constant	kInUseErr					-6721 Object is in use (e.g. cannot reuse active param blocks).
998 	@constant	kTimeoutErr					-6722 Timeout occurred.
999 	@constant	kCanceledErr				-6723 Operation canceled (successful cancel).
1000 	@constant	kAlreadyCanceledErr			-6724 Operation has already been canceled.
1001 	@constant	kCannotCancelErr			-6725 Operation could not be canceled (maybe already done or invalid).
1002 	@constant	kDeletedErr					-6726 Object has already been deleted.
1003 	@constant	kNotFoundErr				-6727 Something was not found.
1004 	@constant	kNoMemoryErr				-6728 Not enough memory was available to perform the operation.
1005 	@constant	kNoResourcesErr				-6729 Resources unavailable to perform the operation.
1006 	@constant	kDuplicateErr				-6730 Duplicate found or something is a duplicate.
1007 	@constant	kImmutableErr				-6731 Entity is not changeable.
1008 	@constant	kUnsupportedDataErr			-6732 Data is unknown or not supported.
1009 	@constant	kIntegrityErr				-6733 Data is corrupt.
1010 	@constant	kIncompatibleErr			-6734 Data is not compatible or it is in an incompatible format.
1011 	@constant	kUnsupportedErr				-6735 Feature or option is not supported.
1012 	@constant	kUnexpectedErr				-6736 Error occurred that was not expected.
1013 	@constant	kValueErr					-6737 Value is not appropriate.
1014 	@constant	kNotReadableErr				-6738 Could not read or reading is not allowed.
1015 	@constant	kNotWritableErr				-6739 Could not write or writing is not allowed.
1016 	@constant	kBadReferenceErr			-6740 An invalid or inappropriate reference was specified.
1017 	@constant	kFlagErr					-6741 An invalid, inappropriate, or unsupported flag was specified.
1018 	@constant	kMalformedErr				-6742 Something was not formed correctly.
1019 	@constant	kSizeErr					-6743 Size was too big, too small, or not appropriate.
1020 	@constant	kNameErr					-6744 Name was not correct, allowed, or appropriate.
1021 	@constant	kNotReadyErr				-6745 Device or service is not ready.
1022 	@constant	kReadErr					-6746 Could not read.
1023 	@constant	kWriteErr					-6747 Could not write.
1024 	@constant	kMismatchErr				-6748 Something does not match.
1025 	@constant	kDateErr					-6749 Date is invalid or out-of-range.
1026 	@constant	kUnderrunErr				-6750 Less data than expected.
1027 	@constant	kOverrunErr					-6751 More data than expected.
1028 	@constant	kEndingErr					-6752 Connection, session, or something is ending.
1029 	@constant	kConnectionErr				-6753 Connection failed or could not be established.
1030 	@constant	kAuthenticationErr			-6754 Authentication failed or is not supported.
1031 	@constant	kOpenErr					-6755 Could not open file, pipe, device, etc.
1032 	@constant	kTypeErr					-6756 Incorrect or incompatible type (e.g. file, data, etc.).
1033 	@constant	kSkipErr					-6757 Items should be or was skipped.
1034 	@constant	kNoAckErr					-6758 No acknowledge.
1035 	@constant	kCollisionErr				-6759 Collision occurred (e.g. two on bus at same time).
1036 	@constant	kBackoffErr					-6760 Backoff in progress and operation intentionally failed.
1037 	@constant	kNoAddressAckErr			-6761 No acknowledge of address.
1038 	@constant	kBusyErr					-6762 Cannot perform because something is busy.
1039 	@constant	kNoSpaceErr					-6763 Not enough space to perform operation.
1040 */
1041 
1042 #if( TARGET_LANGUAGE_C_LIKE )
1043 	#if( !TARGET_OS_MAC && !TARGET_API_MAC_OSX_KERNEL )
1044 		typedef int32_t		OSStatus;
1045 	#endif
1046 #endif
1047 
1048 #define kNoErr						0
1049 #define kInProgressErr				1
1050 
1051 // Generic error codes are in the range -6700 to -6779.
1052 
1053 #define kGenericErrorBase			-6700	// Starting error code for all generic errors.
1054 
1055 #define kUnknownErr					-6700
1056 #define kOptionErr					-6701
1057 #define kSelectorErr				-6702
1058 #define kExecutionStateErr			-6703
1059 #define kPathErr					-6704
1060 #define kParamErr					-6705
1061 #define kParamCountErr				-6706
1062 #define kCommandErr					-6707
1063 #define kIDErr						-6708
1064 #define kStateErr					-6709
1065 #define kRangeErr					-6710
1066 #define kRequestErr					-6711
1067 #define kResponseErr				-6712
1068 #define kChecksumErr				-6713
1069 #define kNotHandledErr				-6714
1070 #define kVersionErr					-6715
1071 #define kSignatureErr				-6716
1072 #define kFormatErr					-6717
1073 #define kNotInitializedErr			-6718
1074 #define kAlreadyInitializedErr		-6719
1075 #define kNotInUseErr				-6720
1076 #define kInUseErr					-6721
1077 #define kTimeoutErr					-6722
1078 #define kCanceledErr				-6723
1079 #define kAlreadyCanceledErr			-6724
1080 #define kCannotCancelErr			-6725
1081 #define kDeletedErr					-6726
1082 #define kNotFoundErr				-6727
1083 #define kNoMemoryErr				-6728
1084 #define kNoResourcesErr				-6729
1085 #define kDuplicateErr				-6730
1086 #define kImmutableErr				-6731
1087 #define kUnsupportedDataErr			-6732
1088 #define kIntegrityErr				-6733
1089 #define kIncompatibleErr			-6734
1090 #define kUnsupportedErr				-6735
1091 #define kUnexpectedErr				-6736
1092 #define kValueErr					-6737
1093 #define kNotReadableErr				-6738
1094 #define kNotWritableErr				-6739
1095 #define	kBadReferenceErr			-6740
1096 #define	kFlagErr					-6741
1097 #define	kMalformedErr				-6742
1098 #define	kSizeErr					-6743
1099 #define	kNameErr					-6744
1100 #define	kNotReadyErr				-6745
1101 #define	kReadErr					-6746
1102 #define	kWriteErr					-6747
1103 #define	kMismatchErr				-6748
1104 #define	kDateErr					-6749
1105 #define	kUnderrunErr				-6750
1106 #define	kOverrunErr					-6751
1107 #define	kEndingErr					-6752
1108 #define	kConnectionErr				-6753
1109 #define	kAuthenticationErr			-6754
1110 #define	kOpenErr					-6755
1111 #define	kTypeErr					-6756
1112 #define	kSkipErr					-6757
1113 #define	kNoAckErr					-6758
1114 #define	kCollisionErr				-6759
1115 #define	kBackoffErr					-6760
1116 #define	kNoAddressAckErr			-6761
1117 #define	kBusyErr					-6762
1118 #define	kNoSpaceErr					-6763
1119 
1120 #define kGenericErrorEnd			-6779	// Last generic error code (inclusive)
1121 
1122 #if 0
1123 #pragma mark == Mac Compatibility ==
1124 #endif
1125 
1126 //===========================================================================================================================
1127 //	Mac Compatibility
1128 //===========================================================================================================================
1129 
1130 //---------------------------------------------------------------------------------------------------------------------------
1131 /*!	@enum		Duration
1132 
1133 	@abstract	Type used to specify a duration of time.
1134 
1135 	@constant	kDurationImmediate			Indicates no delay/wait time.
1136 	@constant	kDurationMicrosecond		Microsecond units.
1137 	@constant	kDurationMillisecond		Millisecond units.
1138 	@constant	kDurationSecond				Second units.
1139 	@constant	kDurationMinute				Minute units.
1140 	@constant	kDurationHour				Hour units.
1141 	@constant	kDurationDay				Day units.
1142 	@constant	kDurationForever			Infinite period of time (no timeout).
1143 
1144 	@discussion
1145 
1146 	Duration values are intended to be multiplied by the specific interval to achieve an actual duration. For example,
1147 	to wait for 5 seconds you would use "5 * kDurationSecond".
1148 */
1149 
1150 #if( TARGET_LANGUAGE_C_LIKE )
1151 	#if( !TARGET_OS_MAC )
1152 		typedef	int32_t		Duration;
1153 	#endif
1154 #endif
1155 
1156 #define	kDurationImmediate				0L
1157 #define	kDurationMicrosecond			-1L
1158 #define	kDurationMillisecond			1L
1159 #define	kDurationSecond					( 1000L * kDurationMillisecond )
1160 #define	kDurationMinute					( 60L * kDurationSecond )
1161 #define	kDurationHour					( 60L * kDurationMinute )
1162 #define	kDurationDay					( 24L * kDurationHour )
1163 #define	kDurationForever				0x7FFFFFFFL
1164 
1165 // Seconds <-> Minutes <-> Hours <-> Days <-> Weeks <-> Months <-> Years conversions
1166 
1167 #define kNanosecondsPerMicrosecond		1000
1168 #define kNanosecondsPerMillisecond		1000000
1169 #define kNanosecondsPerSecond			1000000000
1170 #define kMicrosecondsPerSecond			1000000
1171 #define kMicrosecondsPerMillisecond		1000
1172 #define kMillisecondsPerSecond			1000
1173 #define kSecondsPerMinute				60
1174 #define kSecondsPerHour					( 60 * 60 )				// 3600
1175 #define kSecondsPerDay					( 60 * 60 * 24 )		// 86400
1176 #define kSecondsPerWeek					( 60 * 60 * 24 * 7 )	// 604800
1177 #define kMinutesPerHour					60
1178 #define kMinutesPerDay					( 60 * 24 )				// 1440
1179 #define kHoursPerDay					24
1180 #define kDaysPerWeek					7
1181 #define kWeeksPerYear					52
1182 #define kMonthsPerYear					12
1183 
1184 //---------------------------------------------------------------------------------------------------------------------------
1185 /*!	@defined	VersionStages
1186 
1187 	@abstract	NumVersion-style version stages.
1188 */
1189 
1190 #define	kVersionStageDevelopment		0x20
1191 #define	kVersionStageAlpha				0x40
1192 #define	kVersionStageBeta				0x60
1193 #define	kVersionStageFinal				0x80
1194 
1195 //---------------------------------------------------------------------------------------------------------------------------
1196 /*!	@function	NumVersionBuild
1197 
1198 	@abstract	Builds a 32-bit Mac-style NumVersion value (e.g. NumVersionBuild( 1, 2, 3, kVersionStageBeta, 4 ) -> 1.2.3b4).
1199 */
1200 
1201 #define	NumVersionBuild( MAJOR, MINOR, BUGFIX, STAGE, REV )	\
1202 	( ( ( ( MAJOR )  & 0xFF ) << 24 ) |						\
1203 	  ( ( ( MINOR )  & 0x0F ) << 20 ) |						\
1204 	  ( ( ( BUGFIX ) & 0x0F ) << 16 ) |						\
1205 	  ( ( ( STAGE )  & 0xFF ) <<  8 ) |						\
1206 	  ( ( ( REV )    & 0xFF )       ) )
1207 
1208 #define	NumVersionExtractMajor( VERSION )				( (uint8_t)( ( ( VERSION ) >> 24 ) & 0xFF ) )
1209 #define	NumVersionExtractMinorAndBugFix( VERSION )		( (uint8_t)( ( ( VERSION ) >> 16 ) & 0xFF ) )
1210 #define	NumVersionExtractMinor( VERSION )				( (uint8_t)( ( ( VERSION ) >> 20 ) & 0x0F ) )
1211 #define	NumVersionExtractBugFix( VERSION )				( (uint8_t)( ( ( VERSION ) >> 16 ) & 0x0F ) )
1212 #define	NumVersionExtractStage( VERSION )				( (uint8_t)( ( ( VERSION ) >>  8 ) & 0xFF ) )
1213 #define	NumVersionExtractRevision( VERSION )			( (uint8_t)(   ( VERSION )         & 0xFF ) )
1214 
1215 //---------------------------------------------------------------------------------------------------------------------------
1216 /*!	@function	NumVersionCompare
1217 
1218 	@abstract	Compares two NumVersion values and returns the following values:
1219 
1220 		left < right -> -1
1221 		left > right ->  1
1222 		left = right ->  0
1223 */
1224 
1225 #if( TARGET_LANGUAGE_C_LIKE )
1226 	int	NumVersionCompare( uint32_t inLeft, uint32_t inRight );
1227 #endif
1228 
1229 #if 0
1230 #pragma mark == Binary Constants ==
1231 #endif
1232 
1233 //---------------------------------------------------------------------------------------------------------------------------
1234 /*!	@defined	binary_4
1235 
1236 	@abstract	Macro to generate an 4-bit constant using binary notation (e.g. binary_4( 1010 ) == 0xA).
1237 */
1238 
1239 #define	binary_4( a )						binary_4_hex_wrap( hex_digit4( a ) )
1240 #define binary_4_hex_wrap( a )				binary_4_hex( a )
1241 #define binary_4_hex( a )					( 0x ## a )
1242 
1243 //---------------------------------------------------------------------------------------------------------------------------
1244 /*!	@defined	binary_8
1245 
1246 	@abstract	Macro to generate an 8-bit constant using binary notation (e.g. binary_8( 01111011 ) == 0x7B).
1247 */
1248 
1249 #define	binary_8( a )						binary_8_hex_wrap( hex_digit8( a ) )
1250 #define binary_8_hex_wrap( a )				binary_8_hex( a )
1251 #define binary_8_hex( a )					( 0x ## a )
1252 
1253 //---------------------------------------------------------------------------------------------------------------------------
1254 /*!	@defined	binary_16
1255 
1256 	@abstract	Macro to generate an 16-bit constant using binary notation (e.g. binary_16( 01111011, 01111011 ) == 0x7B7B).
1257 */
1258 
1259 #define	binary_16( a, b )					binary_16_hex_wrap( hex_digit8( a ), hex_digit8( b ) )
1260 #define binary_16_hex_wrap( a, b )			binary_16_hex( a, b )
1261 #define binary_16_hex( a, b )				( 0x ## a ## b )
1262 
1263 //---------------------------------------------------------------------------------------------------------------------------
1264 /*!	@defined	binary_32
1265 
1266 	@abstract	Macro to generate an 32-bit constant using binary notation
1267 				(e.g. binary_32( 01111011, 01111011, 01111011, 01111011 ) == 0x7B7B7B7B).
1268 */
1269 
1270 #define	binary_32( a, b, c, d )				binary_32_hex_wrap( hex_digit8( a ), hex_digit8( b ), hex_digit8( c ), hex_digit8( d ) )
1271 #define binary_32_hex_wrap( a, b, c, d )	binary_32_hex( a, b, c, d )
1272 #define binary_32_hex( a, b, c, d )			( 0x ## a ## b ## c ## d )
1273 
1274 // Binary Constant Helpers
1275 
1276 #define hex_digit8( a )						HEX_DIGIT_ ## a
1277 #define hex_digit4( a )						HEX_DIGIT_ ## 0000 ## a
1278 
1279 #define HEX_DIGIT_00000000					00
1280 #define HEX_DIGIT_00000001					01
1281 #define HEX_DIGIT_00000010					02
1282 #define HEX_DIGIT_00000011					03
1283 #define HEX_DIGIT_00000100					04
1284 #define HEX_DIGIT_00000101					05
1285 #define HEX_DIGIT_00000110					06
1286 #define HEX_DIGIT_00000111					07
1287 #define HEX_DIGIT_00001000					08
1288 #define HEX_DIGIT_00001001					09
1289 #define HEX_DIGIT_00001010					0A
1290 #define HEX_DIGIT_00001011					0B
1291 #define HEX_DIGIT_00001100					0C
1292 #define HEX_DIGIT_00001101					0D
1293 #define HEX_DIGIT_00001110					0E
1294 #define HEX_DIGIT_00001111					0F
1295 #define HEX_DIGIT_00010000					10
1296 #define HEX_DIGIT_00010001					11
1297 #define HEX_DIGIT_00010010					12
1298 #define HEX_DIGIT_00010011					13
1299 #define HEX_DIGIT_00010100					14
1300 #define HEX_DIGIT_00010101					15
1301 #define HEX_DIGIT_00010110					16
1302 #define HEX_DIGIT_00010111					17
1303 #define HEX_DIGIT_00011000					18
1304 #define HEX_DIGIT_00011001					19
1305 #define HEX_DIGIT_00011010					1A
1306 #define HEX_DIGIT_00011011					1B
1307 #define HEX_DIGIT_00011100					1C
1308 #define HEX_DIGIT_00011101					1D
1309 #define HEX_DIGIT_00011110					1E
1310 #define HEX_DIGIT_00011111					1F
1311 #define HEX_DIGIT_00100000					20
1312 #define HEX_DIGIT_00100001					21
1313 #define HEX_DIGIT_00100010					22
1314 #define HEX_DIGIT_00100011					23
1315 #define HEX_DIGIT_00100100					24
1316 #define HEX_DIGIT_00100101					25
1317 #define HEX_DIGIT_00100110					26
1318 #define HEX_DIGIT_00100111					27
1319 #define HEX_DIGIT_00101000					28
1320 #define HEX_DIGIT_00101001					29
1321 #define HEX_DIGIT_00101010					2A
1322 #define HEX_DIGIT_00101011					2B
1323 #define HEX_DIGIT_00101100					2C
1324 #define HEX_DIGIT_00101101					2D
1325 #define HEX_DIGIT_00101110					2E
1326 #define HEX_DIGIT_00101111					2F
1327 #define HEX_DIGIT_00110000					30
1328 #define HEX_DIGIT_00110001					31
1329 #define HEX_DIGIT_00110010					32
1330 #define HEX_DIGIT_00110011					33
1331 #define HEX_DIGIT_00110100					34
1332 #define HEX_DIGIT_00110101					35
1333 #define HEX_DIGIT_00110110					36
1334 #define HEX_DIGIT_00110111					37
1335 #define HEX_DIGIT_00111000					38
1336 #define HEX_DIGIT_00111001					39
1337 #define HEX_DIGIT_00111010					3A
1338 #define HEX_DIGIT_00111011					3B
1339 #define HEX_DIGIT_00111100					3C
1340 #define HEX_DIGIT_00111101					3D
1341 #define HEX_DIGIT_00111110					3E
1342 #define HEX_DIGIT_00111111					3F
1343 #define HEX_DIGIT_01000000					40
1344 #define HEX_DIGIT_01000001					41
1345 #define HEX_DIGIT_01000010					42
1346 #define HEX_DIGIT_01000011					43
1347 #define HEX_DIGIT_01000100					44
1348 #define HEX_DIGIT_01000101					45
1349 #define HEX_DIGIT_01000110					46
1350 #define HEX_DIGIT_01000111					47
1351 #define HEX_DIGIT_01001000					48
1352 #define HEX_DIGIT_01001001					49
1353 #define HEX_DIGIT_01001010					4A
1354 #define HEX_DIGIT_01001011					4B
1355 #define HEX_DIGIT_01001100					4C
1356 #define HEX_DIGIT_01001101					4D
1357 #define HEX_DIGIT_01001110					4E
1358 #define HEX_DIGIT_01001111					4F
1359 #define HEX_DIGIT_01010000					50
1360 #define HEX_DIGIT_01010001					51
1361 #define HEX_DIGIT_01010010					52
1362 #define HEX_DIGIT_01010011					53
1363 #define HEX_DIGIT_01010100					54
1364 #define HEX_DIGIT_01010101					55
1365 #define HEX_DIGIT_01010110					56
1366 #define HEX_DIGIT_01010111					57
1367 #define HEX_DIGIT_01011000					58
1368 #define HEX_DIGIT_01011001					59
1369 #define HEX_DIGIT_01011010					5A
1370 #define HEX_DIGIT_01011011					5B
1371 #define HEX_DIGIT_01011100					5C
1372 #define HEX_DIGIT_01011101					5D
1373 #define HEX_DIGIT_01011110					5E
1374 #define HEX_DIGIT_01011111					5F
1375 #define HEX_DIGIT_01100000					60
1376 #define HEX_DIGIT_01100001					61
1377 #define HEX_DIGIT_01100010					62
1378 #define HEX_DIGIT_01100011					63
1379 #define HEX_DIGIT_01100100					64
1380 #define HEX_DIGIT_01100101					65
1381 #define HEX_DIGIT_01100110					66
1382 #define HEX_DIGIT_01100111					67
1383 #define HEX_DIGIT_01101000					68
1384 #define HEX_DIGIT_01101001					69
1385 #define HEX_DIGIT_01101010					6A
1386 #define HEX_DIGIT_01101011					6B
1387 #define HEX_DIGIT_01101100					6C
1388 #define HEX_DIGIT_01101101					6D
1389 #define HEX_DIGIT_01101110					6E
1390 #define HEX_DIGIT_01101111					6F
1391 #define HEX_DIGIT_01110000					70
1392 #define HEX_DIGIT_01110001					71
1393 #define HEX_DIGIT_01110010					72
1394 #define HEX_DIGIT_01110011					73
1395 #define HEX_DIGIT_01110100					74
1396 #define HEX_DIGIT_01110101					75
1397 #define HEX_DIGIT_01110110					76
1398 #define HEX_DIGIT_01110111					77
1399 #define HEX_DIGIT_01111000					78
1400 #define HEX_DIGIT_01111001					79
1401 #define HEX_DIGIT_01111010					7A
1402 #define HEX_DIGIT_01111011					7B
1403 #define HEX_DIGIT_01111100					7C
1404 #define HEX_DIGIT_01111101					7D
1405 #define HEX_DIGIT_01111110					7E
1406 #define HEX_DIGIT_01111111					7F
1407 #define HEX_DIGIT_10000000					80
1408 #define HEX_DIGIT_10000001					81
1409 #define HEX_DIGIT_10000010					82
1410 #define HEX_DIGIT_10000011					83
1411 #define HEX_DIGIT_10000100					84
1412 #define HEX_DIGIT_10000101					85
1413 #define HEX_DIGIT_10000110					86
1414 #define HEX_DIGIT_10000111					87
1415 #define HEX_DIGIT_10001000					88
1416 #define HEX_DIGIT_10001001					89
1417 #define HEX_DIGIT_10001010					8A
1418 #define HEX_DIGIT_10001011					8B
1419 #define HEX_DIGIT_10001100					8C
1420 #define HEX_DIGIT_10001101					8D
1421 #define HEX_DIGIT_10001110					8E
1422 #define HEX_DIGIT_10001111					8F
1423 #define HEX_DIGIT_10010000					90
1424 #define HEX_DIGIT_10010001					91
1425 #define HEX_DIGIT_10010010					92
1426 #define HEX_DIGIT_10010011					93
1427 #define HEX_DIGIT_10010100					94
1428 #define HEX_DIGIT_10010101					95
1429 #define HEX_DIGIT_10010110					96
1430 #define HEX_DIGIT_10010111					97
1431 #define HEX_DIGIT_10011000					98
1432 #define HEX_DIGIT_10011001					99
1433 #define HEX_DIGIT_10011010					9A
1434 #define HEX_DIGIT_10011011					9B
1435 #define HEX_DIGIT_10011100					9C
1436 #define HEX_DIGIT_10011101					9D
1437 #define HEX_DIGIT_10011110					9E
1438 #define HEX_DIGIT_10011111					9F
1439 #define HEX_DIGIT_10100000					A0
1440 #define HEX_DIGIT_10100001					A1
1441 #define HEX_DIGIT_10100010					A2
1442 #define HEX_DIGIT_10100011					A3
1443 #define HEX_DIGIT_10100100					A4
1444 #define HEX_DIGIT_10100101					A5
1445 #define HEX_DIGIT_10100110					A6
1446 #define HEX_DIGIT_10100111					A7
1447 #define HEX_DIGIT_10101000					A8
1448 #define HEX_DIGIT_10101001					A9
1449 #define HEX_DIGIT_10101010					AA
1450 #define HEX_DIGIT_10101011					AB
1451 #define HEX_DIGIT_10101100					AC
1452 #define HEX_DIGIT_10101101					AD
1453 #define HEX_DIGIT_10101110					AE
1454 #define HEX_DIGIT_10101111					AF
1455 #define HEX_DIGIT_10110000					B0
1456 #define HEX_DIGIT_10110001					B1
1457 #define HEX_DIGIT_10110010					B2
1458 #define HEX_DIGIT_10110011					B3
1459 #define HEX_DIGIT_10110100					B4
1460 #define HEX_DIGIT_10110101					B5
1461 #define HEX_DIGIT_10110110					B6
1462 #define HEX_DIGIT_10110111					B7
1463 #define HEX_DIGIT_10111000					B8
1464 #define HEX_DIGIT_10111001					B9
1465 #define HEX_DIGIT_10111010					BA
1466 #define HEX_DIGIT_10111011					BB
1467 #define HEX_DIGIT_10111100					BC
1468 #define HEX_DIGIT_10111101					BD
1469 #define HEX_DIGIT_10111110					BE
1470 #define HEX_DIGIT_10111111					BF
1471 #define HEX_DIGIT_11000000					C0
1472 #define HEX_DIGIT_11000001					C1
1473 #define HEX_DIGIT_11000010					C2
1474 #define HEX_DIGIT_11000011					C3
1475 #define HEX_DIGIT_11000100					C4
1476 #define HEX_DIGIT_11000101					C5
1477 #define HEX_DIGIT_11000110					C6
1478 #define HEX_DIGIT_11000111					C7
1479 #define HEX_DIGIT_11001000					C8
1480 #define HEX_DIGIT_11001001					C9
1481 #define HEX_DIGIT_11001010					CA
1482 #define HEX_DIGIT_11001011					CB
1483 #define HEX_DIGIT_11001100					CC
1484 #define HEX_DIGIT_11001101					CD
1485 #define HEX_DIGIT_11001110					CE
1486 #define HEX_DIGIT_11001111					CF
1487 #define HEX_DIGIT_11010000					D0
1488 #define HEX_DIGIT_11010001					D1
1489 #define HEX_DIGIT_11010010					D2
1490 #define HEX_DIGIT_11010011					D3
1491 #define HEX_DIGIT_11010100					D4
1492 #define HEX_DIGIT_11010101					D5
1493 #define HEX_DIGIT_11010110					D6
1494 #define HEX_DIGIT_11010111					D7
1495 #define HEX_DIGIT_11011000					D8
1496 #define HEX_DIGIT_11011001					D9
1497 #define HEX_DIGIT_11011010					DA
1498 #define HEX_DIGIT_11011011					DB
1499 #define HEX_DIGIT_11011100					DC
1500 #define HEX_DIGIT_11011101					DD
1501 #define HEX_DIGIT_11011110					DE
1502 #define HEX_DIGIT_11011111					DF
1503 #define HEX_DIGIT_11100000					E0
1504 #define HEX_DIGIT_11100001					E1
1505 #define HEX_DIGIT_11100010					E2
1506 #define HEX_DIGIT_11100011					E3
1507 #define HEX_DIGIT_11100100					E4
1508 #define HEX_DIGIT_11100101					E5
1509 #define HEX_DIGIT_11100110					E6
1510 #define HEX_DIGIT_11100111					E7
1511 #define HEX_DIGIT_11101000					E8
1512 #define HEX_DIGIT_11101001					E9
1513 #define HEX_DIGIT_11101010					EA
1514 #define HEX_DIGIT_11101011					EB
1515 #define HEX_DIGIT_11101100					EC
1516 #define HEX_DIGIT_11101101					ED
1517 #define HEX_DIGIT_11101110					EE
1518 #define HEX_DIGIT_11101111					EF
1519 #define HEX_DIGIT_11110000					F0
1520 #define HEX_DIGIT_11110001					F1
1521 #define HEX_DIGIT_11110010					F2
1522 #define HEX_DIGIT_11110011					F3
1523 #define HEX_DIGIT_11110100					F4
1524 #define HEX_DIGIT_11110101					F5
1525 #define HEX_DIGIT_11110110					F6
1526 #define HEX_DIGIT_11110111					F7
1527 #define HEX_DIGIT_11111000					F8
1528 #define HEX_DIGIT_11111001					F9
1529 #define HEX_DIGIT_11111010					FA
1530 #define HEX_DIGIT_11111011					FB
1531 #define HEX_DIGIT_11111100					FC
1532 #define HEX_DIGIT_11111101					FD
1533 #define HEX_DIGIT_11111110					FE
1534 #define HEX_DIGIT_11111111					FF
1535 
1536 #if 0
1537 #pragma mark == Debugging ==
1538 #endif
1539 
1540 //---------------------------------------------------------------------------------------------------------------------------
1541 /*!	@function	CommonServicesTest
1542 
1543 	@abstract	Unit test.
1544 */
1545 
1546 #if( DEBUG )
1547 	#if( TARGET_LANGUAGE_C_LIKE )
1548 		OSStatus	CommonServicesTest( void );
1549 	#endif
1550 #endif
1551 
1552 #ifdef	__cplusplus
1553 	}
1554 #endif
1555 
1556 #endif	// __COMMON_SERVICES__
1557