diff options
Diffstat (limited to 'src/microhttpd/mhd_bithelpers.h')
-rw-r--r-- | src/microhttpd/mhd_bithelpers.h | 107 |
1 files changed, 87 insertions, 20 deletions
diff --git a/src/microhttpd/mhd_bithelpers.h b/src/microhttpd/mhd_bithelpers.h index d4a47ce4..bf20293f 100644 --- a/src/microhttpd/mhd_bithelpers.h +++ b/src/microhttpd/mhd_bithelpers.h | |||
@@ -29,32 +29,99 @@ | |||
29 | #include "mhd_byteorder.h" | 29 | #include "mhd_byteorder.h" |
30 | #include <stdint.h> | 30 | #include <stdint.h> |
31 | 31 | ||
32 | 32 | /* _MHD_PUT_64BIT_LE (addr, value64) | |
33 | * put native-endian 64-bit value64 to addr | ||
34 | * in little-endian mode. | ||
35 | */ | ||
33 | #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN | 36 | #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN |
34 | #define _MHD_PUT_64BIT_LE(addr, value64) \ | 37 | #define _MHD_PUT_64BIT_LE(addr, value64) \ |
35 | ((*(uint64_t*)(addr)) = (uint64_t)(value64)) | 38 | ((*(uint64_t*)(addr)) = (uint64_t)(value64)) |
36 | #else /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ | 39 | #else /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ |
37 | #define _MHD_PUT_64BIT_LE(addr, value64) do { \ | 40 | #define _MHD_PUT_64BIT_LE(addr, value64) do { \ |
38 | (uint8_t*(addr))[7] = (uint8_t)((value64) >> 56); \ | 41 | ((uint8_t*)(addr))[0] = (uint8_t)((uint64_t)(value64)); \ |
39 | (uint8_t*(addr))[6] = (uint8_t)((value64) >> 48); \ | 42 | ((uint8_t*)(addr))[1] = (uint8_t)(((uint64_t)(value64)) >> 8); \ |
40 | (uint8_t*(addr))[5] = (uint8_t)((value64) >> 40); \ | 43 | ((uint8_t*)(addr))[2] = (uint8_t)(((uint64_t)(value64)) >> 16); \ |
41 | (uint8_t*(addr))[4] = (uint8_t)((value64) >> 32); \ | 44 | ((uint8_t*)(addr))[3] = (uint8_t)(((uint64_t)(value64)) >> 24); \ |
42 | (uint8_t*(addr))[3] = (uint8_t)((value64) >> 24); \ | 45 | ((uint8_t*)(addr))[4] = (uint8_t)(((uint64_t)(value64)) >> 32); \ |
43 | (uint8_t*(addr))[2] = (uint8_t)((value64) >> 16); \ | 46 | ((uint8_t*)(addr))[5] = (uint8_t)(((uint64_t)(value64)) >> 40); \ |
44 | (uint8_t*(addr))[1] = (uint8_t)((value64) >> 8); \ | 47 | ((uint8_t*)(addr))[6] = (uint8_t)(((uint64_t)(value64)) >> 48); \ |
45 | (uint8_t*(addr))[0] = (uint8_t)((value64)); } while (0) | 48 | ((uint8_t*)(addr))[7] = (uint8_t)(((uint64_t)(value64)) >> 56); \ |
49 | } while (0) | ||
46 | #endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ | 50 | #endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ |
47 | 51 | ||
52 | /* _MHD_PUT_32BIT_LE (addr, value32) | ||
53 | * put native-endian 32-bit value32 to addr | ||
54 | * in little-endian mode. | ||
55 | */ | ||
48 | #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN | 56 | #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN |
49 | #define _MHD_PUT_32BIT_LE(addr, value32) \ | 57 | #define _MHD_PUT_32BIT_LE(addr, value32) \ |
50 | ((*(uint32_t*)(addr)) = (uint32_t)(value32)) | 58 | ((*(uint32_t*)(addr)) = (uint32_t)(value32)) |
51 | #else /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ | 59 | #else /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ |
52 | 60 | #define _MHD_PUT_32BIT_LE(addr, value32) do { \ | |
53 | #define _MHD_PUT_32BIT_LE(addr, value32) do { \ | 61 | ((uint8_t*)(addr))[0] = (uint8_t)((uint32_t)(value32)); \ |
54 | (uint8_t*(addr))[3] = (uint8_t)((value32) >> 24); \ | 62 | ((uint8_t*)(addr))[1] = (uint8_t)(((uint32_t)(value32)) >> 8); \ |
55 | (uint8_t*(addr))[2] = (uint8_t)((value32) >> 16); \ | 63 | ((uint8_t*)(addr))[2] = (uint8_t)(((uint32_t)(value32)) >> 16); \ |
56 | (uint8_t*(addr))[1] = (uint8_t)((value32) >> 8); \ | 64 | ((uint8_t*)(addr))[3] = (uint8_t)(((uint32_t)(value32)) >> 24); \ |
57 | (uint8_t*(addr))[0] = (uint8_t)((value32)); } while (0) | 65 | } while (0) |
58 | #endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ | 66 | #endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */ |
59 | 67 | ||
68 | |||
69 | /* _MHD_PUT_64BIT_BE (addr, value64) | ||
70 | * put native-endian 64-bit value64 to addr | ||
71 | * in big-endian mode. | ||
72 | */ | ||
73 | #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN | ||
74 | #define _MHD_PUT_64BIT_BE(addr, value64) \ | ||
75 | ((*(uint64_t*)(addr)) = (uint64_t)(value64)) | ||
76 | #else /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
77 | #define _MHD_PUT_64BIT_BE(addr, value64) do { \ | ||
78 | ((uint8_t*)(addr))[7] = (uint8_t)((uint64_t)(value64)); \ | ||
79 | ((uint8_t*)(addr))[6] = (uint8_t)(((uint64_t)(value64)) >> 8); \ | ||
80 | ((uint8_t*)(addr))[5] = (uint8_t)(((uint64_t)(value64)) >> 16); \ | ||
81 | ((uint8_t*)(addr))[4] = (uint8_t)(((uint64_t)(value64)) >> 24); \ | ||
82 | ((uint8_t*)(addr))[3] = (uint8_t)(((uint64_t)(value64)) >> 32); \ | ||
83 | ((uint8_t*)(addr))[2] = (uint8_t)(((uint64_t)(value64)) >> 40); \ | ||
84 | ((uint8_t*)(addr))[1] = (uint8_t)(((uint64_t)(value64)) >> 48); \ | ||
85 | ((uint8_t*)(addr))[0] = (uint8_t)(((uint64_t)(value64)) >> 56); \ | ||
86 | } while (0) | ||
87 | #endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
88 | |||
89 | /* _MHD_PUT_32BIT_BE (addr, value32) | ||
90 | * put native-endian 32-bit value32 to addr | ||
91 | * in big-endian mode. | ||
92 | */ | ||
93 | #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN | ||
94 | #define _MHD_PUT_32BIT_BE(addr, value32) \ | ||
95 | ((*(uint32_t*)(addr)) = (uint32_t)(value32)) | ||
96 | #else /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
97 | #define _MHD_PUT_32BIT_BE(addr, value32) do { \ | ||
98 | ((uint8_t*)(addr))[3] = (uint8_t)((uint32_t)(value32)); \ | ||
99 | ((uint8_t*)(addr))[2] = (uint8_t)(((uint32_t)(value32)) >> 8); \ | ||
100 | ((uint8_t*)(addr))[1] = (uint8_t)(((uint32_t)(value32)) >> 16); \ | ||
101 | ((uint8_t*)(addr))[0] = (uint8_t)(((uint32_t)(value32)) >> 24); \ | ||
102 | } while (0) | ||
103 | #endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
104 | |||
105 | /* _MHD_GET_32BIT_BE (addr) | ||
106 | * get big-endian 32-bit value storied at addr | ||
107 | * and return it in native-endian mode. | ||
108 | */ | ||
109 | #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN | ||
110 | #define _MHD_GET_32BIT_BE(addr) \ | ||
111 | (*(uint32_t*)(addr)) | ||
112 | #else /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
113 | #define _MHD_GET_32BIT_BE(addr) \ | ||
114 | ( (((uint32_t)(((uint8_t*)addr)[0])) << 24) | \ | ||
115 | (((uint32_t)(((uint8_t*)addr)[1])) << 16) | \ | ||
116 | (((uint32_t)(((uint8_t*)addr)[2])) << 8) | \ | ||
117 | ((uint32_t) (((uint8_t*)addr)[3])) ) | ||
118 | #endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */ | ||
119 | |||
120 | /** | ||
121 | * Rotate right 32-bit value by number of bits. | ||
122 | * bits parameter must be more than zero and must be less than 32. | ||
123 | * Defined in form which modern compiler could optimize. | ||
124 | */ | ||
125 | #define _MHD_ROTR32(value32, bits) ((value32) >> (bits) | (value32) << (32 - bits)) | ||
126 | |||
60 | #endif /* ! MHD_BITHELPERS_H */ | 127 | #endif /* ! MHD_BITHELPERS_H */ |