(de)compress can use new romfile.[ch]

This commit is contained in:
Olaf Rempel 2007-04-30 15:20:09 +02:00
parent ee74ea8a08
commit 956e34db7b
6 changed files with 112 additions and 105 deletions

View File

@ -5,10 +5,10 @@ all: zyxel-revert compress decompress
zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
$(CC) $(CFLAGS) $^ -o $@
compress: lzsc.o filedata.o compress.o
compress: lzsc.o filedata.o romfile.o compress.o
$(CC) $(CFLAGS) $^ -o $@
decompress: lzsd.o filedata.o decompress.o
decompress: lzsd.o filedata.o romfile.o decompress.o
$(CC) $(CFLAGS) $^ -o $@
%.o: %.c

View File

@ -2,67 +2,24 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include "lzsc.h"
#include "filedata.h"
#include "romfile.h"
struct rom0file {
uint16_t version;
uint16_t size;
uint16_t offset;
char name[14];
} __attribute__((packed));
int parse_rom0(struct filedata *filedata, const char *infile)
{
struct rom0file file;
/* zweite page enthält config */
uint32_t offset = 0x2000;
while (1) {
memcpy(&file, (void *)(filedata->data) + offset, sizeof(file));
if (strcmp(file.name, "autoexec.net") == 0) {
printf("found autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
/* 16 byte header */
void *buf = (void *)(filedata->data) + 0x2000 + htons(file.offset) +12;
/* little cleanup */
memset(buf, 0, htons(file.size));
struct filedata *indata = get_filedata(infile);
int size = lzs_pack(indata->data, indata->size, buf, 0xC00);
file.size = htons(size +12);
memcpy((void *)(filedata->data) + offset, &file, sizeof(file));
printf("new autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
put_filedata("350LI2C1.rom.own", filedata);
free(indata);
return 0;
}
if (file.name[0] == 0 || file.name[0] == -1)
return -1;
offset += sizeof(file);
}
}
/*
* $ compress <rom-0-base> <plain-config> <rom-0-outfile>
*/
int main(int argc, char *argv[])
{
struct filedata *rom0 = get_filedata("350LI2C1.rom");
parse_rom0(rom0, "350LI2C1.rom.own_decomp");
struct romfile *rom = get_romfile(argv[1], "autoexec.net");
free(rom0);
struct filedata *config = get_filedata(argv[2]);
rom->size = lzs_pack(config->data, config->size, rom->data + 0xC, 0x1000);
put_romfile(argv[3], rom);
free(config);
free(rom);
return 0;
}

View File

@ -2,64 +2,27 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include "lzsd.h"
#include "filedata.h"
struct rom0file {
uint16_t version;
uint16_t size;
uint16_t offset;
char name[14];
} __attribute__((packed));
int parse_rom0(struct filedata *filedata, const char *outfile)
{
struct rom0file file;
/* zweite page enthält config */
uint32_t offset = 0x2000;
while (1) {
memcpy(&file, (void *)(filedata->data) + offset, sizeof(file));
if (strcmp(file.name, "autoexec.net") == 0) {
printf("found autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
/* 64kb sollten reichen */
struct filedata *out = alloc_filedata(65535);
/* 16 byte header */
void *buf = (void *)(filedata->data) + 0x2000 + htons(file.offset) +12;
out->size = lzs_unpack(buf, htons(file.size) -12, out->data, out->size);
put_filedata(outfile, out);
free(out);
return 0;
}
if (file.name[0] == 0 || file.name[0] == -1)
return -1;
offset += sizeof(file);
}
}
#include "romfile.h"
/*
* $ decompress <rom-0-file>
*/
int main(int argc, char *argv[])
{
struct filedata *in = get_filedata(argv[1]);
struct romfile *rom = get_romfile(argv[1], "autoexec.net");
struct filedata *config = alloc_filedata(65536);
config->size = lzs_unpack(rom->data + 0xC, rom->size - 0xC, config->data, config->size);
char outname[64];
strncpy(outname, argv[1], sizeof(outname));
strcat(outname, ".own_decomp");
parse_rom0(in, outname);
put_filedata(outname, config);
free(in);
free(config);
free(rom);
return 0;
}

2
lzsd.h
View File

@ -1,6 +1,8 @@
#ifndef _LZSD_H_
#define _LZSD_H_
#include <stdint.h>
uint32_t lzs_unpack(void *src, uint32_t srcsize, void *dst, uint32_t dstsize);
#endif /* _LZSD_H_ */

63
romfile.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include "filedata.h"
#include "romfile.h"
struct romfile_header {
uint16_t version;
uint16_t size;
uint16_t offset;
char name[14];
} __attribute__((packed));
struct romfile * get_romfile(const char *filename, const char *romfile_name)
{
struct romfile *romfile = malloc(sizeof(struct romfile));
if (romfile == NULL)
return NULL;
romfile->file = get_filedata(filename);
if (romfile->file == NULL) {
free(romfile);
return NULL;
}
romfile->baseoffset = 0x2000;
struct romfile_header header;
while (1) {
memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
if (strcmp(header.name, romfile_name) == 0) {
romfile->data = (void *)(romfile->file->data) + 0x2000 + htons(header.offset);
romfile->size = htons(header.size);
return romfile;
}
if (header.name[0] == 0 || header.name[0] == -1) {
free(romfile->file);
free(romfile);
return NULL;
}
romfile->baseoffset += sizeof(header);
}
}
int put_romfile(const char *filename, struct romfile *romfile)
{
struct romfile_header header;
memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
header.size = htons(romfile->size);
memcpy((void *)(romfile->file->data) + romfile->baseoffset, &header, sizeof(header));
return put_filedata(filename, romfile->file);
}

22
romfile.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef _ROMFILE_H_
#define _ROMFILE_H_
#include <stdint.h>
#include "filedata.h"
struct romfile {
struct filedata *file;
/* offset to struct rom0file-struct in file->data */
uint32_t baseoffset;
/* the data */
uint8_t *data;
uint16_t size;
};
struct romfile * get_romfile(const char *filename, const char *romfile_name);
int put_romfile(const char *filename, struct romfile *romfile);
#endif /* _ROMFILE_H_ */