1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3    Distributed under MIT license.
4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6 
7 /**
8  * @file
9  * Common constants used in decoder and encoder API.
10  */
11 
12 #ifndef BROTLI_COMMON_CONSTANTS_H_
13 #define BROTLI_COMMON_CONSTANTS_H_
14 
15 #include "./platform.h"
16 #include <brotli/types.h>
17 
18 /* Specification: 7.3. Encoding of the context map */
19 #define BROTLI_CONTEXT_MAP_MAX_RLE 16
20 
21 /* Specification: 2. Compressed representation overview */
22 #define BROTLI_MAX_NUMBER_OF_BLOCK_TYPES 256
23 
24 /* Specification: 3.3. Alphabet sizes: insert-and-copy length */
25 #define BROTLI_NUM_LITERAL_SYMBOLS 256
26 #define BROTLI_NUM_COMMAND_SYMBOLS 704
27 #define BROTLI_NUM_BLOCK_LEN_SYMBOLS 26
28 #define BROTLI_MAX_CONTEXT_MAP_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + \
29                                         BROTLI_CONTEXT_MAP_MAX_RLE)
30 #define BROTLI_MAX_BLOCK_TYPE_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2)
31 
32 /* Specification: 3.5. Complex prefix codes */
33 #define BROTLI_REPEAT_PREVIOUS_CODE_LENGTH 16
34 #define BROTLI_REPEAT_ZERO_CODE_LENGTH 17
35 #define BROTLI_CODE_LENGTH_CODES (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1)
36 /* "code length of 8 is repeated" */
37 #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
38 
39 /* "Large Window Brotli" */
40 
41 /**
42  * The theoretical maximum number of distance bits specified for large window
43  * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit
44  * encoders and decoders only support up to 30 max distance bits, the value is
45  * set to 62 because it affects the large window brotli file format.
46  * Specifically, it affects the encoding of simple huffman tree for distances,
47  * see Specification RFC 7932 chapter 3.4.
48  */
49 #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
50 #define BROTLI_LARGE_MIN_WBITS 10
51 /**
52  * The maximum supported large brotli window bits by the encoder and decoder.
53  * Large window brotli allows up to 62 bits, however the current encoder and
54  * decoder, designed for 32-bit integers, only support up to 30 bits maximum.
55  */
56 #define BROTLI_LARGE_MAX_WBITS 30
57 
58 /* Specification: 4. Encoding of distances */
59 #define BROTLI_NUM_DISTANCE_SHORT_CODES 16
60 /**
61  * Maximal number of "postfix" bits.
62  *
63  * Number of "postfix" bits is stored as 2 bits in meta-block header.
64  */
65 #define BROTLI_MAX_NPOSTFIX 3
66 #define BROTLI_MAX_NDIRECT 120
67 #define BROTLI_MAX_DISTANCE_BITS 24U
68 #define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \
69     BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) +                    \
70     ((MAXNBITS) << ((NPOSTFIX) + 1)))
71 /* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */
72 #define BROTLI_NUM_DISTANCE_SYMBOLS \
73     BROTLI_DISTANCE_ALPHABET_SIZE(  \
74         BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)
75 
76 /* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932
77    brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and
78    NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */
79 #define BROTLI_MAX_DISTANCE 0x3FFFFFC
80 
81 /* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit
82    allows safe distance calculation without overflows, given the distance
83    alphabet size is limited to corresponding size
84    (see kLargeWindowDistanceCodeLimits). */
85 #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
86 
87 /* 7.1. Context modes and context ID lookup for literals */
88 /* "context IDs for literals are in the range of 0..63" */
89 #define BROTLI_LITERAL_CONTEXT_BITS 6
90 
91 /* 7.2. Context ID for distances */
92 #define BROTLI_DISTANCE_CONTEXT_BITS 2
93 
94 /* 9.1. Format of the Stream Header */
95 /* Number of slack bytes for window size. Don't confuse
96    with BROTLI_NUM_DISTANCE_SHORT_CODES. */
97 #define BROTLI_WINDOW_GAP 16
98 #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
99 
100 typedef struct BrotliDistanceCodeLimit {
101   uint32_t max_alphabet_size;
102   uint32_t max_distance;
103 } BrotliDistanceCodeLimit;
104 
105 /* This function calculates maximal size of distance alphabet, such that the
106    distances greater than the given values can not be represented.
107 
108    This limits are designed to support fast and safe 32-bit decoders.
109    "32-bit" means that signed integer values up to ((1 << 31) - 1) could be
110    safely expressed.
111 
112    Brotli distance alphabet symbols do not represent consecutive distance
113    ranges. Each distance alphabet symbol (excluding direct distances and short
114    codes), represent interleaved (for NPOSTFIX > 0) range of distances.
115    A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved
116    range. Two consecutive groups require the same amount of "extra bits".
117 
118    It is important that distance alphabet represents complete "groups".
119    To avoid complex logic on encoder side about interleaved ranges
120    it was decided to restrict both sides to complete distance code "groups".
121  */
BrotliCalculateDistanceCodeLimit(uint32_t max_distance,uint32_t npostfix,uint32_t ndirect)122 BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(
123     uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {
124   BrotliDistanceCodeLimit result;
125   /* Marking this function as unused, because not all files
126      including "constants.h" use it -> compiler warns about that. */
127   BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);
128   if (max_distance <= ndirect) {
129     /* This case never happens / exists only for the sake of completeness. */
130     result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;
131     result.max_distance = max_distance;
132     return result;
133   } else {
134     /* The first prohibited value. */
135     uint32_t forbidden_distance = max_distance + 1;
136     /* Subtract "directly" encoded region. */
137     uint32_t offset = forbidden_distance - ndirect - 1;
138     uint32_t ndistbits = 0;
139     uint32_t tmp;
140     uint32_t half;
141     uint32_t group;
142     /* Postfix for the last dcode in the group. */
143     uint32_t postfix = (1u << npostfix) - 1;
144     uint32_t extra;
145     uint32_t start;
146     /* Remove postfix and "head-start". */
147     offset = (offset >> npostfix) + 4;
148     /* Calculate the number of distance bits. */
149     tmp = offset / 2;
150     /* Poor-man's log2floor, to avoid extra dependencies. */
151     while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}
152     /* One bit is covered with subrange addressing ("half"). */
153     ndistbits--;
154     /* Find subrange. */
155     half = (offset >> ndistbits) & 1;
156     /* Calculate the "group" part of dcode. */
157     group = ((ndistbits - 1) << 1) | half;
158     /* Calculated "group" covers the prohibited distance value. */
159     if (group == 0) {
160       /* This case is added for correctness; does not occur for limit > 128. */
161       result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;
162       result.max_distance = ndirect;
163       return result;
164     }
165     /* Decrement "group", so it is the last permitted "group". */
166     group--;
167     /* After group was decremented, ndistbits and half must be recalculated. */
168     ndistbits = (group >> 1) + 1;
169     /* The last available distance in the subrange has all extra bits set. */
170     extra = (1u << ndistbits) - 1;
171     /* Calculate region start. NB: ndistbits >= 1. */
172     start = (1u << (ndistbits + 1)) - 4;
173     /* Move to subregion. */
174     start += (group & 1) << ndistbits;
175     /* Calculate the alphabet size. */
176     result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +
177         BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
178     /* Calculate the maximal distance representable by alphabet. */
179     result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;
180     return result;
181   }
182 }
183 
184 #endif  /* BROTLI_COMMON_CONSTANTS_H_ */
185