From 54ef5e84db269fba7191ada5c95f3b8cc2838dd3 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 2 Apr 2018 01:21:21 -0700 Subject: [PATCH] Fixed includes in header, added destroy fn, clarity tweaks --- include/devil1pld.h | 11 +++++++---- src/devil1pld.c | 42 ++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index 2082afc..a128a81 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -2,17 +2,20 @@ #define DEVIL1PLD_H #include -#pragma pack(1) struct PldHeader { int32_t numOffset; // array of numOffset elements uint32_t *offsets; // }; -// input: a pld header struct. -void showpldh(struct PldHeader *); - // input: contents of the .pld file. +// allocates heap memory struct PldHeader *getpldh(const char*); +// frees heap memory +void destroypldh(struct PldHeader*); + +// input: a pld header struct. +void showpldh(struct PldHeader*); + #endif \ No newline at end of file diff --git a/src/devil1pld.c b/src/devil1pld.c index 5169a17..d7a760b 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -1,24 +1,13 @@ #include "devil1pld.h" -#include "stdio.h" -#include "stdlib.h" -#include "string.h" +#include +#include +#include -void printpldh(struct PldHeader *ph) { - if (ph == NULL) { - return; - } - printf("number of offsets = %i\n", ph -> numOffset); - unsigned int i; - for (i = 0; i < ph -> numOffset; i++) { - printf("offset %i = %x\n", i, ph -> offsets[i]); - } -} - -struct PldHeader *getpldh(const char *buffer) { - if (buffer == NULL) { +struct PldHeader *getpldh(const char *filedata) { + if (filedata == NULL) { return NULL; } - int32_t *n = (int32_t*)buffer; + int32_t *n = (int32_t*)filedata; struct PldHeader *ph = (struct PldHeader*)malloc( sizeof(struct PldHeader) ); @@ -32,6 +21,23 @@ struct PldHeader *getpldh(const char *buffer) { } // set data of struct. ph -> numOffset = n[0]; - memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets); + memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets); return ph; } + +void destroypldh(struct PldHeader *ph) { + free(ph -> offsets); + free(ph); + ph = NULL; +} + +void printpldh(struct PldHeader *ph) { + if (ph == NULL) { + return; + } + printf("number of offsets = %i\n", ph -> numOffset); + unsigned int i; + for (i = 0; i < ph -> numOffset; i++) { + printf("offset %i = %x\n", i, ph -> offsets[i]); + } +}