-
Notifications
You must be signed in to change notification settings - Fork 12
/
brotli-02-edit.nroff
5698 lines (5397 loc) · 338 KB
/
brotli-02-edit.nroff
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.pl 10.0i
.po 0
.ll 7.2i
.lt 7.2i
.nr LL 7.2i
.nr LT 7.2i
.ds LF Alakuijala & Szabadka
.ds RF FORMFEED[Page %]
.ds LH Internet-Draft
.ds RH April 2015
.ds CH Brotli
.ds CF Expires April 27, 2015
.hy 0
.nh
.ad l
.in 0
.nf
.tl 'Network Working Group''J. Alakuijala'
.tl 'Internet-Draft''Z. Szabadka'
.tl 'Intended Status: Informational''Google, Inc'
.tl 'Expires: April 27, 2015''April 2015'
.fi
.ce 2
Brotli Compressed Data Format
draft-alakuijala-brotli-02-edit
.fi
.in 3
.ti 0
Abstract
This specification defines a lossless compressed data format that
compresses data using a combination of the LZ77 algorithm and Huffman
coding, with efficiency comparable to the best currently available
general-purpose compression methods.
.ti 0
Status of this Memo
This Internet-Draft is submitted in full conformance with the
provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering
Task Force (IETF). Note that other groups may also distribute
working documents as Internet-Drafts. The list of current Internet-
Drafts is at http://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months
and may be updated, replaced, or obsoleted by other documents at any
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."
This Internet-Draft will expire on April 27, 2015.
.ti 0
Copyright Notice
Copyright (c) 2014, 2015 IETF Trust and the persons identified as the
document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal
Provisions Relating to IETF Documents
(http://trustee.ietf.org/license-info) in effect on the date of
publication of this document. Please review these documents
carefully, as they describe your rights and restrictions with respect
to this document. Code Components extracted from this document must
include Simplified BSD License text as described in Section 4.e of
the Trust Legal Provisions and are provided without warranty as
described in the Simplified BSD License.
.ti 0
Table of Contents
.in 0
.nf
INSERT_TOC_HERE
.fi
.in 3
.bp
.ti 0
1. Introduction
.ti 0
1.1. Purpose
The purpose of this specification is to define a lossless
compressed data format that:
.nf
* Is independent of CPU type, operating system, file system,
and character set, and hence can be used for interchange;
* Can be produced or consumed, even for an arbitrarily long
sequentially presented input data stream, using only an a
priori bounded amount of intermediate storage, and hence
can be used in data communications or similar structures,
such as Unix filters;
* Compresses data with a compression ratio comparable to the
best currently available general-purpose compression methods,
and in particular considerably better than the gzip program;
* Decompresses much faster than current LZMA implementations.
.fi
The data format defined by this specification does not attempt to:
.nf
* Allow random access to compressed data;
* Compress specialized data (e.g., raster graphics) as well
as the best currently available specialized algorithms.
.fi
.ti 0
1.2. Intended audience
This specification is intended for use by software implementers
to compress data into and/or decompress data from the brotli format.
The text of the specification assumes a basic background in
programming at the level of bits and other primitive data
representations. Familiarity with the technique of Huffman coding
is helpful but not required.
This specification uses heavily the notations and terminology
introduced in the DEFLATE format specification [RFC 1951].
For the sake of completeness, we always
include the whole text of the relevant parts of RFC 1951,
therefore familiarity with the DEFLATE format is helpful but not
required.
The compressed data format defined in this specification is
an integral part of the WOFF 2.0 web font file format [WOFF2],
therefore this specification is also intended for implementers of
WOFF 2.0 compressors and decompressors.
.ti 0
1.3. Scope
The specification specifies a method for representing a sequence
of bytes as a (usually shorter) sequence of bits, and a method for
packing the latter bit sequence into bytes.
.ti 0
1.4. Compliance
Unless otherwise indicated below, a compliant decompressor must be
able to accept and decompress any data set that conforms to all
the specifications presented here. A compliant compressor must
produce data sets that conform to all the specifications presented
here.
.ti 0
1.5. Definitions of terms and conventions used
Byte: 8 bits stored or transmitted as a unit (same as an octet).
For this specification, a byte is exactly 8 bits, even on machines
which store a character on a number of bits different from eight.
See below for the numbering of bits within a byte.
String: a sequence of arbitrary bytes.
Bytes stored within a computer do not have a "bit order", since
they are always treated as a unit. However, a byte considered as
an integer between 0 and 255 does have a most- and least-
significant bit, and since we write numbers with the most-
significant digit on the left, we also write bytes with the most-
significant bit on the left. In the diagrams below, we number the
bits of a byte so that bit 0 is the least-significant bit, i.e.,
the bits are numbered:
.nf
+--------+
|76543210|
+--------+
.fi
Within a computer, a number may occupy multiple bytes. All
multi-byte numbers in the format described here are stored with
the least-significant byte first (at the lower memory address).
For example, the decimal number 520 is stored as:
.nf
0 1
+--------+--------+
|00001000|00000010|
+--------+--------+
^ ^
| |
| + more significant byte = 2 x 256
+ less significant byte = 8
.fi
.ti 0
1.5.1. Packing into bytes
This document does not address the issue of the order in which
bits of a byte are transmitted on a bit-sequential medium,
since the final data format described here is byte- rather than
bit-oriented. However, we describe the compressed block format
below as a sequence of data elements of various bit
lengths, not a sequence of bytes. We must therefore specify
how to pack these data elements into bytes to form the final
compressed byte sequence:
.nf
* Data elements are packed into bytes in order of
increasing bit number within the byte, i.e., starting
with the least-significant bit of the byte.
* Data elements other than prefix codes are packed
starting with the least-significant bit of the data
element. These are referred to here as integer values
and are considered unsigned.
* Prefix codes are packed starting with the most-
significant bit of the code.
.fi
In other words, if one were to print out the compressed data as
a sequence of bytes, starting with the first byte at the
*right* margin and proceeding to the *left*, with the most-
significant bit of each byte on the left as usual, one would be
able to parse the result from right to left, with fixed-width
elements in the correct MSB-to-LSB order and prefix codes in
bit-reversed order (i.e., with the first bit of the code in the
relative LSB position).
.ti 0
2. Compressed representation overview
A compressed data set consists of a header and a series of meta-
blocks. Each meta-block decompresses to a sequence of 1
to 268,435,456 (256 MiB) uncompressed bytes. The final uncompressed data is
the concatenation of the uncompressed sequences from each meta-block.
The header contains the size of the sliding window that was used during compression.
The decompressor must retain at least that amount of uncompressed data prior to the
current position in the stream, in order to be able to decompress
what follows. The sliding window size is a power of two, minus 16, where
the power is in the range of 16 to 24. The possible sliding window
sizes range from 64 KiB - 16 B to 16 MiB - 16 B.
Each meta-block is compressed using a combination of the LZ77
algorithm (Lempel-Ziv 1977, [LZ77]) and Huffman coding. The
result of Huffman coding is referred to here as a prefix code.
The prefix codes for each meta-block are independent of
those for previous or subsequent meta-blocks; the LZ77 algorithm may
use a reference to a duplicated string occurring in a previous
meta-block, up to the sliding window size of uncompressed bytes before.
In addition, in the brotli format, a string reference may instead refer
to a static dictionary entry.
Each meta-block consists of two parts: a meta-block header that
describes the representation of the compressed data part, and a
compressed data part. The compressed data consists of a series of
commands. Each command consists of two parts: a sequence of literal
bytes (of strings that have not been detected as duplicated within
the sliding window), and a pointer to a duplicated string,
represented as a pair <length, backward distance>. There can be
zero literal bytes in the command. The minimum length of the string to be
duplicated is two, but the last command in the meta-block is permitted to have
only literals and no pointer to a string to duplicate.
Each command in the compressed data is represented using three categories
of prefix codes: one set of prefix codes are for the literal sequence
lengths (also referred to as literal insertion lengths) and backward
copy lengths (that is, a single code word represents two lengths,
one of the literal sequence and one of the backward copy), a separate
set of prefix codes are for literals, and a third set of prefix codes are for
distances. The prefix code descriptions for each meta-block appear in a compact
form just before the compressed data in the meta-block header. The insert and
copy length and distance prefix codes may be followed by extra bits that are
added to the base values determined by the codes. The number of extra bits is
determined by the code.
One meta-block command then appears as a sequence of prefix codes:
Insert and copy length, literal, literal, ..., literal, distance
where the insert and copy defines the number of literals that immediately
follow and the copy length, and the distance defines how far back to go
for the copy, used in combination with the copy length. The resulting
uncompressed data is the sequence of bytes:
literal, literal, ..., literal, copy, copy, ..., copy
where the number of literal bytes and copy bytes are determined by the
insert and copy length code. (The number of bytes copied for a static
dictionary entry can vary from the copy length.)
The last command in the meta-block may end with the last literal if the
total uncompressed length of the meta-block has been satisfied. In
that case there is no distance in the last command, and the copy length is
ignored.
There can be more than one prefix code for each category, where the
prefix code to use for the next element of that category is determined
by the context of the compressed stream that precedes that element.
Part of that context is three current block types, one for each
category. A block type is in the range of 0..255. For each category
there is a count of how many elements of that category remain to be
decoded using the current block type. Once that count is expended,
a new block type and block count is read from the stream immediately
preceding the next element of that category, which will use the new
block type.
The insert and copy block type directly determines which prefix code to
use for the next insert and copy element. For the literal and distance
elements, the respective block type is used in combination with other
context information to determine which prefix code to use for the next
element.
Consider the following
example:
(IaC0, L0, L1, L2, D0)(IaC1, D1)(IaC2, L3, L4, D2)(IaC3, L5, D3)
The meta-block here has four commands, contained in parentheses for clarity,
where each of the three categories of
symbols within these commands can be interpreted using different block types.
Here we separate out each category as its own sequence to show an example of block
types assigned to those elements. Each square-bracketed group is a block that
uses the same block type:
[IaC0, IaC1][IaC2, IaC3] <-- insert-and-copy: block types 0 and 1
[L0, L1][L2, L3, L4][L5] <-- literals: block types 0, 1, and 0
[D0][D1, D2, D3] <-- distances: block types 0 and 1
The subsequent blocks within each block category must have different
block types, but we see that block types can be reused later in the meta-block.
The block types are numbered from 0 to the maximum
block type number of 255 and the first block of each block category
is type 0. The block structure of a meta-block is represented
by the sequence of block-switch commands for each block category,
where a block-switch command is a pair <block type, block count>.
The block-switch commands are represented in the compressed data
before the start of each new block using a prefix code for
block types and a separate prefix code for block counts for
each block category. For the above example the physical layout of the
meta-block is then:
IaC0 L0 L1 LBlockSwitch(1, 3) L2 D0 IaC1 DBlockSwitch(1, 3) D1
IaCBlockSwitch(1, 2) IaC2 L3 L4 D2 IaC3 LBlockSwitch(0, 1) L5 D3
where *BlockSwitch(t, n) switches to block type t for a count of n elements.
Note that in this example DBlockSwitch(1, 3) immediately precedes the
next required distance D1. It does not follow the last distance of
the previous block, D0. Whenever an element of a category is needed,
and the block count for that category has reached zero, then a new
block type and count is read from the stream just before reading that next
element.
The block switch commands for the first blocks of each category are not part
of the meta-block compressed data. Instead the first block type
is defined to be 0, and the first block count for each category is
encoded in the meta-block header. The prefix codes for the block types and counts, a total of
six prefix codes over the three categories, are defined in a compact form in the meta-block
header.
Each category of value (insert-and-copy lengths, literals and distances)
can be encoded with any prefix code from a collection of prefix
codes belonging to the same category appearing in the meta-block header. The
particular prefix code used can depend on two factors: the block
type of the block the value appears in, and the context of the value.
In the case of the literals, the context is the previous two bytes in
the uncompressed data, and in the case of distances, the context is the copy
length from the same command. For insert-and-copy lengths, no context
is used and the prefix code depends only on the block type. In the case
of literals and distances, the context is mapped to a context ID in
the range 0..63 for literals and 0..3 for distances and the matrix
of the prefix code indices for each block type and context ID,
called the context map, is encoded in a compact form in the meta-
block header.
For example, the prefix code to use to decode L2 depends on the
block type (1), the literal context ID for block type 1 defined
in the meta-block header,
and the two uncompressed bytes that were decoded from L0 and L1.
Similarly, the prefix code to use to decode D0 depends on the block
type (0), the distance context ID for block type 0, and the copy
length decoded from IaC0. The prefix code to use to decode IaC3
depends only on the block type (1).
In addition to the parts listed above (prefix code for insert-
and-copy lengths, literals, distances, block types and block counts
and the context map), the meta-block header contains the number of
uncompressed bytes coded in the meta-block and two additional parameters used in
the representation of match distances: the number of postfix bits and
the number of direct distance codes.
A compressed meta-block may be marked in the header as the last meta-block,
which terminates the compressed stream.
A meta-block may instead simply store the uncompressed data directly as
bytes on byte boundaries with no coding or matching strings. In this
case the meta-block header information only contains the number of
uncompressed bytes and the indication that the meta-block is uncompressed.
An uncompressed meta-block cannot be the last meta-block.
A meta-block may also be empty, which generates no uncompressed data at all.
An empty block can only be the last block, which can be used to mark the end of a
stream whose last productive meta-block was an uncompressed block.
.ti 0
3. Compressed representation of prefix codes
.ti 0
3.1. Introduction to prefix coding
Prefix coding represents symbols from an a priori known alphabet
by bit sequences (codes), one code for each symbol, in a manner
such that different symbols may be represented by bit sequences of
different lengths, but a parser can always parse an encoded string
unambiguously symbol-by-symbol.
We define a prefix code in terms of a binary tree in which the two
edges descending from each non-leaf node are labeled 0 and 1 and
in which the leaf nodes correspond one-for-one with (are labeled
with) the symbols of the alphabet; then the code for a symbol is
the sequence of 0's and 1's on the edges leading from the root to
the leaf labeled with that symbol. For example:
.nf
.KS
/\\ Symbol Code
0 1 ------ ----
/ \\ A 00
/\\ B B 1
0 1 C 011
/ \\ D 010
A /\\
0 1
/ \\
D C
.KE
.fi
A parser can decode the next symbol from the compressed stream
by walking down the tree from the root, at each step choosing the
edge corresponding to the next compressed data bit.
Given an alphabet with known symbol frequencies, the Huffman
algorithm allows the construction of an optimal prefix code (one
which represents strings with those symbol frequencies using the
fewest bits of any possible prefix codes for that alphabet). Such
a prefix code is called a Huffman code. (See [HUFFMAN] in Chapter 5,
references for additional information on Huffman codes.)
Note that in the brotli format, the prefix codes for the
various alphabets must not exceed certain maximum code lengths.
This constraint complicates the algorithm for computing code
lengths from symbol frequencies. Again, see Chapter 5, references
for details.
.ti 0
3.2. Use of prefix coding in the brotli format
The prefix codes used for each alphabet in the brotli format
are canonical prefix codes, which have two additional rules:
.nf
* All codes of a given bit length have lexicographically
consecutive values, in the same order as the symbols they
represent;
* Shorter codes lexicographically precede longer codes.
.fi
We could recode the example above to follow this rule as follows,
assuming that the order of the alphabet is ABCD:
.nf
.KS
Symbol Code
------ ----
A 10
B 0
C 110
D 111
.KE
.fi
I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are
lexicographically consecutive.
Given this rule, we can define the canonical prefix code for an
alphabet just by giving the bit lengths of the codes for each
symbol of the alphabet in order; this is sufficient to determine
the actual codes. In our example, the code is completely defined
by the sequence of bit lengths (2, 1, 3, 3). The following
algorithm generates the codes as integers, intended to be read
from most- to least-significant bit. The code lengths are
initially in tree[I].Len; the codes are produced in tree[I].Code.
.nf
1) Count the number of codes for each code length. Let
bl_count[N] be the number of codes of length N, N >= 1.
2) Find the numerical value of the smallest code for each
code length:
.KS
code = 0;
bl_count[0] = 0;
for (bits = 1; bits <= MAX_BITS; bits++) {
code = (code + bl_count[bits-1]) << 1;
next_code[bits] = code;
}
.KE
3) Assign numerical values to all codes, using consecutive
values for all codes of the same length with the base
values determined at step 2. Codes that are never used
(which have a bit length of zero) must not be assigned a
value.
.KS
for (n = 0; n <= max_code; n++) {
len = tree[n].Len;
if (len != 0) {
tree[n].Code = next_code[len];
next_code[len]++;
}
}
.KE
.fi
Example:
Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, 3,
2, 4, 4). After step 1, we have:
.nf
.KS
N bl_count[N]
- -----------
2 1
3 5
4 2
.KE
.fi
Step 2 computes the following next_code values:
.nf
.KS
N next_code[N]
- ------------
1 0
2 0
3 2
4 14
.KE
.fi
Step 3 produces the following code values:
.nf
.KS
Symbol Length Code
------ ------ ----
A 3 010
B 3 011
C 3 100
D 3 101
E 3 110
F 2 00
G 4 1110
H 4 1111
.KE
.fi
.ti 0
3.3. Alphabet sizes
Prefix codes are used for different purposes in the brotli
format, and each purpose has a different alphabet size. For
literal codes the alphabet size is 256. For insert-and-copy
length codes the alphabet size is 704. For block count codes,
the alphabet size is 26. For distance codes, block type codes and
the prefix codes used in compressing the context map, the
alphabet size is dynamic and is based on other parameters.
.ti 0
3.4. Simple prefix codes
The first two bits of the compressed representation of each
prefix code distinguishes between simple and complex prefix
codes. If this value is 1, then a simple prefix code follows.
Otherwise the value indicates the number of leading zeros.
A simple prefix code can have only up to four symbols with non-
zero code length. The format of the simple prefix code is as
follows:
.nf
2 bits: value of 1 indicates a simple prefix code
2 bits: NSYM - 1, where NSYM = # of symbols coded
NSYM symbols, each encoded using ALPHABET_BITS bits
1 bit: tree-select, present only for NSYM = 4
.fi
The value of ALPHABET_BITS depends on the alphabet of the prefix
code: it is the smallest number of bits that can represent all
symbols in the alphabet. E.g. for the alphabet of literal bytes,
ALPHABET_BITS is 8. The value of each of the NSYM symbols above is
the value of the ALPHABETS_BITS width integer value. (If the integer
value is greater than or equal to the alphabet size, then the stream
should be rejected as invalid.)
Note that the NSYM symbols may not be presented in sorted order. Prefix codes
of the same bit length must be assigned to the symbols in sorted order.
The (non-zero) code lengths of the symbols can be reconstructed as
follows:
.nf
* if NSYM = 1, the code length for the one symbol is zero --
when encoding this symbol in the
compressed data stream using this prefix code, no
actual bits are emitted. Similarly, when decoding a symbol
using this prefix code, no bits are read and the one symbol
is returned.
* if NSYM = 2, both symbols have code length 1.
* if NSYM = 3, the code lengths for the symbols are 1, 2, 2 in
the order they appear in the representation of the simple
prefix code.
* if NSYM = 4, the code lengths (in order of symbols decoded)
depend on the tree-select bit: 2, 2, 2, 2, (tree-select bit 0)
or 1, 2, 3, 3 (tree-select bit 1).
.fi
.ti 0
3.5. Complex prefix codes
A complex prefix code is a canonical prefix code, defined by the
sequence of code lengths, as discussed in Paragraph 3.2, above.
For even greater compactness, the code length sequences themselves
are compressed using a prefix code. The alphabet for code lengths
is as follows:
.nf
0 - 15: Represent code lengths of 0 - 15
16: Copy the previous non-zero code length 3 - 6 times
The next 2 bits indicate repeat length
(0 = 3, ... , 3 = 6)
If this is the first code length, or all previous
code lengths are zero, a code length of 8 is
repeated 3 - 6 times
A repeated code length code of 16 modifies the
repeat count of the previous one as follows:
repeat count = (4 * (repeat count - 2)) +
(3 - 6 on the next 2 bits)
Example: Codes 7, 16 (+2 bits 11), 16 (+2 bits 10)
will expand to 22 code lengths of 7
(1 + 4 * (6 - 2) + 5)
17: Repeat a code length of 0 for 3 - 10 times.
(3 bits of length)
A repeated code length code of 17 modifies the
repeat count of the previous one as follows:
repeat count = (8 * (repeat count - 2)) +
(3 - 10 on the next 3 bits)
.fi
Note that a code of 16 that follows an immediately preceding 16 modifies the
previous repeat count, which becomes the new repeat count. The same is true for
a 17 following a 17. A sequence of three or more 16 codes in a row or three of
more 17 codes in a row is possible, modifying the count each time. Only the
final repeat count is used. The modification only applies if the same code
follows. A 16 repeat does not modify an immediately preceding 17 count, nor
vice versa.
A code length of 0 indicates that the corresponding symbol in the
alphabet will not occur in the compressed data, and should not
participate in the prefix code construction algorithm given
earlier. A complex prefix code must have at least two non-zero
code lengths.
The bit lengths of the prefix code over the code length alphabet
are compressed with the following static prefix code (where the
bits shown are reversed in the actual compressed stream):
.nf
.KS
Symbol Code
------ ----
0 00
1 1110
2 110
3 01
4 10
5 1111
.KE
.fi
We can now define the format of the complex prefix code as
follows:
.nf
2 bits: HSKIP, values of 0, 2 or 3 represent the respective
number of skipped code lengths. The skipped lengths
are taken to be zero. (An HSKIP of 1 indicates a
Simple prefix code.)
Code lengths for symbols in the code length alphabet given
just above, in the order: 1, 2, 3, 4, 0, 5, 17, 6, 16, 7,
8, 9, 10, 11, 12, 13, 14, 15. If HSKIP is 2, then the
code lengths for symbols 1 and 2 are zero, and the first
code length is for symbol 3. If HSKIP is 3, then the code
length for symbol 3 is also zero, and the first code length
is for symbol 4.
The code lengths of code length symbols are between 0 and
5 and they are represented with 2 - 4 bits according to
the static prefix code above. A code length of 0 means
the corresponding code length symbol is not used.
If HSKIP is 2 or 3, a respective number of leading code
lengths are implicit zeros and are not present in the
code lengths sequence above.
If there are at least two
non-zero code lengths, any trailing zero code lengths are
omitted, i.e. the last code length in the sequence must
be non-zero. In this case the sum of (32 >> code length)
over all the non-zero code lengths must equal to 32.
If the lengths have been read for the entire code length
alphabet and there was only one non-zero code length,
then the prefix code has one symbol whose code has zero
length. In this case, that symbol results in no bits
being emitted by the compressor, and no bits consumed by
the decompressor. That single symbol is immediately
returned when this code is decoded. (If the ignored non-
zero length is not 1, then the stream should be rejected
as invalid.) An example of where this occurs is if the
entire code to be represented has symbols of length 8.
E.g. a literal code that represents all literal values
with equal probability. In this case the single symbol
is 16, which repeats the previous length. The previous
length is taken to be 8 before any code length code
lengths are read.
Sequence of code lengths symbols, encoded using the code
length prefix code. Any trailing 0 or 17 must be
omitted, i.e. the last encoded code length symbol must be
between 1 and 16. The sum of (32768 >> code length) over
all the non-zero code lengths in the alphabet, including
those encoded using repeat code(s) of 16, must equal to
32768.
.fi
.ti 0
4. Encoding of distances
As described in Section 2, one component of a compressed meta-block
is a sequence of backward distances. In this section we provide the
details to the encoding of distances.
Each distance in the compressed data part of a meta-block is
represented with a pair <distance code, extra bits>. The distance
code and the extra bits are encoded back-to-back, the distance code
is encoded using a prefix code over the distance alphabet,
while the extra bits value is encoded as a fixed-width integer
value. The number of extra bits can be 0 - 24, and it is dependent
on the distance code.
To convert a distance code and associated extra bits to a backward
distance, we need the sequence of past distances and two additional
parameters, the number of "postfix bits", denoted by NPOSTFIX (0..3), and
the number of direct distance codes, denoted by NDIRECT (0..120). Both of
these parameters are encoded in the meta-block header. We will also
use the following derived parameter:
POSTFIX_MASK = ((1 << NPOSTFIX) - 1)
The first 16 distance symbols are special symbols that reference
past distances as follows:
.nf
0: last distance
1: second-to-last distance
2: third-to-last distance
3: fourth-to-last distance
4: last distance - 1
5: last distance + 1
6: last distance - 2
7: last distance + 2
8: last distance - 3
9: last distance + 3
10: second-to-last distance - 1
11: second-to-last distance + 1
12: second-to-last distance - 2
13: second-to-last distance + 2
14: second-to-last distance - 3
15: second-to-last distance + 3
.fi
The ring buffer of four last distances is initialized by the values
16, 15, 11 and 4 (i.e. the fourth-to-last is set to 16, the third-to-last
to 15, the second-to-last to 11 and the last distance to 4) at the
beginning of the *stream* (as opposed to the beginning of the meta-
block) and it is not reset at meta-block boundaries. When a distance
symbol 0 appears, the distance it represents (i.e. the last distance
in the sequence of distances) is not pushed to the ring buffer of
last distances, in other words, the expression "(second, third,
fourth)-to-last distance" means the (second, third, fourth)-to-last
distance that was not represented by a 0 distance symbol. Similarly,
distances that represent static dictionary words (see Section 8.) are
not pushed to the ring buffer of last distances.
The next NDIRECT distance symbols, from 16 to 15 + NDIRECT, represent
distances from 1 to NDIRECT. Neither the distance special symbols, nor
the NDIRECT direct distance symbols are followed by any extra bits.
Distance symbols 16 + NDIRECT and greater all have extra bits, where the
number of extra bits for a distance symbol "dcode" is given by the
following formula:
ndistbits = 1 + ((dcode - NDIRECT - 16) >> (NPOSTFIX + 1))
The maximum number of extra bits is 24, therefore the size of the
distance symbol alphabet is (16 + NDIRECT + (48 << NPOSTFIX)).
Given a distance symbol "dcode" (>= 16 + NDIRECT), and extra bits
"dextra", the backward distance is given by the following formula:
.nf
hcode = (dcode - NDIRECT - 16) >> NPOSTFIX
lcode = (dcode - NDIRECT - 16) & POSTFIX_MASK
offset = ((2 + (hcode & 1)) << ndistbits) - 4;
distance = ((offset + dextra) << NPOSTFIX) + lcode + NDIRECT + 1
.fi
.ti 0
5. Encoding of literal insertion lengths and copy lengths
As described in Section 2, the literal insertion lengths and backward
copy lengths are encoded using a single prefix code. This section
provides the details to this encoding.
Each <insertion length, copy length> pair in the compressed data part
of a meta-block is represented with the following triplet:
<insert-and-copy length code, insert extra bits, copy extra bits>
The insert-and-copy length code, the insert extra bits and the copy
extra bits are encoded back-to-back, the insert-and-copy length code
is encoded using a prefix code over the insert-and-copy length code
alphabet, while the extra bits values are encoded as fixed-width
integer values. The number of insert and copy extra bits can be
0 - 24, and they are dependent on the insert-and-copy length code.
Some of the insert-and-copy length codes also express the fact that
the distance symbol of the distance in the same command is 0, i.e. the
distance component of the command is the same as that of the previous
command. In this case, the distance code and extra bits for the
distance are omitted from the compressed data stream.
We describe the insert-and-copy length code alphabet in terms of the
(not directly used) insert length code and copy length code
alphabets. The symbols of the insert length code alphabet, along with
the number of insert extra bits and the range of the insert lengths
are as follows:
.nf
.KS
Extra Extra Extra
Code Bits Lengths Code Bits Lengths Code Bits Lengths
---- ---- ------ ---- ---- ------- ---- ---- -------
0 0 0 8 2 10-13 16 6 130-193
1 0 1 9 2 14-17 17 7 194-321
2 0 2 10 3 18-25 18 8 322-577
3 0 3 11 3 26-33 19 9 578-1089
4 0 4 12 4 34-49 20 10 1090-2113
5 0 5 13 4 50-65 21 12 2114-6209
6 1 6,7 14 5 66-97 22 14 6210-22593
7 1 8,9 15 5 98-129 23 24 22594-16799809
.KE
.fi
The symbols of the copy length code alphabet, along with the number
of copy extra bits and the range of copy lengths are as follows:
.nf
.KS
Extra Extra Extra
Code Bits Lengths Code Bits Lengths Code Bits Lengths
---- ---- ------ ---- ---- ------- ---- ---- -------
0 0 2 8 1 10,11 16 5 70-101
1 0 3 9 1 12,13 17 5 102-133
2 0 4 10 2 14-17 18 6 134-197
3 0 5 11 2 18-21 19 7 198-325
4 0 6 12 3 22-29 20 8 326-581
5 0 7 13 3 30-37 21 9 582-1093
6 0 8 14 4 38-53 22 10 1094-2117
7 0 9 15 4 54-69 23 24 2118-16779333
.KE
.fi
To convert an insert-and-copy length code to an insert length code
and a copy length code, the following table can be used:
.nf
.KS
Insert
length Copy length code
code 0-7 8-15 16-23
+---------+---------+
| | |
0-7 | 0-63 | 64-127 | <--- distance symbol 0
| | |
+---------+---------+---------+
| | | |
0-7 | 128-191 | 192-255 | 384-447 |
| | | |
+---------+---------+---------+
| | | |
8-15 | 256-319 | 320-383 | 512-575 |
| | | |
+---------+---------+---------+
| | | |
16-23 | 448-511 | 576-639 | 640-703 |
| | | |
+---------+---------+---------+
.KE
.fi
First, look up the cell with the 64 value range containing the
insert-and-copy length code, this gives the insert length code and
the copy length code ranges, both 8 values long. The copy length
code within its range is determined by the lowest 3 bits of the
insert-and-copy length code, and the insert length code within its
range is determined by bits 3-5 (counted from the LSB) of the insert-
and-copy length code. Given the insert length and copy length codes,
the actual insert and copy lengths can be obtained by reading the
number of extra bits given by the tables above.
If the insert-and-copy length code is between 0 and 127, the distance
code of the command is set to zero (the last distance reused).
.ti 0
6. Encoding of block switch commands
As described in Section 2, a block-switch command is a pair
<block type, block count>. These are encoded in the compressed data
part of the meta-block, right before the start of each new block of a
particular block category.
Each block type in the compressed data is represented with a block
type code, encoded using a prefix code over the block type code
alphabet. A block type symbol 0 means that the new block type is the same
as the type of the previous block from the same block category, i.e.
the block type that preceded the current type,
while a block type symbol 1 means that the new block type equals the current
block type plus one. If the current block type is the maximal possible,
then a block type symbol of 1 results in wrapping to a new block type of 0.
Block type symbols 2 - 257
represent block types 0 - 255 respectively. The previous and current block types
are initialized to 1 and 0, respectively, at the end of the
meta-block header.
Since the first block type of each block category is 0, the block
type of the first block switch command is not encoded in
the compressed data. Instead the block count for each category
that has more than one type is encoded in the meta-block header.
The block counts for all three categories should count down to exactly
zero at the end of the meta-block. If any do not, then the stream
should be rejected as invalid.
The number of different block types in each block category, denoted
by NBLTYPESL, NBLTYPESI, and NBLTYPESD for literals, insert-and-copy
lengths and distances, respectively, is encoded in the meta-block
header, and it must equal to the largest block type plus one in that
block category. In other words, the set of literal, insert-and-copy
length and distance block types must be [0..NBLTYPESL-1],
[0..NBLTYPESI-1], and [0..NBLTYPESD-1], respectively. From this it
follows that the alphabet size of literal, insert-and-copy length and
distance block type codes is NBLTYPES + 2, NBLTYPESI + 2 and
NBLTYPESD + 2, respectively.
Each block count in the compressed data is represented with a pair
<block count code, extra bits>. The block count code and the extra
bits are encoded back-to-back, the block count code is encoded using
a prefix code over the block count code alphabet, while the extra
bits value is encoded as a fixed-width integer value. The number of
extra bits can be 0 - 24, and it is dependent on the block count
code. The symbols of the block count code alphabet, along with the
number of extra bits and the range of block counts are as follows:
.nf
.KS
Extra Extra Extra
Code Bits Lengths Code Bits Lengths Code Bits Lengths
---- ---- ------ ---- ---- ------- ---- ---- -------