2018-04-02 02:51:12 +05:30
|
|
|
#include "devil1pld.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-04-02 07:02:54 +05:30
|
|
|
void unpackpld(const char *buffer,
|
|
|
|
const struct PldHeader *ph,
|
|
|
|
const char *name) {
|
2018-04-02 04:47:58 +05:30
|
|
|
char *wbuffer = NULL; // write buffer that will change.
|
|
|
|
unsigned int wsize = 0; // size of wbuffer.
|
2018-04-02 07:02:54 +05:30
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; (i + 1) < (ph -> numOffset); i++) {
|
2018-04-02 04:47:58 +05:30
|
|
|
printf("start: %x - end: %x\n",
|
2018-04-02 07:02:54 +05:30
|
|
|
ph -> offsets[i],
|
|
|
|
ph -> offsets[i + 1]);
|
|
|
|
|
|
|
|
// copy sector to write buffer
|
|
|
|
wsize = (ph -> offsets[i + 1]) - (ph -> offsets[i]) + 1;
|
2018-04-02 04:47:58 +05:30
|
|
|
wbuffer = (char*)malloc(sizeof(char) * wsize);
|
2018-04-02 07:02:54 +05:30
|
|
|
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);
|
2018-04-02 04:47:58 +05:30
|
|
|
}
|
|
|
|
free(wbuffer);
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:54 +05:30
|
|
|
void show_pldheader(struct PldHeader *x) {
|
2018-04-02 02:51:12 +05:30
|
|
|
printf("number of offsets = %i\n", x -> numOffset);
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < x -> numOffset; i++) {
|
|
|
|
printf("offset %i = %x\n", i, x -> offsets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:54 +05:30
|
|
|
struct PldHeader *read_pldheader(const char *buffer) {
|
2018-04-02 05:49:39 +05:30
|
|
|
if (buffer == NULL) {
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 05:49:39 +05:30
|
|
|
}
|
2018-04-02 07:02:54 +05:30
|
|
|
int32_t *n = (int32_t*)buffer;
|
2018-04-02 04:47:58 +05:30
|
|
|
struct PldHeader *ph = (struct PldHeader*)malloc(
|
|
|
|
sizeof(struct PldHeader)
|
|
|
|
);
|
|
|
|
uint32_t size_offsets = sizeof(uint32_t) * n[0];
|
2018-04-02 07:02:54 +05:30
|
|
|
ph -> offsets = (uint32_t*)malloc(size_offsets);
|
2018-04-02 02:51:12 +05:30
|
|
|
if (ph -> offsets == NULL) {
|
|
|
|
perror("Error 4: ");
|
|
|
|
free(ph);
|
|
|
|
free(ph -> offsets);
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
// set data of struct.
|
|
|
|
ph -> numOffset = n[0];
|
|
|
|
memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets);
|
2018-04-02 07:02:54 +05:30
|
|
|
return ph;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:54 +05:30
|
|
|
char *readpld(const char *fname) {
|
2018-04-02 02:51:12 +05:30
|
|
|
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: ");
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
// this method of determining file size doesn't work until 2 GB.
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
size = ftell(f);
|
|
|
|
rewind(f);
|
2018-04-02 07:02:54 +05:30
|
|
|
char *buf = (char*)malloc(sizeof(char) * size);
|
2018-04-02 05:49:39 +05:30
|
|
|
if (buf == NULL) {
|
2018-04-02 02:51:12 +05:30
|
|
|
perror("Error 2: ");
|
2018-04-02 05:49:39 +05:30
|
|
|
free(buf);
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
2018-04-02 05:49:39 +05:30
|
|
|
rcnt = fread(buf, sizeof(char), size, f);
|
2018-04-02 02:51:12 +05:30
|
|
|
if (rcnt < size) {
|
|
|
|
perror("Error 3: ");
|
2018-04-02 05:49:39 +05:30
|
|
|
free(buf);
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
fclose(f);
|
2018-04-02 07:02:54 +05:30
|
|
|
return buf;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
2018-04-02 05:49:39 +05:30
|
|
|
char *filename = argv[1];
|
2018-04-02 07:02:54 +05:30
|
|
|
char *filedata = readpld(filename);
|
|
|
|
if (filedata != NULL) {
|
|
|
|
struct PldHeader *pldh = read_pldheader(filedata);
|
|
|
|
unpackpld(filedata, pldh, filename);
|
|
|
|
free(pldh -> offsets);
|
|
|
|
free(pldh);
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
2018-04-02 05:49:39 +05:30
|
|
|
free(filedata);
|
2018-04-02 02:51:12 +05:30
|
|
|
return 0;
|
2018-04-02 04:47:58 +05:30
|
|
|
}
|
2018-04-02 07:02:54 +05:30
|
|
|
|