Skip to content

Commit

Permalink
Use std::span more in Modules/compression
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271467

Reviewed by Darin Adler.

* Source/WebCore/Modules/compression/CompressionStreamEncoder.cpp:
(WebCore::CompressionStreamEncoder::encode):
(WebCore::CompressionStreamEncoder::flush):
(WebCore::CompressionStreamEncoder::compress):
* Source/WebCore/Modules/compression/CompressionStreamEncoder.h:
* Source/WebCore/Modules/compression/DecompressionStreamDecoder.cpp:
(WebCore::DecompressionStreamDecoder::decode):
(WebCore::DecompressionStreamDecoder::flush):
(WebCore::DecompressionStreamDecoder::decompress):
(WebCore::DecompressionStreamDecoder::decompressZlib):
(WebCore::DecompressionStreamDecoder::decompressAppleCompressionFramework):
* Source/WebCore/Modules/compression/DecompressionStreamDecoder.h:

Canonical link: https://commits.webkit.org/276595@main
  • Loading branch information
cdumez committed Mar 23, 2024
1 parent c6bbd69 commit 30df9b6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
16 changes: 6 additions & 10 deletions Source/WebCore/Modules/compression/CompressionStreamEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ namespace WebCore {

ExceptionOr<RefPtr<Uint8Array>> CompressionStreamEncoder::encode(const BufferSource&& input)
{
auto* data = input.data();
if (!data)
return Exception { ExceptionCode::TypeError, "No data provided"_s };

auto compressedDataCheck = compress(data, input.length());
auto compressedDataCheck = compress(input.span());
if (compressedDataCheck.hasException())
return compressedDataCheck.releaseException();

Expand All @@ -55,7 +51,7 @@ ExceptionOr<RefPtr<Uint8Array>> CompressionStreamEncoder::flush()
{
m_didFinish = true;

auto compressedDataCheck = compress(0, 0);
auto compressedDataCheck = compress({ });
if (compressedDataCheck.hasException())
return compressedDataCheck.releaseException();

Expand Down Expand Up @@ -112,16 +108,16 @@ static bool didDeflateFail(int result)
return result != Z_OK && result != Z_STREAM_END && result != Z_BUF_ERROR;
}

ExceptionOr<RefPtr<JSC::ArrayBuffer>> CompressionStreamEncoder::compress(const uint8_t* input, const size_t inputLength)
ExceptionOr<RefPtr<JSC::ArrayBuffer>> CompressionStreamEncoder::compress(std::span<const uint8_t> input)
{
size_t allocateSize = (inputLength < startingAllocationSize) ? startingAllocationSize : inputLength;
size_t allocateSize = std::max(input.size(), startingAllocationSize);
auto storage = SharedBufferBuilder();

int result;
bool shouldCompress = true;

m_zstream.next_in = const_cast<z_const Bytef*>(input);
m_zstream.avail_in = inputLength;
m_zstream.next_in = const_cast<z_const Bytef*>(input.data());
m_zstream.avail_in = input.size();

if (!m_initialized) {
auto initializeResult = initialize();
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/Modules/compression/CompressionStreamEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CompressionStreamEncoder : public RefCounted<CompressionStreamEncoder> {
return adoptRef(*new CompressionStreamEncoder(format));
}

ExceptionOr<RefPtr<Uint8Array>> encode(const BufferSource&& input);
ExceptionOr<RefPtr<Uint8Array>> encode(const BufferSource&&);
ExceptionOr<RefPtr<Uint8Array>> flush();

~CompressionStreamEncoder()
Expand All @@ -54,7 +54,7 @@ class CompressionStreamEncoder : public RefCounted<CompressionStreamEncoder> {
private:
bool didDeflateFinish(int) const;

ExceptionOr<RefPtr<JSC::ArrayBuffer>> compress(const uint8_t* input, const size_t inputLength);
ExceptionOr<RefPtr<JSC::ArrayBuffer>> compress(std::span<const uint8_t>);
ExceptionOr<bool> initialize();

explicit CompressionStreamEncoder(unsigned char format)
Expand Down
24 changes: 10 additions & 14 deletions Source/WebCore/Modules/compression/DecompressionStreamDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ namespace WebCore {

ExceptionOr<RefPtr<Uint8Array>> DecompressionStreamDecoder::decode(const BufferSource&& input)
{
auto* data = input.data();
if (!data)
return Exception { ExceptionCode::TypeError, "No data provided"_s };

auto compressedDataCheck = decompress(data, input.length());
auto compressedDataCheck = decompress(input.span());
if (compressedDataCheck.hasException())
return compressedDataCheck.releaseException();

Expand All @@ -53,7 +49,7 @@ ExceptionOr<RefPtr<Uint8Array>> DecompressionStreamDecoder::flush()
{
m_didFinish = true;

auto compressedDataCheck = decompress(0, 0);
auto compressedDataCheck = decompress({ });
if (compressedDataCheck.hasException())
return compressedDataCheck.releaseException();

Expand All @@ -64,9 +60,9 @@ ExceptionOr<RefPtr<Uint8Array>> DecompressionStreamDecoder::flush()
return Uint8Array::tryCreate(static_cast<uint8_t *>(compressedData->data()), compressedData->byteLength());
}

inline ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompress(const uint8_t* input, const size_t inputLength)
inline ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompress(std::span<const uint8_t> input)
{
return decompressZlib(input, inputLength);
return decompressZlib(input);
}

ExceptionOr<bool> DecompressionStreamDecoder::initialize()
Expand Down Expand Up @@ -117,16 +113,16 @@ bool DecompressionStreamDecoder::didInflateContainExtraBytes(int result) const
return (result == Z_STREAM_END && m_zstream.avail_in) || (result == Z_BUF_ERROR && m_didFinish);
}

ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompressZlib(const uint8_t* input, const size_t inputLength)
ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompressZlib(std::span<const uint8_t> input)
{
size_t allocateSize = startingAllocationSize;
auto storage = SharedBufferBuilder();

int result;
bool shouldDecompress = true;

m_zstream.next_in = const_cast<z_const Bytef*>(input);
m_zstream.avail_in = inputLength;
m_zstream.next_in = const_cast<z_const Bytef*>(input.data());
m_zstream.avail_in = input.size();

if (!m_initialized) {
auto initializeResult = initialize();
Expand Down Expand Up @@ -188,7 +184,7 @@ ExceptionOr<bool> DecompressionStreamDecoder::initializeAppleCompressionFramewor
return true;
}

ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompressAppleCompressionFramework(const uint8_t* input, const size_t inputLength)
ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompressAppleCompressionFramework(std::span<const uint8_t> input)
{
size_t allocateSize = startingAllocationSize;
auto storage = SharedBufferBuilder();
Expand All @@ -202,8 +198,8 @@ ExceptionOr<RefPtr<JSC::ArrayBuffer>> DecompressionStreamDecoder::decompressAppl
return initializeResult.releaseException();
}

m_stream.src_ptr = input;
m_stream.src_size = inputLength;
m_stream.src_ptr = input.data();
m_stream.src_size = input.size();

while (shouldDecompress) {
Vector<uint8_t> output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DecompressionStreamDecoder : public RefCounted<DecompressionStreamDecoder>
bool didInflateFinish(int) const;
bool didInflateContainExtraBytes(int) const;

ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompressZlib(const uint8_t* input, const size_t inputLength);
ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompressZlib(std::span<const uint8_t>);
ExceptionOr<bool> initialize();

explicit DecompressionStreamDecoder(unsigned char format)
Expand All @@ -89,11 +89,11 @@ class DecompressionStreamDecoder : public RefCounted<DecompressionStreamDecoder>

bool m_usingAppleCompressionFramework { false };

inline ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompress(const uint8_t* input, const size_t inputLength);
inline ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompress(std::span<const uint8_t>);

#if PLATFORM(COCOA)
compression_stream m_stream;
ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompressAppleCompressionFramework(const uint8_t* input, const size_t inputLength);
ExceptionOr<RefPtr<JSC::ArrayBuffer>> decompressAppleCompressionFramework(std::span<const uint8_t>);
ExceptionOr<bool> initializeAppleCompressionFramework();
#endif

Expand Down

0 comments on commit 30df9b6

Please sign in to comment.