LCOV - code coverage report
Current view: top level - ext/trunnel - trunnel-impl.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 26 26 100.0 %
Date: 2021-11-24 03:28:48 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /* trunnel-impl.h -- copied from Trunnel v1.5.3
       2             :  * https://gitweb.torproject.org/trunnel.git
       3             :  * You probably shouldn't edit this file.
       4             :  */
       5             : /* trunnel-impl.h -- Implementation helpers for trunnel, included by
       6             :  * generated trunnel files
       7             :  *
       8             :  * Copyright 2014-2019, The Tor Project, Inc.
       9             :  * See license at the end of this file for copying information.
      10             :  */
      11             : 
      12             : #ifndef TRUNNEL_IMPL_H_INCLUDED_
      13             : #define TRUNNEL_IMPL_H_INCLUDED_
      14             : #ifdef TRUNNEL_LOCAL_H
      15             : #include "trunnel-local.h"
      16             : #endif
      17             : #include "trunnel.h"
      18             : #include <assert.h>
      19             : #include <string.h>
      20             : #include <stdlib.h>
      21             : 
      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
      28             : #else
      29             : #include <stdint.h>
      30             : #endif
      31             : 
      32             : #ifdef _WIN32
      33             : uint32_t trunnel_htonl(uint32_t a);
      34             : uint32_t trunnel_ntohl(uint32_t a);
      35             : uint16_t trunnel_htons(uint16_t a);
      36             : uint16_t trunnel_ntohs(uint16_t a);
      37             : #else
      38             : #include <arpa/inet.h>
      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)
      43             : #endif
      44             : uint64_t trunnel_htonll(uint64_t a);
      45             : uint64_t trunnel_ntohll(uint64_t a);
      46             : 
      47             : #ifndef trunnel_assert
      48             : #define trunnel_assert(x) assert(x)
      49             : #endif
      50             : 
      51             : static inline void
      52          14 : trunnel_set_uint64(void *p, uint64_t v) {
      53          14 :   memcpy(p, &v, 8);
      54          14 : }
      55             : static inline void
      56        1058 : trunnel_set_uint32(void *p, uint32_t v) {
      57        1058 :   memcpy(p, &v, 4);
      58        1058 : }
      59             : static inline void
      60        1447 : trunnel_set_uint16(void *p, uint16_t v) {
      61        1447 :   memcpy(p, &v, 2);
      62        1447 : }
      63             : static inline void
      64        7649 : trunnel_set_uint8(void *p, uint8_t v) {
      65        7649 :   memcpy(p, &v, 1);
      66        7649 : }
      67             : 
      68             : static inline uint64_t
      69          14 : trunnel_get_uint64(const void *p) {
      70          14 :   uint64_t x;
      71          14 :   memcpy(&x, p, 8);
      72          14 :   return x;
      73             : }
      74             : static inline uint32_t
      75        1839 : trunnel_get_uint32(const void *p) {
      76        1839 :   uint32_t x;
      77        1839 :   memcpy(&x, p, 4);
      78        1839 :   return x;
      79             : }
      80             : static inline uint16_t
      81        2883 : trunnel_get_uint16(const void *p) {
      82        2883 :   uint16_t x;
      83        2883 :   memcpy(&x, p, 2);
      84        2883 :   return x;
      85             : }
      86             : static inline uint8_t
      87       76484 : trunnel_get_uint8(const void *p) {
      88       76484 :   return *(const uint8_t*)p;
      89             : }
      90             : 
      91             : 
      92             : #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
      93             : extern int trunnel_provoke_alloc_failure;
      94             : 
      95             : static inline void *
      96             : trunnel_malloc(size_t n)
      97             : {
      98             :    if (trunnel_provoke_alloc_failure) {
      99             :      if (--trunnel_provoke_alloc_failure == 0)
     100             :        return NULL;
     101             :    }
     102             :    return malloc(n);
     103             : }
     104             : static inline void *
     105             : trunnel_calloc(size_t a, size_t b)
     106             : {
     107             :    if (trunnel_provoke_alloc_failure) {
     108             :      if (--trunnel_provoke_alloc_failure == 0)
     109             :        return NULL;
     110             :    }
     111             :    return calloc(a,b);
     112             : }
     113             : static inline char *
     114             : trunnel_strdup(const char *s)
     115             : {
     116             :    if (trunnel_provoke_alloc_failure) {
     117             :      if (--trunnel_provoke_alloc_failure == 0)
     118             :        return NULL;
     119             :    }
     120             :    return strdup(s);
     121             : }
     122             : #else
     123             : #ifndef trunnel_malloc
     124             : #define trunnel_malloc(x) (malloc((x)))
     125             : #endif
     126             : #ifndef trunnel_calloc
     127             : #define trunnel_calloc(a,b) (calloc((a),(b)))
     128             : #endif
     129             : #ifndef trunnel_strdup
     130             : #define trunnel_strdup(s) (strdup((s)))
     131             : #endif
     132             : #endif
     133             : 
     134             : #ifndef trunnel_realloc
     135             : #define trunnel_realloc(a,b) realloc((a),(b))
     136             : #endif
     137             : 
     138             : #ifndef trunnel_free_
     139             : #define trunnel_free_(x) (free(x))
     140             : #endif
     141             : #define trunnel_free(x) ((x) ? (trunnel_free_(x),0) : (0))
     142             : 
     143             : #ifndef trunnel_abort
     144             : #define trunnel_abort() abort()
     145             : #endif
     146             : 
     147             : #ifndef trunnel_memwipe
     148             : #define trunnel_memwipe(mem, len) ((void)0)
     149             : #define trunnel_wipestr(s) ((void)0)
     150             : #else
     151             : #define trunnel_wipestr(s) do {                 \
     152             :     if (s)                                      \
     153             :       trunnel_memwipe(s, strlen(s));            \
     154             :   } while (0)
     155             : #endif
     156             : 
     157             : /* ====== dynamic arrays ======== */
     158             : 
     159             : #ifdef NDEBUG
     160             : #define TRUNNEL_DYNARRAY_GET(da, n)             \
     161             :   ((da)->elts_[(n)])
     162             : #else
     163             : /** Return the 'n'th element of 'da'. */
     164             : #define TRUNNEL_DYNARRAY_GET(da, n)             \
     165             :   (((n) >= (da)->n_ ? (trunnel_abort(),0) : 0), (da)->elts_[(n)])
     166             : #endif
     167             : 
     168             : /** Change the 'n'th element of 'da' to 'v'. */
     169             : #define TRUNNEL_DYNARRAY_SET(da, n, v) do {                    \
     170             :     trunnel_assert((n) < (da)->n_);                            \
     171             :     (da)->elts_[(n)] = (v);                                    \
     172             :   } while (0)
     173             : 
     174             : /** Expand the dynamic array 'da' of 'elttype' so that it can hold at least
     175             :  * 'howmanymore' elements than its current capacity. Always tries to increase
     176             :  * the length of the array. On failure, run the code in 'on_fail' and goto
     177             :  * trunnel_alloc_failed. */
     178             : #define TRUNNEL_DYNARRAY_EXPAND(elttype, da, howmanymore, on_fail) do { \
     179             :     elttype *newarray;                                               \
     180             :     newarray = trunnel_dynarray_expand(&(da)->allocated_,            \
     181             :                                        (da)->elts_, (howmanymore),   \
     182             :                                        sizeof(elttype));             \
     183             :     if (newarray == NULL) {                                          \
     184             :       on_fail;                                                       \
     185             :       goto trunnel_alloc_failed;                                     \
     186             :     }                                                                \
     187             :     (da)->elts_ = newarray;                                          \
     188             :   } while (0)
     189             : 
     190             : /** Add 'v' to the end of the dynamic array 'da' of 'elttype', expanding it if
     191             :  * necessary.  code in 'on_fail' and goto trunnel_alloc_failed. */
     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);  \
     195             :       }                                                    \
     196             :       (da)->elts_[(da)->n_++] = (v);                       \
     197             :     } while (0)
     198             : 
     199             : /** Return the number of elements in 'da'. */
     200             : #define TRUNNEL_DYNARRAY_LEN(da) ((da)->n_)
     201             : 
     202             : /** Remove all storage held by 'da' and set it to be empty.  Does not free
     203             :  * storage held by the elements themselves. */
     204             : #define TRUNNEL_DYNARRAY_CLEAR(da) do {           \
     205             :     trunnel_free((da)->elts_);                    \
     206             :     (da)->elts_ = NULL;                           \
     207             :     (da)->n_ = (da)->allocated_ = 0;              \
     208             :   } while (0)
     209             : 
     210             : /** Remove all storage held by 'da' and set it to be empty.  Does not free
     211             :  * storage held by the elements themselves. */
     212             : #define TRUNNEL_DYNARRAY_WIPE(da) do {                                  \
     213             :     trunnel_memwipe((da)->elts_, (da)->allocated_ * sizeof((da)->elts_[0])); \
     214             :   } while (0)
     215             : 
     216             : /** Helper: wraps or implements an OpenBSD-style reallocarray.  Behaves
     217             :  * as realloc(a, x*y), but verifies that no overflow will occur in the
     218             :  * multiplication. Returns NULL on failure. */
     219             : #ifndef trunnel_reallocarray
     220             : void *trunnel_reallocarray(void *a, size_t x, size_t y);
     221             : #endif
     222             : 
     223             : /** Helper to expand a dynamic array. Behaves as TRUNNEL_DYNARRAY_EXPAND(),
     224             :  * taking the array of elements in 'ptr', a pointer to thethe current number
     225             :  * of allocated elements in allocated_p, the minimum numbeer of elements to
     226             :  * add in 'howmanymore', and the size of a single element in 'eltsize'.
     227             :  *
     228             :  * On success, adjust *allocated_p, and return the new value for the array of
     229             :  * elements.  On failure, adjust nothing and return NULL.
     230             :  */
     231             : void *trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
     232             :                               size_t howmanymore, size_t eltsize);
     233             : 
     234             : /** Type for a function to free members of a dynarray of pointers. */
     235             : typedef void (*trunnel_free_fn_t)(void *);
     236             : 
     237             : /**
     238             :  * Helper to change the length of a dynamic array. Takes pointers to the
     239             :  * current allocated and n fields of the array in 'allocated_p' and 'len_p',
     240             :  * and the current array of elements in 'ptr'; takes the length of a single
     241             :  * element in 'eltsize'.  Changes the length to 'newlen'.  If 'newlen' is
     242             :  * greater than the current length, pads the new elements with 0.  If newlen
     243             :  * is less than the current length, and free_fn is non-NULL, treat the
     244             :  * array as an array of void *, and invoke free_fn() on each removed element.
     245             :  *
     246             :  * On success, adjust *allocated_p and *len_p, and return the new value for
     247             :  * the array of elements.  On failure, adjust nothing, set *errcode_ptr to 1,
     248             :  * and return NULL.
     249             :  */
     250             : void *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);
     254             : 
     255             : /**
     256             :  * Helper: return a pointer to the value of 'str' as a NUL-terminated string.
     257             :  * Might have to reallocate the storage for 'str' in order to fit in the final
     258             :  * NUL character.  On allocation failure, return NULL.
     259             :  */
     260             : const char *trunnel_string_getstr(trunnel_string_t *str);
     261             : 
     262             : /**
     263             :  * Helper: change the contents of 'str' to hold the 'len'-byte string in
     264             :  * 'inp'.  Adjusts the storage to have a terminating NUL that doesn't count
     265             :  * towards the length of the string.  On success, return 0.  On failure, set
     266             :  * *errcode_ptr to 1 and return -1.
     267             :  */
     268             : int trunnel_string_setstr0(trunnel_string_t *str, const char *inp, size_t len,
     269             :                            uint8_t *errcode_ptr);
     270             : 
     271             : /**
     272             :  * As trunnel_dynarray_setlen, but adjusts a string rather than a dynamic
     273             :  * array, and ensures that the new string is NUL-terminated.
     274             :  */
     275             : int trunnel_string_setlen(trunnel_string_t *str, size_t newlen,
     276             :                            uint8_t *errcode_ptr);
     277             : 
     278             : #endif
     279             : 
     280             : 
     281             : /*
     282             : Copyright 2014  The Tor Project, Inc.
     283             : 
     284             : Redistribution and use in source and binary forms, with or without
     285             : modification, are permitted provided that the following conditions are
     286             : met:
     287             : 
     288             :     * Redistributions of source code must retain the above copyright
     289             : notice, this list of conditions and the following disclaimer.
     290             : 
     291             :     * Redistributions in binary form must reproduce the above
     292             : copyright notice, this list of conditions and the following disclaimer
     293             : in the documentation and/or other materials provided with the
     294             : distribution.
     295             : 
     296             :     * Neither the names of the copyright owners nor the names of its
     297             : contributors may be used to endorse or promote products derived from
     298             : this software without specific prior written permission.
     299             : 
     300             : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     301             : "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     302             : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     303             : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     304             : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     305             : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     306             : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     307             : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     308             : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     309             : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     310             : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     311             : */

Generated by: LCOV version 1.14