1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #include <woff2/decode.h>
9 #include "RLBoxWOFF2Sandbox.h"
11 bool RLBoxConvertWOFF2ToTTF(const char* aData
, unsigned long aLength
,
12 unsigned long aDecompressedSize
,
13 unsigned long* aResultSize
, void** aResultOwningStr
,
15 std::unique_ptr
<std::string
> buf
=
16 std::make_unique
<std::string
>(aDecompressedSize
, 0);
17 woff2::WOFF2StringOut
out(buf
.get());
18 if (!woff2::ConvertWOFF2ToTTF(reinterpret_cast<const uint8_t*>(aData
),
22 *aResultSize
= out
.Size();
23 // Return the string and its underlying C string. We need both to make sure we
24 // can free the string (which we do with RLBoxDeleteWOFF2String).
25 *aResultData
= buf
->data();
26 *aResultOwningStr
= static_cast<void*>(buf
.release());
30 void RLBoxDeleteWOFF2String(void** aStr
) {
31 std::string
* buf
= static_cast<std::string
*>(*aStr
);
35 BrotliDecompressCallback
* sRLBoxBrotliDecompressCallback
= nullptr;
37 void RegisterWOFF2Callback(BrotliDecompressCallback
* aCallback
) {
38 #ifdef MOZ_IN_WASM_SANDBOX
39 // When Woff2 is wasmboxed, we need to register a callback for brotli
40 // decompression. The easiest way to store this is in a static variable. This
41 // is thread-safe because each (potentially-concurrent) woff2 instance gets
42 // its own sandbox with its own copy of the statics.
44 // When the sandbox is disabled (replaced with the noop sandbox), setting the
45 // callback is actually racey. However, we don't actually need a callback in
46 // that case, and can just invoke brotli directly.
47 sRLBoxBrotliDecompressCallback
= aCallback
;
51 BrotliDecoderResult
RLBoxBrotliDecoderDecompress(size_t aEncodedSize
,
52 const uint8_t* aEncodedBuffer
,
54 uint8_t* aDecodedBuffer
) {
55 #ifdef MOZ_IN_WASM_SANDBOX
56 assert(sRLBoxBrotliDecompressCallback
);
57 return sRLBoxBrotliDecompressCallback(
58 aEncodedSize
, reinterpret_cast<const char*>(aEncodedBuffer
), aDecodedSize
,
59 reinterpret_cast<char*>(aDecodedBuffer
));
61 return BrotliDecoderDecompress(aEncodedSize
, aEncodedBuffer
, aDecodedSize
,