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 04:47:58 +05:30
|
|
|
void unpackpld(const char *buffer, struct PldHeader *pldh) {
|
|
|
|
unsigned int i = 0;
|
|
|
|
char *wbuffer = NULL; // write buffer that will change.
|
|
|
|
unsigned int wsize = 0; // size of wbuffer.
|
|
|
|
for (i; (i + 1) < (pldh -> numOffset); i++) {
|
|
|
|
printf("start: %x - end: %x\n",
|
|
|
|
pldh -> offsets[i],
|
|
|
|
pldh -> offsets[i + 1]);
|
|
|
|
/* wsize = offsets[i + 1] - offsets[i];
|
|
|
|
wbuffer = (char*)malloc(sizeof(char) * wsize);
|
|
|
|
memcpy(wbuffer, buffer[offsets[i]], wsize); */
|
|
|
|
}
|
|
|
|
free(wbuffer);
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:51:12 +05:30
|
|
|
void disp_pldheader(struct PldHeader *x) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read_pldheader(const char *buffer) {
|
2018-04-02 05:49:39 +05:30
|
|
|
if (buffer == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-02 02:51:12 +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 02:51:12 +05:30
|
|
|
ph -> offsets = (uint32_t*)malloc(size_offsets);
|
|
|
|
if (ph -> offsets == NULL) {
|
|
|
|
perror("Error 4: ");
|
|
|
|
free(ph);
|
|
|
|
free(ph -> offsets);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// set data of struct.
|
|
|
|
ph -> numOffset = n[0];
|
|
|
|
memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets);
|
|
|
|
disp_pldheader(ph);
|
2018-04-02 04:47:58 +05:30
|
|
|
unpackpld(buffer, ph);
|
2018-04-02 02:51:12 +05:30
|
|
|
free(ph -> offsets);
|
|
|
|
free(ph);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-02 05:49:39 +05:30
|
|
|
bool readpld(const char *fname, char *buf) {
|
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: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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 05:49:39 +05:30
|
|
|
buf = (char*)malloc(sizeof(char) * size);
|
|
|
|
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 02:51:12 +05:30
|
|
|
return false;
|
|
|
|
}
|
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 02:51:12 +05:30
|
|
|
return false;
|
|
|
|
}
|
2018-04-02 05:49:39 +05:30
|
|
|
read_pldheader(buf);
|
2018-04-02 02:51:12 +05:30
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
2018-04-02 05:49:39 +05:30
|
|
|
char *filename = argv[1];
|
|
|
|
char *filedata = NULL;
|
|
|
|
bool status = readpld(filename, filedata);
|
2018-04-02 02:51:12 +05:30
|
|
|
if (status) {
|
|
|
|
printf("Read OK");
|
|
|
|
} else {
|
|
|
|
printf("Read not-OK");
|
|
|
|
}
|
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
|
|
|
}
|