Commit graph

14 commits

Author SHA1 Message Date
Pavel Shliak
d0c0db5bb3 LibCompress: Clean up #include directives
This change aims to improve the speed of incremental builds.
2024-11-20 15:17:31 +01:00
Timothy Flynn
355ce72c06 LibCompress: Allow using GzipCompressor in a streaming fashion
GzipCompressor is currently written assuming that it's write_some method
is only called once. When we use this class for LibWeb, we may very well
receive data to compress in small chunks. So this patch makes us write
the gzip header and footer only once, which now resembles the zlib and
deflate compressors.
2024-11-17 22:37:45 +01:00
Timothy Flynn
93712b24bf Everywhere: Hoist the Libraries folder to the top-level 2024-11-10 12:50:45 +01:00
Andreas Kling
13d7c09125 Libraries: Move to Userland/Libraries/ 2021-01-12 12:17:46 +01:00
asynts
f18e927827 AK: Remove OutputMemoryStream for DuplexMemoryStream.
OutputMemoryStream was originally a proxy for DuplexMemoryStream that
did not expose any reading API.

Now I need to add another class that is like OutputMemoryStream but only
for static buffers. My first idea was to make OutputMemoryStream do that
too, but I think it's much better to have a distinct class for that.

I originally wanted to call that class FixedOutputMemoryStream but that
name is really cumbersome and it's a bit unintuitive because
InputMemoryStream is already reading from a fixed buffer.

So let's just use DuplexMemoryStream instead of OutputMemoryStream for
any dynamic stuff and create a new OutputMemoryStream for static
buffers.
2020-09-15 20:36:45 +02:00
asynts
96edcbc27c AK: Lower the requirements for InputStream::eof and rename it.
Consider the following snippet:

    void foo(InputStream& stream) {
        if(!stream.eof()) {
            u8 byte;
            stream >> byte;
        }
    }

There is a very subtle bug in this snippet, for some input streams eof()
might return false even if no more data can be read. In this case an
error flag would be set on the stream.

Until now I've always ensured that this is not the case, but this made
the implementation of eof() unnecessarily complicated.
InputFileStream::eof had to keep a ByteBuffer around just to make this
possible. That meant a ton of unnecessary copies just to get a reliable
eof().

In most cases it isn't actually necessary to have a reliable eof()
implementation.

In most other cases a reliable eof() is avaliable anyways because in
some cases like InputMemoryStream it is very easy to implement.
2020-09-14 20:58:12 +02:00
asynts
4af8eea56f LibCompress: Return Optional from decompress_all method. 2020-09-11 16:07:45 +02:00
asynts
7e02cad476 LibCompress: Use OutputMemoryStream in decompress_all methods. 2020-09-11 16:07:45 +02:00
asynts
6de63782c7 Streams: Consistent behaviour when reading from stream with error.
The streaming operator doesn't short-circuit, consider the following
snippet:

    void foo(InputStream& stream) {
        int a, b;
        stream >> a >> b;
    }

If the first read fails, the second is called regardless. It should be
well defined what happens in this case: nothing.
2020-09-06 12:54:45 +02:00
asynts
5d85be7ed4 LibCompress: Add another unit test.
I suspected an error in CircularDuplexStream::read(Bytes, size_t). This
does not appear to be the case, this test case is useful regardless.

The following script was used to generate the test:

    import gzip

    uncompressed = []
    for _ in range(0x100):
        uncompressed.append(1)
    for _ in range(0x7e00):
        uncompressed.append(0)
    for _ in range(0x100):
        uncompressed.append(1)

    compressed = gzip.compress(bytes(uncompressed))
    compressed = ", ".join(f"0x{byte:02x}" for byte in compressed)

    print(f"""\
    TEST_CASE(gzip_decompress_repeat_around_buffer)
    {{
        const u8 compressed[] = {{
            {compressed}
        }};

        u8 uncompressed[0x8011];
        Bytes{{ uncompressed, sizeof(uncompressed) }}.fill(0);
        uncompressed[0x8000] = 1;

        const auto decompressed = Compress::GzipDecompressor::decompress_all({{ compressed, sizeof(compressed) }});

        EXPECT(compare({{ uncompressed, sizeof(uncompressed) }}, decompressed.bytes()));
    }}
    """, end="")
2020-09-06 12:54:45 +02:00
asynts
e2e2e782d4 Deflate: Fix deadly typo. 2020-09-06 12:54:45 +02:00
asynts
b68a873067 AK: Move memory streams into their own header. 2020-09-01 17:25:26 +02:00
asynts
9ce4475907 Streams: Distinguish recoverable and fatal errors. 2020-09-01 17:25:26 +02:00
asynts
3f1dfc2e97 LibCompress: Implement gzip. 2020-08-30 09:56:10 +02:00