trunnel-impl.h 10 KB

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