commit e30bcf1715477de22c3559380d21874ab73488c9
parent b8b11a437b08cd949b112ebcd0e45bcf8f3a534e
Author: nolash <dev@holbrook.no>
Date: Mon, 12 Apr 2021 01:07:37 +0200
Update librlp files
Diffstat:
3 files changed, 42 insertions(+), 62 deletions(-)
diff --git a/aux/src/decode.c b/aux/src/decode.c
@@ -6,12 +6,16 @@
static int process_state_token(rlp_encoder_t *encoder) {
int len = 0;
int lenlen = 0;
- int r;
unsigned char token;
token = *encoder->ptr;
- if (token >= 0xf7) {
+ if (encoder->ptr == encoder->list_ptr[encoder->depth]) {
+ encoder->depth--;
+ encoder->state = RLP_LIST_ASCEND;
+ len = 0;
+
+ } else if (token >= 0xf7) {
lenlen = token - 0xf7;
encoder->ptr++;
len = 0;
@@ -19,24 +23,25 @@ static int process_state_token(rlp_encoder_t *encoder) {
if (lenlen > 1 && is_le()) {
to_endian(CONVERT_LITTLEENDIAN, lenlen, &len);
}
- encoder->list_ptr[encoder->depth] = (char*)&len;
+ encoder->depth++;
encoder->ptr += lenlen;
+ encoder->buf = encoder->ptr;
+ encoder->list_ptr[encoder->depth] = encoder->buf + len;
- r = encoder->depth;
- encoder->depth++;
- encoder->state = RLP_LIST;
+ encoder->state = RLP_LIST_DESCEND;
} else if (token >= 0xc0) {
len = token - 0xc0;
if (is_le()) {
to_endian(CONVERT_LITTLEENDIAN, sizeof(len), &len);
}
- encoder->list_ptr[encoder->depth] = (char*)&len;
+ encoder->depth++;
encoder->ptr++;
+ encoder->buf = encoder->ptr;
+ encoder->list_ptr[encoder->depth] = encoder->buf + len;
+
+ encoder->state = RLP_LIST_DESCEND;
- r = encoder->depth;
- encoder->depth++;
- encoder->state = RLP_LIST;
} else if (token >= 0xb7) {
lenlen = token - 0xb7;
encoder->ptr++;
@@ -47,65 +52,39 @@ static int process_state_token(rlp_encoder_t *encoder) {
}
encoder->ptr += lenlen;
- encoder->list_ptr[encoder->depth] = encoder->ptr;
- r = encoder->depth;
+ encoder->buf = encoder->ptr;
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->buf = encoder->ptr;
encoder->ptr += len;
- encoder->depth--;
encoder->state = RLP_STRING;
+
} else {
encoder->list_ptr[encoder->depth] = encoder->ptr;
- r = encoder->depth;
+ encoder->buf = encoder->ptr;
encoder->ptr++;
- encoder->depth--;
+ len = 1;
encoder->state = RLP_STRING;
}
-
- 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;
+
+ return len;
}
int rlp_next(rlp_encoder_t *encoder, int *zlen, char **zdest) {
int r;
-
- if (encoder->depth == -1) {
- return assert_complete(encoder);
- }
- r = process_state_token(encoder);
-
- switch (encoder->state) {
- case RLP_LIST:
- *zlen = *((int*)encoder->list_ptr[r]);
- break;
+ if (encoder->list_ptr[0] == encoder->ptr) {
+ encoder->state = RLP_END;
+ return -1;
+ }
- case RLP_STRING:
- *zlen = encoder->ptr - encoder->list_ptr[r];
- break;
- }
- *zdest = encoder->list_ptr[r];
+ *zlen = process_state_token(encoder);
+ *zdest = encoder->buf;
return 0;
}
diff --git a/aux/src/rlp.c b/aux/src/rlp.c
@@ -9,13 +9,15 @@ int rlp_init(rlp_encoder_t *encoder, int buffer_capacity, char *content) {
encoder->alloc = 0;
encoder->state = RLP_DECODE;
encoder->size = buffer_capacity;
- } else {
+ encoder->ptr = encoder->buf;
+ encoder->list_ptr[0] = encoder->buf + buffer_capacity;
+ } else {
encoder->buf = malloc(buffer_capacity);
encoder->alloc = 1;
encoder->state = RLP_ENCODE;
encoder->size = 0;
+ encoder->ptr = encoder->buf;
}
- encoder->ptr = encoder->buf;
}
void rlp_free(rlp_encoder_t *encoder) {
@@ -25,8 +27,8 @@ void rlp_free(rlp_encoder_t *encoder) {
encoder->ptr = NULL;
}
-int rlp_get(rlp_encoder_t *encoder, int *zl, char **zdest) {
- *zdest = encoder->buf;
- *zl = encoder->size;
- return 0;
-}
+//int rlp_get(rlp_encoder_t *encoder, int *zl, char **zdest) {
+// *zdest = encoder->buf;
+// *zl = encoder->size;
+// return 0;
+//}
diff --git a/aux/src/rlp.h b/aux/src/rlp.h
@@ -14,11 +14,10 @@
enum rlp_state {
RLP_DECODE,
RLP_ENCODE,
- RLP_LIST,
+ RLP_LIST_ASCEND,
+ RLP_LIST_DESCEND,
RLP_STRING,
RLP_END,
- RLP_OVERFLOW,
- RLP_INCOMPLETE,
};
@@ -36,12 +35,12 @@ typedef struct rlp_encoder {
int rlp_init(rlp_encoder_t *encoder, int buffer_capacity, char *content);
void rlp_free(rlp_encoder_t *encoder);
+// encode
int rlp_descend(rlp_encoder_t *encoder);
int rlp_ascend(rlp_encoder_t *encoder);
int rlp_add(rlp_encoder_t *encoder, int len, const char *data);
-size_t rlp_length(rlp_encoder_t *encoder);
-int rlp_get(rlp_encoder_t *encoder, int *zl, char **zdest);
+// decode
int rlp_next(rlp_encoder_t *encoder, int *zl, char **zdest);
#endif