2 * This is free and unencumbered software released into the public domain.
17 #define MIN(a, b) ((a) < (b) ? (a) : (b))
20 find_central_directory(uint8_t *addr, size_t len)
27 * At -22 bytes from the end there is the end of the central
28 * directory assuming an empty comment. It's a sensible place
38 if (memcmp(p, "\x50\x4b\x05\x06", 4) == 0)
44 /* read comment length */
45 memcpy(&clen, p + 20, sizeof(clen));
48 /* false signature inside a comment? */
49 if (clen + 22 != end - p) {
54 /* read the offset for the central directory */
55 memcpy(&offset, p + 16, sizeof(offset));
56 offset = le32toh(offset);
58 if (addr + offset > p)
65 ls(uint8_t *zip, size_t len, uint8_t *cd)
67 uint16_t flen, xlen, clen;
69 char filename[PATH_MAX];
72 while (cd < end - 46 && memcmp(cd, "\x50\x4b\x01\x02", 4) == 0) {
73 memcpy(&flen, cd + 28, sizeof(flen));
74 memcpy(&xlen, cd + 28 + 2, sizeof(xlen));
75 memcpy(&clen, cd + 28 + 2 + 2, sizeof(clen));
81 memset(filename, 0, sizeof(filename));
82 memcpy(filename, cd + 46, MIN(sizeof(filename)-1, flen));
84 printf("%s\n", filename);
86 cd += 46 + flen + xlen + clen;
91 map_file(int fd, size_t *len)
96 if ((jump = lseek(fd, 0, SEEK_END)) == -1)
99 if (lseek(fd, 0, SEEK_SET) == -1)
102 if ((addr = mmap(NULL, jump, PROT_READ, MAP_PRIVATE, fd, 0))
111 main(int argc, char **argv)
118 errx(1, "missing file to inspect");
120 if ((fd = open(argv[1], O_RDONLY)) == -1)
121 err(1, "can't open %s", argv[1]);
123 zip = map_file(fd, &len);
124 if ((cd = find_central_directory(zip, len)) == NULL)
125 errx(1, "can't find the central directory");