Endian.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. {
  2. File: CarbonCore/Endian.h
  3. Contains: Endian swapping utilties
  4. The contents of this header file are deprecated.
  5. Use CFByteOrder API instead.
  6. Copyright: © 1997-2011 by Apple Inc. All rights reserved.
  7. }
  8. {
  9. Modified for use with Free Pascal
  10. Version 308
  11. Please report any bugs to <[email protected]>
  12. }
  13. {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
  14. {$mode macpas}
  15. {$modeswitch cblocks}
  16. {$packenum 1}
  17. {$macro on}
  18. {$inline on}
  19. {$calling mwpascal}
  20. unit Endian;
  21. interface
  22. {$setc UNIVERSAL_INTERFACES_VERSION := $0400}
  23. {$setc GAP_INTERFACES_VERSION := $0308}
  24. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  25. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  26. {$endc}
  27. {$ifc defined CPUPOWERPC and defined CPUI386}
  28. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  29. {$endc}
  30. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  31. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  32. {$endc}
  33. {$ifc not defined __ppc__ and defined CPUPOWERPC32}
  34. {$setc __ppc__ := 1}
  35. {$elsec}
  36. {$setc __ppc__ := 0}
  37. {$endc}
  38. {$ifc not defined __ppc64__ and defined CPUPOWERPC64}
  39. {$setc __ppc64__ := 1}
  40. {$elsec}
  41. {$setc __ppc64__ := 0}
  42. {$endc}
  43. {$ifc not defined __i386__ and defined CPUI386}
  44. {$setc __i386__ := 1}
  45. {$elsec}
  46. {$setc __i386__ := 0}
  47. {$endc}
  48. {$ifc not defined __x86_64__ and defined CPUX86_64}
  49. {$setc __x86_64__ := 1}
  50. {$elsec}
  51. {$setc __x86_64__ := 0}
  52. {$endc}
  53. {$ifc not defined __arm__ and defined CPUARM}
  54. {$setc __arm__ := 1}
  55. {$elsec}
  56. {$setc __arm__ := 0}
  57. {$endc}
  58. {$ifc not defined __arm64__ and defined CPUAARCH64}
  59. {$setc __arm64__ := 1}
  60. {$elsec}
  61. {$setc __arm64__ := 0}
  62. {$endc}
  63. {$ifc defined cpu64}
  64. {$setc __LP64__ := 1}
  65. {$elsec}
  66. {$setc __LP64__ := 0}
  67. {$endc}
  68. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  69. {$error Conflicting definitions for __ppc__ and __i386__}
  70. {$endc}
  71. {$ifc defined __ppc__ and __ppc__}
  72. {$setc TARGET_CPU_PPC := TRUE}
  73. {$setc TARGET_CPU_PPC64 := FALSE}
  74. {$setc TARGET_CPU_X86 := FALSE}
  75. {$setc TARGET_CPU_X86_64 := FALSE}
  76. {$setc TARGET_CPU_ARM := FALSE}
  77. {$setc TARGET_CPU_ARM64 := FALSE}
  78. {$setc TARGET_OS_MAC := TRUE}
  79. {$setc TARGET_OS_IPHONE := FALSE}
  80. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  81. {$setc TARGET_OS_EMBEDDED := FALSE}
  82. {$elifc defined __ppc64__ and __ppc64__}
  83. {$setc TARGET_CPU_PPC := FALSE}
  84. {$setc TARGET_CPU_PPC64 := TRUE}
  85. {$setc TARGET_CPU_X86 := FALSE}
  86. {$setc TARGET_CPU_X86_64 := FALSE}
  87. {$setc TARGET_CPU_ARM := FALSE}
  88. {$setc TARGET_CPU_ARM64 := FALSE}
  89. {$setc TARGET_OS_MAC := TRUE}
  90. {$setc TARGET_OS_IPHONE := FALSE}
  91. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  92. {$setc TARGET_OS_EMBEDDED := FALSE}
  93. {$elifc defined __i386__ and __i386__}
  94. {$setc TARGET_CPU_PPC := FALSE}
  95. {$setc TARGET_CPU_PPC64 := FALSE}
  96. {$setc TARGET_CPU_X86 := TRUE}
  97. {$setc TARGET_CPU_X86_64 := FALSE}
  98. {$setc TARGET_CPU_ARM := FALSE}
  99. {$setc TARGET_CPU_ARM64 := FALSE}
  100. {$ifc defined(iphonesim)}
  101. {$setc TARGET_OS_MAC := FALSE}
  102. {$setc TARGET_OS_IPHONE := TRUE}
  103. {$setc TARGET_IPHONE_SIMULATOR := TRUE}
  104. {$elsec}
  105. {$setc TARGET_OS_MAC := TRUE}
  106. {$setc TARGET_OS_IPHONE := FALSE}
  107. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  108. {$endc}
  109. {$setc TARGET_OS_EMBEDDED := FALSE}
  110. {$elifc defined __x86_64__ and __x86_64__}
  111. {$setc TARGET_CPU_PPC := FALSE}
  112. {$setc TARGET_CPU_PPC64 := FALSE}
  113. {$setc TARGET_CPU_X86 := FALSE}
  114. {$setc TARGET_CPU_X86_64 := TRUE}
  115. {$setc TARGET_CPU_ARM := FALSE}
  116. {$setc TARGET_CPU_ARM64 := FALSE}
  117. {$ifc defined(iphonesim)}
  118. {$setc TARGET_OS_MAC := FALSE}
  119. {$setc TARGET_OS_IPHONE := TRUE}
  120. {$setc TARGET_IPHONE_SIMULATOR := TRUE}
  121. {$elsec}
  122. {$setc TARGET_OS_MAC := TRUE}
  123. {$setc TARGET_OS_IPHONE := FALSE}
  124. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  125. {$endc}
  126. {$setc TARGET_OS_EMBEDDED := FALSE}
  127. {$elifc defined __arm__ and __arm__}
  128. {$setc TARGET_CPU_PPC := FALSE}
  129. {$setc TARGET_CPU_PPC64 := FALSE}
  130. {$setc TARGET_CPU_X86 := FALSE}
  131. {$setc TARGET_CPU_X86_64 := FALSE}
  132. {$setc TARGET_CPU_ARM := TRUE}
  133. {$setc TARGET_CPU_ARM64 := FALSE}
  134. { will require compiler define when/if other Apple devices with ARM cpus ship }
  135. {$setc TARGET_OS_MAC := FALSE}
  136. {$setc TARGET_OS_IPHONE := TRUE}
  137. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  138. {$setc TARGET_OS_EMBEDDED := TRUE}
  139. {$elifc defined __arm64__ and __arm64__}
  140. {$setc TARGET_CPU_PPC := FALSE}
  141. {$setc TARGET_CPU_PPC64 := FALSE}
  142. {$setc TARGET_CPU_X86 := FALSE}
  143. {$setc TARGET_CPU_X86_64 := FALSE}
  144. {$setc TARGET_CPU_ARM := FALSE}
  145. {$setc TARGET_CPU_ARM64 := TRUE}
  146. { will require compiler define when/if other Apple devices with ARM cpus ship }
  147. {$setc TARGET_OS_MAC := FALSE}
  148. {$setc TARGET_OS_IPHONE := TRUE}
  149. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  150. {$setc TARGET_OS_EMBEDDED := TRUE}
  151. {$elsec}
  152. {$error __ppc__ nor __ppc64__ nor __i386__ nor __x86_64__ nor __arm__ nor __arm64__ is defined.}
  153. {$endc}
  154. {$ifc defined __LP64__ and __LP64__ }
  155. {$setc TARGET_CPU_64 := TRUE}
  156. {$elsec}
  157. {$setc TARGET_CPU_64 := FALSE}
  158. {$endc}
  159. {$ifc defined FPC_BIG_ENDIAN}
  160. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  161. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  162. {$elifc defined FPC_LITTLE_ENDIAN}
  163. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  164. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  165. {$elsec}
  166. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  167. {$endc}
  168. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  169. {$setc CALL_NOT_IN_CARBON := FALSE}
  170. {$setc OLDROUTINENAMES := FALSE}
  171. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  172. {$setc OPAQUE_UPP_TYPES := TRUE}
  173. {$setc OTCARBONAPPLICATION := TRUE}
  174. {$setc OTKERNEL := FALSE}
  175. {$setc PM_USE_SESSION_APIS := TRUE}
  176. {$setc TARGET_API_MAC_CARBON := TRUE}
  177. {$setc TARGET_API_MAC_OS8 := FALSE}
  178. {$setc TARGET_API_MAC_OSX := TRUE}
  179. {$setc TARGET_CARBON := TRUE}
  180. {$setc TARGET_CPU_68K := FALSE}
  181. {$setc TARGET_CPU_MIPS := FALSE}
  182. {$setc TARGET_CPU_SPARC := FALSE}
  183. {$setc TARGET_OS_UNIX := FALSE}
  184. {$setc TARGET_OS_WIN32 := FALSE}
  185. {$setc TARGET_RT_MAC_68881 := FALSE}
  186. {$setc TARGET_RT_MAC_CFM := FALSE}
  187. {$setc TARGET_RT_MAC_MACHO := TRUE}
  188. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  189. {$setc TYPE_BOOL := FALSE}
  190. {$setc TYPE_EXTENDED := FALSE}
  191. {$setc TYPE_LONGLONG := TRUE}
  192. uses MacTypes;
  193. {$endc} {not MACOSALLINCLUDE}
  194. {$ALIGN MAC68K}
  195. {
  196. This file provides Endian Flipping routines for dealing with converting data
  197. between Big-Endian and Little-Endian machines. These routines are useful
  198. when writing code to compile for both Big and Little Endian machines and
  199. which must handle other endian number formats, such as reading or writing
  200. to a file or network packet.
  201. These routines are named as follows:
  202. Endian<U><W>_<S>to<D>
  203. where
  204. <U> is whether the integer is signed ('S') or unsigned ('U')
  205. <W> is integer bit width: 16, 32, or 64
  206. <S> is the source endian format: 'B' for big, 'L' for little, or 'N' for native
  207. <D> is the destination endian format: 'B' for big, 'L' for little, or 'N' for native
  208. For example, to convert a Big Endian 32-bit unsigned integer to the current native format use:
  209. long i = EndianU32_BtoN(data);
  210. This file is set up so that the function macro to nothing when the target runtime already
  211. is the desired format (e.g. on Big Endian machines, EndianU32_BtoN() macros away).
  212. If long long's are not supported, you cannot get 64-bit quantities as a single value.
  213. The macros are not defined in that case.
  214. For gcc, the macros build on top of the inline byte swapping
  215. routines from <libkern/OSByteOrder.h>, which may have better performance.
  216. <<< W A R N I N G >>>
  217. It is very important not to put any autoincrements inside the macros. This
  218. will produce erroneous results because each time the address is accessed in the macro,
  219. the increment occurs.
  220. }
  221. // Macros might be better solutions
  222. function Endian16_Swap( arg: UInt16 ): UInt16; inline;
  223. function Endian32_Swap( arg: UInt32 ): UInt32; inline;
  224. function Endian64_Swap_Pascal( arg: UInt64 ): UInt64; inline;
  225. function EndianS16_Swap( arg: SInt16 ): SInt16; inline;
  226. function EndianS32_Swap( arg: SInt32 ): SInt32; inline;
  227. function EndianS64_Swap( arg: SInt64 ): SInt64; inline;
  228. function Endian64_Swap( arg: UInt64 ): UInt64; inline;
  229. // Macro away no-op functions
  230. {$ifc TARGET_RT_BIG_ENDIAN}
  231. function EndianS16_BtoN( arg: SInt16 ): SInt16; inline;
  232. function EndianS16_NtoB( arg: SInt16 ): SInt16; inline;
  233. function EndianU16_BtoN( arg: UInt16 ): UInt16; inline;
  234. function EndianU16_NtoB( arg: UInt16 ): UInt16; inline;
  235. function EndianS32_BtoN( arg: SInt32 ): SInt32; inline;
  236. function EndianS32_NtoB( arg: SInt32 ): SInt32; inline;
  237. function EndianU32_BtoN( arg: UInt32 ): UInt32; inline;
  238. function EndianU32_NtoB( arg: UInt32 ): UInt32; inline;
  239. function EndianS64_BtoN( arg: SInt64 ): SInt64; inline;
  240. function EndianS64_NtoB( arg: SInt64 ): SInt64; inline;
  241. function EndianU64_BtoN( arg: UInt64 ): UInt64; inline;
  242. function EndianU64_NtoB( arg: UInt64 ): UInt64; inline;
  243. {$elsec}
  244. function EndianS16_LtoN( arg: SInt16 ): SInt16; inline;
  245. function EndianS16_NtoL( arg: SInt16 ): SInt16; inline;
  246. function EndianU16_LtoN( arg: UInt16 ): UInt16; inline;
  247. function EndianU16_NtoL( arg: UInt16 ): UInt16; inline;
  248. function EndianS32_LtoN( arg: SInt32 ): SInt32; inline;
  249. function EndianS32_NtoL( arg: SInt32 ): SInt32; inline;
  250. function EndianU32_LtoN( arg: UInt32 ): UInt32; inline;
  251. function EndianU32_NtoL( arg: UInt32 ): UInt32; inline;
  252. function EndianS64_LtoN( arg: SInt64 ): SInt64; inline;
  253. function EndianS64_NtoL( arg: SInt64 ): SInt64; inline;
  254. function EndianU64_LtoN( arg: UInt64 ): UInt64; inline;
  255. function EndianU64_NtoL( arg: UInt64 ): UInt64; inline;
  256. {$endc}
  257. // Map native to actual
  258. {$ifc TARGET_RT_BIG_ENDIAN}
  259. function EndianS16_LtoN( arg: SInt16 ): SInt16; inline;
  260. function EndianS16_NtoL( arg: SInt16 ): SInt16; inline;
  261. function EndianU16_LtoN( arg: UInt16 ): UInt16; inline;
  262. function EndianU16_NtoL( arg: UInt16 ): UInt16; inline;
  263. function EndianS32_LtoN( arg: SInt32 ): SInt32; inline;
  264. function EndianS32_NtoL( arg: SInt32 ): SInt32; inline;
  265. function EndianU32_LtoN( arg: UInt32 ): UInt32; inline;
  266. function EndianU32_NtoL( arg: UInt32 ): UInt32; inline;
  267. function EndianS64_LtoN( arg: SInt64 ): SInt64; inline;
  268. function EndianS64_NtoL( arg: SInt64 ): SInt64; inline;
  269. function EndianU64_LtoN( arg: UInt64 ): UInt64; inline;
  270. function EndianU64_NtoL( arg: UInt64 ): UInt64; inline;
  271. {$elsec}
  272. function EndianS16_BtoN( arg: SInt16 ): SInt16; inline;
  273. function EndianS16_NtoB( arg: SInt16 ): SInt16; inline;
  274. function EndianU16_BtoN( arg: UInt16 ): UInt16; inline;
  275. function EndianU16_NtoB( arg: UInt16 ): UInt16; inline;
  276. function EndianS32_BtoN( arg: SInt32 ): SInt32; inline;
  277. function EndianS32_NtoB( arg: SInt32 ): SInt32; inline;
  278. function EndianU32_BtoN( arg: UInt32 ): UInt32; inline;
  279. function EndianU32_NtoB( arg: UInt32 ): UInt32; inline;
  280. function EndianS64_BtoN( arg: SInt64 ): SInt64; inline;
  281. function EndianS64_NtoB( arg: SInt64 ): SInt64; inline;
  282. function EndianU64_BtoN( arg: UInt64 ): UInt64; inline;
  283. function EndianU64_NtoB( arg: UInt64 ): UInt64; inline;
  284. {$endc}
  285. // Implement *LtoB and *BtoL
  286. function EndianS16_LtoB( arg: SInt16 ): SInt16; inline;
  287. function EndianS16_BtoL( arg: SInt16 ): SInt16; inline;
  288. function EndianU16_LtoB( arg: UInt16 ): UInt16; inline;
  289. function EndianU16_BtoL( arg: UInt16 ): UInt16; inline;
  290. function EndianS32_LtoB( arg: SInt32 ): SInt32; inline;
  291. function EndianS32_BtoL( arg: SInt32 ): SInt32; inline;
  292. function EndianU32_LtoB( arg: UInt32 ): UInt32; inline;
  293. function EndianU32_BtoL( arg: UInt32 ): UInt32; inline;
  294. function EndianS64_LtoB( arg: SInt64 ): SInt64; inline;
  295. function EndianS64_BtoL( arg: SInt64 ): SInt64; inline;
  296. function EndianU64_LtoB( arg: UInt64 ): UInt64; inline;
  297. function EndianU64_BtoL( arg: UInt64 ): UInt64; inline;
  298. {
  299. These types are used for structures that contain data that is
  300. always in BigEndian format. This extra typing prevents little
  301. endian code from directly changing the data, thus saving much
  302. time in the debugger.
  303. }
  304. {$ifc TARGET_RT_LITTLE_ENDIAN}
  305. type
  306. BigEndianUInt32 = record
  307. bigEndianValue: UInt32;
  308. end;
  309. type
  310. BigEndianLong = record
  311. bigEndianValue: SIGNEDLONG;
  312. end;
  313. type
  314. BigEndianUnsignedLong = record
  315. bigEndianValue: UNSIGNEDLONG;
  316. end;
  317. type
  318. BigEndianShort = record
  319. bigEndianValue: SInt16;
  320. end;
  321. type
  322. BigEndianUnsignedShort = record
  323. bigEndianValue: UInt16;
  324. end;
  325. type
  326. BigEndianFixed = record
  327. bigEndianValue: Fixed;
  328. end;
  329. type
  330. BigEndianUnsignedFixed = record
  331. bigEndianValue: UnsignedFixed;
  332. end;
  333. type
  334. BigEndianOSType = record
  335. bigEndianValue: OSType;
  336. end;
  337. {$elsec}
  338. type
  339. BigEndianUInt32 = UInt32;
  340. BigEndianLong = SIGNEDLONG;
  341. BigEndianUnsignedLong = UNSIGNEDLONG;
  342. BigEndianShort = SInt16;
  343. BigEndianUnsignedShort = UInt16;
  344. BigEndianFixed = Fixed;
  345. BigEndianUnsignedFixed = UnsignedFixed;
  346. BigEndianOSType = OSType;
  347. {$endc} {TARGET_RT_LITTLE_ENDIAN}
  348. type
  349. BigEndianUInt32Ptr = ^BigEndianUInt32;
  350. BigEndianLongPtr = ^BigEndianLong;
  351. BigEndianUnsignedLongPtr = ^BigEndianUnsignedLong;
  352. BigEndianShortPtr = ^BigEndianShort;
  353. BigEndianUnsignedShortPtr = ^BigEndianUnsignedShort;
  354. BigEndianFixedPtr = ^BigEndianFixed;
  355. BigEndianUnsignedFixedPtr = ^BigEndianUnsignedFixed;
  356. BigEndianOSTypePtr = ^BigEndianOSType;
  357. {$ifc TARGET_API_MAC_OSX}
  358. {
  359. CoreEndian flipping API.
  360. This API is used to generically massage data buffers, in
  361. place, from one endian architecture to another. In effect,
  362. the API supports registering a set of callbacks that can
  363. effect this translation.
  364. The data types have specific meanings within their domain,
  365. although some data types can be registered with the same
  366. callback in several domains. There is no wildcard domain.
  367. A set of pre-defined flippers are implemented by the Carbon
  368. frameworks for most common resource manager and AppleEvent data
  369. types.
  370. }
  371. const
  372. kCoreEndianResourceManagerDomain = FourCharCode('rsrc');
  373. kCoreEndianAppleEventManagerDomain = FourCharCode('aevt');
  374. {
  375. * CoreEndianFlipProc
  376. *
  377. * Discussion:
  378. * Callback use to flip endian-ness of typed data
  379. *
  380. * Parameters:
  381. *
  382. * dataDomain:
  383. * Domain of the data type
  384. *
  385. * dataType:
  386. * Type of data being flipped
  387. *
  388. * id:
  389. * resource id (if being flipped on behalf of the resource
  390. * manager, otherwise will be zero)
  391. *
  392. * dataPtr:
  393. * Pointer to the data
  394. *
  395. * dataSize:
  396. * Length of the data
  397. *
  398. * currentlyNative:
  399. * Boolean indicating which direction to flip: false means flip
  400. * from disk big endian to native (from disk), true means flip
  401. * from native to disk big endian (to disk)
  402. *
  403. * refcon:
  404. * An optional user reference supplied when the flipper is
  405. * installed
  406. *
  407. * Result:
  408. * Error code indicating whether the data was flipped. noErr would
  409. * indicate that the data was flipped as appropriate; any other
  410. * error will be propagated back to the caller.
  411. }
  412. type
  413. CoreEndianFlipProc = function( dataDomain: OSType; dataType: OSType; id: SInt16; dataPtr: UnivPtr; dataSize: ByteCount; currentlyNative: Boolean; refcon: UnivPtr ): OSStatus;
  414. {
  415. * Install a flipper for this application
  416. }
  417. {
  418. * CoreEndianInstallFlipper()
  419. *
  420. * Summary:
  421. * Installs a flipper proc for the given data type. If the flipper
  422. * is already registered, this flipper will take replace it.
  423. *
  424. * Mac OS X threading:
  425. * Thread safe since version 10.3
  426. *
  427. * Parameters:
  428. *
  429. * dataDomain:
  430. * Domain of the data type
  431. *
  432. * dataType:
  433. * Type of data for which this flipper should be installed
  434. *
  435. * proc:
  436. * Flipper callback to be called for data of this type
  437. *
  438. * refcon:
  439. * Optional user reference for the flipper
  440. *
  441. * Result:
  442. * Error code indicating whether or not the flipper could be
  443. * installed
  444. *
  445. * Availability:
  446. * Mac OS X: in version 10.3 and later in CoreServices.framework
  447. * CarbonLib: not available
  448. * Non-Carbon CFM: not available
  449. }
  450. function CoreEndianInstallFlipper( dataDomain: OSType; dataType: OSType; proc: CoreEndianFlipProc; refcon: UnivPtr { can be NULL } ): OSStatus; external name '_CoreEndianInstallFlipper';
  451. (* __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_3, __MAC_10_8, __IPHONE_NA, __IPHONE_NA) *)
  452. {
  453. * CoreEndianGetFlipper()
  454. *
  455. * Summary:
  456. * Gets an existing data flipper proc for the given data type
  457. *
  458. * Mac OS X threading:
  459. * Thread safe since version 10.3
  460. *
  461. * Parameters:
  462. *
  463. * dataDomain:
  464. * Domain of the data type
  465. *
  466. * dataType:
  467. * Type of the data for which this flipper should be installed
  468. *
  469. * proc:
  470. * Pointer to a flipper callback
  471. *
  472. * refcon:
  473. * Pointer to the callback refcon
  474. *
  475. * Result:
  476. * noErr if the given flipper could be found; otherwise
  477. * handlerNotFoundErr will be returned.
  478. *
  479. * Availability:
  480. * Mac OS X: in version 10.3 and later in CoreServices.framework
  481. * CarbonLib: not available
  482. * Non-Carbon CFM: not available
  483. }
  484. function CoreEndianGetFlipper( dataDomain: OSType; dataType: OSType; var proc: CoreEndianFlipProc; refcon: UnivPtrPtr ): OSStatus; external name '_CoreEndianGetFlipper';
  485. (* __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_3, __MAC_10_8, __IPHONE_NA, __IPHONE_NA) *)
  486. {
  487. * CoreEndianFlipData()
  488. *
  489. * Summary:
  490. * Calls the flipper for the given data type with the associated data
  491. *
  492. * Mac OS X threading:
  493. * Thread safe since version 10.3
  494. *
  495. * Parameters:
  496. *
  497. * dataDomain:
  498. * Domain of the data type
  499. *
  500. * dataType:
  501. * type of the data
  502. *
  503. * id:
  504. * resource id (if not a resource, pass zero)
  505. *
  506. * data:
  507. * a pointer to the data to be flipped (in place)
  508. *
  509. * dataLen:
  510. * length of the data to flip
  511. *
  512. * currentlyNative:
  513. * a boolean indicating the direction to flip (whether the data is
  514. * currently native endian or big-endian)
  515. *
  516. * Result:
  517. * Error code indicating whether the data was flipped. If
  518. * handlerNotFound is returned, then no flipping took place (which
  519. * is not necessarily an error condtion)
  520. *
  521. * Availability:
  522. * Mac OS X: in version 10.3 and later in CoreServices.framework
  523. * CarbonLib: not available
  524. * Non-Carbon CFM: not available
  525. }
  526. function CoreEndianFlipData( dataDomain: OSType; dataType: OSType; id: SInt16; data: UnivPtr; dataLen: ByteCount; currentlyNative: Boolean ): OSStatus; external name '_CoreEndianFlipData';
  527. (* __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_3, __MAC_10_8, __IPHONE_NA, __IPHONE_NA) *)
  528. {$endc} {TARGET_API_MAC_OSX}
  529. {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
  530. implementation
  531. {$R-}
  532. function Endian16_Swap( arg: UInt16 ): UInt16; inline;
  533. begin
  534. Endian16_Swap := (( arg shl 8) and $0FF00) or (( arg shr 8) and $00FF);
  535. end;
  536. function Endian32_Swap( arg: UInt32 ): UInt32; inline;
  537. begin
  538. Endian32_Swap := ((arg and $FF) shl 24) or ((arg and $0FF00) shl 8) or ((arg shr 8) and $0FF00) or ((arg shr 24) and $FF);
  539. end;
  540. function Endian64_Swap_Pascal( arg: UInt64 ): UInt64; inline;
  541. begin
  542. Endian64_Swap_Pascal := (Endian32_Swap( arg and $FFFFFFFF ) shl 32) or Endian32_Swap( (arg shr 32) and $FFFFFFFF );
  543. end;
  544. function Endian64_Swap( arg: UInt64 ): UInt64; inline;
  545. begin
  546. Endian64_Swap := Endian64_Swap_Pascal(arg);
  547. end;
  548. function EndianS16_Swap( arg: SInt16 ): SInt16; inline;
  549. begin
  550. EndianS16_Swap := (( arg shl 8) and $0FF00) or (( arg shr 8) and $00FF);
  551. end;
  552. function EndianS32_Swap( arg: SInt32 ): SInt32; inline;
  553. begin
  554. EndianS32_Swap := ((arg and $FF) shl 24) or ((arg and $0FF00) shl 8) or ((arg shr 8) and $0FF00) or ((arg shr 24) and $FF);
  555. end;
  556. function EndianS64_Swap( arg: SInt64 ): SInt64; inline;
  557. begin
  558. EndianS64_Swap := (SInt64( Endian32_Swap( arg and $FFFFFFFF ) ) shl 32) or Endian32_Swap( (arg shr 32) and $FFFFFFFF );
  559. end;
  560. {$ifc TARGET_RT_BIG_ENDIAN}
  561. function EndianS16_BtoN( arg: SInt16 ): SInt16; inline;
  562. begin
  563. EndianS16_BtoN := arg;
  564. end;
  565. function EndianS16_NtoB( arg: SInt16 ): SInt16; inline;
  566. begin
  567. EndianS16_NtoB := arg;
  568. end;
  569. function EndianU16_BtoN( arg: UInt16 ): UInt16; inline;
  570. begin
  571. EndianU16_BtoN := arg;
  572. end;
  573. function EndianU16_NtoB( arg: UInt16 ): UInt16; inline;
  574. begin
  575. EndianU16_NtoB := arg;
  576. end;
  577. function EndianS32_BtoN( arg: SInt32 ): SInt32; inline;
  578. begin
  579. EndianS32_BtoN := arg;
  580. end;
  581. function EndianS32_NtoB( arg: SInt32 ): SInt32; inline;
  582. begin
  583. EndianS32_NtoB := arg;
  584. end;
  585. function EndianU32_BtoN( arg: UInt32 ): UInt32; inline;
  586. begin
  587. EndianU32_BtoN := arg;
  588. end;
  589. function EndianU32_NtoB( arg: UInt32 ): UInt32; inline;
  590. begin
  591. EndianU32_NtoB := arg;
  592. end;
  593. function EndianS64_BtoN( arg: SInt64 ): SInt64; inline;
  594. begin
  595. EndianS64_BtoN := arg;
  596. end;
  597. function EndianS64_NtoB( arg: SInt64 ): SInt64; inline;
  598. begin
  599. EndianS64_NtoB := arg;
  600. end;
  601. function EndianU64_BtoN( arg: UInt64 ): UInt64; inline;
  602. begin
  603. EndianU64_BtoN := arg;
  604. end;
  605. function EndianU64_NtoB( arg: UInt64 ): UInt64; inline;
  606. begin
  607. EndianU64_NtoB := arg;
  608. end;
  609. function EndianS16_LtoN( arg: SInt16 ): SInt16; inline;
  610. begin
  611. EndianS16_LtoN := EndianS16_Swap(arg);
  612. end;
  613. function EndianS16_NtoL( arg: SInt16 ): SInt16; inline;
  614. begin
  615. EndianS16_NtoL := EndianS16_Swap(arg);
  616. end;
  617. function EndianU16_LtoN( arg: UInt16 ): UInt16; inline;
  618. begin
  619. EndianU16_LtoN := Endian16_Swap(arg);
  620. end;
  621. function EndianU16_NtoL( arg: UInt16 ): UInt16; inline;
  622. begin
  623. EndianU16_NtoL := Endian16_Swap(arg);
  624. end;
  625. function EndianS32_LtoN( arg: SInt32 ): SInt32; inline;
  626. begin
  627. EndianS32_LtoN := EndianS32_Swap(arg);
  628. end;
  629. function EndianS32_NtoL( arg: SInt32 ): SInt32; inline;
  630. begin
  631. EndianS32_NtoL := EndianS32_Swap(arg);
  632. end;
  633. function EndianU32_LtoN( arg: UInt32 ): UInt32; inline;
  634. begin
  635. EndianU32_LtoN := Endian32_Swap(arg);
  636. end;
  637. function EndianU32_NtoL( arg: UInt32 ): UInt32; inline;
  638. begin
  639. EndianU32_NtoL := Endian32_Swap(arg);
  640. end;
  641. function EndianS64_LtoN( arg: SInt64 ): SInt64; inline;
  642. begin
  643. EndianS64_LtoN := EndianS64_Swap(arg);
  644. end;
  645. function EndianS64_NtoL( arg: SInt64 ): SInt64; inline;
  646. begin
  647. EndianS64_NtoL := EndianS64_Swap(arg);
  648. end;
  649. function EndianU64_LtoN( arg: UInt64 ): UInt64; inline;
  650. begin
  651. EndianU64_LtoN := Endian64_Swap(arg);
  652. end;
  653. function EndianU64_NtoL( arg: UInt64 ): UInt64; inline;
  654. begin
  655. EndianU64_NtoL := Endian64_Swap(arg);
  656. end;
  657. {$elsec}
  658. function EndianS16_BtoN( arg: SInt16 ): SInt16; inline;
  659. begin
  660. EndianS16_BtoN := EndianS16_Swap(arg);
  661. end;
  662. function EndianS16_NtoB( arg: SInt16 ): SInt16; inline;
  663. begin
  664. EndianS16_NtoB := EndianS16_Swap(arg);
  665. end;
  666. function EndianU16_BtoN( arg: UInt16 ): UInt16; inline;
  667. begin
  668. EndianU16_BtoN := Endian16_Swap(arg);
  669. end;
  670. function EndianU16_NtoB( arg: UInt16 ): UInt16; inline;
  671. begin
  672. EndianU16_NtoB := Endian16_Swap(arg);
  673. end;
  674. function EndianS32_BtoN( arg: SInt32 ): SInt32; inline;
  675. begin
  676. EndianS32_BtoN := EndianS32_Swap(arg);
  677. end;
  678. function EndianS32_NtoB( arg: SInt32 ): SInt32; inline;
  679. begin
  680. EndianS32_NtoB := EndianS32_Swap(arg);
  681. end;
  682. function EndianU32_BtoN( arg: UInt32 ): UInt32; inline;
  683. begin
  684. EndianU32_BtoN := Endian32_Swap(arg);
  685. end;
  686. function EndianU32_NtoB( arg: UInt32 ): UInt32; inline;
  687. begin
  688. EndianU32_NtoB := Endian32_Swap(arg);
  689. end;
  690. function EndianS64_BtoN( arg: SInt64 ): SInt64; inline;
  691. begin
  692. EndianS64_BtoN := EndianS64_Swap(arg);
  693. end;
  694. function EndianS64_NtoB( arg: SInt64 ): SInt64; inline;
  695. begin
  696. EndianS64_NtoB := EndianS64_Swap(arg);
  697. end;
  698. function EndianU64_BtoN( arg: UInt64 ): UInt64; inline;
  699. begin
  700. EndianU64_BtoN := Endian64_Swap(arg);
  701. end;
  702. function EndianU64_NtoB( arg: UInt64 ): UInt64; inline;
  703. begin
  704. EndianU64_NtoB := Endian64_Swap(arg);
  705. end;
  706. function EndianS16_LtoN( arg: SInt16 ): SInt16; inline;
  707. begin
  708. EndianS16_LtoN := arg;
  709. end;
  710. function EndianS16_NtoL( arg: SInt16 ): SInt16; inline;
  711. begin
  712. EndianS16_NtoL := arg;
  713. end;
  714. function EndianU16_LtoN( arg: UInt16 ): UInt16; inline;
  715. begin
  716. EndianU16_LtoN := arg;
  717. end;
  718. function EndianU16_NtoL( arg: UInt16 ): UInt16; inline;
  719. begin
  720. EndianU16_NtoL := arg;
  721. end;
  722. function EndianS32_LtoN( arg: SInt32 ): SInt32; inline;
  723. begin
  724. EndianS32_LtoN := arg;
  725. end;
  726. function EndianS32_NtoL( arg: SInt32 ): SInt32; inline;
  727. begin
  728. EndianS32_NtoL := arg;
  729. end;
  730. function EndianU32_LtoN( arg: UInt32 ): UInt32; inline;
  731. begin
  732. EndianU32_LtoN := arg;
  733. end;
  734. function EndianU32_NtoL( arg: UInt32 ): UInt32; inline;
  735. begin
  736. EndianU32_NtoL := arg;
  737. end;
  738. function EndianS64_LtoN( arg: SInt64 ): SInt64; inline;
  739. begin
  740. EndianS64_LtoN := arg;
  741. end;
  742. function EndianS64_NtoL( arg: SInt64 ): SInt64; inline;
  743. begin
  744. EndianS64_NtoL := arg;
  745. end;
  746. function EndianU64_LtoN( arg: UInt64 ): UInt64; inline;
  747. begin
  748. EndianU64_LtoN := arg;
  749. end;
  750. function EndianU64_NtoL( arg: UInt64 ): UInt64; inline;
  751. begin
  752. EndianU64_NtoL := arg;
  753. end;
  754. {$endc}
  755. function EndianS16_LtoB( arg: SInt16 ): SInt16; inline;
  756. begin
  757. EndianS16_LtoB:=EndianS16_Swap(arg);
  758. end;
  759. function EndianS16_BtoL( arg: SInt16 ): SInt16; inline;
  760. begin
  761. EndianS16_BtoL:=EndianS16_Swap(arg);
  762. end;
  763. function EndianU16_LtoB( arg: UInt16 ): UInt16; inline;
  764. begin
  765. EndianU16_LtoB:=Endian16_Swap(arg);
  766. end;
  767. function EndianU16_BtoL( arg: UInt16 ): UInt16; inline;
  768. begin
  769. EndianU16_BtoL:=Endian16_Swap(arg);
  770. end;
  771. function EndianS32_LtoB( arg: SInt32 ): SInt32; inline;
  772. begin
  773. EndianS32_LtoB:=EndianS32_Swap(arg);
  774. end;
  775. function EndianS32_BtoL( arg: SInt32 ): SInt32; inline;
  776. begin
  777. EndianS32_BtoL:=EndianS32_Swap(arg);
  778. end;
  779. function EndianU32_LtoB( arg: UInt32 ): UInt32; inline;
  780. begin
  781. EndianU32_LtoB:=Endian32_Swap(arg);
  782. end;
  783. function EndianU32_BtoL( arg: UInt32 ): UInt32; inline;
  784. begin
  785. EndianU32_BtoL:=Endian32_Swap(arg);
  786. end;
  787. function EndianS64_LtoB( arg: SInt64 ): SInt64; inline;
  788. begin
  789. EndianS64_LtoB:=EndianS64_Swap(arg);
  790. end;
  791. function EndianS64_BtoL( arg: SInt64 ): SInt64; inline;
  792. begin
  793. EndianS64_BtoL:=EndianS64_Swap(arg);
  794. end;
  795. function EndianU64_LtoB( arg: UInt64 ): UInt64; inline;
  796. begin
  797. EndianU64_LtoB:=Endian64_Swap_Pascal(arg);
  798. end;
  799. function EndianU64_BtoL( arg: UInt64 ): UInt64; inline;
  800. begin
  801. EndianU64_BtoL:=Endian64_Swap_Pascal(arg);
  802. end;
  803. end.
  804. {$endc} {not MACOSALLINCLUDE}