Skip to content
Snippets Groups Projects
Commit 5227f1ae authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Add support for bytearray

Differently than all other types, the length must be set before
parsing because the BPP does not contain the length data, instead
the length is specified by the message definition.
parent 366a8e26
No related branches found
No related tags found
No related merge requests found
Pipeline #7374 passed
......@@ -34,6 +34,38 @@ deuce_ssh_serialize_byte(deuce_ssh_byte_t val, uint8_t *buf, MAYBE_UNUSED size_t
*pos += 1;
}
ssize_t
deuce_ssh_parse_bytearray(uint8_t * buf, size_t bufsz, deuce_ssh_bytearray_t val)
{
ssize_t ret;
uint32_t len;
size_t pos = 0;
assert(val->length > 1);
if (val->length < 2)
return DEUCE_SSH_ERROR_PARSE;
assert(bufsz >= val->length);
if (bufsz < val->length)
return DEUCE_SSH_ERROR_PARSE;
ret = val->length;
val->value = buf;
return ret;
}
size_t
deuce_ssh_serialized_bytearray_length(deuce_ssh_bytearray_t val)
{
return val->length + 4;
}
void
deuce_ssh_serialize_bytearray(deuce_ssh_bytearray_t val, uint8_t *buf, MAYBE_UNUSED size_t bufsz, size_t *pos)
{
assert(*pos + val->length <= bufsz);
memcpy(buf, val->value, val->length);
*pos += val->length;
}
ssize_t
deuce_ssh_parse_boolean(uint8_t * buf, size_t bufsz, deuce_ssh_boolean_t *val)
{
......@@ -140,7 +172,7 @@ deuce_ssh_parse_string(uint8_t * buf, size_t bufsz, deuce_ssh_string_t val)
if (bufsz < sz)
return DEUCE_SSH_ERROR_PARSE;
val->length = len;
memcpy(&val->value, &buf[ret], len);
val->value = &buf[ret];
return sz;
}
......
......@@ -23,6 +23,7 @@ typedef struct deuce_ssh_string {
deuce_ssh_byte_t *value;
deuce_ssh_uint32_t length;
} *deuce_ssh_string_t;
typedef struct deuce_ssh_string *deuce_ssh_bytearray_t;
typedef struct deuce_ssh_string *deuce_ssh_mpint_t;
typedef struct deuce_ssh_namelist {
deuce_ssh_byte_t *value;
......@@ -32,6 +33,7 @@ typedef struct deuce_ssh_namelist {
#define deuce_ssh_parse(buf, bufsz, val) _Generic(val, \
deuce_ssh_byte_t : deuce_ssh_parse_byte(buf, bufsz, val), \
deuce_ssh_bytearray_t : deuce_ssh_parse_bytearray(buf, bufsz, val), \
deuce_ssh_boolean_t : deuce_ssh_parse_boolean(buf, bufsz, val), \
deuce_ssh_uint32_t : deuce_ssh_parse_uint32(buf, bufsz, val), \
deuce_ssh_uint64_t : deuce_ssh_parse_uint64(buf, bufsz, val), \
......@@ -41,6 +43,7 @@ typedef struct deuce_ssh_namelist {
#define deuce_ssh_serialized_length(val) _Generic(val, \
deuce_ssh_byte_t : deuce_ssh_serialized_byte_length(val), \
deuce_ssh_bytearray_t : deuce_ssh_serialized_byte_length(val), \
deuce_ssh_boolean_t : deuce_ssh_serialized_boolean_length(val), \
deuce_ssh_uint32_t : deuce_ssh_serialized_uint32_length(val), \
deuce_ssh_uint64_t : deuce_ssh_serialized_uint64_length(val), \
......@@ -50,6 +53,7 @@ typedef struct deuce_ssh_namelist {
#define deuce_ssh_serialize(val, buf, bufsz, pos) _Generic(val, \
deuce_ssh_byte_t : deuce_ssh_serialize_byte(val, buf, bufsz, pos), \
deuce_ssh_bytearray_t : deuce_ssh_serialize_byte(val, buf, bufsz, pos), \
deuce_ssh_boolean_t : deuce_ssh_serialize_boolean(val, buf, bufsz, pos), \
deuce_ssh_uint32_t : deuce_ssh_serialize_uint32(val, buf, bufsz, pos), \
deuce_ssh_uint64_t : deuce_ssh_serialize_uint64(val, buf, bufsz, pos), \
......@@ -61,6 +65,11 @@ ssize_t deuce_ssh_parse_byte(uint8_t * buf, size_t bufsz, deuce_ssh_byte_t *val)
size_t deuce_ssh_serialized_byte_length(deuce_ssh_byte_t val);
void deuce_ssh_serialize_byte(deuce_ssh_byte_t val, uint8_t *buf, MAYBE_UNUSED size_t bufsz, size_t *pos);
// A byte array is different because val->length *must* be set before parsing
ssize_t deuce_ssh_parse_bytearray(uint8_t * buf, size_t bufsz, deuce_ssh_bytearray_t val);
size_t deuce_ssh_serialized_bytearray_length(deuce_ssh_bytearray_t val);
void deuce_ssh_serialize_bytearray(deuce_ssh_bytearray_t val, uint8_t *buf, MAYBE_UNUSED size_t bufsz, size_t *pos);
ssize_t deuce_ssh_parse_boolean(uint8_t * buf, size_t bufsz, deuce_ssh_boolean_t *val);
size_t deuce_ssh_serialized_boolean_length(deuce_ssh_boolean_t val);
void deuce_ssh_serialize_boolean(deuce_ssh_boolean_t val, uint8_t *buf, MAYBE_UNUSED size_t bufsz, size_t *pos);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment