diff --git a/src/userland/buffersTests/include/buffers.h b/src/userland/buffersTests/include/buffers.h index f947364..2351129 100644 --- a/src/userland/buffersTests/include/buffers.h +++ b/src/userland/buffersTests/include/buffers.h @@ -30,8 +30,8 @@ #define FORMATS(X, S) \ X(POSITIVE_FIXINT, INTEGER, 0x00, 0x7F, Inline, 0x7F) S \ - X(FIXMAP, MAP, 0x80, 0x8F, InlineLength, 0x0F) S \ - X(FIXARRAY, ARRAY, 0x90, 0x9F, InlineLength, 0x0F) S \ + X(FIXMAP, MAP, 0x80, 0x8F, ElementsInline, 0x0F) S \ + X(FIXARRAY, ARRAY, 0x90, 0x9F, ElementsInline, 0x0F) S \ X(FIXSTR, STRING, 0xa0, 0xBF, InlineLength, 0x1F) S \ X(NIL, NIL, 0xC0, 0xC0, Inline, 0x00) S \ X(UNUSED, UNUSED, 0xC1, 0xC1, Inline, 0x00) S \ @@ -62,8 +62,8 @@ X(STR32, STRING, 0xDB, 0xDB, ReadLength, 4) S \ X(ARRAY16, ARRAY, 0xDC, 0xDC, ReadLength, 2) S \ X(ARRAY32, ARRAY, 0xDD, 0xDD, ReadLength, 4) S \ - X(MAP16, MAP, 0xDE, 0xDE, ReadLength, 2) S \ - X(MAP32, MAP, 0xDF, 0xDF, ReadLength, 4) S \ + X(MAP16, MAP, 0xDE, 0xDE, ReadElements, 2) S \ + X(MAP32, MAP, 0xDF, 0xDF, ReadElements, 4) S \ X(NEGATIVE_FIXINT, INTEGER, 0xE0, 0xFF, Inline, 0x1F) // define enums diff --git a/src/userland/buffersTests/include/buffers.h b/src/userland/buffersTests/include/buffers.h index f947364..2351129 100644 --- a/src/userland/buffersTests/include/buffers.h +++ b/src/userland/buffersTests/include/buffers.h @@ -30,8 +30,8 @@ #define FORMATS(X, S) \ X(POSITIVE_FIXINT, INTEGER, 0x00, 0x7F, Inline, 0x7F) S \ - X(FIXMAP, MAP, 0x80, 0x8F, InlineLength, 0x0F) S \ - X(FIXARRAY, ARRAY, 0x90, 0x9F, InlineLength, 0x0F) S \ + X(FIXMAP, MAP, 0x80, 0x8F, ElementsInline, 0x0F) S \ + X(FIXARRAY, ARRAY, 0x90, 0x9F, ElementsInline, 0x0F) S \ X(FIXSTR, STRING, 0xa0, 0xBF, InlineLength, 0x1F) S \ X(NIL, NIL, 0xC0, 0xC0, Inline, 0x00) S \ X(UNUSED, UNUSED, 0xC1, 0xC1, Inline, 0x00) S \ @@ -62,8 +62,8 @@ X(STR32, STRING, 0xDB, 0xDB, ReadLength, 4) S \ X(ARRAY16, ARRAY, 0xDC, 0xDC, ReadLength, 2) S \ X(ARRAY32, ARRAY, 0xDD, 0xDD, ReadLength, 4) S \ - X(MAP16, MAP, 0xDE, 0xDE, ReadLength, 2) S \ - X(MAP32, MAP, 0xDF, 0xDF, ReadLength, 4) S \ + X(MAP16, MAP, 0xDE, 0xDE, ReadElements, 2) S \ + X(MAP32, MAP, 0xDF, 0xDF, ReadElements, 4) S \ X(NEGATIVE_FIXINT, INTEGER, 0xE0, 0xFF, Inline, 0x1F) // define enums diff --git a/src/userland/buffersTests/main.c b/src/userland/buffersTests/main.c index 57c2b20..d48f68a 100644 --- a/src/userland/buffersTests/main.c +++ b/src/userland/buffersTests/main.c @@ -28,7 +28,7 @@ int32_t readInteger(uint8_t *data, Formats format, FormatInfo *info) { if (format == FORMAT_NEGATIVE_FIXINT) { - return ((int8_t) (*data | ~0x1F)); + return ((int8_t) (*data | ~info->readTypeParameter)); } if (format == FORMAT_POSITIVE_FIXINT || format == FORMAT_UINT8 || @@ -46,7 +46,7 @@ return *(int32_t *)data; } -void dumpPack(uint8_t *data, uint32_t indent) { +void *dumpPack(uint8_t *data, uint32_t indent) { uint8_t firstByte = data[0]; Formats format = FirstByteToFormat[firstByte]; FormatInfo *info = &formatInfo[format]; @@ -72,10 +72,12 @@ dataSize = length; break; case ElementsInline: + dataOffset = 1; break; case ReadElements: bytesToRead += info->readTypeParameter; - dataOffset = info->readTypeParameter; + dataOffset = 1; + dataSize = info->readTypeParameter; } char *hexData = malloc(3*bytesToRead); @@ -83,21 +85,24 @@ sprintf(hexData + 3*i, "%x ", data[i]); } hexData[3*bytesToRead - 1] = 0; - printf("%s: ", hexData); - if (info->dataType == TYPE_ARRAY || info->dataType == TYPE_MAP) { - // TODO: read compund types - return; - } + char *indentData = malloc(indent + 1); + memset(indentData, ' ', indent); + indentData[indent] = 0; + printf("%s%s: ", indentData, hexData); void *buffer = malloc(MAX(bytesToRead + 1, 8)); switch (info->readType) { case Inline: + case ElementsInline: *((uint8_t *)buffer) = firstByte & info->readTypeParameter; break; case InlineLength: case FixedLength: case ReadLength: + case ReadElements: memcpy(data + dataOffset, buffer, dataSize); break; default: break; } + uint32_t size; + void *next = data + bytesToRead; switch (info->dataType) { case TYPE_NIL: printf("NIL"); break; @@ -109,16 +114,34 @@ case TYPE_STRING: ((uint8_t *)buffer)[bytesToRead] = 0; printf("str(\"%s\")", buffer); break; + case TYPE_ARRAY: + size = readInteger(buffer, format, info); + printf("array(%i) (%s)\n", size, info->name); + for (uint32_t i = 0; i < size; i++) { + next = dumpPack(next, indent + 2); + } + return next; + case TYPE_MAP: + size = readInteger(buffer, format, info); + printf("map(%i) (%s)\n", size, info->name); + for (uint32_t i = 0; i < size; i++) { + next = dumpPack(next, indent + 1); + next = dumpPack(next, indent + 2); + } + return next; + default: printf("unknown"); break; } printf(" (%s)\n", info->name); free(hexData); + free(indentData); free(buffer); + return next; } -uint8_t demo1[] = {0xD0, 1}; // int8 -uint8_t demo2[] = {0xC2}; // false +uint8_t demo1[] = {0x92, 1, 5}; // fixarray +uint8_t demo2[] = {0xDE, 2, 0, 0xA2, 'h', 'i', 1, 0xA2, ';', ')', 0xC3 }; // map16 uint8_t demo3[] = {0xC3}; // true uint8_t demo4[] = {0xA2, 'h', 'i'}; // fixstring uint8_t demo5[] = {0xD9, 5, 'w', 'o', 'r', 'l', 'd'}; // str8