|
| 1 | +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* vim:set ts=2 sw=2 sts=2 et cindent: */ |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +#include "FormatBrotli.h" |
| 8 | + |
| 9 | +#include <memory> |
| 10 | + |
| 11 | +#include "BaseAlgorithms.h" |
| 12 | +#include "brotli/decode.h" |
| 13 | +#include "mozilla/dom/TransformStreamDefaultController.h" |
| 14 | + |
| 15 | +namespace mozilla::dom::compression { |
| 16 | + |
| 17 | +NS_IMPL_CYCLE_COLLECTION_INHERITED(BrotliDecompressionStreamAlgorithms, |
| 18 | + TransformerAlgorithmsBase) |
| 19 | +NS_IMPL_ADDREF_INHERITED(BrotliDecompressionStreamAlgorithms, |
| 20 | + TransformerAlgorithmsBase) |
| 21 | +NS_IMPL_RELEASE_INHERITED(BrotliDecompressionStreamAlgorithms, |
| 22 | + TransformerAlgorithmsBase) |
| 23 | +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrotliDecompressionStreamAlgorithms) |
| 24 | +NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase) |
| 25 | + |
| 26 | +Result<already_AddRefed<BrotliDecompressionStreamAlgorithms>, nsresult> |
| 27 | +BrotliDecompressionStreamAlgorithms::Create() { |
| 28 | + RefPtr<BrotliDecompressionStreamAlgorithms> alg = |
| 29 | + new BrotliDecompressionStreamAlgorithms(); |
| 30 | + MOZ_TRY(alg->Init()); |
| 31 | + return alg.forget(); |
| 32 | +} |
| 33 | + |
| 34 | +[[nodiscard]] nsresult BrotliDecompressionStreamAlgorithms::Init() { |
| 35 | + mState = std::unique_ptr<BrotliDecoderStateStruct, BrotliDeleter>( |
| 36 | + BrotliDecoderCreateInstance(nullptr, nullptr, nullptr)); |
| 37 | + if (!mState) { |
| 38 | + return NS_ERROR_OUT_OF_MEMORY; |
| 39 | + } |
| 40 | + return NS_OK; |
| 41 | +} |
| 42 | + |
| 43 | +// Shared by: |
| 44 | +// https://wicg.github.io/compression/#decompress-and-enqueue-a-chunk |
| 45 | +// https://wicg.github.io/compression/#decompress-flush-and-enqueue |
| 46 | +// All data errors throw TypeError by step 2: If this results in an error, |
| 47 | +// then throw a TypeError. |
| 48 | +bool BrotliDecompressionStreamAlgorithms::Decompress( |
| 49 | + JSContext* aCx, Span<const uint8_t> aInput, |
| 50 | + JS::MutableHandleVector<JSObject*> aOutput, Flush aFlush, |
| 51 | + ErrorResult& aRv) { |
| 52 | + size_t inputLength = aInput.Length(); |
| 53 | + const uint8_t* inputBuffer = aInput.Elements(); |
| 54 | + |
| 55 | + do { |
| 56 | + std::unique_ptr<uint8_t[], JS::FreePolicy> buffer( |
| 57 | + static_cast<uint8_t*>(JS_malloc(aCx, kBufferSize))); |
| 58 | + if (!buffer) { |
| 59 | + aRv.ThrowTypeError("Out of memory"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + size_t outputLength = kBufferSize; |
| 64 | + uint8_t* outputBuffer = buffer.get(); |
| 65 | + BrotliDecoderResult rv = |
| 66 | + BrotliDecoderDecompressStream(mState.get(), &inputLength, &inputBuffer, |
| 67 | + &outputLength, &outputBuffer, nullptr); |
| 68 | + switch (rv) { |
| 69 | + case BROTLI_DECODER_RESULT_ERROR: |
| 70 | + aRv.ThrowTypeError("Brotli decompression error: "_ns + |
| 71 | + nsDependentCString(BrotliDecoderErrorString( |
| 72 | + BrotliDecoderGetErrorCode(mState.get())))); |
| 73 | + return false; |
| 74 | + case BROTLI_DECODER_RESULT_SUCCESS: |
| 75 | + mObservedStreamEnd = true; |
| 76 | + break; |
| 77 | + case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: |
| 78 | + case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: |
| 79 | + break; |
| 80 | + default: |
| 81 | + MOZ_ASSERT_UNREACHABLE("Unexpected decompression error code"); |
| 82 | + aRv.ThrowTypeError("Unexpected decompression error"); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // Step 3: If buffer is empty, return. |
| 87 | + // (We'll implicitly return when the array is empty.) |
| 88 | + |
| 89 | + // Step 4: Split buffer into one or more non-empty pieces and convert them |
| 90 | + // into Uint8Arrays. |
| 91 | + // (The buffer is 'split' by having a fixed sized buffer above.) |
| 92 | + |
| 93 | + size_t written = kBufferSize - outputLength; |
| 94 | + if (written > 0) { |
| 95 | + JS::Rooted<JSObject*> view(aCx, nsJSUtils::MoveBufferAsUint8Array( |
| 96 | + aCx, written, std::move(buffer))); |
| 97 | + if (!view || !aOutput.append(view)) { |
| 98 | + JS_ClearPendingException(aCx); |
| 99 | + aRv.ThrowTypeError("Out of memory"); |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + } while (BrotliDecoderHasMoreOutput(mState.get())); |
| 104 | + |
| 105 | + return inputLength == 0; |
| 106 | +} |
| 107 | + |
| 108 | +void BrotliDecompressionStreamAlgorithms::BrotliDeleter::operator()( |
| 109 | + BrotliDecoderStateStruct* aState) { |
| 110 | + BrotliDecoderDestroyInstance(aState); |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace mozilla::dom::compression |
0 commit comments