13 #include "trunnel-impl.h"
17 #ifdef HAVE_SYS_PARAM_H
18 #include <sys/param.h>
21 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
22 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
23 # define IS_LITTLE_ENDIAN 1
24 #elif defined(BYTE_ORDER) && defined(ORDER_LITTLE_ENDIAN) && \
25 BYTE_ORDER == __ORDER_LITTLE_ENDIAN
26 # define IS_LITTLE_ENDIAN 1
28 # define IS_LITTLE_ENDIAN 1
29 #elif defined(__APPLE__)
30 # include <libkern/OSByteOrder.h>
31 # define BSWAP64(x) OSSwapLittleToHostInt64(x)
32 #elif defined(sun) || defined(__sun)
33 # include <sys/byteorder.h>
35 # define IS_LITTLE_ENDIAN
38 # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(OpenBSD)
39 # include <sys/endian.h>
43 # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
44 __BYTE_ORDER == __LITTLE_ENDIAN
45 # define IS_LITTLE_ENDIAN
51 trunnel_htons(uint16_t s)
53 return (s << 8) | (s >> 8);
56 trunnel_ntohs(uint16_t s)
58 return (s << 8) | (s >> 8);
61 trunnel_htonl(uint32_t s)
69 trunnel_ntohl(uint32_t s)
79 trunnel_htonll(uint64_t a)
81 #ifdef IS_LITTLE_ENDIAN
82 return trunnel_htonl((uint32_t)(a>>32))
83 | (((uint64_t)trunnel_htonl((uint32_t)a))<<32);
90 trunnel_ntohll(uint64_t a)
92 return trunnel_htonll(a);
95 #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
99 int trunnel_provoke_alloc_failure = 0;
103 trunnel_dynarray_expand(
size_t *allocated_p,
void *ptr,
104 size_t howmanymore,
size_t eltsize)
106 size_t newsize = howmanymore + *allocated_p;
107 void *newarray = NULL;
110 if (newsize < *allocated_p * 2)
111 newsize = *allocated_p * 2;
112 if (newsize <= *allocated_p || newsize < howmanymore)
114 newarray = trunnel_reallocarray(ptr, newsize, eltsize);
115 if (newarray == NULL)
118 *allocated_p = newsize;
122 #ifndef trunnel_reallocarray
124 trunnel_reallocarray(
void *a,
size_t x,
size_t y)
126 #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
127 if (trunnel_provoke_alloc_failure) {
128 if (--trunnel_provoke_alloc_failure == 0)
132 if (x > SIZE_MAX / y)
134 return trunnel_realloc(a, x * y);
139 trunnel_string_getstr(trunnel_string_t *str)
141 trunnel_assert(str->allocated_ >= str->n_);
142 if (str->allocated_ == str->n_) {
143 TRUNNEL_DYNARRAY_EXPAND(
char, str, 1, {});
145 str->elts_[str->n_] = 0;
147 trunnel_alloc_failed:
152 trunnel_string_setstr0(trunnel_string_t *str,
const char *val,
size_t len,
153 uint8_t *errcode_ptr)
156 goto trunnel_alloc_failed;
157 if (str->allocated_ <= len) {
158 TRUNNEL_DYNARRAY_EXPAND(
char, str, len + 1 - str->allocated_, {});
160 memcpy(str->elts_, val, len);
164 trunnel_alloc_failed:
170 trunnel_string_setlen(trunnel_string_t *str,
size_t newlen,
171 uint8_t *errcode_ptr)
173 if (newlen == SIZE_MAX)
174 goto trunnel_alloc_failed;
175 if (str->allocated_ < newlen + 1) {
176 TRUNNEL_DYNARRAY_EXPAND(
char, str, newlen + 1 - str->allocated_, {});
178 if (str->n_ < newlen) {
179 memset(& (str->elts_[str->n_]), 0, (newlen - str->n_));
182 str->elts_[newlen] = 0;
185 trunnel_alloc_failed:
191 trunnel_dynarray_setlen(
size_t *allocated_p,
size_t *len_p,
192 void *ptr,
size_t newlen,
193 size_t eltsize, trunnel_free_fn_t free_fn,
194 uint8_t *errcode_ptr)
196 if (*allocated_p < newlen) {
197 void *newptr = trunnel_dynarray_expand(allocated_p, ptr,
198 newlen - *allocated_p, eltsize);
200 goto trunnel_alloc_failed;
203 if (free_fn && *len_p > newlen) {
205 void **elts = (
void **) ptr;
206 for (i = newlen; i < *len_p; ++i) {
211 if (*len_p < newlen) {
212 memset( ((
char*)ptr) + (eltsize * *len_p), 0, (newlen - *len_p) * eltsize);
216 trunnel_alloc_failed: