2 look for raw deflate regions in a file
14 #define DEBUGADD(l, x) printf x
17 #define MIN(a,b) ((a)<(b)?(a):(b))
20 static void print_asc(int level, const unsigned char *buf,int len)
24 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
27 static void dump_data(int level, const unsigned char *buf,int len)
32 DEBUGADD(level,("[%03X] ",i));
34 DEBUGADD(level,("%02X ",(int)buf[i]));
36 if (i%8 == 0) DEBUGADD(level,(" "));
38 print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
39 print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
40 if (i<len) DEBUGADD(level,("[%03X] ",i));
46 DEBUGADD(level,(" "));
47 if (n>8) DEBUGADD(level,(" "));
48 while (n--) DEBUGADD(level,(" "));
50 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
52 if (n>0) print_asc(level,&buf[i-n],n);
53 DEBUGADD(level,("\n"));
57 static void *map_file(const char *fname, size_t *size)
59 int fd = open(fname, O_RDONLY);
63 if (fd == -1) return MAP_FAILED;
66 p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
73 static void file_save(const char *fname, const char *p, off_t size)
76 fd = open(fname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
81 if (write(fd, p, size) != size) {
82 fprintf(stderr, "Failed to save %d bytes to %s\n", (int)size, fname);
88 int main(int argc, const char *argv[])
96 buf = map_file(argv[1], &size);
97 if (buf == MAP_FAILED) {
102 for (i=0;i<size-1;i++) {
103 memset(&zs, 0, sizeof(zs));
105 zs.avail_in = size - i;
106 zs.next_in = i + (char *)buf;
107 zs.next_out = outbuf;
108 zs.avail_out = sizeof(outbuf);
110 ret = inflateInit2(&zs, -15);
112 fprintf(stderr,"inflateInit2 error %d\n", ret);
116 inflate(&zs, Z_BLOCK);
118 if (zs.avail_out != sizeof(outbuf) && sizeof(outbuf)-zs.avail_out > 200) {
119 printf("Inflated %u bytes at offset %u (consumed %u bytes)\n",
120 sizeof(outbuf)-zs.avail_out, i, (size-i) - zs.avail_in);
121 dump_data(0, outbuf, sizeof(outbuf)-zs.avail_out);
122 if (sizeof(outbuf)-zs.avail_out > 10000) {
123 file_save("x.dat", outbuf, sizeof(outbuf)-zs.avail_out);