| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /*- | 
|  | 2 | * Copyright (c) 1997 Niklas Hallqvist.  All rights reserved. | 
|  | 3 | * | 
|  | 4 | * Redistribution and use in source and binary forms, with or without | 
|  | 5 | * modification, are permitted provided that the following conditions | 
|  | 6 | * are met: | 
|  | 7 | * 1. Redistributions of source code must retain the above copyright | 
|  | 8 | *    notice, this list of conditions and the following disclaimer. | 
|  | 9 | * 2. Redistributions in binary form must reproduce the above copyright | 
|  | 10 | *    notice, this list of conditions and the following disclaimer in the | 
|  | 11 | *    documentation and/or other materials provided with the distribution. | 
|  | 12 | * | 
|  | 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|  | 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|  | 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|  | 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|  | 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|  | 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|  | 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|  | 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|  | 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|  | 23 | */ | 
|  | 24 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 25 | #ifndef _SYS_ENDIAN_H_ | 
|  | 26 | #define _SYS_ENDIAN_H_ | 
|  | 27 |  | 
|  | 28 | #include <sys/cdefs.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 |  | 
| Elliott Hughes | 135b103 | 2014-05-13 18:42:12 -0700 | [diff] [blame] | 30 | #include <stdint.h> | 
|  | 31 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | #define _LITTLE_ENDIAN	1234 | 
|  | 33 | #define _BIG_ENDIAN	4321 | 
|  | 34 | #define _PDP_ENDIAN	3412 | 
| Elliott Hughes | 56e0173 | 2014-12-08 20:32:11 -0800 | [diff] [blame] | 35 | #define _BYTE_ORDER _LITTLE_ENDIAN | 
|  | 36 | #define __LITTLE_ENDIAN_BITFIELD | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 |  | 
| Elliott Hughes | 56e0173 | 2014-12-08 20:32:11 -0800 | [diff] [blame] | 38 | #ifndef __LITTLE_ENDIAN | 
|  | 39 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN | 
|  | 40 | #endif | 
|  | 41 | #ifndef __BIG_ENDIAN | 
|  | 42 | #define __BIG_ENDIAN _BIG_ENDIAN | 
|  | 43 | #endif | 
|  | 44 | #define __BYTE_ORDER _BYTE_ORDER | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 |  | 
| Elliott Hughes | 56e0173 | 2014-12-08 20:32:11 -0800 | [diff] [blame] | 46 | #define __swap16 __builtin_bswap16 | 
|  | 47 | #define __swap32 __builtin_bswap32 | 
|  | 48 | #define __swap64 __builtin_bswap64 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 |  | 
| Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 50 | /* glibc compatibility. */ | 
|  | 51 | __BEGIN_DECLS | 
| Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 52 | uint32_t htonl(uint32_t) __attribute_const__ __INTRODUCED_IN(21); | 
|  | 53 | uint16_t htons(uint16_t) __attribute_const__ __INTRODUCED_IN(21); | 
|  | 54 | uint32_t ntohl(uint32_t) __attribute_const__ __INTRODUCED_IN(21); | 
|  | 55 | uint16_t ntohs(uint16_t) __attribute_const__ __INTRODUCED_IN(21); | 
| Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 56 | __END_DECLS | 
|  | 57 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | #define htonl(x) __swap32(x) | 
| Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 59 | #define htons(x) __swap16(x) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | #define ntohl(x) __swap32(x) | 
| Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 61 | #define ntohs(x) __swap16(x) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 |  | 
|  | 63 | /* Bionic additions */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | #define htonq(x) __swap64(x) | 
| Elliott Hughes | 6a41b0f | 2014-05-13 16:05:51 -0700 | [diff] [blame] | 65 | #define ntohq(x) __swap64(x) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 |  | 
| Elliott Hughes | 9c8d711 | 2016-06-13 13:23:42 -0700 | [diff] [blame] | 67 | #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ | 
| Elliott Hughes | 824f914 | 2014-12-09 19:44:42 -0800 | [diff] [blame] | 68 | #define LITTLE_ENDIAN _LITTLE_ENDIAN | 
|  | 69 | #define BIG_ENDIAN _BIG_ENDIAN | 
|  | 70 | #define PDP_ENDIAN _PDP_ENDIAN | 
|  | 71 | #define BYTE_ORDER _BYTE_ORDER | 
|  | 72 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | #define	NTOHL(x) (x) = ntohl((u_int32_t)(x)) | 
|  | 74 | #define	NTOHS(x) (x) = ntohs((u_int16_t)(x)) | 
|  | 75 | #define	HTONL(x) (x) = htonl((u_int32_t)(x)) | 
|  | 76 | #define	HTONS(x) (x) = htons((u_int16_t)(x)) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 |  | 
| Elliott Hughes | 824f914 | 2014-12-09 19:44:42 -0800 | [diff] [blame] | 78 | #define htobe16 __swap16 | 
|  | 79 | #define htobe32 __swap32 | 
|  | 80 | #define htobe64 __swap64 | 
|  | 81 | #define betoh16 __swap16 | 
|  | 82 | #define betoh32 __swap32 | 
|  | 83 | #define betoh64 __swap64 | 
|  | 84 |  | 
|  | 85 | #define htole16(x) (x) | 
|  | 86 | #define htole32(x) (x) | 
|  | 87 | #define htole64(x) (x) | 
|  | 88 | #define letoh16(x) (x) | 
|  | 89 | #define letoh32(x) (x) | 
|  | 90 | #define letoh64(x) (x) | 
|  | 91 |  | 
| Elliott Hughes | cf820d7 | 2013-02-22 11:01:17 -0800 | [diff] [blame] | 92 | /* | 
|  | 93 | * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX. | 
|  | 94 | * The BSDs export both sets of names, bionic historically only | 
|  | 95 | * exported the ones above (or on the rhs here), and glibc only | 
|  | 96 | * exports these names (on the lhs). | 
|  | 97 | */ | 
|  | 98 | #define be16toh(x) htobe16(x) | 
|  | 99 | #define be32toh(x) htobe32(x) | 
|  | 100 | #define be64toh(x) htobe64(x) | 
|  | 101 | #define le16toh(x) htole16(x) | 
|  | 102 | #define le32toh(x) htole32(x) | 
|  | 103 | #define le64toh(x) htole64(x) | 
| Elliott Hughes | 3ba55f8 | 2016-06-08 18:11:23 -0700 | [diff] [blame] | 104 | #endif /* __USE_BSD */ | 
| Elliott Hughes | cf820d7 | 2013-02-22 11:01:17 -0800 | [diff] [blame] | 105 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | #endif /* _SYS_ENDIAN_H_ */ |