Added unpacking feature that is missing the last chunk of the .pld

This commit is contained in:
_ 2018-04-01 18:32:54 -07:00
parent a8c704e428
commit 7afb14dd0c

69
main.c
View File

@ -4,22 +4,33 @@
#include <stdio.h>
#include <string.h>
void unpackpld(const char *buffer, struct PldHeader *pldh) {
unsigned int i = 0;
void unpackpld(const char *buffer,
const struct PldHeader *ph,
const char *name) {
char *wbuffer = NULL; // write buffer that will change.
unsigned int wsize = 0; // size of wbuffer.
for (i; (i + 1) < (pldh -> numOffset); i++) {
unsigned int i;
for (i = 0; (i + 1) < (ph -> numOffset); i++) {
printf("start: %x - end: %x\n",
pldh -> offsets[i],
pldh -> offsets[i + 1]);
/* wsize = offsets[i + 1] - offsets[i];
ph -> offsets[i],
ph -> offsets[i + 1]);
// copy sector to write buffer
wsize = (ph -> offsets[i + 1]) - (ph -> offsets[i]) + 1;
wbuffer = (char*)malloc(sizeof(char) * wsize);
memcpy(wbuffer, buffer[offsets[i]], wsize); */
memcpy(wbuffer, buffer + (ph -> offsets[i]), wsize);
// create a file.
char *fn = (char*)malloc(sizeof(char)*strlen(name) + 4);
sprintf(fn, "%s_%d", name, i);
FILE *out = fopen(fn, "w");
fputs(wbuffer, out);
fclose(out);
free(fn);
}
free(wbuffer);
}
void disp_pldheader(struct PldHeader *x) {
void show_pldheader(struct PldHeader *x) {
printf("number of offsets = %i\n", x -> numOffset);
unsigned int i;
for (i = 0; i < x -> numOffset; i++) {
@ -27,70 +38,66 @@ void disp_pldheader(struct PldHeader *x) {
}
}
bool read_pldheader(const char *buffer) {
struct PldHeader *read_pldheader(const char *buffer) {
if (buffer == NULL) {
return false;
return NULL;
}
int32_t *n = (int32_t*)buffer;
int32_t *n = (int32_t*)buffer;
struct PldHeader *ph = (struct PldHeader*)malloc(
sizeof(struct PldHeader)
);
uint32_t size_offsets = sizeof(uint32_t) * n[0];
ph -> offsets = (uint32_t*)malloc(size_offsets);
ph -> offsets = (uint32_t*)malloc(size_offsets);
if (ph -> offsets == NULL) {
perror("Error 4: ");
free(ph);
free(ph -> offsets);
return false;
return NULL;
}
// set data of struct.
ph -> numOffset = n[0];
memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets);
disp_pldheader(ph);
unpackpld(buffer, ph);
free(ph -> offsets);
free(ph);
return true;
return ph;
}
bool readpld(const char *fname, char *buf) {
char *readpld(const char *fname) {
FILE *f = fopen(fname, "rb");
unsigned int size = 0; // number of elements to buffer;
unsigned int rcnt = 0; // number of char's read by fread(...)
if (f == NULL) {
perror("Error 1: ");
return false;
return NULL;
}
// this method of determining file size doesn't work until 2 GB.
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
buf = (char*)malloc(sizeof(char) * size);
char *buf = (char*)malloc(sizeof(char) * size);
if (buf == NULL) {
perror("Error 2: ");
free(buf);
return false;
return NULL;
}
rcnt = fread(buf, sizeof(char), size, f);
if (rcnt < size) {
perror("Error 3: ");
free(buf);
return false;
return NULL;
}
read_pldheader(buf);
fclose(f);
return true;
return buf;
}
int main(int argc, char ** argv) {
char *filename = argv[1];
char *filedata = NULL;
bool status = readpld(filename, filedata);
if (status) {
printf("Read OK");
} else {
printf("Read not-OK");
char *filedata = readpld(filename);
if (filedata != NULL) {
struct PldHeader *pldh = read_pldheader(filedata);
unpackpld(filedata, pldh, filename);
free(pldh -> offsets);
free(pldh);
}
free(filedata);
return 0;
}