Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,21 @@ inline ZlibError inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, co
const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]);
if (in.failed() || distance > out.size()) return ZlibError::data;
if (out.size() > limit || length > limit - out.size()) return ZlibError::limit;
const std::size_t from = out.size() - distance;
for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]);
const std::size_t start = out.size();
out.resize(start + length);
auto* dest = out.data() + start;
if (distance == 1) {
std::fill_n(dest, length, dest[-1]);
} else {
// Overlapping matches repeat their first distance bytes. Each copy reads only completed output.
std::size_t copied = std::min(distance, length);
std::memcpy(dest, dest - distance, copied);
while (copied < length) {
const auto count = std::min(copied, length - copied);
std::memcpy(dest + copied, dest, count);
copied += count;
}
}
}
}

Expand Down Expand Up @@ -351,9 +364,16 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint
const std::size_t at = 2 + reader.position();
if (at > in.size() || in.size() - at < 4) return fail(ZlibError::data);
std::uint32_t a = 1, b = 0;
for (const std::uint8_t x : out) {
a = (a + x) % 65521;
b = (b + a) % 65521;
// At most 5,552 bytes of 0xff fit in both 32-bit accumulators between reductions.
for (std::span<const std::uint8_t> tail(out); !tail.empty();) {
const auto block = tail.first(std::min<std::size_t>(tail.size(), 5552));
for (const auto x : block) {
a += x;
b += a;
}
a %= 65521;
b %= 65521;
tail = tail.subspan(block.size());
}
const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) |
(std::uint32_t(in[at + 2]) << 8) | in[at + 3];
Expand Down
16 changes: 16 additions & 0 deletions runtime/test/shader_library_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,22 @@ int main() {
CHECK(zlib_inflate(stored, out) && out.size() == 40);
for (std::uint8_t i = 0; i < 40; ++i) CHECK(out[i] == i);
CHECK(zlib_inflate(fixed, out) && out == bytes("hello hello hello leonida"));
{
// Python zlib.compress(b'\xff' * 5553 + b'abc' * 5553). Exercises overlapping
// matches with periods one and three across multiple Adler reduction boundaries.
const std::vector<std::uint8_t> repeated = {
0x78, 0x9c, 0xed, 0xc2, 0x41, 0x11, 0x00, 0x00, 0x08, 0x03, 0xa0, 0xac,
0xd3, 0xfe, 0x19, 0x66, 0x0d, 0x1f, 0x70, 0xb4, 0x00, 0x00, 0x00, 0x00,
0x00, 0xbc, 0x90, 0x59, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3c, 0x7b, 0xa2, 0x71,
0x87, 0x48,
};
std::vector<std::uint8_t> expected(5553, 0xff);
for (int i = 0; i < 5553; ++i) expected.insert(expected.end(), {'a', 'b', 'c'});
CHECK(zlib_inflate(repeated, out, expected.size()) && out == expected);
ZlibError why{};
CHECK(!zlib_inflate(repeated, out, expected.size() - 1, &why) && why == ZlibError::limit);
}
CHECK(zlib_inflate(dynamic, out) && out == skewed(600));
CHECK(sha256_hex(out) == "9cd6848f765bff2a9f42a8b42f6164042dad67d3e9f82e0044ddc7d1ecdeebb2");
auto broken = dynamic;
Expand Down
Loading