1/*
2 * Copyright (c) 2002-2004,2011-2012,2014 Apple 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
37namespace 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__
46static inline unsigned long h2n(unsigned long v) { return OSSwapHostToBigInt64(v); }
47static inline unsigned long n2h(unsigned long v) { return OSSwapBigToHostInt64(v); }
48static inline unsigned long flip(unsigned long v) { return OSSwapInt64(v); }
49static inline signed long h2n(signed long v) { return OSSwapHostToBigInt64(v); }
50static inline signed long n2h(signed long v) { return OSSwapBigToHostInt64(v); }
51static inline signed long flip(signed long v) { return OSSwapInt64(v); }
52#else
53static inline unsigned long h2n(unsigned long v)	{ return htonl(v); }
54static inline unsigned long n2h(unsigned long v)	{ return ntohl(v); }
55static inline unsigned long flip(unsigned long v)	{ return OSSwapInt32(v); }
56static inline signed long h2n(signed long v)		{ return htonl(v); }
57static inline signed long n2h(signed long v)		{ return ntohl(v); }
58static inline signed long flip(signed long v)		{ return OSSwapInt32(v); }
59#endif
60
61static inline unsigned long long h2n(unsigned long long v) { return OSSwapHostToBigInt64(v); }
62static inline unsigned long long n2h(unsigned long long v) { return OSSwapBigToHostInt64(v); }
63static inline unsigned long long flip(unsigned long long v) { return OSSwapInt64(v); }
64static inline long long h2n(long long v)			{ return OSSwapHostToBigInt64(v); }
65static inline long long n2h(long long v)			{ return OSSwapBigToHostInt64(v); }
66static inline long long flip(long long v)			{ return OSSwapInt64(v); }
67
68static inline unsigned int h2n(unsigned int v)		{ return htonl(v); }
69static inline unsigned int n2h(unsigned int v)		{ return ntohl(v); }
70static inline unsigned int flip(unsigned int v)		{ return OSSwapInt32(v); }
71static inline signed int h2n(int v)					{ return htonl(v); }
72static inline signed int n2h(int v)					{ return ntohl(v); }
73static inline signed int flip(int v)				{ return OSSwapInt32(v); }
74
75static inline unsigned short h2n(unsigned short v)	{ return htons(v); }
76static inline unsigned short n2h(unsigned short v)	{ return ntohs(v); }
77static inline unsigned short flip(unsigned short v)	{ return OSSwapInt16(v); }
78static inline signed short h2n(signed short v)		{ return htons(v); }
79static inline signed short n2h(signed short v)		{ return ntohs(v); }
80static inline signed short flip(signed short v)		{ return OSSwapInt16(v); }
81
82static inline unsigned char h2n(unsigned char v)	{ return v; }
83static inline unsigned char n2h(unsigned char v)	{ return v; }
84static inline unsigned char flip(unsigned char v)	{ return v; }
85static inline signed char h2n(signed char v)		{ return v; }
86static inline signed char n2h(signed char v)		{ return v; }
87static inline signed char flip(signed char v)		{ return v; }
88
89
90//
91// Flip pointers
92//
93template <class Base>
94static inline Base *h2n(Base *p)	{ return (Base *)h2n(uintptr_t(p)); }
95
96template <class Base>
97static inline Base *n2h(Base *p)	{ return (Base *)n2h(uintptr_t(p)); }
98
99
100//
101// In-place fix operations
102//
103template <class Type>
104static inline void h2ni(Type &v)	{ v = h2n(v); }
105
106template <class Type>
107static 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//
121template <class Type>
122class Endian {
123public:
124    typedef Type Value;
125    Endian() : mValue(Type(0)) { }
126    Endian(Value v) : mValue(h2n(v)) { }
127
128    Type get ()	const			{ return n2h(mValue); }
129    operator Value () const		{ return this->get(); }
130    Endian &operator = (Value v)	{ mValue = h2n(v); return *this; }
131
132
133private:
134    Value mValue;
135};
136
137
138}	// end namespace Security
139
140
141#endif //_H_ENDIAN
142