mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add OutputBitStream class
This will be used in the deflate compressor.
This commit is contained in:
parent
5a0ebdb109
commit
0ddc0e45ae
Notes:
sideshowbarker
2024-07-18 21:24:40 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/0ddc0e45aec Pull-request: https://github.com/SerenityOS/serenity/pull/5750 Issue: https://github.com/SerenityOS/serenity/issues/3989 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/bcoles
2 changed files with 82 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -121,6 +122,85 @@ private:
|
|||
InputStream& m_stream;
|
||||
};
|
||||
|
||||
class OutputBitStream final : public OutputStream {
|
||||
public:
|
||||
explicit OutputBitStream(OutputStream& stream)
|
||||
: m_stream(stream)
|
||||
{
|
||||
}
|
||||
|
||||
// WARNING: write aligns to the next byte boundary before writing, if unaligned writes are needed this should be rewritten
|
||||
size_t write(ReadonlyBytes bytes) override
|
||||
{
|
||||
if (has_any_error())
|
||||
return 0;
|
||||
align_to_byte_boundary();
|
||||
if (has_fatal_error()) // if align_to_byte_boundary failed
|
||||
return 0;
|
||||
return m_stream.write(bytes);
|
||||
}
|
||||
|
||||
bool write_or_error(ReadonlyBytes bytes) override
|
||||
{
|
||||
if (write(bytes) < bytes.size()) {
|
||||
set_fatal_error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void write_bits(u32 bits, size_t count)
|
||||
{
|
||||
VERIFY(count <= 32);
|
||||
size_t n_written = 0;
|
||||
while (n_written < count) {
|
||||
if (m_stream.has_any_error()) {
|
||||
set_fatal_error();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_next_byte.has_value()) {
|
||||
m_next_byte.value() |= ((bits >> n_written) & 1) << m_bit_offset;
|
||||
++n_written;
|
||||
|
||||
if (m_bit_offset++ == 7) {
|
||||
m_stream << m_next_byte.value();
|
||||
m_next_byte.clear();
|
||||
}
|
||||
} else {
|
||||
m_bit_offset = 0;
|
||||
m_next_byte = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void write_bit(bool bit)
|
||||
{
|
||||
write_bits(bit, 1);
|
||||
}
|
||||
|
||||
void align_to_byte_boundary()
|
||||
{
|
||||
if (m_next_byte.has_value()) {
|
||||
if (!m_stream.write_or_error(ReadonlyBytes { &m_next_byte.value(), 1 })) {
|
||||
set_fatal_error();
|
||||
}
|
||||
m_next_byte.clear();
|
||||
}
|
||||
}
|
||||
|
||||
size_t bit_offset() const
|
||||
{
|
||||
return m_bit_offset;
|
||||
}
|
||||
|
||||
private:
|
||||
Optional<u8> m_next_byte;
|
||||
size_t m_bit_offset { 0 };
|
||||
OutputStream& m_stream;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::InputBitStream;
|
||||
using AK::OutputBitStream;
|
||||
|
|
|
@ -51,6 +51,7 @@ class InputMemoryStream;
|
|||
class DuplexMemoryStream;
|
||||
class OutputStream;
|
||||
class InputBitStream;
|
||||
class OutputBitStream;
|
||||
class OutputMemoryStream;
|
||||
|
||||
template<size_t Capacity>
|
||||
|
@ -160,6 +161,7 @@ using AK::NonnullOwnPtrVector;
|
|||
using AK::NonnullRefPtr;
|
||||
using AK::NonnullRefPtrVector;
|
||||
using AK::Optional;
|
||||
using AK::OutputBitStream;
|
||||
using AK::OutputMemoryStream;
|
||||
using AK::OutputStream;
|
||||
using AK::OwnPtr;
|
||||
|
|
Loading…
Reference in a new issue