librlp

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

commit 669b086a12b9388f5f8d1fee015b7d9a0a63168b
parent 7d40a4322f39977483a1a87c0b88e26dbe7072cb
Author: nolash <dev@holbrook.no>
Date:   Sun, 11 Apr 2021 11:31:55 +0200

Add long string decode

Diffstat:
Msrc/decode.c | 23++++++++++++++++++++---
Mtests/check_decoder.c | 13++++++++-----
2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/decode.c b/src/decode.c @@ -9,19 +9,36 @@ static int process_state_token(rlp_encoder_t *encoder) { token = *encoder->ptr; - if (token >= 0xc0) { + if (token >= 0xb7) { + lenlen = token - 0xb7; + encoder->ptr++; + len = 0; + memcpy(&len, encoder->ptr, lenlen); + if (lenlen > 1 && is_le()) { + to_endian(CONVERT_LITTLEENDIAN, lenlen, &len); + } + encoder->ptr += lenlen; + + encoder->list_ptr[encoder->depth] = encoder->ptr; + r = encoder->depth; + encoder->ptr += len; + encoder->depth--; + encoder->state = RLP_STRING; } else if (token >= 0x80) { len = token - 0x80; encoder->ptr++; + encoder->list_ptr[encoder->depth] = encoder->ptr; r = encoder->depth; - encoder->depth--; encoder->ptr += len; + encoder->depth--; + encoder->state = RLP_STRING; } else { encoder->list_ptr[encoder->depth] = encoder->ptr; r = encoder->depth; - encoder->depth--; encoder->ptr++; + encoder->depth--; + encoder->state = RLP_STRING; } return r; diff --git a/tests/check_decoder.c b/tests/check_decoder.c @@ -52,8 +52,9 @@ START_TEST(rlp_decode_long_string_test) { int l; int r; - char s[57]; - s[0] = 0xb7 + 56; + char s[58]; + s[0] = 0xb8; + s[1] = 56; char buf[1024]; char *zbuf = buf; @@ -61,9 +62,11 @@ START_TEST(rlp_decode_long_string_test) { rlp_encoder_t encoder; rlp_init(&encoder, 57, s); + r = rlp_next(&encoder, &l, &zbuf); + ck_assert_int_eq(l, 56); ck_assert_int_eq(r, 0); - ck_assert_mem_eq(buf, s+1, l); + ck_assert_mem_eq(zbuf, s+2, l); rlp_free(&encoder); @@ -76,9 +79,9 @@ Suite *rlp_decode_suite(void) { s = suite_create("rlp_decode"); tcb = tcase_create("bytes"); - //tcase_add_test(tcb, rlp_decode_single_test); + tcase_add_test(tcb, rlp_decode_single_test); tcase_add_test(tcb, rlp_decode_short_string_test); - //tcase_add_test(tcb, rlp_decode_string_test); + tcase_add_test(tcb, rlp_decode_long_string_test); suite_add_tcase(s, tcb);