From 36186f24506bab878dc39553217da0e030e81867 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 30 May 2020 22:26:57 +0100 Subject: [PATCH] Added compose functions. Fixed compression error caused by clobbering headerless zlib data. Minor fixes. --- chunk.c | 2 +- compression.c | 6 ++++-- nbt.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nbt.h | 3 ++- 4 files changed, 152 insertions(+), 4 deletions(-) diff --git a/chunk.c b/chunk.c index 4c4f34a..5fcde08 100644 --- a/chunk.c +++ b/chunk.c @@ -32,7 +32,7 @@ int overwriteChunk(const char* regionFolder, ChunkID chunk, void* chunkData, siz relativeChunk.z = chunk.z % 32; char* regionFilename = calloc(MAX_REGION_FILENAME_LENGTH + strlen(regionFolder),sizeof(char)); - sprintf(regionFilename,"%s/r.%d.%d.mca.new",regionFolder,region.x,region.z); + sprintf(regionFilename,"%s/r.%d.%d.mca",regionFolder,region.x,region.z); if(access(regionFilename,R_OK | W_OK) == -1) { fprintf(stderr,"Can't access file %s: %s\n",regionFilename,strerror(errno)); diff --git a/compression.c b/compression.c index 1c93728..c87ed08 100644 --- a/compression.c +++ b/compression.c @@ -125,8 +125,10 @@ ssize_t deflateGzip(void* unCompData, size_t unCompDataLen, void** compData, int return -4; } - // Set OS Flag to 0x00: "FAT filesystem (MS-DOS, OS/2, NT/Win32)" - comp[OS_FLAG_OFFSET] = 0x00; + if(!headerless) { + // Set OS Flag to 0x00: "FAT filesystem (MS-DOS, OS/2, NT/Win32)" + comp[OS_FLAG_OFFSET] = 0x00; + } *compData = comp; return compLength; diff --git a/nbt.c b/nbt.c index a265975..7b89f3b 100644 --- a/nbt.c +++ b/nbt.c @@ -11,6 +11,10 @@ unsigned int parseList(void* addr, TagList* tl, uint8_t type); unsigned int parseCompound(void* addr, TagCompound* tc); unsigned int parsePayload(void* addr,Tag* t); unsigned int parseTag(void* addr, Tag* t); +size_t composeCompound(TagCompound* tc, void** data); +size_t composeList(uint8_t listType, TagList* tl, void** data); +size_t composePayload(Tag t, void** data); +size_t composeTag(Tag t, void** data); ssize_t loadDB(const char* filename, void** data) { if(access(filename,R_OK) == -1) { @@ -246,4 +250,145 @@ unsigned int parseTag(void* addr, Tag* t) { pos += parsePayload(pos,t); return pos-addr; +} + +size_t composeCompound(TagCompound* tc, void** data) { + size_t totalPayloadLength = 0; + void* totalPayload = calloc(1,sizeof(char)); + unsigned int pos = 0; + size_t childTagPayloadLength = 0; + void* childTagPayload; + for(int i = 0; i < tc->numTags; ++i) { + childTagPayloadLength = composeTag(tc->list[i],&childTagPayload); + totalPayloadLength += childTagPayloadLength; + void* newptr = realloc(totalPayload,totalPayloadLength); + if(newptr == NULL) { + fprintf(stderr, "Unable to realloc memory for child tag\n"); + free(totalPayload); + free(childTagPayload); + return 0; + } + totalPayload = newptr; + memcpy(totalPayload+pos,childTagPayload,childTagPayloadLength); + free(childTagPayload); + pos += childTagPayloadLength; + } + void* newptr = realloc(totalPayload,++totalPayloadLength); + if(newptr == NULL) { + fprintf(stderr, "Unable to realloc memory for end tag\n"); + free(totalPayload); + return 0; + } + totalPayload = newptr; + ((uint8_t*)totalPayload)[pos] = 0x00; + *data = totalPayload; + return totalPayloadLength; +} + +size_t composeList(uint8_t listType, TagList* tl, void** data) { + size_t totalPayloadLength = 0; + if(listType == TAG_LIST) { + totalPayloadLength += sizeof(uint8_t); + } + void* totalPayload = calloc(totalPayloadLength + sizeof(uint32_t),sizeof(char)); + if(listType == TAG_LIST) { + ((uint8_t*)totalPayload)[0] = tl->type; + } + uint32_t u32 = __bswap_32(tl->size); + memcpy((uint8_t*)totalPayload + totalPayloadLength,&u32,sizeof(uint32_t)); + totalPayloadLength += sizeof(uint32_t); + + unsigned int pos = totalPayloadLength; + size_t childTagPayloadLength = 0; + void* childTagPayload; + for(int i = 0; i < tl->size; ++i) { + childTagPayloadLength = composePayload(tl->list[i],&childTagPayload); + totalPayloadLength += childTagPayloadLength; + void* newptr = realloc(totalPayload,totalPayloadLength); + if(newptr == NULL) { + fprintf(stderr, "Unable to realloc memory for child tag\n"); + free(totalPayload); + free(childTagPayload); + return 0; + } + totalPayload = newptr; + memcpy(totalPayload+pos,childTagPayload,childTagPayloadLength); + free(childTagPayload); + pos += childTagPayloadLength; + } + + *data = totalPayload; + return totalPayloadLength; +} + +size_t composePayload(Tag t, void** data) { + size_t payloadLength = getTypeSize(t.type); // initially, then particularly for lists/compounds/strings + void* payload; + + uint16_t u16 = 0; + uint32_t u32 = 0; + uint64_t u64 = 0; + switch(t.type) { + case TAG_BYTE: + payload = calloc(1,payloadLength); + memcpy(payload,t.payload,t.payloadLength); + break; + case TAG_SHORT: + u16 = __bswap_16(*(uint16_t*)t.payload); + payload = calloc(1,payloadLength); + memcpy(payload,&u16,t.payloadLength); + break; + case TAG_INT: + case TAG_FLOAT: + u32 = __bswap_32(*(uint32_t*)t.payload); + payload = calloc(1,payloadLength); + memcpy(payload,&u32,t.payloadLength); + break; + case TAG_LONG: + case TAG_DOUBLE: + u64 = __bswap_64(*(uint64_t*)t.payload); + payload = calloc(1,payloadLength); + memcpy(payload,&u64,t.payloadLength); + break; + case TAG_STRING: + payloadLength = sizeof(uint16_t) + t.payloadLength; + payload = calloc(payloadLength,sizeof(char)); + u16 = __bswap_16((uint16_t)t.payloadLength); + memcpy(payload,&u16,sizeof(uint16_t)); + memcpy(payload + sizeof(uint16_t),t.payload,t.payloadLength); + break; + case TAG_COMPOUND: + payloadLength = composeCompound((TagCompound*)t.payload,&payload); + break; + case TAG_LIST: + case TAG_BYTEARRAY: + case TAG_INTARRAY: + payloadLength = composeList(t.type,(TagList*)t.payload,&payload); + break; + } + + *data = payload; + return payloadLength; +} + +size_t composeTag(Tag t, void** data) { + size_t headerSize = sizeof(uint8_t) + sizeof(uint16_t) + t.nameLength; + void* tagHeader = calloc(headerSize,sizeof(char)); + ((uint8_t*)tagHeader)[0] = t.type; + uint16_t u16 = __bswap_16(t.nameLength); + memcpy(tagHeader+sizeof(uint8_t),&u16,sizeof(uint16_t)); + memcpy(tagHeader+sizeof(uint8_t) + sizeof(uint16_t), t.name, t.nameLength); + void* tagPayload; + size_t payloadSize = composePayload(t,&tagPayload); + void* tagData = realloc(tagHeader,headerSize + payloadSize); + if(tagData == NULL) { + fprintf(stderr, "Unable to realloc memory to merge header and payload\n"); + free(tagHeader); + free(tagPayload); + return 0; + } + memcpy(tagData + headerSize,tagPayload, payloadSize); + free(tagPayload); + *data = tagData; + return headerSize + payloadSize; } \ No newline at end of file diff --git a/nbt.h b/nbt.h index b3af23f..ce49e14 100644 --- a/nbt.h +++ b/nbt.h @@ -26,7 +26,7 @@ typedef struct Tag { uint8_t type; char* name; - unsigned int nameLength; + uint16_t nameLength; unsigned int payloadLength; void* payload; } Tag; @@ -60,5 +60,6 @@ enum TAG { ssize_t loadDB(const char* filename, void** data); void destroyTag(Tag* t); unsigned int parseTag(void* addr, Tag* t); +size_t composeTag(Tag t, void** data); #endif \ No newline at end of file -- libgit2 0.22.2