Fixed includes in header, added destroy fn, clarity tweaks

This commit is contained in:
_ 2018-04-02 01:21:21 -07:00
parent f2c827ff6b
commit 54ef5e84db
2 changed files with 31 additions and 22 deletions

View File

@ -2,17 +2,20 @@
#define DEVIL1PLD_H #define DEVIL1PLD_H
#include <stdint.h> #include <stdint.h>
#pragma pack(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>
}; };
// 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. // input: a pld header struct.
void showpldh(struct PldHeader*); void showpldh(struct PldHeader*);
// input: contents of the .pld file.
struct PldHeader *getpldh(const char*);
#endif #endif

View File

@ -1,24 +1,13 @@
#include "devil1pld.h" #include "devil1pld.h"
#include "stdio.h" #include <stdio.h>
#include "stdlib.h" #include <stdlib.h>
#include "string.h" #include <string.h>
void printpldh(struct PldHeader *ph) { struct PldHeader *getpldh(const char *filedata) {
if (ph == NULL) { if (filedata == 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) {
return NULL; return NULL;
} }
int32_t *n = (int32_t*)buffer; int32_t *n = (int32_t*)filedata;
struct PldHeader *ph = (struct PldHeader*)malloc( struct PldHeader *ph = (struct PldHeader*)malloc(
sizeof(struct PldHeader) sizeof(struct PldHeader)
); );
@ -32,6 +21,23 @@ struct PldHeader *getpldh(const char *buffer) {
} }
// set data of struct. // set data of struct.
ph -> numOffset = n[0]; ph -> numOffset = n[0];
memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets); memcpy(ph -> offsets, filedata + sizeof(int32_t), size_offsets);
return ph; 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]);
}
}