]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
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 <security_utilities/memutils.h> | |
35 | #include <security_utilities/debugging.h> | |
36 | ||
37 | namespace Security { | |
38 | ||
39 | ||
40 | // | |
41 | // Encode/decode operations by type, overloaded. | |
42 | // You can use these functions directly, but consider using | |
43 | // the higher-level constructs below instead. | |
44 | // | |
45 | #ifdef __LP64__ | |
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); } | |
52 | #else | |
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); } | |
59 | #endif | |
60 | ||
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); } | |
67 | ||
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); } | |
74 | ||
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); } | |
81 | ||
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; } | |
88 | ||
89 | ||
90 | // | |
91 | // Flip pointers | |
92 | // | |
93 | template <class Base> | |
94 | static inline Base *h2n(Base *p) { return (Base *)h2n(uintptr_t(p)); } | |
95 | ||
96 | template <class Base> | |
97 | static inline Base *n2h(Base *p) { return (Base *)n2h(uintptr_t(p)); } | |
98 | ||
99 | ||
100 | // | |
101 | // In-place fix operations | |
102 | // | |
103 | template <class Type> | |
104 | static inline void h2ni(Type &v) { v = h2n(v); } | |
105 | ||
106 | template <class Type> | |
107 | static inline void n2hi(Type &v) { v = n2h(v); } | |
108 | ||
109 | // | |
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. | |
114 | // | |
115 | // The generic definition uses h2n/n2h to flip bytes. Feel free | |
116 | // to declare specializations of Endian<T> as appropriate. | |
117 | // | |
118 | // Note well that the address of an Endian<T> is not an address-of-T, | |
119 | // and there is no conversion available. | |
120 | // | |
121 | template <class Type> | |
122 | class Endian { | |
123 | public: | |
124 | typedef Type Value; | |
125 | Endian() : mValue(Type(0)) { } | |
126 | Endian(Value v) : mValue(h2n(v)) { } | |
127 | ||
427c49bc A |
128 | Type get () const { return n2h(mValue); } |
129 | operator Value () const { return this->get(); } | |
b1ab9ed8 | 130 | Endian &operator = (Value v) { mValue = h2n(v); return *this; } |
427c49bc | 131 | |
b1ab9ed8 A |
132 | |
133 | private: | |
134 | Value mValue; | |
135 | }; | |
136 | ||
137 | ||
138 | } // end namespace Security | |
139 | ||
140 | ||
141 | #endif //_H_ENDIAN |