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
|
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/IntegralMath.h>
#include <AK/MemoryStream.h>
#include <AK/Span.h>
#include <AK/TypeCasts.h>
#include <AK/Types.h>
#include <LibCompress/Deflate.h>
#include <LibCompress/Zlib.h>
namespace Compress {
constexpr static size_t Adler32Size = sizeof(u32);
Optional<ZlibDecompressor> ZlibDecompressor::try_create(ReadonlyBytes data)
{
if (data.size() < (sizeof(ZlibHeader) + Adler32Size))
return {};
ZlibHeader header { .as_u16 = data.at(0) << 8 | data.at(1) };
if (header.compression_method != ZlibCompressionMethod::Deflate || header.compression_info > 7)
return {}; // non-deflate compression
if (header.present_dictionary)
return {}; // we dont support pre-defined dictionaries
if (header.as_u16 % 31 != 0)
return {}; // error correction code doesn't match
ZlibDecompressor zlib { header, data };
zlib.m_data_bytes = data.slice(2, data.size() - sizeof(ZlibHeader) - Adler32Size);
return zlib;
}
ZlibDecompressor::ZlibDecompressor(ZlibHeader header, ReadonlyBytes data)
: m_header(header)
, m_input_data(data)
{
}
Optional<ByteBuffer> ZlibDecompressor::decompress()
{
auto buffer_or_error = DeflateDecompressor::decompress_all(m_data_bytes);
if (buffer_or_error.is_error())
return {};
return buffer_or_error.release_value();
}
Optional<ByteBuffer> ZlibDecompressor::decompress_all(ReadonlyBytes bytes)
{
auto zlib = try_create(bytes);
if (!zlib.has_value())
return {};
return zlib->decompress();
}
u32 ZlibDecompressor::checksum()
{
if (!m_checksum) {
auto bytes = m_input_data.slice_from_end(Adler32Size);
m_checksum = bytes.at(0) << 24 | bytes.at(1) << 16 | bytes.at(2) << 8 || bytes.at(3);
}
return m_checksum;
}
ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(MaybeOwned<Stream> stream, ZlibCompressionLevel compression_level)
{
// Zlib only defines Deflate as a compression method.
auto compression_method = ZlibCompressionMethod::Deflate;
// FIXME: Find a way to compress with Deflate's "Best" compression level.
auto compressor_stream = TRY(DeflateCompressor::construct(MaybeOwned(*stream), static_cast<DeflateCompressor::CompressionLevel>(compression_level)));
auto zlib_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ZlibCompressor(move(stream), move(compressor_stream))));
TRY(zlib_compressor->write_header(compression_method, compression_level));
return zlib_compressor;
}
ZlibCompressor::ZlibCompressor(MaybeOwned<Stream> stream, NonnullOwnPtr<Stream> compressor_stream)
: m_output_stream(move(stream))
, m_compressor(move(compressor_stream))
{
}
ZlibCompressor::~ZlibCompressor()
{
VERIFY(m_finished);
}
ErrorOr<void> ZlibCompressor::write_header(ZlibCompressionMethod compression_method, ZlibCompressionLevel compression_level)
{
u8 compression_info = 0;
if (compression_method == ZlibCompressionMethod::Deflate) {
compression_info = AK::log2(DeflateCompressor::window_size) - 8;
VERIFY(compression_info <= 7);
}
ZlibHeader header {
.compression_method = compression_method,
.compression_info = compression_info,
.check_bits = 0,
.present_dictionary = false,
.compression_level = compression_level,
};
header.check_bits = 0b11111 - header.as_u16 % 31;
// FIXME: Support pre-defined dictionaries.
TRY(m_output_stream->write(header.as_u16.bytes()));
return {};
}
ErrorOr<Bytes> ZlibCompressor::read(Bytes)
{
return Error::from_errno(EBADF);
}
ErrorOr<size_t> ZlibCompressor::write(ReadonlyBytes bytes)
{
VERIFY(!m_finished);
size_t n_written = TRY(m_compressor->write(bytes));
m_adler32_checksum.update(bytes.trim(n_written));
return n_written;
}
bool ZlibCompressor::is_eof() const
{
return false;
}
bool ZlibCompressor::is_open() const
{
return m_output_stream->is_open();
}
void ZlibCompressor::close()
{
}
ErrorOr<void> ZlibCompressor::finish()
{
VERIFY(!m_finished);
if (is<DeflateCompressor>(m_compressor.ptr()))
TRY(static_cast<DeflateCompressor*>(m_compressor.ptr())->final_flush());
NetworkOrdered<u32> adler_sum = m_adler32_checksum.digest();
TRY(m_output_stream->write(adler_sum.bytes()));
m_finished = true;
return {};
}
ErrorOr<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, ZlibCompressionLevel compression_level)
{
auto output_stream = TRY(try_make<AllocatingMemoryStream>());
auto zlib_stream = TRY(ZlibCompressor::construct(MaybeOwned<Stream>(*output_stream), compression_level));
TRY(zlib_stream->write_entire_buffer(bytes));
TRY(zlib_stream->finish());
auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));
TRY(output_stream->read_entire_buffer(buffer.bytes()));
return buffer;
}
}
|