]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/endian.h
Security-179.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / endian.h
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * cssm utilities
21 */
22 #ifndef _H_ENDIAN
23 #define _H_ENDIAN
24
25 #include <Security/utilities.h>
26 #include <Security/memutils.h>
27 #include <Security/debugging.h>
28
29 namespace Security {
30
31
32 //
33 // Encode/decode operations by type, overloaded.
34 // You can use these functions directly, but consider using
35 // the higher-level constructs below instead.
36 //
37 inline uint32 h2n(uint32 v) { return htonl(v); }
38 inline sint32 h2n(sint32 v) { return htonl(v); }
39 inline uint16 h2n(uint16 v) { return htons(v); }
40 inline sint16 h2n(sint16 v) { return htons(v); }
41 inline uint8 h2n(uint8 v) { return v; }
42 inline sint8 h2n(sint8 v) { return v; }
43
44 inline uint32 n2h(uint32 v) { return ntohl(v); }
45 inline sint32 n2h(sint32 v) { return ntohl(v); }
46 inline uint16 n2h(uint16 v) { return ntohs(v); }
47 inline sint16 n2h(sint16 v) { return ntohs(v); }
48 inline uint8 n2h(uint8 v) { return v; }
49 inline sint8 n2h(sint8 v) { return v; }
50
51
52 //
53 // Flip pointers
54 //
55 template <class Base>
56 inline Base *h2n(Base *p) { return (Base *)h2n(LowLevelMemoryUtilities::PointerInt(p)); }
57
58 template <class Base>
59 inline Base *n2h(Base *p) { return (Base *)n2h(LowLevelMemoryUtilities::PointerInt(p)); }
60
61
62 //
63 // Generic template - do nothing, issue debug warning
64 //
65 template <class Type>
66 inline const Type &h2n(const Type &v)
67 {
68 secdebug("endian", "generic h2n called for type %s", Debug::typeName(v).c_str());
69 return v;
70 }
71
72 template <class Type>
73 inline const Type &n2h(const Type &v)
74 {
75 secdebug("endian", "generic n2h called for type %s", Debug::typeName(v).c_str());
76 return v;
77 }
78
79
80 //
81 // In-place fix operations
82 //
83 template <class Type>
84 inline void h2ni(Type &v) { v = h2n(v); }
85
86 template <class Type>
87 inline void n2hi(Type &v) { v = n2h(v); }
88
89
90 //
91 // Some structs we may want swapped in-place
92 //
93 void n2hi(CssmKey::Header &key);
94 void h2ni(CssmKey::Header &key);
95
96 inline void n2hi(CSSM_KEYHEADER &key) { n2hi(CssmKey::Header::overlay (key));}
97 inline void h2ni(CSSM_KEYHEADER &key) { h2ni(CssmKey::Header::overlay (key));}
98
99
100 //
101 // Endian<SomeType> keeps NBO values in memory and converts
102 // during loads and stores. This presumes that you are using
103 // memory blocks thare are read/written/mapped as amorphous byte
104 // streams, but want to be byte-order clean using them.
105 //
106 // The generic definition uses h2n/n2h to flip bytes. Feel free
107 // to declare specializations of Endian<T> as appropriate.
108 //
109 // Note well that the address of an Endian<T> is not an address-of-T,
110 // and there is no conversion available.
111 //
112 template <class Type>
113 class Endian {
114 public:
115 typedef Type Value;
116 Endian() : mValue(0) { }
117 Endian(Value v) : mValue(h2n(v)) { }
118
119 operator Value () const { return n2h(mValue); }
120 Endian &operator = (Value v) { mValue = h2n(v); return *this; }
121
122 private:
123 Value mValue;
124 };
125
126 } // end namespace Security
127
128
129 #endif //_H_ENDIAN