From 95ffa588b1b3adca0d29043347a8487f75d5c248 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 14:42:52 -0700 Subject: [PATCH 1/5] Revised getpldh(.., ..) - no malloc's --- include/devil1pld.h | 11 +++++++++-- src/devil1pld.c | 15 +++++++++++++-- test/main.c | 12 +++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index a128a81..b640184 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -2,20 +2,27 @@ #define DEVIL1PLD_H #include +#include +#pragma pack(push, 1) + struct PldHeader { int32_t numOffset; // array of numOffset elements uint32_t *offsets; // }; +#pragma pack(pop) + // input: contents of the .pld file. // allocates heap memory -struct PldHeader *getpldh(const char*); +//struct PldHeader *getpldh(const char*); + +bool getpldh(struct PldHeader*, const char*); // frees heap memory void destroypldh(struct PldHeader*); // input: a pld header struct. -void showpldh(struct PldHeader*); +void printpldh(struct PldHeader*); #endif \ No newline at end of file diff --git a/src/devil1pld.c b/src/devil1pld.c index d7a760b..8d2bcff 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -3,7 +3,7 @@ #include #include -struct PldHeader *getpldh(const char *filedata) { +/* struct PldHeader *getpldh(const char *filedata) { if (filedata == NULL) { return NULL; } @@ -23,7 +23,7 @@ struct PldHeader *getpldh(const char *filedata) { ph -> numOffset = n[0]; memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets); return ph; -} +} */ void destroypldh(struct PldHeader *ph) { free(ph -> offsets); @@ -31,11 +31,22 @@ void destroypldh(struct PldHeader *ph) { ph = NULL; } +bool getpldh(struct PldHeader *ph, const char *filedata) { + bool good = false; + if (ph != NULL && filedata != NULL) { + ph -> numOffset = (int32_t)filedata[0]; + ph -> offsets = (uint32_t*)(filedata + sizeof(int32_t)); + good = true; + } + return good; +} + void printpldh(struct PldHeader *ph) { if (ph == NULL) { return; } printf("number of offsets = %i\n", ph -> numOffset); + printf("offsets = %x\n", ph -> offsets); unsigned int i; for (i = 0; i < ph -> numOffset; i++) { printf("offset %i = %x\n", i, ph -> offsets[i]); diff --git a/test/main.c b/test/main.c index e5879e6..1a6e312 100644 --- a/test/main.c +++ b/test/main.c @@ -74,7 +74,7 @@ char *loadfile(const char *fname, unsigned int *s) { return buf; } -bool unpackpld (const char *filedata, +/* bool unpackpld (const char *filedata, unsigned int filesize, const char *filename) { bool status = false; @@ -86,6 +86,16 @@ bool unpackpld (const char *filedata, status = true; } return status; +} */ + +bool unpackpld(const char *filedata, + unsigned int filesize, + const char *filename) { + struct PldHeader h; + if (filedata != NULL && filesize > 0) { + getpldh(&h, filedata); + printpldh(&h); + } } int main(int argc, char ** argv) { From a8b7c51a3a286eaa994c4ba0f0cfa8d3c1ed9ec8 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 14:46:17 -0700 Subject: [PATCH 2/5] Removed unneeded functions and libraries --- include/devil1pld.h | 10 +++------- src/devil1pld.c | 30 ------------------------------ 2 files changed, 3 insertions(+), 37 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index b640184..93a006d 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -13,16 +13,12 @@ struct PldHeader { #pragma pack(pop) -// input: contents of the .pld file. -// allocates heap memory -//struct PldHeader *getpldh(const char*); - +// input: pointer to a struct, contents of the .pld file. +// * = pass by reference of a struct PldHeader bool getpldh(struct PldHeader*, const char*); -// frees heap memory -void destroypldh(struct PldHeader*); - // input: a pld header struct. +// * = pass by reference of a struct PldHeader void printpldh(struct PldHeader*); #endif \ No newline at end of file diff --git a/src/devil1pld.c b/src/devil1pld.c index 8d2bcff..1f55484 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -1,35 +1,5 @@ #include "devil1pld.h" #include -#include -#include - -/* struct PldHeader *getpldh(const char *filedata) { - if (filedata == NULL) { - return NULL; - } - int32_t *n = (int32_t*)filedata; - struct PldHeader *ph = (struct PldHeader*)malloc( - sizeof(struct PldHeader) - ); - uint32_t size_offsets = sizeof(uint32_t) * n[0]; - ph -> offsets = (uint32_t*)malloc(size_offsets); - if (ph -> offsets == NULL) { - perror("Error 4: "); - free(ph); - free(ph -> offsets); - return NULL; - } - // set data of struct. - ph -> numOffset = n[0]; - memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets); - return ph; -} */ - -void destroypldh(struct PldHeader *ph) { - free(ph -> offsets); - free(ph); - ph = NULL; -} bool getpldh(struct PldHeader *ph, const char *filedata) { bool good = false; From 0ea47e44c34793058e0b3a5963f8d129ced2e9a9 Mon Sep 17 00:00:00 2001 From: <> Date: Fri, 6 Apr 2018 15:48:44 -0700 Subject: [PATCH 3/5] Added function that determines size of pld section --- include/devil1pld.h | 4 ++++ src/devil1pld.c | 18 ++++++++++++++++++ test/main.c | 11 ++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index 93a006d..d57bfd7 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -17,6 +17,10 @@ struct PldHeader { // * = pass by reference of a struct PldHeader bool getpldh(struct PldHeader*, const char*); +// input: pointer to header, index of offset, filesize +// * = pass by reference of a struct PldHeader +int sizeofpldstruct(struct PldHeader*, unsigned int, unsigned int); + // input: a pld header struct. // * = pass by reference of a struct PldHeader void printpldh(struct PldHeader*); diff --git a/src/devil1pld.c b/src/devil1pld.c index 1f55484..bc0e378 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -11,6 +11,24 @@ bool getpldh(struct PldHeader *ph, const char *filedata) { return good; } +// determine the size of the i-th pld structure +int sizeofpldstruct(struct PldHeader *ph, unsigned int i, unsigned int max) { + unsigned int size = -1; + if (ph == NULL) { + fprintf(stderr, "Error: given null pointer\n"); + return size; + } + if (i > ph -> numOffset) { + fprintf(stderr, "Error: i exceeds pldHeader -> numOffset\n"); + return size; + } + unsigned int start = 0, end = 0; + start = ph -> offsets[i]; + end = (i == (ph -> numOffset) - 1) ? max : ph -> offsets[i + 1]; + size = end - start; + return size; +} + void printpldh(struct PldHeader *ph) { if (ph == NULL) { return; diff --git a/test/main.c b/test/main.c index c92f3ba..6617ba4 100644 --- a/test/main.c +++ b/test/main.c @@ -96,7 +96,12 @@ bool unpackpld(const char *filedata, struct PldHeader h; if (filedata != NULL && filesize > 0) { getpldh(&h, filedata); - printpldh(&h); + } + int size = 0; + unsigned int i = 0; + for (i = 0; i < h.numOffset; i++) { + size = sizeofpldstruct(&h, i, filesize); + printf("%x\n", size); } } @@ -152,8 +157,8 @@ int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); -// unpackpld(buffer, bufsize, f); - exporttextures(buffer, bufsize, f); + unpackpld(buffer, bufsize, f); +// exporttextures(buffer, bufsize, f); free(buffer); return 0; } From a7cee528f32b21ddb6ccdcd58c5357db84241dc6 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 16:05:12 -0700 Subject: [PATCH 4/5] Added simplier pld unpacking --- test/main.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/main.c b/test/main.c index 6617ba4..a8dc956 100644 --- a/test/main.c +++ b/test/main.c @@ -93,16 +93,22 @@ char *loadfile(const char *fname, unsigned int *s) { bool unpackpld(const char *filedata, unsigned int filesize, const char *filename) { + if (filedata == NULL || filesize <= 0) { + return false; + } struct PldHeader h; - if (filedata != NULL && filesize > 0) { - getpldh(&h, filedata); - } - int size = 0; + getpldh(&h, filedata); + char *fn = NULL; + fn = (char*)malloc(strlen(filename) + 3 + 4); + int size = 0; unsigned int i = 0; for (i = 0; i < h.numOffset; i++) { size = sizeofpldstruct(&h, i, filesize); - printf("%x\n", size); + sprintf(fn, "%s_%d", filename, i); + write(fn, filedata + h.offsets[i], size); } + free(fn); + return true; } void write(const char *filename, From c3a74a6dd54d35cf53d3b150d374bb2d55fb7486 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 16:07:35 -0700 Subject: [PATCH 5/5] Removed old versions of code --- test/main.c | 85 ++++++++++------------------------------------------- 1 file changed, 15 insertions(+), 70 deletions(-) diff --git a/test/main.c b/test/main.c index a8dc956..6bf10b0 100644 --- a/test/main.c +++ b/test/main.c @@ -6,47 +6,6 @@ #include "devil1pld.h" #include "devil1tex.h" -bool writedata(const char *basename, - unsigned int id, - const char *data, - unsigned int dsize) { - bool status = false; - char *fn = (char*)malloc(sizeof(char) * strlen(basename) + 4); - sprintf(fn, "%s_%d", basename, id); - FILE *out = fopen(fn, "w"); - if (out != NULL) { - fwrite(data, sizeof(char), dsize, out); - fclose(out); - status = true; - } - free(fn); - return status; -} - -void splitpld(const char *filedata, - unsigned int filesize, - const struct PldHeader *ph, - const char *name) { - char *wbuffer = NULL; // write buffer that will change. - unsigned int wsize = 0; // size of wbuffer. - unsigned int i; - unsigned int start = 0; // ending offset for data - unsigned int end = 0; // ending offset for data - for (i = 0; (i + 1) < (ph -> numOffset) + 1; i++) { - start = ph -> offsets[i]; - // the last offset still has some data until the end of file - end = (i == (ph -> numOffset) - 1) ? - filesize : ph -> offsets[i + 1]; - // copy sector to write buffer - wsize = end - start; - wbuffer = (char*)malloc(sizeof(char) * wsize); - memcpy(wbuffer, filedata + start, wsize); - // create a file. - writedata(name, i, wbuffer, wsize); - free(wbuffer); - } -} - char *loadfile(const char *fname, unsigned int *s) { FILE *f = fopen(fname, "rb"); unsigned int size = 0; // number of elements to buffer; @@ -76,19 +35,22 @@ char *loadfile(const char *fname, unsigned int *s) { return buf; } -/* bool unpackpld (const char *filedata, - unsigned int filesize, - const char *filename) { - bool status = false; - printf("fsize is %i\n", filesize); - if (filedata != NULL && filesize != 0) { - struct PldHeader *pldh = getpldh(filedata); - splitpld(filedata, filesize, pldh, filename); - destroypldh(pldh); - status = true; +void write(const char *filename, + const char* t, + unsigned int size) { + if (filename == NULL) { + return; } - return status; -} */ + unsigned int written = 0; + FILE *out = fopen(filename, "wb"); + if (out != NULL) { + written = fwrite(t, sizeof(unsigned char), size, out); + fclose(out); + if (written == 0) { + perror("texture write error"); + } + } +} bool unpackpld(const char *filedata, unsigned int filesize, @@ -111,23 +73,6 @@ bool unpackpld(const char *filedata, return true; } -void write(const char *filename, - const char* t, - unsigned int size) { - if (filename == NULL) { - return; - } - unsigned int written = 0; - FILE *out = fopen(filename, "wb"); - if (out != NULL) { - written = fwrite(t, sizeof(unsigned char), size, out); - fclose(out); - if (written == 0) { - perror("texture write error"); - } - } -} - void exporttextures(const char *filedata, unsigned int filesize, const char *filename) {