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
|
#import "XADPowerPackerParser.h"
#import "CSMemoryHandle.h"
#import "XADException.h"
static NSData *PowerPackerUnpack(NSData *packeddata,int unpackedlength);
@implementation XADPowerPackerParser
+(int)requiredHeaderSize { return 4; }
+(BOOL)recognizeFileWithHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
int length=[data length];
const uint8_t *bytes=[data bytes];
return length>=8&&bytes[0]=='P'&&bytes[1]=='P'&&bytes[2]=='2'&&bytes[3]=='0';
}
-(void)parse
{
CSHandle *fh=[self handle];
[fh seekToEndOfFile];
off_t compsize=[fh offsetInFile]-4;
[fh skipBytes:-4];
int size=[fh readUInt8]<<16;
size|=[fh readUInt8]<<8;
size|=[fh readUInt8];
[self addEntryWithDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:
[self XADPathWithUnseparatedString:[[self name] stringByDeletingPathExtension]],XADFileNameKey,
[NSNumber numberWithLongLong:size],XADFileSizeKey,
[NSNumber numberWithLongLong:compsize],XADCompressedSizeKey,
[self XADStringWithString:@"PowerPacker"],XADCompressionNameKey,
[NSNumber numberWithLongLong:4],XADDataOffsetKey,
[NSNumber numberWithLongLong:compsize],XADDataLengthKey,
nil]];
}
-(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum
{
NSData *data=[dict objectForKey:@"PowerPackerFileContents"];
if(!data)
{
CSHandle *handle=[self handleAtDataOffsetForDictionary:dict];
data=PowerPackerUnpack([handle remainingFileContents],[[dict objectForKey:XADFileSizeKey] intValue]);
[(NSMutableDictionary *)dict setObject:data forKey:@"PowerPackerFileContents"];
}
return [CSMemoryHandle memoryHandleForReadingData:data];
}
-(NSString *)formatName { return @"PowerPacker"; }
@end
static uint32_t GetBits(int n,const uint8_t *buffer,int *bitpos)
{
uint32_t result=0;
for(int i=0;i<n;i++)
{
(*bitpos)--;
if(*bitpos<0) [XADException raiseDecrunchException];
int currbyte=*bitpos/8;
int currbit=7-(*bitpos&7);
result=(result<<1)|((buffer[currbyte]>>currbit)&1);
}
return result;
}
static NSData *PowerPackerUnpack(NSData *packeddata,int unpackedlength)
{
const uint8_t *packed=[packeddata bytes];
int packedlength=[packeddata length];
NSMutableData *unpackeddata=[NSMutableData dataWithLength:unpackedlength];
uint8_t *unpacked=[unpackeddata mutableBytes];
int bitpos=packedlength*8-32;
uint8_t *dest=unpacked+unpackedlength;
// Skip extra bits
GetBits(packed[packedlength-1],packed,&bitpos);
for(;;)
{
if(GetBits(1,packed,&bitpos)==0) // copy some bytes from the source
{
int add,numbytes=1;
do
{
add=GetBits(2,packed,&bitpos);
numbytes+=add;
} while(add==3);
for(int i=0;i<numbytes;i++)
{
if(dest<=unpacked) [XADException raiseDecrunchException];
*--dest=GetBits(8,packed,&bitpos);
}
if(dest==unpacked) return unpackeddata;
}
// decode what to copy from the destination file
int index=GetBits(2,packed,&bitpos);
int numbits=packed[index];
int numbytes=index+2;
int offset;
if(numbytes==5) // 5 means >=5
{
// and maybe a bigger offset
if(GetBits(1,packed,&bitpos)==0) offset=GetBits(7,packed,&bitpos);
else offset=GetBits(numbits,packed,&bitpos);
int add;
do {
add=GetBits(3,packed,&bitpos);
numbytes+=add;
} while(add==7);
}
else offset=GetBits(numbits,packed,&bitpos);
for(int i=0;i<numbytes;i++)
{
if(dest<=unpacked) [XADException raiseDecrunchException];
dest[-1]=dest[offset];
dest--;
}
if(dest==unpacked) return unpackeddata;
}
return nil; // can't happen
}
|