Skip to main content

script/dom/stream/
decompressionstream.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use std::cell::RefCell;
6use std::io::{self, Write};
7use std::ptr;
8
9use brotli::DecompressorWriter as BrotliDecoder;
10use dom_struct::dom_struct;
11use flate2::write::{DeflateDecoder, GzDecoder, ZlibDecoder};
12use js::jsapi::JSObject;
13use js::jsval::UndefinedValue;
14use js::rust::{HandleObject as SafeHandleObject, HandleValue as SafeHandleValue};
15use js::typedarray::Uint8;
16use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
17use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
18
19use crate::dom::bindings::buffer_source::create_buffer_source;
20use crate::dom::bindings::codegen::Bindings::CompressionStreamBinding::CompressionFormat;
21use crate::dom::bindings::codegen::Bindings::DecompressionStreamBinding::DecompressionStreamMethods;
22use crate::dom::bindings::conversions::SafeToJSValConvertible;
23use crate::dom::bindings::error::{Error, Fallible};
24use crate::dom::bindings::root::{Dom, DomRoot};
25use crate::dom::stream::compressionstream::{BROTLI_BUFFER_SIZE, convert_chunk_to_vec};
26use crate::dom::stream::transformstreamdefaultcontroller::TransformerType;
27use crate::dom::types::{
28    GlobalScope, ReadableStream, TransformStream, TransformStreamDefaultController, WritableStream,
29};
30
31/// <https://compression.spec.whatwg.org/#decompressionstream>
32#[dom_struct]
33pub(crate) struct DecompressionStream {
34    reflector_: Reflector,
35
36    /// <https://streams.spec.whatwg.org/#generictransformstream>
37    transform: Dom<TransformStream>,
38
39    /// <https://compression.spec.whatwg.org/#decompressionstream-format>
40    format: CompressionFormat,
41
42    // <https://compression.spec.whatwg.org/#decompressionstream-context>
43    #[no_trace]
44    context: RefCell<DecompressionContext>,
45}
46
47impl DecompressionStream {
48    fn new_inherited(
49        transform: &TransformStream,
50        format: CompressionFormat,
51    ) -> DecompressionStream {
52        DecompressionStream {
53            reflector_: Reflector::new(),
54            transform: Dom::from_ref(transform),
55            format,
56            context: RefCell::new(DecompressionContext::new(format)),
57        }
58    }
59
60    fn new_with_proto(
61        cx: &mut js::context::JSContext,
62        global: &GlobalScope,
63        proto: Option<SafeHandleObject>,
64        transform: &TransformStream,
65        format: CompressionFormat,
66    ) -> DomRoot<DecompressionStream> {
67        reflect_dom_object_with_proto_and_cx(
68            Box::new(DecompressionStream::new_inherited(transform, format)),
69            global,
70            proto,
71            cx,
72        )
73    }
74}
75
76impl DecompressionStreamMethods<crate::DomTypeHolder> for DecompressionStream {
77    /// <https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream>
78    fn Constructor(
79        cx: &mut js::context::JSContext,
80        global: &GlobalScope,
81        proto: Option<SafeHandleObject>,
82        format: CompressionFormat,
83    ) -> Fallible<DomRoot<DecompressionStream>> {
84        // Step 1. If format is unsupported in DecompressionStream, then throw a TypeError.
85        // NOTE: All of "brotli", "deflate", "deflate-raw" and "gzip" are supported.
86
87        // Step 2. Set this’s format to format.
88        // Step 5. Set this’s transform to a new TransformStream.
89        let transform = TransformStream::new_with_proto(cx, global, None);
90        let decompression_stream =
91            DecompressionStream::new_with_proto(cx, global, proto, &transform, format);
92
93        // Step 3. Let transformAlgorithm be an algorithm which takes a chunk argument and runs the
94        // decompress and enqueue a chunk algorithm with this and chunk.
95        // Step 4. Let flushAlgorithm be an algorithm which takes no argument and runs the
96        // decompress flush and enqueue algorithm with this.
97        let transformer_type = TransformerType::Decompressor(decompression_stream.clone());
98
99        // Step 6. Set up this’s transform with transformAlgorithm set to transformAlgorithm and
100        // flushAlgorithm set to flushAlgorithm.
101        transform.set_up(cx, global, transformer_type)?;
102
103        Ok(decompression_stream)
104    }
105
106    /// <https://streams.spec.whatwg.org/#dom-generictransformstream-readable>
107    fn Readable(&self) -> DomRoot<ReadableStream> {
108        // The readable getter steps are to return this’s transform.[[readable]].
109        self.transform.get_readable()
110    }
111
112    /// <https://streams.spec.whatwg.org/#dom-generictransformstream-writable>
113    fn Writable(&self) -> DomRoot<WritableStream> {
114        // The writable getter steps are to return this’s transform.[[writable]].
115        self.transform.get_writable()
116    }
117}
118
119/// <https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk>
120pub(crate) fn decompress_and_enqueue_a_chunk(
121    cx: &mut js::context::JSContext,
122    global: &GlobalScope,
123    ds: &DecompressionStream,
124    chunk: SafeHandleValue,
125    controller: &TransformStreamDefaultController,
126) -> Fallible<()> {
127    // Step 1. If chunk is not a BufferSource type, then throw a TypeError.
128    let chunk = convert_chunk_to_vec(cx, chunk)?;
129
130    // Step 2. Let buffer be the result of decompressing chunk with ds’s format and context. If
131    // this results in an error, then throw a TypeError.
132    // NOTE: In our implementation, the enum type of context already indicates the format.
133    let mut decompression_context = ds.context.borrow_mut();
134    let buffer = decompression_context
135        .decompress(&chunk)
136        .map_err(|_| Error::Type(c"Failed to decompress a chunk of compressed input".into()))?;
137
138    // Step 3. If buffer is empty, return.
139    if buffer.is_empty() {
140        return Ok(());
141    }
142
143    // Step 4. Let arrays be the result of splitting buffer into one or more non-empty pieces and
144    // converting them into Uint8Arrays.
145    // Step 5. For each Uint8Array array of arrays, enqueue array in ds’s transform.
146    // NOTE: We process the result in a single Uint8Array.
147    rooted!(&in(cx) let mut js_object = ptr::null_mut::<JSObject>());
148    let array = create_buffer_source::<Uint8>(cx, &buffer, js_object.handle_mut())
149        .map_err(|_| Error::Type(c"Cannot convert byte sequence to Uint8Array".to_owned()))?;
150    rooted!(&in(cx) let mut rval = UndefinedValue());
151    array.safe_to_jsval(cx, rval.handle_mut());
152    controller.enqueue(cx, global, rval.handle())?;
153
154    // Step 6. If the end of the compressed input has been reached, and ds’s context has not fully
155    // consumed chunk, then throw a TypeError.
156    if decompression_context.is_ended {
157        return Err(Error::Type(
158            c"The end of the compressed input has been reached".to_owned(),
159        ));
160    }
161
162    Ok(())
163}
164
165/// <https://compression.spec.whatwg.org/#decompress-flush-and-enqueue>
166pub(crate) fn decompress_flush_and_enqueue(
167    cx: &mut js::context::JSContext,
168    global: &GlobalScope,
169    ds: &DecompressionStream,
170    controller: &TransformStreamDefaultController,
171) -> Fallible<()> {
172    // Step 1. Let buffer be the result of decompressing an empty input with ds’s format and
173    // context, with the finish flag.
174    // NOTE: In our implementation, the enum type of context already indicates the format.
175    let mut decompression_context = ds.context.borrow_mut();
176    let buffer = decompression_context
177        .finalize()
178        .map_err(|_| Error::Type(c"Failed to finalize the decompression stream".into()))?;
179
180    // Step 2. If buffer is empty, return.
181    if !buffer.is_empty() {
182        // Step 2.1. Let arrays be the result of splitting buffer into one or more non-empty pieces
183        // and converting them into Uint8Arrays.
184        // Step 2.2. For each Uint8Array array of arrays, enqueue array in ds’s transform.
185        // NOTE: We process the result in a single Uint8Array.
186        rooted!(&in(cx) let mut js_object = ptr::null_mut::<JSObject>());
187        let array = create_buffer_source::<Uint8>(cx, &buffer, js_object.handle_mut())
188            .map_err(|_| Error::Type(c"Cannot convert byte sequence to Uint8Array".to_owned()))?;
189        rooted!(&in(cx) let mut rval = UndefinedValue());
190        array.safe_to_jsval(cx, rval.handle_mut());
191        controller.enqueue(cx, global, rval.handle())?;
192    }
193
194    // Step 3. If the end of the compressed input has not been reached, then throw a TypeError.
195    //
196    // NOTE: If the end of the compressed input has not been reached, flate2::write::DeflateDecoder
197    // and flate2::write::GzDecoder can detect it and throw an error on `try_finish` in Step 1.
198    // However, flate2::write::ZlibDecoder does not. We need to test it by ourselves.
199    //
200    // To test it, we write one more byte to the decoder. If it accepts the extra byte, this
201    // indicates the end has not been reached. Otherwise, the end has been reached. This test has
202    // to been done before calling `try_finish`, so we execute it in Step 1, and store the result
203    // in `is_ended`.
204    if !decompression_context.is_ended {
205        return Err(Error::Type(
206            c"The end of the compressed input has not been reached".to_owned(),
207        ));
208    }
209
210    Ok(())
211}
212
213/// An enum grouping decoders of differenct compression algorithms.
214enum Decoder {
215    Brotli(Box<BrotliDecoder<Vec<u8>>>),
216    Deflate(ZlibDecoder<Vec<u8>>),
217    DeflateRaw(DeflateDecoder<Vec<u8>>),
218    Gzip(GzDecoder<Vec<u8>>),
219}
220
221impl MallocSizeOf for Decoder {
222    #[expect(unsafe_code)]
223    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
224        match self {
225            Decoder::Brotli(decoder) => unsafe { ops.malloc_size_of(&**decoder) },
226            Decoder::Deflate(decoder) => decoder.size_of(ops),
227            Decoder::DeflateRaw(decoder) => decoder.size_of(ops),
228            Decoder::Gzip(decoder) => decoder.size_of(ops),
229        }
230    }
231}
232
233/// <https://compression.spec.whatwg.org/#decompressionstream-context>
234/// Used to encapsulate the logic of decoder.
235#[derive(MallocSizeOf)]
236struct DecompressionContext {
237    decoder: Decoder,
238    is_ended: bool,
239}
240
241impl DecompressionContext {
242    fn new(format: CompressionFormat) -> DecompressionContext {
243        let decoder = match format {
244            CompressionFormat::Brotli => {
245                Decoder::Brotli(Box::new(BrotliDecoder::new(Vec::new(), BROTLI_BUFFER_SIZE)))
246            },
247            CompressionFormat::Deflate => Decoder::Deflate(ZlibDecoder::new(Vec::new())),
248            CompressionFormat::Deflate_raw => Decoder::DeflateRaw(DeflateDecoder::new(Vec::new())),
249            CompressionFormat::Gzip => Decoder::Gzip(GzDecoder::new(Vec::new())),
250        };
251        DecompressionContext {
252            decoder,
253            is_ended: false,
254        }
255    }
256
257    fn decompress(&mut self, mut chunk: &[u8]) -> Result<Vec<u8>, io::Error> {
258        let mut result = Vec::new();
259
260        match &mut self.decoder {
261            Decoder::Brotli(decoder) => {
262                while !chunk.is_empty() {
263                    let written = decoder.write(chunk)?;
264                    if written == 0 {
265                        self.is_ended = true;
266                        break;
267                    }
268                    chunk = &chunk[written..];
269                }
270                decoder.flush()?;
271                result.append(decoder.get_mut());
272            },
273            Decoder::Deflate(decoder) => {
274                while !chunk.is_empty() {
275                    let written = decoder.write(chunk)?;
276                    if written == 0 {
277                        self.is_ended = true;
278                        break;
279                    }
280                    chunk = &chunk[written..];
281                }
282                decoder.flush()?;
283                result.append(decoder.get_mut());
284            },
285            Decoder::DeflateRaw(decoder) => {
286                while !chunk.is_empty() {
287                    let written = decoder.write(chunk)?;
288                    if written == 0 {
289                        self.is_ended = true;
290                        break;
291                    }
292                    chunk = &chunk[written..];
293                }
294                decoder.flush()?;
295                result.append(decoder.get_mut());
296            },
297            Decoder::Gzip(decoder) => {
298                while !chunk.is_empty() {
299                    let written = decoder.write(chunk)?;
300                    if written == 0 {
301                        self.is_ended = true;
302                        break;
303                    }
304                    chunk = &chunk[written..];
305                }
306                decoder.flush()?;
307                result.append(decoder.get_mut());
308            },
309        }
310
311        Ok(result)
312    }
313
314    fn finalize(&mut self) -> Result<Vec<u8>, io::Error> {
315        let mut result = Vec::new();
316
317        match &mut self.decoder {
318            Decoder::Brotli(decoder) => {
319                if decoder.close().is_ok() {
320                    self.is_ended = true;
321                };
322                result.append(decoder.get_mut());
323            },
324            Decoder::Deflate(decoder) => {
325                // Compressed data in "Deflate" format does not have trailing bytes. Therefore,
326                // `ZlibEncoder::try_finish` is designed not to throw an error when the end of
327                // compressed input has not been reached, in order to decompress as much of the
328                // input as possible.
329                //
330                // To detect whether the end is reached, the workaround is to write one more byte to
331                // the encoder. Refusing to take the extra byte indicates the end has been reached.
332                //
333                // Note that we need to pull out the data in buffer first to avoid the extra byte
334                // contaminate the output.
335                decoder.flush()?;
336                result.append(decoder.get_mut());
337                if decoder.write(&[0])? == 0 {
338                    self.is_ended = true;
339                }
340                decoder.try_finish()?;
341            },
342            Decoder::DeflateRaw(decoder) => {
343                if decoder.try_finish().is_ok() {
344                    self.is_ended = true;
345                };
346                result.append(decoder.get_mut());
347            },
348            Decoder::Gzip(decoder) => {
349                if decoder.try_finish().is_ok() {
350                    self.is_ended = true;
351                };
352                result.append(decoder.get_mut());
353            },
354        }
355
356        Ok(result)
357    }
358}