| 1 | /*- | ||
| 2 | * Copyright 2007-2014 Colin Percival | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * | ||
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 24 | * SUCH DAMAGE. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #ifndef _SYSENDIAN_H_ | ||
| 28 | #define _SYSENDIAN_H_ | ||
| 29 | |||
| 30 | #include <stdint.h> | ||
| 31 | |||
| 32 | /* Avoid namespace collisions with BSD <sys/endian.h>. */ | ||
| 33 | #define be32dec libcperciva_be32dec | ||
| 34 | #define be32enc libcperciva_be32enc | ||
| 35 | #define be64enc libcperciva_be64enc | ||
| 36 | #define le32dec libcperciva_le32dec | ||
| 37 | #define le32enc libcperciva_le32enc | ||
| 38 | #define le64dec libcperciva_le64dec | ||
| 39 | #define le64enc libcperciva_le64enc | ||
| 40 | |||
| 41 | static inline uint32_t | ||
| 42 | 120304 | be32dec(const void * pp) | |
| 43 | { | ||
| 44 | 120304 | const uint8_t * p = (uint8_t const *)pp; | |
| 45 | |||
| 46 | 120304 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + | |
| 47 | 120304 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); | |
| 48 | } | ||
| 49 | |||
| 50 | static inline void | ||
| 51 | 49864 | be32enc(void * pp, uint32_t x) | |
| 52 | { | ||
| 53 | 49864 | uint8_t * p = (uint8_t *)pp; | |
| 54 | |||
| 55 | 49864 | p[3] = x & 0xff; | |
| 56 | 49864 | p[2] = (x >> 8) & 0xff; | |
| 57 | 49864 | p[1] = (x >> 16) & 0xff; | |
| 58 | 49864 | p[0] = (x >> 24) & 0xff; | |
| 59 | 49864 | } | |
| 60 | |||
| 61 | static inline void | ||
| 62 | 243 | be64enc(void * pp, uint64_t x) | |
| 63 | { | ||
| 64 | 243 | uint8_t * p = (uint8_t *)pp; | |
| 65 | |||
| 66 | 243 | p[7] = x & 0xff; | |
| 67 | 243 | p[6] = (x >> 8) & 0xff; | |
| 68 | 243 | p[5] = (x >> 16) & 0xff; | |
| 69 | 243 | p[4] = (x >> 24) & 0xff; | |
| 70 | 243 | p[3] = (x >> 32) & 0xff; | |
| 71 | 243 | p[2] = (x >> 40) & 0xff; | |
| 72 | 243 | p[1] = (x >> 48) & 0xff; | |
| 73 | 243 | p[0] = (x >> 56) & 0xff; | |
| 74 | 243 | } | |
| 75 | |||
| 76 | static inline uint32_t | ||
| 77 | le32dec(const void * pp) | ||
| 78 | { | ||
| 79 | const uint8_t * p = (uint8_t const *)pp; | ||
| 80 | |||
| 81 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + | ||
| 82 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); | ||
| 83 | } | ||
| 84 | |||
| 85 | static inline void | ||
| 86 | le32enc(void * pp, uint32_t x) | ||
| 87 | { | ||
| 88 | uint8_t * p = (uint8_t *)pp; | ||
| 89 | |||
| 90 | p[0] = x & 0xff; | ||
| 91 | p[1] = (x >> 8) & 0xff; | ||
| 92 | p[2] = (x >> 16) & 0xff; | ||
| 93 | p[3] = (x >> 24) & 0xff; | ||
| 94 | } | ||
| 95 | |||
| 96 | static inline uint64_t | ||
| 97 | le64dec(const void * pp) | ||
| 98 | { | ||
| 99 | const uint8_t * p = (uint8_t const *)pp; | ||
| 100 | |||
| 101 | return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) + | ||
| 102 | ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) + | ||
| 103 | ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) + | ||
| 104 | ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56)); | ||
| 105 | } | ||
| 106 | |||
| 107 | static inline void | ||
| 108 | le64enc(void * pp, uint64_t x) | ||
| 109 | { | ||
| 110 | uint8_t * p = (uint8_t *)pp; | ||
| 111 | |||
| 112 | p[0] = x & 0xff; | ||
| 113 | p[1] = (x >> 8) & 0xff; | ||
| 114 | p[2] = (x >> 16) & 0xff; | ||
| 115 | p[3] = (x >> 24) & 0xff; | ||
| 116 | p[4] = (x >> 32) & 0xff; | ||
| 117 | p[5] = (x >> 40) & 0xff; | ||
| 118 | p[6] = (x >> 48) & 0xff; | ||
| 119 | p[7] = (x >> 56) & 0xff; | ||
| 120 | } | ||
| 121 | |||
| 122 | #endif /* !_SYSENDIAN_H_ */ |