Mercurial > hg > toybox
annotate toys/pending/compress.c @ 1736:5892daac85ab draft
Switch nsenter to default y.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 12 Mar 2015 15:34:03 -0500 |
parents | 5de199de14ba |
children |
rev | line source |
---|---|
1199 | 1 /* compress.c - deflate/inflate code for zip, gzip, zlib, and raw |
2 * | |
3 * Copyright 2014 Rob Landley <rob@landley.net> | |
4 * | |
5 * The inflate/deflate code lives here, so the various things that use it | |
6 * either live here or call these commands to pipe data through them. | |
7 * | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
8 * Divergence from posix: replace obsolete/patented "compress" with mutiplexer. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
9 * (gzip already replaces "uncompress".) |
1199 | 10 * |
11 * See RFCs 1950 (zlib), 1951 (deflate), and 1952 (gzip) | |
12 * LSB 4.1 has gzip, gunzip, and zcat | |
13 * TODO: zip -d DIR -x LIST -list -quiet -no overwrite -overwrite -p to stdout | |
14 | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
15 // Accept many different kinds of command line argument. |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
16 // Leave Lrg at end so flag values line up. |
1199 | 17 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
18 USE_COMPRESS(NEWTOY(compress, "zcd9lrg[-cd][!zgLr]", TOYFLAG_USR|TOYFLAG_BIN)) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
19 USE_GZIP(NEWTOY(gzip, USE_GZIP_D("d")"19dcflqStvgLRz[!gLRz]", TOYFLAG_USR|TOYFLAG_BIN)) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
20 USE_ZCAT(NEWTOY(zcat, 0, TOYFLAG_USR|TOYFLAG_BIN)) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
21 USE_GUNZIP(NEWTOY(gunzip, "cflqStv", TOYFLAG_USR|TOYFLAG_BIN)) |
1199 | 22 |
23 //zip unzip gzip gunzip zcat | |
24 | |
25 config COMPRESS | |
26 bool "compress" | |
27 default n | |
28 help | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
29 usage: compress [-zgLR19] [FILE] |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
30 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
31 Compress or decompress file (or stdin) using "deflate" algorithm. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
32 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
33 -1 min compression |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
34 -9 max compression (default) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
35 -g gzip (default) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
36 -L zlib |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
37 -R raw |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
38 -z zip |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
39 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
40 config GZIP |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
41 bool "gzip" |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
42 default y |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
43 depends on COMPRESS |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
44 help |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
45 usage: gzip [-19cfqStvzgLR] [FILE...] |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
46 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
47 Compess (deflate) file(s). With no files, compress stdin to stdout. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
48 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
49 On successful decompression, compressed files are replaced with the |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
50 uncompressed version. The input file is removed and replaced with |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
51 a new file without the .gz extension (with same ownership/permissions). |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
52 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
53 -1 Minimal compression (fastest) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
54 -9 Max compression (default) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
55 -c cat to stdout (act as zcat) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
56 -f force (if output file exists, input is tty, unrecognized extension) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
57 -q quiet (no warnings) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
58 -S specify exension (default .*) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
59 -t test compressed file(s) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
60 -v verbose (like -l, but compress files) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
61 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
62 Compression type: |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
63 -g gzip (default) -L zlib -R raw -z zip |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
64 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
65 config GZIP_D |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
66 bool |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
67 default y |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
68 depends on GZIP && DECOMPRESS |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
69 help |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
70 usage: gzip [-d] |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
71 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
72 -d decompress (act as gunzip) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
73 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
74 config DECOMPRESS |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
75 bool "decompress" |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
76 default n |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
77 help |
1199 | 78 usage: compress [-zglrcd9] [FILE] |
79 | |
80 Compress or decompress file (or stdin) using "deflate" algorithm. | |
81 | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
82 -c compress with -g gzip (default) -l zlib -r raw -z zip |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
83 -d decompress (autodetects type) |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
84 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
85 |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
86 config ZCAT |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
87 bool "zcat" |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
88 default y |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
89 depends on DECOMPRESS |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
90 help |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
91 usage: zcat [FILE...] |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
92 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
93 Decompress deflated file(s) to stdout |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
94 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
95 config GUNZIP |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
96 bool "gunzip" |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
97 default y |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
98 depends on DECOMPRESS |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
99 help |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
100 usage: gunzip [-cflqStv] [FILE...] |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
101 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
102 Decompess (deflate) file(s). With no files, compress stdin to stdout. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
103 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
104 On successful decompression, compressed files are replaced with the |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
105 uncompressed version. The input file is removed and replaced with |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
106 a new file without the .gz extension (with same ownership/permissions). |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
107 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
108 -c cat to stdout (act as zcat) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
109 -f force (output file exists, input is tty, unrecognized extension) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
110 -l list compressed/uncompressed/ratio/name for each input file. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
111 -q quiet (no warnings) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
112 -S specify exension (default .*) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
113 -t test compressed file(s) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
114 -v verbose (like -l, but decompress files) |
1199 | 115 */ |
116 | |
117 #define FOR_compress | |
118 #include "toys.h" | |
119 | |
120 GLOBALS( | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
121 // Huffman codes: base offset and extra bits tables (length and distance) |
1199 | 122 char lenbits[29], distbits[30]; |
123 unsigned short lenbase[29], distbase[30]; | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
124 void *fixdisthuff, *fixlithuff; |
1200 | 125 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
126 // CRC |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
127 void (*crcfunc)(char *data, int len); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
128 unsigned crc; |
1200 | 129 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
130 // Compressed data buffer |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
131 char *data; |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
132 unsigned pos, len; |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
133 int infd, outfd; |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
134 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
135 // Tables only used for deflation |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
136 unsigned short *hashhead, *hashchain; |
1199 | 137 ) |
138 | |
139 // little endian bit buffer | |
140 struct bitbuf { | |
1200 | 141 int fd, bitpos, len, max; |
1199 | 142 char buf[]; |
143 }; | |
144 | |
1200 | 145 // malloc a struct bitbuf |
146 struct bitbuf *bitbuf_init(int fd, int size) | |
1199 | 147 { |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
148 struct bitbuf *bb = xzalloc(sizeof(struct bitbuf)+size); |
1199 | 149 |
150 bb->max = size; | |
151 bb->fd = fd; | |
152 | |
153 return bb; | |
154 } | |
155 | |
1200 | 156 // Advance bitpos without the overhead of recording bits |
157 void bitbuf_skip(struct bitbuf *bb, int bits) | |
158 { | |
159 int pos = bb->bitpos + bits, len = bb->len << 3; | |
160 | |
161 while (pos >= len) { | |
162 pos -= len; | |
163 len = (bb->len = read(bb->fd, bb->buf, bb->max)) << 3; | |
164 if (bb->len < 1) perror_exit("inflate EOF"); | |
165 } | |
166 bb->bitpos = pos; | |
167 } | |
168 | |
169 // Optimized single bit inlined version | |
170 static inline int bitbuf_bit(struct bitbuf *bb) | |
171 { | |
172 int bufpos = bb->bitpos>>3; | |
173 | |
174 if (bufpos == bb->len) { | |
175 bitbuf_skip(bb, 0); | |
176 bufpos = 0; | |
177 } | |
178 | |
179 return (bb->buf[bufpos]>>(bb->bitpos++&7))&1; | |
180 } | |
181 | |
182 // Fetch the next X bits from the bitbuf, little endian | |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
183 unsigned bitbuf_get(struct bitbuf *bb, int bits) |
1199 | 184 { |
185 int result = 0, offset = 0; | |
186 | |
187 while (bits) { | |
1200 | 188 int click = bb->bitpos >> 3, blow, blen; |
1199 | 189 |
190 // Load more data if buffer empty | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
191 if (click == bb->len) bitbuf_skip(bb, click = 0); |
1199 | 192 |
193 // grab bits from next byte | |
1200 | 194 blow = bb->bitpos & 7; |
1199 | 195 blen = 8-blow; |
196 if (blen > bits) blen = bits; | |
197 result |= ((bb->buf[click] >> blow) & ((1<<blen)-1)) << offset; | |
198 offset += blen; | |
199 bits -= blen; | |
1200 | 200 bb->bitpos += blen; |
1199 | 201 } |
202 | |
203 return result; | |
204 } | |
205 | |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
206 void bitbuf_flush(struct bitbuf *bb) |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
207 { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
208 if (!bb->bitpos) return; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
209 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
210 xwrite(bb->fd, bb->buf, (bb->bitpos+7)/8); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
211 memset(bb->buf, 0, bb->max); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
212 bb->bitpos = 0; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
213 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
214 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
215 void bitbuf_put(struct bitbuf *bb, int data, int len) |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
216 { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
217 while (len) { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
218 int click = bb->bitpos >> 3, blow, blen; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
219 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
220 // Flush buffer if necessary |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
221 if (click == bb->max) { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
222 bitbuf_flush(bb); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
223 click = 0; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
224 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
225 blow = bb->bitpos & 7; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
226 blen = 8-blow; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
227 if (blen > len) blen = len; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
228 bb->buf[click] |= data << blow; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
229 bb->bitpos += blen; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
230 data >>= blen; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
231 len -= blen; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
232 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
233 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
234 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
235 static void data_crc(char sym) |
1200 | 236 { |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
237 TT.data[TT.pos++ & 32767] = sym; |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
238 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
239 if (!(TT.pos & 32767)) { |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
240 xwrite(TT.outfd, TT.data, 32768); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
241 if (TT.crcfunc) TT.crcfunc(TT.data, 32768); |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
242 } |
1200 | 243 } |
244 | |
245 // Huffman coding uses bits to traverse a binary tree to a leaf node, | |
246 // By placing frequently occurring symbols at shorter paths, frequently | |
247 // used symbols may be represented in fewer bits than uncommon symbols. | |
248 | |
249 struct huff { | |
250 unsigned short length[16]; | |
251 unsigned short symbol[288]; | |
252 }; | |
253 | |
254 // Create simple huffman tree from array of bit lengths. | |
255 | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
256 // The symbols in the huffman trees are sorted (first by bit length |
1200 | 257 // of the code to reach them, then by symbol number). This means that given |
258 // the bit length of each symbol, we can construct a unique tree. | |
259 static void len2huff(struct huff *huff, char bitlen[], int len) | |
260 { | |
261 int offset[16]; | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
262 int i; |
1200 | 263 |
264 // Count number of codes at each bit length | |
265 memset(huff, 0, sizeof(struct huff)); | |
266 for (i = 0; i<len; i++) huff->length[bitlen[i]]++; | |
267 | |
268 // Sort symbols by bit length. (They'll remain sorted by symbol within that.) | |
269 *huff->length = *offset = 0; | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
270 for (i = 1; i<16; i++) offset[i] = offset[i-1] + huff->length[i-1]; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
271 |
1200 | 272 for (i = 0; i<len; i++) if (bitlen[i]) huff->symbol[offset[bitlen[i]]++] = i; |
273 } | |
274 | |
275 // Fetch and decode next huffman coded symbol from bitbuf. | |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
276 // This takes advantage of the sorting to navigate the tree as an array: |
1200 | 277 // each time we fetch a bit we have all the codes at that bit level in |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
278 // order with no gaps. |
1200 | 279 static unsigned huff_and_puff(struct bitbuf *bb, struct huff *huff) |
280 { | |
281 unsigned short *length = huff->length; | |
282 int start = 0, offset = 0; | |
283 | |
284 // Traverse through the bit lengths until our code is in this range | |
285 for (;;) { | |
286 offset = (offset << 1) | bitbuf_bit(bb); | |
287 start += *++length; | |
288 if ((offset -= *length) < 0) break; | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
289 if ((length - huff->length) & 16) error_exit("bad symbol"); |
1200 | 290 } |
291 | |
292 return huff->symbol[start + offset]; | |
293 } | |
294 | |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
295 // Decompress deflated data from bitbuf to TT.outfd. |
1200 | 296 static void inflate(struct bitbuf *bb) |
1199 | 297 { |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
298 TT.crc = ~0; |
1200 | 299 // repeat until spanked |
300 for (;;) { | |
301 int final, type; | |
302 | |
303 final = bitbuf_get(bb, 1); | |
304 type = bitbuf_get(bb, 2); | |
305 | |
306 if (type == 3) error_exit("bad type"); | |
307 | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
308 // Uncompressed block? |
1200 | 309 if (!type) { |
310 int len, nlen; | |
311 | |
312 // Align to byte, read length | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
313 bitbuf_skip(bb, (8-bb->bitpos)&7); |
1200 | 314 len = bitbuf_get(bb, 16); |
315 nlen = bitbuf_get(bb, 16); | |
316 if (len != (0xffff & ~nlen)) error_exit("bad len"); | |
317 | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
318 // Dump literal output data |
1200 | 319 while (len) { |
320 int pos = bb->bitpos >> 3, bblen = bb->len - pos; | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
321 char *p = bb->buf+pos; |
1200 | 322 |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
323 // dump bytes until done or end of current bitbuf contents |
1200 | 324 if (bblen > len) bblen = len; |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
325 pos = bblen; |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
326 while (pos--) data_crc(*(p++)); |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
327 bitbuf_skip(bb, bblen << 3); |
1200 | 328 len -= bblen; |
329 } | |
330 | |
331 // Compressed block | |
332 } else { | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
333 struct huff *disthuff, *lithuff; |
1200 | 334 |
335 // Dynamic huffman codes? | |
336 if (type == 2) { | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
337 struct huff *h2 = ((struct huff *)toybuf)+1; |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
338 int i, litlen, distlen, hufflen; |
1200 | 339 char *hufflen_order = "\x10\x11\x12\0\x08\x07\x09\x06\x0a\x05\x0b" |
340 "\x04\x0c\x03\x0d\x02\x0e\x01\x0f", *bits; | |
341 | |
342 // The huffman trees are stored as a series of bit lengths | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
343 litlen = bitbuf_get(bb, 5)+257; // max 288 |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
344 distlen = bitbuf_get(bb, 5)+1; // max 32 |
1200 | 345 hufflen = bitbuf_get(bb, 4)+4; // max 19 |
346 | |
347 // The literal and distance codes are themselves compressed, in | |
348 // a complicated way: an array of bit lengths (hufflen many | |
349 // entries, each 3 bits) is used to fill out an array of 19 entries | |
350 // in a magic order, leaving the rest 0. Then make a tree out of it: | |
351 memset(bits = toybuf+1, 0, 19); | |
352 for (i=0; i<hufflen; i++) bits[hufflen_order[i]] = bitbuf_get(bb, 3); | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
353 len2huff(h2, bits, 19); |
1200 | 354 |
355 // Use that tree to read in the literal and distance bit lengths | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
356 for (i = 0; i < litlen + distlen;) { |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
357 int sym = huff_and_puff(bb, h2); |
1200 | 358 |
359 // 0-15 are literals, 16 = repeat previous code 3-6 times, | |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
360 // 17 = 3-10 zeroes (3 bit), 18 = 11-138 zeroes (7 bit) |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
361 if (sym < 16) bits[i++] = sym; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
362 else { |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
363 int len = sym & 2; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
364 |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
365 len = bitbuf_get(bb, sym-14+len+(len>>1)) + 3 + (len<<2); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
366 memset(bits+i, bits[i-1] * !(sym&3), len); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
367 i += len; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
368 } |
1200 | 369 } |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
370 if (i > litlen+distlen) error_exit("bad tree"); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
371 |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
372 len2huff(lithuff = h2, bits, litlen); |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
373 len2huff(disthuff = ((struct huff *)toybuf)+2, bits+litlen, distlen); |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
374 |
1200 | 375 // Static huffman codes |
376 } else { | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
377 lithuff = TT.fixlithuff; |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
378 disthuff = TT.fixdisthuff; |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
379 } |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
380 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
381 // Use huffman tables to decode block of compressed symbols |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
382 for (;;) { |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
383 int sym = huff_and_puff(bb, lithuff); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
384 |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
385 // Literal? |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
386 if (sym < 256) data_crc(sym); |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
387 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
388 // Copy range? |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
389 else if (sym > 256) { |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
390 int len, dist; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
391 |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
392 sym -= 257; |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
393 len = TT.lenbase[sym] + bitbuf_get(bb, TT.lenbits[sym]); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
394 sym = huff_and_puff(bb, disthuff); |
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
395 dist = TT.distbase[sym] + bitbuf_get(bb, TT.distbits[sym]); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
396 sym = TT.pos & 32767; |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
397 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
398 while (len--) data_crc(TT.data[(TT.pos-dist) & 32767]); |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
399 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
400 // End of block |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
401 } else break; |
1200 | 402 } |
403 } | |
404 | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
405 // Was that the last block? |
1201
9adf66c7dd4e
Ok, _maybe_ I'm rewriting deflate from scratch rather than cleaning up the existing one, but you can't prove it. I plead the fifth, third, twelvefth, twentieth, twenty-first, twenth-fith, and twenty-seventh.
Rob Landley <rob@landley.net>
parents:
1200
diff
changeset
|
406 if (final) break; |
1200 | 407 } |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
408 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
409 if (TT.pos & 32767) { |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
410 xwrite(TT.outfd, TT.data, TT.pos & 32767); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
411 if (TT.crcfunc) TT.crcfunc(TT.data, TT.pos & 32767); |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
412 } |
1199 | 413 } |
414 | |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
415 // Deflate from TT.infd to bitbuf |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
416 // For deflate, TT.len = input read, TT.pos = input consumed |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
417 static void deflate(struct bitbuf *bb) |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
418 { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
419 char *data = TT.data; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
420 int len, final = 0; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
421 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
422 TT.crc = ~0; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
423 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
424 while (!final) { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
425 // Read next half-window of data if we haven't hit EOF yet. |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
426 len = readall(TT.infd, data+(TT.len&32768), 32768); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
427 if (len < 0) perror_exit("read"); // todo: add filename |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
428 if (len != 32768) final++; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
429 if (TT.crcfunc) TT.crcfunc(data+(TT.len&32768), len); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
430 // TT.len += len; crcfunc advances len |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
431 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
432 // store block as literal |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
433 bitbuf_put(bb, final, 1); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
434 bitbuf_put(bb, 0, 1); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
435 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
436 bitbuf_put(bb, 0, (8-bb->bitpos)&7); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
437 bitbuf_put(bb, len, 16); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
438 bitbuf_put(bb, 0xffff & ~len, 16); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
439 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
440 // repeat until spanked |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
441 while (TT.pos != TT.len) { |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
442 unsigned pos = TT.pos & 65535; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
443 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
444 bitbuf_put(bb, data[pos], 8); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
445 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
446 // need to refill buffer? |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
447 if (!(32767 & ++TT.pos) && !final) break; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
448 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
449 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
450 bitbuf_flush(bb); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
451 } |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
452 |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
453 // Allocate memory for deflate/inflate. |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
454 static void init_deflate(int compress) |
1199 | 455 { |
1261
9e105bab92e5
Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents:
1251
diff
changeset
|
456 int i, n = 1; |
1199 | 457 |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
458 // compress needs 64k data and 32k each for hashhead and hashchain. |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
459 // decompress just needs 32k data. |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
460 TT.data = xmalloc(32768*(compress ? 4 : 1)); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
461 if (compress) { |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
462 TT.hashhead = (unsigned short *)(TT.data + 65536); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
463 TT.hashchain = (unsigned short *)(TT.data + 65536 + 32768); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
464 } |
1200 | 465 |
1199 | 466 // Calculate lenbits, lenbase, distbits, distbase |
467 *TT.lenbase = 3; | |
468 for (i = 0; i<sizeof(TT.lenbits)-1; i++) { | |
469 if (i>4) { | |
470 if (!(i&3)) { | |
471 TT.lenbits[i]++; | |
472 n <<= 1; | |
473 } | |
474 if (i == 27) n--; | |
475 else TT.lenbits[i+1] = TT.lenbits[i]; | |
476 } | |
477 TT.lenbase[i+1] = n + TT.lenbase[i]; | |
478 } | |
479 n = 0; | |
480 for (i = 0; i<sizeof(TT.distbits); i++) { | |
481 TT.distbase[i] = 1<<n; | |
482 if (i) TT.distbase[i] += TT.distbase[i-1]; | |
483 if (i>3 && !(i&1)) n++; | |
484 TT.distbits[i] = n; | |
485 } | |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
486 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
487 // Init fixed huffman tables |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
488 for (i=0; i<288; i++) toybuf[i] = 8 + (i>143) - ((i>255)<<1) + (i>279); |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
489 len2huff(TT.fixlithuff = ((struct huff *)toybuf)+3, toybuf, 288); |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
490 memset(toybuf, 5, 30); |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
491 len2huff(TT.fixdisthuff = ((struct huff *)toybuf)+4, toybuf, 30); |
1199 | 492 } |
493 | |
494 // Return true/false whether we consumed a gzip header. | |
495 static int is_gzip(struct bitbuf *bb) | |
496 { | |
497 int flags; | |
498 | |
499 // Confirm signature | |
1200 | 500 if (bitbuf_get(bb, 24) != 0x088b1f || (flags = bitbuf_get(bb, 8)) > 31) |
501 return 0; | |
502 bitbuf_skip(bb, 6*8); | |
1199 | 503 |
504 // Skip extra, name, comment, header CRC fields | |
1200 | 505 if (flags & 4) bitbuf_skip(bb, 16); |
506 if (flags & 8) while (bitbuf_get(bb, 8)); | |
507 if (flags & 16) while (bitbuf_get(bb, 8)); | |
508 if (flags & 2) bitbuf_skip(bb, 16); | |
1199 | 509 |
510 return 1; | |
511 } | |
512 | |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
513 void gzip_crc(char *data, int len) |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
514 { |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
515 int i; |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
516 unsigned crc, *crc_table = (unsigned *)(toybuf+sizeof(toybuf)-1024); |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
517 |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
518 crc = TT.crc; |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
519 for (i=0; i<len; i++) crc = crc_table[(crc^data[i])&0xff] ^ (crc>>8); |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
520 TT.crc = crc; |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
521 TT.len += len; |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
522 } |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
523 |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
524 static void do_gzip(int fd, char *name) |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
525 { |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
526 struct bitbuf *bb = bitbuf_init(1, sizeof(toybuf)); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
527 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
528 // Header from RFC 1952 section 2.2: |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
529 // 2 ID bytes (1F, 8b), gzip method byte (8=deflate), FLAG byte (none), |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
530 // 4 byte MTIME (zeroed), Extra Flags (2=maximum compression), |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
531 // Operating System (FF=unknown) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
532 |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
533 TT.infd = fd; |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
534 xwrite(bb->fd, "\x1f\x8b\x08\0\0\0\0\0\x02\xff", 10); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
535 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
536 // Use last 1k of toybuf for little endian crc table |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
537 crc_init((unsigned *)(toybuf+sizeof(toybuf)-1024), 1); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
538 TT.crcfunc = gzip_crc; |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
539 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
540 deflate(bb); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
541 |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
542 // tail: crc32, len32 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
543 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
544 bitbuf_put(bb, 0, (8-bb->bitpos)&7); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
545 bitbuf_put(bb, ~TT.crc, 32); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
546 bitbuf_put(bb, TT.len, 32); |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
547 |
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
548 bitbuf_flush(bb); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
549 free(bb); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
550 } |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
551 |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
552 static void do_zcat(int fd, char *name) |
1199 | 553 { |
1200 | 554 struct bitbuf *bb = bitbuf_init(fd, sizeof(toybuf)); |
1199 | 555 |
1200 | 556 if (!is_gzip(bb)) error_exit("not gzip"); |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
557 TT.outfd = 1; |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
558 |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
559 // Use last 1k of toybuf for little endian crc table |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
560 crc_init((unsigned *)(toybuf+sizeof(toybuf)-1024), 1); |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
561 TT.crcfunc = gzip_crc; |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
562 |
1200 | 563 inflate(bb); |
1199 | 564 |
565 // tail: crc32, len32 | |
566 | |
1206
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
567 bitbuf_skip(bb, (8-bb->bitpos)&7); |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
568 if (~TT.crc != bitbuf_get(bb, 32) || TT.len != bitbuf_get(bb, 32)) |
1b36fd8dd5cf
Add crc code: zcat now works.
Rob Landley <rob@landley.net>
parents:
1204
diff
changeset
|
569 error_exit("bad crc"); |
1199 | 570 free(bb); |
571 } | |
572 | |
573 // Parse many different kinds of command line argument: | |
574 | |
575 void compress_main(void) | |
576 { | |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
577 // todo: this |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
578 printf("hello world"); |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
579 } |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
580 |
1261
9e105bab92e5
Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents:
1251
diff
changeset
|
581 //#define CLEANUP_compress |
9e105bab92e5
Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents:
1251
diff
changeset
|
582 //#define FOR_zcat |
9e105bab92e5
Revert lots of half-finished local debris I didn't mean to check in with Isaac's roadmap update.
Rob Landley <rob@landley.net>
parents:
1251
diff
changeset
|
583 //#include "generated/flags.h" |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
584 |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
585 void zcat_main(void) |
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
586 { |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
587 init_deflate(0); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
588 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
589 loopfiles(toys.optargs, do_zcat); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
590 } |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
591 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
592 void gunzip_main(void) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
593 { |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
594 init_deflate(0); |
1199 | 595 |
1204
a0f08d393def
Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents:
1201
diff
changeset
|
596 loopfiles(toys.optargs, do_zcat); |
1199 | 597 } |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
598 |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
599 void gzip_main(void) |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
600 { |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
601 init_deflate(1); |
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
602 |
1597
5de199de14ba
More gzip stuff: now creates -0 compression files (store only), and does so to stdout.
Rob Landley <rob@landley.net>
parents:
1592
diff
changeset
|
603 loopfiles(toys.optargs, do_gzip); |
1592
c0dee3caad31
Start of deflate compress-side code, mostly refactoring and stubs so far.
Rob Landley <rob@landley.net>
parents:
1261
diff
changeset
|
604 } |