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
#include <stdint.h>
#include <stdbool.h>
#pragma pack(push, 1)
struct PldHeader {
int32_t numOffset;
// array of numOffset elements
uint32_t *offsets; // <format=hex>
};
#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

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
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]);

View File

@ -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) {