trunnel.c 6.4 KB

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