trunnel.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* trunnel.c -- copied from Trunnel v1.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, 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 <stdlib.h>
  13. #include <string.h>
  14. #include "trunnel-impl.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(a>>32) | (((uint64_t)trunnel_htonl(a))<<32);
  75. #else
  76. return a;
  77. #endif
  78. }
  79. uint64_t
  80. trunnel_ntohll(uint64_t a)
  81. {
  82. return trunnel_htonll(a);
  83. }
  84. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  85. /** Used for debugging and running tricky test cases: Makes the nth
  86. * memoryation allocation call from now fail.
  87. */
  88. int trunnel_provoke_alloc_failure = 0;
  89. #endif
  90. void *
  91. trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
  92. size_t howmanymore, size_t eltsize)
  93. {
  94. size_t newsize = howmanymore + *allocated_p;
  95. void *newarray = NULL;
  96. if (newsize < 8)
  97. newsize = 8;
  98. if (newsize < *allocated_p * 2)
  99. newsize = *allocated_p * 2;
  100. if (newsize <= *allocated_p || newsize < howmanymore)
  101. return NULL;
  102. newarray = trunnel_reallocarray(ptr, newsize, eltsize);
  103. if (newarray == NULL)
  104. return NULL;
  105. *allocated_p = newsize;
  106. return newarray;
  107. }
  108. #ifndef trunnel_reallocarray
  109. void *
  110. trunnel_reallocarray(void *a, size_t x, size_t y)
  111. {
  112. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  113. if (trunnel_provoke_alloc_failure) {
  114. if (--trunnel_provoke_alloc_failure == 0)
  115. return NULL;
  116. }
  117. #endif
  118. if (x > SIZE_MAX / y)
  119. return NULL;
  120. return trunnel_realloc(a, x * y);
  121. }
  122. #endif
  123. const char *
  124. trunnel_string_getstr(trunnel_string_t *str)
  125. {
  126. trunnel_assert(str->allocated_ >= str->n_);
  127. if (str->allocated_ == str->n_) {
  128. TRUNNEL_DYNARRAY_EXPAND(char, str, 1, {});
  129. }
  130. str->elts_[str->n_] = 0;
  131. return str->elts_;
  132. trunnel_alloc_failed:
  133. return NULL;
  134. }
  135. int
  136. trunnel_string_setstr0(trunnel_string_t *str, const char *val, size_t len,
  137. uint8_t *errcode_ptr)
  138. {
  139. if (len == SIZE_MAX)
  140. goto trunnel_alloc_failed;
  141. if (str->allocated_ <= len) {
  142. TRUNNEL_DYNARRAY_EXPAND(char, str, len + 1 - str->allocated_, {});
  143. }
  144. memcpy(str->elts_, val, len);
  145. str->n_ = len;
  146. str->elts_[len] = 0;
  147. return 0;
  148. trunnel_alloc_failed:
  149. *errcode_ptr = 1;
  150. return -1;
  151. }
  152. int
  153. trunnel_string_setlen(trunnel_string_t *str, size_t newlen,
  154. uint8_t *errcode_ptr)
  155. {
  156. if (newlen == SIZE_MAX)
  157. goto trunnel_alloc_failed;
  158. if (str->allocated_ < newlen + 1) {
  159. TRUNNEL_DYNARRAY_EXPAND(char, str, newlen + 1 - str->allocated_, {});
  160. }
  161. if (str->n_ < newlen) {
  162. memset(& (str->elts_[str->n_]), 0, (newlen - str->n_));
  163. }
  164. str->n_ = newlen;
  165. str->elts_[newlen] = 0;
  166. return 0;
  167. trunnel_alloc_failed:
  168. *errcode_ptr = 1;
  169. return -1;
  170. }
  171. void *
  172. trunnel_dynarray_setlen(size_t *allocated_p, size_t *len_p,
  173. void *ptr, size_t newlen,
  174. size_t eltsize, trunnel_free_fn_t free_fn,
  175. uint8_t *errcode_ptr)
  176. {
  177. if (*allocated_p < newlen) {
  178. void *newptr = trunnel_dynarray_expand(allocated_p, ptr,
  179. newlen - *allocated_p, eltsize);
  180. if (newptr == NULL)
  181. goto trunnel_alloc_failed;
  182. ptr = newptr;
  183. }
  184. if (free_fn && *len_p > newlen) {
  185. size_t i;
  186. void **elts = (void **) ptr;
  187. for (i = newlen; i < *len_p; ++i) {
  188. free_fn(elts[i]);
  189. elts[i] = NULL;
  190. }
  191. }
  192. if (*len_p < newlen) {
  193. memset( ((char*)ptr) + (eltsize * *len_p), 0, (newlen - *len_p) * eltsize);
  194. }
  195. *len_p = newlen;
  196. return ptr;
  197. trunnel_alloc_failed:
  198. *errcode_ptr = 1;
  199. return NULL;
  200. }
  201. /*
  202. Copyright 2014 The Tor Project, Inc.
  203. Redistribution and use in source and binary forms, with or without
  204. modification, are permitted provided that the following conditions are
  205. met:
  206. * Redistributions of source code must retain the above copyright
  207. notice, this list of conditions and the following disclaimer.
  208. * Redistributions in binary form must reproduce the above
  209. copyright notice, this list of conditions and the following disclaimer
  210. in the documentation and/or other materials provided with the
  211. distribution.
  212. * Neither the names of the copyright owners nor the names of its
  213. contributors may be used to endorse or promote products derived from
  214. this software without specific prior written permission.
  215. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  216. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  217. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  218. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  219. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  220. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  221. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  222. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  223. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  224. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  225. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  226. */