util.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifndef __ELL_UTIL_H
  23. #define __ELL_UTIL_H
  24. #include <string.h>
  25. #include <stdbool.h>
  26. #include <stdarg.h>
  27. #include <inttypes.h>
  28. #include <endian.h>
  29. #include <byteswap.h>
  30. #include <sys/uio.h>
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #define l_container_of(ptr, type, member) ({ \
  35. _Pragma("GCC diagnostic push") \
  36. _Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
  37. const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
  38. (type *)((char *) __mptr - offsetof(type, member)); \
  39. _Pragma("GCC diagnostic pop") \
  40. })
  41. #define L_STRINGIFY(val) L_STRINGIFY_ARG(val)
  42. #define L_STRINGIFY_ARG(contents) #contents
  43. #define L_WARN_ON(condition) __extension__ ({ \
  44. bool r = !!(condition); \
  45. if (r) \
  46. l_warn("WARNING: %s:%s() condition %s failed", \
  47. __FILE__, __func__, \
  48. #condition); \
  49. r; \
  50. })
  51. #define L_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
  52. #define L_UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
  53. #define L_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
  54. #define L_INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
  55. #define L_GET_UNALIGNED(ptr) __extension__ \
  56. ({ \
  57. struct __attribute__((packed)) { \
  58. __typeof__(*(ptr)) __v; \
  59. } *__p = (__typeof__(__p)) (ptr); \
  60. __p->__v; \
  61. })
  62. #define L_PUT_UNALIGNED(val, ptr) \
  63. do { \
  64. struct __attribute__((packed)) { \
  65. __typeof__(*(ptr)) __v; \
  66. } *__p = (__typeof__(__p)) (ptr); \
  67. __p->__v = (val); \
  68. } while(0)
  69. #if __BYTE_ORDER == __LITTLE_ENDIAN
  70. #define L_LE16_TO_CPU(val) (val)
  71. #define L_LE32_TO_CPU(val) (val)
  72. #define L_LE64_TO_CPU(val) (val)
  73. #define L_CPU_TO_LE16(val) (val)
  74. #define L_CPU_TO_LE32(val) (val)
  75. #define L_CPU_TO_LE64(val) (val)
  76. #define L_BE16_TO_CPU(val) bswap_16(val)
  77. #define L_BE32_TO_CPU(val) bswap_32(val)
  78. #define L_BE64_TO_CPU(val) bswap_64(val)
  79. #define L_CPU_TO_BE16(val) bswap_16(val)
  80. #define L_CPU_TO_BE32(val) bswap_32(val)
  81. #define L_CPU_TO_BE64(val) bswap_64(val)
  82. #elif __BYTE_ORDER == __BIG_ENDIAN
  83. #define L_LE16_TO_CPU(val) bswap_16(val)
  84. #define L_LE32_TO_CPU(val) bswap_32(val)
  85. #define L_LE64_TO_CPU(val) bswap_64(val)
  86. #define L_CPU_TO_LE16(val) bswap_16(val)
  87. #define L_CPU_TO_LE32(val) bswap_32(val)
  88. #define L_CPU_TO_LE64(val) bswap_64(val)
  89. #define L_BE16_TO_CPU(val) (val)
  90. #define L_BE32_TO_CPU(val) (val)
  91. #define L_BE64_TO_CPU(val) (val)
  92. #define L_CPU_TO_BE16(val) (val)
  93. #define L_CPU_TO_BE32(val) (val)
  94. #define L_CPU_TO_BE64(val) (val)
  95. #else
  96. #error "Unknown byte order"
  97. #endif
  98. #if __STDC_VERSION__ <= 199409L
  99. #define inline __inline__
  100. #endif
  101. static inline uint8_t l_get_u8(const void *ptr)
  102. {
  103. return *((const uint8_t *) ptr);
  104. }
  105. static inline void l_put_u8(uint8_t val, void *ptr)
  106. {
  107. *((uint8_t *) ptr) = val;
  108. }
  109. static inline uint16_t l_get_u16(const void *ptr)
  110. {
  111. return L_GET_UNALIGNED((const uint16_t *) ptr);
  112. }
  113. static inline void l_put_u16(uint16_t val, void *ptr)
  114. {
  115. L_PUT_UNALIGNED(val, (uint16_t *) ptr);
  116. }
  117. static inline uint32_t l_get_u32(const void *ptr)
  118. {
  119. return L_GET_UNALIGNED((const uint32_t *) ptr);
  120. }
  121. static inline void l_put_u32(uint32_t val, void *ptr)
  122. {
  123. L_PUT_UNALIGNED(val, (uint32_t *) ptr);
  124. }
  125. static inline uint64_t l_get_u64(const void *ptr)
  126. {
  127. return L_GET_UNALIGNED((const uint64_t *) ptr);
  128. }
  129. static inline void l_put_u64(uint64_t val, void *ptr)
  130. {
  131. L_PUT_UNALIGNED(val, (uint64_t *) ptr);
  132. }
  133. static inline int16_t l_get_s16(const void *ptr)
  134. {
  135. return L_GET_UNALIGNED((const int16_t *) ptr);
  136. }
  137. static inline int32_t l_get_s32(const void *ptr)
  138. {
  139. return L_GET_UNALIGNED((const int32_t *) ptr);
  140. }
  141. static inline int64_t l_get_s64(const void *ptr)
  142. {
  143. return L_GET_UNALIGNED((const int64_t *) ptr);
  144. }
  145. static inline uint16_t l_get_le16(const void *ptr)
  146. {
  147. return L_LE16_TO_CPU(L_GET_UNALIGNED((const uint16_t *) ptr));
  148. }
  149. static inline uint16_t l_get_be16(const void *ptr)
  150. {
  151. return L_BE16_TO_CPU(L_GET_UNALIGNED((const uint16_t *) ptr));
  152. }
  153. static inline uint32_t l_get_le32(const void *ptr)
  154. {
  155. return L_LE32_TO_CPU(L_GET_UNALIGNED((const uint32_t *) ptr));
  156. }
  157. static inline uint32_t l_get_be32(const void *ptr)
  158. {
  159. return L_BE32_TO_CPU(L_GET_UNALIGNED((const uint32_t *) ptr));
  160. }
  161. static inline uint64_t l_get_le64(const void *ptr)
  162. {
  163. return L_LE64_TO_CPU(L_GET_UNALIGNED((const uint64_t *) ptr));
  164. }
  165. static inline uint64_t l_get_be64(const void *ptr)
  166. {
  167. return L_BE64_TO_CPU(L_GET_UNALIGNED((const uint64_t *) ptr));
  168. }
  169. static inline void l_put_le16(uint16_t val, void *ptr)
  170. {
  171. L_PUT_UNALIGNED(L_CPU_TO_LE16(val), (uint16_t *) ptr);
  172. }
  173. static inline void l_put_be16(uint16_t val, const void *ptr)
  174. {
  175. L_PUT_UNALIGNED(L_CPU_TO_BE16(val), (uint16_t *) ptr);
  176. }
  177. static inline void l_put_le32(uint32_t val, void *ptr)
  178. {
  179. L_PUT_UNALIGNED(L_CPU_TO_LE32(val), (uint32_t *) ptr);
  180. }
  181. static inline void l_put_be32(uint32_t val, void *ptr)
  182. {
  183. L_PUT_UNALIGNED(L_CPU_TO_BE32(val), (uint32_t *) ptr);
  184. }
  185. static inline void l_put_le64(uint64_t val, void *ptr)
  186. {
  187. L_PUT_UNALIGNED(L_CPU_TO_LE64(val), (uint64_t *) ptr);
  188. }
  189. static inline void l_put_be64(uint64_t val, void *ptr)
  190. {
  191. L_PUT_UNALIGNED(L_CPU_TO_BE64(val), (uint64_t *) ptr);
  192. }
  193. #define L_AUTO_FREE_VAR(vartype,varname) \
  194. vartype varname __attribute__((cleanup(auto_free)))
  195. #define L_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  196. void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));
  197. void *l_memdup(const void *mem, size_t size)
  198. __attribute__ ((warn_unused_result, malloc));
  199. void l_free(void *ptr);
  200. void *l_realloc(void *mem, size_t size)
  201. __attribute__ ((warn_unused_result, malloc));
  202. static inline void auto_free(void *a)
  203. {
  204. void **p = (void **)a;
  205. l_free(*p);
  206. }
  207. #define l_steal_ptr(ptr) \
  208. (__extension__ ({ typeof(ptr) _tmp = (ptr); (ptr) = NULL; _tmp; }))
  209. /**
  210. * l_new:
  211. * @type: type of structure
  212. * @count: amount of structures
  213. *
  214. * Returns: pointer to allocated memory
  215. **/
  216. #define l_new(type, count) \
  217. (type *) (__extension__ ({ \
  218. size_t __n = (size_t) (count); \
  219. size_t __s = sizeof(type); \
  220. void *__p; \
  221. __p = l_malloc(__n * __s); \
  222. memset(__p, 0, __n * __s); \
  223. __p; \
  224. }))
  225. char *l_strdup(const char *str);
  226. char *l_strndup(const char *str, size_t max);
  227. char *l_strdup_printf(const char *format, ...)
  228. __attribute__((format(printf, 1, 2)));
  229. char *l_strdup_vprintf(const char *format, va_list args)
  230. __attribute__((format(printf, 1, 0)));
  231. size_t l_strlcpy(char* dst, const char *src, size_t len);
  232. bool l_str_has_prefix(const char *str, const char *prefix);
  233. bool l_str_has_suffix(const char *str, const char *suffix);
  234. bool l_streq0(const char *a, const char *b);
  235. char *l_util_hexstring(const void *buf, size_t len);
  236. char *l_util_hexstring_upper(const void *buf, size_t len);
  237. unsigned char *l_util_from_hexstring(const char *str, size_t *out_len);
  238. typedef void (*l_util_hexdump_func_t) (const char *str, void *user_data);
  239. void l_util_hexdump(bool in, const void *buf, size_t len,
  240. l_util_hexdump_func_t function, void *user_data);
  241. void l_util_hexdump_two(bool in, const void *buf1, size_t len1,
  242. const void *buf2, size_t len2,
  243. l_util_hexdump_func_t function, void *user_data);
  244. void l_util_hexdumpv(bool in, const struct iovec *iov, size_t n_iov,
  245. l_util_hexdump_func_t function,
  246. void *user_data);
  247. void l_util_debug(l_util_hexdump_func_t function, void *user_data,
  248. const char *format, ...)
  249. __attribute__((format(printf, 3, 4)));
  250. const char *l_util_get_debugfs_path(void);
  251. #define L_TFR(expression) \
  252. (__extension__ \
  253. ({ long int __result; \
  254. do __result = (long int) (expression); \
  255. while (__result == -1L && errno == EINTR); \
  256. __result; }))
  257. #define _L_IN_SET_CMP(val, type, cmp, ...) __extension__ ({ \
  258. const type __v = (val); \
  259. const typeof(__v) __elems[] = {__VA_ARGS__}; \
  260. unsigned int __i; \
  261. static const unsigned int __n = L_ARRAY_SIZE(__elems); \
  262. bool __r = false; \
  263. for (__i = 0; __i < __n && !__r; __i++) \
  264. __r = (cmp); \
  265. __r; \
  266. })
  267. /* Warning: evaluates all set elements even after @val has matched one */
  268. #define L_IN_SET(val, ...) \
  269. _L_IN_SET_CMP((val), __auto_type, __v == __elems[__i], ##__VA_ARGS__)
  270. #define L_IN_STRSET(val, ...) \
  271. _L_IN_SET_CMP((val), const char *, __v == __elems[__i] || \
  272. (__v && __elems[__i] && \
  273. !strcmp(__v, __elems[__i])), ##__VA_ARGS__)
  274. /*
  275. * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to
  276. * ensure the compiler does not try to optimize the constant time behavior.
  277. * The code has been modified to add comments and project specific code
  278. * styling.
  279. * This specific piece of code is subject to the following copyright:
  280. *
  281. * The MIT License (MIT)
  282. *
  283. * Copyright (c) 2015 Christophe Meessen
  284. *
  285. * Permission is hereby granted, free of charge, to any person obtaining a copy
  286. * of this software and associated documentation files (the "Software"), to
  287. * deal in the Software without restriction, including without limitation the
  288. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  289. * sell copies of the Software, and to permit persons to whom the Software is
  290. * furnished to do so, subject to the following conditions:
  291. *
  292. * The above copyright notice and this permission notice shall be included in
  293. * all copies or substantial portions of the Software.
  294. *
  295. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  296. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  297. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  298. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  299. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  300. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  301. * IN THE SOFTWARE.
  302. *
  303. * This function performs a secure memory comparison of two buffers of size
  304. * bytes, representing an integer (byte order is big endian). It returns
  305. * a negative, zero or positif value if a < b, a == b or a > b respectively.
  306. */
  307. static inline int l_secure_memcmp(const void *a, const void *b,
  308. size_t size)
  309. {
  310. const volatile uint8_t *aa = a;
  311. const volatile uint8_t *bb = b;
  312. int res = 0, diff, mask;
  313. /*
  314. * We will compare all bytes, starting with the less significant. When
  315. * we find a non-zero difference, we update the result accordingly.
  316. */
  317. if (size > 0) {
  318. /*
  319. * The following couple of lines can be summarized as a
  320. * constant time/memory access version of:
  321. * if (diff != 0) res = diff;
  322. *
  323. * From the previous operation, we know that diff is in
  324. * [-255, 255]
  325. *
  326. * The following figure show the possible value of mask, based
  327. * on different cases of diff:
  328. *
  329. * diff | diff-1 | ~diff | ((diff-1) & ~diff) | mask
  330. * ------|------------|------------|--------------------|------
  331. * < 0 | 0xFFFFFFXX | 0x000000YY | 0x000000ZZ | 0
  332. * == 0 | 0xFFFFFFFF | 0xFFFFFFFF | 0xFFFFFFFF | 0xF..F
  333. * > 0 | 0x000000XX | 0xFFFFFFYY | 0x000000ZZ | 0
  334. *
  335. * Hence, the mask allows to keep res when diff == 0, and to
  336. * set res to diff otherwise.
  337. */
  338. do {
  339. --size;
  340. diff = aa[size] - bb[size];
  341. mask = (((diff - 1) & ~diff) >> 8);
  342. res = (res & mask) | diff;
  343. } while (size != 0);
  344. }
  345. return res;
  346. }
  347. bool l_memeq(const void *field, size_t size, uint8_t byte);
  348. bool l_secure_memeq(const void *field, size_t size, uint8_t byte);
  349. static inline bool l_memeqzero(const void *field, size_t size)
  350. {
  351. return l_memeq(field, size, 0);
  352. }
  353. static inline void l_secure_select(bool select_left,
  354. const void *left, const void *right,
  355. void *out, size_t len)
  356. {
  357. const uint8_t *l = left;
  358. const uint8_t *r = right;
  359. uint8_t *o = out;
  360. uint8_t mask = -(!!select_left);
  361. size_t i;
  362. for (i = 0; i < len; i++)
  363. o[i] = r[i] ^ ((l[i] ^ r[i]) & mask);
  364. }
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif /* __ELL_UTIL_H */