common.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <errno.h>
  2. #include <limits.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #if !defined(__unix__) && (defined(__APPLE__) || defined(__linux__))
  8. #define __unix__ 1
  9. #endif
  10. #ifndef __GNUC__
  11. #define __restrict__
  12. #endif
  13. #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  14. __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  15. #define NATIVE_BIG_ENDIAN
  16. #endif
  17. #ifndef NATIVE_BIG_ENDIAN
  18. #ifndef NATIVE_LITTLE_ENDIAN
  19. #define NATIVE_LITTLE_ENDIAN
  20. #endif
  21. #endif
  22. #ifndef TLS
  23. #if defined(_WIN32) && !defined(__GNUC__)
  24. #define TLS __declspec(thread)
  25. #elif (defined(__clang__) || defined(__GNUC__)) && defined(__unix__)
  26. #define TLS __thread
  27. #else
  28. #define TLS
  29. #endif
  30. #endif
  31. #ifndef SIZE_MAX
  32. #define SIZE_MAX ((size_t) -1)
  33. #endif
  34. #ifdef __OpenBSD__
  35. #define HAVE_EXPLICIT_BZERO 1
  36. #elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) && defined(_GNU_SOURCE)
  37. #if __GLIBC_PREREQ(2, 25)
  38. #define HAVE_EXPLICIT_BZERO 1
  39. #endif
  40. #endif
  41. #define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
  42. #define ROTL32(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
  43. #define ROTL64(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
  44. #define ROTR32(x, b) (uint32_t)(((x) >> (b)) | ((x) << (32 - (b))))
  45. #define ROTR64(x, b) (uint64_t)(((x) >> (b)) | ((x) << (64 - (b))))
  46. #define LOAD64_LE(SRC) load64_le(SRC)
  47. static inline uint64_t
  48. load64_le(const uint8_t src[8])
  49. {
  50. #ifdef NATIVE_LITTLE_ENDIAN
  51. uint64_t w;
  52. memcpy(&w, src, sizeof w);
  53. return w;
  54. #else
  55. uint64_t w = (uint64_t) src[0];
  56. w |= (uint64_t) src[1] << 8;
  57. w |= (uint64_t) src[2] << 16;
  58. w |= (uint64_t) src[3] << 24;
  59. w |= (uint64_t) src[4] << 32;
  60. w |= (uint64_t) src[5] << 40;
  61. w |= (uint64_t) src[6] << 48;
  62. w |= (uint64_t) src[7] << 56;
  63. return w;
  64. #endif
  65. }
  66. #define STORE64_LE(DST, W) store64_le((DST), (W))
  67. static inline void
  68. store64_le(uint8_t dst[8], uint64_t w)
  69. {
  70. #ifdef NATIVE_LITTLE_ENDIAN
  71. memcpy(dst, &w, sizeof w);
  72. #else
  73. dst[0] = (uint8_t) w;
  74. w >>= 8;
  75. dst[1] = (uint8_t) w;
  76. w >>= 8;
  77. dst[2] = (uint8_t) w;
  78. w >>= 8;
  79. dst[3] = (uint8_t) w;
  80. w >>= 8;
  81. dst[4] = (uint8_t) w;
  82. w >>= 8;
  83. dst[5] = (uint8_t) w;
  84. w >>= 8;
  85. dst[6] = (uint8_t) w;
  86. w >>= 8;
  87. dst[7] = (uint8_t) w;
  88. #endif
  89. }
  90. #define LOAD32_LE(SRC) load32_le(SRC)
  91. static inline uint32_t
  92. load32_le(const uint8_t src[4])
  93. {
  94. #ifdef NATIVE_LITTLE_ENDIAN
  95. uint32_t w;
  96. memcpy(&w, src, sizeof w);
  97. return w;
  98. #else
  99. uint32_t w = (uint32_t) src[0];
  100. w |= (uint32_t) src[1] << 8;
  101. w |= (uint32_t) src[2] << 16;
  102. w |= (uint32_t) src[3] << 24;
  103. return w;
  104. #endif
  105. }
  106. #define STORE32_LE(DST, W) store32_le((DST), (W))
  107. static inline void
  108. store32_le(uint8_t dst[4], uint32_t w)
  109. {
  110. #ifdef NATIVE_LITTLE_ENDIAN
  111. memcpy(dst, &w, sizeof w);
  112. #else
  113. dst[0] = (uint8_t) w;
  114. w >>= 8;
  115. dst[1] = (uint8_t) w;
  116. w >>= 8;
  117. dst[2] = (uint8_t) w;
  118. w >>= 8;
  119. dst[3] = (uint8_t) w;
  120. #endif
  121. }
  122. #define LOAD16_LE(SRC) load16_le(SRC)
  123. static inline uint16_t
  124. load16_le(const uint8_t src[2])
  125. {
  126. #ifdef NATIVE_LITTLE_ENDIAN
  127. uint16_t w;
  128. memcpy(&w, src, sizeof w);
  129. return w;
  130. #else
  131. uint16_t w = (uint16_t) src[0];
  132. w |= (uint16_t) src[1] << 8;
  133. return w;
  134. #endif
  135. }
  136. #define STORE16_LE(DST, W) store16_le((DST), (W))
  137. static inline void
  138. store16_le(uint8_t dst[2], uint16_t w)
  139. {
  140. #ifdef NATIVE_LITTLE_ENDIAN
  141. memcpy(dst, &w, sizeof w);
  142. #else
  143. dst[0] = (uint8_t) w;
  144. w >>= 8;
  145. dst[1] = (uint8_t) w;
  146. #endif
  147. }
  148. /* ----- */
  149. #define LOAD64_BE(SRC) load64_be(SRC)
  150. static inline uint64_t
  151. load64_be(const uint8_t src[8])
  152. {
  153. #ifdef NATIVE_BIG_ENDIAN
  154. uint64_t w;
  155. memcpy(&w, src, sizeof w);
  156. return w;
  157. #else
  158. uint64_t w = (uint64_t) src[7];
  159. w |= (uint64_t) src[6] << 8;
  160. w |= (uint64_t) src[5] << 16;
  161. w |= (uint64_t) src[4] << 24;
  162. w |= (uint64_t) src[3] << 32;
  163. w |= (uint64_t) src[2] << 40;
  164. w |= (uint64_t) src[1] << 48;
  165. w |= (uint64_t) src[0] << 56;
  166. return w;
  167. #endif
  168. }
  169. #define STORE64_BE(DST, W) store64_be((DST), (W))
  170. static inline void
  171. store64_be(uint8_t dst[8], uint64_t w)
  172. {
  173. #ifdef NATIVE_BIG_ENDIAN
  174. memcpy(dst, &w, sizeof w);
  175. #else
  176. dst[7] = (uint8_t) w;
  177. w >>= 8;
  178. dst[6] = (uint8_t) w;
  179. w >>= 8;
  180. dst[5] = (uint8_t) w;
  181. w >>= 8;
  182. dst[4] = (uint8_t) w;
  183. w >>= 8;
  184. dst[3] = (uint8_t) w;
  185. w >>= 8;
  186. dst[2] = (uint8_t) w;
  187. w >>= 8;
  188. dst[1] = (uint8_t) w;
  189. w >>= 8;
  190. dst[0] = (uint8_t) w;
  191. #endif
  192. }
  193. #define LOAD32_BE(SRC) load32_be(SRC)
  194. static inline uint32_t
  195. load32_be(const uint8_t src[4])
  196. {
  197. #ifdef NATIVE_BIG_ENDIAN
  198. uint32_t w;
  199. memcpy(&w, src, sizeof w);
  200. return w;
  201. #else
  202. uint32_t w = (uint32_t) src[3];
  203. w |= (uint32_t) src[2] << 8;
  204. w |= (uint32_t) src[1] << 16;
  205. w |= (uint32_t) src[0] << 24;
  206. return w;
  207. #endif
  208. }
  209. #define STORE32_BE(DST, W) store32_be((DST), (W))
  210. static inline void
  211. store32_be(uint8_t dst[4], uint32_t w)
  212. {
  213. #ifdef NATIVE_BIG_ENDIAN
  214. memcpy(dst, &w, sizeof w);
  215. #else
  216. dst[3] = (uint8_t) w;
  217. w >>= 8;
  218. dst[2] = (uint8_t) w;
  219. w >>= 8;
  220. dst[1] = (uint8_t) w;
  221. w >>= 8;
  222. dst[0] = (uint8_t) w;
  223. #endif
  224. }
  225. #define LOAD16_BE(SRC) load16_be(SRC)
  226. static inline uint16_t
  227. load16_be(const uint8_t src[2])
  228. {
  229. #ifdef NATIVE_BIG_ENDIAN
  230. uint16_t w;
  231. memcpy(&w, src, sizeof w);
  232. return w;
  233. #else
  234. uint16_t w = (uint16_t) src[1];
  235. w |= (uint16_t) src[0] << 8;
  236. return w;
  237. #endif
  238. }
  239. #define STORE16_BE(DST, W) store16_be((DST), (W))
  240. static inline void
  241. store16_be(uint8_t dst[2], uint16_t w)
  242. {
  243. #ifdef NATIVE_BIG_ENDIAN
  244. memcpy(dst, &w, sizeof w);
  245. #else
  246. dst[1] = (uint8_t) w;
  247. w >>= 8;
  248. dst[0] = (uint8_t) w;
  249. #endif
  250. }
  251. static inline void
  252. mem_cpy(void *__restrict__ dst_, const void *__restrict__ src_, size_t n)
  253. {
  254. unsigned char * dst = (unsigned char *) dst_;
  255. const unsigned char *src = (const unsigned char *) src_;
  256. size_t i;
  257. for (i = 0; i < n; i++) {
  258. dst[i] = src[i];
  259. }
  260. }
  261. static inline void
  262. mem_zero(void *dst_, size_t n)
  263. {
  264. unsigned char *dst = (unsigned char *) dst_;
  265. size_t i;
  266. for (i = 0; i < n; i++) {
  267. dst[i] = 0;
  268. }
  269. }
  270. static inline void
  271. mem_xor(void *__restrict__ dst_, const void *__restrict__ src_, size_t n)
  272. {
  273. unsigned char * dst = (unsigned char *) dst_;
  274. const unsigned char *src = (const unsigned char *) src_;
  275. size_t i;
  276. for (i = 0; i < n; i++) {
  277. dst[i] ^= src[i];
  278. }
  279. }
  280. static inline void
  281. mem_xor2(void *__restrict__ dst_, const void *__restrict__ src1_, const void *__restrict__ src2_,
  282. size_t n)
  283. {
  284. unsigned char * dst = (unsigned char *) dst_;
  285. const unsigned char *src1 = (const unsigned char *) src1_;
  286. const unsigned char *src2 = (const unsigned char *) src2_;
  287. size_t i;
  288. for (i = 0; i < n; i++) {
  289. dst[i] = src1[i] ^ src2[i];
  290. }
  291. }
  292. static const uint8_t zero[64] = { 0 };