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