2018-08-04 11:44:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace nall { namespace Decode {
|
|
|
|
|
2018-08-21 03:17:12 +00:00
|
|
|
template<uint S = 1, uint M = 4 / S> //S = word size; M = match length
|
|
|
|
inline auto RLE(const void* data, uint remaining = ~0) -> vector<uint8_t> {
|
|
|
|
vector<uint8_t> output;
|
|
|
|
|
|
|
|
auto input = (const uint8_t*)data;
|
2018-08-04 11:44:00 +00:00
|
|
|
|
|
|
|
auto load = [&]() -> uint8_t {
|
|
|
|
if(!remaining) return 0x00;
|
2018-08-21 03:17:12 +00:00
|
|
|
return --remaining, *input++;
|
2018-08-04 11:44:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint base = 0;
|
2018-08-21 03:17:12 +00:00
|
|
|
uint64_t size = 0;
|
|
|
|
for(uint byte : range(8)) size |= load() << byte * 8;
|
|
|
|
output.resize(size);
|
|
|
|
|
|
|
|
auto read = [&]() -> uint64_t {
|
|
|
|
uint64_t value = 0;
|
|
|
|
for(uint byte : range(S)) value |= load() << byte * 8;
|
2018-08-04 11:44:00 +00:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2018-08-21 03:17:12 +00:00
|
|
|
auto write = [&](uint64_t value) -> void {
|
2018-08-04 11:44:00 +00:00
|
|
|
if(base >= size) return;
|
2018-08-21 03:17:12 +00:00
|
|
|
for(uint byte : range(S)) output[base++] = value >> byte * 8;
|
2018-08-04 11:44:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
while(base < size) {
|
2018-08-21 03:17:12 +00:00
|
|
|
auto byte = load();
|
2018-08-04 11:44:00 +00:00
|
|
|
if(byte < 128) {
|
|
|
|
byte++;
|
|
|
|
while(byte--) write(read());
|
|
|
|
} else {
|
|
|
|
auto value = read();
|
2018-08-21 03:17:12 +00:00
|
|
|
byte = (byte & 127) + M;
|
2018-08-04 11:44:00 +00:00
|
|
|
while(byte--) write(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 03:17:12 +00:00
|
|
|
return output;
|
2018-08-04 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|