libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / include / bcmendian.h
blobbd1b1ef1905a6f8267c1601572def764456c802f
1 /*
2 * local version of endian.h - byte order defines
4 * Copyright 2006, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 * $Id: bcmendian.h,v 1.1.1.10 2006/02/27 03:43:16 honor Exp $
15 #ifndef _BCMENDIAN_H_
16 #define _BCMENDIAN_H_
18 #include <typedefs.h>
20 /* Byte swap a 16 bit value */
21 #define BCMSWAP16(val) \
22 ((uint16)(\
23 (((uint16)(val) & (uint16)0x00ffU) << 8) | \
24 (((uint16)(val) & (uint16)0xff00U) >> 8)))
26 /* Byte swap a 32 bit value */
27 #define BCMSWAP32(val) \
28 ((uint32)(\
29 (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
30 (((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \
31 (((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \
32 (((uint32)(val) & (uint32)0xff000000UL) >> 24)))
34 /* 2 Byte swap a 32 bit value */
35 #define BCMSWAP32BY16(val) \
36 ((uint32)(\
37 (((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \
38 (((uint32)(val) & (uint32)0xffff0000UL) >> 16)))
41 static INLINE uint16
42 bcmswap16(uint16 val)
44 return BCMSWAP16(val);
47 static INLINE uint32
48 bcmswap32(uint32 val)
50 return BCMSWAP32(val);
53 static INLINE uint32
54 bcmswap32by16(uint32 val)
56 return BCMSWAP32BY16(val);
59 /* buf - start of buffer of shorts to swap */
60 /* len - byte length of buffer */
61 static INLINE void
62 bcmswap16_buf(uint16 *buf, uint len)
64 len = len/2;
66 while (len--) {
67 *buf = bcmswap16(*buf);
68 buf++;
72 #ifndef hton16
73 #ifndef IL_BIGENDIAN
74 #define HTON16(i) BCMSWAP16(i)
75 #define hton16(i) bcmswap16(i)
76 #define hton32(i) bcmswap32(i)
77 #define ntoh16(i) bcmswap16(i)
78 #define ntoh32(i) bcmswap32(i)
79 #define ltoh16(i) (i)
80 #define ltoh32(i) (i)
81 #define htol16(i) (i)
82 #define htol32(i) (i)
83 #else
84 #define HTON16(i) (i)
85 #define hton16(i) (i)
86 #define hton32(i) (i)
87 #define ntoh16(i) (i)
88 #define ntoh32(i) (i)
89 #define ltoh16(i) bcmswap16(i)
90 #define ltoh32(i) bcmswap32(i)
91 #define htol16(i) bcmswap16(i)
92 #define htol32(i) bcmswap32(i)
93 #endif /* IL_BIGENDIAN */
94 #endif /* hton16 */
96 #ifndef IL_BIGENDIAN
97 #define ltoh16_buf(buf, i)
98 #define htol16_buf(buf, i)
99 #else
100 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
101 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
102 #endif /* IL_BIGENDIAN */
105 * store 16-bit value to unaligned little endian byte array.
107 static INLINE void
108 htol16_ua_store(uint16 val, uint8 *bytes)
110 bytes[0] = val&0xff;
111 bytes[1] = val>>8;
115 * store 32-bit value to unaligned little endian byte array.
117 static INLINE void
118 htol32_ua_store(uint32 val, uint8 *bytes)
120 bytes[0] = val&0xff;
121 bytes[1] = (val>>8)&0xff;
122 bytes[2] = (val>>16)&0xff;
123 bytes[3] = val>>24;
127 * store 16-bit value to unaligned network(big) endian byte array.
129 static INLINE void
130 hton16_ua_store(uint16 val, uint8 *bytes)
132 bytes[1] = val&0xff;
133 bytes[0] = val>>8;
137 * store 32-bit value to unaligned network(big) endian byte array.
139 static INLINE void
140 hton32_ua_store(uint32 val, uint8 *bytes)
142 bytes[3] = val&0xff;
143 bytes[2] = (val>>8)&0xff;
144 bytes[1] = (val>>16)&0xff;
145 bytes[0] = val>>24;
149 * load 16-bit value from unaligned little endian byte array.
151 static INLINE uint16
152 ltoh16_ua(void *bytes)
154 return (((uint8*)bytes)[1]<<8)+((uint8 *)bytes)[0];
158 * load 32-bit value from unaligned little endian byte array.
160 static INLINE uint32
161 ltoh32_ua(void *bytes)
163 return (((uint8*)bytes)[3]<<24)+(((uint8*)bytes)[2]<<16)+
164 (((uint8*)bytes)[1]<<8)+((uint8*)bytes)[0];
168 * load 16-bit value from unaligned big(network) endian byte array.
170 static INLINE uint16
171 ntoh16_ua(void *bytes)
173 return (((uint8*)bytes)[0]<<8)+((uint8*)bytes)[1];
177 * load 32-bit value from unaligned big(network) endian byte array.
179 static INLINE uint32
180 ntoh32_ua(void *bytes)
182 return (((uint8*)bytes)[0]<<24)+(((uint8*)bytes)[1]<<16)+
183 (((uint8*)bytes)[2]<<8)+((uint8*)bytes)[3];
186 #define ltoh_ua(ptr) (\
187 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
188 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] : \
189 (((uint8 *)ptr)[3]<<24)+(((uint8 *)ptr)[2]<<16)+(((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] \
192 #define ntoh_ua(ptr) (\
193 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
194 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[0]<<8)+((uint8 *)ptr)[1] : \
195 (((uint8 *)ptr)[0]<<24)+(((uint8 *)ptr)[1]<<16)+(((uint8 *)ptr)[2]<<8)+((uint8 *)ptr)[3] \
198 #endif /* _BCMENDIAN_H_ */