bsnes-libretro/nall/decode/lzsa.hpp
Tim Allen 559a6585ef Update to v106r81 release.
byuu says:

First 32 instructions implemented in the TLCS900H disassembler. Only 992
to go!

I removed the use of anonymous namespaces in nall. It was something I
rarely used, because it rarely did what I wanted.

I updated all nested namespaces to use C++17-style namespace Foo::Bar {}
syntax instead of classic C++-style namespace Foo { namespace Bar {}}.

I updated ruby::Video::acquire() to return a struct, so we can use C++17
structured bindings. Long term, I want to get away from all functions
that take references for output only. Even though C++ botched structured
bindings by not allowing you to bind to existing variables, it's even
worse to have function calls that take arguments by reference and then
write to them. From the caller side, you can't tell the value is being
written, nor that the value passed in doesn't matter, which is terrible.
2019-01-16 13:02:24 +11:00

73 lines
1.9 KiB
C++

#pragma once
#include <nall/decode/huffman.hpp>
namespace nall::Decode {
inline auto LZSA(array_view<uint8_t> input) -> vector<uint8_t> {
vector<uint8_t> output;
uint index = 0;
uint size = 0;
for(uint byte : range(8)) size |= *input++ << byte * 8;
output.resize(size);
auto load = [&]() -> vector<uint8_t> {
uint size = 0;
for(uint byte : range(8)) size |= *input++ << byte * 8;
vector<uint8_t> buffer;
buffer.reserve(size);
while(size--) buffer.append(*input++);
return buffer;
};
auto flags = Decode::Huffman(load());
auto literals = Decode::Huffman(load());
auto lengths = Decode::Huffman(load());
auto offsets = Decode::Huffman(load());
auto flagData = flags.data();
uint byte = 0, bits = 0;
auto flagRead = [&]() -> bool {
if(bits == 0) bits = 8, byte = *flagData++;
return byte >> --bits & 1;
};
auto literalData = literals.data();
auto literalRead = [&]() -> uint8_t {
return *literalData++;
};
auto lengthData = lengths.data();
auto lengthRead = [&]() -> uint64_t {
uint byte = *lengthData++, bytes = 1;
while(!(byte & 1)) byte >>= 1, bytes++;
uint length = byte >> 1, shift = 8 - bytes;
while(--bytes) length |= *lengthData++ << shift, shift += 8;
return length;
};
auto offsetData = offsets.data();
auto offsetRead = [&]() -> uint {
uint offset = 0;
offset |= *offsetData++ << 0; if(index < 1 << 8) return offset;
offset |= *offsetData++ << 8; if(index < 1 << 16) return offset;
offset |= *offsetData++ << 16; if(index < 1 << 24) return offset;
offset |= *offsetData++ << 24; return offset;
};
while(index < size) {
if(!flagRead()) {
output[index++] = literalRead();
} else {
uint length = lengthRead() + 6;
uint offset = index - offsetRead();
while(length--) output[index++] = output[offset++];
}
}
return output;
}
}