123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- /* cell_common.c -- generated by Trunnel v1.5.
- * https://gitweb.torproject.org/trunnel.git
- * You probably shouldn't edit this file.
- */
- #include <stdlib.h>
- #include "trunnel-impl.h"
- #include "cell_common.h"
- #define TRUNNEL_SET_ERROR_CODE(obj) \
- do { \
- (obj)->trunnel_error_code_ = 1; \
- } while (0)
- #if defined(__COVERITY__) || defined(__clang_analyzer__)
- /* If we're runnning a static analysis tool, we don't want it to complain
- * that some of our remaining-bytes checks are dead-code. */
- int cellcommon_deadcode_dummy__ = 0;
- #define OR_DEADCODE_DUMMY || cellcommon_deadcode_dummy__
- #else
- #define OR_DEADCODE_DUMMY
- #endif
- #define CHECK_REMAINING(nbytes, label) \
- do { \
- if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
- goto label; \
- } \
- } while (0)
- cell_extension_fields_t *
- cell_extension_fields_new(void)
- {
- cell_extension_fields_t *val = trunnel_calloc(1, sizeof(cell_extension_fields_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- cell_extension_fields_clear(cell_extension_fields_t *obj)
- {
- (void) obj;
- TRUNNEL_DYNARRAY_WIPE(&obj->field);
- TRUNNEL_DYNARRAY_CLEAR(&obj->field);
- }
- void
- cell_extension_fields_free(cell_extension_fields_t *obj)
- {
- if (obj == NULL)
- return;
- cell_extension_fields_clear(obj);
- trunnel_memwipe(obj, sizeof(cell_extension_fields_t));
- trunnel_free_(obj);
- }
- uint8_t
- cell_extension_fields_get_field_type(cell_extension_fields_t *inp)
- {
- return inp->field_type;
- }
- int
- cell_extension_fields_set_field_type(cell_extension_fields_t *inp, uint8_t val)
- {
- inp->field_type = val;
- return 0;
- }
- uint8_t
- cell_extension_fields_get_field_len(cell_extension_fields_t *inp)
- {
- return inp->field_len;
- }
- int
- cell_extension_fields_set_field_len(cell_extension_fields_t *inp, uint8_t val)
- {
- inp->field_len = val;
- return 0;
- }
- size_t
- cell_extension_fields_getlen_field(const cell_extension_fields_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->field);
- }
- uint8_t
- cell_extension_fields_get_field(cell_extension_fields_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->field, idx);
- }
- uint8_t
- cell_extension_fields_getconst_field(const cell_extension_fields_t *inp, size_t idx)
- {
- return cell_extension_fields_get_field((cell_extension_fields_t*)inp, idx);
- }
- int
- cell_extension_fields_set_field(cell_extension_fields_t *inp, size_t idx, uint8_t elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->field, idx, elt);
- return 0;
- }
- int
- cell_extension_fields_add_field(cell_extension_fields_t *inp, uint8_t elt)
- {
- #if SIZE_MAX >= UINT8_MAX
- if (inp->field.n_ == UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->field, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- uint8_t *
- cell_extension_fields_getarray_field(cell_extension_fields_t *inp)
- {
- return inp->field.elts_;
- }
- const uint8_t *
- cell_extension_fields_getconstarray_field(const cell_extension_fields_t *inp)
- {
- return (const uint8_t *)cell_extension_fields_getarray_field((cell_extension_fields_t*)inp);
- }
- int
- cell_extension_fields_setlen_field(cell_extension_fields_t *inp, size_t newlen)
- {
- uint8_t *newptr;
- #if UINT8_MAX < SIZE_MAX
- if (newlen > UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->field.allocated_,
- &inp->field.n_, inp->field.elts_, newlen,
- sizeof(inp->field.elts_[0]), (trunnel_free_fn_t) NULL,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->field.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- cell_extension_fields_check(const cell_extension_fields_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- if (TRUNNEL_DYNARRAY_LEN(&obj->field) != obj->field_len)
- return "Length mismatch for field";
- return NULL;
- }
- ssize_t
- cell_extension_fields_encoded_len(const cell_extension_fields_t *obj)
- {
- ssize_t result = 0;
- if (NULL != cell_extension_fields_check(obj))
- return -1;
- /* Length of u8 field_type */
- result += 1;
- /* Length of u8 field_len */
- result += 1;
- /* Length of u8 field[field_len] */
- result += TRUNNEL_DYNARRAY_LEN(&obj->field);
- return result;
- }
- int
- cell_extension_fields_clear_errors(cell_extension_fields_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- cell_extension_fields_encode(uint8_t *output, const size_t avail, const cell_extension_fields_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = cell_extension_fields_encoded_len(obj);
- #endif
- if (NULL != (msg = cell_extension_fields_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 field_type */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->field_type));
- written += 1; ptr += 1;
- /* Encode u8 field_len */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->field_len));
- written += 1; ptr += 1;
- /* Encode u8 field[field_len] */
- {
- size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->field);
- trunnel_assert(obj->field_len == elt_len);
- trunnel_assert(written <= avail);
- if (avail - written < elt_len)
- goto truncated;
- if (elt_len)
- memcpy(ptr, obj->field.elts_, elt_len);
- written += elt_len; ptr += elt_len;
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As cell_extension_fields_parse(), but do not allocate the output
- * object.
- */
- static ssize_t
- cell_extension_fields_parse_into(cell_extension_fields_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 field_type */
- CHECK_REMAINING(1, truncated);
- obj->field_type = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse u8 field_len */
- CHECK_REMAINING(1, truncated);
- obj->field_len = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse u8 field[field_len] */
- CHECK_REMAINING(obj->field_len, truncated);
- TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->field, obj->field_len, {});
- obj->field.n_ = obj->field_len;
- if (obj->field_len)
- memcpy(obj->field.elts_, ptr, obj->field_len);
- ptr += obj->field_len; remaining -= obj->field_len;
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- cell_extension_fields_parse(cell_extension_fields_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = cell_extension_fields_new();
- if (NULL == *output)
- return -1;
- result = cell_extension_fields_parse_into(*output, input, len_in);
- if (result < 0) {
- cell_extension_fields_free(*output);
- *output = NULL;
- }
- return result;
- }
- cell_extension_t *
- cell_extension_new(void)
- {
- cell_extension_t *val = trunnel_calloc(1, sizeof(cell_extension_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- cell_extension_clear(cell_extension_t *obj)
- {
- (void) obj;
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
- cell_extension_fields_free(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
- }
- }
- TRUNNEL_DYNARRAY_WIPE(&obj->fields);
- TRUNNEL_DYNARRAY_CLEAR(&obj->fields);
- }
- void
- cell_extension_free(cell_extension_t *obj)
- {
- if (obj == NULL)
- return;
- cell_extension_clear(obj);
- trunnel_memwipe(obj, sizeof(cell_extension_t));
- trunnel_free_(obj);
- }
- uint8_t
- cell_extension_get_num(cell_extension_t *inp)
- {
- return inp->num;
- }
- int
- cell_extension_set_num(cell_extension_t *inp, uint8_t val)
- {
- inp->num = val;
- return 0;
- }
- size_t
- cell_extension_getlen_fields(const cell_extension_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->fields);
- }
- struct cell_extension_fields_st *
- cell_extension_get_fields(cell_extension_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
- }
- const struct cell_extension_fields_st *
- cell_extension_getconst_fields(const cell_extension_t *inp, size_t idx)
- {
- return cell_extension_get_fields((cell_extension_t*)inp, idx);
- }
- int
- cell_extension_set_fields(cell_extension_t *inp, size_t idx, struct cell_extension_fields_st * elt)
- {
- cell_extension_fields_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
- if (oldval && oldval != elt)
- cell_extension_fields_free(oldval);
- return cell_extension_set0_fields(inp, idx, elt);
- }
- int
- cell_extension_set0_fields(cell_extension_t *inp, size_t idx, struct cell_extension_fields_st * elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->fields, idx, elt);
- return 0;
- }
- int
- cell_extension_add_fields(cell_extension_t *inp, struct cell_extension_fields_st * elt)
- {
- #if SIZE_MAX >= UINT8_MAX
- if (inp->fields.n_ == UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(struct cell_extension_fields_st *, &inp->fields, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- struct cell_extension_fields_st * *
- cell_extension_getarray_fields(cell_extension_t *inp)
- {
- return inp->fields.elts_;
- }
- const struct cell_extension_fields_st * const *
- cell_extension_getconstarray_fields(const cell_extension_t *inp)
- {
- return (const struct cell_extension_fields_st * const *)cell_extension_getarray_fields((cell_extension_t*)inp);
- }
- int
- cell_extension_setlen_fields(cell_extension_t *inp, size_t newlen)
- {
- struct cell_extension_fields_st * *newptr;
- #if UINT8_MAX < SIZE_MAX
- if (newlen > UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->fields.allocated_,
- &inp->fields.n_, inp->fields.elts_, newlen,
- sizeof(inp->fields.elts_[0]), (trunnel_free_fn_t) cell_extension_fields_free,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->fields.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- cell_extension_check(const cell_extension_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- {
- const char *msg;
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
- if (NULL != (msg = cell_extension_fields_check(TRUNNEL_DYNARRAY_GET(&obj->fields, idx))))
- return msg;
- }
- }
- if (TRUNNEL_DYNARRAY_LEN(&obj->fields) != obj->num)
- return "Length mismatch for fields";
- return NULL;
- }
- ssize_t
- cell_extension_encoded_len(const cell_extension_t *obj)
- {
- ssize_t result = 0;
- if (NULL != cell_extension_check(obj))
- return -1;
- /* Length of u8 num */
- result += 1;
- /* Length of struct cell_extension_fields fields[num] */
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
- result += cell_extension_fields_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
- }
- }
- return result;
- }
- int
- cell_extension_clear_errors(cell_extension_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- cell_extension_encode(uint8_t *output, const size_t avail, const cell_extension_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = cell_extension_encoded_len(obj);
- #endif
- if (NULL != (msg = cell_extension_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 num */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->num));
- written += 1; ptr += 1;
- /* Encode struct cell_extension_fields fields[num] */
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
- trunnel_assert(written <= avail);
- result = cell_extension_fields_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
- if (result < 0)
- goto fail; /* XXXXXXX !*/
- written += result; ptr += result;
- }
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As cell_extension_parse(), but do not allocate the output object.
- */
- static ssize_t
- cell_extension_parse_into(cell_extension_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 num */
- CHECK_REMAINING(1, truncated);
- obj->num = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse struct cell_extension_fields fields[num] */
- TRUNNEL_DYNARRAY_EXPAND(cell_extension_fields_t *, &obj->fields, obj->num, {});
- {
- cell_extension_fields_t * elt;
- unsigned idx;
- for (idx = 0; idx < obj->num; ++idx) {
- result = cell_extension_fields_parse(&elt, ptr, remaining);
- if (result < 0)
- goto relay_fail;
- trunnel_assert((size_t)result <= remaining);
- remaining -= result; ptr += result;
- TRUNNEL_DYNARRAY_ADD(cell_extension_fields_t *, &obj->fields, elt, {cell_extension_fields_free(elt);});
- }
- }
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- relay_fail:
- trunnel_assert(result < 0);
- return result;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- cell_extension_parse(cell_extension_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = cell_extension_new();
- if (NULL == *output)
- return -1;
- result = cell_extension_parse_into(*output, input, len_in);
- if (result < 0) {
- cell_extension_free(*output);
- *output = NULL;
- }
- return result;
- }
|