12#ifndef TRUNNEL_IMPL_H_INCLUDED_
13#define TRUNNEL_IMPL_H_INCLUDED_
15#include "trunnel-local.h"
22#if defined(_MSC_VER) && (_MSC_VER < 1600)
23#define uint8_t unsigned char
24#define uint16_t unsigned short
25#define uint32_t unsigned int
26#define uint64_t unsigned __int64
27#define inline __inline
33uint32_t trunnel_htonl(uint32_t a);
34uint32_t trunnel_ntohl(uint32_t a);
35uint16_t trunnel_htons(uint16_t a);
36uint16_t trunnel_ntohs(uint16_t a);
39#define trunnel_htonl(x) htonl(x)
40#define trunnel_htons(x) htons(x)
41#define trunnel_ntohl(x) ntohl(x)
42#define trunnel_ntohs(x) ntohs(x)
44uint64_t trunnel_htonll(uint64_t a);
45uint64_t trunnel_ntohll(uint64_t a);
48#define trunnel_assert(x) assert(x)
52trunnel_set_uint64(
void *p, uint64_t v) {
56trunnel_set_uint32(
void *p, uint32_t v) {
60trunnel_set_uint16(
void *p, uint16_t v) {
64trunnel_set_uint8(
void *p, uint8_t v) {
69trunnel_get_uint64(
const void *p) {
75trunnel_get_uint32(
const void *p) {
81trunnel_get_uint16(
const void *p) {
87trunnel_get_uint8(
const void *p) {
88 return *(
const uint8_t*)p;
92#ifdef TRUNNEL_DEBUG_FAILING_ALLOC
93extern int trunnel_provoke_alloc_failure;
96trunnel_malloc(
size_t n)
98 if (trunnel_provoke_alloc_failure) {
99 if (--trunnel_provoke_alloc_failure == 0)
105trunnel_calloc(
size_t a,
size_t b)
107 if (trunnel_provoke_alloc_failure) {
108 if (--trunnel_provoke_alloc_failure == 0)
114trunnel_strdup(
const char *s)
116 if (trunnel_provoke_alloc_failure) {
117 if (--trunnel_provoke_alloc_failure == 0)
123#ifndef trunnel_malloc
124#define trunnel_malloc(x) (malloc((x)))
126#ifndef trunnel_calloc
127#define trunnel_calloc(a,b) (calloc((a),(b)))
129#ifndef trunnel_strdup
130#define trunnel_strdup(s) (strdup((s)))
134#ifndef trunnel_realloc
135#define trunnel_realloc(a,b) realloc((a),(b))
139#define trunnel_free_(x) (free(x))
141#define trunnel_free(x) ((x) ? (trunnel_free_(x),0) : (0))
144#define trunnel_abort() abort()
147#ifndef trunnel_memwipe
148#define trunnel_memwipe(mem, len) ((void)0)
149#define trunnel_wipestr(s) ((void)0)
151#define trunnel_wipestr(s) do { \
153 trunnel_memwipe(s, strlen(s)); \
160#define TRUNNEL_DYNARRAY_GET(da, n) \
164#define TRUNNEL_DYNARRAY_GET(da, n) \
165 (((n) >= (da)->n_ ? (trunnel_abort(),0) : 0), (da)->elts_[(n)])
169#define TRUNNEL_DYNARRAY_SET(da, n, v) do { \
170 trunnel_assert((n) < (da)->n_); \
171 (da)->elts_[(n)] = (v); \
178#define TRUNNEL_DYNARRAY_EXPAND(elttype, da, howmanymore, on_fail) do { \
180 newarray = trunnel_dynarray_expand(&(da)->allocated_, \
181 (da)->elts_, (howmanymore), \
183 if (newarray == NULL) { \
185 goto trunnel_alloc_failed; \
187 (da)->elts_ = newarray; \
192#define TRUNNEL_DYNARRAY_ADD(elttype, da, v, on_fail) do { \
193 if ((da)->n_ == (da)->allocated_) { \
194 TRUNNEL_DYNARRAY_EXPAND(elttype, da, 1, on_fail); \
196 (da)->elts_[(da)->n_++] = (v); \
200#define TRUNNEL_DYNARRAY_LEN(da) ((da)->n_)
204#define TRUNNEL_DYNARRAY_CLEAR(da) do { \
205 trunnel_free((da)->elts_); \
206 (da)->elts_ = NULL; \
207 (da)->n_ = (da)->allocated_ = 0; \
212#define TRUNNEL_DYNARRAY_WIPE(da) do { \
213 trunnel_memwipe((da)->elts_, (da)->allocated_ * sizeof((da)->elts_[0])); \
219#ifndef trunnel_reallocarray
220void *trunnel_reallocarray(
void *a,
size_t x,
size_t y);
231void *trunnel_dynarray_expand(
size_t *allocated_p,
void *ptr,
232 size_t howmanymore,
size_t eltsize);
235typedef void (*trunnel_free_fn_t)(
void *);
250void *trunnel_dynarray_setlen(
size_t *allocated_p,
size_t *len_p,
251 void *ptr,
size_t newlen,
252 size_t eltsize, trunnel_free_fn_t free_fn,
253 uint8_t *errcode_ptr);
260const char *trunnel_string_getstr(trunnel_string_t *str);
268int trunnel_string_setstr0(trunnel_string_t *str,
const char *inp,
size_t len,
269 uint8_t *errcode_ptr);
275int trunnel_string_setlen(trunnel_string_t *str,
size_t newlen,
276 uint8_t *errcode_ptr);