This commit is contained in:
elasota
2020-10-18 14:56:04 -04:00
parent 1da35d45ae
commit c96a1ab251

View File

@@ -1,366 +1,366 @@
#include "StuffItArsenicDecompressor.h" #include "StuffItArsenicDecompressor.h"
#include "CRC.h" #include "CRC.h"
#include "CSInputBuffer.h" #include "CSInputBuffer.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <crtdbg.h> #include <crtdbg.h>
static const uint16_t RandomizationTable[] = static const uint16_t RandomizationTable[] =
{ {
0xee, 0x56, 0xf8, 0xc3, 0x9d, 0x9f, 0xae, 0x2c, 0xee, 0x56, 0xf8, 0xc3, 0x9d, 0x9f, 0xae, 0x2c,
0xad, 0xcd, 0x24, 0x9d, 0xa6, 0x101, 0x18, 0xb9, 0xad, 0xcd, 0x24, 0x9d, 0xa6, 0x101, 0x18, 0xb9,
0xa1, 0x82, 0x75, 0xe9, 0x9f, 0x55, 0x66, 0x6a, 0xa1, 0x82, 0x75, 0xe9, 0x9f, 0x55, 0x66, 0x6a,
0x86, 0x71, 0xdc, 0x84, 0x56, 0x96, 0x56, 0xa1, 0x86, 0x71, 0xdc, 0x84, 0x56, 0x96, 0x56, 0xa1,
0x84, 0x78, 0xb7, 0x32, 0x6a, 0x3, 0xe3, 0x2, 0x84, 0x78, 0xb7, 0x32, 0x6a, 0x3, 0xe3, 0x2,
0x11, 0x101, 0x8, 0x44, 0x83, 0x100, 0x43, 0xe3, 0x11, 0x101, 0x8, 0x44, 0x83, 0x100, 0x43, 0xe3,
0x1c, 0xf0, 0x86, 0x6a, 0x6b, 0xf, 0x3, 0x2d, 0x1c, 0xf0, 0x86, 0x6a, 0x6b, 0xf, 0x3, 0x2d,
0x86, 0x17, 0x7b, 0x10, 0xf6, 0x80, 0x78, 0x7a, 0x86, 0x17, 0x7b, 0x10, 0xf6, 0x80, 0x78, 0x7a,
0xa1, 0xe1, 0xef, 0x8c, 0xf6, 0x87, 0x4b, 0xa7, 0xa1, 0xe1, 0xef, 0x8c, 0xf6,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, 0x98, 0x29, 0xcd, 0x1f, 0x2, 0xa8, 0x87, 0xd2,
0xa0, 0x93, 0x98, 0xef, 0xc, 0x43, 0xed, 0x9d, 0xa0, 0x93, 0x98, 0xef, 0xc, 0x43, 0xed, 0x9d,
0xc2, 0xeb, 0x81, 0xe9, 0x64, 0x23, 0x68, 0x1e, 0xc2, 0xeb, 0x81, 0xe9, 0x64, 0x23, 0x68, 0x1e,
0x25, 0x57, 0xde, 0x9a, 0xcf, 0x7f, 0xe5, 0xba, 0x25, 0x57, 0xde, 0x9a, 0xcf, 0x7f, 0xe5, 0xba,
0x41, 0xea, 0xea, 0x36, 0x1a, 0x28, 0x79, 0x20, 0x41, 0xea, 0xea, 0x36, 0x1a, 0x28, 0x79, 0x20,
0x5e, 0x18, 0x4e, 0x7c, 0x8e, 0x58, 0x7a, 0xef, 0x5e, 0x18, 0x4e, 0x7c, 0x8e, 0x58, 0x7a, 0xef,
0x91, 0x2, 0x93, 0xbb, 0x56, 0xa1, 0x49, 0x1b, 0x91, 0x2, 0x93, 0xbb, 0x56, 0xa1, 0x49, 0x1b,
0x79, 0x92, 0xf3, 0x58, 0x4f, 0x52, 0x9c, 0x2, 0x79, 0x92, 0xf3, 0x58, 0x4f, 0x52, 0x9c, 0x2,
0x77, 0xaf, 0x2a, 0x8f, 0x49, 0xd0, 0x99, 0x4d, 0x77, 0xaf, 0x2a, 0x8f, 0x49, 0xd0, 0x99, 0x4d,
0x98, 0x101, 0x60, 0x93, 0x100, 0x75, 0x31, 0xce, 0x98, 0x101, 0x60, 0x93, 0x100, 0x75, 0x31, 0xce,
0x49, 0x20, 0x56, 0x57, 0xe2, 0xf5, 0x26, 0x2b, 0x49, 0x20, 0x56, 0x57, 0xe2, 0xf5, 0x26, 0x2b,
0x8a, 0xbf, 0xde, 0xd0, 0x83, 0x34, 0xf4, 0x17 0x8a, 0xbf, 0xde, 0xd0, 0x83, 0x34, 0xf4, 0x17
}; };
// Arithmetic decoder model // Arithmetic decoder model
void StuffItArsenicDecompressor::InitializeArithmeticModel(ArithmeticModel *model,int firstsymbol,int lastsymbol,int increment,int frequencylimit) void StuffItArsenicDecompressor::InitializeArithmeticModel(ArithmeticModel *model,int firstsymbol,int lastsymbol,int increment,int frequencylimit)
{ {
model->increment=increment; model->increment=increment;
model->frequencylimit=frequencylimit; model->frequencylimit=frequencylimit;
model->numsymbols=lastsymbol-firstsymbol+1; model->numsymbols=lastsymbol-firstsymbol+1;
for(int i=0;i<model->numsymbols;i++) model->symbols[i].symbol=i+firstsymbol; for(int i=0;i<model->numsymbols;i++) model->symbols[i].symbol=i+firstsymbol;
ResetArithmeticModel(model); ResetArithmeticModel(model);
} }
void StuffItArsenicDecompressor::ResetArithmeticModel(ArithmeticModel *model) void StuffItArsenicDecompressor::ResetArithmeticModel(ArithmeticModel *model)
{ {
model->totalfrequency=model->increment*model->numsymbols; model->totalfrequency=model->increment*model->numsymbols;
for(int i=0;i<model->numsymbols;i++) model->symbols[i].frequency=model->increment; for(int i=0;i<model->numsymbols;i++) model->symbols[i].frequency=model->increment;
} }
void StuffItArsenicDecompressor::IncreaseArithmeticModelFrequency(ArithmeticModel *model,int symindex) void StuffItArsenicDecompressor::IncreaseArithmeticModelFrequency(ArithmeticModel *model,int symindex)
{ {
model->symbols[symindex].frequency+=model->increment; model->symbols[symindex].frequency+=model->increment;
model->totalfrequency+=model->increment; model->totalfrequency+=model->increment;
if(model->totalfrequency>model->frequencylimit) if(model->totalfrequency>model->frequencylimit)
{ {
model->totalfrequency=0; model->totalfrequency=0;
for(int i=0;i<model->numsymbols;i++) for(int i=0;i<model->numsymbols;i++)
{ {
model->symbols[i].frequency++; model->symbols[i].frequency++;
model->symbols[i].frequency>>=1; model->symbols[i].frequency>>=1;
model->totalfrequency+=model->symbols[i].frequency; model->totalfrequency+=model->symbols[i].frequency;
} }
} }
} }
// Arithmetic decoder // Arithmetic decoder
#define NumBits 26 #define NumBits 26
#define One (1<<(NumBits-1)) #define One (1<<(NumBits-1))
#define Half (1<<(NumBits-2)) #define Half (1<<(NumBits-2))
bool StuffItArsenicDecompressor::InitializeArithmeticDecoder(ArithmeticDecoder *decoder,CSInputBuffer *input) bool StuffItArsenicDecompressor::InitializeArithmeticDecoder(ArithmeticDecoder *decoder,CSInputBuffer *input)
{ {
unsigned int initialCode; unsigned int initialCode;
if (!CSInputNextLongBitString(input, NumBits, initialCode)) if (!CSInputNextLongBitString(input, NumBits, initialCode))
return false; return false;
decoder->input=input; decoder->input=input;
decoder->range=One; decoder->range=One;
decoder->code = initialCode; decoder->code = initialCode;
return true; return true;
} }
bool StuffItArsenicDecompressor::ReadNextArithmeticCode(ArithmeticDecoder *decoder,int symlow,int symsize,int symtot) bool StuffItArsenicDecompressor::ReadNextArithmeticCode(ArithmeticDecoder *decoder,int symlow,int symsize,int symtot)
{ {
int renorm_factor=decoder->range/symtot; int renorm_factor=decoder->range/symtot;
int lowincr=renorm_factor*symlow; int lowincr=renorm_factor*symlow;
decoder->code-=lowincr; decoder->code-=lowincr;
if(symlow+symsize==symtot) decoder->range-=lowincr; if(symlow+symsize==symtot) decoder->range-=lowincr;
else decoder->range=symsize*renorm_factor; else decoder->range=symsize*renorm_factor;
while(decoder->range<=Half) while(decoder->range<=Half)
{ {
decoder->range<<=1; decoder->range<<=1;
unsigned int bit; unsigned int bit;
if (!CSInputNextBit(decoder->input, bit)) if (!CSInputNextBit(decoder->input, bit))
return false; return false;
decoder->code = (decoder->code << 1) | bit; decoder->code = (decoder->code << 1) | bit;
} }
return true; return true;
} }
bool StuffItArsenicDecompressor::NextArithmeticSymbol(ArithmeticDecoder *decoder,ArithmeticModel *model,int &outSymbol) bool StuffItArsenicDecompressor::NextArithmeticSymbol(ArithmeticDecoder *decoder,ArithmeticModel *model,int &outSymbol)
{ {
int frequency=decoder->code/(decoder->range/model->totalfrequency); int frequency=decoder->code/(decoder->range/model->totalfrequency);
int cumulative=0,n; int cumulative=0,n;
for(n=0;n<model->numsymbols-1;n++) for(n=0;n<model->numsymbols-1;n++)
{ {
if(cumulative+model->symbols[n].frequency>frequency) break; if(cumulative+model->symbols[n].frequency>frequency) break;
cumulative+=model->symbols[n].frequency; cumulative+=model->symbols[n].frequency;
} }
if (!ReadNextArithmeticCode(decoder, cumulative, model->symbols[n].frequency, model->totalfrequency)) if (!ReadNextArithmeticCode(decoder, cumulative, model->symbols[n].frequency, model->totalfrequency))
return false; return false;
IncreaseArithmeticModelFrequency(model,n); IncreaseArithmeticModelFrequency(model,n);
outSymbol = model->symbols[n].symbol; outSymbol = model->symbols[n].symbol;
return true; return true;
} }
bool StuffItArsenicDecompressor::NextArithmeticBitString(ArithmeticDecoder *decoder,ArithmeticModel *model,int bits,int &outBits) bool StuffItArsenicDecompressor::NextArithmeticBitString(ArithmeticDecoder *decoder,ArithmeticModel *model,int bits,int &outBits)
{ {
int res=0; int res=0;
for (int i = 0; i < bits; i++) for (int i = 0; i < bits; i++)
{ {
int sym; int sym;
if (!NextArithmeticSymbol(decoder, model, sym)) if (!NextArithmeticSymbol(decoder, model, sym))
return false; return false;
if (sym) if (sym)
res |= 1 << i; res |= 1 << i;
} }
outBits = res; outBits = res;
return true; return true;
} }
StuffItArsenicDecompressor::StuffItArsenicDecompressor() StuffItArsenicDecompressor::StuffItArsenicDecompressor()
: block(NULL) : block(NULL)
, transform(NULL) , transform(NULL)
{ {
} }
StuffItArsenicDecompressor::~StuffItArsenicDecompressor() StuffItArsenicDecompressor::~StuffItArsenicDecompressor()
{ {
delete[] block; delete[] block;
delete[] transform; delete[] transform;
} }
bool StuffItArsenicDecompressor::Reset(CSInputBuffer *input, size_t compressedSize, size_t decompressedSize) bool StuffItArsenicDecompressor::Reset(CSInputBuffer *input, size_t compressedSize, size_t decompressedSize)
{ {
if (!InitializeArithmeticDecoder(&decoder, input)) if (!InitializeArithmeticDecoder(&decoder, input))
return false; return false;
InitializeArithmeticModel(&initialmodel,0,1,1,256); InitializeArithmeticModel(&initialmodel,0,1,1,256);
InitializeArithmeticModel(&selectormodel,0,10,8,1024); InitializeArithmeticModel(&selectormodel,0,10,8,1024);
InitializeArithmeticModel(&mtfmodel[0],2,3,8,1024); InitializeArithmeticModel(&mtfmodel[0],2,3,8,1024);
InitializeArithmeticModel(&mtfmodel[1],4,7,4,1024); InitializeArithmeticModel(&mtfmodel[1],4,7,4,1024);
InitializeArithmeticModel(&mtfmodel[2],8,15,4,1024); InitializeArithmeticModel(&mtfmodel[2],8,15,4,1024);
InitializeArithmeticModel(&mtfmodel[3],16,31,4,1024); InitializeArithmeticModel(&mtfmodel[3],16,31,4,1024);
InitializeArithmeticModel(&mtfmodel[4],32,63,2,1024); InitializeArithmeticModel(&mtfmodel[4],32,63,2,1024);
InitializeArithmeticModel(&mtfmodel[5],64,127,2,1024); InitializeArithmeticModel(&mtfmodel[5],64,127,2,1024);
InitializeArithmeticModel(&mtfmodel[6],128,255,1,1024); InitializeArithmeticModel(&mtfmodel[6],128,255,1,1024);
int bits; int bits;
if (!NextArithmeticBitString(&decoder, &initialmodel, 8, bits) || bits != 'A') if (!NextArithmeticBitString(&decoder, &initialmodel, 8, bits) || bits != 'A')
return false; return false;
if (!NextArithmeticBitString(&decoder, &initialmodel, 8, bits) || bits != 's') if (!NextArithmeticBitString(&decoder, &initialmodel, 8, bits) || bits != 's')
return false; return false;
if (!NextArithmeticBitString(&decoder, &initialmodel, 4, blockbits)) if (!NextArithmeticBitString(&decoder, &initialmodel, 4, blockbits))
return false; return false;
blockbits+=9; blockbits+=9;
blocksize=1<<blockbits; blocksize=1<<blockbits;
numbytes=bytecount=0; numbytes=bytecount=0;
repeat=0; repeat=0;
delete[] block; delete[] block;
block=new uint8_t[blocksize]; block=new uint8_t[blocksize];
crc=0xffffffff; crc=0xffffffff;
compcrc=0; compcrc=0;
// check first end marker // check first end marker
int eobsym; int eobsym;
if (!NextArithmeticSymbol(&decoder, &initialmodel, eobsym)) if (!NextArithmeticSymbol(&decoder, &initialmodel, eobsym))
return false; return false;
endofblocks = (eobsym != 0); endofblocks = (eobsym != 0);
return true; return true;
} }
bool StuffItArsenicDecompressor::ReadBlock() bool StuffItArsenicDecompressor::ReadBlock()
{ {
ResetMTFDecoder(&mtf); ResetMTFDecoder(&mtf);
if (!NextArithmeticSymbol(&decoder, &initialmodel, randomized)) if (!NextArithmeticSymbol(&decoder, &initialmodel, randomized))
return false; return false;
if (!NextArithmeticBitString(&decoder, &initialmodel, blockbits, transformindex)) if (!NextArithmeticBitString(&decoder, &initialmodel, blockbits, transformindex))
return false; return false;
numbytes=0; numbytes=0;
for(;;) for(;;)
{ {
int sel; int sel;
if (!NextArithmeticSymbol(&decoder, &selectormodel, sel)) if (!NextArithmeticSymbol(&decoder, &selectormodel, sel))
return false; return false;
if(sel==0||sel==1) // zero counting if(sel==0||sel==1) // zero counting
{ {
int zerostate=1,zerocount=0; int zerostate=1,zerocount=0;
while(sel<2) while(sel<2)
{ {
if(sel==0) zerocount+=zerostate; if(sel==0) zerocount+=zerostate;
else if(sel==1) zerocount+=2*zerostate; else if(sel==1) zerocount+=2*zerostate;
zerostate*=2; zerostate*=2;
if (!NextArithmeticSymbol(&decoder, &selectormodel, sel)) if (!NextArithmeticSymbol(&decoder, &selectormodel, sel))
return false; return false;
} }
if (numbytes + zerocount > blocksize) if (numbytes + zerocount > blocksize)
return false; return false;
memset(&block[numbytes],DecodeMTF(&mtf,0),zerocount); memset(&block[numbytes],DecodeMTF(&mtf,0),zerocount);
numbytes+=zerocount; numbytes+=zerocount;
} }
int symbol; int symbol;
if(sel==10) if(sel==10)
break; break;
else if(sel==2) else if(sel==2)
symbol=1; symbol=1;
else else
{ {
if (!NextArithmeticSymbol(&decoder, &mtfmodel[sel - 3], symbol)) if (!NextArithmeticSymbol(&decoder, &mtfmodel[sel - 3], symbol))
return false; return false;
} }
if (numbytes >= blocksize) if (numbytes >= blocksize)
return false; return false;
block[numbytes++] = DecodeMTF(&mtf, symbol); block[numbytes++] = DecodeMTF(&mtf, symbol);
} }
if (transformindex >= numbytes) if (transformindex >= numbytes)
return false; return false;
ResetArithmeticModel(&selectormodel); ResetArithmeticModel(&selectormodel);
for(int i=0;i<7;i++) ResetArithmeticModel(&mtfmodel[i]); for(int i=0;i<7;i++) ResetArithmeticModel(&mtfmodel[i]);
int endMarkerSym; int endMarkerSym;
if (!NextArithmeticSymbol(&decoder, &initialmodel, endMarkerSym)) if (!NextArithmeticSymbol(&decoder, &initialmodel, endMarkerSym))
return false; return false;
if(endMarkerSym) // end marker if(endMarkerSym) // end marker
{ {
int compcrcsym; int compcrcsym;
if (!NextArithmeticBitString(&decoder, &initialmodel, 32, compcrcsym)) if (!NextArithmeticBitString(&decoder, &initialmodel, 32, compcrcsym))
return false; return false;
compcrc = compcrcsym; compcrc = compcrcsym;
endofblocks=true; endofblocks=true;
} }
free(transform); free(transform);
transform = new uint32_t[numbytes]; transform = new uint32_t[numbytes];
CalculateInverseBWT(transform,block,numbytes); CalculateInverseBWT(transform,block,numbytes);
return true; return true;
} }
bool StuffItArsenicDecompressor::ReadBytes(void *dest, size_t numBytes) bool StuffItArsenicDecompressor::ReadBytes(void *dest, size_t numBytes)
{ {
while (numBytes) while (numBytes)
{ {
uint8_t outbyte; uint8_t outbyte;
if (repeat) if (repeat)
{ {
repeat--; repeat--;
outbyte = last; outbyte = last;
} }
else else
{ {
retry: retry:
if (bytecount >= numbytes) if (bytecount >= numbytes)
{ {
if (endofblocks) if (endofblocks)
return false; return false;
if (!this->ReadBlock()) if (!this->ReadBlock())
return false; return false;
bytecount = 0; bytecount = 0;
count = 0; count = 0;
last = 0; last = 0;
randindex = 0; randindex = 0;
randcount = RandomizationTable[0]; randcount = RandomizationTable[0];
} }
transformindex = transform[transformindex]; transformindex = transform[transformindex];
int byte = block[transformindex]; int byte = block[transformindex];
if (randomized&&randcount == bytecount) if (randomized&&randcount == bytecount)
{ {
byte ^= 1; byte ^= 1;
randindex = (randindex + 1) & 255; randindex = (randindex + 1) & 255;
randcount += RandomizationTable[randindex]; randcount += RandomizationTable[randindex];
} }
bytecount++; bytecount++;
if (count == 4) if (count == 4)
{ {
count = 0; count = 0;
if (byte == 0) goto retry; if (byte == 0) goto retry;
repeat = byte - 1; repeat = byte - 1;
outbyte = last; outbyte = last;
} }
else else
{ {
if (byte == last) count++; if (byte == last) count++;
else { count = 1; last = byte; } else { count = 1; last = byte; }
outbyte = byte; outbyte = byte;
} }
} }
crc = XADCRC(crc, outbyte, XADCRCTable_edb88320); crc = XADCRC(crc, outbyte, XADCRCTable_edb88320);
uint8_t *destBytes = static_cast<uint8_t*>(dest); uint8_t *destBytes = static_cast<uint8_t*>(dest);
destBytes[0] = outbyte; destBytes[0] = outbyte;
numBytes--; numBytes--;
dest = destBytes + 1; dest = destBytes + 1;
} }
return true; return true;
} }