extract.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 1992, 1993, 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /*
  22. * For 8-bit values; provided for the sake of completeness. Byte order
  23. * isn't relevant, and alignment isn't an issue.
  24. */
  25. #define EXTRACT_8BITS(p) (*(p))
  26. #define EXTRACT_LE_8BITS(p) (*(p))
  27. /*
  28. * Inline functions or macros to extract possibly-unaligned big-endian
  29. * integral values.
  30. */
  31. #include "funcattrs.h"
  32. /*
  33. * If we have versions of GCC or Clang that support an __attribute__
  34. * to say "if we're building with unsigned behavior sanitization,
  35. * don't complain about undefined behavior in this function", we
  36. * label these functions with that attribute - we *know* it's undefined
  37. * in the C standard, but we *also* know it does what we want with
  38. * the ISA we're targeting and the compiler we're using.
  39. *
  40. * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
  41. * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
  42. * GCC or Clang first had __attribute__((no_sanitize(XXX)).
  43. *
  44. * For Clang, we check for __attribute__((no_sanitize(XXX)) with
  45. * __has_attribute, as there are versions of Clang that support
  46. * __attribute__((no_sanitize("undefined")) but don't support
  47. * __attribute__((no_sanitize_undefined)).
  48. *
  49. * We define this here, rather than in funcattrs.h, because we
  50. * only want it used here, we don't want it to be broadly used.
  51. * (Any printer will get this defined, but this should at least
  52. * make it harder for people to find.)
  53. */
  54. #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
  55. #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
  56. #elif __has_attribute(no_sanitize)
  57. #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
  58. #else
  59. #define UNALIGNED_OK
  60. #endif
  61. #ifdef LBL_ALIGN
  62. /*
  63. * The processor doesn't natively handle unaligned loads.
  64. */
  65. #if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
  66. (defined(__alpha) || defined(__alpha__) || \
  67. defined(__mips) || defined(__mips__))
  68. /*
  69. * This is a GCC-compatible compiler and we have __attribute__, which
  70. * we assume that mean we have __attribute__((packed)), and this is
  71. * MIPS or Alpha, which has instructions that can help when doing
  72. * unaligned loads.
  73. *
  74. * Declare packed structures containing a uint16_t and a uint32_t,
  75. * cast the pointer to point to one of those, and fetch through it;
  76. * the GCC manual doesn't appear to explicitly say that
  77. * __attribute__((packed)) causes the compiler to generate unaligned-safe
  78. * code, but it apppears to do so.
  79. *
  80. * We do this in case the compiler can generate code using those
  81. * instructions to do an unaligned load and pass stuff to "ntohs()" or
  82. * "ntohl()", which might be better than than the code to fetch the
  83. * bytes one at a time and assemble them. (That might not be the
  84. * case on a little-endian platform, such as DEC's MIPS machines and
  85. * Alpha machines, where "ntohs()" and "ntohl()" might not be done
  86. * inline.)
  87. *
  88. * We do this only for specific architectures because, for example,
  89. * at least some versions of GCC, when compiling for 64-bit SPARC,
  90. * generate code that assumes alignment if we do this.
  91. *
  92. * XXX - add other architectures and compilers as possible and
  93. * appropriate.
  94. *
  95. * HP's C compiler, indicated by __HP_cc being defined, supports
  96. * "#pragma unaligned N" in version A.05.50 and later, where "N"
  97. * specifies a number of bytes at which the typedef on the next
  98. * line is aligned, e.g.
  99. *
  100. * #pragma unalign 1
  101. * typedef uint16_t unaligned_uint16_t;
  102. *
  103. * to define unaligned_uint16_t as a 16-bit unaligned data type.
  104. * This could be presumably used, in sufficiently recent versions of
  105. * the compiler, with macros similar to those below. This would be
  106. * useful only if that compiler could generate better code for PA-RISC
  107. * or Itanium than would be generated by a bunch of shifts-and-ORs.
  108. *
  109. * DEC C, indicated by __DECC being defined, has, at least on Alpha,
  110. * an __unaligned qualifier that can be applied to pointers to get the
  111. * compiler to generate code that does unaligned loads and stores when
  112. * dereferencing the pointer in question.
  113. *
  114. * XXX - what if the native C compiler doesn't support
  115. * __attribute__((packed))? How can we get it to generate unaligned
  116. * accesses for *specific* items?
  117. */
  118. typedef struct {
  119. uint16_t val;
  120. } __attribute__((packed)) unaligned_uint16_t;
  121. typedef struct {
  122. uint32_t val;
  123. } __attribute__((packed)) unaligned_uint32_t;
  124. UNALIGNED_OK static inline uint16_t
  125. EXTRACT_16BITS(const void *p)
  126. {
  127. return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
  128. }
  129. UNALIGNED_OK static inline uint32_t
  130. EXTRACT_32BITS(const void *p)
  131. {
  132. return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
  133. }
  134. UNALIGNED_OK static inline uint64_t
  135. EXTRACT_64BITS(const void *p)
  136. {
  137. return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
  138. ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
  139. }
  140. #else /* have to do it a byte at a time */
  141. /*
  142. * This isn't a GCC-compatible compiler, we don't have __attribute__,
  143. * or we do but we don't know of any better way with this instruction
  144. * set to do unaligned loads, so do unaligned loads of big-endian
  145. * quantities the hard way - fetch the bytes one at a time and
  146. * assemble them.
  147. */
  148. #define EXTRACT_16BITS(p) \
  149. ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
  150. ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
  151. #define EXTRACT_32BITS(p) \
  152. ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
  153. ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
  154. ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
  155. ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
  156. #define EXTRACT_64BITS(p) \
  157. ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
  158. ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
  159. ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
  160. ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
  161. ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
  162. ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
  163. ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
  164. ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
  165. #endif /* must special-case unaligned accesses */
  166. #else /* LBL_ALIGN */
  167. /*
  168. * The processor natively handles unaligned loads, so we can just
  169. * cast the pointer and fetch through it.
  170. */
  171. static inline uint16_t UNALIGNED_OK
  172. EXTRACT_16BITS(const void *p)
  173. {
  174. return ((uint16_t)ntohs(*(const uint16_t *)(p)));
  175. }
  176. static inline uint32_t UNALIGNED_OK
  177. EXTRACT_32BITS(const void *p)
  178. {
  179. return ((uint32_t)ntohl(*(const uint32_t *)(p)));
  180. }
  181. static inline uint64_t UNALIGNED_OK
  182. EXTRACT_64BITS(const void *p)
  183. {
  184. return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
  185. ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
  186. }
  187. #endif /* LBL_ALIGN */
  188. #define EXTRACT_24BITS(p) \
  189. ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
  190. ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
  191. ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
  192. #define EXTRACT_40BITS(p) \
  193. ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
  194. ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
  195. ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
  196. ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
  197. ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
  198. #define EXTRACT_48BITS(p) \
  199. ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
  200. ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
  201. ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
  202. ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
  203. ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
  204. ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
  205. #define EXTRACT_56BITS(p) \
  206. ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
  207. ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
  208. ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
  209. ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
  210. ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
  211. ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
  212. ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
  213. /*
  214. * Macros to extract possibly-unaligned little-endian integral values.
  215. * XXX - do loads on little-endian machines that support unaligned loads?
  216. */
  217. #define EXTRACT_LE_16BITS(p) \
  218. ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
  219. ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
  220. #define EXTRACT_LE_32BITS(p) \
  221. ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
  222. ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
  223. ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
  224. ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
  225. #define EXTRACT_LE_24BITS(p) \
  226. ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
  227. ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
  228. ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
  229. #define EXTRACT_LE_64BITS(p) \
  230. ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
  231. ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
  232. ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
  233. ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
  234. ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
  235. ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
  236. ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
  237. ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
  238. /*
  239. * Macros to check the presence of the values in question.
  240. */
  241. #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
  242. #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
  243. #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
  244. #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
  245. #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
  246. #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
  247. #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
  248. #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
  249. #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
  250. #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
  251. #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
  252. #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
  253. #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
  254. #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
  255. #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
  256. #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
  257. #define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16)
  258. #define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16)