1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#import "XADZipImplodeHandle.h"
#import "XADException.h"
@implementation XADZipImplodeHandle
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
largeDictionary:(BOOL)largedict hasLiterals:(BOOL)hasliterals
{
if((self=[super initWithHandle:handle length:length windowSize:largedict?8192:4096]))
{
if(largedict) offsetbits=7;
else offsetbits=6;
literals=hasliterals;
literalcode=lengthcode=offsetcode=nil;
}
return self;
}
-(void)dealloc
{
[literalcode release];
[lengthcode release];
[offsetcode release];
[super dealloc];
}
-(void)resetLZSSHandle
{
[literalcode release];
[lengthcode release];
[offsetcode release];
literalcode=lengthcode=offsetcode=nil;
if(literals) literalcode=[self allocAndParseCodeOfSize:256];
lengthcode=[self allocAndParseCodeOfSize:64];
offsetcode=[self allocAndParseCodeOfSize:64];
}
-(XADPrefixCode *)allocAndParseCodeOfSize:(int)size
{
int numgroups=CSInputNextByte(input)+1;
int codelengths[size],currcode=0;
for(int i=0;i<numgroups;i++)
{
int val=CSInputNextByte(input);
int num=(val>>4)+1;
int length=(val&0x0f)+1;
while(num--) codelengths[currcode++]=length;
}
if(currcode!=size) [XADException raiseDecrunchException];
return [[XADPrefixCode alloc] initWithLengths:codelengths numberOfSymbols:size maximumLength:16 shortestCodeIsZeros:NO];
}
-(void)expandFromPosition:(off_t)pos;
{
while(XADLZSSShouldKeepExpanding(self))
{
if(CSInputNextBitLE(input))
{
if(literals) XADEmitLZSSLiteral(self,CSInputNextSymbolUsingCodeLE(input,literalcode),NULL);
else XADEmitLZSSLiteral(self,CSInputNextBitStringLE(input,8),NULL);
}
else
{
int offset=CSInputNextBitStringLE(input,offsetbits);
offset|=CSInputNextSymbolUsingCodeLE(input,offsetcode)<<offsetbits;
offset+=1;
int length=CSInputNextSymbolUsingCodeLE(input,lengthcode)+2;
if(length==65) length+=CSInputNextBitStringLE(input,8);
if(literals) length++;
XADEmitLZSSMatch(self,offset,length,NULL);
}
}
}
@end
|