]> git.saurik.com Git - apple/ld64.git/blame - src/ld/code-sign-blobs/endian.h
ld64-409.12.tar.gz
[apple/ld64.git] / src / ld / code-sign-blobs / endian.h
CommitLineData
ebf6f434
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 "memutils.h"
35
36namespace 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__
45static inline unsigned long h2n(unsigned long v) { return OSSwapHostToBigInt64(v); }
46static inline unsigned long n2h(unsigned long v) { return OSSwapBigToHostInt64(v); }
47static inline unsigned long flip(unsigned long v) { return OSSwapInt64(v); }
48static inline signed long h2n(signed long v) { return OSSwapHostToBigInt64(v); }
49static inline signed long n2h(signed long v) { return OSSwapBigToHostInt64(v); }
50static inline signed long flip(signed long v) { return OSSwapInt64(v); }
51#else
52static inline unsigned long h2n(unsigned long v) { return htonl(v); }
53static inline unsigned long n2h(unsigned long v) { return ntohl(v); }
54static inline unsigned long flip(unsigned long v) { return OSSwapInt32(v); }
55static inline signed long h2n(signed long v) { return htonl(v); }
56static inline signed long n2h(signed long v) { return ntohl(v); }
57static inline signed long flip(signed long v) { return OSSwapInt32(v); }
58#endif
59
60static inline unsigned long long h2n(unsigned long long v) { return OSSwapHostToBigInt64(v); }
61static inline unsigned long long n2h(unsigned long long v) { return OSSwapBigToHostInt64(v); }
62static inline unsigned long long flip(unsigned long long v) { return OSSwapInt64(v); }
63static inline long long h2n(long long v) { return OSSwapHostToBigInt64(v); }
64static inline long long n2h(long long v) { return OSSwapBigToHostInt64(v); }
65static inline long long flip(long long v) { return OSSwapInt64(v); }
66
67static inline unsigned int h2n(unsigned int v) { return htonl(v); }
68static inline unsigned int n2h(unsigned int v) { return ntohl(v); }
69static inline unsigned int flip(unsigned int v) { return OSSwapInt32(v); }
70static inline signed int h2n(int v) { return htonl(v); }
71static inline signed int n2h(int v) { return ntohl(v); }
72static inline signed int flip(int v) { return OSSwapInt32(v); }
73
74static inline unsigned short h2n(unsigned short v) { return htons(v); }
75static inline unsigned short n2h(unsigned short v) { return ntohs(v); }
76static inline unsigned short flip(unsigned short v) { return OSSwapInt16(v); }
77static inline signed short h2n(signed short v) { return htons(v); }
78static inline signed short n2h(signed short v) { return ntohs(v); }
79static inline signed short flip(signed short v) { return OSSwapInt16(v); }
80
81static inline unsigned char h2n(unsigned char v) { return v; }
82static inline unsigned char n2h(unsigned char v) { return v; }
83static inline unsigned char flip(unsigned char v) { return v; }
84static inline signed char h2n(signed char v) { return v; }
85static inline signed char n2h(signed char v) { return v; }
86static inline signed char flip(signed char v) { return v; }
87
88
89//
90// Flip pointers
91//
92template <class Base>
93static inline Base *h2n(Base *p) { return (Base *)h2n(uintptr_t(p)); }
94
95template <class Base>
96static inline Base *n2h(Base *p) { return (Base *)n2h(uintptr_t(p)); }
97
98
99//
100// In-place fix operations
101//
102template <class Type>
103static inline void h2ni(Type &v) { v = h2n(v); }
104
105template <class Type>
106static 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//
120template <class Type>
121class Endian {
122public:
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
130private:
131 Value mValue;
132};
133
134
135} // end namespace Security
136
137
138#endif //_H_ENDIAN