show lock scaling table
[tridge/junkcode.git] / zfind.c
1 /*
2   look for raw deflate regions in a file
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <zlib.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <sys/mman.h>
12 #include <sys/stat.h>
13
14 #define DEBUGADD(l, x) printf x
15
16 #ifndef MIN
17 #define MIN(a,b) ((a)<(b)?(a):(b))
18 #endif
19
20 static void print_asc(int level, const unsigned char *buf,int len)
21 {
22         int i;
23         for (i=0;i<len;i++)
24                 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
25 }
26
27 static void dump_data(int level, const unsigned char *buf,int len)
28 {
29         int i=0;
30         if (len<=0) return;
31
32         DEBUGADD(level,("[%03X] ",i));
33         for (i=0;i<len;) {
34                 DEBUGADD(level,("%02X ",(int)buf[i]));
35                 i++;
36                 if (i%8 == 0) DEBUGADD(level,(" "));
37                 if (i%16 == 0) {      
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));
41                 }
42         }
43         if (i%16) {
44                 int n;
45                 n = 16 - (i%16);
46                 DEBUGADD(level,(" "));
47                 if (n>8) DEBUGADD(level,(" "));
48                 while (n--) DEBUGADD(level,("   "));
49                 n = MIN(8,i%16);
50                 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
51                 n = (i%16) - n;
52                 if (n>0) print_asc(level,&buf[i-n],n); 
53                 DEBUGADD(level,("\n"));    
54         }       
55 }
56
57 static void *map_file(const char *fname, size_t *size)
58 {
59         int fd = open(fname, O_RDONLY);
60         struct stat st;
61         void *p;
62
63         if (fd == -1) return MAP_FAILED;
64
65         fstat(fd, &st);
66         p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
67         close(fd);
68
69         *size = st.st_size;
70         return p;
71 }
72
73 static void file_save(const char *fname, const char *p, off_t size)
74 {
75         int fd;
76         fd = open(fname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
77         if (fd == -1) {
78                 perror(fname);
79                 return;
80         }
81         if (write(fd, p, size) != size) {
82                 fprintf(stderr, "Failed to save %d bytes to %s\n", (int)size, fname);
83                 return;
84         }
85         close(fd);
86 }
87
88 int main(int argc, const char *argv[])
89 {
90         char outbuf[102400];
91         struct z_stream_s zs;
92         int ret, i;
93         void *buf;
94         size_t size;
95
96         buf = map_file(argv[1], &size);
97         if (buf == MAP_FAILED) {
98                 perror(argv[1]);
99                 exit(1);
100         }
101
102         for (i=0;i<size-1;i++) {
103                 memset(&zs, 0, sizeof(zs));
104
105                 zs.avail_in = size - i;
106                 zs.next_in = i + (char *)buf;
107                 zs.next_out = outbuf;
108                 zs.avail_out = sizeof(outbuf);
109
110                 ret = inflateInit2(&zs, -15);
111                 if (ret != Z_OK) {
112                         fprintf(stderr,"inflateInit2 error %d\n", ret);
113                         goto failed;
114                 }
115
116                 inflate(&zs, Z_BLOCK);
117
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);
124                         }
125                 }
126
127                 inflateEnd(&zs);
128         }
129
130         return 0;
131
132 failed:
133         return -1;
134 }