]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/endian.h
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
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
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.
25 #include <Security/utilities.h>
26 #include <Security/memutils.h>
27 #include <Security/debugging.h>
33 // Encode/decode operations by type, overloaded.
34 // You can use these functions directly, but consider using
35 // the higher-level constructs below instead.
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
; }
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
; }
56 inline Base
*h2n(Base
*p
) { return (Base
*)h2n(LowLevelMemoryUtilities::PointerInt(p
)); }
59 inline Base
*n2h(Base
*p
) { return (Base
*)n2h(LowLevelMemoryUtilities::PointerInt(p
)); }
63 // Generic template - do nothing, issue debug warning
66 inline const Type
&h2n(const Type
&v
)
68 secdebug("endian", "generic h2n called for type %s", Debug::typeName(v
).c_str());
73 inline const Type
&n2h(const Type
&v
)
75 secdebug("endian", "generic n2h called for type %s", Debug::typeName(v
).c_str());
81 // In-place fix operations
84 inline void h2ni(Type
&v
) { v
= h2n(v
); }
87 inline void n2hi(Type
&v
) { v
= n2h(v
); }
91 // Some structs we may want swapped in-place
93 void n2hi(CssmKey::Header
&key
);
94 void h2ni(CssmKey::Header
&key
);
96 inline void n2hi(CSSM_KEYHEADER
&key
) { n2hi(CssmKey::Header::overlay (key
));}
97 inline void h2ni(CSSM_KEYHEADER
&key
) { h2ni(CssmKey::Header::overlay (key
));}
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.
106 // The generic definition uses h2n/n2h to flip bytes. Feel free
107 // to declare specializations of Endian<T> as appropriate.
109 // Note well that the address of an Endian<T> is not an address-of-T,
110 // and there is no conversion available.
112 template <class Type
>
116 Endian() : mValue(0) { }
117 Endian(Value v
) : mValue(h2n(v
)) { }
119 operator Value () const { return n2h(mValue
); }
120 Endian
&operator = (Value v
) { mValue
= h2n(v
); return *this; }
126 } // end namespace Security