From a446dd87417fe6802032e8726fcc56da8daefd7f Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 10:12:29 -0700 Subject: [PATCH] Implemented function that correctly acquires TextureBatchDescriptors --- include/devil1tex.h | 7 +++---- src/devil1tex.c | 49 ++++++++++++++++++++++++--------------------- test/main.c | 16 +++++++++------ 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index df279c0..1dc4b89 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -38,12 +38,11 @@ struct TextureBatch { #pragma pack(pop) // re-enable stuct padding for whatever other reason. -void gettph(struct TexturePack*, const char*); - void printtph(struct TexturePack*); -void gettbd(struct TextureBatchDescriptor*, - struct TexturePack *tph, +void printtbd(struct TextureBatchDescriptor*); + +void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); diff --git a/src/devil1tex.c b/src/devil1tex.c index 8bd8ab3..8e5ccc4 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -2,15 +2,6 @@ #include #include -// get texture pack header -void gettph (struct TexturePack *tp, const char *filedata) { -// Assume the Texture Pack Header is already allocated memory. - if (tp != NULL && filedata != NULL) { - memcpy(tp, filedata, sizeof(struct TexturePack)); - } - return; -} - void printtph (struct TexturePack *tp) { if (tp != NULL) { printf("id: \t %c %c %c %c \n", tp -> id[3], @@ -24,31 +15,43 @@ void printtph (struct TexturePack *tp) { return; } +void printtbd(struct TextureBatchDescriptor *bd) { + if (bd != NULL) { + printf("batch index: \t %i \n", bd -> batchIdx); + printf("hash: \t %u \n", bd -> hash); + printf("textures: \t %i\n", bd -> texNumber); + } +} + // get texture pack descriptors -void gettbd (struct TextureBatchDescriptor *tbd, - struct TexturePack* tph, - const char *filedata, - unsigned int filesize) { - if (tbd == NULL || tph == NULL || filedata == NULL) { - // any of these being null will cause segmentation fault. +void gettbd (struct TextureBatchDescriptor **descriptors, + const char *filedata, + unsigned int filesize) { + if (descriptors == NULL || filedata == NULL || filesize <= 0) { return; } - // do some checks regarding the header - if (tph -> firstBatchOffset >= filesize || - tph -> firstBatchOffset <= 0) { + struct TexturePack *tp = NULL; + tp = (struct TexturePack*)filedata; + if (filesize <= (sizeof(struct TextureBatchDescriptor) + * tp -> batchNumber + + sizeof(struct TexturePack))){ + fprintf(stderr, + "File Error: file size to struct sizes ratio.\n"); return; } - unsigned int index = tph -> firstBatchOffset; + unsigned int index = sizeof(struct TexturePack); // Batch Descriptors are structured one after another. // therefore the next one is at // current index + sizeof(TextureBatchDescriptor) unsigned int i = 0; do { - index += (sizeof(struct TextureBatchDescriptor) * i); + index = sizeof(struct TextureBatchDescriptor) * i + + sizeof(struct TexturePack); printf("batch descriptor start: %x\n", index); - tbd = (struct TextureBatchDescriptor*)(filedata + index); - // - } while(i < (tph -> batchNumber)); + descriptors[i] = (struct TextureBatchDescriptor*) + (filedata + index); + i++; + } while(i < (tp -> batchNumber)); return; } diff --git a/test/main.c b/test/main.c index 84e1a7d..25a8061 100644 --- a/test/main.c +++ b/test/main.c @@ -97,17 +97,21 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); - struct TexturePack *tp = (struct TexturePack*)malloc - (sizeof(struct TexturePack)); + struct TexturePack *tp = NULL; printf("%d %d %d %d \n", buffer[0], buffer[1], buffer[2], buffer[3]); - gettph(tp, buffer); - printf("%x %x \n", buffer + 0, tp -> id[0]); + tp = (struct TexturePack*)buffer; + struct TextureBatchDescriptor **bds = NULL; + bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); + gettbd(bds, buffer, bufsize); printtph(tp); - - free(tp); + int i; + for (i = 0; i < tp -> batchNumber; i++) { + printtbd(bds[i]); + } + free(bds); free(buffer); return 0; }