mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-09-23 14:53:52 +00:00
Add unpacktool
This commit is contained in:
75
unpacktool/StuffItHuffmanDecompressor.cpp
Normal file
75
unpacktool/StuffItHuffmanDecompressor.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "StuffItHuffmanDecompressor.h"
|
||||
|
||||
StuffItHuffmanDecompressor::StuffItHuffmanDecompressor()
|
||||
: input(nullptr)
|
||||
, code(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
StuffItHuffmanDecompressor::~StuffItHuffmanDecompressor()
|
||||
{
|
||||
delete code;
|
||||
}
|
||||
|
||||
bool StuffItHuffmanDecompressor::Reset(CSInputBuffer *input, size_t compressedSize, size_t decompressedSize)
|
||||
{
|
||||
this->input = input;
|
||||
code = nullptr;
|
||||
|
||||
return this->resetByteStream();
|
||||
}
|
||||
|
||||
bool StuffItHuffmanDecompressor::resetByteStream()
|
||||
{
|
||||
delete code;
|
||||
code = XADPrefixCode::prefixCode();
|
||||
|
||||
code->startBuildingTree();
|
||||
if (!parseTree())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StuffItHuffmanDecompressor::parseTree()
|
||||
{
|
||||
unsigned int firstBit;
|
||||
if (!CSInputNextBit(input, firstBit))
|
||||
return false;
|
||||
|
||||
if (firstBit == 1)
|
||||
{
|
||||
unsigned int nextBits;
|
||||
if (!CSInputNextBitString(input, 8, nextBits))
|
||||
return false;
|
||||
code->makeLeafWithValue(nextBits);
|
||||
}
|
||||
else
|
||||
{
|
||||
code->startZeroBranch();
|
||||
if (!parseTree())
|
||||
return false;
|
||||
|
||||
code->startOneBranch();
|
||||
if (!parseTree())
|
||||
return false;
|
||||
|
||||
code->finishBranches();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StuffItHuffmanDecompressor::ReadBytes(void *dest, size_t numBytes)
|
||||
{
|
||||
for (size_t i = 0; i < numBytes; i++)
|
||||
{
|
||||
int sym;
|
||||
if (!CSInputNextSymbolUsingCode(input, code, sym))
|
||||
return false;
|
||||
|
||||
static_cast<uint8_t*>(dest)[i] = sym;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user