commit 2e8af3a5d55f7c7f4d97f298c8a3ad88373c2cfe
parent 7ae29157d604ee1f7a9744af5d2a23a6bb08b761
Author: nolash <dev@holbrook.no>
Date: Sun, 11 Apr 2021 13:00:36 +0200
Add reciprocal decode to all vector tests
Diffstat:
3 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/src/decode.c b/src/decode.c
@@ -72,12 +72,26 @@ static int process_state_token(rlp_encoder_t *encoder) {
return r;
}
+static int assert_complete(rlp_encoder_t *encoder) {
+ int r = 0;
+
+ if (encoder->ptr - encoder->buf < encoder->size) {
+ encoder->state = RLP_INCOMPLETE;
+ r = -1;
+ } else if (encoder->ptr - encoder->buf > encoder->size) {
+ encoder->state = RLP_OVERFLOW;
+ r = -1;
+ } else {
+ encoder->state = RLP_END;
+ }
+ return r;
+}
+
int rlp_next(rlp_encoder_t *encoder, int *zlen, char **zdest) {
int r;
if (encoder->depth == -1) {
- encoder->state = RLP_END;
- return -1;
+ return assert_complete(encoder);
}
r = process_state_token(encoder);
diff --git a/src/rlp.h b/src/rlp.h
@@ -7,13 +7,18 @@
#define RLP_MAX_LIST_DEPTH 1024
#endif
+#define RLP_SUCCESS 0
+#define RLP_FAILURE -1
+
+
enum rlp_state {
RLP_ENCODE,
RLP_DECODE,
RLP_LIST,
RLP_STRING,
RLP_END,
- RLP_ERR,
+ RLP_OVERFLOW,
+ RLP_INCOMPLETE,
};
diff --git a/tests/check_vectors.c b/tests/check_vectors.c
@@ -21,7 +21,7 @@ START_TEST(rlp_dog_test) {
ck_assert_int_eq(encoder.size, 4);
// reuse for decode
- rlp_init(&encoder, 1024, encoder.buf);
+ rlp_init(&encoder, 4, encoder.buf);
rlp_next(&encoder, &l, &zbuf);
ck_assert_mem_eq(&encoder.state, &state, 1);
ck_assert_int_eq(l, 3);
@@ -57,7 +57,7 @@ START_TEST(rlp_catdog_test) {
// reuse for decode
state = RLP_LIST;
- rlp_init(&encoder, 1024, encoder.buf);
+ rlp_init(&encoder, 9, encoder.buf);
rlp_next(&encoder, &l, &zbuf);
ck_assert_mem_eq(&encoder.state, &state, 1);
ck_assert_int_eq(l, 8);
@@ -88,7 +88,7 @@ START_TEST(rlp_lorem_test) {
char state;
char *lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
- char target[2] = {0xb8, 0x38};
+ char target[2] = {0xb8, strlen(lorem)};
rlp_init(&encoder, 1024, NULL);
rlp_add(&encoder, strlen(lorem), lorem);
@@ -98,10 +98,10 @@ START_TEST(rlp_lorem_test) {
// reuse for decode
state = RLP_STRING;
- rlp_init(&encoder, 1024, encoder.buf);
+ rlp_init(&encoder, strlen(lorem) + 2, encoder.buf);
rlp_next(&encoder, &l, &zbuf);
ck_assert_mem_eq(&encoder.state, &state, 1);
- ck_assert_int_eq(l, 0x38);
+ ck_assert_int_eq(l, strlen(lorem));
ck_assert_mem_eq(zbuf, lorem, l);
state = RLP_END;