summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCompress/Gzip.cpp
blob: 644718acfcba327e247601ae3871f41eab4f2c6b (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (c) 2020, the SerenityOS developers.
 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCompress/Gzip.h>

#include <AK/MemoryStream.h>
#include <AK/String.h>
#include <LibCore/DateTime.h>

namespace Compress {

bool GzipDecompressor::is_likely_compressed(ReadonlyBytes bytes)
{
    return bytes.size() >= 2 && bytes[0] == gzip_magic_1 && bytes[1] == gzip_magic_2;
}

bool BlockHeader::valid_magic_number() const
{
    return identification_1 == gzip_magic_1 && identification_2 == gzip_magic_2;
}

bool BlockHeader::supported_by_implementation() const
{
    if (compression_method != 0x08) {
        // RFC 1952 does not define any compression methods other than deflate.
        return false;
    }

    if (flags > Flags::MAX) {
        // RFC 1952 does not define any more flags.
        return false;
    }

    return true;
}

GzipDecompressor::GzipDecompressor(InputStream& stream)
    : m_input_stream(stream)
{
}

GzipDecompressor::~GzipDecompressor()
{
    m_current_member.clear();
}

// FIXME: Again, there are surely a ton of bugs because the code doesn't check for read errors.
size_t GzipDecompressor::read(Bytes bytes)
{
    size_t total_read = 0;
    while (total_read < bytes.size()) {
        if (has_any_error() || m_eof)
            break;

        auto slice = bytes.slice(total_read);

        if (m_current_member.has_value()) {
            size_t nread = current_member().m_stream.read(slice);
            current_member().m_checksum.update(slice.trim(nread));
            current_member().m_nread += nread;

            if (current_member().m_stream.handle_any_error()) {
                set_fatal_error();
                break;
            }

            if (nread < slice.size()) {
                LittleEndian<u32> crc32, input_size;
                m_input_stream >> crc32 >> input_size;

                if (crc32 != current_member().m_checksum.digest()) {
                    // FIXME: Somehow the checksum is incorrect?

                    set_fatal_error();
                    break;
                }

                if (input_size != current_member().m_nread) {
                    set_fatal_error();
                    break;
                }

                m_current_member.clear();

                total_read += nread;
                continue;
            }

            total_read += nread;
            continue;
        } else {
            m_partial_header_offset += m_input_stream.read(Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset));

            if (m_input_stream.handle_any_error() || m_input_stream.unreliable_eof()) {
                m_eof = true;
                break;
            }

            if (m_partial_header_offset < sizeof(BlockHeader)) {
                break; // partial header read
            }
            m_partial_header_offset = 0;

            BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header));

            if (!header.valid_magic_number() || !header.supported_by_implementation()) {
                set_fatal_error();
                break;
            }

            if (header.flags & Flags::FEXTRA) {
                LittleEndian<u16> subfield_id, length;
                m_input_stream >> subfield_id >> length;
                m_input_stream.discard_or_error(length);
            }

            auto discard_string = [&]() {
                char next_char;
                do {
                    m_input_stream >> next_char;
                    if (m_input_stream.has_any_error()) {
                        set_fatal_error();
                        break;
                    }
                } while (next_char);
            };

            if (header.flags & Flags::FNAME) {
                discard_string();
                if (has_any_error())
                    break;
            }

            if (header.flags & Flags::FCOMMENT) {
                discard_string();
                if (has_any_error())
                    break;
            }

            if (header.flags & Flags::FHCRC) {
                LittleEndian<u16> crc16;
                m_input_stream >> crc16;
                // FIXME: we should probably verify this instead of just assuming it matches
            }

            m_current_member.emplace(header, m_input_stream);
            continue;
        }
    }
    return total_read;
}

Optional<String> GzipDecompressor::describe_header(ReadonlyBytes bytes)
{
    if (bytes.size() < sizeof(BlockHeader))
        return {};

    auto& header = *(reinterpret_cast<const BlockHeader*>(bytes.data()));
    if (!header.valid_magic_number() || !header.supported_by_implementation())
        return {};

    LittleEndian<u32> original_size = *reinterpret_cast<const u32*>(bytes.offset(bytes.size() - sizeof(u32)));
    return String::formatted("last modified: {}, original size {}", Core::DateTime::from_timestamp(header.modification_time).to_string(), (u32)original_size);
}

bool GzipDecompressor::read_or_error(Bytes bytes)
{
    if (read(bytes) < bytes.size()) {
        set_fatal_error();
        return false;
    }

    return true;
}

bool GzipDecompressor::discard_or_error(size_t count)
{
    u8 buffer[4096];

    size_t ndiscarded = 0;
    while (ndiscarded < count) {
        if (unreliable_eof()) {
            set_fatal_error();
            return false;
        }

        ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
    }

    return true;
}

Optional<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
{
    InputMemoryStream memory_stream { bytes };
    GzipDecompressor gzip_stream { memory_stream };
    DuplexMemoryStream output_stream;

    u8 buffer[4096];
    while (!gzip_stream.has_any_error() && !gzip_stream.unreliable_eof()) {
        const auto nread = gzip_stream.read({ buffer, sizeof(buffer) });
        output_stream.write_or_error({ buffer, nread });
    }

    if (gzip_stream.handle_any_error())
        return {};

    return output_stream.copy_into_contiguous_buffer();
}

bool GzipDecompressor::unreliable_eof() const { return m_eof; }

bool GzipDecompressor::handle_any_error()
{
    bool handled_errors = m_input_stream.handle_any_error();
    return Stream::handle_any_error() || handled_errors;
}

GzipCompressor::GzipCompressor(OutputStream& stream)
    : m_output_stream(stream)
{
}

GzipCompressor::~GzipCompressor()
{
}

size_t GzipCompressor::write(ReadonlyBytes bytes)
{
    BlockHeader header;
    header.identification_1 = 0x1f;
    header.identification_2 = 0x8b;
    header.compression_method = 0x08;
    header.flags = 0;
    header.modification_time = 0;
    header.extra_flags = 3;      // DEFLATE sets 2 for maximum compression and 4 for minimum compression
    header.operating_system = 3; // unix
    m_output_stream << Bytes { &header, sizeof(header) };
    DeflateCompressor compressed_stream { m_output_stream };
    VERIFY(compressed_stream.write_or_error(bytes));
    compressed_stream.final_flush();
    Crypto::Checksum::CRC32 crc32;
    crc32.update(bytes);
    LittleEndian<u32> digest = crc32.digest();
    LittleEndian<u32> size = bytes.size();
    m_output_stream << digest << size;
    return bytes.size();
}

bool GzipCompressor::write_or_error(ReadonlyBytes bytes)
{
    if (write(bytes) < bytes.size()) {
        set_fatal_error();
        return false;
    }

    return true;
}

Optional<ByteBuffer> GzipCompressor::compress_all(const ReadonlyBytes& bytes)
{
    DuplexMemoryStream output_stream;
    GzipCompressor gzip_stream { output_stream };

    gzip_stream.write_or_error(bytes);

    if (gzip_stream.handle_any_error())
        return {};

    return output_stream.copy_into_contiguous_buffer();
}

}