From 95ffa588b1b3adca0d29043347a8487f75d5c248 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 14:42:52 -0700 Subject: [PATCH] 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) {