]> git.saurik.com Git - apple/ld64.git/blob - src/ld/code-sign-blobs/endian.h
f312e43daecd9254d65721627fdcd360f711e6d1
[apple/ld64.git] / src / ld / code-sign-blobs / endian.h
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 /*
26 * cssm utilities
27 */
28 #ifndef _H_ENDIAN
29 #define _H_ENDIAN
30
31 #include <machine/endian.h>
32 #include <libkern/OSByteOrder.h>
33 //#include <security_utilities/utilities.h>
34 #include "memutils.h"
35
36 namespace Security {
37
38
39 //
40 // Encode/decode operations by type, overloaded.
41 // You can use these functions directly, but consider using
42 // the higher-level constructs below instead.
43 //
44 #ifdef __LP64__
45 static inline unsigned long h2n(unsigned long v) { return OSSwapHostToBigInt64(v); }
46 static inline unsigned long n2h(unsigned long v) { return OSSwapBigToHostInt64(v); }
47 static inline unsigned long flip(unsigned long v) { return OSSwapInt64(v); }
48 static inline signed long h2n(signed long v) { return OSSwapHostToBigInt64(v); }
49 static inline signed long n2h(signed long v) { return OSSwapBigToHostInt64(v); }
50 static inline signed long flip(signed long v) { return OSSwapInt64(v); }
51 #else
52 static inline unsigned long h2n(unsigned long v) { return htonl(v); }
53 static inline unsigned long n2h(unsigned long v) { return ntohl(v); }
54 static inline unsigned long flip(unsigned long v) { return OSSwapInt32(v); }
55 static inline signed long h2n(signed long v) { return htonl(v); }
56 static inline signed long n2h(signed long v) { return ntohl(v); }
57 static inline signed long flip(signed long v) { return OSSwapInt32(v); }
58 #endif
59
60 static inline unsigned long long h2n(unsigned long long v) { return OSSwapHostToBigInt64(v); }
61 static inline unsigned long long n2h(unsigned long long v) { return OSSwapBigToHostInt64(v); }
62 static inline unsigned long long flip(unsigned long long v) { return OSSwapInt64(v); }
63 static inline long long h2n(long long v) { return OSSwapHostToBigInt64(v); }
64 static inline long long n2h(long long v) { return OSSwapBigToHostInt64(v); }
65 static inline long long flip(long long v) { return OSSwapInt64(v); }
66
67 static inline unsigned int h2n(unsigned int v) { return htonl(v); }
68 static inline unsigned int n2h(unsigned int v) { return ntohl(v); }
69 static inline unsigned int flip(unsigned int v) { return OSSwapInt32(v); }
70 static inline signed int h2n(int v) { return htonl(v); }
71 static inline signed int n2h(int v) { return ntohl(v); }
72 static inline signed int flip(int v) { return OSSwapInt32(v); }
73
74 static inline unsigned short h2n(unsigned short v) { return htons(v); }
75 static inline unsigned short n2h(unsigned short v) { return ntohs(v); }
76 static inline unsigned short flip(unsigned short v) { return OSSwapInt16(v); }
77 static inline signed short h2n(signed short v) { return htons(v); }
78 static inline signed short n2h(signed short v) { return ntohs(v); }
79 static inline signed short flip(signed short v) { return OSSwapInt16(v); }
80
81 static inline unsigned char h2n(unsigned char v) { return v; }
82 static inline unsigned char n2h(unsigned char v) { return v; }
83 static inline unsigned char flip(unsigned char v) { return v; }
84 static inline signed char h2n(signed char v) { return v; }
85 static inline signed char n2h(signed char v) { return v; }
86 static inline signed char flip(signed char v) { return v; }
87
88
89 //
90 // Flip pointers
91 //
92 template <class Base>
93 static inline Base *h2n(Base *p) { return (Base *)h2n(uintptr_t(p)); }
94
95 template <class Base>
96 static inline Base *n2h(Base *p) { return (Base *)n2h(uintptr_t(p)); }
97
98
99 //
100 // In-place fix operations
101 //
102 template <class Type>
103 static inline void h2ni(Type &v) { v = h2n(v); }
104
105 template <class Type>
106 static inline void n2hi(Type &v) { v = n2h(v); }
107
108 //
109 // Endian<SomeType> keeps NBO values in memory and converts
110 // during loads and stores. This presumes that you are using
111 // memory blocks thare are read/written/mapped as amorphous byte
112 // streams, but want to be byte-order clean using them.
113 //
114 // The generic definition uses h2n/n2h to flip bytes. Feel free
115 // to declare specializations of Endian<T> as appropriate.
116 //
117 // Note well that the address of an Endian<T> is not an address-of-T,
118 // and there is no conversion available.
119 //
120 template <class Type>
121 class Endian {
122 public:
123 typedef Type Value;
124 Endian() : mValue(Type(0)) { }
125 Endian(Value v) : mValue(h2n(v)) { }
126
127 operator Value () const { return n2h(mValue); }
128 Endian &operator = (Value v) { mValue = h2n(v); return *this; }
129
130 private:
131 Value mValue;
132 };
133
134
135 } // end namespace Security
136
137
138 #endif //_H_ENDIAN