Skip to main content

turnloop_http/
compression.rs

1//! Bounded decompression into a caller-reused result buffer. This convenience API
2//! accepts a complete encoded body; wire body streaming is exposed by the codecs.
3use crate::{Error, Result};
4/// Convenience whole-body decode. Cache `StreamingDecoder` and use `reset` for
5/// allocation-free repeated responses instead of constructing one per call.
6pub fn decode(encoding: &str, input: &[u8], output: &mut Vec<u8>, limit: usize) -> Result<()> {
7    output.clear();
8    let mut decoder = StreamingDecoder::new(encoding.trim(), limit)?;
9    let mut pos = 0;
10    loop {
11        let mut bytes = [0; 8192];
12        let step = decoder.process(&input[pos..], &mut bytes, true)?;
13        pos += step.consumed;
14        output.extend_from_slice(&bytes[..step.written]);
15        if step.finished {
16            return Ok(());
17        }
18        if step.consumed == 0 && step.written == 0 {
19            return Err(corrupt());
20        }
21    }
22}
23
24/// A single incremental decode step. Retain input after `consumed`, and consume
25/// output before calling again. No internal input/output staging allocation.
26#[derive(Debug)]
27pub struct DecodeStep {
28    pub consumed: usize,
29    pub written: usize,
30    pub finished: bool,
31}
32#[derive(Clone, Copy, PartialEq, Eq)]
33enum Stage {
34    Header,
35    Body,
36    Trailer,
37    Done,
38}
39type BytePool = crate::recycling::Pool<u8>;
40type IntPool = crate::recycling::Pool<u32>;
41type CodePool = crate::recycling::Pool<brotli::HuffmanCode>;
42type BrotliState = brotli::BrotliState<BytePool, IntPool, CodePool>;
43// Keep the WASM decoder inline: 480 bytes avoids another per-response allocation.
44#[allow(clippy::large_enum_variant)]
45enum Engine {
46    Identity,
47    Deflate {
48        decoder: flate2::Decompress,
49        gzip: bool,
50        detect: bool,
51        stage: Stage,
52        crc: flate2::Crc,
53        members: usize,
54    },
55    Brotli {
56        state: Box<BrotliState>,
57        bytes: BytePool,
58        ints: IntPool,
59        codes: CodePool,
60    },
61    #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
62    Zstd {
63        decoder: zstd::stream::raw::Decoder<'static>,
64        boundary: bool,
65    },
66    #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
67    Zstd {
68        decoder: turnloop_zstd_decoder::decoding::FrameDecoder,
69        reset: bool,
70        boundary: bool,
71    },
72}
73pub struct StreamingDecoder {
74    engine: Engine,
75    total: usize,
76    limit: usize,
77    done: bool,
78    failed: bool,
79}
80fn corrupt() -> Error {
81    Error::new("UND_ERR_SOCKET", "invalid or incomplete compressed body")
82}
83impl StreamingDecoder {
84    pub fn new(encoding: &str, limit: usize) -> Result<Self> {
85        let engine = match encoding {
86            "" | "identity" => Engine::Identity,
87            "gzip" | "x-gzip" => Engine::Deflate {
88                decoder: flate2::Decompress::new(false),
89                gzip: true,
90                detect: false,
91                stage: Stage::Header,
92                crc: flate2::Crc::new(),
93                members: 0,
94            },
95            "deflate" => Engine::Deflate {
96                decoder: flate2::Decompress::new(false),
97                gzip: false,
98                detect: true,
99                stage: Stage::Body,
100                crc: flate2::Crc::new(),
101                members: 0,
102            },
103            "br" => {
104                let bytes = BytePool::default();
105                let ints = IntPool::default();
106                let codes = CodePool::default();
107                let state = Box::new(BrotliState::new(bytes.clone(), ints.clone(), codes.clone()));
108                Engine::Brotli {
109                    state,
110                    bytes,
111                    ints,
112                    codes,
113                }
114            }
115            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
116            "zstd" => {
117                let mut decoder = zstd::stream::raw::Decoder::new().map_err(|_| corrupt())?;
118                decoder
119                    .set_parameter(zstd::stream::raw::DParameter::WindowLogMax(27))
120                    .map_err(|_| corrupt())?;
121                Engine::Zstd {
122                    decoder,
123                    boundary: false,
124                }
125            }
126            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
127            "zstd" => Engine::Zstd {
128                decoder: turnloop_zstd_decoder::decoding::FrameDecoder::new(),
129                reset: true,
130                boundary: false,
131            },
132            _ => {
133                return Err(Error::new(
134                    "UND_ERR_NOT_SUPPORTED",
135                    "unsupported content encoding",
136                ));
137            }
138        };
139        Ok(Self {
140            engine,
141            total: 0,
142            limit,
143            done: false,
144            failed: false,
145        })
146    }
147    /// Reuse algorithm and scratch storage for another body of the same encoding.
148    /// Warm up with the largest expected body shape before measuring allocations.
149    pub fn reset(&mut self, limit: usize) -> Result<()> {
150        match &mut self.engine {
151            Engine::Identity => {}
152            Engine::Deflate {
153                decoder,
154                gzip,
155                detect,
156                stage,
157                crc,
158                members,
159            } => {
160                decoder.reset(false);
161                *detect = !*gzip;
162                *stage = if *gzip { Stage::Header } else { Stage::Body };
163                crc.reset();
164                *members = 0;
165            }
166            Engine::Brotli {
167                state,
168                bytes,
169                ints,
170                codes,
171            } => {
172                **state = BrotliState::new(bytes.clone(), ints.clone(), codes.clone());
173            }
174            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
175            Engine::Zstd { decoder, boundary } => {
176                use zstd::stream::raw::Operation;
177                decoder.reinit().map_err(|_| corrupt())?;
178                *boundary = false;
179            }
180            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
181            Engine::Zstd {
182                reset, boundary, ..
183            } => {
184                *reset = true;
185                *boundary = false;
186            }
187        }
188        self.total = 0;
189        self.limit = limit;
190        self.done = false;
191        self.failed = false;
192        Ok(())
193    }
194    /// `end` means no more encoded bytes will arrive, not that output is unbounded.
195    pub fn process(&mut self, input: &[u8], output: &mut [u8], end: bool) -> Result<DecodeStep> {
196        if self.failed {
197            return Err(corrupt());
198        }
199        let result = self.process_inner(input, output, end);
200        if result.is_err() {
201            self.failed = true;
202        }
203        result
204    }
205    fn process_inner(&mut self, input: &[u8], output: &mut [u8], end: bool) -> Result<DecodeStep> {
206        if self.done {
207            if !input.is_empty() {
208                return Err(corrupt());
209            }
210            return Ok(DecodeStep {
211                consumed: 0,
212                written: 0,
213                finished: true,
214            });
215        }
216        if output.is_empty() {
217            return Ok(DecodeStep {
218                consumed: 0,
219                written: 0,
220                finished: false,
221            });
222        }
223        let mut consumed = 0;
224        let mut written = 0;
225        let mut finished = false;
226        match &mut self.engine {
227            Engine::Identity => {
228                consumed = input.len().min(output.len());
229                output[..consumed].copy_from_slice(&input[..consumed]);
230                written = consumed;
231                finished = end && consumed == input.len();
232            }
233            Engine::Deflate {
234                decoder,
235                gzip,
236                detect,
237                stage,
238                crc,
239                members,
240            } => {
241                if *stage == Stage::Header {
242                    if input.is_empty() && end && *members > 0 {
243                        finished = true;
244                    } else if let Some(n) = gzip_header(input)? {
245                        consumed = n;
246                        decoder.reset(false);
247                        crc.reset();
248                        *stage = Stage::Body;
249                    } else if end {
250                        return Err(corrupt());
251                    }
252                } else if *stage == Stage::Trailer {
253                    if input.len() >= 8 {
254                        if u32::from_le_bytes(input[..4].try_into().unwrap()) != crc.sum()
255                            || u32::from_le_bytes(input[4..8].try_into().unwrap()) != crc.amount()
256                        {
257                            return Err(corrupt());
258                        }
259                        consumed = 8;
260                        *members += 1;
261                        *stage = Stage::Header;
262                        finished = end && input.len() == 8;
263                    } else if end {
264                        return Err(corrupt());
265                    }
266                } else if *stage == Stage::Done {
267                    if !input.is_empty() {
268                        return Err(corrupt());
269                    }
270                    finished = end;
271                } else {
272                    if *detect {
273                        if input.len() < 2 {
274                            if end {
275                                return Err(corrupt());
276                            }
277                            return Ok(DecodeStep {
278                                consumed: 0,
279                                written: 0,
280                                finished: false,
281                            });
282                        }
283                        let zlib = input[0] & 15 == 8
284                            && u16::from_be_bytes([input[0], input[1]]).is_multiple_of(31);
285                        decoder.reset(zlib);
286                        *detect = false;
287                    }
288                    let before_in = decoder.total_in();
289                    let before_out = decoder.total_out();
290                    let status = decoder
291                        .decompress(input, output, flate2::FlushDecompress::None)
292                        .map_err(|_| corrupt())?;
293                    consumed = (decoder.total_in() - before_in) as usize;
294                    written = (decoder.total_out() - before_out) as usize;
295                    if *gzip {
296                        crc.update(&output[..written]);
297                    }
298                    if status == flate2::Status::StreamEnd {
299                        *stage = if *gzip { Stage::Trailer } else { Stage::Done };
300                        finished = !*gzip && end && consumed == input.len();
301                    } else if end && consumed == input.len() && written < output.len() {
302                        return Err(corrupt());
303                    }
304                }
305            }
306            Engine::Brotli { state, .. } => {
307                let mut available_in = input.len();
308                let mut available_out = output.len();
309                let mut total = self.total;
310                let result = brotli::BrotliDecompressStream(
311                    &mut available_in,
312                    &mut consumed,
313                    input,
314                    &mut available_out,
315                    &mut written,
316                    output,
317                    &mut total,
318                    state,
319                );
320                match result {
321                    brotli::BrotliResult::ResultSuccess => {
322                        if consumed != input.len() {
323                            return Err(corrupt());
324                        }
325                        finished = true;
326                    }
327                    brotli::BrotliResult::ResultFailure => return Err(corrupt()),
328                    brotli::BrotliResult::NeedsMoreInput if end => return Err(corrupt()),
329                    _ => {}
330                }
331            }
332            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
333            Engine::Zstd { decoder, boundary } => {
334                use zstd::stream::raw::Operation;
335                if input.is_empty() && end && *boundary {
336                    self.done = true;
337                    return Ok(DecodeStep {
338                        consumed: 0,
339                        written: 0,
340                        finished: true,
341                    });
342                }
343                let result = decoder
344                    .run_on_buffers(input, output)
345                    .map_err(|_| corrupt())?;
346                consumed = result.bytes_read;
347                written = result.bytes_written;
348                *boundary = result.remaining == 0;
349                finished = result.remaining == 0 && end && consumed == input.len();
350                if result.remaining != 0 && end && consumed == input.len() && written < output.len()
351                {
352                    return Err(corrupt());
353                }
354            }
355            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
356            Engine::Zstd {
357                decoder,
358                reset,
359                boundary,
360            } => {
361                if input.is_empty() && end && *boundary {
362                    self.done = true;
363                    return Ok(DecodeStep {
364                        consumed: 0,
365                        written: 0,
366                        finished: true,
367                    });
368                }
369                let mut source = input;
370                if *reset {
371                    if source.len() < 18 && !end {
372                        return Ok(DecodeStep {
373                            consumed: 0,
374                            written: 0,
375                            finished: false,
376                        });
377                    }
378                    decoder.reset(&mut source).map_err(|_| corrupt())?;
379                    consumed = input.len() - source.len();
380                    *reset = false;
381                    *boundary = false;
382                }
383                let result = decoder
384                    .decode_from_to(source, output)
385                    .map_err(|_| corrupt())?;
386                consumed += result.0;
387                written = result.1;
388                if decoder.is_finished() && decoder.can_collect() == 0 {
389                    if let Some(expected) = decoder.get_checksum_from_data()
390                        && decoder.get_calculated_checksum() != Some(expected)
391                    {
392                        return Err(corrupt());
393                    }
394                    *boundary = true;
395                    *reset = true;
396                    finished = end && consumed == input.len();
397                } else if end && consumed == 0 && written == 0 {
398                    return Err(corrupt());
399                }
400            }
401        }
402        if written > self.limit.saturating_sub(self.total) {
403            return Err(Error::new(
404                "UND_ERR_RES_EXCEEDED_MAX_SIZE",
405                "decoded body exceeds limit",
406            ));
407        }
408        self.total += written;
409        self.done = finished;
410        Ok(DecodeStep {
411            consumed,
412            written,
413            finished,
414        })
415    }
416}
417fn gzip_header(input: &[u8]) -> Result<Option<usize>> {
418    if input.len() < 10 {
419        return Ok(None);
420    }
421    if input[..3] != [31, 139, 8] || input[3] & 0xe0 != 0 {
422        return Err(corrupt());
423    }
424    let flags = input[3];
425    let mut pos = 10;
426    if flags & 4 != 0 {
427        if input.len() < 12 {
428            return Ok(None);
429        }
430        pos = 12 + u16::from_le_bytes(input[10..12].try_into().unwrap()) as usize;
431    }
432    for flag in [8, 16] {
433        if flags & flag != 0 {
434            if pos > input.len() {
435                break;
436            }
437            if let Some(n) = input[pos..].iter().position(|b| *b == 0) {
438                pos += n + 1;
439            } else {
440                if input.len() > 8192 {
441                    return Err(corrupt());
442                }
443                return Ok(None);
444            }
445        }
446    }
447    if pos > 8192 {
448        return Err(corrupt());
449    }
450    if input.len() < pos {
451        return Ok(None);
452    }
453    if flags & 2 != 0 {
454        if input.len() < pos + 2 {
455            return Ok(None);
456        }
457        let mut crc = flate2::Crc::new();
458        crc.update(&input[..pos]);
459        if crc.sum() as u16 != u16::from_le_bytes(input[pos..pos + 2].try_into().unwrap()) {
460            return Err(corrupt());
461        }
462        pos += 2;
463    }
464    Ok(Some(pos))
465}