2019-06-05 15:57:52 +02:00
|
|
|
/* cell_common.c -- generated by Trunnel v1.5.3.
|
2016-08-29 21:28:58 +02:00
|
|
|
* 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__)
|
2017-08-24 22:13:01 +02:00
|
|
|
/* If we're running a static analysis tool, we don't want it to complain
|
2016-08-29 21:28:58 +02:00
|
|
|
* 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)
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_t *
|
|
|
|
trn_cell_extension_fields_new(void)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_t *val = trunnel_calloc(1, sizeof(trn_cell_extension_fields_t));
|
2016-08-29 21:28:58 +02:00
|
|
|
if (NULL == val)
|
|
|
|
return NULL;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release all storage held inside 'obj', but do not free 'obj'.
|
|
|
|
*/
|
|
|
|
static void
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_clear(trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
(void) obj;
|
|
|
|
TRUNNEL_DYNARRAY_WIPE(&obj->field);
|
|
|
|
TRUNNEL_DYNARRAY_CLEAR(&obj->field);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_free(trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
if (obj == NULL)
|
|
|
|
return;
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_clear(obj);
|
|
|
|
trunnel_memwipe(obj, sizeof(trn_cell_extension_fields_t));
|
2016-08-29 21:28:58 +02:00
|
|
|
trunnel_free_(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_get_field_type(const trn_cell_extension_fields_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return inp->field_type;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_set_field_type(trn_cell_extension_fields_t *inp, uint8_t val)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
inp->field_type = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_get_field_len(const trn_cell_extension_fields_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return inp->field_len;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_set_field_len(trn_cell_extension_fields_t *inp, uint8_t val)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
inp->field_len = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_getlen_field(const trn_cell_extension_fields_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return TRUNNEL_DYNARRAY_LEN(&inp->field);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_get_field(trn_cell_extension_fields_t *inp, size_t idx)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return TRUNNEL_DYNARRAY_GET(&inp->field, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_getconst_field(const trn_cell_extension_fields_t *inp, size_t idx)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
return trn_cell_extension_fields_get_field((trn_cell_extension_fields_t*)inp, idx);
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_set_field(trn_cell_extension_fields_t *inp, size_t idx, uint8_t elt)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
TRUNNEL_DYNARRAY_SET(&inp->field, idx, elt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_add_field(trn_cell_extension_fields_t *inp, uint8_t elt)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
#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 *
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_getarray_field(trn_cell_extension_fields_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return inp->field.elts_;
|
|
|
|
}
|
|
|
|
const uint8_t *
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_getconstarray_field(const trn_cell_extension_fields_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
return (const uint8_t *)trn_cell_extension_fields_getarray_field((trn_cell_extension_fields_t*)inp);
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_setlen_field(trn_cell_extension_fields_t *inp, size_t newlen)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
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 *
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_check(const trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
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
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_encoded_len(const trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result = 0;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
if (NULL != trn_cell_extension_fields_check(obj))
|
2016-08-29 21:28:58 +02:00
|
|
|
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
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_clear_errors(trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
int r = obj->trunnel_error_code_;
|
|
|
|
obj->trunnel_error_code_ = 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
ssize_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_encode(uint8_t *output, const size_t avail, const trn_cell_extension_fields_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result = 0;
|
|
|
|
size_t written = 0;
|
|
|
|
uint8_t *ptr = output;
|
|
|
|
const char *msg;
|
|
|
|
#ifdef TRUNNEL_CHECK_ENCODED_LEN
|
2017-04-11 19:46:41 +02:00
|
|
|
const ssize_t encoded_len = trn_cell_extension_fields_encoded_len(obj);
|
2016-08-29 21:28:58 +02:00
|
|
|
#endif
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
if (NULL != (msg = trn_cell_extension_fields_check(obj)))
|
2016-08-29 21:28:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
/** As trn_cell_extension_fields_parse(), but do not allocate the
|
|
|
|
* output object.
|
2016-08-29 21:28:58 +02:00
|
|
|
*/
|
|
|
|
static ssize_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_parse_into(trn_cell_extension_fields_t *obj, const uint8_t *input, const size_t len_in)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
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
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_parse(trn_cell_extension_fields_t **output, const uint8_t *input, const size_t len_in)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result;
|
2017-04-11 19:46:41 +02:00
|
|
|
*output = trn_cell_extension_fields_new();
|
2016-08-29 21:28:58 +02:00
|
|
|
if (NULL == *output)
|
|
|
|
return -1;
|
2017-04-11 19:46:41 +02:00
|
|
|
result = trn_cell_extension_fields_parse_into(*output, input, len_in);
|
2016-08-29 21:28:58 +02:00
|
|
|
if (result < 0) {
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_free(*output);
|
2016-08-29 21:28:58 +02:00
|
|
|
*output = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_t *
|
|
|
|
trn_cell_extension_new(void)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_t *val = trunnel_calloc(1, sizeof(trn_cell_extension_t));
|
2016-08-29 21:28:58 +02:00
|
|
|
if (NULL == val)
|
|
|
|
return NULL;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release all storage held inside 'obj', but do not free 'obj'.
|
|
|
|
*/
|
|
|
|
static void
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_clear(trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
(void) obj;
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned idx;
|
|
|
|
for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_free(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TRUNNEL_DYNARRAY_WIPE(&obj->fields);
|
|
|
|
TRUNNEL_DYNARRAY_CLEAR(&obj->fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_free(trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
if (obj == NULL)
|
|
|
|
return;
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_clear(obj);
|
|
|
|
trunnel_memwipe(obj, sizeof(trn_cell_extension_t));
|
2016-08-29 21:28:58 +02:00
|
|
|
trunnel_free_(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_get_num(const trn_cell_extension_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return inp->num;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_set_num(trn_cell_extension_t *inp, uint8_t val)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
inp->num = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_getlen_fields(const trn_cell_extension_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return TRUNNEL_DYNARRAY_LEN(&inp->fields);
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
struct trn_cell_extension_fields_st *
|
|
|
|
trn_cell_extension_get_fields(trn_cell_extension_t *inp, size_t idx)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
const struct trn_cell_extension_fields_st *
|
|
|
|
trn_cell_extension_getconst_fields(const trn_cell_extension_t *inp, size_t idx)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
return trn_cell_extension_get_fields((trn_cell_extension_t*)inp, idx);
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_set_fields(trn_cell_extension_t *inp, size_t idx, struct trn_cell_extension_fields_st * elt)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
|
2016-08-29 21:28:58 +02:00
|
|
|
if (oldval && oldval != elt)
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_free(oldval);
|
|
|
|
return trn_cell_extension_set0_fields(inp, idx, elt);
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_set0_fields(trn_cell_extension_t *inp, size_t idx, struct trn_cell_extension_fields_st * elt)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
TRUNNEL_DYNARRAY_SET(&inp->fields, idx, elt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_add_fields(trn_cell_extension_t *inp, struct trn_cell_extension_fields_st * elt)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
#if SIZE_MAX >= UINT8_MAX
|
|
|
|
if (inp->fields.n_ == UINT8_MAX)
|
|
|
|
goto trunnel_alloc_failed;
|
|
|
|
#endif
|
2017-04-11 19:46:41 +02:00
|
|
|
TRUNNEL_DYNARRAY_ADD(struct trn_cell_extension_fields_st *, &inp->fields, elt, {});
|
2016-08-29 21:28:58 +02:00
|
|
|
return 0;
|
|
|
|
trunnel_alloc_failed:
|
|
|
|
TRUNNEL_SET_ERROR_CODE(inp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
struct trn_cell_extension_fields_st * *
|
|
|
|
trn_cell_extension_getarray_fields(trn_cell_extension_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
return inp->fields.elts_;
|
|
|
|
}
|
2017-04-11 19:46:41 +02:00
|
|
|
const struct trn_cell_extension_fields_st * const *
|
|
|
|
trn_cell_extension_getconstarray_fields(const trn_cell_extension_t *inp)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
return (const struct trn_cell_extension_fields_st * const *)trn_cell_extension_getarray_fields((trn_cell_extension_t*)inp);
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_setlen_fields(trn_cell_extension_t *inp, size_t newlen)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
struct trn_cell_extension_fields_st * *newptr;
|
2016-08-29 21:28:58 +02:00
|
|
|
#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,
|
2017-04-11 19:46:41 +02:00
|
|
|
sizeof(inp->fields.elts_[0]), (trunnel_free_fn_t) trn_cell_extension_fields_free,
|
2016-08-29 21:28:58 +02:00
|
|
|
&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 *
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_check(const trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
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) {
|
2017-04-11 19:46:41 +02:00
|
|
|
if (NULL != (msg = trn_cell_extension_fields_check(TRUNNEL_DYNARRAY_GET(&obj->fields, idx))))
|
2016-08-29 21:28:58 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (TRUNNEL_DYNARRAY_LEN(&obj->fields) != obj->num)
|
|
|
|
return "Length mismatch for fields";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_encoded_len(const trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result = 0;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
if (NULL != trn_cell_extension_check(obj))
|
2016-08-29 21:28:58 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Length of u8 num */
|
|
|
|
result += 1;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
/* Length of struct trn_cell_extension_fields fields[num] */
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
unsigned idx;
|
|
|
|
for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
|
2017-04-11 19:46:41 +02:00
|
|
|
result += trn_cell_extension_fields_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
int
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_clear_errors(trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
int r = obj->trunnel_error_code_;
|
|
|
|
obj->trunnel_error_code_ = 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
ssize_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_encode(uint8_t *output, const size_t avail, const trn_cell_extension_t *obj)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result = 0;
|
|
|
|
size_t written = 0;
|
|
|
|
uint8_t *ptr = output;
|
|
|
|
const char *msg;
|
|
|
|
#ifdef TRUNNEL_CHECK_ENCODED_LEN
|
2017-04-11 19:46:41 +02:00
|
|
|
const ssize_t encoded_len = trn_cell_extension_encoded_len(obj);
|
2016-08-29 21:28:58 +02:00
|
|
|
#endif
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
if (NULL != (msg = trn_cell_extension_check(obj)))
|
2016-08-29 21:28:58 +02:00
|
|
|
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;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
/* Encode struct trn_cell_extension_fields fields[num] */
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
unsigned idx;
|
|
|
|
for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
|
|
|
|
trunnel_assert(written <= avail);
|
2017-04-11 19:46:41 +02:00
|
|
|
result = trn_cell_extension_fields_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
|
2016-08-29 21:28:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
/** As trn_cell_extension_parse(), but do not allocate the output
|
|
|
|
* object.
|
2016-08-29 21:28:58 +02:00
|
|
|
*/
|
|
|
|
static ssize_t
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_parse_into(trn_cell_extension_t *obj, const uint8_t *input, const size_t len_in)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
/* Parse struct trn_cell_extension_fields fields[num] */
|
|
|
|
TRUNNEL_DYNARRAY_EXPAND(trn_cell_extension_fields_t *, &obj->fields, obj->num, {});
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_fields_t * elt;
|
2016-08-29 21:28:58 +02:00
|
|
|
unsigned idx;
|
|
|
|
for (idx = 0; idx < obj->num; ++idx) {
|
2017-04-11 19:46:41 +02:00
|
|
|
result = trn_cell_extension_fields_parse(&elt, ptr, remaining);
|
2016-08-29 21:28:58 +02:00
|
|
|
if (result < 0)
|
|
|
|
goto relay_fail;
|
|
|
|
trunnel_assert((size_t)result <= remaining);
|
|
|
|
remaining -= result; ptr += result;
|
2017-04-11 19:46:41 +02:00
|
|
|
TRUNNEL_DYNARRAY_ADD(trn_cell_extension_fields_t *, &obj->fields, elt, {trn_cell_extension_fields_free(elt);});
|
2016-08-29 21:28:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_parse(trn_cell_extension_t **output, const uint8_t *input, const size_t len_in)
|
2016-08-29 21:28:58 +02:00
|
|
|
{
|
|
|
|
ssize_t result;
|
2017-04-11 19:46:41 +02:00
|
|
|
*output = trn_cell_extension_new();
|
2016-08-29 21:28:58 +02:00
|
|
|
if (NULL == *output)
|
|
|
|
return -1;
|
2017-04-11 19:46:41 +02:00
|
|
|
result = trn_cell_extension_parse_into(*output, input, len_in);
|
2016-08-29 21:28:58 +02:00
|
|
|
if (result < 0) {
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_free(*output);
|
2016-08-29 21:28:58 +02:00
|
|
|
*output = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|