#! Contains inflate, which undoes the compression of binary FBX's arrays: #! DEFLATE, as RFC 1951 writes it, within zlib's wrapping, as RFC 1950 writes #! it. It's `image`'s, of its PNG reader, copied, so `models` needs no more #! than `physics`: Huffman codes read as stb_image reads them, by a table of #! codes up to 9 bits long and longer ones by where each length's codes end. module malunal::fbx; # How many bits the fast table reads at once. const fast_bits: u64 = 9; # The lengths and distances of runs: the least of each symbol's, and how many # bits more it reads. const length_base: u32[29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]; const length_extra: u32[29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]; const distance_base: u32[30] = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]; const distance_extra: u32[30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]; # The order a dynamic block gives the lengths of the code its lengths are in. const length_order: u64[19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; # A Huffman code: each symbol's code, as the lengths of all of them give it. struct huffman { var fast: u16[512]; #! `length << 9 | symbol` by a code's first 9 bits, read reversed, or 0 for a longer one. var first_code: u32[16]; var first_symbol: u32[16]; var max_code: u32[17]; #! Just past the last code of each length, shifted to be 16 bits long. var size: u8[288]; var value: u16[288]; } # Builds a code from each symbol's length, which may leave codes unused, but not # give more than there are. def huffman::build(&mut this, lengths: span): bool { var counts: u32[17]; for length in lengths { counts[length as u64]++; } counts[0] = 0; for bits in 1u..16u { if counts[bits] as u64 > (1u << bits) { return false; } } var next: u32[16]; var code: u32 = 0; var symbols: u32 = 0; for bits in 1u..16u { next[bits] = code; this.first_code[bits] = code; this.first_symbol[bits] = symbols; code += counts[bits]; if counts[bits] != 0 && (code - 1) as u64 >= (1u << bits) { return false; } this.max_code[bits] = code << (16 - bits) as u32; code <<= 1; symbols += counts[bits]; } this.max_code[16] = 0x10000; for symbol, length in lengths { if length == 0 { continue; } val bits := length as u64; val slot := (next[bits] - this.first_code[bits] + this.first_symbol[bits]) as u64; this.size[slot] = length; this.value[slot] = symbol as u16; if bits <= fast_bits { var at := reversed_bits(next[bits], bits) as u64; while at < 512 { this.fast[at] = ((bits << 9) | symbol) as u16; at += 1u << bits; } } next[bits]++; } return true; } # The lowest bits of a number, in the opposite order. def reversed_bits(code: u32, bits: u64): u32 { var from := code; var made: u32 = 0; for _ in 0..bits { made = (made << 1) | (from & 1); from >>= 1; } return made; } # The 16 lowest bits of a number, in the opposite order. def reversed16(code: u32): u32 { var made := code; made = ((made & 0xAAAA) >> 1) | ((made & 0x5555) << 1); made = ((made & 0xCCCC) >> 2) | ((made & 0x3333) << 2); made = ((made & 0xF0F0) >> 4) | ((made & 0x0F0F) << 4); return ((made & 0xFF00) >> 8) | ((made & 0x00FF) << 8); } # The Adler-32 of bytes, as zlib checks them by. def adler32(data: span): u32 { var low: u32 = 1; var high: u32 = 0; var at: u64 = 0; while at < data.length { # 5552 bytes is the most that can be summed before either could pass 32 bits. val end := at + 5552 < data.length ? at + 5552 : data.length; while at < end { low += data[at]; high += low; at++; } low %= 65521; high %= 65521; } return (high << 16) | low; } # Reads DEFLATE's bits, least significant first, into the bytes made. struct inflater { var data: span; 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; 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, out: slice): maybe { 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 { var lengths: u8[288]; for symbol in 0u..288u { lengths[symbol] = symbol < 144 ? 8 : (symbol < 256 ? 9 : (symbol < 280 ? 7 : 8)); } 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); } # 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 := reversed16((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; }