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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#import "XADLZXHandle.h"
#import "XADException.h"
@implementation XADLZXHandle
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
{
if((self=[super initWithHandle:[[[XADLZXSwapHandle alloc] initWithHandle:handle] autorelease]
length:length windowSize:65536]))
{
maincode=offsetcode=nil;
}
return self;
}
-(void)dealloc
{
[maincode release];
[offsetcode release];
[super dealloc];
}
-(void)resetLZSSHandle
{
lastoffs=1;
blockend=0;
memset(mainlengths,0,sizeof(mainlengths));
}
-(int)nextLiteralOrOffset:(int *)offset andLength:(int *)length atPosition:(off_t)pos
{
static const unsigned char AdditionalBitsTable[32]=
{
0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14
};
static const unsigned int BaseTable[32]=
{
0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,
1536,2048,3072,4096,6144,8192,12288,16384,24576,32768,49152
};
if(pos>=blockend) [self readBlockHeaderAtPosition:pos];
int symbol=CSInputNextSymbolUsingCodeLE(input,maincode);
if(symbol<256) return symbol;
int offsclass=symbol&31;
int offs=BaseTable[offsclass];
int offsbits=AdditionalBitsTable[offsclass];
if(offs==0)
{
offs=lastoffs;
}
else if(blocktype==3 && offsbits>=3)
{
offs+=CSInputNextBitStringLE(input,offsbits-3)<<3;
offs+=CSInputNextSymbolUsingCodeLE(input,offsetcode);
}
else
{
offs+=CSInputNextBitStringLE(input,offsbits);
}
int lenclass=((symbol-256)>>5)&15;
int len=BaseTable[lenclass]+3;
int lenbits=AdditionalBitsTable[lenclass];
len+=CSInputNextBitStringLE(input,lenbits);
*offset=offs;
*length=len;
lastoffs=offs;
return XADLZSSMatch;
}
-(void)readBlockHeaderAtPosition:(off_t)pos
{
[maincode release];
[offsetcode release];
maincode=offsetcode=nil;
blocktype=CSInputNextBitStringLE(input,3);
if(blocktype<1||blocktype>3) [XADException raiseIllegalDataException];
if(blocktype==1) [XADException raiseNotSupportedException]; // Never encountered one of these?
if(blocktype==3)
{
int codelengths[8];
for(int i=0;i<8;i++) codelengths[i]=CSInputNextBitStringLE(input,3);
offsetcode=[[XADPrefixCode alloc] initWithLengths:codelengths
numberOfSymbols:8 maximumLength:7 shortestCodeIsZeros:YES];
}
int blocksize=CSInputNextBitStringLE(input,8)<<16;
blocksize|=CSInputNextBitStringLE(input,8)<<8;
blocksize|=CSInputNextBitStringLE(input,8);
blockend=pos+blocksize;
if(blocktype!=1)
{
[self readDeltaLengths:&mainlengths[0] count:256 alternateMode:NO];
[self readDeltaLengths:&mainlengths[256] count:512 alternateMode:YES];
maincode=[[XADPrefixCode alloc] initWithLengths:mainlengths
numberOfSymbols:768 maximumLength:16 shortestCodeIsZeros:YES];
}
}
-(void)readDeltaLengths:(int *)lengths count:(int)count alternateMode:(BOOL)altmode;
{
XADPrefixCode *precode=nil;
int fix=altmode?1:0;
@try
{
int prelengths[20];
for(int i=0;i<20;i++) prelengths[i]=CSInputNextBitStringLE(input,4);
precode=[[XADPrefixCode alloc] initWithLengths:prelengths
numberOfSymbols:20 maximumLength:15 shortestCodeIsZeros:YES];
int i=0;
while(i<count)
{
int val=CSInputNextSymbolUsingCodeLE(input,precode);
int n,length;
if(val<=16)
{
n=1;
length=(lengths[i]+17-val)%17;
}
else if(val==17)
{
n=CSInputNextBitStringLE(input,4)+4-fix;
length=0;
}
else if(val==18)
{
n=CSInputNextBitStringLE(input,5+fix)+20-fix;
length=0;
}
else if(val==19)
{
n=CSInputNextBitStringLE(input,1)+4-fix;
int newval=CSInputNextSymbolUsingCodeLE(input,precode);
length=(lengths[i]+17-newval)%17;
}
for(int j=0;j<n;j++) lengths[i+j]=length;
i+=n;
}
[precode release];
}
@catch(id e)
{
[precode release];
@throw;
}
}
@end
@implementation XADLZXSwapHandle
-(uint8_t)produceByteAtOffset:(off_t)pos
{
if(pos&1) return otherbyte;
if(CSInputAtEOF(input)) CSByteStreamEOF(self);
otherbyte=CSInputNextByte(input);
if(CSInputAtEOF(input)) CSByteStreamEOF(self);
return CSInputNextByte(input);
}
@end
|