trunnel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* trunnel.c -- copied from Trunnel v1.5.2
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. /* trunnel.c -- Helper functions to implement trunnel.
  6. *
  7. * Copyright 2014-2019, The Tor Project, Inc.
  8. * See license at the end of this file for copying information.
  9. *
  10. * See trunnel-impl.h for documentation of these functions.
  11. */
  12. #include "trunnel-impl.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #ifdef HAVE_SYS_PARAM_H
  16. #include <sys/param.h>
  17. #endif
  18. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  19. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  20. # define IS_LITTLE_ENDIAN 1
  21. #elif defined(BYTE_ORDER) && defined(ORDER_LITTLE_ENDIAN) && \
  22. BYTE_ORDER == __ORDER_LITTLE_ENDIAN
  23. # define IS_LITTLE_ENDIAN 1
  24. #elif defined(_WIN32)
  25. # define IS_LITTLE_ENDIAN 1
  26. #elif defined(__APPLE__)
  27. # include <libkern/OSByteOrder.h>
  28. # define BSWAP64(x) OSSwapLittleToHostInt64(x)
  29. #elif defined(sun) || defined(__sun)
  30. # include <sys/byteorder.h>
  31. # ifndef _BIG_ENDIAN
  32. # define IS_LITTLE_ENDIAN
  33. # endif
  34. #else
  35. # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(OpenBSD)
  36. # include <sys/endian.h>
  37. # else
  38. # include <endian.h>
  39. # endif
  40. # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  41. __BYTE_ORDER == __LITTLE_ENDIAN
  42. # define IS_LITTLE_ENDIAN
  43. # endif
  44. #endif
  45. #ifdef _WIN32
  46. uint16_t
  47. trunnel_htons(uint16_t s)
  48. {
  49. return (s << 8) | (s >> 8);
  50. }
  51. uint16_t
  52. trunnel_ntohs(uint16_t s)
  53. {
  54. return (s << 8) | (s >> 8);
  55. }
  56. uint32_t
  57. trunnel_htonl(uint32_t s)
  58. {
  59. return (s << 24) |
  60. ((s << 8)&0xff0000) |
  61. ((s >> 8)&0xff00) |
  62. (s >> 24);
  63. }
  64. uint32_t
  65. trunnel_ntohl(uint32_t s)
  66. {
  67. return (s << 24) |
  68. ((s << 8)&0xff0000) |
  69. ((s >> 8)&0xff00) |
  70. (s >> 24);
  71. }
  72. #endif
  73. uint64_t
  74. trunnel_htonll(uint64_t a)
  75. {
  76. #ifdef IS_LITTLE_ENDIAN
  77. return trunnel_htonl((uint32_t)(a>>32))
  78. | (((uint64_t)trunnel_htonl((uint32_t)a))<<32);
  79. #else
  80. return a;
  81. #endif
  82. }
  83. uint64_t
  84. trunnel_ntohll(uint64_t a)
  85. {
  86. return trunnel_htonll(a);
  87. }
  88. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  89. /** Used for debugging and running tricky test cases: Makes the nth
  90. * memoryation allocation call from now fail.
  91. */
  92. int trunnel_provoke_alloc_failure = 0;
  93. #endif
  94. void *
  95. trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
  96. size_t howmanymore, size_t eltsize)
  97. {
  98. size_t newsize = howmanymore + *allocated_p;
  99. void *newarray = NULL;
  100. if (newsize < 8)
  101. newsize = 8;
  102. if (newsize < *allocated_p * 2)
  103. newsize = *allocated_p * 2;
  104. if (newsize <= *allocated_p || newsize < howmanymore)
  105. return NULL;
  106. newarray = trunnel_reallocarray(ptr, newsize, eltsize);
  107. if (newarray == NULL)
  108. return NULL;
  109. *allocated_p = newsize;
  110. return newarray;
  111. }
  112. #ifndef trunnel_reallocarray
  113. void *
  114. trunnel_reallocarray(void *a, size_t x, size_t y)
  115. {
  116. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  117. if (trunnel_provoke_alloc_failure) {
  118. if (--trunnel_provoke_alloc_failure == 0)
  119. return NULL;
  120. }
  121. #endif
  122. if (x > SIZE_MAX / y)
  123. return NULL;
  124. return trunnel_realloc(a, x * y);
  125. }
  126. #endif
  127. const char *
  128. trunnel_string_getstr(trunnel_string_t *str)
  129. {
  130. trunnel_assert(str->allocated_ >= str->n_);
  131. if (str->allocated_ == str->n_) {
  132. TRUNNEL_DYNARRAY_EXPAND(char, str, 1, {});
  133. }
  134. str->elts_[str->n_] = 0;
  135. return str->elts_;
  136. trunnel_alloc_failed:
  137. return NULL;
  138. }
  139. int
  140. trunnel_string_setstr0(trunnel_string_t *str, const char *val, size_t len,
  141. uint8_t *errcode_ptr)
  142. {
  143. if (len == SIZE_MAX)
  144. goto trunnel_alloc_failed;
  145. if (str->allocated_ <= len) {
  146. TRUNNEL_DYNARRAY_EXPAND(char, str, len + 1 - str->allocated_, {});
  147. }
  148. memcpy(str->elts_, val, len);
  149. str->n_ = len;
  150. str->elts_[len] = 0;
  151. return 0;
  152. trunnel_alloc_failed:
  153. *errcode_ptr = 1;
  154. return -1;
  155. }
  156. int
  157. trunnel_string_setlen(trunnel_string_t *str, size_t newlen,
  158. uint8_t *errcode_ptr)
  159. {
  160. if (newlen == SIZE_MAX)
  161. goto trunnel_alloc_failed;
  162. if (str->allocated_ < newlen + 1) {
  163. TRUNNEL_DYNARRAY_EXPAND(char, str, newlen + 1 - str->allocated_, {});
  164. }
  165. if (str->n_ < newlen) {
  166. memset(& (str->elts_[str->n_]), 0, (newlen - str->n_));
  167. }
  168. str->n_ = newlen;
  169. str->elts_[newlen] = 0;
  170. return 0;
  171. trunnel_alloc_failed:
  172. *errcode_ptr = 1;
  173. return -1;
  174. }
  175. void *
  176. trunnel_dynarray_setlen(size_t *allocated_p, size_t *len_p,
  177. void *ptr, size_t newlen,
  178. size_t eltsize, trunnel_free_fn_t free_fn,
  179. uint8_t *errcode_ptr)
  180. {
  181. if (*allocated_p < newlen) {
  182. void *newptr = trunnel_dynarray_expand(allocated_p, ptr,
  183. newlen - *allocated_p, eltsize);
  184. if (newptr == NULL)
  185. goto trunnel_alloc_failed;
  186. ptr = newptr;
  187. }
  188. if (free_fn && *len_p > newlen) {
  189. size_t i;
  190. void **elts = (void **) ptr;
  191. for (i = newlen; i < *len_p; ++i) {
  192. free_fn(elts[i]);
  193. elts[i] = NULL;
  194. }
  195. }
  196. if (*len_p < newlen) {
  197. memset( ((char*)ptr) + (eltsize * *len_p), 0, (newlen - *len_p) * eltsize);
  198. }
  199. *len_p = newlen;
  200. return ptr;
  201. trunnel_alloc_failed:
  202. *errcode_ptr = 1;
  203. return NULL;
  204. }
  205. /*
  206. Copyright 2014 The Tor Project, Inc.
  207. Redistribution and use in source and binary forms, with or without
  208. modification, are permitted provided that the following conditions are
  209. met:
  210. * Redistributions of source code must retain the above copyright
  211. notice, this list of conditions and the following disclaimer.
  212. * Redistributions in binary form must reproduce the above
  213. copyright notice, this list of conditions and the following disclaimer
  214. in the documentation and/or other materials provided with the
  215. distribution.
  216. * Neither the names of the copyright owners nor the names of its
  217. contributors may be used to endorse or promote products derived from
  218. this software without specific prior written permission.
  219. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  220. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  221. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  222. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  223. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  224. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  225. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  226. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  227. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  228. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  229. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  230. */