librlp

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

commit 0642605191720e53ab5bdbefbfccc4806fe3617a
parent b9673535b29c4aeee9231cf37e2f6ad0b2cda342
Author: nolash <dev@holbrook.no>
Date:   Sun, 11 Apr 2021 12:20:36 +0200

Add long list decoder

Diffstat:
Msrc/decode.c | 21+++++++++++++++++++--
Mtests/check_decoder.c | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/src/decode.c b/src/decode.c @@ -1,3 +1,5 @@ +#include <string.h> + #include "endian.h" #include "rlp.h" @@ -9,12 +11,27 @@ static int process_state_token(rlp_encoder_t *encoder) { token = *encoder->ptr; - if (token >= 0xc0) { + if (token >= 0xf7) { + lenlen = token - 0xf7; + encoder->ptr++; + len = 0; + memcpy(&len, encoder->ptr, lenlen); + if (lenlen > 1 && is_le()) { + to_endian(CONVERT_LITTLEENDIAN, lenlen, &len); + } + encoder->list_ptr[encoder->depth] = (char*)&len; + encoder->ptr += lenlen; + + r = encoder->depth; + encoder->depth++; + encoder->state = RLP_LIST; + + } else if (token >= 0xc0) { len = token - 0xc0; if (is_le()) { to_endian(CONVERT_LITTLEENDIAN, sizeof(len), &len); } - encoder->list_ptr[encoder->depth] = &len; + encoder->list_ptr[encoder->depth] = (char*)&len; encoder->ptr++; r = encoder->depth; diff --git a/tests/check_decoder.c b/tests/check_decoder.c @@ -83,7 +83,7 @@ START_TEST(rlp_decode_long_string_test) { END_TEST -START_TEST(rlp_decode_short_list_test) { +START_TEST(rlp_decode_zero_list_test) { int l; int r; @@ -106,6 +106,56 @@ START_TEST(rlp_decode_short_list_test) { END_TEST +START_TEST(rlp_decode_short_list_test) { + int l; + int r; + + char s[2] = {0xc1, 0x80}; + char buf[2]; + char *zbuf = &buf; + + char state = RLP_LIST; + + rlp_encoder_t encoder; + + rlp_init(&encoder, 2, s); + r = rlp_next(&encoder, &l, &zbuf); + ck_assert_mem_eq(&encoder.state, &state, 1); + ck_assert_int_eq(l, 1); + ck_assert_int_eq(r, 0); + + rlp_free(&encoder); +} +END_TEST + + +START_TEST(rlp_decode_long_list_test) { + int l; + int r; + + char s[58]; + s[0] = 0xf8; + s[1] = 56; + s[2] = 0x80 + 55; + + char buf[56]; + char *zbuf = &buf; + + char state = RLP_LIST; + + rlp_encoder_t encoder; + + rlp_init(&encoder, 58, s); + r = rlp_next(&encoder, &l, &zbuf); + ck_assert_mem_eq(&encoder.state, &state, 1); + ck_assert_int_eq(l, 56); + ck_assert_int_eq(r, 0); + + rlp_free(&encoder); +} +END_TEST + + Suite *rlp_decode_suite(void) { Suite *s; TCase *tcb; @@ -115,15 +165,16 @@ Suite *rlp_decode_suite(void) { tcb = tcase_create("bytes"); tcl = tcase_create("list"); - tcase_add_test(tcb, rlp_decode_single_test); - tcase_add_test(tcb, rlp_decode_short_string_test); - tcase_add_test(tcb, rlp_decode_long_string_test); + //tcase_add_test(tcb, rlp_decode_single_test); + //tcase_add_test(tcb, rlp_decode_short_string_test); + //tcase_add_test(tcb, rlp_decode_long_string_test); suite_add_tcase(s, tcb); + tcase_add_test(tcl, rlp_decode_zero_list_test); tcase_add_test(tcl, rlp_decode_short_list_test); + tcase_add_test(tcl, rlp_decode_long_list_test); suite_add_tcase(s, tcl); - return s; }