Revised getpldh(.., ..) - no malloc's

This commit is contained in:
_ 2018-04-06 14:42:52 -07:00
parent a188a19d77
commit 95ffa588b1
3 changed files with 33 additions and 5 deletions

View File

@ -2,20 +2,27 @@
#define DEVIL1PLD_H #define DEVIL1PLD_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#pragma pack(push, 1)
struct PldHeader { struct PldHeader {
int32_t numOffset; int32_t numOffset;
// array of numOffset elements // array of numOffset elements
uint32_t *offsets; // <format=hex> uint32_t *offsets; // <format=hex>
}; };
#pragma pack(pop)
// input: contents of the .pld file. // input: contents of the .pld file.
// allocates heap memory // allocates heap memory
struct PldHeader *getpldh(const char*); //struct PldHeader *getpldh(const char*);
bool getpldh(struct PldHeader*, const char*);
// frees heap memory // frees heap memory
void destroypldh(struct PldHeader*); void destroypldh(struct PldHeader*);
// input: a pld header struct. // input: a pld header struct.
void showpldh(struct PldHeader*); void printpldh(struct PldHeader*);
#endif #endif

View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
struct PldHeader *getpldh(const char *filedata) { /* struct PldHeader *getpldh(const char *filedata) {
if (filedata == NULL) { if (filedata == NULL) {
return NULL; return NULL;
} }
@ -23,7 +23,7 @@ struct PldHeader *getpldh(const char *filedata) {
ph -> numOffset = n[0]; ph -> numOffset = n[0];
memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets); memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets);
return ph; return ph;
} } */
void destroypldh(struct PldHeader *ph) { void destroypldh(struct PldHeader *ph) {
free(ph -> offsets); free(ph -> offsets);
@ -31,11 +31,22 @@ void destroypldh(struct PldHeader *ph) {
ph = NULL; 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) { void printpldh(struct PldHeader *ph) {
if (ph == NULL) { if (ph == NULL) {
return; return;
} }
printf("number of offsets = %i\n", ph -> numOffset); printf("number of offsets = %i\n", ph -> numOffset);
printf("offsets = %x\n", ph -> offsets);
unsigned int i; unsigned int i;
for (i = 0; i < ph -> numOffset; i++) { for (i = 0; i < ph -> numOffset; i++) {
printf("offset %i = %x\n", i, ph -> offsets[i]); printf("offset %i = %x\n", i, ph -> offsets[i]);

View File

@ -74,7 +74,7 @@ char *loadfile(const char *fname, unsigned int *s) {
return buf; return buf;
} }
bool unpackpld (const char *filedata, /* bool unpackpld (const char *filedata,
unsigned int filesize, unsigned int filesize,
const char *filename) { const char *filename) {
bool status = false; bool status = false;
@ -86,6 +86,16 @@ bool unpackpld (const char *filedata,
status = true; status = true;
} }
return status; 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) { int main(int argc, char ** argv) {