diff --git a/asm_unpackers/Makefile b/asm_unpackers/Makefile index 40c959e..43b7473 100644 --- a/asm_unpackers/Makefile +++ b/asm_unpackers/Makefile @@ -17,6 +17,14 @@ test_armv6m: build/unpack_armv6m build/unpack_armv6m.bin: unpack_armv6m.S arm-none-eabi-gcc -march=armv6-m -c -o build/unpack_armv6m.o $? arm-none-eabi-objcopy -O binary --only-section=.text build/unpack_armv6m.o $@ + +build/unpack_c: ../c_unpacker/main.c ../c_unpacker/unpack.c + mkdir -p build + gcc -g -o $@ $^ + +test_c: build/unpack_c + $< test_data.upk /tmp/out.bin + cmp test_data.bin /tmp/out.bin sizes: build/unpack_armv6m.bin ls -l build/*.bin \ No newline at end of file diff --git a/c_unpacker/decode_bit_alt.c b/c_unpacker/decode_bit_alt.c new file mode 100644 index 0000000..2003cdd --- /dev/null +++ b/c_unpacker/decode_bit_alt.c @@ -0,0 +1,33 @@ +int upkr_decode_bit(int context_index) { +#ifdef UPKR_BITSTREAM + while(upkr_state < 32768) { + if(upkr_bits_left == 0) { + upkr_current_byte = *upkr_data_ptr++; + upkr_bits_left = 8; + } + upkr_state = (upkr_state << 1) + (upkr_current_byte & 1); + upkr_current_byte >>= 1; + --upkr_bits_left; + } +#else + while(upkr_state < 4096) { + upkr_state = (upkr_state << 8) | *upkr_data_ptr++; + } +#endif + + int prob = upkr_probs[context_index]; + int bit = (upkr_state & 255) < prob ? 1 : 0; + + if(bit) { + prob = 256 - prob; + } + upkr_state -= prob * ((upkr_state >> 8) + (bit ^ 1)); + prob -= (prob + 8) >> 4; + if(bit) { + prob = -prob; + } + upkr_probs[context_index] = prob; + + return bit; +} + diff --git a/c_unpacker/unpack.c b/c_unpacker/unpack.c index 0b79206..d317e02 100644 --- a/c_unpacker/unpack.c +++ b/c_unpacker/unpack.c @@ -32,17 +32,14 @@ int upkr_decode_bit(int context_index) { int prob = upkr_probs[context_index]; int bit = (upkr_state & 255) < prob ? 1 : 0; - int tmp = prob; - if(!bit) { - tmp = 256 - tmp; + if(bit) { + upkr_state = prob * (upkr_state >> 8) + (upkr_state & 255); + prob += (256 - prob + 8) >> 4; + } else { + upkr_state = (256 - prob) * (upkr_state >> 8) + (upkr_state & 255) - prob; + prob -= (prob + 8) >> 4; } - upkr_state = tmp * (upkr_state >> 8) + (upkr_state & 255); - tmp += (256 - tmp + 8) >> 4; - if(!bit) { - upkr_state -= prob; - tmp = 256 - tmp; - } - upkr_probs[context_index] = tmp; + upkr_probs[context_index] = prob; return bit; }