trunnel-impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* trunnel-impl.h -- copied from Trunnel v1.5.2
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. /* trunnel-impl.h -- Implementation helpers for trunnel, included by
  6. * generated trunnel files
  7. *
  8. * Copyright 2014-2017, The Tor Project, Inc.
  9. * See license at the end of this file for copying information.
  10. */
  11. #ifndef TRUNNEL_IMPL_H_INCLUDED_
  12. #define TRUNNEL_IMPL_H_INCLUDED_
  13. #ifdef TRUNNEL_LOCAL_H
  14. #include "trunnel-local.h"
  15. #endif
  16. #include "trunnel.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #if defined(_MSC_VER) && (_MSC_VER < 1600)
  21. #define uint8_t unsigned char
  22. #define uint16_t unsigned short
  23. #define uint32_t unsigned int
  24. #define uint64_t unsigned __int64
  25. #define inline __inline
  26. #else
  27. #include <stdint.h>
  28. #endif
  29. #ifdef _WIN32
  30. uint32_t trunnel_htonl(uint32_t a);
  31. uint32_t trunnel_ntohl(uint32_t a);
  32. uint16_t trunnel_htons(uint16_t a);
  33. uint16_t trunnel_ntohs(uint16_t a);
  34. #else
  35. #include <arpa/inet.h>
  36. #define trunnel_htonl(x) htonl(x)
  37. #define trunnel_htons(x) htons(x)
  38. #define trunnel_ntohl(x) ntohl(x)
  39. #define trunnel_ntohs(x) ntohs(x)
  40. #endif
  41. uint64_t trunnel_htonll(uint64_t a);
  42. uint64_t trunnel_ntohll(uint64_t a);
  43. #ifndef trunnel_assert
  44. #define trunnel_assert(x) assert(x)
  45. #endif
  46. static inline void
  47. trunnel_set_uint64(void *p, uint64_t v) {
  48. memcpy(p, &v, 8);
  49. }
  50. static inline void
  51. trunnel_set_uint32(void *p, uint32_t v) {
  52. memcpy(p, &v, 4);
  53. }
  54. static inline void
  55. trunnel_set_uint16(void *p, uint16_t v) {
  56. memcpy(p, &v, 2);
  57. }
  58. static inline void
  59. trunnel_set_uint8(void *p, uint8_t v) {
  60. memcpy(p, &v, 1);
  61. }
  62. static inline uint64_t
  63. trunnel_get_uint64(const void *p) {
  64. uint64_t x;
  65. memcpy(&x, p, 8);
  66. return x;
  67. }
  68. static inline uint32_t
  69. trunnel_get_uint32(const void *p) {
  70. uint32_t x;
  71. memcpy(&x, p, 4);
  72. return x;
  73. }
  74. static inline uint16_t
  75. trunnel_get_uint16(const void *p) {
  76. uint16_t x;
  77. memcpy(&x, p, 2);
  78. return x;
  79. }
  80. static inline uint8_t
  81. trunnel_get_uint8(const void *p) {
  82. return *(const uint8_t*)p;
  83. }
  84. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  85. extern int trunnel_provoke_alloc_failure;
  86. static inline void *
  87. trunnel_malloc(size_t n)
  88. {
  89. if (trunnel_provoke_alloc_failure) {
  90. if (--trunnel_provoke_alloc_failure == 0)
  91. return NULL;
  92. }
  93. return malloc(n);
  94. }
  95. static inline void *
  96. trunnel_calloc(size_t a, size_t b)
  97. {
  98. if (trunnel_provoke_alloc_failure) {
  99. if (--trunnel_provoke_alloc_failure == 0)
  100. return NULL;
  101. }
  102. return calloc(a,b);
  103. }
  104. static inline char *
  105. trunnel_strdup(const char *s)
  106. {
  107. if (trunnel_provoke_alloc_failure) {
  108. if (--trunnel_provoke_alloc_failure == 0)
  109. return NULL;
  110. }
  111. return strdup(s);
  112. }
  113. #else
  114. #ifndef trunnel_malloc
  115. #define trunnel_malloc(x) (malloc((x)))
  116. #endif
  117. #ifndef trunnel_calloc
  118. #define trunnel_calloc(a,b) (calloc((a),(b)))
  119. #endif
  120. #ifndef trunnel_strdup
  121. #define trunnel_strdup(s) (strdup((s)))
  122. #endif
  123. #endif
  124. #ifndef trunnel_realloc
  125. #define trunnel_realloc(a,b) realloc((a),(b))
  126. #endif
  127. #ifndef trunnel_free_
  128. #define trunnel_free_(x) (free(x))
  129. #endif
  130. #define trunnel_free(x) ((x) ? (trunnel_free_(x),0) : (0))
  131. #ifndef trunnel_abort
  132. #define trunnel_abort() abort()
  133. #endif
  134. #ifndef trunnel_memwipe
  135. #define trunnel_memwipe(mem, len) ((void)0)
  136. #define trunnel_wipestr(s) ((void)0)
  137. #else
  138. #define trunnel_wipestr(s) do { \
  139. if (s) \
  140. trunnel_memwipe(s, strlen(s)); \
  141. } while (0)
  142. #endif
  143. /* ====== dynamic arrays ======== */
  144. #ifdef NDEBUG
  145. #define TRUNNEL_DYNARRAY_GET(da, n) \
  146. ((da)->elts_[(n)])
  147. #else
  148. /** Return the 'n'th element of 'da'. */
  149. #define TRUNNEL_DYNARRAY_GET(da, n) \
  150. (((n) >= (da)->n_ ? (trunnel_abort(),0) : 0), (da)->elts_[(n)])
  151. #endif
  152. /** Change the 'n'th element of 'da' to 'v'. */
  153. #define TRUNNEL_DYNARRAY_SET(da, n, v) do { \
  154. trunnel_assert((n) < (da)->n_); \
  155. (da)->elts_[(n)] = (v); \
  156. } while (0)
  157. /** Expand the dynamic array 'da' of 'elttype' so that it can hold at least
  158. * 'howmanymore' elements than its current capacity. Always tries to increase
  159. * the length of the array. On failure, run the code in 'on_fail' and goto
  160. * trunnel_alloc_failed. */
  161. #define TRUNNEL_DYNARRAY_EXPAND(elttype, da, howmanymore, on_fail) do { \
  162. elttype *newarray; \
  163. newarray = trunnel_dynarray_expand(&(da)->allocated_, \
  164. (da)->elts_, (howmanymore), \
  165. sizeof(elttype)); \
  166. if (newarray == NULL) { \
  167. on_fail; \
  168. goto trunnel_alloc_failed; \
  169. } \
  170. (da)->elts_ = newarray; \
  171. } while (0)
  172. /** Add 'v' to the end of the dynamic array 'da' of 'elttype', expanding it if
  173. * necessary. code in 'on_fail' and goto trunnel_alloc_failed. */
  174. #define TRUNNEL_DYNARRAY_ADD(elttype, da, v, on_fail) do { \
  175. if ((da)->n_ == (da)->allocated_) { \
  176. TRUNNEL_DYNARRAY_EXPAND(elttype, da, 1, on_fail); \
  177. } \
  178. (da)->elts_[(da)->n_++] = (v); \
  179. } while (0)
  180. /** Return the number of elements in 'da'. */
  181. #define TRUNNEL_DYNARRAY_LEN(da) ((da)->n_)
  182. /** Remove all storage held by 'da' and set it to be empty. Does not free
  183. * storage held by the elements themselves. */
  184. #define TRUNNEL_DYNARRAY_CLEAR(da) do { \
  185. trunnel_free((da)->elts_); \
  186. (da)->elts_ = NULL; \
  187. (da)->n_ = (da)->allocated_ = 0; \
  188. } while (0)
  189. /** Remove all storage held by 'da' and set it to be empty. Does not free
  190. * storage held by the elements themselves. */
  191. #define TRUNNEL_DYNARRAY_WIPE(da) do { \
  192. trunnel_memwipe((da)->elts_, (da)->allocated_ * sizeof((da)->elts_[0])); \
  193. } while (0)
  194. /** Helper: wraps or implements an OpenBSD-style reallocarray. Behaves
  195. * as realloc(a, x*y), but verifies that no overflow will occur in the
  196. * multiplication. Returns NULL on failure. */
  197. #ifndef trunnel_reallocarray
  198. void *trunnel_reallocarray(void *a, size_t x, size_t y);
  199. #endif
  200. /** Helper to expand a dynamic array. Behaves as TRUNNEL_DYNARRAY_EXPAND(),
  201. * taking the array of elements in 'ptr', a pointer to thethe current number
  202. * of allocated elements in allocated_p, the minimum numbeer of elements to
  203. * add in 'howmanymore', and the size of a single element in 'eltsize'.
  204. *
  205. * On success, adjust *allocated_p, and return the new value for the array of
  206. * elements. On failure, adjust nothing and return NULL.
  207. */
  208. void *trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
  209. size_t howmanymore, size_t eltsize);
  210. /** Type for a function to free members of a dynarray of pointers. */
  211. typedef void (*trunnel_free_fn_t)(void *);
  212. /**
  213. * Helper to change the length of a dynamic array. Takes pointers to the
  214. * current allocated and n fields of the array in 'allocated_p' and 'len_p',
  215. * and the current array of elements in 'ptr'; takes the length of a single
  216. * element in 'eltsize'. Changes the length to 'newlen'. If 'newlen' is
  217. * greater than the current length, pads the new elements with 0. If newlen
  218. * is less than the current length, and free_fn is non-NULL, treat the
  219. * array as an array of void *, and invoke free_fn() on each removed element.
  220. *
  221. * On success, adjust *allocated_p and *len_p, and return the new value for
  222. * the array of elements. On failure, adjust nothing, set *errcode_ptr to 1,
  223. * and return NULL.
  224. */
  225. void *trunnel_dynarray_setlen(size_t *allocated_p, size_t *len_p,
  226. void *ptr, size_t newlen,
  227. size_t eltsize, trunnel_free_fn_t free_fn,
  228. uint8_t *errcode_ptr);
  229. /**
  230. * Helper: return a pointer to the value of 'str' as a NUL-terminated string.
  231. * Might have to reallocate the storage for 'str' in order to fit in the final
  232. * NUL character. On allocation failure, return NULL.
  233. */
  234. const char *trunnel_string_getstr(trunnel_string_t *str);
  235. /**
  236. * Helper: change the contents of 'str' to hold the 'len'-byte string in
  237. * 'inp'. Adjusts the storage to have a terminating NUL that doesn't count
  238. * towards the length of the string. On success, return 0. On failure, set
  239. * *errcode_ptr to 1 and return -1.
  240. */
  241. int trunnel_string_setstr0(trunnel_string_t *str, const char *inp, size_t len,
  242. uint8_t *errcode_ptr);
  243. /**
  244. * As trunnel_dynarray_setlen, but adjusts a string rather than a dynamic
  245. * array, and ensures that the new string is NUL-terminated.
  246. */
  247. int trunnel_string_setlen(trunnel_string_t *str, size_t newlen,
  248. uint8_t *errcode_ptr);
  249. #endif
  250. /*
  251. Copyright 2014 The Tor Project, Inc.
  252. Redistribution and use in source and binary forms, with or without
  253. modification, are permitted provided that the following conditions are
  254. met:
  255. * Redistributions of source code must retain the above copyright
  256. notice, this list of conditions and the following disclaimer.
  257. * Redistributions in binary form must reproduce the above
  258. copyright notice, this list of conditions and the following disclaimer
  259. in the documentation and/or other materials provided with the
  260. distribution.
  261. * Neither the names of the copyright owners nor the names of its
  262. contributors may be used to endorse or promote products derived from
  263. this software without specific prior written permission.
  264. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  265. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  266. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  267. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  268. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  269. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  270. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  271. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  272. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  273. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  274. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  275. */