diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-07 10:18:58 -0500 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-07 10:18:58 -0500 |
commit | 925c4fa86368f019c683be18a641b660a42d06e5 (patch) | |
tree | e19e7c54c8e7368adb98c07b0bdcb0b1b94c6b2a /src/parser.c | |
parent | 73efe8f2d78c780ddf8be80909572807fab71ef7 (diff) |
Finished bytecode parser
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/parser.c b/src/parser.c index 4cd26fa..54e7ecc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,9 +7,11 @@ int blen; static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value); +static char *getpair(uint8_t *buf, int *pos); struct byte *parse(uint8_t *buf) { + char *value; int len = 2; struct byte *bytes = calloc(len, sizeof(struct byte)); for (int i = 0, j = 0; buf[i]; ++i) { @@ -17,12 +19,21 @@ struct byte *parse(uint8_t *buf) len *= 2; bytes = realloc(bytes, len * sizeof(struct byte)); switch (buf[i]) { - case '\xb': + case 250: + value = getpair(buf, &i); + if (value == NULL) + addbyte(bytes, &j, ERROR, "Invalid Key-Value pair"); + else + addbyte(bytes, &j, PAIR, value); + break; + case 251: addbyte(bytes, &j, BEGIN, NULL); break; - case '\xe': + case 254: addbyte(bytes, &j, END, NULL); break; + default: + addbyte(bytes, &j, ERROR, "Invalid chunk"); } blen = j; } @@ -34,11 +45,27 @@ static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value) bytes[*pos].type = type; if (value != NULL) { - bytes[*pos].value = calloc(strlen(value) + 1, sizeof(char)); - strcpy(bytes[*pos].value, value); + bytes[*pos].value = value; } else bytes[*pos].value = NULL; (*pos)++; } + +static char *getpair(uint8_t *buf, int *pos) +{ + int i; + for (i = *pos + 1; buf[i] != 250 && buf[i] != 254 && buf[i]; ++i) + ; + if (buf[i] != 250 && buf[i] != 254) + return NULL; + + char *pair = calloc(i - *pos, sizeof(char)); + int j; + for (j = *pos + 1; j < i; ++j) + pair[j - (*pos + 1)] = buf[j]; + pair[j - (*pos + 1)] = '\0'; + *pos = i - 1; + return pair; +} |