Browse Source

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

Olaf Rempel 12 years ago
parent
commit
956e34db7b
6 changed files with 111 additions and 104 deletions
  1. 2
    2
      Makefile
  2. 11
    54
      compress.c
  3. 11
    48
      decompress.c
  4. 2
    0
      lzsd.h
  5. 63
    0
      romfile.c
  6. 22
    0
      romfile.h

+ 2
- 2
Makefile View File

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

+ 11
- 54
compress.c View File

@@ -2,67 +2,24 @@
2 2
 #include <stdlib.h>
3 3
 #include <unistd.h>
4 4
 #include <string.h>
5
-#include <stdint.h>
6
-
7
-#include <arpa/inet.h>
8 5
 
9 6
 #include "lzsc.h"
10 7
 #include "filedata.h"
8
+#include "romfile.h"
11 9
 
12
-struct rom0file {
13
-	uint16_t version;
14
-	uint16_t size;
15
-	uint16_t offset;
16
-
17
-	char name[14];
18
-} __attribute__((packed));
19
-
20
-int parse_rom0(struct filedata *filedata, const char *infile)
10
+/*
11
+ * $ compress <rom-0-base> <plain-config> <rom-0-outfile>
12
+ */
13
+int main(int argc, char *argv[])
21 14
 {
22
-	struct rom0file file;
23
-
24
-	/* zweite page enthält config */
25
-	uint32_t offset = 0x2000;
26
-	while (1) {
27
-		memcpy(&file, (void *)(filedata->data) + offset, sizeof(file));
28
-
29
-		if (strcmp(file.name, "autoexec.net") == 0) {
30
-			printf("found autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
31
-				0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
32
-
33
-			/* 16 byte header */
34
-			void *buf = (void *)(filedata->data) + 0x2000 + htons(file.offset) +12;
35
-
36
-			/* little cleanup */
37
-			memset(buf, 0, htons(file.size));
38
-
39
-			struct filedata *indata = get_filedata(infile);
40
-			int size = lzs_pack(indata->data, indata->size, buf, 0xC00);
15
+	struct romfile *rom = get_romfile(argv[1], "autoexec.net");
41 16
 
42
-			file.size = htons(size +12);
17
+	struct filedata *config = get_filedata(argv[2]);
18
+	rom->size = lzs_pack(config->data, config->size, rom->data + 0xC, 0x1000);
43 19
 
44
-			memcpy((void *)(filedata->data) + offset, &file, sizeof(file));
45
-			printf("new autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
46
-				0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
47
-
48
-			put_filedata("350LI2C1.rom.own", filedata);
49
-
50
-			free(indata);
51
-			return 0;
52
-		}
53
-
54
-		if (file.name[0] == 0 || file.name[0] == -1)
55
-			return -1;
56
-
57
-		offset += sizeof(file);
58
-	}
59
-}
60
-
61
-int main(int argc, char *argv[])
62
-{
63
-	struct filedata *rom0 = get_filedata("350LI2C1.rom");
64
-	parse_rom0(rom0, "350LI2C1.rom.own_decomp");
20
+	put_romfile(argv[3], rom);
65 21
 
66
-	free(rom0);
22
+	free(config);
23
+	free(rom);
67 24
 	return 0;
68 25
 }

+ 11
- 48
decompress.c View File

@@ -2,64 +2,27 @@
2 2
 #include <stdlib.h>
3 3
 #include <unistd.h>
4 4
 #include <string.h>
5
-#include <stdint.h>
6
-
7
-#include <arpa/inet.h>
8 5
 
9 6
 #include "lzsd.h"
10
-#include "filedata.h"
11
-
12
-struct rom0file {
13
-	uint16_t version;
14
-	uint16_t size;
15
-	uint16_t offset;
16
-
17
-	char name[14];
18
-} __attribute__((packed));
19
-
20
-int parse_rom0(struct filedata *filedata, const char *outfile)
21
-{
22
-	struct rom0file file;
23
-
24
-	/* zweite page enthält config */
25
-	uint32_t offset = 0x2000;
26
-	while (1) {
27
-		memcpy(&file, (void *)(filedata->data) + offset, sizeof(file));
28
-
29
-		if (strcmp(file.name, "autoexec.net") == 0) {
30
-			printf("found autoexec.net: 0x%04x - 0x%04x (%d bytes)\n",
31
-				0x2000 + htons(file.offset), 0x2000 + htons(file.offset) + htons(file.size), htons(file.size));
32
-
33
-			/* 64kb sollten reichen */
34
-			struct filedata *out = alloc_filedata(65535);
35
-
36
-			/* 16 byte header */
37
-			void *buf = (void *)(filedata->data) + 0x2000 + htons(file.offset) +12;
38
-
39
-			out->size = lzs_unpack(buf, htons(file.size) -12, out->data, out->size);
40
-			put_filedata(outfile, out);
41
-			free(out);
42
-
43
-			return 0;
44
-		}
45
-
46
-		if (file.name[0] == 0 || file.name[0] == -1)
47
-			return -1;
48
-
49
-		offset += sizeof(file);
50
-	}
51
-}
7
+#include "romfile.h"
52 8
 
9
+/*
10
+ * $ decompress <rom-0-file>
11
+ */
53 12
 int main(int argc, char *argv[])
54 13
 {
55
-	struct filedata *in = get_filedata(argv[1]);
14
+	struct romfile *rom = get_romfile(argv[1], "autoexec.net");
15
+
16
+	struct filedata *config = alloc_filedata(65536);
17
+	config->size = lzs_unpack(rom->data + 0xC, rom->size - 0xC, config->data, config->size);
56 18
 
57 19
 	char outname[64];
58 20
 	strncpy(outname, argv[1], sizeof(outname));
59 21
 	strcat(outname, ".own_decomp");
60 22
 
61
-	parse_rom0(in, outname);
23
+	put_filedata(outname, config);
62 24
 
63
-	free(in);
25
+	free(config);
26
+	free(rom);
64 27
 	return 0;
65 28
 }

+ 2
- 0
lzsd.h View File

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

+ 63
- 0
romfile.c View File

@@ -0,0 +1,63 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <unistd.h>
4
+#include <string.h>
5
+#include <stdint.h>
6
+
7
+#include <arpa/inet.h>
8
+
9
+#include "filedata.h"
10
+#include "romfile.h"
11
+
12
+struct romfile_header {
13
+	uint16_t version;
14
+	uint16_t size;
15
+	uint16_t offset;
16
+
17
+	char name[14];
18
+} __attribute__((packed));
19
+
20
+struct romfile * get_romfile(const char *filename, const char *romfile_name)
21
+{
22
+	struct romfile *romfile = malloc(sizeof(struct romfile));
23
+	if (romfile == NULL)
24
+		return NULL;
25
+
26
+	romfile->file = get_filedata(filename);
27
+	if (romfile->file == NULL) {
28
+		free(romfile);
29
+		return NULL;
30
+	}
31
+
32
+	romfile->baseoffset = 0x2000;
33
+
34
+	struct romfile_header header;
35
+	while (1) {
36
+		memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
37
+
38
+		if (strcmp(header.name, romfile_name) == 0) {
39
+			romfile->data = (void *)(romfile->file->data) + 0x2000 + htons(header.offset);
40
+			romfile->size = htons(header.size);
41
+			return romfile;
42
+		}
43
+
44
+		if (header.name[0] == 0 || header.name[0] == -1) {
45
+			free(romfile->file);
46
+			free(romfile);
47
+			return NULL;
48
+		}
49
+
50
+		romfile->baseoffset += sizeof(header);
51
+	}
52
+}
53
+
54
+int put_romfile(const char *filename, struct romfile *romfile)
55
+{
56
+	struct romfile_header header;
57
+	memcpy(&header, (void *)(romfile->file->data) + romfile->baseoffset, sizeof(header));
58
+
59
+	header.size = htons(romfile->size);
60
+	memcpy((void *)(romfile->file->data) + romfile->baseoffset, &header, sizeof(header));
61
+
62
+	return put_filedata(filename, romfile->file);
63
+}

+ 22
- 0
romfile.h View File

@@ -0,0 +1,22 @@
1
+#ifndef _ROMFILE_H_
2
+#define _ROMFILE_H_
3
+
4
+#include <stdint.h>
5
+
6
+#include "filedata.h"
7
+
8
+struct romfile {
9
+	struct filedata *file;
10
+
11
+	/* offset to struct rom0file-struct in file->data */
12
+	uint32_t baseoffset;
13
+
14
+	/* the data */
15
+	uint8_t *data;
16
+	uint16_t size;
17
+};
18
+
19
+struct romfile * get_romfile(const char *filename, const char *romfile_name);
20
+int put_romfile(const char *filename, struct romfile *romfile);
21
+
22
+#endif /* _ROMFILE_H_ */

Loading…
Cancel
Save