summaryrefslogtreecommitdiff
path: root/lib/flate/flate.myr
blob: 4b7900062cf56b3cadd9b42817cb63abf3cc4cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use std
use bio
use "types"

/* DEFLATE  https://www.ietf.org/rfc/rfc1951.txt

   Terminology:
     - A 'Huffman code' is one mechanism to map codes to symbols.
     - A 'code' is the sequence of bits used to encode a symbol
       in a Huffman code.  Codes are what appears in the DEFLATE
       stream.
     - A 'symbol' is what a code decodes to.
     - A 'length' is the length of an LZ77 reference (the number
       of bytes to repeat).
     - A 'distance' is the distance at which the bits referred to
       by an LZ77 reference are from the current point.
*/

pkg flate =
	/* state maintained during decompression */
	type flatedec = struct
		rdf : bio.file#
		wrf : bio.file#

		/* one-byte buffer */
		bval : int /* in [0, 255] current input byte */
		bpos : int /* in [0, 8] */

		/* ring buffer window for the bytes output */
		wpos : std.size
		wlen : std.size
		wbuf : byte[WinSz]

		/* Huffman codes */
		lenhc : hufc /* for lengths */
		dsthc : hufc /* for distances */
	;;

	/* one Huffman code */
	type hufc = struct
		/* # of codes of length [1, MaxBits] */
		count : int[MaxBits + 1]
		/* symbols sorted lexicographically by
		   <code length, symbol value>; c.f., mkhufc()
		   and readcode() for usage */
		symbol : int[MaxSyms]
	;;

	/* decode a DEFLATE stream */
	const decode : (outf : bio.file#, inf : bio.file# -> std.result(void, err))

	pkglocal const MaxSyms : std.size
	pkglocal const MaxBits : std.size
	pkglocal const WinSz : std.size
	pkglocal const bits : (st : flatedec#, n : int -> std.result(int, err))
	pkglocal const nocomp : (st : flatedec# -> std.result(void, err))
	pkglocal const mkhufc : (lengths : int[:], hc : hufc# -> void)
	pkglocal const readcode : (st : flatedec#, hc : hufc# -> std.result(int, err))
	pkglocal const outb : (st : flatedec#, b : byte -> std.result(void, err))
;;

const MaxLenSyms : std.size = 288 /* max # of length symbols */
const MaxDstSyms : std.size = 32 /* max # of distance symbols */
const MaxSyms = MaxLenSyms /* max # of symbols in a Huffman code */
const MaxBits = 15 /* max length of a code (in bits) */

const WinSz = 32 * std.KiB /* max distance for a reference */

var fixedlenhc : hufc
var fixeddsthc : hufc

const __init__ = {
	/* the fixed Huffman codes; c.f., RFC Section 3.2.6 */
	var length : int[MaxSyms]

	std.slfill(length[0:144], 8)
	std.slfill(length[144:256], 9)
	std.slfill(length[256:280], 7)
	std.slfill(length[280:288], 8)
	mkhufc(length[:], &fixedlenhc)

	std.slfill(length[0:32], 5)
	mkhufc(length[0:32], &fixeddsthc)
}

const outb = {st, b
	/* outputs a single byte, to the output file; additionally
	   record the byte emitted in the output window */
	var pos

	match bio.putb(st.wrf, b)
	| `std.Ok 0: std.die("no bytes written by bio.putb()")
	| `std.Ok _:
	| `std.Err e: -> `std.Err (`Io e)
	;;
	pos = st.wpos
	if pos == WinSz
		std.assert(st.wlen >= WinSz, "window should be full")
		pos = 0
	;;
	st.wbuf[pos] = b
	st.wpos = pos + 1
	st.wlen++ /* don't care about it being > WinSz */
	-> `std.Ok void
}

const bits = {st, n
	var pos, len, bits, bval

	std.assert(n >= 0 && n <= 31, "invalid argument")
	pos = st.bpos
	bits = st.bval >> pos
	for len = 8 - pos; len < n; len += 8
		match bio.getb(st.rdf)
		| `std.Ok b:
			bval = (b : int)
			st.bval = bval
			bits += bval << len
		| `std.Err e: -> `std.Err (`Io e)
		;;
	;;
	st.bpos = 8 - (len - n)
	-> `std.Ok (bits & ((1 << n) - 1))
}

const nocomp = {st
	/* process a non-compressed block; c.f., RFC Section 3.2.4 */
	var len

	st.bpos = 8 /* skip to a byte boundary */
	match bits(st, 16)
	| `std.Ok n: len = (n : std.size)
	| `std.Err e: -> `std.Err e
	;;
	match bits(st, 16)
	| `std.Ok nlen:
		if len != (nlen : std.size) ^ 0xffff
			-> `std.Err (`Format \
				"non-compressed block length could " \
				"not be verified")
		;;
	| `std.Err e: -> `std.Err e
	;;
	for var n = 0; n < len; n++
		match bio.getb(st.rdf)
		| `std.Ok b:
			match outb(st, b)
			| `std.Ok _:
			| err: -> err
			;;
		| `std.Err e: -> `std.Err (`Io e)
		;;
	;;
	-> `std.Ok void

}

const mkhufc = {length, hc
	/* note: it is possible to have 0s in the length array; this means
	   that the symbol at this index will never be part of the input
	   stream */
	var index : int[MaxBits + 1]

	std.assert(length.len <= hc.symbol.len, "invalid argument")
	std.slfill(hc.count[:], 0)
	for var symb = 0; symb < length.len; symb++
		hc.count[length[symb]]++
	;;
	std.slfill(index[:], 0)
	hc.count[0] = 0
	for var l = 1; l <= MaxBits; l++
		index[l] = index[l - 1] + hc.count[l - 1]
	;;
	for var symb = 0; symb < length.len; symb++
		if length[symb] != 0
			hc.symbol[index[length[symb]]++] = symb
		;;
	;;
	/* TODO: validate the Huffman code */
}

const readcode = {st, hc
	/* if you want to know more about the algorithm here, you
	   must look for "canonical Huffman codes"; see, for example,
	     Managing Gigabytes: Compressing and Indexing Documents
	       and Images
	     by I. H. Witten, A. Moffat, and T. C. Bell. */
	var code, first, index, count

	code = 0 /* code seen so far */
	first = 0 /* first code of length len */
	index = 0 /* index of the first code of length len in hc.symbol[:] */
	for var len = 1; len <= MaxBits; len++
		match bits(st, 1)
		| `std.Ok n: code += n
		| err: -> err
		;;
		count = hc.count[len]
		if code < first + count
			/* the code is exactly len bits long;
			   return the symbol from the table */
			-> `std.Ok (hc.symbol[index + (code - first)])
		;;
		index += count
		first = (first + count) << 1 /* c.f. RFC Section 3.2.2 */
		code <<= 1 /* make room for the next bit */
	;;
	-> `std.Err (`Format "invalid code")
}

const decomp = {st
	/* process data of a compressed block; c.f., RFC Section 3.2.5 */
	const basel = [
		3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
		35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
	]
	const extral = [
		0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3,
		3, 4, 4, 4, 4, 5, 5, 5, 5, 0
	]
	const based = [
		1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
		257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
		8193, 12289, 16385, 24577
	]
	const extrad = [
		0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
		8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
	]
	var symb, len, dst, index

	symb = 0
	while symb != 256
		match readcode(st, &st.lenhc)
		| `std.Ok s: symb = s
		| `std.Err e: -> `std.Err e
		;;
		if symb < 256
			match outb(st, (symb : byte))
			| `std.Ok _:
			| err: -> err
			;;
		elif symb > 256
			symb -= 257
			match bits(st, extral[symb])
			| `std.Ok x: len = basel[symb] + x
			| `std.Err e: -> `std.Err e
			;;
			match readcode(st, &st.dsthc)
			| `std.Ok s: symb = s
			| `std.Err e: -> `std.Err e
			;;
			match bits(st, extrad[symb])
			| `std.Ok x: dst = (based[symb] + x : std.size)
			| `std.Err e: -> `std.Err e
			;;
			if dst > st.wlen
				-> `std.Err (`Format \
					"reference to a byte before file start")
			;;
			index = (WinSz + st.wpos - dst) % WinSz
			for ; len > 0; len--
				match outb(st, st.wbuf[index])
				| `std.Ok _:
				| err: -> err
				;;
				index = (index + 1) % WinSz
			;;
		;;
	;;
	-> `std.Ok void
}

const dynamic = {st
	/* process a block compressed with dynamic Huffman codes;
	   c.f., RFC Section 3.2.7 */
	const coden = [
		16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2,
		14, 1, 15
	]
	const extra = [2, 3, 7]
	var length : int[MaxLenSyms + MaxDstSyms]
	var nlen, ndst, ncode, symb, nbits, hufc

	match bits(st, 5)
	| `std.Ok n: nlen = n + 257
	| `std.Err e: -> `std.Err e
	;;
	if nlen > 286
		-> `std.Err (`Format "invalid number of length codes")
	;;
	match bits(st, 5)
	| `std.Ok n: ndst = n + 1
	| `std.Err e: -> `std.Err e
	;;
	if ndst > 30
		-> `std.Err (`Format "invalid number of distance codes")
	;;
	match bits(st, 4)
	| `std.Ok n: ncode = n + 4
	| `std.Err e: -> `std.Err e
	;;
	std.slfill(length[:19], 0)
	for var c = 0; c < ncode; c++
		match bits(st, 3)
		| `std.Ok n: length[coden[c]] = n
		| `std.Err e: -> `std.Err e
		;;
	;;
	mkhufc(length[:19], &hufc)
	for var c = 0; c < nlen + ndst;
		/* according to the RFC: "the code length repeat codes
		   can cross from HLIT + 257 to the HDIST + 1 code
		   lengths"; this is why we have a single loop here */
		match readcode(st, &hufc)
		| `std.Ok n: symb = n
		| `std.Err e: -> `std.Err e
		;;
		if symb > 18
			-> `std.Err (`Format "invalid code code")
		;;
		if symb < 16
			length[c++] = symb
		else
			/* repeat some code length */
			match bits(st, extra[symb - 16])
			| `std.Ok n: nbits = n
			| `std.Err e: -> `std.Err e
			;;
			if symb == 16
				/* copy the previous code length 3-6 times */
				nbits += 3
				if c == 0 || c + nbits > nlen + ndst
					-> `std.Err (`Format \
						"invalid code repetition")
				;;
				std.slfill(length[c:c+nbits], length[c-1])
			else
				/* repeat a 0 length */
				if symb == 17
					nbits += 3
				else
					nbits += 11
				;;
				if c + nbits > nlen + ndst
					-> `std.Err (`Format \
						"invalid zero repetition")
				;;
				std.slfill(length[c:c+nbits], 0)
			;;
			c += nbits
		;;
	;;
	if length[256] == 0
		-> `std.Err (`Format "no code length of end-of-block symbol")
	;;
	mkhufc(length[:nlen], &st.lenhc)
	mkhufc(length[nlen:nlen + ndst], &st.dsthc)
	-> decomp(st)
}

const decode = {outf, inf
	var st, err, last

	st = [
		.rdf = inf,
		.wrf = outf,
		.bpos = 8,
	]
	last = 0
	while last != 1
		match bits(&st, 1)
		| `std.Ok l: last = l
		| `std.Err e: -> `std.Err e
		;;
		match bits(&st, 2)
		| `std.Ok 0:
			err = nocomp(&st)
		| `std.Ok 1:
			st.lenhc = fixedlenhc
			st.dsthc = fixeddsthc
			err = decomp(&st)
		| `std.Ok 2:
			err = dynamic(&st)
		| `std.Ok _:
			-> `std.Err (`Format "invalid block type")
		| `std.Err e:
			-> `std.Err e
		;;
		match err
		| `std.Ok _:
		| `std.Err e: -> `std.Err e
		;;
	;;
	-> `std.Ok void
}