diff options
| author | Markus Mittendrein <maxmitti@maxmitti.tk> | 2018-07-05 10:09:58 +0200 |
|---|---|---|
| committer | Markus Mittendrein <maxmitti@maxmitti.tk> | 2018-07-05 10:09:58 +0200 |
| commit | c04362bf55b077481299960614b97672cceb8e75 (patch) | |
| tree | de96df67943d7c856a5fc24ced92abe1700aec97 | |
| parent | e1623f08ffda3602928fec65b1f02c70324188ce (diff) | |
| download | cc4group-c04362bf55b077481299960614b97672cceb8e75.tar.gz cc4group-c04362bf55b077481299960614b97672cceb8e75.zip | |
Refactoring
| -rw-r--r-- | main.c | 73 |
1 files changed, 37 insertions, 36 deletions
@@ -12,6 +12,8 @@ #include <zlib.h> +#include <assert.h> + #include "GenericList.h" #define C4GroupMaxMaker 30 @@ -82,7 +84,6 @@ void memScrambleHeader(uint8_t* data) void buildChildren(C4GroupEntryData* entry) { - memScrambleHeader(entry->data); C4GroupHeader* header = (C4GroupHeader*)entry->data; entry->children = GroupEntryListNew(); @@ -97,6 +98,7 @@ void buildChildren(C4GroupEntryData* entry) if(core->Directory) { + memScrambleHeader(childEntry->data); buildChildren(childEntry); } } @@ -245,10 +247,11 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - C4GroupHeader header; + size_t currentSize = sizeof(C4GroupHeader); + C4GroupHeader* header = malloc(currentSize); - strm.next_out = (Bytef*)&header; - strm.avail_out = sizeof(header); + strm.next_out = (Bytef*)header; + strm.avail_out = currentSize; ret = inflate(&strm, Z_SYNC_FLUSH); if(ret != Z_OK) @@ -257,35 +260,40 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - memScrambleHeader((uint8_t*)&header); + memScrambleHeader((uint8_t*)header); + + printf("Version %d.%d\n", header->Ver1, header->Ver2); + printf("%d Entries\n", header->Entries); + printf("%sOriginal\n", header->Original == 1234567 ? "" : "Not "); + printf("Created %s\n", formatTime(header->Creation)); + puts(header->id); + puts(header->Maker); + + assert(header->Ver1 == 1); + assert(header->Ver2 == 2); - printf("Version %d.%d\n", header.Ver1, header.Ver2); - printf("%d Entries\n", header.Entries); - printf("%sOriginal\n", header.Original == 1234567 ? "" : "Not "); - printf("Created %s\n", formatTime(header.Creation)); - puts(header.id); - puts(header.Maker); + currentSize += sizeof(C4GroupEntryCore) * header->Entries; + header = realloc(header, currentSize); + C4GroupEntryCore* cores = (C4GroupEntryCore*)((void*)(header) + sizeof(C4GroupHeader)); GroupEntryList* entries = GroupEntryListNew(); - for(size_t i = 0; i < (size_t)header.Entries; ++i) - { - GroupEntryListEntry* listEntry = GroupEntryListAppend(entries, (C4GroupEntryData){.data = NULL, .children = NULL}); - strm.next_out = (Bytef*)&listEntry->value.core; - strm.avail_out = sizeof(listEntry->value.core); + strm.next_out = (Bytef*)cores; + strm.avail_out = sizeof(C4GroupEntryCore) * header->Entries; - ret = inflate(&strm, Z_SYNC_FLUSH); - if(ret != Z_OK) - { - fprintf(stderr, "ERROR: inflating header: %s\n", zError(ret)); - return EXIT_FAILURE; - } + ret = inflate(&strm, Z_SYNC_FLUSH); + if(ret != Z_OK) + { + fprintf(stderr, "ERROR: inflating header: %s\n", zError(ret)); + return EXIT_FAILURE; } - GroupEntryListEntry* last = GroupEntryListLast(entries); - size_t uncompressedSize = last->value.core.Offset + last->value.core.Size; + C4GroupEntryCore* last = cores + header->Entries - 1; + size_t uncompressedSize = last->Offset + last->Size; - uint8_t* data = malloc(uncompressedSize); + currentSize += uncompressedSize; + header = realloc(header, currentSize); + uint8_t* data = (void*)(header) + sizeof(C4GroupHeader) + sizeof(C4GroupEntryCore) * header->Entries; strm.next_out = data; strm.avail_out = uncompressedSize; @@ -305,21 +313,14 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - ForeachGroupEntry(entries) - { - entry->value.data = data + entry->value.core.Offset; - - if(entry->value.core.Directory) - { - buildChildren(&entry->value); - } - } + C4GroupEntryData root = {.data = (uint8_t*)header, .children = NULL}; + buildChildren(&root); - handleChildren(entries, 0, argv[2]); + handleChildren(root.children, 0, argv[2]); deleteChildren(entries); - free(data); + free(header); return EXIT_SUCCESS; } |
