APSCommonServices.h 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863
  1. /*
  2. File: APSCommonServices.h
  3. Package: AirPlayAudioPOSIXReceiver
  4. Version: AirPlay_Audio_POSIX_Receiver_211.1.p8
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you, by Apple Inc. ("Apple"), in your
  6. capacity as a current, and in good standing, Licensee in the MFi Licensing Program. Use of this
  7. Apple software is governed by and subject to the terms and conditions of your MFi License,
  8. including, but not limited to, the restrictions specified in the provision entitled ”Public
  9. Software”, and is further subject to your agreement to the following additional terms, and your
  10. agreement that the use, installation, modification or redistribution of this Apple software
  11. constitutes acceptance of these additional terms. If you do not agree with these additional terms,
  12. please do not use, install, modify or redistribute this Apple software.
  13. Subject to all of these terms and in?consideration of your agreement to abide by them, Apple grants
  14. you, for as long as you are a current and in good-standing MFi Licensee, a personal, non-exclusive
  15. license, under Apple's copyrights in this original Apple software (the "Apple Software"), to use,
  16. reproduce, and modify the Apple Software in source form, and to use, reproduce, modify, and
  17. redistribute the Apple Software, with or without modifications, in binary form. While you may not
  18. redistribute the Apple Software in source form, should you redistribute the Apple Software in binary
  19. form, you must retain this notice and the following text and disclaimers in all such redistributions
  20. of the Apple Software. Neither the name, trademarks, service marks, or logos of Apple Inc. may be
  21. used to endorse or promote products derived from the Apple Software without specific prior written
  22. permission from Apple. Except as expressly stated in this notice, no other rights or licenses,
  23. express or implied, are granted by Apple herein, including but not limited to any patent rights that
  24. may be infringed by your derivative works or by other works in which the Apple Software may be
  25. incorporated.
  26. Unless you explicitly state otherwise, if you provide any ideas, suggestions, recommendations, bug
  27. fixes or enhancements to Apple in connection with this software (“Feedback”), you hereby grant to
  28. Apple a non-exclusive, fully paid-up, perpetual, irrevocable, worldwide license to make, use,
  29. reproduce, incorporate, modify, display, perform, sell, make or have made derivative works of,
  30. distribute (directly or indirectly) and sublicense, such Feedback in connection with Apple products
  31. and services. Providing this Feedback is voluntary, but if you do provide Feedback to Apple, you
  32. acknowledge and agree that Apple may exercise the license granted above without the payment of
  33. royalties or further consideration to Participant.
  34. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR
  35. IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY
  36. AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR
  37. IN COMBINATION WITH YOUR PRODUCTS.
  38. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
  39. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  40. PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
  41. AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  43. POSSIBILITY OF SUCH DAMAGE.
  44. Copyright (C) 2002-2014 Apple Inc. All Rights Reserved.
  45. */
  46. #ifndef __APSCommonServices_h__
  47. #define __APSCommonServices_h__
  48. // CommonServices_PLATFORM_HEADER can be defined to include a platform-specific file before any other files are included.
  49. #include "wm_type_def.h"
  50. #include "lwip/arch.h"
  51. #include "wm_sockets.h"
  52. #if( defined( CommonServices_PLATFORM_HEADER ) )
  53. #include CommonServices_PLATFORM_HEADER
  54. #endif
  55. #if 0
  56. #pragma mark == Compiler ==
  57. #endif
  58. #define HAVE_IPV6 TLS_CONFIG_IPV6
  59. #if HAVE_IPV6
  60. #define mDNSIPv6Support 1
  61. #endif
  62. #define LOG printf
  63. //#define LOG(...)
  64. #ifndef ssize_t
  65. typedef int ssize_t;
  66. #endif
  67. #define usleep(a) tls_os_time_delay(a/10000)
  68. //===========================================================================================================================
  69. // Compiler
  70. //===========================================================================================================================
  71. #if( __clang__ )
  72. #define COMPILER_CLANG ( ( __clang_major__ * 10000 ) + ( __clang_minor__ * 100 ) + __clang_patchlevel__ )
  73. #endif
  74. #if( __GNUC__ )
  75. #define COMPILER_GCC ( ( __GNUC__ * 10000 ) + ( __GNUC_MINOR__ * 100 ) + __GNUC_PATCHLEVEL__ )
  76. #endif
  77. // STATIC_INLINE - Portable way to marking an inline function for use in a header file.
  78. #if( !defined( STATIC_INLINE ) )
  79. #if( defined( __GNUC__ ) && ( __GNUC__ >= 4 ) )
  80. #define STATIC_INLINE static __inline__ __attribute__( ( always_inline ) )
  81. #elif( defined( __GNUC__ ) )
  82. #define STATIC_INLINE static __inline__
  83. #elif( defined( __MWERKS__ ) || defined( __cplusplus ) )
  84. #define STATIC_INLINE static inline
  85. #elif( defined( __WIN32__ ) )
  86. #define STATIC_INLINE static __inline__
  87. #else
  88. #define STATIC_INLINE static inline
  89. #endif
  90. #endif
  91. // ATTRIBUTE_NORETURN -- Marks a function as never returning.
  92. #if( !defined( ATTRIBUTE_NORETURN ) )
  93. #if( defined( __GNUC__ ) )
  94. #define ATTRIBUTE_NORETURN __attribute__( ( __noreturn__ ) )
  95. #else
  96. #define ATTRIBUTE_NORETURN
  97. #endif
  98. #endif
  99. // Compatibility for clang's extension macros.
  100. #if( !defined( __has_feature ) )
  101. #define __has_feature( x ) 0
  102. #endif
  103. #if( !defined( __has_include ) )
  104. #define __has_include( x ) 0
  105. #endif
  106. // CF_RETURNS_RETAINED -- Marks a function as returning a CFRetain'd object.
  107. #if( !defined( CF_RETURNS_RETAINED ) )
  108. #if( __has_feature( attribute_cf_returns_retained ) )
  109. #define CF_RETURNS_RETAINED __attribute__( ( cf_returns_retained ) )
  110. #else
  111. #define CF_RETURNS_RETAINED
  112. #endif
  113. #endif
  114. // CF_RETURNS_NOT_RETAINED -- Marks a function as not returning a CFRetain'd object.
  115. #if( !defined( CF_RETURNS_NOT_RETAINED ) )
  116. #if( __has_feature( attribute_cf_returns_not_retained ) )
  117. #define CF_RETURNS_NOT_RETAINED __attribute__( ( cf_returns_not_retained ) )
  118. #else
  119. #define CF_RETURNS_NOT_RETAINED
  120. #endif
  121. #endif
  122. // STATIC_ANALYZER_NORETURN -- Tells the static analyzer assume a function doesn't return (e.g. assertion handlers).
  123. #if( !defined( STATIC_ANALYZER_NORETURN ) )
  124. #if( __clang__ )
  125. #define STATIC_ANALYZER_NORETURN __attribute__( ( analyzer_noreturn ) )
  126. #else
  127. #define STATIC_ANALYZER_NORETURN
  128. #endif
  129. #endif
  130. #if 0
  131. #pragma mark == Target ==
  132. #endif
  133. //===========================================================================================================================
  134. // Target
  135. //===========================================================================================================================
  136. // Windows
  137. #define TARGET_OS_WINDOWS_KERNEL 0
  138. #if 0
  139. #pragma mark == Target High Level ==
  140. #endif
  141. //===========================================================================================================================
  142. // Target High Level -- TARGET_* flags based on the above TARGET_OS_* flags.
  143. //===========================================================================================================================
  144. #define TARGET_OS_POSIX 1
  145. #define TARGET_HAS_STD_C_LIB 1
  146. // TARGET_HAS_C_LIB_IO -- Has C library I/O support (fopen, fprintf, etc.).
  147. #if( !defined( TARGET_HAS_C_LIB_IO ) )
  148. #define TARGET_HAS_C_LIB_IO 1
  149. #endif
  150. // TARGET_HAS_FLOATING_POINT_SUPPORT -- Has either floating point emulation libraries or hardware floating point.
  151. #define TARGET_HAS_FLOATING_POINT_SUPPORT 1
  152. #define TARGET_LANGUAGE_C_LIKE 1
  153. // which doesn't have BSD sockets support. NetX Duo is only available on ThreadX.
  154. // Note: EFI's compiler says _MSC_VER is 1400, but it doesn't support VS 2005 or later features.
  155. #if 0
  156. #pragma mark == Includes ==
  157. #endif
  158. //===========================================================================================================================
  159. // Includes
  160. //===========================================================================================================================
  161. #include <stddef.h>
  162. // Unknown
  163. // Include sys/param.h on systems that have it to pick up things like the "BSD" preprocessor symbol.
  164. #define TARGET_OS_BSD 0
  165. #if 0
  166. #pragma mark == Post-Include Defines ==
  167. #endif
  168. //===========================================================================================================================
  169. // Defines that rely on the base set of includes
  170. //===========================================================================================================================
  171. // TARGET_HAS_AUDIO_SESSION: 1=Platform supports CoreAudio's AudioSession APIs (and AVFoundation's AVAudioSession).
  172. #if( !defined( TARGET_HAS_AUDIO_SESSION ) )
  173. #define TARGET_HAS_AUDIO_SESSION 0
  174. #endif
  175. // TARGET_HAS_COMMON_CRYPTO: 1=Use CommonCrypto. 0=Use OpenSSL or some other crypto library.
  176. #if( !defined( TARGET_HAS_COMMON_CRYPTO ) )
  177. #define TARGET_HAS_COMMON_CRYPTO 0
  178. #endif
  179. #if( !defined( TARGET_HAS_COMMON_CRYPTO_DIGEST ) )
  180. #define TARGET_HAS_COMMON_CRYPTO_DIGEST 1
  181. #endif
  182. // TARGET_HAS_MD5_UTILS: 1=Use MD5Utils.c instead of an OS-specific library.
  183. #if( !defined( TARGET_HAS_MD5_UTILS ) )
  184. #endif
  185. // TARGET_HAS_SHA_UTILS: 1=Use SHAUtils.c instead of an OS-specific library.
  186. #if( !defined( TARGET_HAS_SHA_UTILS ) )
  187. #endif
  188. #if( TARGET_HAS_COMMON_CRYPTO_DIGEST )
  189. #if( !defined( COMMON_DIGEST_FOR_OPENSSL ) )
  190. #define MD5_DIGEST_LENGTH CC_MD5_DIGEST_LENGTH
  191. #define MD5_CTX psDigestContext_t
  192. #define MD5_Init psMd5Init
  193. #define MD5_Update( CTX, PTR, LEN ) psMd5Update( (CTX), (PTR), (LEN) )
  194. #define MD5_Final(PTR, CTX) psMd5Final( (CTX), (PTR))
  195. #define SHA_DIGEST_LENGTH 20
  196. #define SHA_CTX psDigestContext_t
  197. #define SHA1_Init psSha1Init
  198. #define SHA1_Update( CTX, PTR, LEN ) psSha1Update( (CTX), (PTR), (LEN) )
  199. #define SHA1_Final( DIGEST, CTX ) psSha1Final( (CTX), (DIGEST) )
  200. #define SHA1( PTR, LEN, DIGEST ) sha1( (PTR), (LEN), DIGEST )
  201. #define SHA256_DIGEST_LENGTH CC_SHA256_DIGEST_LENGTH
  202. #define SHA256_CTX psDigestContext_t
  203. #define SHA256_Init CC_SHA256_Init
  204. #define SHA256_Update( CTX, PTR, LEN ) CC_SHA256_Update( (CTX), (PTR), (LEN) )
  205. #define SHA256_Final CC_SHA256_Final
  206. #define SHA256( PTR, LEN, DIGEST ) CC_SHA256( (PTR), (LEN), DIGEST )
  207. #define SHA512_DIGEST_LENGTH CC_SHA512_DIGEST_LENGTH
  208. #define SHA512_CTX psDigestContext_t
  209. #define SHA512_Init psSha512Init
  210. #define SHA512_Update( CTX, PTR, LEN ) psSha512Update( (CTX), (PTR), (LEN) )
  211. #define SHA512_Final(PTR, CTX) psSha512Final( (CTX), (PTR))
  212. #define SHA512( PTR, LEN, DIGEST ) CC_SHA512( (PTR), (LEN), DIGEST )
  213. #endif
  214. #define SHA512_CTX psDigestContext_t
  215. #define HEADER_MD5_H 1 // Trick openssl/md5.h into doing nothing.
  216. #define HEADER_SHA_H 1 // Trick openssl/sha.h into doing nothing.
  217. #elif( TARGET_HAS_MOCANA_SSL )
  218. #define MD5_DIGEST_LENGTH MD5_DIGESTSIZE
  219. #define MD5_CTX MD5_CTX
  220. #define MD5_Init MD5init_HandShake
  221. #define MD5_Update( CTX, PTR, LEN ) MD5update_HandShake( (CTX), (const ubyte *)(PTR), (LEN) )
  222. #define MD5_Final( DIGEST, CTX ) MD5final_HandShake( (CTX), (ubyte *)(DIGEST) )
  223. #define SHA_DIGEST_LENGTH 20
  224. #define SHA_CTX SHA1_CTX
  225. #define SHA1_Init SHA1_initDigestHandShake
  226. #define SHA1_Update( CTX, PTR, LEN ) SHA1_updateDigestHandShake( (CTX), (const ubyte *)(PTR), (LEN) )
  227. #define SHA1_Final( DIGEST, CTX ) SHA1_finalDigestHandShake( (CTX), (DIGEST) )
  228. #define SHA1( PTR, LEN, DIGEST ) SHA1_completeDigest( (ubyte *)(PTR), (LEN), (DIGEST) )
  229. #define SHA512_DIGEST_LENGTH 64
  230. #define SHA512_CTX SHA512_CTX_compat
  231. #define SHA512_Init( CTX ) SHA512_Init_compat( (CTX) )
  232. #define SHA512_Update( CTX, PTR, LEN ) SHA512_Update_compat( (CTX), (PTR), (LEN) )
  233. #define SHA512_Final( DIGEST, CTX ) SHA512_Final_compat( (DIGEST), (CTX) )
  234. #define SHA512( PTR, LEN, DIGEST ) SHA512_compat( (PTR), (LEN), DIGEST )
  235. #elif( TARGET_HAS_USSL )
  236. #define MD5_DIGEST_LENGTH 16
  237. #define MD5_CTX md5_context
  238. #define MD5_Init md5_starts
  239. #define MD5_Update( CTX, PTR, LEN ) md5_update( (CTX), (unsigned char *)(PTR), (int)(LEN) )
  240. #define MD5_Final( DIGEST, CTX ) md5_finish( (CTX), (DIGEST) )
  241. #define SHA_DIGEST_LENGTH 20
  242. #define SHA_CTX sha1_context
  243. #define SHA1_Init sha1_starts
  244. #define SHA1_Update( CTX, PTR, LEN ) sha1_update( (CTX), (unsigned char *)(PTR), (int)(LEN) )
  245. #define SHA1_Final( DIGEST, CTX ) sha1_finish( (CTX), (DIGEST) )
  246. #define SHA1( PTR, LEN, DIGEST ) sha1( (unsigned char *)(PTR), (int)(LEN), (DIGEST) )
  247. #define SHA256_DIGEST_LENGTH 32
  248. #define SHA256_CTX sha2_context
  249. #define SHA256_Init( CTX ) sha2_starts( (CTX), 0 )
  250. #define SHA256_Update( CTX, PTR, LEN ) sha2_update( (CTX), (unsigned char *)(PTR), (int)(LEN) )
  251. #define SHA256_Final( DIGEST, CTX ) sha2_finish( (CTX), (DIGEST) )
  252. #define SHA256( PTR, LEN, DIGEST ) sha2( (unsigned char *)(PTR), (int)(LEN), (DIGEST), 0 )
  253. #elif( TARGET_HAS_SHA_UTILS )
  254. #define SHA_DIGEST_LENGTH 20
  255. #define SHA_CTX SHA_CTX_compat
  256. #define SHA1_Init( CTX ) SHA1_Init_compat( (CTX) )
  257. #define SHA1_Update( CTX, PTR, LEN ) SHA1_Update_compat( (CTX), (PTR), (LEN) )
  258. #define SHA1_Final( DIGEST, CTX ) SHA1_Final_compat( (DIGEST), (CTX) )
  259. #define SHA1( PTR, LEN, DIGEST ) SHA1_compat( (PTR), (LEN), DIGEST )
  260. #define SHA512_DIGEST_LENGTH 64
  261. #define SHA512_CTX SHA512_CTX_compat
  262. #define SHA512_Init( CTX ) SHA512_Init_compat( (CTX) )
  263. #define SHA512_Update( CTX, PTR, LEN ) SHA512_Update_compat( (CTX), (PTR), (LEN) )
  264. #define SHA512_Final( DIGEST, CTX ) SHA512_Final_compat( (DIGEST), (CTX) )
  265. #define SHA512( PTR, LEN, DIGEST ) SHA512_compat( (PTR), (LEN), DIGEST )
  266. #define SHA3_DIGEST_LENGTH 64
  267. #define SHA3_CTX SHA3_CTX_compat
  268. #define SHA3_Init( CTX ) SHA3_Init_compat( (CTX) )
  269. #define SHA3_Update( CTX, PTR, LEN ) SHA3_Update_compat( (CTX), (PTR), (LEN) )
  270. #define SHA3_Final( DIGEST, CTX ) SHA3_Final_compat( (DIGEST), (CTX) )
  271. #define SHA3( PTR, LEN, DIGEST ) SHA3_compat( (PTR), (LEN), DIGEST )
  272. #endif
  273. #if 0
  274. #pragma mark == CPU ==
  275. #endif
  276. //===========================================================================================================================
  277. // CPU
  278. //===========================================================================================================================
  279. #if( !defined( TARGET_CPU_ARM ) )
  280. // ARM
  281. #if( defined( __arm__ ) || defined( __arm ) || defined( __ARM ) || defined( __THUMB ) )
  282. #define TARGET_CPU_ARM 1
  283. #else
  284. #define TARGET_CPU_ARM 0
  285. #endif
  286. #endif
  287. #if( !defined( TARGET_CPU_ARM64 ) )
  288. #if( defined( __arm64__ ) || defined( __arm64 ) )
  289. #define TARGET_CPU_ARM64 1
  290. #else
  291. #define TARGET_CPU_ARM64 0
  292. #endif
  293. #endif
  294. #if( !defined( TARGET_CPU_MIPS ) )
  295. // MIPS
  296. #if( defined( __mips__ ) || __MIPS__ || defined( MIPS32 ) || defined( __MIPSEB__ ) || defined( __MIPSEL__ ) )
  297. #define TARGET_CPU_MIPS 1
  298. #elif( defined( R3000 ) || defined( R4000 ) || defined( R4650 ) || defined( _M_MRX000 ) )
  299. #define TARGET_CPU_MIPS 1
  300. #else
  301. #define TARGET_CPU_MIPS 0
  302. #endif
  303. #endif
  304. #if( !defined( TARGET_CPU_PPC ) )
  305. // PowerPC
  306. #if( defined( __ppc__ ) || defined( __POWERPC__ ) || defined( __PPC__ ) || defined( powerpc ) || defined( ppc ) || defined( _M_MPPC ) )
  307. #define TARGET_CPU_PPC 1
  308. #else
  309. #define TARGET_CPU_PPC 0
  310. #endif
  311. #endif
  312. #if( !defined( TARGET_CPU_PPC64 ) )
  313. #if( defined( __ppc64__ ) )
  314. #define TARGET_CPU_PPC64 1
  315. #else
  316. #define TARGET_CPU_PPC64 0
  317. #endif
  318. #endif
  319. #if( !defined( TARGET_CPU_X86 ) )
  320. // x86
  321. #if( defined( __i386__ ) || __INTEL__ || defined( i386 ) || defined( intel ) || defined( _M_IX86 ) )
  322. #define TARGET_CPU_X86 1
  323. #else
  324. #define TARGET_CPU_X86 0
  325. #endif
  326. #endif
  327. #if( !defined( TARGET_CPU_X86_64 ) )
  328. #if( defined( __x86_64__ ) || defined( _M_X64 ) || defined( _M_AMD64 ) || defined( _M_IA64 ) )
  329. #define TARGET_CPU_X86_64 1
  330. #else
  331. #define TARGET_CPU_X86_64 0
  332. #endif
  333. #endif
  334. // TARGET_NEEDS_NATURAL_ALIGNMENT - CPU requires naturally aligned accesses or an exception occurs.
  335. #if( TARGET_CPU_PPC || TARGET_CPU_PPC64 || TARGET_CPU_X86 || TARGET_CPU_X86_64 )
  336. #define TARGET_NEEDS_NATURAL_ALIGNMENT 0
  337. #else
  338. #define TARGET_NEEDS_NATURAL_ALIGNMENT 1
  339. #endif
  340. // 32-bit and 64-bit support to avoid relying on platform-specific conditionals in code outside of this file.
  341. // See also <http://msdn.microsoft.com/msdnmag/issues/06/05/x64/default.aspx> for Windows 64 bit stuff.
  342. #if( !defined( TARGET_RT_64_BIT ) )
  343. #if( __LP64__ || defined( _WIN64 ) || defined( EFI64 ) || defined( EFIX64 ) || TARGET_CPU_X86_64 || TARGET_CPU_PPC64 )
  344. #define TARGET_RT_64_BIT 1
  345. #else
  346. #define TARGET_RT_64_BIT 0
  347. #endif
  348. #endif
  349. #if( !defined( TARGET_RT_32_BIT ) )
  350. #if( TARGET_RT_64_BIT )
  351. #define TARGET_RT_32_BIT 0
  352. #else
  353. #define TARGET_RT_32_BIT 1
  354. #endif
  355. #endif
  356. #if 0
  357. #pragma mark == Byte Order ==
  358. #endif
  359. //===========================================================================================================================
  360. // Byte Order
  361. //===========================================================================================================================
  362. #define TARGET_RT_LITTLE_ENDIAN 1
  363. #if( !defined( TARGET_RT_LITTLE_ENDIAN ) && !defined( TARGET_RT_BIG_ENDIAN ) )
  364. #error byte order not specified. Update platform include makefile appropriately
  365. #endif
  366. #if( !defined( TARGET_RT_BIG_ENDIAN ) && defined( TARGET_RT_LITTLE_ENDIAN ) && TARGET_RT_LITTLE_ENDIAN )
  367. #define TARGET_RT_BIG_ENDIAN 0
  368. #endif
  369. #if( !defined( TARGET_RT_LITTLE_ENDIAN ) && defined( TARGET_RT_BIG_ENDIAN ) && TARGET_RT_BIG_ENDIAN )
  370. #define TARGET_RT_LITTLE_ENDIAN 0
  371. #endif
  372. // TARGET_RT_BYTE_ORDER
  373. #if( !defined( TARGET_RT_BYTE_ORDER_BIG_ENDIAN ) )
  374. #define TARGET_RT_BYTE_ORDER_BIG_ENDIAN 1234
  375. #endif
  376. #if( !defined( TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN ) )
  377. #define TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN 4321
  378. #endif
  379. #if( !defined( TARGET_RT_BYTE_ORDER ) )
  380. #if( TARGET_RT_LITTLE_ENDIAN )
  381. #define TARGET_RT_BYTE_ORDER TARGET_RT_BYTE_ORDER_LITTLE_ENDIAN
  382. #else
  383. #define TARGET_RT_BYTE_ORDER TARGET_RT_BYTE_ORDER_BIG_ENDIAN
  384. #endif
  385. #endif
  386. //===========================================================================================================================
  387. // Alignment safe read/write/swap macros
  388. //===========================================================================================================================
  389. // Big endian reading
  390. #define ReadBig16( PTR ) \
  391. ( (uint16_t)( \
  392. ( ( (uint16_t)( (uint8_t *)(PTR) )[ 0 ] ) << 8 ) | \
  393. ( (uint16_t)( (uint8_t *)(PTR) )[ 1 ] ) ) )
  394. #define ReadBig32( PTR ) \
  395. ( (uint32_t)( \
  396. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 0 ] ) << 24 ) | \
  397. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 1 ] ) << 16 ) | \
  398. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 2 ] ) << 8 ) | \
  399. ( (uint32_t)( (uint8_t *)(PTR) )[ 3 ] ) ) )
  400. #define ReadBig48( PTR ) \
  401. ( (uint64_t)( \
  402. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 0 ] ) << 40 ) | \
  403. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 1 ] ) << 32 ) | \
  404. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 2 ] ) << 24 ) | \
  405. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 3 ] ) << 16 ) | \
  406. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 4 ] ) << 8 ) | \
  407. ( (uint64_t)( (uint8_t *)(PTR) )[ 5 ] ) ) )
  408. #define ReadBig64( PTR ) \
  409. ( (uint64_t)( \
  410. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 0 ] ) << 56 ) | \
  411. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 1 ] ) << 48 ) | \
  412. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 2 ] ) << 40 ) | \
  413. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 3 ] ) << 32 ) | \
  414. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 4 ] ) << 24 ) | \
  415. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 5 ] ) << 16 ) | \
  416. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 6 ] ) << 8 ) | \
  417. ( (uint64_t)( (uint8_t *)(PTR) )[ 7 ] ) ) )
  418. // Big endian wWriting
  419. #define WriteBig16( PTR, X ) \
  420. do \
  421. { \
  422. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  423. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( (X) & 0xFF ); \
  424. \
  425. } while( 0 )
  426. #define WriteBig32( PTR, X ) \
  427. do \
  428. { \
  429. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  430. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  431. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  432. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( (X) & 0xFF ); \
  433. \
  434. } while( 0 )
  435. #define WriteBig48( PTR, X ) \
  436. do \
  437. { \
  438. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( ( (X) >> 40 ) & 0xFF ); \
  439. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 32 ) & 0xFF ); \
  440. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  441. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  442. ( (uint8_t *)(PTR) )[ 4 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  443. ( (uint8_t *)(PTR) )[ 5 ] = (uint8_t)( (X) & 0xFF ); \
  444. \
  445. } while( 0 )
  446. #define WriteBig64( PTR, X ) \
  447. do \
  448. { \
  449. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( ( (X) >> 56 ) & 0xFF ); \
  450. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 48 ) & 0xFF ); \
  451. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 40 ) & 0xFF ); \
  452. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( ( (X) >> 32 ) & 0xFF ); \
  453. ( (uint8_t *)(PTR) )[ 4 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  454. ( (uint8_t *)(PTR) )[ 5 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  455. ( (uint8_t *)(PTR) )[ 6 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  456. ( (uint8_t *)(PTR) )[ 7 ] = (uint8_t)( (X) & 0xFF ); \
  457. \
  458. } while( 0 )
  459. // Little endian reading
  460. #define ReadLittle16( PTR ) \
  461. ( (uint16_t)( \
  462. ( (uint16_t)( (uint8_t *)(PTR) )[ 0 ] ) | \
  463. ( ( (uint16_t)( (uint8_t *)(PTR) )[ 1 ] ) << 8 ) ) )
  464. #define ReadLittle32( PTR ) \
  465. ( (uint32_t)( \
  466. ( (uint32_t)( (uint8_t *)(PTR) )[ 0 ] ) | \
  467. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 1 ] ) << 8 ) | \
  468. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 2 ] ) << 16 ) | \
  469. ( ( (uint32_t)( (uint8_t *)(PTR) )[ 3 ] ) << 24 ) ) )
  470. #define ReadLittle48( PTR ) \
  471. ( (uint64_t)( \
  472. ( (uint64_t)( (uint8_t *)(PTR) )[ 0 ] ) | \
  473. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 1 ] ) << 8 ) | \
  474. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 2 ] ) << 16 ) | \
  475. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 3 ] ) << 24 ) | \
  476. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 4 ] ) << 32 ) | \
  477. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 5 ] ) << 40 ) ) )
  478. #define ReadLittle64( PTR ) \
  479. ( (uint64_t)( \
  480. ( (uint64_t)( (uint8_t *)(PTR) )[ 0 ] ) | \
  481. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 1 ] ) << 8 ) | \
  482. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 2 ] ) << 16 ) | \
  483. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 3 ] ) << 24 ) | \
  484. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 4 ] ) << 32 ) | \
  485. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 5 ] ) << 40 ) | \
  486. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 6 ] ) << 48 ) | \
  487. ( ( (uint64_t)( (uint8_t *)(PTR) )[ 7 ] ) << 56 ) ) )
  488. // Little endian writing
  489. #define WriteLittle16( PTR, X ) \
  490. do \
  491. { \
  492. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( (X) & 0xFF ); \
  493. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  494. \
  495. } while( 0 )
  496. #define WriteLittle32( PTR, X ) \
  497. do \
  498. { \
  499. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( (X) & 0xFF ); \
  500. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  501. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  502. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  503. \
  504. } while( 0 )
  505. #define WriteLittle48( PTR, X ) \
  506. do \
  507. { \
  508. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( (X) & 0xFF ); \
  509. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  510. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  511. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  512. ( (uint8_t *)(PTR) )[ 4 ] = (uint8_t)( ( (X) >> 32 ) & 0xFF ); \
  513. ( (uint8_t *)(PTR) )[ 5 ] = (uint8_t)( ( (X) >> 40 ) & 0xFF ); \
  514. \
  515. } while( 0 )
  516. #define WriteLittle64( PTR, X ) \
  517. do \
  518. { \
  519. ( (uint8_t *)(PTR) )[ 0 ] = (uint8_t)( (X) & 0xFF ); \
  520. ( (uint8_t *)(PTR) )[ 1 ] = (uint8_t)( ( (X) >> 8 ) & 0xFF ); \
  521. ( (uint8_t *)(PTR) )[ 2 ] = (uint8_t)( ( (X) >> 16 ) & 0xFF ); \
  522. ( (uint8_t *)(PTR) )[ 3 ] = (uint8_t)( ( (X) >> 24 ) & 0xFF ); \
  523. ( (uint8_t *)(PTR) )[ 4 ] = (uint8_t)( ( (X) >> 32 ) & 0xFF ); \
  524. ( (uint8_t *)(PTR) )[ 5 ] = (uint8_t)( ( (X) >> 40 ) & 0xFF ); \
  525. ( (uint8_t *)(PTR) )[ 6 ] = (uint8_t)( ( (X) >> 48 ) & 0xFF ); \
  526. ( (uint8_t *)(PTR) )[ 7 ] = (uint8_t)( ( (X) >> 56 ) & 0xFF ); \
  527. \
  528. } while( 0 )
  529. // Host order read/write
  530. #define Read8( PTR ) ( ( (const uint8_t *)(PTR) )[ 0 ] )
  531. #define Write8( PTR, X ) ( ( (uint8_t *)(PTR) )[ 0 ] = (X) )
  532. #if( TARGET_RT_BIG_ENDIAN )
  533. #define ReadHost16( PTR ) ReadBig16( (PTR) )
  534. #define ReadHost32( PTR ) ReadBig32( (PTR) )
  535. #define ReadHost48( PTR ) ReadBig48( (PTR) )
  536. #define ReadHost64( PTR ) ReadBig64( (PTR) )
  537. #define WriteHost16( PTR, X ) WriteBig16( (PTR), (X) )
  538. #define WriteHost32( PTR, X ) WriteBig32( (PTR), (X) )
  539. #define WriteHost48( PTR, X ) WriteBig48( (PTR), (X) )
  540. #define WriteHost64( PTR, X ) WriteBig64( (PTR), (X) )
  541. #else
  542. #define ReadHost16( PTR ) ReadLittle16( (PTR) )
  543. #define ReadHost32( PTR ) ReadLittle32( (PTR) )
  544. #define ReadHost48( PTR ) ReadLittle48( (PTR) )
  545. #define ReadHost64( PTR ) ReadLittle64( (PTR) )
  546. #define WriteHost16( PTR, X ) WriteLittle16( (PTR), (X) )
  547. #define WriteHost32( PTR, X ) WriteLittle32( (PTR), (X) )
  548. #define WriteHost48( PTR, X ) WriteLittle48( (PTR), (X) )
  549. #define WriteHost64( PTR, X ) WriteLittle64( (PTR), (X) )
  550. #endif
  551. // Unconditional swap read/write.
  552. #if( TARGET_RT_BIG_ENDIAN )
  553. #define ReadSwap16( PTR ) ReadLittle16( (PTR) )
  554. #define ReadSwap32( PTR ) ReadLittle32( (PTR) )
  555. #define ReadSwap48( PTR ) ReadLittle48( (PTR) )
  556. #define ReadSwap64( PTR ) ReadLittle64( (PTR) )
  557. #define WriteSwap16( PTR, X ) WriteLittle16( (PTR), (X) )
  558. #define WriteSwap32( PTR, X ) WriteLittle32( (PTR), (X) )
  559. #define WriteSwap48( PTR, X ) WriteLittle48( (PTR), (X) )
  560. #define WriteSwap64( PTR, X ) WriteLittle64( (PTR), (X) )
  561. #else
  562. #define ReadSwap16( PTR ) ReadBig16( (PTR) )
  563. #define ReadSwap32( PTR ) ReadBig32( (PTR) )
  564. #define ReadSwap48( PTR ) ReadBig48( (PTR) )
  565. #define ReadSwap64( PTR ) ReadBig64( (PTR) )
  566. #define WriteSwap16( PTR, X ) WriteBig16( (PTR), (X) )
  567. #define WriteSwap32( PTR, X ) WriteBig32( (PTR), (X) )
  568. #define WriteSwap48( PTR, X ) WriteBig48( (PTR), (X) )
  569. #define WriteSwap64( PTR, X ) WriteBig64( (PTR), (X) )
  570. #endif
  571. // Memory swaps
  572. #if( TARGET_RT_BIG_ENDIAN )
  573. #define HostToBig16Mem( SRC, LEN, DST ) do {} while( 0 )
  574. #define BigToHost16Mem( SRC, LEN, DST ) do {} while( 0 )
  575. #define LittleToHost16Mem( SRC, LEN, DST ) Swap16Mem( (SRC), (LEN), (DST) )
  576. #define LittleToHost16Mem( SRC, LEN, DST ) Swap16Mem( (SRC), (LEN), (DST) )
  577. #else
  578. #define HostToBig16Mem( SRC, LEN, DST ) Swap16Mem( (SRC), (LEN), (DST) )
  579. #define BigToHost16Mem( SRC, LEN, DST ) Swap16Mem( (SRC), (LEN), (DST) )
  580. #define HostToLittle16Mem( SRC, LEN, DST ) do {} while( 0 )
  581. #define LittleToHost16Mem( SRC, LEN, DST ) do {} while( 0 )
  582. #endif
  583. // Unconditional endian swaps
  584. #define Swap16( X ) \
  585. ( (uint16_t)( \
  586. ( ( ( (uint16_t)(X) ) << 8 ) & UINT16_C( 0xFF00 ) ) | \
  587. ( ( ( (uint16_t)(X) ) >> 8 ) & UINT16_C( 0x00FF ) ) ) )
  588. #define Swap32( X ) \
  589. ( (uint32_t)( \
  590. ( ( ( (uint32_t)(X) ) << 24 ) & UINT32_C( 0xFF000000 ) ) | \
  591. ( ( ( (uint32_t)(X) ) << 8 ) & UINT32_C( 0x00FF0000 ) ) | \
  592. ( ( ( (uint32_t)(X) ) >> 8 ) & UINT32_C( 0x0000FF00 ) ) | \
  593. ( ( ( (uint32_t)(X) ) >> 24 ) & UINT32_C( 0x000000FF ) ) ) )
  594. #define Swap64( X ) \
  595. ( (uint64_t)( \
  596. ( ( ( (uint64_t)(X) ) << 56 ) & UINT64_C( 0xFF00000000000000 ) ) | \
  597. ( ( ( (uint64_t)(X) ) << 40 ) & UINT64_C( 0x00FF000000000000 ) ) | \
  598. ( ( ( (uint64_t)(X) ) << 24 ) & UINT64_C( 0x0000FF0000000000 ) ) | \
  599. ( ( ( (uint64_t)(X) ) << 8 ) & UINT64_C( 0x000000FF00000000 ) ) | \
  600. ( ( ( (uint64_t)(X) ) >> 8 ) & UINT64_C( 0x00000000FF000000 ) ) | \
  601. ( ( ( (uint64_t)(X) ) >> 24 ) & UINT64_C( 0x0000000000FF0000 ) ) | \
  602. ( ( ( (uint64_t)(X) ) >> 40 ) & UINT64_C( 0x000000000000FF00 ) ) | \
  603. ( ( ( (uint64_t)(X) ) >> 56 ) & UINT64_C( 0x00000000000000FF ) ) ) )
  604. // Host<->Network/Big endian swaps
  605. #if( TARGET_RT_BIG_ENDIAN )
  606. #define hton16( X ) (X)
  607. #define ntoh16( X ) (X)
  608. #define hton32( X ) (X)
  609. #define ntoh32( X ) (X)
  610. #define hton64( X ) (X)
  611. #define ntoh64( X ) (X)
  612. #else
  613. #define hton16( X ) Swap16( X )
  614. #define ntoh16( X ) Swap16( X )
  615. #define hton32( X ) Swap32( X )
  616. #define ntoh32( X ) Swap32( X )
  617. #define hton64( X ) Swap64( X )
  618. #define ntoh64( X ) Swap64( X )
  619. #endif
  620. #if 0
  621. #pragma mark == Compile Time Asserts ==
  622. #endif
  623. //---------------------------------------------------------------------------------------------------------------------------
  624. /*! @defined check_compile_time
  625. @abstract Performs a compile-time check of something such as the size of an int.
  626. @discussion
  627. This declares an array with a size that is determined by a compile-time expression. If the expression evaluates
  628. to 0, the array has a size of -1, which is illegal and generates a compile-time error.
  629. For example:
  630. check_compile_time( sizeof( int ) == 4 );
  631. Note: This only works with compile-time expressions.
  632. Note: This only works in places where extern declarations are allowed (e.g. global scope).
  633. References:
  634. <http://www.jaggersoft.com/pubs/CVu11_3.html>
  635. <http://www.jaggersoft.com/pubs/CVu11_5.html>
  636. Note: The following macros differ from the macros on the www.jaggersoft.com web site because those versions do not
  637. work with GCC due to GCC allowing a zero-length array. Using a -1 condition turned out to be more portable.
  638. */
  639. #undef check_compile_time
  640. #if( !defined( check_compile_time ) )
  641. #if( defined( __cplusplus ) )
  642. #define check_compile_time( X ) extern "C" int compile_time_assert_failed[ (X) ? 1 : -1 ]
  643. #else
  644. #define check_compile_time( X ) extern int compile_time_assert_failed[ (X) ? 1 : -1 ]
  645. #endif
  646. #endif
  647. //---------------------------------------------------------------------------------------------------------------------------
  648. /*! @defined check_compile_time_code
  649. @abstract Perform a compile-time check, suitable for placement in code, of something such as the size of an int.
  650. @discussion
  651. This creates a switch statement with an existing case for 0 and an additional case using the result of a
  652. compile-time expression. A switch statement cannot have two case labels with the same constant so if the
  653. compile-time expression evaluates to 0, it is illegal and generates a compile-time error. If the compile-time
  654. expression does not evaluate to 0, the resulting value is used as the case label and it compiles without error.
  655. For example:
  656. check_compile_time_code( sizeof( int ) == 4 );
  657. Note: This only works with compile-time expressions.
  658. Note: This does not work in a global scope so it must be inside a function.
  659. References:
  660. <http://www.jaggersoft.com/pubs/CVu11_3.html>
  661. <http://www.jaggersoft.com/pubs/CVu11_5.html>
  662. */
  663. #undef check_compile_time_code
  664. #if( !defined( check_compile_time_code ) )
  665. #define check_compile_time_code( X ) switch( 0 ) { case 0: case X:; }
  666. #endif
  667. #if 0
  668. #pragma mark == Constants ==
  669. #endif
  670. //===========================================================================================================================
  671. // Constants
  672. //===========================================================================================================================
  673. // Note: "CR" cannot be defined because Darwin, EFI, and others define it for another purpose.
  674. #define LF '\n'
  675. #define CRSTR "\r"
  676. #define LFSTR "\n"
  677. #define CRLF "\r\n"
  678. //#define CRCR "\r\r"
  679. #if 0
  680. #pragma mark == Types ==
  681. #endif
  682. //===========================================================================================================================
  683. // Standard Types
  684. //===========================================================================================================================
  685. // Primitive types
  686. #if( !defined( INT8_MIN ) && !defined( _INT8_T_DECLARED ) && !defined( __MWERKS__ ) && !defined( __intptr_t_defined ) )
  687. #include <stdint.h>
  688. //#define INT8_MIN SCHAR_MIN
  689. //#ifdef int8_t
  690. //#undef int8_t
  691. //#endif
  692. //typedef char int8_t;
  693. #ifdef int16_t
  694. #undef int16_t
  695. #endif
  696. typedef short int16_t;
  697. //typedef int8_t int_least8_t;
  698. typedef int16_t int_least16_t;
  699. //typedef int32_t int_least32_t;
  700. typedef int64_t int_least64_t;
  701. typedef uint8_t uint_least8_t;
  702. typedef uint16_t uint_least16_t;
  703. typedef uint32_t uint_least32_t;
  704. typedef uint64_t uint_least64_t;
  705. //typedef int8_t int_fast8_t;
  706. //typedef int16_t int_fast16_t;
  707. //typedef int32_t int_fast32_t;
  708. typedef int64_t int_fast64_t;
  709. //typedef uint8_t uint_fast8_t;
  710. //typedef uint16_t uint_fast16_t;
  711. //typedef uint32_t uint_fast32_t;
  712. typedef uint64_t uint_fast64_t;
  713. #if 0
  714. #if( !defined( intptr_t ) )
  715. typedef long intptr_t;
  716. #endif
  717. #if( !defined( uintptr_t ) )
  718. typedef unsigned long uintptr_t;
  719. #endif
  720. #endif
  721. #endif
  722. // int128 support
  723. typedef struct
  724. {
  725. int64_t hi;
  726. uint64_t lo;
  727. } int128_compat;
  728. typedef struct
  729. {
  730. uint64_t hi;
  731. uint64_t lo;
  732. } uint128_compat;
  733. #if( TARGET_RT_64_BIT && ( COMPILER_CLANG >= 30100 ) )
  734. #define TARGET_HAS_NATIVE_INT128 1
  735. typedef __int128 int128_t;
  736. typedef unsigned __int128 uint128_t;
  737. #elif( TARGET_RT_64_BIT && __GNUC__ )
  738. #if( __SIZEOF_INT128__ )
  739. #define TARGET_HAS_NATIVE_INT128 1
  740. typedef __int128 int128_t;
  741. typedef unsigned __int128 uint128_t;
  742. #elif( COMPILER_GCC >= 40400 )
  743. #define TARGET_HAS_NATIVE_INT128 1
  744. typedef signed int128_t __attribute__( ( mode( TI ) ) );
  745. typedef unsigned uint128_t __attribute__( ( mode( TI ) ) );
  746. #endif
  747. #endif
  748. // Limits
  749. #if( !defined( SCHAR_MIN ) )
  750. #define SCHAR_MIN ( -128 )
  751. #endif
  752. #if( !defined( UCHAR_MAX ) )
  753. #define UCHAR_MAX 255
  754. #endif
  755. #if( !defined( SHRT_MIN ) )
  756. #define SHRT_MIN ( -32768 )
  757. #endif
  758. #if( !defined( USHRT_MAX ) )
  759. #define USHRT_MAX 65535
  760. #endif
  761. #if( !defined( UINT_MAX ) )
  762. #define UINT_MAX 0xFFFFFFFFU
  763. #endif
  764. #if( !defined( INT8_MAX ) )
  765. #define INT8_MAX 127
  766. #endif
  767. #if( !defined( INT16_MAX ) )
  768. #define INT16_MAX 32767
  769. #endif
  770. #if( !defined( INT32_MAX ) )
  771. #define INT32_MAX 2147483647
  772. #endif
  773. #if( !defined( INT64_MAX ) )
  774. #define INT64_MAX INT64_C( 9223372036854775807 )
  775. #endif
  776. #if( !defined( INT8_MIN ) )
  777. #define INT8_MIN ( -128 )
  778. #endif
  779. #if( !defined( INT16_MIN ) )
  780. #define INT16_MIN ( -32768 )
  781. #endif
  782. #if( !defined( INT32_MIN ) )
  783. #define INT32_MIN ( -INT32_MAX - 1 )
  784. #endif
  785. #if( !defined( INT64_MIN ) )
  786. #define INT64_MIN (-INT64_MAX - 1 )
  787. #endif
  788. #if( !defined( UINT8_MAX ) )
  789. #define UINT8_MAX 255
  790. #endif
  791. #if( !defined( UINT16_MAX ) )
  792. #define UINT16_MAX 65535
  793. #endif
  794. #if( !defined( UINT32_MAX ) )
  795. #define UINT32_MAX UINT32_C( 4294967295 )
  796. #endif
  797. #if( !defined( UINT64_MAX ) )
  798. #define UINT64_MAX UINT64_C( 18446744073709551615 )
  799. #endif
  800. #if( !defined( __MACTYPES__ ) && !defined( __MACTYPES_H__ ) && !defined( __COREFOUNDATION_CFBASE__ ) )
  801. typedef float Float32; // 32 bit IEEE float: 1 sign bit, 8 exponent bits, 23 fraction bits.
  802. typedef double Float64; // 64 bit IEEE float: 1 sign bit, 11 exponent bits, 52 fraction bits.
  803. check_compile_time( sizeof( Float32 ) == 4 );
  804. check_compile_time( sizeof( Float64 ) == 8 );
  805. #endif
  806. // Old MacTypes for emulating with Mac-ish APIs (unfortunately, UInt32 isn't exactly uint32_t so we have to provide this).
  807. #if( !defined( __MACTYPES__ ) && !defined( _OS_OSTYPES_H ) )
  808. #if( __LP64__ )
  809. typedef unsigned int UInt32;
  810. typedef signed int SInt32;
  811. #else
  812. typedef unsigned long UInt32;
  813. typedef signed long SInt32;
  814. #endif
  815. #endif
  816. // Macros for minimum-width integer constants
  817. #if( !defined( INT8_C ) )
  818. #define INT8_C( value ) value
  819. #endif
  820. #if( !defined( INT16_C ) )
  821. #define INT16_C( value ) value
  822. #endif
  823. #if( !defined( INT32_C ) )
  824. #define INT32_C( value ) value
  825. #endif
  826. #define INT64_C_safe( value ) INT64_C( value )
  827. #if( !defined( INT64_C ) )
  828. #define INT64_C( value ) value ## LL
  829. #endif
  830. #define UINT8_C_safe( value ) UINT8_C( value )
  831. #if( !defined( UINT8_C ) )
  832. #define UINT8_C( value ) value ## U
  833. #endif
  834. #define UINT16_C_safe( value ) UINT16_C( value )
  835. #if( !defined( UINT16_C ) )
  836. #define UINT16_C( value ) value ## U
  837. #endif
  838. #define UINT32_C_safe( value ) UINT32_C( value )
  839. #if( !defined( UINT32_C ) )
  840. #define UINT32_C( value ) value ## U
  841. #endif
  842. #define UINT64_C_safe( value ) UINT64_C( value )
  843. #if( !defined( UINT64_C ) )
  844. #define UINT64_C( value ) value ## ULL
  845. #endif
  846. #if( !defined( INT_MIN ) )
  847. #define INT_MIN ( -2147483647 - 1 )
  848. #endif
  849. #if( !defined( INT_MAX ) )
  850. #define INT_MAX 2147483647
  851. #endif
  852. #if( !defined( SIZE_MAX ) )
  853. #define SIZE_MAX 2147483647
  854. #endif
  855. // Value16 -- 16-bit union of a bunch of types.
  856. typedef union
  857. {
  858. char c[ 2 ];
  859. uint8_t u8[ 2 ];
  860. int8_t s8[ 2 ];
  861. uint16_t u16;
  862. int16_t s16;
  863. } Value16;
  864. check_compile_time( sizeof( Value16 ) == 2 );
  865. // Value32 -- 32-bit union of a bunch of types.
  866. typedef union
  867. {
  868. char c[ 4 ];
  869. uint8_t u8[ 4 ];
  870. int8_t s8[ 4 ];
  871. uint16_t u16[ 2 ];
  872. int16_t s16[ 2 ];
  873. uint32_t u32;
  874. int32_t s32;
  875. Float32 f32;
  876. } Value32;
  877. check_compile_time( sizeof( Value32 ) == 4 );
  878. // Value64 -- 64-bit union of a bunch of types.
  879. typedef union
  880. {
  881. char c[ 8 ];
  882. uint8_t u8[ 8 ];
  883. int8_t s8[ 8 ];
  884. uint16_t u16[ 4 ];
  885. int16_t s16[ 4 ];
  886. uint32_t u32[ 2 ];
  887. int32_t s32[ 2 ];
  888. uint64_t u64;
  889. int64_t s64;
  890. Float32 f32[ 2 ];
  891. Float64 f64;
  892. } Value64;
  893. check_compile_time( sizeof( Value64 ) == 8 );
  894. // timespec
  895. // timeval
  896. #if 0
  897. #pragma mark == Compatibility ==
  898. #endif
  899. //===========================================================================================================================
  900. // Compatibility
  901. //===========================================================================================================================
  902. #if( !defined( __BEGIN_DECLS ) )
  903. #if( defined( __cplusplus ) )
  904. #define __BEGIN_DECLS extern "C" {
  905. #define __END_DECLS }
  906. #else
  907. #define __BEGIN_DECLS
  908. #define __END_DECLS
  909. #endif
  910. #endif
  911. // Macros to allow the same code to work on Windows and other sockets API-compatible platforms.
  912. typedef int SocketRef;
  913. #define IsValidSocket( X ) ( (X) >= 0 )
  914. #define kInvalidSocketRef -1
  915. #define close_compat( X ) close( X )
  916. #define read_compat( SOCK, BUF, LEN ) read( (SOCK), (BUF), (LEN) )
  917. #define write_compat( SOCK, BUF, LEN ) write( (SOCK), (BUF), (LEN) )
  918. #define errno_compat() errno
  919. #define set_errno_compat( X ) do { errno = (X); } while( 0 )
  920. #if( defined( SHUT_WR ) )
  921. #define SHUT_WR_COMPAT SHUT_WR
  922. #else
  923. #define SHUT_WR_COMPAT 1
  924. #endif
  925. #define errno_safe() ( errno_compat() ? errno_compat() : kUnknownErr )
  926. // iovec
  927. typedef struct iovec iovec_t;
  928. #if( !defined( SETIOV ) )
  929. #define SETIOV( IOV, PTR, LEN ) \
  930. do \
  931. { \
  932. (IOV)->iov_base = (void *)(PTR); \
  933. (IOV)->iov_len = (LEN); \
  934. \
  935. } while( 0 )
  936. #endif
  937. // Path Delimiters
  938. #define kHFSPathDelimiterChar ':'
  939. #define kHFSPathDelimiterString ":"
  940. #define kPOSIXPathDelimiterChar '/'
  941. #define kPOSIXPathDelimiterString "/"
  942. #define kWindowsPathDelimiterChar '\\'
  943. #define kWindowsPathDelimiterString "\\"
  944. #define kNativePathDelimiterChar kPOSIXPathDelimiterChar
  945. #define kNativePathDelimiterString kPOSIXPathDelimiterString
  946. // FDRef for File Handles/Descriptors
  947. #define TARGET_HAVE_FDREF 1
  948. typedef int FDRef;
  949. #define IsValidFD( X ) ( (X) >= 0 )
  950. #define kInvalidFD -1
  951. #define CloseFD( X ) close( X )
  952. #define ReadFD( FD, PTR, LEN ) read( FD, PTR, LEN )
  953. #define WriteFD( FD, PTR, LEN ) write( FD, PTR, LEN )
  954. // socklen_t is not defined on the following platforms so emulate it if not defined:
  955. //
  956. // - Pre-Panther Mac OS X. Panther defines SO_NOADDRERR so trigger off that.
  957. // - Windows SDK prior to 2003. 2003+ SDK's define EAI_AGAIN so trigger off that.
  958. // - VxWorks prior to PNE 2.2.1/IPv6.
  959. // - EFI when not building with GCC.
  960. #if( ( TARGET_OS_DARWIN && !defined( SO_NOADDRERR ) ) || \
  961. ( TARGET_OS_WINDOWS && !defined( EAI_AGAIN ) ) || \
  962. ( TARGET_OS_VXWORKS && ( TORNADO_VERSION < 221 ) ) || \
  963. TARGET_OS_EFI && !defined( __GNUC__ ) )
  964. typedef int socklen_t;
  965. #endif
  966. // EFI doesn't have stdarg.h or string.h, but it does have some equivalents so map them to the standard names.
  967. // Darwin Kernel mappings.
  968. // NetBSD Kernel mappings.
  969. // Windows CE doesn't have strdup and Visual Studio 2005 marks strdup, stricmp, and strnicmp as a deprecated so map
  970. // them to their underscore variants on Windows so code can use the standard names on all platforms.
  971. // Windows doesn't have snprintf/vsnprintf, but it does have _snprintf/_vsnprintf.
  972. // Additionally, Visual Studio 2005 and later have deprecated these functions and replaced them with
  973. // versions with an _s suffix (supposedly more secure). So just map the standard functions to those.
  974. // Generic mappings.
  975. #if( !MALLOC_COMPAT_DEFINED )
  976. #define malloc_compat( SIZE ) tls_mem_alloc( (SIZE ) )
  977. #define free_compat( PTR ) tls_mem_free( (PTR) )
  978. #endif
  979. #if 0
  980. #pragma mark -
  981. #pragma mark == Compatibility - Compiler ==
  982. #endif
  983. //===========================================================================================================================
  984. // Compatibility - Compiler
  985. //===========================================================================================================================
  986. //---------------------------------------------------------------------------------------------------------------------------
  987. /*! @defined __builtin_expect
  988. @abstract Compatibility macro for GCC branch prediction.
  989. @discussion
  990. GCC supports explicit branch prediction so that the CPU back-end can hint the processor and also so that code
  991. blocks can be reordered such that the predicted path sees a more linear flow, thus improving cache behavior, etc.
  992. See <http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html> for info
  993. Here's an example of using it when "x == 0" is unlikely:
  994. if( __builtin_expect( x == 0, 0 ) ) // Programmer predicts that the result of "x == 0" is likely to be 0 (i.e. false).
  995. {
  996. // x == 0, but this should be unlikely.
  997. }
  998. Or to reverse the prediction such that "x == 0" is likely:
  999. if( __builtin_expect( x == 0, 1 ) ) // Programmer predicts that the result of "x == 0" is likely to be 1 (i.e. true).
  1000. {
  1001. // x == 0, the likely situation.
  1002. }
  1003. Note: since GCC only supports integer expressions for the expected result, if you are doing pointer comparisons,
  1004. you have to do the test inside the expression and test it for 1 or 0, like this:
  1005. if( __builtin_expect( ptr != NULL, 1 ) ) // Programmer predicts that ptr is likely to be non-NULL.
  1006. {
  1007. // ptr != NULL, the likely situation.
  1008. }
  1009. */
  1010. // Note: the RealView ARM compiler says it supports __builtin_expect, but it doesn't seem to work so exclude it too.
  1011. #if( !defined( __builtin_expect ) && ( !defined( __GNUC__ ) || ( __GNUC__ < 3 ) || __ARMCC_VERSION ) )
  1012. #define __builtin_expect( EXPRESSSION, EXPECTED_RESULT ) ( EXPRESSSION )
  1013. #endif
  1014. //---------------------------------------------------------------------------------------------------------------------------
  1015. /*! @defined likely/unlikely
  1016. @abstract Macro for Linux-style branch prediction.
  1017. @discussion
  1018. Use these macros like this:
  1019. if( likely( ptr != NULL ) )
  1020. {
  1021. // This code path happens frequently (ptr is usually non-NULL).
  1022. }
  1023. else
  1024. {
  1025. // This code path rarely happens (ptr is rarely NULL).
  1026. }
  1027. if( unlikely( err != kNoErr ) )
  1028. {
  1029. // This code path rarely happens (errors rarely occur).
  1030. }
  1031. else
  1032. {
  1033. // This code path happens frequently (normally kNoErr).
  1034. }
  1035. */
  1036. #if( !defined( likely ) )
  1037. #define likely( EXPRESSSION ) __builtin_expect( !!(EXPRESSSION), 1 )
  1038. #endif
  1039. #if( !defined( unlikely ) )
  1040. #define unlikely( EXPRESSSION ) __builtin_expect( !!(EXPRESSSION), 0 )
  1041. #endif
  1042. // C99's _Pragma operator is not yet supported with Visual Studio, but Visual Studio does support __pragma so use that.
  1043. // PRAGMA_PACKPUSH - Compiler supports: #pragma pack(push, n)/pack(pop)
  1044. // PRAGMA_PACK - Compiler supports: #pragma pack(n)
  1045. //
  1046. // Here's the common way to use these before declaring structures:
  1047. /*
  1048. #if( PRAGMA_PACKPUSH )
  1049. #pragma pack( push, 2 )
  1050. #elif( PRAGMA_PACK )
  1051. #pragma pack( 2 )
  1052. #else
  1053. #warning "FIX ME: packing not supported by this compiler"
  1054. #endif
  1055. Then after the declaring structures:
  1056. #if( PRAGMA_PACKPUSH )
  1057. #pragma pack( pop )
  1058. #elif( PRAGMA_PACK )
  1059. #pragma pack()
  1060. #else
  1061. #warning "FIX ME: packing not supported by this compiler"
  1062. #endif
  1063. */
  1064. #if( _MSC_VER || ( defined( __GNUC__ ) && TARGET_OS_DARWIN ) || defined( __ghs__ ) )
  1065. #define PRAGMA_PACKPUSH 1
  1066. #else
  1067. #define PRAGMA_PACKPUSH 0
  1068. #endif
  1069. #if( defined( __GNUC__ ) || _MSC_VER || defined( __MWERKS__ ) || defined( __ghs__ ) )
  1070. #define PRAGMA_PACK 1
  1071. #else
  1072. #define PRAGMA_PACK 0
  1073. #endif
  1074. #if( !defined( PRAGMA_STRUCT_PACKPUSH ) )
  1075. #define PRAGMA_STRUCT_PACKPUSH PRAGMA_PACKPUSH
  1076. #endif
  1077. #if( !defined( PRAGMA_STRUCT_PACK ) )
  1078. #define PRAGMA_STRUCT_PACK PRAGMA_PACK
  1079. #endif
  1080. // TARGET_HAS_BUILTIN_CLZ - Compiler supports __builtin_clz to count the number of leading zeros in an integer.
  1081. #if ( __clang__ || ( __GNUC__ >= 4 ) )
  1082. #define TARGET_HAS_BUILTIN_CLZ 1
  1083. #else
  1084. #define TARGET_HAS_BUILTIN_CLZ 0
  1085. #endif
  1086. // static_analyzer_cfretained -- Tells the static analyzer that a CF object was retained (e.g. by a called function).
  1087. // static_analyzer_cfreleased -- Tells the static analyzer that a CF object will be released (e.g. passed to a releasing function).
  1088. // Remove this when clang does inter-procedural analysis <rdar://problem/8178274>.
  1089. #ifdef __clang_analyzer__
  1090. #define static_analyzer_cfretained( X ) CFRetain( (X) )
  1091. #define static_analyzer_cfreleased( X ) CFRelease( (X) )
  1092. #else
  1093. #define static_analyzer_cfretained( X )
  1094. #define static_analyzer_cfreleased( X )
  1095. #endif
  1096. // static_analyzer_malloc_freed -- Tells the static analyzer that malloc'd memory will be freed (e.g. by a called function).
  1097. // Remove this when clang does inter-procedural analysis <rdar://problem/10925331>.
  1098. #ifdef __clang_analyzer__
  1099. #define static_analyzer_malloc_freed( X ) free( (X) )
  1100. #else
  1101. #define static_analyzer_malloc_freed( X )
  1102. #endif
  1103. // static_analyzer_nsretained -- Tells the static analyzer that an NS object was retained (e.g. by a called function).
  1104. // static_analyzer_nsreleased -- Tells the static analyzer that an NS object will be released (e.g. passed to a releasing function).
  1105. // Remove this when clang does inter-procedural analysis <rdar://problem/8178274>.
  1106. #ifdef __clang_analyzer__
  1107. #define static_analyzer_nsretained( X ) [(X) retain]
  1108. #define static_analyzer_nsreleased( X ) [(X) release]
  1109. #else
  1110. #define static_analyzer_nsretained( X )
  1111. #define static_analyzer_nsreleased( X )
  1112. #endif
  1113. #if 0
  1114. #pragma mark -
  1115. #pragma mark == Compatibility - Includes ==
  1116. #endif
  1117. //===========================================================================================================================
  1118. // Compatibility - Includes
  1119. //===========================================================================================================================
  1120. // AUDIO_CONVERTER_HEADER -- Header file to include for AudioConverter support.
  1121. #define AUDIO_CONVERTER_HEADER "AudioConverterLite.h"
  1122. #if( !defined( AUDIO_CONVERTER_LITE_ENABLED ) )
  1123. #define AUDIO_CONVERTER_LITE_ENABLED 1
  1124. #endif
  1125. // COREAUDIO_HEADER -- Header file to include for CoreAudio types.
  1126. #define COREAUDIO_HEADER "APSCommonServices.h"
  1127. // CF_HEADER -- Header file to include for CoreFoundation support.
  1128. #define CF_HEADER "CFCompat.h"
  1129. // CF_RUNTIME_HEADER -- Header file to include for CoreFoundation's runtime support.
  1130. #define CF_RUNTIME_HEADER "CFCompat.h"
  1131. // CMSYNC_HEADER -- Header file to include for CoreMedia's clock APIs.
  1132. // LIBDISPATCH_HEADER -- Header file to include for libdispatch/GCD support.
  1133. #define LIBDISPATCH_HEADER "DispatchLite.h"
  1134. // MD5_HEADER -- Header file to include for MD5 support.
  1135. #if( TARGET_HAS_COMMON_CRYPTO_DIGEST )
  1136. #define MD5_HEADER <CommonCrypto/CommonDigest.h>
  1137. #elif( TARGET_HAS_MOCANA_SSL )
  1138. #define MD5_HEADER "MD5Utils.h"
  1139. #elif( TARGET_HAS_USSL )
  1140. #define MD5_HEADER "wiced_security.h"
  1141. #elif( TARGET_HAS_MD5_UTILS )
  1142. #define MD5_HEADER "MD5Utils.h"
  1143. #elif( !TARGET_NO_OPENSSL )
  1144. #define MD5_HEADER <openssl/md5.h>
  1145. #else
  1146. #define MD5_HEADER "APSCommonServices.h" // Can't think of a better way without messy #if's at the call site.
  1147. #endif
  1148. // SHA_HEADER -- Header file to include for SHA support.
  1149. #if( TARGET_HAS_COMMON_CRYPTO_DIGEST )
  1150. #define SHA_HEADER <CommonCrypto/CommonDigest.h>
  1151. #elif( TARGET_HAS_MOCANA_SSL )
  1152. #define SHA_HEADER "SHAUtils.h"
  1153. #elif( TARGET_HAS_USSL )
  1154. #define SHA_HEADER "wiced_security.h"
  1155. #elif( TARGET_HAS_SHA_UTILS )
  1156. #define SHA_HEADER "SHAUtils.h"
  1157. #elif( !TARGET_NO_OPENSSL )
  1158. #define SHA_HEADER <openssl/sha.h>
  1159. #else
  1160. #define SHA_HEADER "APSCommonServices.h" // Can't think of a better way without messy #if's at the call site.
  1161. #endif
  1162. #if 0
  1163. #pragma mark -
  1164. #pragma mark == Compatibility - Networking ==
  1165. #endif
  1166. //===========================================================================================================================
  1167. // Compatibility - Networking
  1168. //===========================================================================================================================
  1169. #if( !defined( AF_UNSPEC ) && !TARGET_NETWORK_NETX_DUO )
  1170. #define AF_UNSPEC 0
  1171. #define AF_INET 4
  1172. //#define AF_INET6 6
  1173. #define AF_LINK 7
  1174. #define LLADDR( X ) ( (X)->sdl_data + (X)->sdl_nlen )
  1175. #endif
  1176. #if( !defined( SO_REUSEPORT ) )
  1177. #define SO_REUSEPORT SO_REUSEADDR
  1178. #endif
  1179. // sockaddr
  1180. // sockaddr_in
  1181. // sockaddr_in6
  1182. // sockaddr_dl
  1183. // sockaddr_ip -- like sockaddr_storage, but only for IPv4 and IPv6, doesn't require casts, and saves space.
  1184. #if( TARGET_LANGUAGE_C_LIKE && !TARGET_NETWORK_NETX_DUO && !defined( __TCP_CONNECTION_H__ ) )
  1185. typedef unsigned char UCHAR;
  1186. typedef unsigned short USHORT;
  1187. typedef USHORT ADDRESS_FAMILY;
  1188. //
  1189. // IPv6 Internet address (RFC 2553)
  1190. // This is an 'on-wire' format structure.
  1191. //
  1192. /*
  1193. typedef struct in6_addr {
  1194. union {
  1195. UCHAR s6_bytes[16];
  1196. USHORT Word[8];
  1197. } u;
  1198. } IN6_ADDR, *PIN6_ADDR;*/
  1199. typedef struct in6_addr IN6_ADDR, *PIN6_ADDR;
  1200. typedef struct {
  1201. union {
  1202. struct {
  1203. ULONG Zone : 28;
  1204. ULONG Level : 4;
  1205. };
  1206. ULONG Value;
  1207. };
  1208. } SCOPE_ID, *PSCOPE_ID;
  1209. /*typedef struct sockaddr_in6 {
  1210. ADDRESS_FAMILY sin6_family; // AF_INET6.
  1211. USHORT sin6_port; // Transport level port number.
  1212. ULONG sin6_flowinfo; // IPv6 flow information.
  1213. IN6_ADDR sin6_addr; // IPv6 address.
  1214. union {
  1215. ULONG sin6_scope_id; // Set of interfaces for a scope.
  1216. SCOPE_ID sin6_scope_struct;
  1217. };
  1218. } SOCKADDR_IN6_LH, *PSOCKADDR_IN6_LH;
  1219. */
  1220. typedef struct sockaddr_in6 SOCKADDR_IN6_LH, *PSOCKADDR_IN6_LH;
  1221. typedef union
  1222. {
  1223. struct sockaddr sa;
  1224. struct sockaddr_in v4;
  1225. #if TLS_CONFIG_IPV6
  1226. #if( defined( AF_INET6 ) )
  1227. struct sockaddr_in6 v6;
  1228. #endif
  1229. #endif
  1230. } sockaddr_ip;
  1231. #endif
  1232. // Macros to workaround sin_len, etc. not being present on some platforms. SIN6_LEN seems like a reasonable indictor.
  1233. #if( !defined( SUN_LEN_SET ) )
  1234. #if( defined( SIN6_LEN ) )
  1235. #define SUN_LEN_SET( X ) (X)->sun_len = (unsigned char) SUN_LEN( (X) )
  1236. #else
  1237. #define SUN_LEN_SET( X ) do {} while( 0 )
  1238. #endif
  1239. #endif
  1240. #if( !defined( SIN_LEN_SET ) )
  1241. #if( defined( SIN6_LEN ) )
  1242. #define SIN_LEN_SET( X ) (X)->sin_len = (unsigned char) sizeof( struct sockaddr_in )
  1243. #else
  1244. #define SIN_LEN_SET( X ) do {} while( 0 )
  1245. #endif
  1246. #endif
  1247. #if( !defined( SIN6_LEN_SET ) )
  1248. #if( defined( SIN6_LEN ) )
  1249. #define SIN6_LEN_SET( X ) (X)->sin6_len = (unsigned char) sizeof( struct sockaddr_in6 )
  1250. #else
  1251. #define SIN6_LEN_SET( X ) do {} while( 0 )
  1252. #endif
  1253. #endif
  1254. // Determines if a sockaddr is a link-local address whether IPv6 is supported or not.
  1255. #if( defined( AF_INET6 ) )
  1256. #define SockAddrIsLinkLocal( X ) ( SockAddrIsIPv4LinkLocal( (X) ) || SockAddrIsIPv6LinkLocal( (X) ) )
  1257. #else
  1258. #define SockAddrIsLinkLocal( X ) SockAddrIsIPv4LinkLocal( (X) )
  1259. #endif
  1260. #define SockAddrIsIPv4LinkLocal( X ) \
  1261. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET ) \
  1262. ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)(X) )->sin_addr ) )[ 0 ] == 169 ) && \
  1263. ( ( (uint8_t *)( &( (const struct sockaddr_in *)(X) )->sin_addr ) )[ 1 ] == 254 ) ) \
  1264. : 0 )
  1265. #define SockAddrIsIPv6LinkLocal( X ) \
  1266. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET6 ) \
  1267. ? IN6_IS_ADDR_LINKLOCAL( &( (const struct sockaddr_in6 *)(X) )->sin6_addr ) : 0 )
  1268. #if( !defined( IN6_IS_ADDR_LINKLOCAL ) )
  1269. #define IN6_IS_ADDR_LINKLOCAL( a ) ( ( (a)->s6_addr[ 0 ] == 0xfe ) && ( ( (a)->s6_addr[ 1 ] & 0xc0 ) == 0x80 ) )
  1270. #endif
  1271. // Determines if a sockaddr is a loopback address whether IPv6 is supported or not.
  1272. #if( defined( AF_INET6 ) )
  1273. #define SockAddrIsLoopBack( X ) \
  1274. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET ) \
  1275. ? ( ( (const struct sockaddr_in *)(X) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) \
  1276. : ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET6 ) \
  1277. ? IN6_IS_ADDR_LOOPBACK( &( (const struct sockaddr_in6 *)(X) )->sin6_addr ) \
  1278. : 0 )
  1279. #else
  1280. #define SockAddrIsLoopBack( X ) \
  1281. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET ) \
  1282. ? ( ( (const struct sockaddr_in *)(X) )->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) \
  1283. : 0 )
  1284. #endif
  1285. // Determines if a sockaddr is a multicast address whether IPv6 is supported or not.
  1286. #if( defined( AF_INET6 ) )
  1287. #define SockAddrIsMulticast( X ) \
  1288. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET ) \
  1289. ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)(X) )->sin_addr ) )[ 0 ] & 0xF0 ) == 0xE0 ) \
  1290. : ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET6 ) \
  1291. ? IN6_IS_ADDR_MULTICAST( &( (const struct sockaddr_in6 *)(X) )->sin6_addr ) \
  1292. : 0 )
  1293. #else
  1294. #define SockAddrIsMulticast( X ) \
  1295. ( ( ( (const struct sockaddr *)(X) )->sa_family == AF_INET ) \
  1296. ? ( ( ( (uint8_t *)( &( (const struct sockaddr_in *)(X) )->sin_addr ) )[ 0 ] & 0xF0 ) == 0xE0 ) \
  1297. : 0 )
  1298. #endif
  1299. // Maps a sockaddr family to a string.
  1300. #if( defined( AF_INET6 ) && defined( AF_LINK ) )
  1301. #define SockAddrFamilyToString( X ) \
  1302. ( ( (X) == AF_INET ) ? "AF_INET" : \
  1303. ( ( (X) == AF_INET6 ) ? "AF_INET6" : \
  1304. ( ( (X) == AF_LINK ) ? "AF_LINK" : \
  1305. "UNKNOWN" ) ) )
  1306. #elif( defined( AF_INET6 ) )
  1307. #define SockAddrFamilyToString( X ) \
  1308. ( ( (X) == AF_INET ) ? "AF_INET" : \
  1309. ( ( (X) == AF_INET6 ) ? "AF_INET6" : \
  1310. "UNKNOWN" ) )
  1311. #elif( defined( AF_LINK ) )
  1312. #define SockAddrFamilyToString( X ) \
  1313. ( ( (X) == AF_INET ) ? "AF_INET" : \
  1314. ( ( (X) == AF_LINK ) ? "AF_LINK" : \
  1315. "UNKNOWN" ) )
  1316. #else
  1317. #define SockAddrFamilyToString( X ) \
  1318. ( ( (X) == AF_INET ) ? "AF_INET" : \
  1319. "UNKNOWN" )
  1320. #endif
  1321. // Determines if a 16-byte IPv6 address is an IPv4-mapped IPv6 address.
  1322. #define IsIPv4MappedIPv6Address( A ) \
  1323. ( ( ( (const uint8_t *)(A) )[ 0 ] == 0 ) && \
  1324. ( ( (const uint8_t *)(A) )[ 1 ] == 0 ) && \
  1325. ( ( (const uint8_t *)(A) )[ 2 ] == 0 ) && \
  1326. ( ( (const uint8_t *)(A) )[ 3 ] == 0 ) && \
  1327. ( ( (const uint8_t *)(A) )[ 4 ] == 0 ) && \
  1328. ( ( (const uint8_t *)(A) )[ 5 ] == 0 ) && \
  1329. ( ( (const uint8_t *)(A) )[ 6 ] == 0 ) && \
  1330. ( ( (const uint8_t *)(A) )[ 7 ] == 0 ) && \
  1331. ( ( (const uint8_t *)(A) )[ 8 ] == 0 ) && \
  1332. ( ( (const uint8_t *)(A) )[ 9 ] == 0 ) && \
  1333. ( ( (const uint8_t *)(A) )[ 10 ] == 0xFF ) && \
  1334. ( ( (const uint8_t *)(A) )[ 11 ] == 0xFF ) )
  1335. // Determines if a 16-byte IPv6 address is an IPv4-compatible IPv6 address.
  1336. #define IsIPv4CompatibleIPv6Address( A ) \
  1337. ( ( ( ( (const uint8_t *)(A) )[ 0 ] == 0 ) && \
  1338. ( ( (const uint8_t *)(A) )[ 1 ] == 0 ) && \
  1339. ( ( (const uint8_t *)(A) )[ 2 ] == 0 ) && \
  1340. ( ( (const uint8_t *)(A) )[ 3 ] == 0 ) && \
  1341. ( ( (const uint8_t *)(A) )[ 4 ] == 0 ) && \
  1342. ( ( (const uint8_t *)(A) )[ 5 ] == 0 ) && \
  1343. ( ( (const uint8_t *)(A) )[ 6 ] == 0 ) && \
  1344. ( ( (const uint8_t *)(A) )[ 7 ] == 0 ) && \
  1345. ( ( (const uint8_t *)(A) )[ 8 ] == 0 ) && \
  1346. ( ( (const uint8_t *)(A) )[ 9 ] == 0 ) && \
  1347. ( ( (const uint8_t *)(A) )[ 10 ] == 0 ) && \
  1348. ( ( (const uint8_t *)(A) )[ 11 ] == 0 ) ) && \
  1349. !( ( ( (const uint8_t *)(A) )[ 12 ] == 0 ) && \
  1350. ( ( (const uint8_t *)(A) )[ 13 ] == 0 ) && \
  1351. ( ( (const uint8_t *)(A) )[ 14 ] == 0 ) && \
  1352. ( ( (const uint8_t *)(A) )[ 15 ] == 0 ) ) && \
  1353. !( ( ( (const uint8_t *)(A) )[ 12 ] == 0 ) && \
  1354. ( ( (const uint8_t *)(A) )[ 13 ] == 0 ) && \
  1355. ( ( (const uint8_t *)(A) )[ 14 ] == 0 ) && \
  1356. ( ( (const uint8_t *)(A) )[ 15 ] == 1 ) ) )
  1357. #define kDNSServiceFlagsUnicastResponse_compat 0x400000 // Added in version 393 of dns_sd.h.
  1358. #define kDNSServiceFlagsThresholdOne_compat 0x2000000 // Added in version 504 of dns_sd.h
  1359. #define kDNSServiceFlagsThresholdReached_compat 0x2000000 // Added in version 504 of dns_sd.h
  1360. #if 0
  1361. #pragma mark -
  1362. #pragma mark == Compatibility - CoreAudio ==
  1363. #endif
  1364. //===========================================================================================================================
  1365. // Compatibility - CoreAudio
  1366. //===========================================================================================================================
  1367. #if( TARGET_RT_BIG_ENDIAN )
  1368. #define kAudioFormatFlagsNativeEndian kAudioFormatFlagIsBigEndian
  1369. #else
  1370. #define kAudioFormatFlagsNativeEndian 0
  1371. #endif
  1372. #define kAudioFormatFlagIsFloat ( 1 << 0 )
  1373. #define kAudioFormatFlagIsBigEndian ( 1 << 1 )
  1374. #define kAudioFormatFlagIsPacked ( 1 << 3 )
  1375. #define kAudioFormatFlagIsSignedInteger ( 1 << 2 )
  1376. #define kAudioFormatFlagIsAlignedHigh ( 1 << 4 )
  1377. #define kAudioFormatFlagIsNonInterleaved ( 1 << 5 )
  1378. #define kAudioFormatFlagIsNonMixable ( 1 << 6 )
  1379. #define kAudioConverterDecompressionMagicCookie 0x646D6763 // 'dmgc'
  1380. #define kAudioFormatAppleLossless 0x616C6163 // 'alac'
  1381. #define kAppleLosslessFormatFlag_16BitSourceData 1
  1382. #define kAppleLosslessFormatFlag_20BitSourceData 2
  1383. #define kAppleLosslessFormatFlag_24BitSourceData 3
  1384. #define kAppleLosslessFormatFlag_32BitSourceData 4
  1385. #define kAudioFormatLinearPCM 0x6C70636D // 'lpcm'
  1386. #define kLinearPCMFormatFlagIsFloat kAudioFormatFlagIsFloat
  1387. #define kLinearPCMFormatFlagIsBigEndian kAudioFormatFlagIsBigEndian
  1388. #define kLinearPCMFormatFlagIsSignedInteger kAudioFormatFlagIsSignedInteger
  1389. #define kLinearPCMFormatFlagIsPacked kAudioFormatFlagIsPacked
  1390. #define kLinearPCMFormatFlagIsAlignedHigh kAudioFormatFlagIsAlignedHigh
  1391. #define kLinearPCMFormatFlagIsNonInterleaved kAudioFormatFlagIsNonInterleaved
  1392. #define kLinearPCMFormatFlagIsNonMixable kAudioFormatFlagIsNonMixable
  1393. #define kLinearPCMFormatFlagsSampleFractionShift 7
  1394. #define kLinearPCMFormatFlagsSampleFractionMask ( 0x3F << kLinearPCMFormatFlagsSampleFractionShift )
  1395. #define kAudioFormatMPEG4AAC 0x61616320 // 'aac '
  1396. #define kAudioFormatMPEG4AAC_ELD 0x61616365 // 'aace'
  1397. typedef struct
  1398. {
  1399. uint32_t mSampleRate;
  1400. uint32_t mFormatID;
  1401. uint32_t mFormatFlags;
  1402. uint32_t mBytesPerPacket;
  1403. uint32_t mFramesPerPacket;
  1404. uint32_t mBytesPerFrame;
  1405. uint32_t mChannelsPerFrame;
  1406. uint32_t mBitsPerChannel;
  1407. uint32_t mReserved;
  1408. } AudioStreamBasicDescription;
  1409. #define kAudioSamplesPerPacket_AAC_ELD 480
  1410. #define kAudioSamplesPerPacket_AAC_LC 1024
  1411. #define kAudioSamplesPerPacket_ALAC_Small 352 // Sized for sending one frame per UDP packet.
  1412. #define kAudioSamplesPerPacket_ALAC_Default 4096
  1413. #define ASBD_FillAAC_ELD( FMT, RATE, CHANNELS ) \
  1414. do \
  1415. { \
  1416. (FMT)->mSampleRate = (RATE); \
  1417. (FMT)->mFormatID = kAudioFormatMPEG4AAC_ELD; \
  1418. (FMT)->mFormatFlags = 0; \
  1419. (FMT)->mBytesPerPacket = 0; \
  1420. (FMT)->mFramesPerPacket = kAudioSamplesPerPacket_AAC_ELD; \
  1421. (FMT)->mBytesPerFrame = 0; \
  1422. (FMT)->mChannelsPerFrame = (CHANNELS); \
  1423. (FMT)->mBitsPerChannel = 0; \
  1424. (FMT)->mReserved = 0; \
  1425. \
  1426. } while( 0 )
  1427. #define ASBD_FillAAC_LC( FMT, RATE, CHANNELS ) \
  1428. do \
  1429. { \
  1430. (FMT)->mSampleRate = (RATE); \
  1431. (FMT)->mFormatID = kAudioFormatMPEG4AAC; \
  1432. (FMT)->mFormatFlags = 0; \
  1433. (FMT)->mBytesPerPacket = 0; \
  1434. (FMT)->mFramesPerPacket = kAudioSamplesPerPacket_AAC_LC; \
  1435. (FMT)->mBytesPerFrame = 0; \
  1436. (FMT)->mChannelsPerFrame = (CHANNELS); \
  1437. (FMT)->mBitsPerChannel = 0; \
  1438. (FMT)->mReserved = 0; \
  1439. \
  1440. } while( 0 )
  1441. #define ASBD_FillALAC( FMT, RATE, BITS, CHANNELS ) \
  1442. do \
  1443. { \
  1444. (FMT)->mSampleRate = (RATE); \
  1445. (FMT)->mFormatID = kAudioFormatAppleLossless; \
  1446. (FMT)->mFormatFlags = ( (BITS) == 16 ) ? kAppleLosslessFormatFlag_16BitSourceData : \
  1447. ( (BITS) == 20 ) ? kAppleLosslessFormatFlag_20BitSourceData : \
  1448. ( (BITS) == 24 ) ? kAppleLosslessFormatFlag_24BitSourceData : 0; \
  1449. (FMT)->mBytesPerPacket = 0; \
  1450. (FMT)->mFramesPerPacket = kAudioSamplesPerPacket_ALAC_Small; \
  1451. (FMT)->mBytesPerFrame = 0; \
  1452. (FMT)->mChannelsPerFrame = (CHANNELS); \
  1453. (FMT)->mBitsPerChannel = 0; \
  1454. (FMT)->mReserved = 0; \
  1455. \
  1456. } while( 0 )
  1457. #define ASBD_FillAUCanonical( FMT, CHANNELS ) \
  1458. do \
  1459. { \
  1460. (FMT)->mSampleRate = 48000; \
  1461. (FMT)->mFormatID = kAudioFormatLinearPCM; \
  1462. (FMT)->mFormatFlags = kAudioFormatFlagIsFloat | \
  1463. kAudioFormatFlagsNativeEndian | \
  1464. kAudioFormatFlagIsPacked | \
  1465. kAudioFormatFlagIsNonInterleaved; \
  1466. (FMT)->mBytesPerPacket = 4; \
  1467. (FMT)->mFramesPerPacket = 1; \
  1468. (FMT)->mBytesPerFrame = 4; \
  1469. (FMT)->mChannelsPerFrame = (CHANNELS); \
  1470. (FMT)->mBitsPerChannel = 32; \
  1471. (FMT)->mReserved = 0; \
  1472. \
  1473. } while( 0 )
  1474. #define ASBD_FillPCM( FMT, RATE, VALID_BITS, TOTAL_BITS, CHANNELS ) \
  1475. do \
  1476. { \
  1477. (FMT)->mSampleRate = (RATE); \
  1478. (FMT)->mFormatID = kAudioFormatLinearPCM; \
  1479. (FMT)->mFormatFlags = kAudioFormatFlagsNativeEndian | \
  1480. kAudioFormatFlagIsSignedInteger | \
  1481. ( ( (VALID_BITS) == (TOTAL_BITS) ) ? \
  1482. kAudioFormatFlagIsPacked : \
  1483. kAudioFormatFlagIsAlignedHigh ); \
  1484. (FMT)->mBytesPerPacket = (CHANNELS) * ( (TOTAL_BITS) / 8 ); \
  1485. (FMT)->mFramesPerPacket = 1; \
  1486. (FMT)->mBytesPerFrame = (CHANNELS) * ( (TOTAL_BITS) / 8 ); \
  1487. (FMT)->mChannelsPerFrame = (CHANNELS); \
  1488. (FMT)->mBitsPerChannel = (VALID_BITS); \
  1489. (FMT)->mReserved = 0; \
  1490. \
  1491. } while( 0 )
  1492. #define ASBD_MakePCM( RATE, VALID_BITS, TOTAL_BITS, CHANNELS ) \
  1493. { \
  1494. /* mSampleRate */ (RATE), \
  1495. /* mFormatID */ kAudioFormatLinearPCM, \
  1496. /* mFormatFlags */ kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsSignedInteger | \
  1497. ( ( (VALID_BITS) == (TOTAL_BITS) ) ? \
  1498. kAudioFormatFlagIsPacked : \
  1499. kAudioFormatFlagIsAlignedHigh ), \
  1500. /* mBytesPerPacket */ (CHANNELS) * ( (TOTAL_BITS) / 8 ), \
  1501. /* mFramesPerPacket */ 1, \
  1502. /* mBytesPerFrame */ (CHANNELS) * ( (TOTAL_BITS) / 8 ), \
  1503. /* mChannelsPerFrame */ (CHANNELS), \
  1504. /* mBitsPerChannel */ (VALID_BITS), \
  1505. /* mReserved */ 0 \
  1506. }
  1507. typedef struct // From ACAppleLosslessCodec.h.
  1508. {
  1509. uint32_t frameLength; // Note: AudioConverter expects this in big endian byte order.
  1510. uint8_t compatibleVersion;
  1511. uint8_t bitDepth; // max 32
  1512. uint8_t pb; // 0 <= pb <= 255
  1513. uint8_t mb;
  1514. uint8_t kb;
  1515. uint8_t numChannels;
  1516. uint16_t maxRun; // Note: AudioConverter expects this in big endian byte order.
  1517. uint32_t maxFrameBytes; // Note: AudioConverter expects this in big endian byte order.
  1518. uint32_t avgBitRate; // Note: AudioConverter expects this in big endian byte order.
  1519. uint32_t sampleRate; // Note: AudioConverter expects this in big endian byte order.
  1520. } ALACParams;
  1521. #if 0
  1522. #pragma mark -
  1523. #pragma mark == Compatibility - Other ==
  1524. #endif
  1525. //===========================================================================================================================
  1526. // Compatibility - Other
  1527. //===========================================================================================================================
  1528. // _beginthreadex and _endthreadex are not supported on Windows CE 2.1 or later (the C runtime issues with leaking
  1529. // resources have apparently been resolved and they seem to have just ripped out support for the API) so map it to
  1530. // CreateThread on Windows CE.
  1531. // GetProcAddress takes a char string on normal Windows, but a TCHAR string on Windows CE so hide this in a macro.
  1532. // Calling conventions
  1533. #if( !defined( CALLBACK_COMPAT ) )
  1534. #define CALLBACK_COMPAT
  1535. #endif
  1536. // CFEqual that's safe to call with NULL parameters.
  1537. #define CFEqualNullSafe( A, B ) ( ( (A) == (B) ) || ( (A) && (B) && CFEqual( (A), (B) ) ) )
  1538. // CFEqual that's safe to call with NULL parameters and treats NULL and kCFNull as equal.
  1539. #define CFEqualNullSafeEx( A, B ) ( ( (A) == (B) ) || CFEqual( (A) ? (A) : kCFNull, (B) ? (B) : kCFNull ) )
  1540. // Null-safe macro to make CF type checking easier.
  1541. #define CFIsType( OBJ, TYPE ) ( (OBJ) && ( CFGetTypeID( (OBJ) ) == TYPE##GetTypeID() ) )
  1542. // CFRelease that's safe to call with NULL.
  1543. #define CFReleaseNullSafe( X ) do { if( (X) ) CFRelease( (X) ); } while( 0 )
  1544. // CFRetain that's safe to call with NULL.
  1545. #define CFRetainNullSafe( X ) do { if( (X) ) CFRetain( (X) ); } while( 0 )
  1546. // CFString comparison with all the right options for sorting the way humans expect it.
  1547. #define CFStringLocalizedStandardCompare( A, B ) \
  1548. CFStringCompare( (A), (B), kCFCompareCaseInsensitive | kCFCompareNumerically )
  1549. // HAS_CF_DISTRIBUTED_NOTIFICATIONS
  1550. #if( !defined( HAS_CF_DISTRIBUTED_NOTIFICATIONS ) )
  1551. #define HAS_CF_DISTRIBUTED_NOTIFICATIONS 0
  1552. #endif
  1553. // NetBSD uses a uintptr_t for the udata field of the kevent structure, but other platforms use a void * so map it on NetBSD.
  1554. #define EV_SET_compat( kevp, a, b, c, d, e, f ) EV_SET( kevp, a, b, c, d, e, f )
  1555. // MAP_ANONYMOUS is preferred on Linux and some other platforms so map MAP_ANON to that.
  1556. //---------------------------------------------------------------------------------------------------------------------------
  1557. /*! @function RandomRange
  1558. @abstract Returns a random number that's >= MIN and <= MAX (i.e. it's inclusive). MIN must be <= MAX.
  1559. */
  1560. #define RandomRange( MIN, MAX ) ( (MIN) + ( Random32() % ( ( (MAX) - (MIN) ) + 1 ) ) )
  1561. #define RandomRangeF( MIN, MAX ) ( ( ( Random32() / 4294967295.0 ) * ( (MAX) - (MIN) ) ) + (MIN) )
  1562. //---------------------------------------------------------------------------------------------------------------------------
  1563. /*! @function Random32
  1564. @abstract Returns a random number that usually has 30-32 bits of randomness and is reasonably fast.
  1565. */
  1566. #define Random32() ( (uint32_t) rand() )
  1567. #if 0
  1568. #pragma mark -
  1569. #pragma mark == Misc ==
  1570. #endif
  1571. //---------------------------------------------------------------------------------------------------------------------------
  1572. /*! @defined PRINTF_STYLE_FUNCTION
  1573. @abstract Some compilers allow you to mark printf-style functions so their format string and argument list
  1574. is checked by the compiler. Adding PRINTF_STYLE_FUNCTION to a function enables this functionality.
  1575. @param FORMAT_INDEX 1's-based index of the format string (e.g. if the 2nd param is the format string, use 2).
  1576. @param ARGS_INDEX 1's-based index of the first arg. The function takes a va_list, use 0.
  1577. @discussion
  1578. Here's an example of using it:
  1579. void MyPrintF( const char *inFormat, ... ) PRINTF_STYLE_FUNCTION( 1, 2 );
  1580. Many of the printf-style function provide more format string features than supported by GCC's printf checking
  1581. (e.g. dlog supports %.4a for printing IPv4 addresses). GCC will flag these as errors so by default, printf-style
  1582. function checking is disabled. To enable it, #define CHECK_PRINTF_STYLE_FUNCTIONS to 1.
  1583. */
  1584. #if( CHECK_PRINTF_STYLE_FUNCTIONS && defined( __GNUC__ ) )
  1585. #define PRINTF_STYLE_FUNCTION( FORMAT_INDEX, ARGS_INDEX ) __attribute__( ( format( printf, FORMAT_INDEX, ARGS_INDEX ) ) )
  1586. #else
  1587. #define PRINTF_STYLE_FUNCTION( FORMAT_INDEX, ARGS_INDEX )
  1588. #endif
  1589. //---------------------------------------------------------------------------------------------------------------------------
  1590. /*! @defined EXPORT_PACKAGE
  1591. @abstract Macro to mark a function as exported to other code within the same package.
  1592. */
  1593. #define EXPORT_PACKAGE
  1594. //---------------------------------------------------------------------------------------------------------------------------
  1595. /*! @defined EXPORT_GLOBAL
  1596. @abstract Macro to mark a function as exported outside of the package.
  1597. @discussion
  1598. GCC 4.0 and later have improved support for marking all symbols __private_extern__ automatically so you can
  1599. explicitly mark only the functions you actually want exported. To make this work portably, EXPORT_GLOBAL lets
  1600. you mark a symbol globally when it is supported by the compiler. Only really needed for IOKit drivers and DLL.
  1601. To export a class as global, declare it like this:
  1602. class EXPORT_GLOBAL MyClass
  1603. {
  1604. ... normal class declaration stuff
  1605. };
  1606. To export a function as global:
  1607. EXPORT_GLOBAL void MyFunction( void );
  1608. */
  1609. #if( ( __GNUC__ > 4 ) || ( ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ >= 0 ) ) )
  1610. #define EXPORT_GLOBAL __attribute__( ( visibility( "default" ) ) )
  1611. #else
  1612. #define EXPORT_GLOBAL
  1613. #endif
  1614. //---------------------------------------------------------------------------------------------------------------------------
  1615. /*! @defined EXPORT_GLOBAL_DATA
  1616. @abstract Macro to mark a variable as exported outside of the package.
  1617. @discussion
  1618. Microsoft's linker requires that data imported from dll's be marked with "_declspec( dllimport )"
  1619. To export a variable as global, declare it like this:
  1620. EXPORT_GLOBAL_DATA int gMyVariable;
  1621. */
  1622. #define EXPORT_GLOBAL_DATA extern
  1623. #if 0
  1624. #pragma mark == ctype safe macros ==
  1625. #endif
  1626. //---------------------------------------------------------------------------------------------------------------------------
  1627. /*! @group ctype safe macros
  1628. @abstract Wrappers for the ctype.h macros make them safe when used with signed characters.
  1629. @discussion
  1630. Some implementations of the ctype.h macros use the character value to directly index into a table.
  1631. This can lead to crashes and other problems when used with signed characters if the character value
  1632. is greater than 127 because the values 128-255 will appear to be negative if viewed as a signed char.
  1633. A negative subscript to an array causes it to index before the beginning and access invalid memory.
  1634. To work around this, these *_safe wrappers mask the value and cast it to an unsigned char.
  1635. */
  1636. #define isalnum_safe( X ) isalnum( ( (unsigned char)( (X) & 0xFF ) ) )
  1637. #define isalpha_safe( X ) isalpha( ( (unsigned char)( (X) & 0xFF ) ) )
  1638. #define iscntrl_safe( X ) iscntrl( ( (unsigned char)( (X) & 0xFF ) ) )
  1639. #define isdigit_safe( X ) isdigit( ( (unsigned char)( (X) & 0xFF ) ) )
  1640. #define isgraph_safe( X ) isgraph( ( (unsigned char)( (X) & 0xFF ) ) )
  1641. #define islower_safe( X ) islower( ( (unsigned char)( (X) & 0xFF ) ) )
  1642. #define isoctal_safe( X ) isoctal( ( (unsigned char)( (X) & 0xFF ) ) )
  1643. #define isprint_safe( X ) isprint( ( (unsigned char)( (X) & 0xFF ) ) )
  1644. #define ispunct_safe( X ) ispunct( ( (unsigned char)( (X) & 0xFF ) ) )
  1645. #define isspace_safe( X ) isspace( ( (unsigned char)( (X) & 0xFF ) ) )
  1646. #define isupper_safe( X ) isupper( ( (unsigned char)( (X) & 0xFF ) ) )
  1647. #define isxdigit_safe( X ) isxdigit( ( (unsigned char)( (X) & 0xFF ) ) )
  1648. #define tolower_safe( X ) tolower( ( (unsigned char)( (X) & 0xFF ) ) )
  1649. #define toupper_safe( X ) toupper( ( (unsigned char)( (X) & 0xFF ) ) )
  1650. //---------------------------------------------------------------------------------------------------------------------------
  1651. /*! @group CoreFoundation object creation/subclassing.
  1652. @abstract Macros to make it easier to create CFType subclasses.
  1653. @example
  1654. struct MyClass
  1655. {
  1656. CFRuntimeBase base; // CF type info. Must be first.
  1657. ... put any other fields you need here.
  1658. };
  1659. CF_CLASS_DEFINE( MyClass )
  1660. OSStatus MyClassCreate( MyClassRef *outObj )
  1661. {
  1662. OSStatus err;
  1663. MyClassRef me;
  1664. CF_OBJECT_CREATE( MyClass, me, err, exit );
  1665. ... object will be zero'd, but do any non-zero init you need here.
  1666. *outObj = me;
  1667. err = kNoErr;
  1668. exit:
  1669. return( err );
  1670. }
  1671. static void _MyClassFinalize( CFTypeRef inCF )
  1672. {
  1673. MyClassRef const me = (MyClassRef) inCF;
  1674. ... do any finalization you need here. Don't free the object itself (that's handled by CF after this returns).
  1675. }
  1676. */
  1677. #define CF_CLASS_DEFINE( NAME ) \
  1678. static void _ ## NAME ## Finalize( CFTypeRef inCF ); \
  1679. \
  1680. static dispatch_once_t g ## NAME ## InitOnce = 0; \
  1681. static CFTypeID g ## NAME ## TypeID = _kCFRuntimeNotATypeID; \
  1682. static const CFRuntimeClass k ## NAME ## Class = \
  1683. { \
  1684. 0, /* version */ \
  1685. # NAME, /* className */ \
  1686. NULL, /* init */ \
  1687. NULL, /* copy */ \
  1688. _ ## NAME ## Finalize, /* finalize */ \
  1689. NULL, /* equal -- NULL means pointer equality. */ \
  1690. NULL, /* hash -- NULL means pointer hash. */ \
  1691. NULL, /* copyFormattingDesc */ \
  1692. NULL, /* copyDebugDesc */ \
  1693. NULL, /* reclaim */ \
  1694. NULL /* refcount */ \
  1695. }; \
  1696. \
  1697. static void _ ## NAME ## GetTypeID( void *inContext ) \
  1698. { \
  1699. (void) inContext; \
  1700. \
  1701. g ## NAME ## TypeID = _CFRuntimeRegisterClass( &k ## NAME ## Class ); \
  1702. check( g ## NAME ## TypeID != _kCFRuntimeNotATypeID ); \
  1703. } \
  1704. \
  1705. CFTypeID NAME ## GetTypeID( void ) \
  1706. { \
  1707. dispatch_once_f( &g ## NAME ## InitOnce, NULL, _ ## NAME ## GetTypeID ); \
  1708. return( g ## NAME ## TypeID ); \
  1709. } \
  1710. \
  1711. check_compile_time( sizeof_field( struct NAME ## Private, base ) == sizeof( CFRuntimeBase ) ); \
  1712. check_compile_time( offsetof( struct NAME ## Private, base ) == 0 )
  1713. #define CF_OBJECT_CREATE( NAME, OBJ, ERR, EXIT_LABEL ) \
  1714. do \
  1715. { \
  1716. size_t extraLen; \
  1717. \
  1718. extraLen = sizeof( *OBJ ) - sizeof( OBJ->base ); \
  1719. OBJ = (NAME ## Ref) _CFRuntimeCreateInstance( NULL, NAME ## GetTypeID(), (CFIndex) extraLen, NULL ); \
  1720. require_action( OBJ, EXIT_LABEL, ERR = kNoMemoryErr ); \
  1721. memset( ( (uint8_t *) OBJ ) + sizeof( OBJ->base ), 0, extraLen ); \
  1722. \
  1723. } while( 0 )
  1724. #if 0
  1725. #pragma mark == Macros ==
  1726. #endif
  1727. //---------------------------------------------------------------------------------------------------------------------------
  1728. /*! @defined kSizeCString
  1729. @abstract A meta-value to pass to supported routines to indicate the size should be calculated with strlen.
  1730. */
  1731. #define kSizeCString ( (size_t) -1 )
  1732. //---------------------------------------------------------------------------------------------------------------------------
  1733. /*! @defined countof
  1734. @abstract Determines the number of elements in an array.
  1735. */
  1736. #define countof( X ) ( sizeof( X ) / sizeof( X[ 0 ] ) )
  1737. #define countof_field( TYPE, FIELD ) countof( ( (TYPE *) 0 )->FIELD )
  1738. //---------------------------------------------------------------------------------------------------------------------------
  1739. /*! @defined offsetof
  1740. @abstract Number of bytes from the beginning of the type to the specified field.
  1741. */
  1742. #if( !defined( offsetof ) )
  1743. #define offsetof( TYPE, FIELD ) ( (size_t)(uintptr_t)( &( (TYPE *) 0 )->FIELD ) )
  1744. #endif
  1745. //---------------------------------------------------------------------------------------------------------------------------
  1746. /*! @defined sizeof_element
  1747. @abstract Determines the size of an array element.
  1748. */
  1749. #define sizeof_element( X ) sizeof( X[ 0 ] )
  1750. //---------------------------------------------------------------------------------------------------------------------------
  1751. /*! @defined sizeof_field
  1752. @abstract Determines the size of a field of a type.
  1753. */
  1754. #define sizeof_field( TYPE, FIELD ) sizeof( ( ( (TYPE *) 0 )->FIELD ) )
  1755. //---------------------------------------------------------------------------------------------------------------------------
  1756. /*! @defined sizeof_string
  1757. @abstract Determines the size of a constant C string, excluding the null terminator.
  1758. */
  1759. #define sizeof_string( X ) ( sizeof( (X) ) - 1 )
  1760. //---------------------------------------------------------------------------------------------------------------------------
  1761. /*! @function AbsoluteDiff
  1762. @abstract Returns the absolute value of the difference between two values.
  1763. */
  1764. #define AbsoluteDiff( X, Y ) ( ( (X) < (Y) ) ? ( (Y) - (X) ) : ( (X) - (Y) ) )
  1765. //---------------------------------------------------------------------------------------------------------------------------
  1766. /*! @function AbsoluteValue
  1767. @abstract Returns the absolute value of a value.
  1768. */
  1769. #define AbsoluteValue( X ) ( ( (X) < 0 ) ? -(X) : (X) )
  1770. //---------------------------------------------------------------------------------------------------------------------------
  1771. /*! @function RoundDown
  1772. @abstract Rounds VALUE down to the nearest multiple of MULTIPLE.
  1773. */
  1774. #define RoundDown( VALUE, MULTIPLE ) ( ( (VALUE) / (MULTIPLE) ) * (MULTIPLE) )
  1775. //---------------------------------------------------------------------------------------------------------------------------
  1776. /*! @function RoundUp
  1777. @abstract Rounds VALUE up to the nearest multiple of MULTIPLE.
  1778. */
  1779. #define RoundUp( VALUE, MULTIPLE ) ( ( ( (VALUE) + ( (MULTIPLE) - 1 ) ) / (MULTIPLE) ) * (MULTIPLE) )
  1780. //---------------------------------------------------------------------------------------------------------------------------
  1781. /*! @function RoundTo
  1782. @abstract Rounds a value to a specific precision.
  1783. @discussion
  1784. This can round to any arbitrary precision. To round to a specific number of decimal digits, use a precision that is
  1785. pow( 10, -digits ). For example, for 2 decimal places, pow( 10, -2 ) -> .01 and RoundTo( 1.234, .01 ) -> 1.23. This
  1786. can also be used to round to other precisions, such as 1/8: RoundTo( 1.3, 1.0 / 8.0 ) -> 1.25.
  1787. */
  1788. #define RoundTo( VALUE, PRECISION ) ( floor( ( (VALUE) / (PRECISION) ) + 0.5 ) * (PRECISION) )
  1789. //---------------------------------------------------------------------------------------------------------------------------
  1790. /*! @function IsAligned
  1791. @abstract Returns non-zero if X is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
  1792. */
  1793. #define IsAligned( X, Y ) ( ( (X) & ( (Y) - 1 ) ) == 0 )
  1794. //---------------------------------------------------------------------------------------------------------------------------
  1795. /*! @function IsFieldAligned
  1796. @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.
  1797. */
  1798. #define IsFieldAligned( X, TYPE, FIELD, Y ) IsAligned( ( (uintptr_t)(X) ) + offsetof( TYPE, FIELD ), (Y) )
  1799. //---------------------------------------------------------------------------------------------------------------------------
  1800. /*! @function PtrsOverlap
  1801. @abstract Returns true if the two ptr/len pairs overlap each other.
  1802. */
  1803. #define PtrsOverlap( PTR1, LEN1, PTR2, LEN2 ) \
  1804. ( !( ( ( ( (uintptr_t)(PTR1) ) + (LEN1) ) <= ( (uintptr_t)(PTR2) ) ) || \
  1805. ( ( (uintptr_t)(PTR1) ) >= ( ( (uintptr_t)(PTR2) ) + (LEN2) ) ) ) )
  1806. //---------------------------------------------------------------------------------------------------------------------------
  1807. /*! @function IsPtrAligned
  1808. @abstract Returns non-zero if PTR is aligned to a Y byte boundary and 0 if not. Y must be a power of 2.
  1809. */
  1810. #define IsPtrAligned( PTR, Y ) ( ( ( (uintptr_t)(PTR) ) & ( (Y) - 1 ) ) == 0 )
  1811. //---------------------------------------------------------------------------------------------------------------------------
  1812. /*! @function AlignDown
  1813. @abstract Aligns X down to a Y byte boundary. Y must be a power of 2.
  1814. */
  1815. #define AlignDown( X, Y ) ( (X) & ~( (Y) - 1 ) )
  1816. //---------------------------------------------------------------------------------------------------------------------------
  1817. /*! @function AlignUp
  1818. @abstract Aligns X up to a Y byte boundary. Y must be a power of 2.
  1819. */
  1820. #define AlignUp( X, Y ) ( ( (X) + ( (Y) - 1 ) ) & ~( (Y) - 1 ) )
  1821. //---------------------------------------------------------------------------------------------------------------------------
  1822. /*! @function AlignedBuffer
  1823. @abstract Specifies a buffer of a specific number of bytes that is aligned to the strictest C alignment.
  1824. @discussion
  1825. This is useful for things like defining a buffer on the stack that can be cast to structures that may need alignment.
  1826. For example, the following allocates a 128 byte buffer on the stack that is safe to cast as a uint64_t:
  1827. AlignedBuffer( 128 ) buf;
  1828. uint64_t * ptr;
  1829. ptr = (uint64_t *) &buf; // This is safe because the buffer is guaranteed to be aligned for the largest type.
  1830. */
  1831. #define AlignedBuffer( SIZE ) \
  1832. union \
  1833. { \
  1834. uintmax_t align; \
  1835. uint8_t buf[ SIZE ]; \
  1836. }
  1837. //---------------------------------------------------------------------------------------------------------------------------
  1838. /*! @group Logical and arithmetic shifts on signed values.
  1839. @abstract These work around undefined behavior in C for shifts of signed values.
  1840. ASR = Arithmetic Shift Right. The sign bit is replicated to fill in vacant positions (e.g. 0x80 >> 1 = 0xC0).
  1841. LSR = Logical Shift Right. Zero bits fill in vacant positions (e.g. 0x80 >> 1 = 0x40).
  1842. X is the value and N is the number of bits to shift. The return value contains the result.
  1843. Warning: shifting a signed value to the right is not the same as dividing because shifts round down instead of
  1844. toward zero so -1 >> 1 = -1 instead of 0. If you care about the low bit then you'll need something better.
  1845. */
  1846. #define HighOnes32( N ) ( ~( (uint32_t) 0 ) << ( 32 - (N) ) )
  1847. #define HighOnes64( N ) ( ~( (uint64_t) 0 ) << ( 64 - (N) ) )
  1848. #define LSR32( X, N ) ( (int32_t)( ( (uint32_t)(X) ) >> (N) ) )
  1849. #define ASR32( X, N ) ( (int32_t)( ( ( (uint32_t)(X) ) >> (N) ) ^ ( ( ( (int32_t)(X) ) < 0 ) ? HighOnes32( (N) ) : 0 ) ) )
  1850. #define LSR64( X, N ) ( (int64_t)( ( (uint64_t)(X) ) >> (N) ) )
  1851. #define ASR64( X, N ) ( (int64_t)( ( ( (uint64_t)(X) ) >> (N) ) ^ ( ( ( (int64_t)(X) ) < 0 ) ? HighOnes64( (N) ) : 0 ) ) )
  1852. //---------------------------------------------------------------------------------------------------------------------------
  1853. /*! @group BitRotates
  1854. @abstract Rotates X COUNT bits to the left or right.
  1855. */
  1856. #define ROTL( X, N, SIZE ) ( ( (X) << (N) ) | ( (X) >> ( (SIZE) - N ) ) )
  1857. #define ROTR( X, N, SIZE ) ( ( (X) >> (N) ) | ( (X) << ( (SIZE) - N ) ) )
  1858. #define ROTL32( X, N ) ROTL( (X), (N), 32 )
  1859. #define ROTR32( X, N ) ROTR( (X), (N), 32 )
  1860. #define ROTL64( X, N ) ROTL( (X), (N), 64 )
  1861. #define ROTR64( X, N ) ROTR( (X), (N), 64 )
  1862. #define RotateBitsLeft( X, N ) ROTL( (X), (N), sizeof( (X) ) * 8 )
  1863. #define RotateBitsRight( X, N ) ROTR( (X), (N), sizeof( (X) ) * 8 )
  1864. //---------------------------------------------------------------------------------------------------------------------------
  1865. /*! @function IsOdd
  1866. @abstract Returns non-zero if the value is odd and 0 if it is even.
  1867. */
  1868. #define IsOdd( X ) ( ( (X) & 1 ) != 0 )
  1869. //---------------------------------------------------------------------------------------------------------------------------
  1870. /*! @function IsEven
  1871. @abstract Returns non-zero if the value is even and 0 if it is odd.
  1872. */
  1873. #define IsEven( X ) ( ( (X) & 1 ) == 0 )
  1874. //---------------------------------------------------------------------------------------------------------------------------
  1875. /*! @function IsPowerOf2
  1876. @abstract Returns non-zero if the value is a power of 2 and 0 if it is not. 0 and 1 are not considered powers of 2.
  1877. */
  1878. #define IsPowerOf2( X ) ( ( (X) > 1 ) && ( ( (X) & ( (X) - 1 ) ) == 0 ) )
  1879. //---------------------------------------------------------------------------------------------------------------------------
  1880. /*! @function MinPowerOf2BytesForValue
  1881. @abstract Returns the minimum number of power-of-2 bytes needed to hold a specific value.
  1882. */
  1883. #define MinPowerOf2BytesForValue( X ) ( \
  1884. ( (X) & UINT64_C( 0xFFFFFFFF00000000 ) ) ? 8 : \
  1885. ( (X) & UINT64_C( 0x00000000FFFF0000 ) ) ? 4 : \
  1886. ( (X) & UINT64_C( 0x000000000000FF00 ) ) ? 2 : \
  1887. 1 )
  1888. //---------------------------------------------------------------------------------------------------------------------------
  1889. /*! @function IsMultipleOf
  1890. @abstract Returns non-zero if X is a multiple of Y.
  1891. */
  1892. #define IsMultipleOf( X, Y ) ( ( ( (X) / (Y) ) * (Y) ) == (X) )
  1893. //---------------------------------------------------------------------------------------------------------------------------
  1894. /*! @function Min
  1895. @abstract Returns the lesser of X and Y.
  1896. */
  1897. #if( !defined( Min ) )
  1898. #define Min( X, Y ) ( ( (X) < (Y) ) ? (X) : (Y) )
  1899. #endif
  1900. //---------------------------------------------------------------------------------------------------------------------------
  1901. /*! @function Max
  1902. @abstract Returns the greater of X and Y.
  1903. */
  1904. #if( !defined( Max ) )
  1905. #define Max( X, Y ) ( ( (X) > (Y) ) ? (X) : (Y) )
  1906. #endif
  1907. //---------------------------------------------------------------------------------------------------------------------------
  1908. /*! @function Clamp
  1909. @abstract Clamps a value to no less than "a" and no greater than "b".
  1910. */
  1911. #define Clamp( x, a, b ) Max( (a), Min( (b), (x) ) )
  1912. //---------------------------------------------------------------------------------------------------------------------------
  1913. /*! @function increment_wrap
  1914. @abstract Increments VAR and if it wraps to 0, set VAR to WRAP.
  1915. */
  1916. #define increment_wrap( VAR, WRAP ) do { ++(VAR); if( (VAR) == 0 ) { (VAR) = (WRAP); } } while( 0 )
  1917. //---------------------------------------------------------------------------------------------------------------------------
  1918. /*! @function increment_saturate
  1919. @abstract Increments VAR unless doing so would cause it to exceed MAX.
  1920. */
  1921. #define increment_saturate( VAR, MAX ) do { if( (VAR) < (MAX) ) { ++(VAR); } } while( 0 )
  1922. //---------------------------------------------------------------------------------------------------------------------------
  1923. /*! @function add_saturate
  1924. @abstract Adds VALUE to VAR. If the result would go over MAX, VAR is capped to MAX.
  1925. */
  1926. #define add_saturate( VAR, VALUE, MAX ) \
  1927. do \
  1928. { \
  1929. if( (VAR) < ( (MAX) - (VALUE) ) ) \
  1930. { \
  1931. (VAR) += (VALUE); \
  1932. } \
  1933. else \
  1934. { \
  1935. (VAR) = (MAX); \
  1936. } \
  1937. \
  1938. } while( 0 )
  1939. //---------------------------------------------------------------------------------------------------------------------------
  1940. /*! @function median_of_3
  1941. @abstract Returns the median (middle) value given 3 values.
  1942. */
  1943. #define median_of_3( x0, x1, x2 ) \
  1944. ( ( (x0) > (x1) ) ? \
  1945. ( (x1) > (x2) ) ? (x1) : ( (x2) > (x0) ) ? (x0) : (x2) : \
  1946. ( (x1) < (x2) ) ? (x1) : ( (x2) < (x0) ) ? (x0) : (x2) )
  1947. //---------------------------------------------------------------------------------------------------------------------------
  1948. /*! @function median_of_5
  1949. @abstract Returns the median (middle) value given 5 values.
  1950. */
  1951. #define median_of_5( a, b, c, d, e ) \
  1952. ( (b) < (a) ? (d) < (c) ? (b) < (d) ? (a) < (e) ? (a) < (d) ? (e) < (d) ? (e) \
  1953. : (d) \
  1954. : (c) < (a) ? (c) : (a) \
  1955. : (e) < (d) ? (a) < (d) ? (a) : (d) \
  1956. : (c) < (e) ? (c) : (e) \
  1957. : (c) < (e) ? (b) < (c) ? (a) < (c) ? (a) : (c) \
  1958. : (e) < (b) ? (e) : (b) \
  1959. : (b) < (e) ? (a) < (e) ? (a) : (e) \
  1960. : (c) < (b) ? (c) : (b) \
  1961. : (b) < (c) ? (a) < (e) ? (a) < (c) ? (e) < (c) ? (e) : (c) \
  1962. : (d) < (a) ? (d) : (a) \
  1963. : (e) < (c) ? (a) < (c) ? (a) : (c) \
  1964. : (d) < (e) ? (d) : (e) \
  1965. : (d) < (e) ? (b) < (d) ? (a) < (d) ? (a) : (d) \
  1966. : (e) < (b) ? (e) : (b) \
  1967. : (b) < (e) ? (a) < (e) ? (a) : (e) \
  1968. : (d) < (b) ? (d) : (b) \
  1969. : (d) < (c) ? (a) < (d) ? (b) < (e) ? (b) < (d) ? (e) < (d) ? (e) : (d) \
  1970. : (c) < (b) ? (c) : (b) \
  1971. : (e) < (d) ? (b) < (d) ? (b) : (d) \
  1972. : (c) < (e) ? (c) : (e) \
  1973. : (c) < (e) ? (a) < (c) ? (b) < (c) ? (b) : (c) \
  1974. : (e) < (a) ? (e) : (a) \
  1975. : (a) < (e) ? (b) < (e) ? (b) : (e) \
  1976. : (c) < (a) ? (c) : (a) \
  1977. : (a) < (c) ? (b) < (e) ? (b) < (c) ? (e) < (c) ? (e) : (c) \
  1978. : (d) < (b) ? (d) : (b) \
  1979. : (e) < (c) ? (b) < (c) ? (b) : (c) \
  1980. : (d) < (e) ? (d) : (e) \
  1981. : (d) < (e) ? (a) < (d) ? (b) < (d) ? (b) : (d) \
  1982. : (e) < (a) ? (e) : (a) \
  1983. : (a) < (e) ? (b) < (e) ? (b) : (e) \
  1984. : (d) < (a) ? (d) : (a) )
  1985. //---------------------------------------------------------------------------------------------------------------------------
  1986. /*! @function append_decimal_string
  1987. @abstract Appends a decimal string to a buffer.
  1988. @param X Unsigned decimal number to convert to a string. It will be modified by this macro.
  1989. @param DST Pointer to write string to. Will point to end of string on return.
  1990. @discussion
  1991. Example usage:
  1992. char str[ 32 ];
  1993. char * dst;
  1994. int x;
  1995. strcpy( str, "test" );
  1996. dst = str + 4;
  1997. x = 1234;
  1998. append_decimal_string( x, dst );
  1999. strcpy( dst, "end" );
  2000. ... str is "test1234end".
  2001. */
  2002. #define append_decimal_string( X, DST ) \
  2003. do \
  2004. { \
  2005. char _adsBuf[ 32 ]; \
  2006. char * _adsPtr; \
  2007. \
  2008. _adsPtr = _adsBuf; \
  2009. do \
  2010. { \
  2011. *_adsPtr++ = (char)( '0' + ( (X) % 10 ) ); \
  2012. (X) /= 10; \
  2013. \
  2014. } while( (X) > 0 ); \
  2015. \
  2016. while( _adsPtr > _adsBuf ) \
  2017. { \
  2018. *(DST)++ = *( --_adsPtr ); \
  2019. } \
  2020. \
  2021. } while( 0 )
  2022. //---------------------------------------------------------------------------------------------------------------------------
  2023. /*! @function BitArray
  2024. @abstract Macros for working with bit arrays.
  2025. @discussion
  2026. This treats bit numbers starting from the left so bit 0 is 0x80 in byte 0, bit 1 is 0x40 in bit 0,
  2027. bit 8 is 0x80 in byte 1, etc. For example, the following ASCII art shows how the bits are arranged:
  2028. 1 1 1 1 1 1 1 1 1 1 2 2 2 2
  2029. Bit 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  2030. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2031. | x |x | x x| = 0x20 0x80 0x41 (bits 2, 8, 17, and 23).
  2032. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2033. Byte 0 1 2
  2034. */
  2035. #define BitArray_MinBytes( ARRAY, N_BYTES ) memrlen( (ARRAY), (N_BYTES) )
  2036. #define BitArray_MaxBytes( BITS ) ( ( (BITS) + 7 ) / 8 )
  2037. #define BitArray_MaxBits( ARRAY_BYTES ) ( (ARRAY_BYTES) * 8 )
  2038. #define BitArray_Clear( ARRAY_PTR, ARRAY_BYTES ) memset( (ARRAY_PTR), 0, (ARRAY_BYTES) );
  2039. #define BitArray_GetBit( PTR, LEN, BIT ) \
  2040. ( ( (BIT) < BitArray_MaxBits( (LEN) ) ) && ( (PTR)[ (BIT) / 8 ] & ( 1 << ( 7 - ( (BIT) & 7 ) ) ) ) )
  2041. #define BitArray_SetBit( ARRAY, BIT ) ( (ARRAY)[ (BIT) / 8 ] |= ( 1 << ( 7 - ( (BIT) & 7 ) ) ) )
  2042. #define BitArray_ClearBit( ARRAY, BIT ) ( (ARRAY)[ (BIT) / 8 ] &= ~( 1 << ( 7 - ( (BIT) & 7 ) ) ) )
  2043. //---------------------------------------------------------------------------------------------------------------------------
  2044. /*! @function InsertBits
  2045. @abstract Inserts BITS (both 0 and 1 bits) into X, controlled by MASK and SHIFT, and returns the result.
  2046. @discussion
  2047. MASK is the bitmask of the bits in the final position.
  2048. SHIFT is the number of bits to shift left for 1 to reach the first bit position of MASK.
  2049. For example, if you wanted to insert 0x3 into the leftmost 4 bits of a 32-bit value:
  2050. InsertBits( 0, 0x3, 0xF0000000U, 28 ) == 0x30000000
  2051. */
  2052. #define InsertBits( X, BITS, MASK, SHIFT ) ( ( (X) & ~(MASK) ) | ( ( (BITS) << (SHIFT) ) & (MASK) ) )
  2053. //---------------------------------------------------------------------------------------------------------------------------
  2054. /*! @function ExtractBits
  2055. @abstract Extracts bits from X, controlled by MASK and SHIFT, and returns the result.
  2056. @discussion
  2057. MASK is the bitmask of the bits in the final position.
  2058. SHIFT is the number of bits to shift right to right justify MASK.
  2059. For example, if you had a 32-bit value (e.g. 0x30000000) wanted the left-most 4 bits (e.g. 3 in this example):
  2060. ExtractBits( 0x30000000U, 0xF0000000U, 28 ) == 0x3
  2061. */
  2062. #define ExtractBits( X, MASK, SHIFT ) ( ( (X) >> (SHIFT) ) & ( (MASK) >> (SHIFT) ) )
  2063. //---------------------------------------------------------------------------------------------------------------------------
  2064. /*! @function SetOrClearBits
  2065. @abstract Sets bits if the test is non-zero or clears bits if the test is zero.
  2066. @discussion
  2067. int x;
  2068. SetOrClearBits( &x, 0x7, true ); // Sets bits 0, 1, 2
  2069. SetOrClearBits( &x, 0x7, false ); // Clears bits 0, 1, 2
  2070. */
  2071. #define SetOrClearBits( VALUE_PTR, BITS, TEST ) \
  2072. do { *(VALUE_PTR) = (TEST) ? ( *(VALUE_PTR) | (BITS) ) : ( *(VALUE_PTR) & ~(BITS) ); } while( 0 )
  2073. //---------------------------------------------------------------------------------------------------------------------------
  2074. /*! @function Stringify
  2075. @abstract Stringify's an expression.
  2076. @discussion
  2077. Stringify macros to process raw text passed via -D options to C string constants. The double-wrapping is necessary
  2078. because the C preprocessor doesn't perform its normal argument expansion pre-scan with stringified macros so the
  2079. -D macro needs to be expanded once via the wrapper macro then stringified so the raw text is stringified. Otherwise,
  2080. the replacement value would be used instead of the symbolic name (only for preprocessor symbols like #defines).
  2081. For example:
  2082. #define kMyConstant 1
  2083. printf( "%s", Stringify( kMyConstant ) ); // Prints "kMyConstant"
  2084. printf( "%s", StringifyExpansion( kMyConstant ) ); // Prints "1"
  2085. Non-preprocessor symbols do not have this issue. For example:
  2086. enum
  2087. {
  2088. kMyConstant = 1
  2089. };
  2090. printf( "%s", Stringify( kMyConstant ) ); // Prints "kMyConstant"
  2091. printf( "%s", StringifyExpansion( kMyConstant ) ); // Prints "kMyConstant"
  2092. See <http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html> for more info on C preprocessor pre-scanning.
  2093. */
  2094. #define Stringify( X ) # X
  2095. #define StringifyExpansion( X ) Stringify( X )
  2096. //---------------------------------------------------------------------------------------------------------------------------
  2097. /*! @function Forget macros
  2098. @abstract These take a pointer and if what it points to is valid, it gets rid of it and marks it invalid.
  2099. */
  2100. #define ForgetCustom( X, DELETER ) do { if( *(X) ) { DELETER( *(X) ); *(X) = NULL; } } while( 0 )
  2101. #define ForgetCustomEx( X, STOPPER, DELETER ) do { if( *(X) ) { STOPPER( *(X) ); DELETER( *(X) ); *(X) = NULL; } } while( 0 )
  2102. #define AudioConverterForget( X ) ForgetCustom( X, AudioConverterDispose )
  2103. #define dispatch_forget( X ) ForgetCustom( X, dispatch_release )
  2104. #define DNSServiceForget( X ) ForgetCustom( X, DNSServiceRefDeallocate )
  2105. #define ForgetBlock( X ) ForgetCustom( X, Block_release )
  2106. #define ForgetCF( X ) ForgetCustom( X, CFRelease )
  2107. #define ForgetIOKitKernelObject( X ) do { if( *(X) ) { ( *(X) )->release(); *(X) = NULL; } } while( 0 )
  2108. #define ForgetMem( X ) ForgetCustom( X, free_compat )
  2109. #define ForgetObjectiveCObject( X ) do { [*(X) release]; *(X) = nil; } while( 0 )
  2110. #define ForgetVxSem( X ) do { if( *(X) ) { semDelete( *(X) ); *(X) = 0; } } while( 0 )
  2111. #define ForgetWinHandle( X ) do { if( *(X) ) { CloseHandle( *(X) ); *(X) = 0; } } while( 0 )
  2112. #define ForgetWinRegKey( X ) ForgetCustom( X, RegCloseKey )
  2113. #define IONotificationPortForget( X ) ForgetCustom( X, IONotificationPortDestroy )
  2114. #define SRP_forget( X ) ForgetCustom( X, SRP_free )
  2115. #define SRP_cstr_forget( X ) ForgetCustom( X, cstr_free )
  2116. #define xpc_forget( X ) ForgetCustom( X, xpc_release )
  2117. #define dispatch_socket_forget( SOURCE, SOCK_PTR, SUSPENDED ) \
  2118. do \
  2119. { \
  2120. if( (SOURCE) ) \
  2121. { \
  2122. dispatch_source_cancel( (SOURCE) ); \
  2123. if( (SUSPENDED) ) dispatch_resume( (SOURCE) ); \
  2124. dispatch_release( (SOURCE) ); \
  2125. } \
  2126. else \
  2127. { \
  2128. ForgetSocket( (SOCK_PTR) ); \
  2129. } \
  2130. \
  2131. } while( 0 )
  2132. #define dispatch_source_forget( X ) \
  2133. do \
  2134. { \
  2135. if( *(X) ) \
  2136. { \
  2137. dispatch_source_cancel( *(X) ); \
  2138. dispatch_release( *(X) ); \
  2139. *(X) = NULL; \
  2140. } \
  2141. \
  2142. } while( 0 )
  2143. #define dispatch_source_forget_ex( SOURCE_PTR, SUSPENDED_PTR ) \
  2144. do \
  2145. { \
  2146. if( *(SOURCE_PTR) ) \
  2147. { \
  2148. dispatch_source_cancel( *(SOURCE_PTR) ); \
  2149. if( (SUSPENDED_PTR) && *(SUSPENDED_PTR) ) \
  2150. { \
  2151. dispatch_resume( *(SOURCE_PTR) ); \
  2152. *(SUSPENDED_PTR) = false; \
  2153. } \
  2154. dispatch_release( *(SOURCE_PTR) ); \
  2155. *(SOURCE_PTR) = NULL; \
  2156. } \
  2157. \
  2158. } while( 0 )
  2159. #define dispatch_resume_if_suspended( SOURCE, SUSPENDED_PTR ) \
  2160. do \
  2161. { \
  2162. if( *(SUSPENDED_PTR) ) \
  2163. { \
  2164. *(SUSPENDED_PTR) = false; \
  2165. dispatch_resume( (SOURCE) ); \
  2166. } \
  2167. \
  2168. } while( 0 )
  2169. #define dispatch_suspend_if_resumed( SOURCE, SUSPENDED_PTR ) \
  2170. do \
  2171. { \
  2172. if( !*(SUSPENDED_PTR) ) \
  2173. { \
  2174. *(SUSPENDED_PTR) = true; \
  2175. dispatch_suspend( (SOURCE) ); \
  2176. } \
  2177. \
  2178. } while( 0 )
  2179. #define ForgetANSIFile( X ) \
  2180. do \
  2181. { \
  2182. if( *(X) ) \
  2183. { \
  2184. OSStatus ForgetANSIFileErr; \
  2185. \
  2186. ForgetANSIFileErr = fclose( *(X) ); \
  2187. ForgetANSIFileErr = map_noerr_errno( ForgetANSIFileErr ); \
  2188. check_noerr( ForgetANSIFileErr ); \
  2189. *(X) = NULL; \
  2190. } \
  2191. \
  2192. } while( 0 )
  2193. #define ForgetFD( X ) \
  2194. do \
  2195. { \
  2196. if( IsValidFD( *(X) ) ) \
  2197. { \
  2198. OSStatus ForgetFDErr; \
  2199. \
  2200. ForgetFDErr = CloseFD( *(X) ); \
  2201. ForgetFDErr = map_global_noerr_errno( ForgetFDErr ); \
  2202. check_noerr( ForgetFDErr ); \
  2203. *(X) = kInvalidFD; \
  2204. } \
  2205. \
  2206. } while( 0 )
  2207. #define notify_forget( X ) \
  2208. do \
  2209. { \
  2210. if( *(X) != -1 ) \
  2211. { \
  2212. OSStatus notify_forget_err_; \
  2213. \
  2214. notify_forget_err_ = notify_cancel( *(X) ); \
  2215. check_noerr( notify_forget_err_ ); \
  2216. *(X) = -1; \
  2217. } \
  2218. \
  2219. } while( 0 )
  2220. #define pthread_cond_forget( X ) \
  2221. do \
  2222. { \
  2223. if( *(X) ) \
  2224. { \
  2225. int pthread_cond_forget_err_; \
  2226. \
  2227. DEBUG_USE_ONLY( pthread_cond_forget_err_ ); \
  2228. \
  2229. pthread_cond_forget_err_ = pthread_cond_destroy( *(X) ); \
  2230. check_noerr( pthread_cond_forget_err_ ); \
  2231. *(X) = NULL; \
  2232. } \
  2233. \
  2234. } while( 0 )
  2235. #define pthread_mutex_forget( X ) \
  2236. do \
  2237. { \
  2238. if( *(X) ) \
  2239. { \
  2240. int pthread_mutex_forget_err_; \
  2241. \
  2242. DEBUG_USE_ONLY( pthread_mutex_forget_err_ ); \
  2243. \
  2244. pthread_mutex_forget_err_ = pthread_mutex_destroy( *(X) ); \
  2245. check_noerr( pthread_mutex_forget_err_ ); \
  2246. *(X) = NULL; \
  2247. } \
  2248. \
  2249. } while( 0 )
  2250. #define ForgetSocket( X ) \
  2251. do \
  2252. { \
  2253. if( IsValidSocket( *(X) ) ) \
  2254. { \
  2255. OSStatus ForgetSocketErr; \
  2256. \
  2257. ForgetSocketErr = close_compat( *(X) ); \
  2258. ForgetSocketErr = map_socket_noerr_errno( *(X), ForgetSocketErr ); \
  2259. check_noerr( ForgetSocketErr ); \
  2260. *(X) = kInvalidSocketRef; \
  2261. } \
  2262. \
  2263. } while( 0 )
  2264. #define IOObjectForget( X ) \
  2265. do \
  2266. { \
  2267. if( *(X) != IO_OBJECT_NULL ) \
  2268. { \
  2269. IOReturn IOObjectForgetErr; \
  2270. \
  2271. IOObjectForgetErr = IOObjectRelease( *(X) ); \
  2272. check_noerr( IOObjectForgetErr ); \
  2273. *(X) = IO_OBJECT_NULL; \
  2274. } \
  2275. \
  2276. } while( 0 )
  2277. #define xpc_connection_forget( X ) \
  2278. do \
  2279. { \
  2280. if( *(X) ) \
  2281. { \
  2282. xpc_connection_cancel( *(X) ); \
  2283. xpc_release( *(X) ); \
  2284. *(X) = NULL; \
  2285. } \
  2286. \
  2287. } while( 0 )
  2288. //---------------------------------------------------------------------------------------------------------------------------
  2289. /*! @function Replace macros
  2290. @abstract These retain/copy/etc the new thing and release/free/etc the old thing (if the old thing is valid).
  2291. */
  2292. #define ReplaceBlock( BLOCK_PTR, NEW_BLOCK ) \
  2293. do \
  2294. { \
  2295. __typeof( (NEW_BLOCK) ) ReplaceBlock_TempBlock = (NEW_BLOCK); \
  2296. \
  2297. if( ReplaceBlock_TempBlock ) \
  2298. { \
  2299. ReplaceBlock_TempBlock = Block_copy( ReplaceBlock_TempBlock ); \
  2300. check( ReplaceBlock_TempBlock ); \
  2301. } \
  2302. if( *(BLOCK_PTR) ) Block_release( *(BLOCK_PTR) ); \
  2303. *(BLOCK_PTR) = ReplaceBlock_TempBlock; \
  2304. \
  2305. } while( 0 )
  2306. #define ReplaceCF( OBJECT_PTR, NEW_OBJECT ) \
  2307. do \
  2308. { \
  2309. CFTypeRef * ReplaceCF_objectPtr = (CFTypeRef *)(OBJECT_PTR); \
  2310. CFTypeRef ReplaceCF_oldObject = *ReplaceCF_objectPtr; \
  2311. CFTypeRef ReplaceCF_newObject = (NEW_OBJECT); \
  2312. \
  2313. if( ReplaceCF_newObject ) CFRetain( ReplaceCF_newObject ); \
  2314. *ReplaceCF_objectPtr = ReplaceCF_newObject; \
  2315. if( ReplaceCF_oldObject ) CFRelease( ReplaceCF_oldObject ); \
  2316. \
  2317. } while( 0 )
  2318. #define ReplaceDispatchQueue( QUEUE_PTR, NEW_QUEUE ) \
  2319. do \
  2320. { \
  2321. dispatch_queue_t ReplaceDispatchQueue_TempQueue = (NEW_QUEUE); \
  2322. \
  2323. if( !ReplaceDispatchQueue_TempQueue ) ReplaceDispatchQueue_TempQueue = dispatch_get_main_queue(); \
  2324. dispatch_retain( ReplaceDispatchQueue_TempQueue ); \
  2325. if( *(QUEUE_PTR) ) dispatch_release( *(QUEUE_PTR) ); \
  2326. *(QUEUE_PTR) = ReplaceDispatchQueue_TempQueue; \
  2327. \
  2328. } while( 0 )
  2329. //---------------------------------------------------------------------------------------------------------------------------
  2330. /*! @function MemEqual
  2331. @abstract Returns non-zero if two ptr/len pairs are equal and 0 otherwise.
  2332. */
  2333. #define MemEqual( PTR1, LEN1, PTR2, LEN2 ) \
  2334. ( ( ( LEN1 ) == ( LEN2 ) ) && ( memcmp( ( PTR1 ), ( PTR2 ), ( LEN1 ) ) == 0 ) )
  2335. #define MemIEqual( PTR1, LEN1, PTR2, LEN2 ) \
  2336. ( ( ( LEN1 ) == ( LEN2 ) ) && ( memicmp( ( PTR1 ), ( PTR2 ), ( LEN1 ) ) == 0 ) )
  2337. //---------------------------------------------------------------------------------------------------------------------------
  2338. /*! @function DECLARE_QSORT_FUNC / DEFINE_QSORT_FUNC
  2339. @abstract Declares/defines a qsort-compatible sort function for numeric types.
  2340. @abstract
  2341. Use it like this in your header file:
  2342. DECLARE_QSORT_NUMERIC_COMPARATOR( cmp_double );
  2343. Then in your source file:
  2344. DEFINE_QSORT_NUMERIC_COMPARATOR( double, cmd_double );
  2345. Then to use it in code:
  2346. qsort( array, count, elementSize, cmd_double );
  2347. */
  2348. #define DECLARE_QSORT_NUMERIC_COMPARATOR( NAME ) int NAME( const void *a, const void *b )
  2349. #define DEFINE_QSORT_NUMERIC_COMPARATOR( TYPE, NAME ) \
  2350. int NAME( const void *a, const void *b ) \
  2351. { \
  2352. TYPE const aa = *( (const TYPE *) a ); \
  2353. TYPE const bb = *( (const TYPE *) b ); \
  2354. \
  2355. return( ( aa > bb ) - ( aa < bb ) ); \
  2356. }
  2357. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_int8 );
  2358. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_uint8 );
  2359. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_int16 );
  2360. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_uint16 );
  2361. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_int32 );
  2362. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_uint32 );
  2363. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_int64 );
  2364. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_uint64 );
  2365. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_float );
  2366. DECLARE_QSORT_NUMERIC_COMPARATOR( qsort_cmp_double );
  2367. //---------------------------------------------------------------------------------------------------------------------------
  2368. /*! @defined HAS_FEATURE
  2369. @abstract Performs a compile-time check for a feature flag and fails to compile if the feature is not defined.
  2370. @discussion
  2371. This can be used to detect if a feature is defined to 1 (feature enabled) or defined 0 (feature not enabled) or
  2372. it fails to compile if the feature flag is not defined at all. This can help catch errors when you're testing
  2373. for a feature, but typed it wrong, forgot to include the right header file, or passed the wrong -D compile flags.
  2374. Here's how you use it:
  2375. #if( HAS_FEATURE( COOL_FEATURE ) )
  2376. ... code to relate to COOL_FEATURE.
  2377. #endif
  2378. */
  2379. #define HAS_FEATURE_CAT( a, b ) a ## b
  2380. #define HAS_FEATURE_CAT2( a, b ) HAS_FEATURE_CAT( a, b )
  2381. #define HAS_FEATURE_CHECK_0 1
  2382. #define HAS_FEATURE_CHECK_1 1
  2383. #define HAS_FEATURE( X ) ( X / HAS_FEATURE_CAT2( HAS_FEATURE_CHECK_, X ) )
  2384. #if 0
  2385. #pragma mark == Fixed-Point Math ==
  2386. #endif
  2387. //---------------------------------------------------------------------------------------------------------------------------
  2388. /*! @group Fixed-point math
  2389. @abstract Macros to work with Q32.32 numbers. <http://en.wikipedia.org/wiki/Q_(number_format)>.
  2390. @discussion
  2391. - Addition and subtraction with other Q32.32 numbers can use the normal + and - operators, respectively.
  2392. - Multiplication by other Q32.32 numbers needs to use the normal * then right shift the result by 32.
  2393. Warning: Due to the lack of a standard int128_t, multiplication will overflow if either value is >= 1 (and other cases).
  2394. - Negation can use the normal unary - operator.
  2395. - Negative numbers can be detected using a normal < 0 check.
  2396. - Setting to 0 can be by simply assigning 0.
  2397. */
  2398. typedef int32_t Q16x16;
  2399. typedef int64_t Q32x32;
  2400. #define kQ16_1pt0 0x00010000 // 1.0 in 16.16 fixed format.
  2401. #define kQ16_0pt5 0x00008000 // 0.5 in 16.16 fixed format.
  2402. #define FloatToQ32x32( X ) ( (int64_t)( (X) * ( (double) UINT32_C( 0xFFFFFFFF ) ) ) )
  2403. #define Q32x32ToFloat( X ) ( ( (double)(X) ) / ( (double)( UINT32_C( 0xFFFFFFFF ) ) ) )
  2404. #define Q32x32_Integer( a ) ( ( (Q32x32)(a) ) << 32 )
  2405. #define Q32x32_GetInteger( x ) ( (int32_t)( ( (x) < 0 ) ? ( -( -(x) >> 32 ) ) : ( (x) >> 32 ) ) )
  2406. #define Q32x32_SetInteger( x, a ) ( (x) = ( (Q32x32)(a) ) << 32 )
  2407. #define Q32x32_GetFraction( x ) ( (uint32_t)( (x) & UINT32_C( 0xFFFFFFFF ) ) )
  2408. #define Q32x32_AddInteger( x, a ) ( (x) += ( ( (Q32x32)(a) ) << 32 ) )
  2409. #define Q32x32_MultiplyByInteger( x, a ) ( (x) *= (a) )
  2410. #define Q32x32_RightShift( x, a ) \
  2411. do \
  2412. { \
  2413. if( (x) < 0 ) (x) = -( -(x) >> (a) ); \
  2414. else (x) = (x) >> (a); \
  2415. \
  2416. } while ( 0 )
  2417. #if 0
  2418. #pragma mark -
  2419. #pragma mark == Modular Math ==
  2420. #endif
  2421. //---------------------------------------------------------------------------------------------------------------------------
  2422. /*! @group Modular math
  2423. @abstract AKA Serial Number Arithmetic per RFC 1982). See <http://en.wikipedia.org/wiki/Serial_number_arithmetic>.
  2424. @discussion
  2425. EQ: Returns non-zero if A == B.
  2426. LT: Returns non-zero if A < B.
  2427. LE: Returns non-zero if A <= B.
  2428. GT: Returns non-zero if A > B.
  2429. GE: Returns non-zero if A >= B.
  2430. Cmp: Returns 0 if A == B, < 0 if A < B, and > 0 if A > B. Do not compare against -1 or 1...use < 0 or > 0, respectively.
  2431. Diff: Returns the absolute value of the difference between A and B (e.g. Mod8_Diff( 5, 10 ) == 5 and Mod8_Diff( 10, 5 ) == 5).
  2432. */
  2433. // 8-bit modular math. Note: these only work if the difference is less than 2^7.
  2434. #if( !defined( Mod8_EQ ) )
  2435. #define Mod8_EQ( A, B ) ( ( (uint8_t)(A) ) == ( (uint8_t)(B) ) )
  2436. #define Mod8_LT( A, B ) ( (int8_t)( ( (uint8_t)(A) ) - ( (uint8_t)(B) ) ) < 0 )
  2437. #define Mod8_LE( A, B ) ( (int8_t)( ( (uint8_t)(A) ) - ( (uint8_t)(B) ) ) <= 0 )
  2438. #define Mod8_GT( A, B ) ( (int8_t)( ( (uint8_t)(A) ) - ( (uint8_t)(B) ) ) > 0 )
  2439. #define Mod8_GE( A, B ) ( (int8_t)( ( (uint8_t)(A) ) - ( (uint8_t)(B) ) ) >= 0 )
  2440. #define Mod8_Cmp( A, B ) ( (int8_t)( ( (uint8_t)(A) ) - ( (uint8_t)(B) ) ) )
  2441. #define Mod8_Diff( A, B ) ( Mod8_LT( (A), (B) ) ? ( (B) - (A) ) : ( (A) - (B) ) )
  2442. #endif
  2443. // 16-bit modular math. Note: these only work if the difference is less than 2^15.
  2444. #if( !defined( Mod16_LT ) )
  2445. #define Mod16_EQ( A, B ) ( ( (uint16_t)(A) ) == ( (uint16_t)(B) ) )
  2446. #define Mod16_LT( A, B ) ( (int16_t)( ( (uint16_t)(A) ) - ( (uint16_t)(B) ) ) < 0 )
  2447. #define Mod16_LE( A, B ) ( (int16_t)( ( (uint16_t)(A) ) - ( (uint16_t)(B) ) ) <= 0 )
  2448. #define Mod16_GT( A, B ) ( (int16_t)( ( (uint16_t)(A) ) - ( (uint16_t)(B) ) ) > 0 )
  2449. #define Mod16_GE( A, B ) ( (int16_t)( ( (uint16_t)(A) ) - ( (uint16_t)(B) ) ) >= 0 )
  2450. #define Mod16_Cmp( A, B ) ( (int16_t)( ( (uint16_t)(A) ) - ( (uint16_t)(B) ) ) )
  2451. #define Mod16_Diff( A, B ) ( Mod16_LT( (A), (B) ) ? ( (B) - (A) ) : ( (A) - (B) ) )
  2452. #endif
  2453. // 32-bit modular math. Note: these only work if the difference is less than 2^31.
  2454. #if( !defined( Mod32_LT ) )
  2455. #define Mod32_EQ( A, B ) ( ( (uint32_t)(A) ) == ( (uint32_t)(B) ) )
  2456. #define Mod32_LT( A, B ) ( (int32_t)( ( (uint32_t)(A) ) - ( (uint32_t)(B) ) ) < 0 )
  2457. #define Mod32_LE( A, B ) ( (int32_t)( ( (uint32_t)(A) ) - ( (uint32_t)(B) ) ) <= 0 )
  2458. #define Mod32_GT( A, B ) ( (int32_t)( ( (uint32_t)(A) ) - ( (uint32_t)(B) ) ) > 0 )
  2459. #define Mod32_GE( A, B ) ( (int32_t)( ( (uint32_t)(A) ) - ( (uint32_t)(B) ) ) >= 0 )
  2460. #define Mod32_Cmp( A, B ) ( (int32_t)( ( (uint32_t)(A) ) - ( (uint32_t)(B) ) ) )
  2461. #define Mod32_Diff( A, B ) ( Mod32_LT( (A), (B) ) ? ( (B) - (A) ) : ( (A) - (B) ) )
  2462. #endif
  2463. #if 0
  2464. #pragma mark -
  2465. #pragma mark == Booleans ==
  2466. #endif
  2467. //---------------------------------------------------------------------------------------------------------------------------
  2468. /*! @typedef bool
  2469. @abstract Standardized boolean type. Built-in with C99 and C++, but emulated elsewhere.
  2470. @discussion
  2471. C++ defines bool, true, and false. Metrowerks allows this to be controlled by the "bool" option though.
  2472. C99 defines __bool_true_false_are_defined when bool, true, and false are defined.
  2473. MacTypes.h defines Boolean, true, and false.
  2474. VxWorks rwos.h defines _RWOS_H_ and defines bool, true, and false if bool is not already defined.
  2475. Note: The Metrowerks has to be in its own block because Microsoft Visual Studio .NET does not completely
  2476. short-circuit and gets confused by the option( bool ) portion of the conditional.
  2477. The moral the story is just don't use "bool" unless you know you're using C++ and never want compatibility with C
  2478. code. Otherwise, it's just too much of a pain. There are also binary compatibility issues because bool may be a
  2479. different size in different environments. Use Boolean instead (provided in this file if needed and always 1 byte).
  2480. */
  2481. #if( defined( __MWERKS__ ) )
  2482. // Note: The following test is done on separate lines because CodeWarrior doesn't like it all on one line.
  2483. #if( !__bool_true_false_are_defined && ( !defined( __cplusplus ) || !__option( bool ) ) )
  2484. #define COMMON_SERVICES_NEEDS_BOOL 1
  2485. #else
  2486. #define COMMON_SERVICES_NEEDS_BOOL 0
  2487. #endif
  2488. // Workaround when building with CodeWarrior, but using the Apple stdbool.h header, which uses _Bool.
  2489. #if( __bool_true_false_are_defined && !defined( __cplusplus ) && !__option( c9x ) )
  2490. #define _Bool int
  2491. #endif
  2492. // Workaround when building with CodeWarrior for C++ with bool disabled and using the Apple stdbool.h header,
  2493. // which defines true and false to map to C++ true and false (which are not enabled). Serenity Now!
  2494. #if( __bool_true_false_are_defined && defined( __cplusplus ) && !__option( bool ) )
  2495. #define true 1
  2496. #define false 0
  2497. #endif
  2498. #else
  2499. #if( !defined( __cplusplus ) && !__bool_true_false_are_defined && !defined( bool ) && !defined( _RWOS_H_ ) && !defined( __IOKIT_IOTYPES_H ) )
  2500. #define COMMON_SERVICES_NEEDS_BOOL 1
  2501. #else
  2502. #define COMMON_SERVICES_NEEDS_BOOL 0
  2503. #endif
  2504. #endif
  2505. #if( COMMON_SERVICES_NEEDS_BOOL )
  2506. // typedef int bool;
  2507. #define bool bool
  2508. #if( !defined( true ) )
  2509. #define true 1
  2510. #endif
  2511. #if( !defined( false ) )
  2512. #define false 0
  2513. #endif
  2514. #define __bool_true_false_are_defined 1
  2515. #endif
  2516. // IOKit IOTypes.h typedef's bool if TYPE_BOOL is not defined so define it here to prevent redefinition by IOTypes.h.
  2517. //---------------------------------------------------------------------------------------------------------------------------
  2518. /*! @typedef Boolean
  2519. @abstract Mac-style Boolean type. Emulated on non-Mac platforms.
  2520. */
  2521. // MacTypes.h (Carbon) and OSTypes.h (IOKit) typedef Boolean so only typedef if those haven't been included.
  2522. // Others use __BOOLEAN_DEFINED__ when they typedef Boolean so check for that and define it if we typedef it.
  2523. #if( !defined( __MACTYPES__ ) && !defined( _OS_OSTYPES_H ) && !defined( __BOOLEAN_DEFINED__ ) )
  2524. typedef uint8_t Boolean;
  2525. #define __BOOLEAN_DEFINED__ 1
  2526. #endif
  2527. //---------------------------------------------------------------------------------------------------------------------------
  2528. /*! @defined TYPE_LONGLONG_NATIVE
  2529. @abstract Defines whether long long (or its equivalent) is natively supported or requires special libraries.
  2530. */
  2531. #if( !defined( TYPE_LONGLONG_NATIVE ) )
  2532. #if( !defined( __GNUC__ ) || ( ( __GNUC__ > 2 ) || ( ( __GNUC__ == 2 ) && ( __GNUC_MINOR__ >= 9 ) ) ) || defined( __ghs__ ) )
  2533. #define TYPE_LONGLONG_NATIVE 1
  2534. #else
  2535. #define TYPE_LONGLONG_NATIVE 0
  2536. #endif
  2537. #endif
  2538. #if 0
  2539. #pragma mark -
  2540. #pragma mark == Errors ==
  2541. #endif
  2542. //---------------------------------------------------------------------------------------------------------------------------
  2543. /*! @group OSStatus
  2544. @abstract Status Code
  2545. */
  2546. typedef int32_t OSStatus;
  2547. #define OSSTATUS_DEFINED 1
  2548. #define kNoErr 0 //! No error occurred.
  2549. #define kInProgressErr 1 //! Operation in progress.
  2550. // Generic error codes are in the range -6700 to -6779.
  2551. #define kGenericErrorBase -6700 //! Starting error code for all generic errors.
  2552. #define kUnknownErr -6700 //! Unknown error occurred.
  2553. #define kOptionErr -6701 //! Option was not acceptable.
  2554. #define kSelectorErr -6702 //! Selector passed in is invalid or unknown.
  2555. #define kExecutionStateErr -6703 //! Call made in the wrong execution state (e.g. called at interrupt time).
  2556. #define kPathErr -6704 //! Path is invalid, too long, or otherwise not usable.
  2557. #define kParamErr -6705 //! Parameter is incorrect, missing, or not appropriate.
  2558. #define kUserRequiredErr -6706 //! User interaction is required.
  2559. #define kCommandErr -6707 //! Command invalid or not supported.
  2560. #define kIDErr -6708 //! Unknown, invalid, or inappropriate identifier.
  2561. #define kStateErr -6709 //! Not in appropriate state to perform operation.
  2562. #define kRangeErr -6710 //! Index is out of range or not valid.
  2563. #define kRequestErr -6711 //! Request was improperly formed or not appropriate.
  2564. #define kResponseErr -6712 //! Response was incorrect or out of sequence.
  2565. #define kChecksumErr -6713 //! Checksum does not match the actual data.
  2566. #define kNotHandledErr -6714 //! Operation was not handled (or not handled completely).
  2567. #define kVersionErr -6715 //! Version is not correct or not compatible.
  2568. #define kSignatureErr -6716 //! Signature did not match what was expected.
  2569. #define kFormatErr -6717 //! Unknown, invalid, or inappropriate file/data format.
  2570. #define kNotInitializedErr -6718 //! Action request before needed services were initialized.
  2571. #define kAlreadyInitializedErr -6719 //! Attempt made to initialize when already initialized.
  2572. #define kNotInUseErr -6720 //! Object not in use (e.g. cannot abort if not already in use).
  2573. #define kAlreadyInUseErr -6721 //! Object is in use (e.g. cannot reuse active param blocks).
  2574. #define kTimeoutErr -6722 //! Timeout occurred.
  2575. #define kCanceledErr -6723 //! Operation canceled (successful cancel).
  2576. #define kAlreadyCanceledErr -6724 //! Operation has already been canceled.
  2577. #define kCannotCancelErr -6725 //! Operation could not be canceled (maybe already done or invalid).
  2578. #define kDeletedErr -6726 //! Object has already been deleted.
  2579. #define kNotFoundErr -6727 //! Something was not found.
  2580. #define kNoMemoryErr -6728 //! Not enough memory was available to perform the operation.
  2581. #define kNoResourcesErr -6729 //! Resources unavailable to perform the operation.
  2582. #define kDuplicateErr -6730 //! Duplicate found or something is a duplicate.
  2583. #define kImmutableErr -6731 //! Entity is not changeable.
  2584. #define kUnsupportedDataErr -6732 //! Data is unknown or not supported.
  2585. #define kIntegrityErr -6733 //! Data is corrupt.
  2586. #define kIncompatibleErr -6734 //! Data is not compatible or it is in an incompatible format.
  2587. #define kUnsupportedErr -6735 //! Feature or option is not supported.
  2588. #define kUnexpectedErr -6736 //! Error occurred that was not expected.
  2589. #define kValueErr -6737 //! Value is not appropriate.
  2590. #define kNotReadableErr -6738 //! Could not read or reading is not allowed.
  2591. #define kNotWritableErr -6739 //! Could not write or writing is not allowed.
  2592. #define kBadReferenceErr -6740 //! An invalid or inappropriate reference was specified.
  2593. #define kFlagErr -6741 //! An invalid, inappropriate, or unsupported flag was specified.
  2594. #define kMalformedErr -6742 //! Something was not formed correctly.
  2595. #define kSizeErr -6743 //! Size was too big, too small, or not appropriate.
  2596. #define kNameErr -6744 //! Name was not correct, allowed, or appropriate.
  2597. #define kNotPreparedErr -6745 //! Device or service is not ready.
  2598. #define kReadErr -6746 //! Could not read.
  2599. #define kWriteErr -6747 //! Could not write.
  2600. #define kMismatchErr -6748 //! Something does not match.
  2601. #define kDateErr -6749 //! Date is invalid or out-of-range.
  2602. #define kUnderrunErr -6750 //! Less data than expected.
  2603. #define kOverrunErr -6751 //! More data than expected.
  2604. #define kEndingErr -6752 //! Connection, session, or something is ending.
  2605. #define kConnectionErr -6753 //! Connection failed or could not be established.
  2606. #define kAuthenticationErr -6754 //! Authentication failed or is not supported.
  2607. #define kOpenErr -6755 //! Could not open file, pipe, device, etc.
  2608. #define kTypeErr -6756 //! Incorrect or incompatible type (e.g. file, data, etc.).
  2609. #define kSkipErr -6757 //! Items should be or was skipped.
  2610. #define kNoAckErr -6758 //! No acknowledge.
  2611. #define kCollisionErr -6759 //! Collision occurred (e.g. two on bus at same time).
  2612. #define kBackoffErr -6760 //! Backoff in progress and operation intentionally failed.
  2613. #define kAddressErr -6761 //! Bad address or no acknowledge of address.
  2614. #define kInternalErr -6762 //! An error internal to the implementation occurred.
  2615. #define kNoSpaceErr -6763 //! Not enough space to perform operation.
  2616. #define kCountErr -6764 //! Count is incorrect.
  2617. #define kEndOfDataErr -6765 //! Reached the end of the data (e.g. recv returned 0).
  2618. #define kWouldBlockErr -6766 //! Would need to block to continue (e.g. non-blocking read/write).
  2619. #define kLookErr -6767 //! Special case that needs to be looked at (e.g. interleaved data).
  2620. #define kSecurityRequiredErr -6768 //! Security is required for the operation (e.g. must use encryption).
  2621. #define kOrderErr -6769 //! Order is incorrect.
  2622. #define kUpgradeErr -6770 //! Must upgrade.
  2623. #define kAsyncNoErr -6771 //! Async operation successfully started and is now in progress.
  2624. #define kDeprecatedErr -6772 //! Operation or data is deprecated.
  2625. #define kPermissionErr -6773 //! Permission denied.
  2626. #define kGenericErrorEnd -6779 //! Last generic error code (inclusive)
  2627. // NSErrorCreateWithOSStatus -- Creates an NSError object from an OSStatus, following the convention of nil == noErr.
  2628. #define NSErrorCreateWithOSStatus( ERR ) \
  2629. ( (ERR) ? [[NSError alloc] initWithDomain:NSOSStatusErrorDomain code:(ERR) userInfo:nil] : nil )
  2630. #if 0
  2631. #pragma mark -
  2632. #pragma mark == Misc ==
  2633. #endif
  2634. //===========================================================================================================================
  2635. // Misc
  2636. //===========================================================================================================================
  2637. // Seconds <-> Minutes <-> Hours <-> Days <-> Weeks <-> Months <-> Years conversions
  2638. #define kAttosecondsPerSecond 1000000000000000000 // 1e-18 seconds.
  2639. #define kFemtosecondsPerSecond 1000000000000000 // 1e-15 seconds.
  2640. #define kPicosecondsPerSecond 1000000000000 // 1e-12 seconds.
  2641. #define kNanosecondsPerMicrosecond 1000
  2642. #define kNanosecondsPerMillisecond 1000000
  2643. #define kNanosecondsPerSecond 1000000000 // 1e-9 seconds.
  2644. #define kMicrosecondsPerSecond 1000000 // 1e-6 seconds.
  2645. #define kMicrosecondsPerMillisecond 1000
  2646. #define kMillisecondsPerSecond 1000
  2647. #define kSecondsPerMinute 60
  2648. #define kSecondsPerHour ( 60 * 60 ) // 3600
  2649. #define kSecondsPerDay ( 60 * 60 * 24 ) // 86400
  2650. #define kSecondsPerWeek ( 60 * 60 * 24 * 7 ) // 604800
  2651. #define kSecondsPerMonth ( 60 * 60 * 24 * 30 ) // 2592000
  2652. #define kSecondsPerYear ( 60 * 60 * 24 * 365 ) // 31536000
  2653. #define kMinutesPerHour 60
  2654. #define kMinutesPerDay ( 60 * 24 ) // 1440
  2655. #define kHoursPerDay 24
  2656. #define kDaysPerWeek 7
  2657. #define kWeeksPerYear 52
  2658. #define kMonthsPerYear 12
  2659. #define IsLeapYear( YEAR ) ( !( ( YEAR ) % 4 ) && ( ( ( YEAR ) % 100 ) || !( ( YEAR ) % 400 ) ) )
  2660. #define YearToDays( YEAR ) ( ( (YEAR) * 365 ) + ( (YEAR) / 4 ) - ( (YEAR) / 100 ) + ( (YEAR) / 400 ) )
  2661. #define MonthToDays( MONTH ) ( ( ( (MONTH) * 3057 ) - 3007 ) / 100 )
  2662. #define dispatch_time_milliseconds( MS ) dispatch_time( DISPATCH_TIME_NOW, (MS) * UINT64_C_safe( kNanosecondsPerMillisecond ) )
  2663. #define dispatch_time_seconds( SECS ) dispatch_time( DISPATCH_TIME_NOW, (SECS) * UINT64_C_safe( kNanosecondsPerSecond ) )
  2664. // Bytes
  2665. #define kBytesPerTeraByte 1099511627776
  2666. #define kBytesPerGigaByte 1073741824
  2667. #define kBytesPerMegaByte 1048576
  2668. #define kBytesPerKiloByte 1024
  2669. //---------------------------------------------------------------------------------------------------------------------------
  2670. /*! @group NumVersion
  2671. @abstract Mac-style version numbers represented by 32-bit numbers (e.g. 1.2.3b4 -> 0x01236004).
  2672. */
  2673. #define kVersionStageDevelopment 0x20 //! Development version.
  2674. #define kVersionStageAlpha 0x40 //! Alpha version (feature complete, possibly crashing bugs).
  2675. #define kVersionStageBeta 0x60 //! Beta version (feature complete, no crashing bugs).
  2676. #define kVersionStageFinal 0x80 //! Final version (f0 means GM).
  2677. //---------------------------------------------------------------------------------------------------------------------------
  2678. /*! @defined NumVersionBuild
  2679. @abstract Builds a 32-bit Mac-style NumVersion value (e.g. NumVersionBuild( 1, 2, 3, kVersionStageBeta, 4 ) -> 1.2.3b4).
  2680. */
  2681. #define NumVersionBuild( MAJOR, MINOR, BUGFIX, STAGE, REV ) \
  2682. ( ( ( ( MAJOR ) & 0xFF ) << 24 ) | \
  2683. ( ( ( MINOR ) & 0x0F ) << 20 ) | \
  2684. ( ( ( BUGFIX ) & 0x0F ) << 16 ) | \
  2685. ( ( ( STAGE ) & 0xFF ) << 8 ) | \
  2686. ( ( ( REV ) & 0xFF ) ) )
  2687. #define NumVersionExtractMajor( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 24 ) & 0xFF ) )
  2688. #define NumVersionExtractMinorAndBugFix( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 16 ) & 0xFF ) )
  2689. #define NumVersionExtractMinor( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 20 ) & 0x0F ) )
  2690. #define NumVersionExtractBugFix( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 16 ) & 0x0F ) )
  2691. #define NumVersionExtractStage( VERSION ) ( (uint8_t)( ( ( VERSION ) >> 8 ) & 0xFF ) )
  2692. #define NumVersionExtractRevision( VERSION ) ( (uint8_t)( ( VERSION ) & 0xFF ) )
  2693. //---------------------------------------------------------------------------------------------------------------------------
  2694. /*! @defined NumVersionCompare
  2695. @abstract Compares two NumVersion values and returns < 0 if A < B, 0 if A == B, or > 0 if A > B.
  2696. */
  2697. #define NumVersionCompare( A, B ) \
  2698. ( ( ( (A) & 0xFFFFFF00U ) < ( (B) & 0xFFFFFF00U ) ) ? -1 : \
  2699. ( ( (A) & 0xFFFFFF00U ) > ( (B) & 0xFFFFFF00U ) ) ? 1 : \
  2700. ( ( ( (A) - 1 ) & 0xFF ) < ( ( (B) - 1 ) & 0xFF ) ) ? -1 : \
  2701. ( ( ( (A) - 1 ) & 0xFF ) > ( ( (B) - 1 ) & 0xFF ) ) ? 1 : 0 )
  2702. //---------------------------------------------------------------------------------------------------------------------------
  2703. /*! @defined SourceVersionToInteger
  2704. @abstract Converts source version components to an integer.
  2705. @discussion For example, source version 110.35 would be SourceVersionToInteger( 110, 35, 0 ) which is 1103500.
  2706. */
  2707. #define SourceVersionToInteger( X, Y, Z ) ( ( 10000 * (X) ) + ( 100 * (Y) ) + (Z) )
  2708. //---------------------------------------------------------------------------------------------------------------------------
  2709. /*! @group CharSets
  2710. @abstract Useful character sets.
  2711. */
  2712. #define kBinaryDigits "01"
  2713. #define kDecimalDigits "0123456789"
  2714. #define kHexDigitsUppercase "0123456789ABCDEF"
  2715. #define kHexDigitsLowercase "0123456789abcdef"
  2716. #define kOctalDigits "01234567"
  2717. #define kAlphaCharSet "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  2718. #define kAlphaNumericCharSet "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  2719. #define kUnmistakableCharSet "ACDEFGHJKMNPQRSTUVWXYZ2345679" // Avoids easily mistaken characters: 0/o, 1/l/i, B/8
  2720. // AFP Volume names -- (0x20-0x7E, except ':').
  2721. #define kAFPVolumeNameCharSet \
  2722. " !\"#$%&'()*+,-./" \
  2723. "0123456789" \
  2724. ";<=>?@" \
  2725. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  2726. "[\\]^_`" \
  2727. "abcdefghijklmnopqrstuvwxyz" \
  2728. "{|}~"
  2729. // ASCII
  2730. #define kASCII7BitCharSet \
  2731. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" \
  2732. "\x00\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" \
  2733. " !\"#$%&'()*+,-./" \
  2734. "0123456789" \
  2735. ":;<=>?@" \
  2736. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  2737. "[\\]^_`" \
  2738. "abcdefghijklmnopqrstuvwxyz" \
  2739. "{|}~\x7F"
  2740. #define kASCIIPrintableCharSet \
  2741. "\t\n\x0B\x0C\r" \
  2742. " !\"#$%&'()*+,-./" \
  2743. "0123456789" \
  2744. ":;<=>?@" \
  2745. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  2746. "[\\]^_`" \
  2747. "abcdefghijklmnopqrstuvwxyz" \
  2748. "{|}~"
  2749. // Bonjour SubTypes -- restrict to lowercase letters, digits, '_', and '-'.
  2750. #define kBonjourSubTypeCharSet "abcdefghijklmnopqrstuvwxyz0123456789_-"
  2751. // DNS names -- RFC 1034 says DNS names must consist of only letters, digits, dots, and hyphens.
  2752. #define kDNSCharSet "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-"
  2753. // TXT record keys -- Printable ASCII (0x20-0x7E, except 0x3D '=').
  2754. #define kTXTKeyCharSet \
  2755. " !\"#$%&'()*+,-./" \
  2756. "0123456789" \
  2757. ":;<>?@" \
  2758. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  2759. "[\\]^_`" \
  2760. "abcdefghijklmnopqrstuvwxyz" \
  2761. "{|}~"
  2762. //---------------------------------------------------------------------------------------------------------------------------
  2763. /*! @group Hex Char Testing and Conversions
  2764. @abstract Macros for testing for hex chars and converting them to values and/or bytes.
  2765. */
  2766. // Hex Char Testing and Conversions.
  2767. #define HexCharToValue( X ) \
  2768. ( ( ( (X) >= '0' ) && ( (X) <= '9' ) ) ? ( (X) - '0' ) : \
  2769. ( ( (X) >= 'A' ) && ( (X) <= 'F' ) ) ? ( 10 + ( (X) - 'A' ) ) : \
  2770. ( ( (X) >= 'a' ) && ( (X) <= 'f' ) ) ? ( 10 + ( (X) - 'a' ) ) : 0 )
  2771. #define IsHexPair( PTR ) \
  2772. ( isxdigit_safe( ( (const unsigned char *)(PTR) )[ 0 ] ) && \
  2773. isxdigit_safe( ( (const unsigned char *)(PTR) )[ 1 ] ) )
  2774. #define HexPairToByte( PTR ) ( (uint8_t)( \
  2775. ( HexCharToValue( ( (const unsigned char *)(PTR) )[ 0 ] ) << 4 ) | \
  2776. HexCharToValue( ( (const unsigned char *)(PTR) )[ 1 ] ) ) )
  2777. //---------------------------------------------------------------------------------------------------------------------------
  2778. /*! @group Octal Char Testing and Conversions
  2779. @abstract Macros for testing for octal chars and converting them to values and/or bytes.
  2780. */
  2781. #define isoctal( X ) ( ( (X) >= '0' ) && ( (X) <= '7' ) )
  2782. #define OctalCharToValue( X ) ( ( ( (X) >= '0' ) && ( (X) <= '7' ) ) ? ( (X) - '0' ) : 0 )
  2783. #define IsOctalTriple( PTR ) \
  2784. ( ( ( ( (const unsigned char *)(PTR) )[ 0 ] >= '0' ) && \
  2785. ( ( (const unsigned char *)(PTR) )[ 0 ] <= '3' ) ) && \
  2786. isoctal_safe( ( (const unsigned char *)(PTR) )[ 1 ] ) && \
  2787. isoctal_safe( ( (const unsigned char *)(PTR) )[ 2 ] ) )
  2788. #define OctalTripleToByte( PTR ) ( (uint8_t)( \
  2789. ( OctalCharToValue( ( (const unsigned char *)(PTR) )[ 0 ] ) * 64 ) | \
  2790. ( OctalCharToValue( ( (const unsigned char *)(PTR) )[ 1 ] ) * 8 ) | \
  2791. OctalCharToValue( ( (const unsigned char *)(PTR) )[ 2 ] ) ) )
  2792. //---------------------------------------------------------------------------------------------------------------------------
  2793. /*! @function BCDByteToDecimal/DecimalByteToBCD
  2794. @abstract Converts a byte to/from BSD (e.g. 0x50 -> 50).
  2795. */
  2796. #define BCDByteToDecimal( X ) ( ( ( ( (X) >> 4 ) & 0x0F ) * 10 ) + ( (X) & 0x0F ) )
  2797. #define DecimalByteToBCD( X ) ( ( ( (X) / 10 ) << 4 ) | ( (X) % 10 ) )
  2798. //---------------------------------------------------------------------------------------------------------------------------
  2799. /*! @group dB <-> linear Conversions
  2800. @discussion Macros to convert between dB attentuation values and linear volume levels.
  2801. dB = 20log10( linear )
  2802. linear = pow( 10, dB / 20 )
  2803. See <http://en.wikipedia.org/wiki/Decibel> for details on the math behind this.
  2804. */
  2805. #define DBtoLinear( DB ) ( ( (DB) <= -144.0f ) ? 0.0f : ( (DB) >= 0.0f ) ? 1.0f : powf( 10, (DB) / 20 ) )
  2806. #define LinearToDB( LINEAR ) ( ( (LINEAR) <= 0.0f ) ? -144.0f : ( (LINEAR) >= 1.0f ) ? 0.0f : ( 20 * log10f( (LINEAR) ) ) )
  2807. //---------------------------------------------------------------------------------------------------------------------------
  2808. /*! @group Q-format fixed-point number conversions
  2809. @abstract Macros to convert between floating point and fixed-point Q-format numbers.
  2810. @discussion See <http://en.wikipedia.org/wiki/Q_(number_format)> for details.
  2811. N is number of fractional bits to use.
  2812. */
  2813. #define FloatToQ( X, N ) ( (int)( (X) * ( 1 << (N) ) ) )
  2814. #define QToFloat( X, N ) ( ( (float)(X) ) / ( (float)( 1 << (N) ) ) )
  2815. //---------------------------------------------------------------------------------------------------------------------------
  2816. /*! @typedef dispatch_status_block_t
  2817. @abstract Block type for a commonly used block with a status parameter.
  2818. */
  2819. //---------------------------------------------------------------------------------------------------------------------------
  2820. /*! @function memcmp_constant_time
  2821. @abstract Compares memory so that the time it takes does not depend on the data being compared.
  2822. @discussion This is needed to avoid certain timing attacks in cryptographic software.
  2823. */
  2824. STATIC_INLINE int memcmp_constant_time( const void *inA, const void *inB, size_t inLen )
  2825. {
  2826. const uint8_t * const a = (const uint8_t *) inA;
  2827. const uint8_t * const b = (const uint8_t *) inB;
  2828. int result = 0;
  2829. size_t i;
  2830. for( i = 0; i < inLen; ++i )
  2831. {
  2832. result |= ( a[ i ] ^ b[ i ] );
  2833. }
  2834. return( result );
  2835. }
  2836. //---------------------------------------------------------------------------------------------------------------------------
  2837. /*! @function MemZeroSecure
  2838. @abstract Zeros memory in a way that prevents the compiler from optimizing it out (as it could with memset).
  2839. @discussion This is needed for cases such as clearing a buffer containing a cryptographic key.
  2840. */
  2841. STATIC_INLINE void MemZeroSecure( void *inPtr, size_t inLen )
  2842. {
  2843. volatile unsigned char * ptr = (volatile unsigned char *) inPtr;
  2844. while( inLen-- ) *ptr++ = 0;
  2845. }
  2846. #if 0
  2847. #pragma mark == Time96 ==
  2848. #endif
  2849. //===========================================================================================================================
  2850. // Time96
  2851. //
  2852. // Support for 96-bit (32.64) binary time.
  2853. //===========================================================================================================================
  2854. typedef struct
  2855. {
  2856. int32_t secs; //! Number of seconds. Epoch depends on usage. 0, 1970-01-01 00:00:00 (Unix time), etc.
  2857. uint64_t frac; //! Fraction of a second in units of 1/2^64.
  2858. } Time96;
  2859. //---------------------------------------------------------------------------------------------------------------------------
  2860. /*! @function Time96ToDouble / DoubleToTime96
  2861. @abstract Convert between Time96 and floating-point seconds values.
  2862. */
  2863. #define Time96ToDouble( T96 ) ( ( (double) (T96)->secs ) + ( ( (double) (T96)->frac ) * ( 1.0 / 18446744073709551615.0 ) ) )
  2864. #define DoubleToTime96( D, T96 ) \
  2865. do \
  2866. { \
  2867. double _DoubleToTime96_secs; \
  2868. \
  2869. _DoubleToTime96_secs = floor( (D) ); \
  2870. (T96)->secs = (int32_t) _DoubleToTime96_secs; \
  2871. (T96)->frac = (uint64_t)( ( (D) - _DoubleToTime96_secs ) * 18446744073709551615.0 ); \
  2872. \
  2873. } while( 0 )
  2874. //---------------------------------------------------------------------------------------------------------------------------
  2875. /*! @function Time96ToNTP / NTPToTime96
  2876. @abstract Convert between Time96 and NTP 32.32 values.
  2877. */
  2878. #define Time96ToNTP( T96 ) ( ( ( (uint64_t) (T96)->secs ) << 32 ) | ( (T96)->frac >> 32 ) )
  2879. #define NTPToTime96( NTP, T96 ) \
  2880. do \
  2881. { \
  2882. (T96)->secs = (int32_t)( (NTP) >> 32 ); \
  2883. (T96)->frac = ( (NTP) << 32 ); \
  2884. \
  2885. } while( 0 )
  2886. //---------------------------------------------------------------------------------------------------------------------------
  2887. /*! @function Time96ToNTP / NTPToTime96
  2888. @abstract Convert between Time96 and NTP 32.32 values.
  2889. */
  2890. #define Time96ToNTP( T96 ) ( ( ( (uint64_t) (T96)->secs ) << 32 ) | ( (T96)->frac >> 32 ) )
  2891. #define NTPToTime96( NTP, T96 ) \
  2892. do \
  2893. { \
  2894. (T96)->secs = (int32_t)( (NTP) >> 32 ); \
  2895. (T96)->frac = ( (NTP) << 32 ); \
  2896. \
  2897. } while( 0 )
  2898. //---------------------------------------------------------------------------------------------------------------------------
  2899. /*! @function Time96FracToNanoseconds / NanosecondsToTime96Frac
  2900. @abstract Convert between Time96 fractional seconds and nanoseconds.
  2901. */
  2902. #define Time96FracToNanoseconds( FRAC ) ( ( UINT64_C( 1000000000 ) * (uint32_t)( (FRAC) >> 32 ) ) >> 32 )
  2903. #define NanosecondsToTime96Frac( NS ) ( (NS) * UINT64_C( 18446744073 ) ) // 2^64 / 1000000000 = 18446744073
  2904. //---------------------------------------------------------------------------------------------------------------------------
  2905. /*! @function Time96_AddFrac
  2906. @abstract Adds a fractional seconds (1/2^64 units) value to a time.
  2907. */
  2908. STATIC_INLINE void Time96_AddFrac( Time96 *inTime, uint64_t inFrac )
  2909. {
  2910. uint64_t frac;
  2911. frac = inTime->frac;
  2912. inTime->frac = frac + inFrac;
  2913. if( frac > inTime->frac ) inTime->secs += 1; // Increment seconds on fraction wrap.
  2914. }
  2915. //---------------------------------------------------------------------------------------------------------------------------
  2916. /*! @function Time96_Add
  2917. @abstract Adds one time to another time.
  2918. */
  2919. STATIC_INLINE void Time96_Add( Time96 *inTime, const Time96 *inAdd )
  2920. {
  2921. uint64_t frac;
  2922. frac = inTime->frac;
  2923. inTime->frac = frac + inAdd->frac;
  2924. if( frac > inTime->frac ) inTime->secs += 1; // Increment seconds on fraction wrap.
  2925. inTime->secs += inAdd->secs;
  2926. }
  2927. //---------------------------------------------------------------------------------------------------------------------------
  2928. /*! @function Time96_Sub
  2929. @abstract Subtracts one time from another time.
  2930. */
  2931. STATIC_INLINE void Time96_Sub( Time96 *inTime, const Time96 *inSub )
  2932. {
  2933. uint64_t frac;
  2934. frac = inTime->frac;
  2935. inTime->frac = frac - inSub->frac;
  2936. if( frac < inTime->frac ) inTime->secs -= 1; // Decrement seconds on fraction wrap.
  2937. inTime->secs -= inSub->secs;
  2938. }
  2939. #if 0
  2940. #pragma mark -
  2941. #pragma mark == timeval macros ==
  2942. #endif
  2943. //===========================================================================================================================
  2944. // timeval macros
  2945. //===========================================================================================================================
  2946. #define TIMEVAL_USECS_PER_SEC 1000000
  2947. // A == B
  2948. #define TIMEVAL_EQ( A, B ) ( ( (A).tv_sec == (B).tv_sec ) && ( (A).tv_usec == (B).tv_usec ) )
  2949. // A < B
  2950. #define TIMEVAL_LT( A, B ) ( ( (A).tv_sec < (B).tv_sec ) || \
  2951. ( ( (A).tv_sec == (B).tv_sec ) && ( (A).tv_usec < (B).tv_usec ) ) )
  2952. // A <= B
  2953. #define TIMEVAL_LE( A, B ) ( ( (A).tv_sec < (B).tv_sec ) || \
  2954. ( ( (A).tv_sec == (B).tv_sec ) && ( (A).tv_usec <= (B).tv_usec ) ) )
  2955. // A > B
  2956. #define TIMEVAL_GT( A, B ) ( ( (A).tv_sec > (B).tv_sec ) || \
  2957. ( ( (A).tv_sec == (B).tv_sec ) && ( (A).tv_usec > (B).tv_usec ) ) )
  2958. // A >= B
  2959. #define TIMEVAL_GE( A, B ) ( ( (A).tv_sec > (B).tv_sec ) || \
  2960. ( ( (A).tv_sec == (B).tv_sec ) && ( (A).tv_usec >= (B).tv_usec ) ) )
  2961. // A < B = -1
  2962. // A > B = 1
  2963. // A == B = 0
  2964. #define TIMEVAL_CMP( A, B ) \
  2965. ( (A).tv_sec < (B).tv_sec ) ? -1 : \
  2966. ( (A).tv_sec > (B).tv_sec ) ? 1 : \
  2967. ( (A).tv_usec < (B).tv_usec ) ? -1 : \
  2968. ( (A).tv_usec > (B).tv_usec ) ? 1 : 0
  2969. // Non-zero if tv_usec is between 0 and (1000000 - 1).
  2970. #define TIMEVAL_VALID( X ) ( ( (X).tv_usec >= 0 ) && ( (X).tv_usec < TIMEVAL_USECS_PER_SEC ) )
  2971. // Sets X to 0 seconds and 0 microseconds.
  2972. #define TIMEVAL_ZERO( X ) TIMEVAL_SET( X, 0, 0 )
  2973. // Sets X from secs and microseconds.
  2974. #define TIMEVAL_SET( X, SECS, USECS ) \
  2975. do \
  2976. { \
  2977. (X).tv_sec = ( SECS ); \
  2978. (X).tv_usec = ( USECS ); \
  2979. \
  2980. } while( 0 )
  2981. // A += B
  2982. #define TIMEVAL_ADD( A, B ) \
  2983. do \
  2984. { \
  2985. (A).tv_sec += (B).tv_sec; \
  2986. (A).tv_usec += (B).tv_usec; \
  2987. TIMEVAL_NORMALIZE( A ); \
  2988. \
  2989. } while( 0 )
  2990. // A += X. X is the number of microseconds to add.
  2991. #define TIMEVAL_ADD_USEC( A, X ) \
  2992. do \
  2993. { \
  2994. (A).tv_usec += (X); \
  2995. TIMEVAL_NORMALIZE( A ); \
  2996. \
  2997. } while( 0 )
  2998. // X = A + B
  2999. #define TIMEVAL_ADD_COPY( X, A, B ) \
  3000. do \
  3001. { \
  3002. (X) = (A); \
  3003. (X).tv_sec += (B).tv_sec; \
  3004. (X).tv_usec += (B).tv_usec; \
  3005. TIMEVAL_NORMALIZE( X ); \
  3006. \
  3007. } while( 0 )
  3008. // A -= B
  3009. #define TIMEVAL_SUB( A, B ) \
  3010. do \
  3011. { \
  3012. if( TIMEVAL_GT( A, B ) ) \
  3013. { \
  3014. (A).tv_sec -= (B).tv_sec; \
  3015. (A).tv_usec -= (B).tv_usec; \
  3016. TIMEVAL_NORMALIZE( A ); \
  3017. } \
  3018. else \
  3019. { \
  3020. (A).tv_sec = 0; \
  3021. (A).tv_usec = 0; \
  3022. } \
  3023. \
  3024. } while( 0 )
  3025. // X = A - B
  3026. #define TIMEVAL_SUB_COPY( X, A, B ) \
  3027. do \
  3028. { \
  3029. (X) = (A); \
  3030. TIMEVAL_SUB( (X), (B) ); \
  3031. \
  3032. } while( 0 )
  3033. // A *= X. X must be a positive integer. X must be <= 2147 to avoid overflow.
  3034. #define TIMEVAL_MUL( A, X ) \
  3035. do \
  3036. { \
  3037. (A).tv_sec *= (X); \
  3038. (A).tv_usec *= (X); \
  3039. TIMEVAL_NORMALIZE( A ); \
  3040. \
  3041. } while( 0 )
  3042. // X = A * Y. Y must be a positive integer. Y must be <= 2147 to avoid overflow.
  3043. #define TIMEVAL_MUL_COPY( X, A, Y ) \
  3044. do \
  3045. { \
  3046. (X) = (A); \
  3047. (X).tv_sec *= (Y); \
  3048. (X).tv_usec *= (Y); \
  3049. TIMEVAL_NORMALIZE( X ); \
  3050. \
  3051. } while( 0 )
  3052. // Adjusts tv_sec and tv_usec so tv_usec is between 0 and (1000000 - 1).
  3053. #define TIMEVAL_NORMALIZE( X ) \
  3054. do \
  3055. { \
  3056. for( ;; ) \
  3057. { \
  3058. if( (X).tv_usec >= TIMEVAL_USECS_PER_SEC ) \
  3059. { \
  3060. (X).tv_sec += 1; \
  3061. (X).tv_usec -= TIMEVAL_USECS_PER_SEC; \
  3062. } \
  3063. else if( (X).tv_usec < 0 ) \
  3064. { \
  3065. (X).tv_sec -= 1; \
  3066. (X).tv_usec += TIMEVAL_USECS_PER_SEC; \
  3067. } \
  3068. else \
  3069. { \
  3070. break; \
  3071. } \
  3072. } \
  3073. \
  3074. } while( 0 )
  3075. // X as single, unsigned 32-bit microseconds value. X must be <= 4294 to avoid overflow.
  3076. #define TIMEVAL_USEC32( X ) ( ( ( (uint32_t)(X).tv_sec ) * TIMEVAL_USECS_PER_SEC ) + (X).tv_usec )
  3077. // X as single, signed 64-bit microseconds value.
  3078. #define TIMEVAL_USEC64( X ) ( ( ( (int64_t)(X).tv_sec ) * TIMEVAL_USECS_PER_SEC ) + (X).tv_usec )
  3079. // A - B as single, signed 64-bit microseconds value (negative if A < B).
  3080. #define TIMEVAL_USEC64_DIFF( A, B ) ( TIMEVAL_USEC64( A ) - TIMEVAL_USEC64( B ) )
  3081. // X as a single, floating point seconds value.
  3082. #define TIMEVAL_FP_SECS( X ) ( ( (double)(X).tv_sec ) + ( ( (double)(X).tv_usec ) * ( 1.0 / 1000000.0 ) ) )
  3083. #if 0
  3084. #pragma mark == ANSI Escape Sequences ==
  3085. #endif
  3086. //---------------------------------------------------------------------------------------------------------------------------
  3087. /*! @group ANSI Escape Sequences
  3088. @abstract Escape sequences for use with printf/etc to terminal windows.
  3089. @discussion
  3090. These are intended to be used either directly in a string or via %s in printf. For example:
  3091. // Print "hello" in bold then reset it back to normal.
  3092. printf( kANSIBold "hello" kANSINormal "\n" );
  3093. // Print "hello" in bold then reset it back to normal.
  3094. printf( "%s%s%s\n", kANSIBold, "hello", kANSINormal );
  3095. */
  3096. #define kANSINormal "\x1B[0m" // all attributes off
  3097. #define kANSINormalIntensity "\x1B[22m" // not bold and not faint.
  3098. #define kANSIBold "\x1B[1m"
  3099. #define kANSIFaint "\x1B[2m" // not widely suppport.
  3100. #define kANSIItalic "\x1B[3m" // not widely suppport. Sometimes treated as inverse.
  3101. #define kANSIUnderline "\x1B[4m" // not widely suppport.
  3102. #define kANSIUnderlineDouble "\x1B[21m" // not widely suppport.
  3103. #define kANSIUnderlineOff "\x1B[24m"
  3104. #define kANSIBlink "\x1B[5m" // less than 150 per minute.
  3105. #define kANSIBlinkRapid "\x1B[6m" // 150 per minute or more.
  3106. #define kANSIBlinkOff "\x1B[25m" // not widely suppport.
  3107. #define kANSINegative "\x1B[7m" // inverse/reverse; swap foreground and background.
  3108. #define kANSIPositive "\x1B[27m" // inverse of negative.
  3109. #define kANSIConceal "\x1B[8m" // not widely supported.
  3110. #define kANSIReveal "\x1B[28m" // conceal off.
  3111. // Foreground colors
  3112. #define kANSIBlack "\x1B[30m"
  3113. #define kANSIGray "\x1B[0;37m"
  3114. #define kANSIRed "\x1B[31m"
  3115. #define kANSIGreen "\x1B[32m"
  3116. #define kANSIYellow "\x1B[33m"
  3117. #define kANSIBlue "\x1B[34m"
  3118. #define kANSIMagenta "\x1B[35m"
  3119. #define kANSICyan "\x1B[36m"
  3120. #define kANSIWhite "\x1B[37m"
  3121. #define kANSIForeReset "\x1B[39m"
  3122. // Background colors
  3123. #define kANSIBackBlack "\x1B[40m"
  3124. #define kANSIBackRed "\x1B[41m"
  3125. #define kANSIBackGreen "\x1B[42m"
  3126. #define kANSIBackYellow "\x1B[43m"
  3127. #define kANSIBackBlue "\x1B[44m"
  3128. #define kANSIBackMagenta "\x1B[45m"
  3129. #define kANSIBackCyan "\x1B[46m"
  3130. #define kANSIBackWhite "\x1B[47m"
  3131. #define kANSIBackReset "\x1B[49m"
  3132. // High Intensity Foreground colors
  3133. #define kANSIHighBlack "\x1B[90m"
  3134. #define kANSIHighRed "\x1B[91m"
  3135. #define kANSIHighGreen "\x1B[92m"
  3136. #define kANSIHighYellow "\x1B[93m"
  3137. #define kANSIHighBlue "\x1B[94m"
  3138. #define kANSIHighMagenta "\x1B[95m"
  3139. #define kANSIHighCyan "\x1B[96m"
  3140. #define kANSIHighWhite "\x1B[97m"
  3141. #define kANSIHighForeReset "\x1B[99m"
  3142. // High Intensity Background colors
  3143. #define kANSIHighBackBlack "\x1B[100m"
  3144. #define kANSIHighBackRed "\x1B[101m"
  3145. #define kANSIHighBackGreen "\x1B[102m"
  3146. #define kANSIHighBackYellow "\x1B[103m"
  3147. #define kANSIHighBackBlue "\x1B[104m"
  3148. #define kANSIHighBackMagenta "\x1B[105m"
  3149. #define kANSIHighBackCyan "\x1B[106m"
  3150. #define kANSIHighBackWhite "\x1B[107m"
  3151. #define kANSIHighBackReset "\x1B[109m"
  3152. // Unit Test
  3153. #define kANSIEscapeSequenceTest \
  3154. kANSINormal "kANSINormal" kANSINormal "\n" \
  3155. kANSIBold "kANSIBold" kANSINormal "\n" \
  3156. kANSIFaint "kANSIFaint" kANSINormal "\n" \
  3157. kANSIItalic "kANSIItalic" kANSINormal "\n" \
  3158. kANSIUnderline "kANSIUnderline" kANSINormal "\n" \
  3159. kANSIUnderlineDouble "kANSIUnderlineDouble" kANSINormal "\n" \
  3160. kANSIUnderlineOff "kANSIUnderlineOff" kANSINormal "\n" \
  3161. kANSIBlink "kANSIBlink" kANSINormal "\n" \
  3162. kANSIBlinkRapid "kANSIBlinkRapid" kANSINormal "\n" \
  3163. kANSIBlinkOff "kANSIBlinkOff" kANSINormal "\n" \
  3164. kANSINegative "kANSINegative" kANSINormal "\n" \
  3165. kANSIPositive "kANSIPositive" kANSINormal "\n" \
  3166. kANSIConceal "kANSIConceal" kANSINormal " (kANSIConceal)\n" \
  3167. kANSIReveal "kANSIReveal" kANSINormal "\n" \
  3168. \
  3169. kANSIBlack "kANSIBlack" kANSINormal " (kANSIBlack)\n" \
  3170. kANSIRed "kANSIRed" kANSINormal "\n" \
  3171. kANSIGreen "kANSIGreen" kANSINormal "\n" \
  3172. kANSIYellow "kANSIYellow" kANSINormal "\n" \
  3173. kANSIBlue "kANSIBlue" kANSINormal "\n" \
  3174. kANSIMagenta "kANSIMagenta" kANSINormal "\n" \
  3175. kANSICyan "kANSICyan" kANSINormal "\n" \
  3176. kANSIWhite "kANSIWhite" kANSINormal " (kANSIWhite)\n" \
  3177. kANSIForeReset "kANSIForeReset" kANSINormal "\n" \
  3178. \
  3179. kANSIBackBlack "kANSIBackBlack" kANSINormal "\n" \
  3180. kANSIBackRed "kANSIBackRed" kANSINormal "\n" \
  3181. kANSIBackGreen "kANSIBackGreen" kANSINormal "\n" \
  3182. kANSIBackYellow "kANSIBackYellow" kANSINormal "\n" \
  3183. kANSIBackBlue "kANSIBackBlue" kANSINormal "\n" \
  3184. kANSIBackMagenta "kANSIBackMagenta" kANSINormal "\n" \
  3185. kANSIBackCyan "kANSIBackCyan" kANSINormal "\n" \
  3186. kANSIBackWhite "kANSIBackWhite" kANSINormal "\n" \
  3187. kANSIBackReset "kANSIBackReset" kANSINormal "\n" \
  3188. \
  3189. kANSIHighBlack "kANSIHighBlack" kANSINormal "\n" \
  3190. kANSIHighRed "kANSIHighRed" kANSINormal "\n" \
  3191. kANSIHighGreen "kANSIHighGreen" kANSINormal "\n" \
  3192. kANSIHighYellow "kANSIHighYellow" kANSINormal "\n" \
  3193. kANSIHighBlue "kANSIHighBlue" kANSINormal "\n" \
  3194. kANSIHighMagenta "kANSIHighMagenta" kANSINormal "\n" \
  3195. kANSIHighCyan "kANSIHighCyan" kANSINormal "\n" \
  3196. kANSIHighWhite "kANSIHighWhite" kANSINormal " (kANSIHighWhite)\n" \
  3197. kANSIHighForeReset "kANSIHighForeReset" kANSINormal "\n" \
  3198. \
  3199. kANSIHighBackBlack "kANSIHighBackBlack" kANSINormal "\n" \
  3200. kANSIHighBackRed "kANSIHighBackRed" kANSINormal "\n" \
  3201. kANSIHighBackGreen "kANSIHighBackGreen" kANSINormal "\n" \
  3202. kANSIHighBackYellow "kANSIHighBackYellow" kANSINormal "\n" \
  3203. kANSIHighBackBlue "kANSIHighBackBlue" kANSINormal "\n" \
  3204. kANSIHighBackMagenta "kANSIHighBackMagenta" kANSINormal "\n" \
  3205. kANSIHighBackCyan "kANSIHighBackCyan" kANSINormal "\n" \
  3206. kANSIHighBackWhite "kANSIHighBackWhite" kANSINormal " (kANSIHighBackWhite)\n" \
  3207. kANSIHighBackReset "kANSIHighBackReset" kANSINormal "\n"
  3208. #ifndef PATH_MAX
  3209. #define PATH_MAX 512
  3210. #endif
  3211. #endif // __APSCommonServices_h__