]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/endian.h
a85790d475d7eb6d5ca67a7abb4bc829ae94680f
   2  * Copyright (c) 2002-2004,2011-2012,2014 Apple Inc. All Rights Reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. Please obtain a copy of the License at 
  10  * http://www.opensource.apple.com/apsl/ and read it before using this 
  13  * The Original Code and all software distributed under the License are 
  14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  18  * Please see the License for the specific language governing rights and 
  19  * limitations under the License. 
  21  * @APPLE_LICENSE_HEADER_END@ 
  31 #include <machine/endian.h> 
  32 #include <libkern/OSByteOrder.h> 
  33 #include <security_utilities/utilities.h> 
  34 #include <security_utilities/memutils.h> 
  35 #include <security_utilities/debugging.h> 
  41 // Encode/decode operations by type, overloaded. 
  42 // You can use these functions directly, but consider using 
  43 // the higher-level constructs below instead. 
  46 static inline unsigned long h2n(unsigned long v
) { return OSSwapHostToBigInt64(v
); } 
  47 static inline unsigned long n2h(unsigned long v
) { return OSSwapBigToHostInt64(v
); } 
  48 static inline unsigned long flip(unsigned long v
) { return OSSwapInt64(v
); } 
  49 static inline signed long h2n(signed long v
) { return OSSwapHostToBigInt64(v
); } 
  50 static inline signed long n2h(signed long v
) { return OSSwapBigToHostInt64(v
); } 
  51 static inline signed long flip(signed long v
) { return OSSwapInt64(v
); } 
  53 static inline unsigned long h2n(unsigned long v
)        { return htonl(v
); } 
  54 static inline unsigned long n2h(unsigned long v
)        { return ntohl(v
); } 
  55 static inline unsigned long flip(unsigned long v
)       { return OSSwapInt32(v
); } 
  56 static inline signed long h2n(signed long v
)            { return htonl(v
); } 
  57 static inline signed long n2h(signed long v
)            { return ntohl(v
); } 
  58 static inline signed long flip(signed long v
)           { return OSSwapInt32(v
); } 
  61 static inline unsigned long long h2n(unsigned long long v
) { return OSSwapHostToBigInt64(v
); } 
  62 static inline unsigned long long n2h(unsigned long long v
) { return OSSwapBigToHostInt64(v
); } 
  63 static inline unsigned long long flip(unsigned long long v
) { return OSSwapInt64(v
); } 
  64 static inline long long h2n(long long v
)                        { return OSSwapHostToBigInt64(v
); } 
  65 static inline long long n2h(long long v
)                        { return OSSwapBigToHostInt64(v
); } 
  66 static inline long long flip(long long v
)                       { return OSSwapInt64(v
); } 
  68 static inline unsigned int h2n(unsigned int v
)          { return htonl(v
); } 
  69 static inline unsigned int n2h(unsigned int v
)          { return ntohl(v
); } 
  70 static inline unsigned int flip(unsigned int v
)         { return OSSwapInt32(v
); } 
  71 static inline signed int h2n(int v
)                                     { return htonl(v
); } 
  72 static inline signed int n2h(int v
)                                     { return ntohl(v
); } 
  73 static inline signed int flip(int v
)                            { return OSSwapInt32(v
); } 
  75 static inline unsigned short h2n(unsigned short v
)      { return htons(v
); } 
  76 static inline unsigned short n2h(unsigned short v
)      { return ntohs(v
); } 
  77 static inline unsigned short flip(unsigned short v
)     { return OSSwapInt16(v
); } 
  78 static inline signed short h2n(signed short v
)          { return htons(v
); } 
  79 static inline signed short n2h(signed short v
)          { return ntohs(v
); } 
  80 static inline signed short flip(signed short v
)         { return OSSwapInt16(v
); } 
  82 static inline unsigned char h2n(unsigned char v
)        { return v
; } 
  83 static inline unsigned char n2h(unsigned char v
)        { return v
; } 
  84 static inline unsigned char flip(unsigned char v
)       { return v
; } 
  85 static inline signed char h2n(signed char v
)            { return v
; } 
  86 static inline signed char n2h(signed char v
)            { return v
; } 
  87 static inline signed char flip(signed char v
)           { return v
; } 
  94 static inline Base 
*h2n(Base 
*p
)        { return (Base 
*)h2n(uintptr_t(p
)); } 
  97 static inline Base 
*n2h(Base 
*p
)        { return (Base 
*)n2h(uintptr_t(p
)); } 
 101 // In-place fix operations 
 103 template <class Type
> 
 104 static inline void h2ni(Type 
&v
)        { v 
= h2n(v
); } 
 106 template <class Type
> 
 107 static inline void n2hi(Type 
&v
)        { v 
= n2h(v
); } 
 110 // Endian<SomeType> keeps NBO values in memory and converts 
 111 // during loads and stores. This presumes that you are using 
 112 // memory blocks thare are read/written/mapped as amorphous byte 
 113 // streams, but want to be byte-order clean using them. 
 115 // The generic definition uses h2n/n2h to flip bytes. Feel free 
 116 // to declare specializations of Endian<T> as appropriate. 
 118 // Note well that the address of an Endian<T> is not an address-of-T, 
 119 // and there is no conversion available. 
 121 template <class Type
> 
 125     Endian() : mValue(Type(0)) { } 
 126     Endian(Value v
) : mValue((Type
) h2n(v
)) { } 
 128     Type 
get () const                   { return (Type
) n2h(mValue
); } 
 129     operator Value () const             { return this->get(); } 
 130     Endian 
&operator = (Value v
)        { mValue 
= h2n(v
); return *this; } 
 138 }       // end namespace Security