1 /*
2  * Copyright (c) 2011 Apple Inc. All rights reserved.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20 
21 //
22 //  EndianPortable.c
23 //
24 //  Copyright 2011 Apple Inc. All rights reserved.
25 //
26 
27 #include <stdio.h>
28 #include "EndianPortable.h"
29 
30 #define BSWAP16(x) (((x << 8) | ((x >> 8) & 0x00ff)))
31 #define BSWAP32(x) (((x << 24) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff)))
32 #define BSWAP64(x) ((((int64_t)x << 56) | (((int64_t)x << 40) & 0x00ff000000000000LL) | \
33                     (((int64_t)x << 24) & 0x0000ff0000000000LL) | (((int64_t)x << 8) & 0x000000ff00000000LL) | \
34                     (((int64_t)x >> 8) & 0x00000000ff000000LL) | (((int64_t)x >> 24) & 0x0000000000ff0000LL) | \
35                     (((int64_t)x >> 40) & 0x000000000000ff00LL) | (((int64_t)x >> 56) & 0x00000000000000ffLL)))
36 
37 #if defined(__i386__)
38 #define TARGET_RT_LITTLE_ENDIAN 1
39 #elif defined(__x86_64__)
40 #define TARGET_RT_LITTLE_ENDIAN 1
41 #elif defined (TARGET_OS_WIN32)
42 #define TARGET_RT_LITTLE_ENDIAN 1
43 #endif
44 
Swap16NtoB(uint16_t inUInt16)45 uint16_t Swap16NtoB(uint16_t inUInt16)
46 {
47 #if TARGET_RT_LITTLE_ENDIAN
48     return BSWAP16(inUInt16);
49 #else
50     return inUInt16;
51 #endif
52 }
53 
Swap16BtoN(uint16_t inUInt16)54 uint16_t Swap16BtoN(uint16_t inUInt16)
55 {
56 #if TARGET_RT_LITTLE_ENDIAN
57     return BSWAP16(inUInt16);
58 #else
59     return inUInt16;
60 #endif
61 }
62 
Swap32NtoB(uint32_t inUInt32)63 uint32_t Swap32NtoB(uint32_t inUInt32)
64 {
65 #if TARGET_RT_LITTLE_ENDIAN
66     return BSWAP32(inUInt32);
67 #else
68     return inUInt32;
69 #endif
70 }
71 
Swap32BtoN(uint32_t inUInt32)72 uint32_t Swap32BtoN(uint32_t inUInt32)
73 {
74 #if TARGET_RT_LITTLE_ENDIAN
75     return BSWAP32(inUInt32);
76 #else
77     return inUInt32;
78 #endif
79 }
80 
Swap64BtoN(uint64_t inUInt64)81 uint64_t Swap64BtoN(uint64_t inUInt64)
82 {
83 #if TARGET_RT_LITTLE_ENDIAN
84     return BSWAP64(inUInt64);
85 #else
86     return inUInt64;
87 #endif
88 }
89 
Swap64NtoB(uint64_t inUInt64)90 uint64_t Swap64NtoB(uint64_t inUInt64)
91 {
92 #if TARGET_RT_LITTLE_ENDIAN
93     return BSWAP64(inUInt64);
94 #else
95     return inUInt64;
96 #endif
97 }
98 
SwapFloat32BtoN(float in)99 float SwapFloat32BtoN(float in)
100 {
101 #if TARGET_RT_LITTLE_ENDIAN
102 	union {
103 		float f;
104 		int32_t i;
105 	} x;
106 	x.f = in;
107 	x.i = BSWAP32(x.i);
108 	return x.f;
109 #else
110 	return in;
111 #endif
112 }
113 
SwapFloat32NtoB(float in)114 float SwapFloat32NtoB(float in)
115 {
116 #if TARGET_RT_LITTLE_ENDIAN
117 	union {
118 		float f;
119 		int32_t i;
120 	} x;
121 	x.f = in;
122 	x.i = BSWAP32(x.i);
123 	return x.f;
124 #else
125 	return in;
126 #endif
127 }
128 
SwapFloat64BtoN(double in)129 double SwapFloat64BtoN(double in)
130 {
131 #if TARGET_RT_LITTLE_ENDIAN
132 	union {
133 		double f;
134 		int64_t i;
135 	} x;
136 	x.f = in;
137 	x.i = BSWAP64(x.i);
138 	return x.f;
139 #else
140 	return in;
141 #endif
142 }
143 
SwapFloat64NtoB(double in)144 double SwapFloat64NtoB(double in)
145 {
146 #if TARGET_RT_LITTLE_ENDIAN
147 	union {
148 		double f;
149 		int64_t i;
150 	} x;
151 	x.f = in;
152 	x.i = BSWAP64(x.i);
153 	return x.f;
154 #else
155 	return in;
156 #endif
157 }
158 
Swap16(uint16_t * inUInt16)159 void Swap16(uint16_t * inUInt16)
160 {
161 	*inUInt16 = BSWAP16(*inUInt16);
162 }
163 
Swap24(uint8_t * inUInt24)164 void Swap24(uint8_t * inUInt24)
165 {
166 	uint8_t tempVal = inUInt24[0];
167 	inUInt24[0] = inUInt24[2];
168 	inUInt24[2] = tempVal;
169 }
170 
Swap32(uint32_t * inUInt32)171 void Swap32(uint32_t * inUInt32)
172 {
173 	*inUInt32 = BSWAP32(*inUInt32);
174 }
175 
176