Mercurial > repos > blastem
annotate zip.c @ 2195:40290a923886
Implement Game Gear start button and region register
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 21 Aug 2022 22:56:32 -0700 |
parents | 137dbd05ceab |
children | 0111c8344477 |
rev | line source |
---|---|
1531
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <stdio.h> |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stdlib.h> |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include <stddef.h> |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include <string.h> |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include "util.h" |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 #include "zip.h" |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 #ifndef DISABLE_ZLIB |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 #include "zlib/zlib.h" |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 #endif |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 static const char cdfd_magic[4] = {'P', 'K', 1, 2}; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 static const char eocd_magic[4] = {'P', 'K', 5, 6}; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 #define MIN_EOCD_SIZE 22 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 #define MIN_CDFD_SIZE 46 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 #define ZIP_MAX_EOCD_OFFSET (64*1024+MIN_EOCD_SIZE) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 enum { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 ZIP_STORE = 0, |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 ZIP_DEFLATE = 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 }; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 |
1581
7121daaa48c2
Fix drag and drop when using Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1531
diff
changeset
|
22 zip_file *zip_open(const char *filename) |
1531
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 FILE *f = fopen(filename, "rb"); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 if (!f) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 long fsize = file_size(f); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 if (fsize < MIN_EOCD_SIZE) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 //too small to be a zip file |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 goto fail; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 long max_offset = fsize > ZIP_MAX_EOCD_OFFSET ? ZIP_MAX_EOCD_OFFSET : fsize; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 fseek(f, -max_offset, SEEK_END); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 uint8_t *buf = malloc(max_offset); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 if (max_offset != fread(buf, 1, max_offset, f)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 goto fail; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 long current_offset; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 uint32_t cd_start, cd_size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 uint16_t cd_count; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 for (current_offset = max_offset - MIN_EOCD_SIZE; current_offset >= 0; current_offset--) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 if (memcmp(eocd_magic, buf + current_offset, sizeof(eocd_magic))) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 continue; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 uint16_t comment_size = buf[current_offset + 20] | buf[current_offset + 21] << 8; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 if (comment_size != (max_offset - current_offset - MIN_EOCD_SIZE)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 continue; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 cd_start = buf[current_offset + 16] | buf[current_offset + 17] << 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 | buf[current_offset + 18] << 16 | buf[current_offset + 19] << 24; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 if (cd_start > (fsize - (max_offset - current_offset))) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 continue; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 cd_size = buf[current_offset + 12] | buf[current_offset + 13] << 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 | buf[current_offset + 14] << 16 | buf[current_offset + 15] << 24; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 if ((cd_start + cd_size) > (fsize - (max_offset - current_offset))) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 continue; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 cd_count = buf[current_offset + 10] | buf[current_offset + 11] << 8; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 break; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 free(buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 if (current_offset < 0) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 //failed to find EOCD |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 goto fail; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 buf = malloc(cd_size); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 fseek(f, cd_start, SEEK_SET); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 if (cd_size != fread(buf, 1, cd_size, f)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 goto fail_free; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 zip_entry *entries = calloc(cd_count, sizeof(zip_entry)); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 uint32_t cd_max_last = cd_size - MIN_CDFD_SIZE; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 zip_entry *cur_entry = entries; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 for (uint32_t off = 0; cd_count && off <= cd_max_last; cur_entry++, cd_count--) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 if (memcmp(buf + off, cdfd_magic, sizeof(cdfd_magic))) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 goto fail_entries; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 uint32_t name_length = buf[off + 28] | buf[off + 29] << 8; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 uint32_t extra_length = buf[off + 30] | buf[off + 31] << 8; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 //TODO: verify name length doesn't go past end of CD |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 cur_entry->name = malloc(name_length + 1); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 memcpy(cur_entry->name, buf + off + MIN_CDFD_SIZE, name_length); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 cur_entry->name[name_length] = 0; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 cur_entry->compressed_size = buf[off + 20] | buf[off + 21] << 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 | buf[off + 22] << 16 | buf[off + 23] << 24; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 cur_entry->size = buf[off + 24] | buf[off + 25] << 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 | buf[off + 26] << 16 | buf[off + 27] << 24; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 cur_entry->local_header_off = buf[off + 42] | buf[off + 43] << 8 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 | buf[off + 44] << 16 | buf[off + 45] << 24; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 cur_entry->compression_method = buf[off + 10] | buf[off + 11] << 8; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 off += name_length + extra_length + MIN_CDFD_SIZE; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 zip_file *z = malloc(sizeof(zip_file)); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 z->entries = entries; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 z->file = f; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 z->num_entries = cur_entry - entries; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 return z; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 fail_entries: |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 for (cur_entry--; cur_entry >= entries; cur_entry--) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 free(cur_entry->name); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 free(entries); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 fail_free: |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 free(buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 fail: |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 fclose(f); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 uint8_t *zip_read(zip_file *f, uint32_t index, size_t *out_size) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 fseek(f->file, f->entries[index].local_header_off + 26, SEEK_SET); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 uint8_t tmp[4]; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 if (sizeof(tmp) != fread(tmp, 1, sizeof(tmp), f->file)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 uint32_t local_variable = (tmp[0] | tmp[1] << 8) + (tmp[2] | tmp[3] << 8); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 fseek(f->file, f->entries[index].local_header_off + local_variable + 30, SEEK_SET); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 size_t int_size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 if (!out_size) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 out_size = &int_size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 int_size = f->entries[index].size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 uint8_t *buf = malloc(*out_size); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 if (*out_size > f->entries[index].size) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 *out_size = f->entries[index].size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 switch(f->entries[index].compression_method) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 case ZIP_STORE: |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 if (*out_size != fread(buf, 1, *out_size, f->file)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 free(buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 break; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 #ifndef DISABLE_ZLIB |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 case ZIP_DEFLATE: { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 //note in unzip.c in zlib/contrib suggests a dummy byte is needed, so we allocate an extra byte here |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 uint8_t *src_buf = malloc(f->entries[index].compressed_size + 1); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 if (f->entries[index].compressed_size != fread(src_buf, 1, f->entries[index].compressed_size, f->file)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 free(src_buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 uLongf destLen = *out_size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 z_stream stream; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 memset(&stream, 0, sizeof(stream)); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 stream.avail_in = f->entries[index].compressed_size + 1; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 stream.next_in = src_buf; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 stream.next_out = buf; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 stream.avail_out = *out_size; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 if (Z_OK == inflateInit2(&stream, -15)) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 int result = inflate(&stream, Z_FINISH); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 *out_size = stream.total_out; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 free(src_buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 inflateEnd(&stream); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 if (result != Z_OK && result != Z_STREAM_END && result != Z_BUF_ERROR) { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 free(buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 break; |
1594
137dbd05ceab
Fix some issues identified by cppcheck
Michael Pavone <pavone@retrodev.com>
parents:
1581
diff
changeset
|
179 } |
1531
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 #endif |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 default: |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 free(buf); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 return NULL; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 return buf; |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 void zip_close(zip_file *f) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 fclose(f->file); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 for (uint32_t i = 0; i < f->num_entries; i++) |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 { |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 free(f->entries[i].name); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 free(f->entries); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 free(f); |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 } |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 |
092675db4f37
Add support for loading ROMs from zip files
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 |