/* Copyright (C) Teemu Suutari */ #include "MASHDecompressor.hpp" #include "HuffmanDecoder.hpp" #include "InputStream.hpp" #include "OutputStream.hpp" #include "common/Common.hpp" namespace ancient::internal { bool MASHDecompressor::detectHeaderXPK(uint32_t hdr) noexcept { return hdr==FourCC("MASH"); } std::shared_ptr MASHDecompressor::create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr &state,bool verify) { return std::make_shared(hdr,recursionLevel,packedData,state,verify); } MASHDecompressor::MASHDecompressor(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr &state,bool verify) : XPKDecompressor(recursionLevel), _packedData(packedData) { if (!detectHeaderXPK(hdr)) throw Decompressor::InvalidFormatError(); } MASHDecompressor::~MASHDecompressor() { // nothing needed } const std::string &MASHDecompressor::getSubName() const noexcept { static std::string name="XPK-MASH: LZRW-compressor"; return name; } void MASHDecompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify) { ForwardInputStream inputStream(_packedData,0,_packedData.size()); MSBBitReader bitReader(inputStream); auto readBits=[&](uint32_t count)->uint32_t { return bitReader.readBits8(count); }; auto readBit=[&]()->uint32_t { return bitReader.readBits8(1); }; auto readByte=[&]()->uint8_t { return inputStream.readByte(); }; size_t rawSize=rawData.size(); ForwardOutputStream outputStream(rawData,0,rawSize); HuffmanDecoder litDecoder { HuffmanCode{1,0b000000,0}, HuffmanCode{2,0b000010,1}, HuffmanCode{3,0b000110,2}, HuffmanCode{4,0b001110,3}, HuffmanCode{5,0b011110,4}, HuffmanCode{6,0b111110,5}, HuffmanCode{6,0b111111,6} }; while (!outputStream.eof()) { uint32_t litLength=litDecoder.decode(readBit); if (litLength==6) { uint32_t litBits; for (litBits=1;litBits<=17;litBits++) if (!readBit()) break; if (litBits==17) throw Decompressor::DecompressionError(); litLength=readBits(litBits)+(1<