12 #define GZIP_HEADER "\037\213\008"
14 static void *map_file(const char *fname, off_t *size)
20 fd = open(fname, O_RDONLY);
27 p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
35 static void file_save(const char *fname, const char *p, off_t size)
38 fd = open(fname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
43 if (write(fd, p, size) != size) {
44 fprintf(stderr, "Failed to save %d bytes to %s\n", (int)size, fname);
50 static void scan_memory(int i, unsigned char *p, off_t size)
52 unsigned char *p0 = p;
55 while ((p = memmem(p, size - (p-p0), GZIP_HEADER, strlen(GZIP_HEADER)))) {
63 /* only want unix gzip output */
70 asprintf(&fname, "extract-%d-%d.gz", i, found);
71 file_save(fname, p, size - (p-p0));
72 printf("Extracted %d bytes to %s\n", (int)(size - (p-p0)), fname);
78 int main(int argc, const char *argv[])
82 for (i=1;i<argc;i++) {
86 printf("Scanning %s\n", argv[i]);
88 p = map_file(argv[i], &size);
90 scan_memory(i, p, size);