/src/fontations/shared-brotli-patch-decoder/src/lib.rs
Line | Count | Source |
1 | | #[cfg(feature = "c-brotli")] |
2 | | mod c_brotli; |
3 | | |
4 | | #[cfg(feature = "c-brotli")] |
5 | | mod sys; |
6 | | |
7 | | #[cfg(feature = "rust-brotli")] |
8 | | mod rust_brotli; |
9 | | |
10 | | pub mod decode_error; |
11 | | |
12 | | #[cfg(fuzzing)] |
13 | | use decode_error::DecodeError; |
14 | | |
15 | | /// A Shared Brotli Decoder. |
16 | | /// |
17 | | /// Shared brotli (<https://datatracker.ietf.org/doc/draft-vandevenne-shared-brotli-format/>) is an |
18 | | /// extension of brotli to allow the decompression to include a shared dictionary. |
19 | | pub trait SharedBrotliDecoder { |
20 | | /// Decodes shared brotli encoded data using the optional shared dictionary. |
21 | | /// |
22 | | /// The shared dictionary is a raw LZ77 style dictionary, see: |
23 | | /// <https://datatracker.ietf.org/doc/html/draft-vandevenne-shared-brotli-format#section-3.2> |
24 | | /// |
25 | | /// Will fail if the decoded result will be greater than max_uncompressed_length. Any excess data |
26 | | /// in encoded after the encoded stream finishes is also considered an error. |
27 | | fn decode( |
28 | | &self, |
29 | | encoded: &[u8], |
30 | | shared_dictionary: Option<&[u8]>, |
31 | | max_uncompressed_length: usize, |
32 | | ) -> Result<Vec<u8>, decode_error::DecodeError>; |
33 | | } |
34 | | |
35 | | /// The brotli decoder provided by this crate. |
36 | | /// |
37 | | /// By default a rust wrapper around the c brotli decoder implementation is used. |
38 | | pub struct BuiltInBrotliDecoder; |
39 | | |
40 | | /// An implementation that just passes through the input data. |
41 | | /// |
42 | | /// Useful in fuzzers and unit testing. |
43 | | pub struct NoopBrotliDecoder; |
44 | | |
45 | | impl SharedBrotliDecoder for Box<dyn SharedBrotliDecoder> { |
46 | | fn decode( |
47 | | &self, |
48 | | encoded: &[u8], |
49 | | shared_dictionary: Option<&[u8]>, |
50 | | max_uncompressed_length: usize, |
51 | | ) -> Result<Vec<u8>, decode_error::DecodeError> { |
52 | | self.as_ref() |
53 | | .decode(encoded, shared_dictionary, max_uncompressed_length) |
54 | | } |
55 | | } |
56 | | |
57 | | impl SharedBrotliDecoder for BuiltInBrotliDecoder { |
58 | 0 | fn decode( |
59 | 0 | &self, |
60 | 0 | encoded: &[u8], |
61 | 0 | shared_dictionary: Option<&[u8]>, |
62 | 0 | max_uncompressed_length: usize, |
63 | 0 | ) -> Result<Vec<u8>, decode_error::DecodeError> { |
64 | | cfg_if::cfg_if! { |
65 | | if #[cfg(feature = "c-brotli")] { |
66 | | #[allow(clippy::needless_return)] |
67 | 0 | return c_brotli::shared_brotli_decode_c( |
68 | 0 | encoded, |
69 | 0 | shared_dictionary, |
70 | 0 | max_uncompressed_length, |
71 | | ); |
72 | | } else if #[cfg(feature = "rust-brotli")] { |
73 | | rust_brotli::shared_brotli_decode_rust( |
74 | | encoded, |
75 | | shared_dictionary, |
76 | | max_uncompressed_length, |
77 | | ) |
78 | | } else { |
79 | | compile_error!("At least one of 'c-brotli' or 'rust-brotli' must be enabled."); |
80 | | } |
81 | | } |
82 | 0 | } |
83 | | } |
84 | | |
85 | | impl SharedBrotliDecoder for NoopBrotliDecoder { |
86 | 0 | fn decode( |
87 | 0 | &self, |
88 | 0 | encoded: &[u8], |
89 | 0 | _shared_dictionary: Option<&[u8]>, |
90 | 0 | max_uncompressed_length: usize, |
91 | 0 | ) -> Result<Vec<u8>, decode_error::DecodeError> { |
92 | 0 | if encoded.len() <= max_uncompressed_length { |
93 | 0 | Ok(encoded.to_vec()) |
94 | | } else { |
95 | 0 | Err(decode_error::DecodeError::MaxSizeExceeded) |
96 | | } |
97 | 0 | } |
98 | | } |
99 | | |
100 | | #[cfg(test)] |
101 | | mod tests { |
102 | | use super::*; |
103 | | use decode_error::DecodeError; |
104 | | |
105 | | const TARGET: &[u8] = "hijkabcdeflmnohijkabcdeflmno\n".as_bytes(); |
106 | | const BASE: &str = "abcdef\n"; |
107 | | |
108 | | // This patch was manually generated with a brotli encoder (https://github.com/google/brotli) |
109 | | // uncompressed = TARGET |
110 | | // dict = BASE |
111 | | const SHARED_DICT_PATCH: [u8; 23] = [ |
112 | | 0xa1, 0xe0, 0x00, 0xc0, 0x2f, 0x3a, 0x38, 0xf4, 0x01, 0xd1, 0xaf, 0x54, 0x84, 0x14, 0x71, |
113 | | 0x2a, 0x80, 0x04, 0xa2, 0x1c, 0xd3, 0xdd, 0x07, |
114 | | ]; |
115 | | |
116 | | // This patch was manually generated with a brotli encoder (https://github.com/google/brotli) |
117 | | // uncompressed = TARGET |
118 | | const NO_DICT_PATCH: [u8; 26] = [ |
119 | | 0xa1, 0xe0, 0x00, 0xc0, 0x2f, 0x96, 0x1c, 0xf3, 0x03, 0xb1, 0xcf, 0x45, 0x95, 0x22, 0x4a, |
120 | | 0xc5, 0x03, 0x21, 0xb2, 0x9a, 0x58, 0xd4, 0x7c, 0xf6, 0x1e, 0x00u8, |
121 | | ]; |
122 | | |
123 | | #[test] |
124 | | fn brotli_decode_with_shared_dict() { |
125 | | assert_eq!( |
126 | | Ok(TARGET.to_vec()), |
127 | | BuiltInBrotliDecoder.decode(&SHARED_DICT_PATCH, Some(BASE.as_bytes()), TARGET.len(),) |
128 | | ); |
129 | | } |
130 | | |
131 | | #[test] |
132 | | fn brotli_decode_rust_brotli_regression_case() { |
133 | | // Tests a case that triggered a bug in the rust brotli decompressor library, |
134 | | // see: https://github.com/dropbox/rust-brotli-decompressor/issues/36 |
135 | | let patch: &[u8] = &[ |
136 | | 27, 103, 0, 96, 47, 14, 120, 211, 142, 228, 22, 15, 167, 193, 55, 28, 228, 226, 254, |
137 | | 54, 10, 36, 226, 192, 19, 76, 50, 8, 169, 92, 9, 197, 47, 12, 211, 114, 34, 175, 18, |
138 | | 241, 122, 134, 170, 32, 189, 4, 112, 153, 119, 12, 237, 23, 120, 130, 2, |
139 | | ]; |
140 | | |
141 | | let dict: Vec<u8> = vec![ |
142 | | 2, 0, 0, 0, 0, 213, 195, 31, 121, 231, 225, 250, 238, 34, 174, 158, 246, 208, 145, 187, |
143 | | 92, 2, 0, 0, 4, 0, 0, 0, 46, 0, 0, 0, 0, 0, 11, 123, 105, 100, 125, 46, 105, 102, 116, |
144 | | 95, 116, 107, 20, 0, 0, 52, 40, 103, 221, 215, 223, 255, 95, 54, 15, 13, 85, 53, 206, |
145 | | 115, 249, 165, 159, 159, 16, 29, 37, 17, 114, 1, 163, 2, 16, 33, 51, 4, 32, 0, 226, 29, |
146 | | 19, 88, 254, 195, 129, 23, 25, 22, 8, 19, 21, 41, 130, 136, 51, 8, 67, 209, 52, 204, |
147 | | 204, 70, 199, 130, 252, 47, 16, 40, 186, 251, 62, 63, 19, 236, 147, 240, 211, 215, 59, |
148 | | ]; |
149 | | |
150 | | let decompressed = BuiltInBrotliDecoder.decode(patch, Some(&dict), 500); |
151 | | |
152 | | assert_eq!( |
153 | | decompressed, |
154 | | Ok(vec![ |
155 | | 0x02, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x16, 0xa6, 0x25, 0x18, 0xf8, 0x68, 0x63, 0x4e, |
156 | | 0xe4, 0x09, 0x2b, 0xa1, 0xe2, 0x4b, 0xba, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, |
157 | | 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x7b, 0x69, 0x64, 0x7d, 0x2e, 0x69, 0x66, |
158 | | 0x74, 0x5f, 0x74, 0x6b, 0x14, 0x00, 0x00, 0x38, 0x1d, 0x25, 0x11, 0x72, 0x01, 0xa3, |
159 | | 0x02, 0x10, 0x21, 0x33, 0x04, 0x20, 0x00, 0xe2, 0x1d, 0x13, 0x58, 0xfe, 0xc3, 0x81, |
160 | | 0x17, 0x19, 0x16, 0x08, 0x13, 0x15, 0x29, 0x82, 0x88, 0x33, 0x08, 0x43, 0xd1, 0x34, |
161 | | 0xcc, 0xcc, 0x46, 0xc7, 0x82, 0xfc, 0x2f, 0x10, 0x28, 0xba, 0xfb, 0x3e, 0x3f, 0x13, |
162 | | 0xec, 0x93, 0xf0, 0xd3, 0xd7, 0x3b, |
163 | | ]) |
164 | | ); |
165 | | } |
166 | | |
167 | | #[test] |
168 | | fn brotli_decode_shared_dict_beyond_window() { |
169 | | // Tests a case that triggered a bug in the rust brotli decompressor library, |
170 | | // see: https://github.com/dropbox/rust-brotli-decompressor/issues/42 (fixed in 6.0.0) |
171 | | // |
172 | | // The dictionary is larger than the patch's 1008 byte window (lgwin 10) and the output runs |
173 | | // past the window, so the patch copies dictionary data from further back than the window. |
174 | | // Generated with the reference encoder: brotli -q 11 -w 10 -D dict -c target |
175 | | const PATCH: [u8; 19] = [ |
176 | | 0xa1, 0x38, 0x38, 0xc0, 0x3f, 0x01, 0x10, 0x85, 0xe5, 0x37, 0xe7, 0xac, 0x23, 0x21, |
177 | | 0x57, 0x02, 0x45, 0xc2, 0x60, |
178 | | ]; |
179 | | |
180 | | // xorshift32 output, so the only matches available to the encoder are in the dictionary. |
181 | | let mut x = 0x1234_5678u32; |
182 | | let dict: Vec<u8> = (0..2000) |
183 | | .map(|_| { |
184 | | x ^= x << 13; |
185 | | x ^= x >> 17; |
186 | | x ^= x << 5; |
187 | | x as u8 |
188 | | }) |
189 | | .collect(); |
190 | | let target = [&dict[..600], &dict[1400..], &dict[100..700]].concat(); |
191 | | |
192 | | let decompressed = BuiltInBrotliDecoder.decode(&PATCH, Some(&dict), target.len()); |
193 | | |
194 | | assert_eq!(decompressed, Ok(target)); |
195 | | } |
196 | | |
197 | | #[test] |
198 | | fn brotli_decode_without_shared_dict() { |
199 | | assert_eq!( |
200 | | Ok(TARGET.to_vec()), |
201 | | BuiltInBrotliDecoder.decode(&NO_DICT_PATCH, None, TARGET.len()) |
202 | | ); |
203 | | } |
204 | | |
205 | | #[test] |
206 | | fn brotli_decode_with_empty_shared_dict() { |
207 | | // An empty shared dictionary used to behave similarly to a `None`, but it is now invalid as |
208 | | // of https://github.com/google/brotli/pull/1479 |
209 | | assert_eq!( |
210 | | Err(DecodeError::InvalidDictionary), |
211 | | BuiltInBrotliDecoder.decode(&NO_DICT_PATCH, Some(b""), TARGET.len()) |
212 | | ); |
213 | | } |
214 | | |
215 | | #[test] |
216 | | fn brotli_decode_too_little_output() { |
217 | | assert_eq!( |
218 | | Err(DecodeError::MaxSizeExceeded), |
219 | | BuiltInBrotliDecoder.decode( |
220 | | &SHARED_DICT_PATCH, |
221 | | Some(BASE.as_bytes()), |
222 | | TARGET.len() - 1 |
223 | | ) |
224 | | ); |
225 | | } |
226 | | |
227 | | #[test] |
228 | | fn brotli_decode_excess_output() { |
229 | | assert_eq!( |
230 | | Ok(TARGET.to_vec()), |
231 | | BuiltInBrotliDecoder.decode( |
232 | | &SHARED_DICT_PATCH, |
233 | | Some(BASE.as_bytes()), |
234 | | TARGET.len() + 1, |
235 | | ) |
236 | | ); |
237 | | } |
238 | | |
239 | | #[test] |
240 | | fn brotli_decode_too_much_input() { |
241 | | let mut patch: Vec<u8> = NO_DICT_PATCH.to_vec(); |
242 | | patch.push(0u8); |
243 | | |
244 | | assert_eq!( |
245 | | Err(DecodeError::ExcessInputData), |
246 | | BuiltInBrotliDecoder.decode(&patch, None, TARGET.len()) |
247 | | ); |
248 | | } |
249 | | |
250 | | #[test] |
251 | | fn brotli_decode_input_missing() { |
252 | | // Check what happens if input stream is missing some trailing bytes |
253 | | let patch: Vec<u8> = NO_DICT_PATCH[..NO_DICT_PATCH.len() - 1].to_vec(); |
254 | | assert!(matches!( |
255 | | BuiltInBrotliDecoder.decode(&patch, None, TARGET.len()), |
256 | | Err(DecodeError::InvalidStream) |
257 | | )); |
258 | | } |
259 | | |
260 | | #[test] |
261 | | fn brotli_decode_invalid() { |
262 | | let patch = [0xFF, 0xFF, 0xFFu8]; |
263 | | assert!(matches!( |
264 | | BuiltInBrotliDecoder.decode(&patch, None, 10), |
265 | | Err(DecodeError::InvalidStream) |
266 | | )); |
267 | | } |
268 | | } |