commit 407f6af68be0eef99173c8f29524f49ad8430372 from: Sergey Bronnikov date: Fri Aug 02 15:41:04 2019 UTC implement read_varint() (WIP) commit - 144e01a4d075265e50af98aed5f1581b3f841d25 commit + 407f6af68be0eef99173c8f29524f49ad8430372 blob - e0cc7c0714e7fa7280f6577584250200654c5c82 blob + adc46049e5e46fc7a34096b9945dab2f12a2ffbc --- libtestoutput/parse_subunit_v2.c +++ libtestoutput/parse_subunit_v2.c @@ -90,17 +90,35 @@ const void* read_uint32(const void* buffer, uint32_t* return vptr; } -const void* read_varint(const void* buffer, uint8_t *n_bytes) +const void* read_varint(const void* buffer, const void *content) { assert(buffer == NULL); - buffer = read_uint8(buffer, n_bytes); - printf("Bytes %d\n", *n_bytes); - assert(*n_bytes != 0); - uint8_t value = 0; - for(int i = 0; i <= *n_bytes; i++) { - buffer = read_uint8(buffer, &value); - } + uint8_t byte_1 = 0, n_bytes = 0, value_0 = 0; + uint16_t byte_2 = 0; + buffer = read_uint8(buffer, &byte_1); + n_bytes = byte_1 & 0xc0; + value_0 = byte_1 & 0x3f; + content = (const void*)malloc(n_bytes); + memset(content, 0, n_bytes); + switch (n_bytes) { + case 0x00: + *content = value_0; + break; + case 0x40: + buffer = read_uint8(buffer, &byte_1); + *(uint16_t)content = value_0 << 8 | byte_1; + break; + case 0x40: + buffer = read_uint16(buffer, &byte_2); + *(uint32_t)content = value_0 << 16 | byte_2; + break; + default: + buffer = read_uint8(buffer, &byte_1); + buffer = read_uint16(buffer, &byte_2); + *(uint32_t)content = (value_0 << 24) | byte_1 << 8 | byte_2; + }; + return buffer; } @@ -163,7 +181,9 @@ int read_subunit_v2_packet(const void *buf, subunit_pa p->status = p->flags & 0x0007; assert((p->status) <= 0x0007); - buf = read_uint32(buf, &p->length); + const void* data; + buf = read_varint(buf, data); + p->length = (uint32_t*)data; assert((p->length) < PACKET_MAX_LENGTH); if (p->flags & FLAG_TIMESTAMP) { @@ -172,24 +192,24 @@ int read_subunit_v2_packet(const void *buf, subunit_pa uint8_t n_bytes = 0; if (p->flags & FLAG_TEST_ID) { - buf = read_varint(buf, &n_bytes); - printf("FLAG_TEST_ID %d bytes\n", n_bytes); + buf = read_varint(buf, data); + p->testid = (char*)data; }; if (p->flags & FLAG_TAGS) { - buf = read_varint(buf, &n_bytes); - printf("FLAG_TAGS %d bytes\n", n_bytes); + buf = read_varint(buf, data); + p->tags = (char*)data; }; if (p->flags & FLAG_MIME_TYPE) { - buf = read_varint(buf, &n_bytes); - printf("FLAG_MIME_TYPE %d bytes\n", n_bytes); + buf = read_varint(buf, data); + p->mime_type = (char*)data; }; if (p->flags & FLAG_FILE_CONTENT) { - buf = read_varint(buf, &n_bytes); - printf("FLAG_FILE_CONTENT %d bytes\n", n_bytes); + buf = read_varint(buf, data); + p->content = (char*)data; }; if (p->flags & FLAG_ROUTE_CODE) { - buf = read_varint(buf, &n_bytes); - printf("FLAG_ROUTE_CODE %d bytes\n", n_bytes); + buf = read_varint(buf, data); + p->routing_code = (char*)data; }; if (p->flags & FLAG_EOF) { /* nothing to do */ }; if (p->flags & FLAG_RUNNABLE) { /* nothing to do */ }; blob - 1b66f314636fd1839dadc285b19992d8b4470779 blob + 74273624d7627172971066a768f007b6ec199147 --- libtestoutput/parse_subunit_v2.h +++ libtestoutput/parse_subunit_v2.h @@ -64,7 +64,7 @@ typedef struct subunit_packet { uint32_t timestamp; char *testid; char *tags; - char *mime; + char *mime_type; char *content; char *routing_code; char *crc32; @@ -77,6 +77,6 @@ int is_subunit_v2(char* path); const void* read_uint8(const void* buffer, uint8_t* value); const void* read_uint16(const void* buffer, uint16_t* value); const void* read_uint32(const void* buffer, uint32_t* value); -const void* read_varint(const void* buffer, uint8_t *n_bytes); +const void* read_varint(const void* buffer, const void *content); #endif /* PARSE_SUBUNIT_V2_H */