seekgz.c - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
seekgz.c (1039B)
---
1 /* seek a file for (embedded) GZ data, list them one by line:
2 offset<TAB>length
3
4 For each line one can write the data segments:
5
6 dd skip=offsetB count=lengthB if=input of=output */
7
8 #include <stdio.h>
9
10 #define GETCHAR getchar_unlocked
11
12 void
13 output(size_t o1, size_t o2)
14 {
15 printf("%zu\t%zu", o1, o2 - o1);
16 /* dd command for copy-pasta (I'm lazy) */
17 if (o1)
18 printf("\tdd skip=%zuB count=%zuB", o1, o2 - o1);
19 else
20 printf("\tdd count=%zuB", o2 - o1);
21 printf("\n");
22 }
23
24 int
25 main(void)
26 {
27 size_t offset = 0, prevoffset = 0, len = 0;
28 unsigned char tab[] = { 0x1f, 0x8b, 0x08 }; /* GZ magic (0x1f, 0x8b), 0x08 is deflate */
29 int c, b = 0, tablen = 3, f = 0;
30
31 while ((c = GETCHAR()) != EOF) {
32 len++;
33 if (c == tab[b]) {
34 b++;
35 if (b == tablen) {
36 f = 1;
37 b = 0;
38 offset = len - tablen;
39 if (prevoffset)
40 output(prevoffset, offset);
41 prevoffset = offset;
42 }
43 } else {
44 b = 0;
45 }
46 }
47
48 if (ferror(stdin)) {
49 perror(NULL);
50 return 1;
51 }
52
53 if (len > offset && f)
54 output(offset, len);
55
56 return 0;
57 }