2 * This is free and unencumbered software released into the public domain.
17 #define COMPRESSION_NONE 0x00
18 #define COMPRESSION_DEFLATE 0x08
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
23 find_central_directory(uint8_t *addr, size_t len)
30 * At -22 bytes from the end there is the end of the central
31 * directory assuming an empty comment. It's a sensible place
41 if (memcmp(p, "\x50\x4b\x05\x06", 4) == 0)
47 /* read comment length */
48 memcpy(&clen, p + 20, sizeof(clen));
51 /* false signature inside a comment? */
52 if (clen + 22 != end - p) {
57 /* read the offset for the central directory */
58 memcpy(&offset, p + 16, sizeof(offset));
59 offset = le32toh(offset);
61 if (addr + offset > p)
68 unzip_none(uint8_t *data, size_t size, unsigned long ocrc)
70 unsigned long crc = 0;
72 fwrite(data, 1, size, stdout);
74 crc = crc32(0, data, size);
76 errx(1, "CRC mismatch");
80 unzip_deflate(uint8_t *data, size_t size, unsigned long ocrc)
84 unsigned long crc = 0;
87 stream.zalloc = Z_NULL;
88 stream.zfree = Z_NULL;
89 stream.opaque = Z_NULL;
90 stream.next_in = data;
91 stream.avail_in = size;
92 stream.next_out = Z_NULL;
94 if (inflateInit2(&stream, -15) != Z_OK)
95 err(1, "inflateInit failed");
98 stream.next_out = buf;
99 stream.avail_out = sizeof(buf);
101 switch (inflate(&stream, Z_BLOCK)) {
103 errx(1, "stream error");
105 errx(1, "need dict");
107 errx(1, "data error: %s", stream.msg);
109 errx(1, "memory error");
112 have = sizeof(buf) - stream.avail_out;
113 fwrite(buf, 1, have, stdout);
114 crc = crc32(crc, buf, have);
115 } while (stream.avail_out == 0);
120 errx(1, "CRC mismatch");
124 unzip(uint8_t *zip, size_t len, uint8_t *entry)
126 uint32_t size, osize, crc, off;
127 uint16_t flags, compression;
129 uint8_t *data, *offset;
131 /* read the offset of the file record */
132 memcpy(&off, entry + 42, sizeof(off));
133 offset = zip + le32toh(off);
135 if (offset > zip + len - 4 ||
136 memcmp(offset, "\x50\x4b\x03\x04", 4) != 0)
137 errx(1, "invalid offset or file header signature");
139 memcpy(&flags, offset + 6, sizeof(flags));
140 memcpy(&compression, offset + 8, sizeof(compression));
142 flags = le16toh(flags);
143 compression = le16toh(compression);
145 memcpy(&crc, entry + 16, sizeof(crc));
146 memcpy(&size, entry + 20, sizeof(size));
147 memcpy(&osize, entry + 24, sizeof(osize));
150 size = le32toh(size);
151 osize = le32toh(osize);
153 memcpy(&flen, offset + 26, sizeof(flen));
154 memcpy(&xlen, offset + 28, sizeof(xlen));
156 flen = le16toh(flen);
157 xlen = le16toh(xlen);
159 data = offset + 30 + flen + xlen;
160 if (data + size > zip + len)
161 errx(1, "corrupted zip, offset out of file");
163 switch (compression) {
164 case COMPRESSION_NONE:
165 unzip_none(data, size, crc);
167 case COMPRESSION_DEFLATE:
168 unzip_deflate(data, size, crc);
171 errx(1, "unknown compression method 0x%02x",
177 next(uint8_t *zip, size_t len, uint8_t *entry)
179 uint16_t flen, xlen, clen;
182 memcpy(&flen, entry + 28, sizeof(flen));
183 memcpy(&xlen, entry + 28 + 2, sizeof(xlen));
184 memcpy(&clen, entry + 28 + 2 + 2, sizeof(xlen));
186 flen = le16toh(flen);
187 xlen = le16toh(xlen);
188 clen = le16toh(clen);
190 next = entry + 46 + flen + xlen + clen;
192 if (next >= end - 46 ||
193 memcmp(next, "\x50\x4b\x01\x02", 4) != 0)
199 filename(uint8_t *zip, size_t len, uint8_t *entry, char *buf,
205 memcpy(&flen, entry + 28, sizeof(flen));
206 flen = le16toh(flen);
208 s = MIN(size-1, flen);
209 memcpy(buf, entry + 46, s);
214 ls(uint8_t *zip, size_t len, uint8_t *cd)
219 filename(zip, len, cd, name, sizeof(name));
220 printf("%s\n", name);
221 } while ((cd = next(zip, len, cd)) != NULL);
225 find_file(uint8_t *zip, size_t len, uint8_t *cd, const char *target)
230 filename(zip, len, cd, name, sizeof(name));
231 if (!strcmp(name, target))
233 } while ((cd = next(zip, len, cd)) != NULL);
239 extract_file(uint8_t *zip, size_t len, uint8_t *cd, const char *target)
241 if ((cd = find_file(zip, len, cd, target)) == NULL)
249 map_file(int fd, size_t *len)
254 if ((jump = lseek(fd, 0, SEEK_END)) == -1)
257 if (lseek(fd, 0, SEEK_SET) == -1)
260 if ((addr = mmap(NULL, jump, PROT_READ, MAP_PRIVATE, fd, 0))
269 main(int argc, char **argv)
276 fprintf(stderr, "Usage: %s archive.zip [files...]",
281 if ((fd = open(argv[1], O_RDONLY)) == -1)
282 err(1, "can't open %s", argv[1]);
284 zip = map_file(fd, &len);
287 if (pledge("stdio", NULL) == -1)
291 if ((cd = find_central_directory(zip, len)) == NULL)
292 errx(1, "can't find the central directory");
297 for (i = 2; i < argc; ++i)
298 extract_file(zip, len, cd, argv[i]);