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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* compress.c - deflate/inflate code for zip, gzip, zlib, and raw
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2014 Rob Landley <rob@landley.net>
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * The inflate/deflate code lives here, so the various things that use it
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * either live here or call these commands to pipe data through them.
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 *
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 * See RFCs 1950 (zlib), 1951 (deflate), and 1952 (gzip)
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 * LSB 4.1 has gzip, gunzip, and zcat
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 * TODO: zip -d DIR -x LIST -list -quiet -no overwrite -overwrite -p to stdout
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 //zip unzip gzip gunzip zcat
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 config COMPRESS
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 bool "compress"
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 default n
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 usage: compress [-zglrcd9] [FILE]
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
79
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 Compress or decompress file (or stdin) using "deflate" algorithm.
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 */
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
116
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 #define FOR_compress
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 #include "toys.h"
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
119
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 char lenbits[29], distbits[30];
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 )
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
138
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 // little endian bit buffer
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 struct bitbuf {
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
141 int fd, bitpos, len, max;
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 char buf[];
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 };
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
144
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
145 // malloc a struct bitbuf
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
146 struct bitbuf *bitbuf_init(int fd, int size)
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
149
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 bb->max = size;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 bb->fd = fd;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
152
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 return bb;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
155
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
156 // Advance bitpos without the overhead of recording bits
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
157 void bitbuf_skip(struct bitbuf *bb, int bits)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
158 {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
159 int pos = bb->bitpos + bits, len = bb->len << 3;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
160
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
161 while (pos >= len) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
162 pos -= len;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
163 len = (bb->len = read(bb->fd, bb->buf, bb->max)) << 3;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
164 if (bb->len < 1) perror_exit("inflate EOF");
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
165 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
166 bb->bitpos = pos;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
167 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
168
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
169 // Optimized single bit inlined version
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
170 static inline int bitbuf_bit(struct bitbuf *bb)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
171 {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
172 int bufpos = bb->bitpos>>3;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
173
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
174 if (bufpos == bb->len) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
175 bitbuf_skip(bb, 0);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
176 bufpos = 0;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
177 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
178
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
179 return (bb->buf[bufpos]>>(bb->bitpos++&7))&1;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
180 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
181
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 int result = 0, offset = 0;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
186
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 while (bits) {
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
188 int click = bb->bitpos >> 3, blow, blen;
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
189
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
192
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 // grab bits from next byte
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
194 blow = bb->bitpos & 7;
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 blen = 8-blow;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 if (blen > bits) blen = bits;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 result |= ((bb->buf[click] >> blow) & ((1<<blen)-1)) << offset;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 offset += blen;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 bits -= blen;
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
200 bb->bitpos += blen;
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
202
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 return result;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
243 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
244
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
245 // Huffman coding uses bits to traverse a binary tree to a leaf node,
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
246 // By placing frequently occurring symbols at shorter paths, frequently
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
247 // used symbols may be represented in fewer bits than uncommon symbols.
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
248
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
249 struct huff {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
250 unsigned short length[16];
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
251 unsigned short symbol[288];
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
252 };
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
253
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
254 // Create simple huffman tree from array of bit lengths.
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
257 // of the code to reach them, then by symbol number). This means that given
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
258 // the bit length of each symbol, we can construct a unique tree.
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
259 static void len2huff(struct huff *huff, char bitlen[], int len)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
260 {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
263
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
264 // Count number of codes at each bit length
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
265 memset(huff, 0, sizeof(struct huff));
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
266 for (i = 0; i<len; i++) huff->length[bitlen[i]]++;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
267
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
268 // Sort symbols by bit length. (They'll remain sorted by symbol within that.)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
272 for (i = 0; i<len; i++) if (bitlen[i]) huff->symbol[offset[bitlen[i]]++] = i;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
273 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
274
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
279 static unsigned huff_and_puff(struct bitbuf *bb, struct huff *huff)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
280 {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
281 unsigned short *length = huff->length;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
282 int start = 0, offset = 0;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
283
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
284 // Traverse through the bit lengths until our code is in this range
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
285 for (;;) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
286 offset = (offset << 1) | bitbuf_bit(bb);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
287 start += *++length;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
290 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
291
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
292 return huff->symbol[start + offset];
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
293 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
296 static void inflate(struct bitbuf *bb)
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
299 // repeat until spanked
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
300 for (;;) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
301 int final, type;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
302
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
303 final = bitbuf_get(bb, 1);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
304 type = bitbuf_get(bb, 2);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
305
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
306 if (type == 3) error_exit("bad type");
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
307
1204
a0f08d393def Update inflate code: fixed tables, bugfixes, zcat alias.
Rob Landley <rob@landley.net>
parents: 1201
diff changeset
308 // Uncompressed block?
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
309 if (!type) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
310 int len, nlen;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
311
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
314 len = bitbuf_get(bb, 16);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
315 nlen = bitbuf_get(bb, 16);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
316 if (len != (0xffff & ~nlen)) error_exit("bad len");
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
319 while (len) {
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
328 len -= bblen;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
329 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
330
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
331 // Compressed block
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
334
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
335 // Dynamic huffman codes?
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
339 char *hufflen_order = "\x10\x11\x12\0\x08\x07\x09\x06\x0a\x05\x0b"
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
340 "\x04\x0c\x03\x0d\x02\x0e\x01\x0f", *bits;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
341
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
345 hufflen = bitbuf_get(bb, 4)+4; // max 19
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
346
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
347 // The literal and distance codes are themselves compressed, in
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
348 // a complicated way: an array of bit lengths (hufflen many
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
349 // entries, each 3 bits) is used to fill out an array of 19 entries
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
350 // in a magic order, leaving the rest 0. Then make a tree out of it:
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
351 memset(bits = toybuf+1, 0, 19);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
354
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
358
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
375 // Static huffman codes
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
402 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
403 }
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
465
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 // Calculate lenbits, lenbase, distbits, distbase
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 *TT.lenbase = 3;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 for (i = 0; i<sizeof(TT.lenbits)-1; i++) {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
469 if (i>4) {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 if (!(i&3)) {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
471 TT.lenbits[i]++;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
472 n <<= 1;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
473 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
474 if (i == 27) n--;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 else TT.lenbits[i+1] = TT.lenbits[i];
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 TT.lenbase[i+1] = n + TT.lenbase[i];
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
478 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
479 n = 0;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 for (i = 0; i<sizeof(TT.distbits); i++) {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
481 TT.distbase[i] = 1<<n;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
482 if (i) TT.distbase[i] += TT.distbase[i-1];
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
483 if (i>3 && !(i&1)) n++;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 TT.distbits[i] = n;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
493
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
494 // Return true/false whether we consumed a gzip header.
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 static int is_gzip(struct bitbuf *bb)
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 {
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 int flags;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
498
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
499 // Confirm signature
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
500 if (bitbuf_get(bb, 24) != 0x088b1f || (flags = bitbuf_get(bb, 8)) > 31)
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
501 return 0;
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
502 bitbuf_skip(bb, 6*8);
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
503
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
504 // Skip extra, name, comment, header CRC fields
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
505 if (flags & 4) bitbuf_skip(bb, 16);
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
506 if (flags & 8) while (bitbuf_get(bb, 8));
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
507 if (flags & 16) while (bitbuf_get(bb, 8));
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
508 if (flags & 2) bitbuf_skip(bb, 16);
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
509
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
510 return 1;
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
511 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
553 {
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
554 struct bitbuf *bb = bitbuf_init(fd, sizeof(toybuf));
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
555
1200
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
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
de10c8142fce Not buying it, eh?
Rob Landley <rob@landley.net>
parents: 1199
diff changeset
563 inflate(bb);
1199
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
564
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
565 // tail: crc32, len32
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
570 free(bb);
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
571 }
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
572
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
573 // Parse many different kinds of command line argument:
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
574
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
575 void compress_main(void)
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
ff8d73e78089 Nothing to see here, move along.
Rob Landley <rob@landley.net>
parents:
diff changeset
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 }