Initial Commit
This commit is contained in:
69 files changed
+7703
No files matched your search
@@ -0,0 +1,286 @@
|
||||
#! Contains inflate, which undoes the compression of PNG's image data: DEFLATE, as
|
||||
#! RFC 1951 writes it, within zlib's wrapping, as RFC 1950 writes it, by the
|
||||
#! codes `huffman.sakura` reads.
|
||||
|
||||
module image::png;
|
||||
|
||||
|
||||
# Reads DEFLATE's bits, least significant first, into the bytes made.
|
||||
struct inflater {
|
||||
var data: span<u8>;
|
||||
var at: u64; #! The next byte to read, which runs past the end where bits read past it.
|
||||
var buffer: u64;
|
||||
var count: u64; #! How many bits the buffer holds.
|
||||
var out: slice<u8>;
|
||||
var written: u64;
|
||||
}
|
||||
|
||||
# Inflates zlib's data into memory exactly as long as it should be, which it must
|
||||
# not go past, checking its Adler-32.
|
||||
# @returns How many bytes it inflated to, or `nothing` where it's broken.
|
||||
def inflate(data: span<u8>, out: slice<u8>): maybe<u64> {
|
||||
if data.length < 6 {
|
||||
return nothing {};
|
||||
}
|
||||
val method := data[0];
|
||||
val flags := data[1];
|
||||
if (method & 0x0F) != 8 || (method >> 4) > 7 || (flags & 0x20) != 0 || ((method as u32) * 256 + flags as u32) % 31 != 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
var z := inflater { data = data, at = 2, out = out };
|
||||
var last := false;
|
||||
while !last {
|
||||
last = z.take(1) == 1;
|
||||
val kind := z.take(2);
|
||||
var read := false;
|
||||
if kind == 0 {
|
||||
read = z.stored();
|
||||
} else if kind == 1 {
|
||||
read = z.fixed();
|
||||
} else if kind == 2 {
|
||||
read = z.dynamic();
|
||||
}
|
||||
if !read || z.overran() {
|
||||
return nothing {};
|
||||
}
|
||||
}
|
||||
|
||||
# The checksum is in the next whole bytes, the most significant first.
|
||||
z.align();
|
||||
var expected: u32 = 0;
|
||||
for _ in 0..4 {
|
||||
expected = (expected << 8) | z.take(8);
|
||||
}
|
||||
if z.overran() || adler32(z.out[0:z.written]) != expected {
|
||||
return nothing {};
|
||||
}
|
||||
return z.written;
|
||||
}
|
||||
|
||||
# Copies a stored block, whose length follows from the next whole byte.
|
||||
def inflater::stored(&mut this): bool {
|
||||
this.align();
|
||||
val length := this.take(16);
|
||||
if (length ^ 0xFFFF) != this.take(16) {
|
||||
return false;
|
||||
}
|
||||
|
||||
# What the buffer holds first, then the rest straight from the data.
|
||||
var left := length as u64;
|
||||
while left > 0 && this.count > 0 {
|
||||
if !this.put(this.take(8) as u8) {
|
||||
return false;
|
||||
}
|
||||
left--;
|
||||
}
|
||||
if this.at + left > this.data.length || this.written + left > this.out.length {
|
||||
return false;
|
||||
}
|
||||
for index in 0..left {
|
||||
this.out[this.written + index] = this.data[this.at + index];
|
||||
}
|
||||
this.at += left;
|
||||
this.written += left;
|
||||
return true;
|
||||
}
|
||||
|
||||
# Inflates a block of the fixed codes RFC 1951 gives.
|
||||
def inflater::fixed(&mut this): bool {
|
||||
val lengths := fixed_lengths();
|
||||
var distances: u8[30];
|
||||
for symbol in 0u..30u {
|
||||
distances[symbol] = 5;
|
||||
}
|
||||
var literal_code := huffman {};
|
||||
var distance_code := huffman {};
|
||||
_ = literal_code.build(lengths[:]);
|
||||
_ = distance_code.build(distances[:]);
|
||||
return this.codes(&literal_code, &distance_code);
|
||||
}
|
||||
|
||||
# The lengths of the fixed codes of literals and lengths RFC 1951 gives.
|
||||
def fixed_lengths(): u8[288] {
|
||||
var lengths: u8[288];
|
||||
for symbol in 0u..288u {
|
||||
lengths[symbol] = symbol < 144 ? 8 : (symbol < 256 ? 9 : (symbol < 280 ? 7 : 8));
|
||||
}
|
||||
return lengths;
|
||||
}
|
||||
|
||||
# Inflates a block of codes of its own, whose lengths it gives first, in a code of
|
||||
# their own.
|
||||
def inflater::dynamic(&mut this): bool {
|
||||
val literals := this.take(5) as u64 + 257;
|
||||
val distances := this.take(5) as u64 + 1;
|
||||
val count := this.take(4) as u64 + 4;
|
||||
|
||||
var length_lengths: u8[19];
|
||||
for index in 0..count {
|
||||
length_lengths[length_order[index]] = this.take(3) as u8;
|
||||
}
|
||||
var length_code := huffman {};
|
||||
if !length_code.build(length_lengths[:]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
# The lengths, where 16 repeats the last, and 17 and 18 give runs of zeros.
|
||||
var lengths: u8[320];
|
||||
val total := literals + distances;
|
||||
var read: u64 = 0;
|
||||
while read < total {
|
||||
val symbol := this.decode(&length_code);
|
||||
if symbol < 0 {
|
||||
return false;
|
||||
}
|
||||
var length: u8 = 0;
|
||||
var repeat: u64 = 1;
|
||||
if symbol < 16 {
|
||||
length = symbol as u8;
|
||||
} else if symbol == 16 {
|
||||
if read == 0 {
|
||||
return false;
|
||||
}
|
||||
length = lengths[read - 1];
|
||||
repeat = 3 + this.take(2) as u64;
|
||||
} else if symbol == 17 {
|
||||
repeat = 3 + this.take(3) as u64;
|
||||
} else {
|
||||
repeat = 11 + this.take(7) as u64;
|
||||
}
|
||||
if read + repeat > total {
|
||||
return false;
|
||||
}
|
||||
for _ in 0..repeat {
|
||||
lengths[read] = length;
|
||||
read++;
|
||||
}
|
||||
}
|
||||
if lengths[256] == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
var literal_code := huffman {};
|
||||
var distance_code := huffman {};
|
||||
if !literal_code.build(lengths[0:literals]) || !distance_code.build(lengths[literals:distances]) {
|
||||
return false;
|
||||
}
|
||||
return this.codes(&literal_code, &distance_code);
|
||||
}
|
||||
|
||||
# Inflates a block's symbols: bytes, then runs of what came before, to its end.
|
||||
def inflater::codes(&mut this, literals: &huffman, distances: &huffman): bool {
|
||||
while true {
|
||||
val symbol := this.decode(literals);
|
||||
if symbol < 0 {
|
||||
return false;
|
||||
}
|
||||
if symbol < 256 {
|
||||
if !this.put(symbol as u8) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if symbol == 256 {
|
||||
return true;
|
||||
}
|
||||
|
||||
val index := (symbol - 257) as u64;
|
||||
if index >= 29 {
|
||||
return false;
|
||||
}
|
||||
val length := (length_base[index] + this.take(length_extra[index] as u64)) as u64;
|
||||
val which := this.decode(distances);
|
||||
if which < 0 || which >= 30 {
|
||||
return false;
|
||||
}
|
||||
val distance := (distance_base[which as u64] + this.take(distance_extra[which as u64] as u64)) as u64;
|
||||
if distance > this.written || this.written + length > this.out.length || this.overran() {
|
||||
return false;
|
||||
}
|
||||
# A run may overlap itself, repeating what it copies, so it goes a byte at a time.
|
||||
var from := this.written - distance;
|
||||
for _ in 0..length {
|
||||
this.out[this.written] = this.out[from];
|
||||
this.written++;
|
||||
from++;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Decodes a symbol of a code.
|
||||
# @returns The symbol, or -1 where the bits are no code's.
|
||||
def inflater::decode(&mut this, code: &huffman): i32 {
|
||||
if this.count < 16 {
|
||||
this.fill();
|
||||
}
|
||||
val entry := code.fast[this.buffer & 511];
|
||||
if entry != 0 {
|
||||
val length := (entry >> 9) as u64;
|
||||
this.buffer >>= length;
|
||||
this.count -= length;
|
||||
return (entry & 511) as i32;
|
||||
}
|
||||
|
||||
# Longer than the table reads: the code's length is the first whose codes end
|
||||
# past the bits, read most significant first.
|
||||
val bits := reverse16((this.buffer & 0xFFFF) as u32);
|
||||
var length: u64 = fast_bits + 1;
|
||||
while bits >= code.max_code[length] {
|
||||
length++;
|
||||
}
|
||||
if length >= 16 {
|
||||
return -1;
|
||||
}
|
||||
val slot := ((bits >> (16 - length) as u32) - code.first_code[length] + code.first_symbol[length]) as u64;
|
||||
if slot >= 288 || code.size[slot] as u64 != length {
|
||||
return -1;
|
||||
}
|
||||
this.buffer >>= length;
|
||||
this.count -= length;
|
||||
return code.value[slot] as i32;
|
||||
}
|
||||
|
||||
# Reads a number of bits, up to 16, the first the least significant.
|
||||
def inflater::take(&mut this, bits: u64): u32 {
|
||||
if this.count < bits {
|
||||
this.fill();
|
||||
}
|
||||
val made := (this.buffer & ((1u << bits) - 1)) as u32;
|
||||
this.buffer >>= bits;
|
||||
this.count -= bits;
|
||||
return made;
|
||||
}
|
||||
|
||||
# Fills the buffer, with zeros past the end of the data, which `overran` tells.
|
||||
def inflater::fill(&mut this): void {
|
||||
while this.count <= 56 {
|
||||
if this.at < this.data.length {
|
||||
this.buffer |= (this.data[this.at] as u64) << this.count;
|
||||
}
|
||||
this.at++;
|
||||
this.count += 8;
|
||||
}
|
||||
}
|
||||
|
||||
# Drops the bits up to the next whole byte.
|
||||
def inflater::align(&mut this): void {
|
||||
val extra := this.count % 8;
|
||||
this.buffer >>= extra;
|
||||
this.count -= extra;
|
||||
}
|
||||
|
||||
# Whether more bits have been read than the data holds.
|
||||
def inflater::overran(&this): bool {
|
||||
return this.at * 8 - this.count > this.data.length * 8;
|
||||
}
|
||||
|
||||
def inflater::put(&mut this, byte: u8): bool {
|
||||
if this.written >= this.out.length {
|
||||
return false;
|
||||
}
|
||||
this.out[this.written] = byte;
|
||||
this.written++;
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user