Re: aPLib decruncher for 68000
Posted: Fri Sep 01, 2017 9:45 am
Pseudocode (mix of python, c++, and pascal ):
Code: Select all
copy first byte.
last_offset := uninitialized // some trash that is happened to be in this variable
LWM := 2 // Last Was Match = false
while (true) do:
if (read_bit() == 0): // 0 literal
LWM := 2
write_byte(read_byte)
continue
endif
if (read_bit() == 0): // 10 copy
offset := read_gamma() - LWM;
if (offset != 0): // full copy encode ( != means not equal)
offset := (offset - 1)*256 + read_byte()
last_offset := offset
len := read_gamma()
if (offset >= 32000):
len := len + 2
else if (offset >= 1280):
len := len + 1
else if (offset >= 128):
do_nothing // no operation
else:
len := len + 2
endif
else: // copy with omitted offset
len := read_gamma()
offset := last_offset
endif
for (i=0; i<len; ++i):
write_byte(get_byte_from_output_at_offset_from_end(offset))
LWM := 1 // Last Was Match = true
continue
endif // end if copy encode
if (read_bit() == 0) // 110 short copy
offset := read_byte()
len := (offset % 2) + 2 // % = remainder
offset := floor(offset / 2) // logical shift right
last_offset := offset
if (offset == 0):
break // end of data
endif
for (i=0; i<len; ++i):
write_byte(get_byte_from_output_at_offset_from_end(offset))
LWM := 1 // Last Was Match = true
continue
endif
// 111 single byte copy or null
LWM := 2 // Last Was Match = true
offset := 0;
for (i=0; i<4; ++i)
offset := offset*2 + read_bit()
if (offset == 0):
write_byte(0) // null
else
write_byte(get_byte_from_output_at_offset_from_end(offset))
endif
end
So, when read_bit() has empty buffer of bits, it will read whole byte from input, and save all 8 bits into its buffer.
Code: Select all
byte read_gamma():
result := 1
while(true):
result = result*2 + read_bit()
if (read_bit() == 0):
break
endif
end
return result
end