1 /* trunnel.c -- copied from Trunnel v1.5.3
2 * https://gitweb.torproject.org/trunnel.git
3 * You probably shouldn't edit this file.
5 /* trunnel.c -- Helper functions to implement trunnel.
7 * Copyright 2014-2019, The Tor Project, Inc.
8 * See license at the end of this file for copying information.
10 * See trunnel-impl.h for documentation of these functions.
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
96 /** Used for debugging and running tricky test cases: Makes the nth
97 * memoryation allocation call from now fail.
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
:
222 Copyright 2014 The Tor Project, Inc.
224 Redistribution and use in source and binary forms, with or without
225 modification, are permitted provided that the following conditions are
228 * Redistributions of source code must retain the above copyright
229 notice, this list of conditions and the following disclaimer.
231 * Redistributions in binary form must reproduce the above
232 copyright notice, this list of conditions and the following disclaimer
233 in the documentation and/or other materials provided with the
236 * Neither the names of the copyright owners nor the names of its
237 contributors may be used to endorse or promote products derived from
238 this software without specific prior written permission.
240 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
241 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
242 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
243 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
244 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
246 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
250 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.