File: FormatBrotli.cpp

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (210 lines) | stat: -rw-r--r-- 7,541 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FormatBrotli.h"

#include <memory>

#include "BaseAlgorithms.h"
#include "brotli/decode.h"
#include "brotli/encode.h"
#include "mozilla/dom/TransformStreamDefaultController.h"

namespace mozilla::dom::compression {

NS_IMPL_CYCLE_COLLECTION_INHERITED(BrotliCompressionStreamAlgorithms,
                                   TransformerAlgorithmsBase)
NS_IMPL_ADDREF_INHERITED(BrotliCompressionStreamAlgorithms,
                         TransformerAlgorithmsBase)
NS_IMPL_RELEASE_INHERITED(BrotliCompressionStreamAlgorithms,
                          TransformerAlgorithmsBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrotliCompressionStreamAlgorithms)
NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase)

NS_IMPL_CYCLE_COLLECTION_INHERITED(BrotliDecompressionStreamAlgorithms,
                                   TransformerAlgorithmsBase)
NS_IMPL_ADDREF_INHERITED(BrotliDecompressionStreamAlgorithms,
                         TransformerAlgorithmsBase)
NS_IMPL_RELEASE_INHERITED(BrotliDecompressionStreamAlgorithms,
                          TransformerAlgorithmsBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrotliDecompressionStreamAlgorithms)
NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase)

inline BrotliEncoderOperation intoBrotliOp(Flush aFlush) {
  switch (aFlush) {
    case Flush::No: {
      return BROTLI_OPERATION_PROCESS;
    }
    case Flush::Yes: {
      return BROTLI_OPERATION_FINISH;
    }
    default: {
      MOZ_ASSERT_UNREACHABLE("Unknown flush mode");
      return BROTLI_OPERATION_PROCESS;
    }
  }
}

Result<already_AddRefed<BrotliCompressionStreamAlgorithms>, nsresult>
BrotliCompressionStreamAlgorithms::Create() {
  RefPtr<BrotliCompressionStreamAlgorithms> alg =
      new BrotliCompressionStreamAlgorithms();
  MOZ_TRY(alg->Init());
  return alg.forget();
}

[[nodiscard]] nsresult BrotliCompressionStreamAlgorithms::Init() {
  mState = std::unique_ptr<BrotliEncoderStateStruct, BrotliDeleter>(
      BrotliEncoderCreateInstance(nullptr, nullptr, nullptr));
  if (!mState) {
    return NS_ERROR_OUT_OF_MEMORY;
  }
  return NS_OK;
}

// Shared by:
// https://wicg.github.io/compression/#compress-and-enqueue-a-chunk
// https://wicg.github.io/compression/#compress-flush-and-enqueue
// All data errors throw TypeError by step 2: If this results in an error,
// then throw a TypeError.
void BrotliCompressionStreamAlgorithms::Compress(
    JSContext* aCx, Span<const uint8_t> aInput,
    JS::MutableHandleVector<JSObject*> aOutput, Flush aFlush,
    ErrorResult& aRv) {
  size_t inputLength = aInput.Length();
  const uint8_t* inputBuffer = aInput.Elements();

  do {
    std::unique_ptr<uint8_t[], JS::FreePolicy> buffer(
        static_cast<uint8_t*>(JS_malloc(aCx, kBufferSize)));
    if (!buffer) {
      aRv.ThrowTypeError("Out of memory");
      return;
    }

    size_t outputLength = kBufferSize;
    uint8_t* outputBuffer = buffer.get();
    bool succeeded = BrotliEncoderCompressStream(
        mState.get(), intoBrotliOp(aFlush), &inputLength, &inputBuffer,
        &outputLength, &outputBuffer, nullptr);
    if (!succeeded) {
      aRv.ThrowTypeError("Unexpected compression error");
      return;
    }

    // Step 3: If buffer is empty, return.
    // (We'll implicitly return when the array is empty.)

    // Step 4: Split buffer into one or more non-empty pieces and convert them
    // into Uint8Arrays.
    // (The buffer is 'split' by having a fixed sized buffer above.)

    size_t written = kBufferSize - outputLength;
    if (written > 0) {
      JS::Rooted<JSObject*> view(aCx, nsJSUtils::MoveBufferAsUint8Array(
                                          aCx, written, std::move(buffer)));
      if (!view || !aOutput.append(view)) {
        JS_ClearPendingException(aCx);
        aRv.ThrowTypeError("Out of memory");
        return;
      }
    }
  } while (BrotliEncoderHasMoreOutput(mState.get()));
}

void BrotliCompressionStreamAlgorithms::BrotliDeleter::operator()(
    BrotliEncoderStateStruct* aState) {
  BrotliEncoderDestroyInstance(aState);
}

Result<already_AddRefed<BrotliDecompressionStreamAlgorithms>, nsresult>
BrotliDecompressionStreamAlgorithms::Create() {
  RefPtr<BrotliDecompressionStreamAlgorithms> alg =
      new BrotliDecompressionStreamAlgorithms();
  MOZ_TRY(alg->Init());
  return alg.forget();
}

[[nodiscard]] nsresult BrotliDecompressionStreamAlgorithms::Init() {
  mState = std::unique_ptr<BrotliDecoderStateStruct, BrotliDeleter>(
      BrotliDecoderCreateInstance(nullptr, nullptr, nullptr));
  if (!mState) {
    return NS_ERROR_OUT_OF_MEMORY;
  }
  return NS_OK;
}

// Shared by:
// https://wicg.github.io/compression/#decompress-and-enqueue-a-chunk
// https://wicg.github.io/compression/#decompress-flush-and-enqueue
// All data errors throw TypeError by step 2: If this results in an error,
// then throw a TypeError.
bool BrotliDecompressionStreamAlgorithms::Decompress(
    JSContext* aCx, Span<const uint8_t> aInput,
    JS::MutableHandleVector<JSObject*> aOutput, Flush aFlush,
    ErrorResult& aRv) {
  size_t inputLength = aInput.Length();
  const uint8_t* inputBuffer = aInput.Elements();

  do {
    std::unique_ptr<uint8_t[], JS::FreePolicy> buffer(
        static_cast<uint8_t*>(JS_malloc(aCx, kBufferSize)));
    if (!buffer) {
      aRv.ThrowTypeError("Out of memory");
      return false;
    }

    size_t outputLength = kBufferSize;
    uint8_t* outputBuffer = buffer.get();
    BrotliDecoderResult rv =
        BrotliDecoderDecompressStream(mState.get(), &inputLength, &inputBuffer,
                                      &outputLength, &outputBuffer, nullptr);
    switch (rv) {
      case BROTLI_DECODER_RESULT_ERROR:
        aRv.ThrowTypeError("Brotli decompression error: "_ns +
                           nsDependentCString(BrotliDecoderErrorString(
                               BrotliDecoderGetErrorCode(mState.get()))));
        return false;
      case BROTLI_DECODER_RESULT_SUCCESS:
        mObservedStreamEnd = true;
        break;
      case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
      case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
        break;
      default:
        MOZ_ASSERT_UNREACHABLE("Unexpected decompression error code");
        aRv.ThrowTypeError("Unexpected decompression error");
        return false;
    }

    // Step 3: If buffer is empty, return.
    // (We'll implicitly return when the array is empty.)

    // Step 4: Split buffer into one or more non-empty pieces and convert them
    // into Uint8Arrays.
    // (The buffer is 'split' by having a fixed sized buffer above.)

    size_t written = kBufferSize - outputLength;
    if (written > 0) {
      JS::Rooted<JSObject*> view(aCx, nsJSUtils::MoveBufferAsUint8Array(
                                          aCx, written, std::move(buffer)));
      if (!view || !aOutput.append(view)) {
        JS_ClearPendingException(aCx);
        aRv.ThrowTypeError("Out of memory");
        return false;
      }
    }
  } while (BrotliDecoderHasMoreOutput(mState.get()));

  return inputLength == 0;
}

void BrotliDecompressionStreamAlgorithms::BrotliDeleter::operator()(
    BrotliDecoderStateStruct* aState) {
  BrotliDecoderDestroyInstance(aState);
}

}  // namespace mozilla::dom::compression