librlp

C library for the Recursive Length Prefix (RLP) serialization format
git clone git://git.defalsify.org/librlp.git
Log | Files | Refs | LICENSE

commit f356053b1490a28f8a6027f6276b0adaac374282
parent 2aff2fe9a2fd3c69a964dbcaa116f4ce673c42f1
Author: nolash <dev@holbrook.no>
Date:   Sun, 11 Apr 2021 09:33:58 +0200

Add size counter

Diffstat:
Msrc/decode.c | 5+++++
Msrc/encode.c | 12++++++++++--
Msrc/rlp.c | 9+++------
Msrc/rlp.h | 1+
Mtests/check_decoder.c | 7+++++++
Mtests/check_rlp.c | 15+++++++++++++++
Mtests/check_vectors.c | 8++++----
7 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/src/decode.c b/src/decode.c @@ -0,0 +1,5 @@ +#include "rlp.h" + +int rlp_next(rlp_encoder_t *encoder) { + +} diff --git a/src/encode.c b/src/encode.c @@ -16,11 +16,13 @@ int rlp_add(rlp_encoder_t *encoder, int len, const char *data) { if (len == 0) { *(encoder->ptr) = 0x80; encoder->ptr++; + encoder->size++; } else { v = (char)*data; if (len == 1 && v < 56) { *(encoder->ptr) = v; encoder->ptr++; + encoder->size++; } else { v = (char)*data; if (len < 56) { @@ -28,6 +30,7 @@ int rlp_add(rlp_encoder_t *encoder, int len, const char *data) { encoder->ptr++; memcpy(encoder->ptr, data, len); encoder->ptr += len; + encoder->size++; } else { lenlen = intbytes_le(sizeof(int), (char*)&len); *(encoder->ptr) = lenlen + 0xb7; @@ -35,12 +38,15 @@ int rlp_add(rlp_encoder_t *encoder, int len, const char *data) { to_endian(CONVERT_BIGENDIAN, lenlen, &len); memcpy(encoder->ptr, &len, lenlen); to_endian(CONVERT_PLATFORM, lenlen, &len); - encoder->ptr+=lenlen; + encoder->ptr += lenlen; memcpy(encoder->ptr, data, len); - encoder->ptr+=len; + encoder->ptr += len; + encoder->size += 1 + lenlen; } } } + + encoder->size += len; return (int)(r - (size_t)encoder->ptr); } @@ -62,6 +68,7 @@ int rlp_ascend(rlp_encoder_t *encoder) { memcpy(ptr + 1, ptr, len); *(ptr) = 0xc0 + len; encoder->ptr++; + encoder->size++; } else { lenlen = intbytes_le(sizeof(size_t), (char*)&len); memcpy(ptr + 1 + lenlen, ptr, len); @@ -70,6 +77,7 @@ int rlp_ascend(rlp_encoder_t *encoder) { memcpy(ptr+1, &len, lenlen); to_endian(CONVERT_PLATFORM, lenlen, &len); encoder->ptr += (lenlen + 1); + encoder->size + 1 + lenlen; } encoder->depth--; diff --git a/src/rlp.c b/src/rlp.c @@ -9,11 +9,13 @@ int rlp_init(rlp_encoder_t *encoder, int buffer_capacity, char *content) { encoder->alloc = 0; encoder->state = RLP_DECODE; encoder->ptr = encoder->buf + buffer_capacity; + encoder->size = buffer_capacity; } else { encoder->buf = malloc(buffer_capacity); encoder->alloc = 1; encoder->state = RLP_ENCODE; encoder->ptr = encoder->buf; + encoder->size = 0; } } @@ -24,13 +26,8 @@ void rlp_free(rlp_encoder_t *encoder) { encoder->ptr = NULL; } -size_t rlp_length(rlp_encoder_t *encoder) { - return encoder->ptr - encoder->buf; -} - - int rlp_get(rlp_encoder_t *encoder, int *zl, char **zdest) { *zdest = encoder->buf; - *zl = rlp_length(encoder); + *zl = encoder->size; return 0; } diff --git a/src/rlp.h b/src/rlp.h @@ -20,6 +20,7 @@ enum rlp_state { typedef struct rlp_encoder { char *buf; int depth; + int size; enum rlp_state state; char *list_ptr[RLP_MAX_LIST_DEPTH]; char *ptr; diff --git a/tests/check_decoder.c b/tests/check_decoder.c @@ -6,6 +6,13 @@ START_TEST(rlp_decode_single) { + char s[5] = {0x84, 0x66, 0x6f, 0x6f, 0x0a}; + char target[] = "foo"; + rlp_encoder_t encoder; + + rlp_init(&encoder, 5, s); + + rlp_free(&encoder); } END_TEST diff --git a/tests/check_rlp.c b/tests/check_rlp.c @@ -8,11 +8,26 @@ START_TEST(rlp_init_alloc_test) { rlp_init(&encoder, 1024, NULL); + ck_assert_int_eq(encoder.state, RLP_ENCODE); + rlp_free(&encoder); } END_TEST +START_TEST(rlp_init_noalloc_test) { + char something[] = "foo"; + + rlp_encoder_t encoder; + + rlp_init(&encoder, 4, something); + + ck_assert_int_eq(encoder.state, RLP_DECODE); + + rlp_free(&encoder); +} +END_TEST + Suite *rlp_init_suite(void) { Suite *s; TCase *tc; diff --git a/tests/check_vectors.c b/tests/check_vectors.c @@ -12,7 +12,7 @@ START_TEST(rlp_dog_test) { char r_dog[4] = {0x83, 'd', 'o', 'g'}; rlp_add(&encoder, 3, x_dog); ck_assert_mem_eq(encoder.buf, r_dog, 4); - ck_assert_int_eq(rlp_length(&encoder), 4); + ck_assert_int_eq(encoder.size, 4); rlp_free(&encoder); } @@ -31,7 +31,7 @@ START_TEST(rlp_catdog_test) { rlp_add(&encoder, 3, x_dog); rlp_ascend(&encoder); ck_assert_mem_eq(encoder.buf, r_catdog, 9); - ck_assert_int_eq(rlp_length(&encoder), 9); + ck_assert_int_eq(encoder.size, 9); rlp_free(&encoder); } @@ -48,7 +48,7 @@ START_TEST(rlp_lorem_test) { rlp_add(&encoder, strlen(lorem), lorem); ck_assert_mem_eq(encoder.buf, target, 2); ck_assert_mem_eq(encoder.buf+2, lorem, strlen(lorem)); - ck_assert_int_eq(rlp_length(&encoder), 2 + strlen(lorem)); + ck_assert_int_eq(encoder.size, 2 + strlen(lorem)); rlp_free(&encoder); } @@ -85,7 +85,7 @@ START_TEST(rlp_set_theoretical_representation_of_three) { rlp_ascend(&encoder); ck_assert_mem_eq(encoder.buf, target, 8); - ck_assert_int_eq(rlp_length(&encoder), 8); + ck_assert_int_eq(encoder.size, 8); rlp_free(&encoder); }