From 9f6e34103aeb44fafdcd8d4878281c783b7c9ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Ma=C5=BEeikis?= Date: Tue, 28 May 2019 20:36:14 +0300 Subject: Protocol: new functions and associated docs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The header declares new functions: * tlv_init() * tlv_destroy() * tlv_push() * tlv_get_raw() * tlv_raw_size() Signed-off-by: Ramūnas Mažeikis --- src/common/protocol.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/common/protocol.c') diff --git a/src/common/protocol.c b/src/common/protocol.c index b2e0d40..edaf491 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -131,3 +131,35 @@ size_t tlv_data_size(struct tlv_parser *parser) } return size; } + +void tlv_destroy(struct tlv *t) +{ + free(t->data); + t->length = 0; +} + +size_t tlv_raw_size(struct tlv *t) +{ + return sizeof(*t) + t->length; +} + +int tlv_push(struct tlv *t, const char *data, size_t size) +{ + int ret = 0; + size_t final_size = tlv_raw_size(t) + size; + if (final_size > TLV_SZ_MAX_RAW) { + ret = E_TLV_OVERFLOW; + } else { + t->data = realloc(t->data, final_size); + memcpy(t->data + t->length, data, size); + t->length = final_size; + } + return ret; +} + +void tlv_init(struct tlv *t) +{ + t->type = TEXT; + t->length = 0; + t->data = NULL; +} \ No newline at end of file -- cgit v1.2.3