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
|
#import "XADZipCryptHandle.h"
#import "XADException.h"
#import "CRC.h"
@implementation XADZipCryptHandle
static void UpdateKeys(XADZipCryptHandle *self,uint8_t b)
{
self->key0=XADCRC(self->key0,b,XADCRCTable_edb88320);
self->key1+=self->key0&0xff;
self->key1=self->key1*134775813+1;
self->key2=XADCRC(self->key2,self->key1>>24,XADCRCTable_edb88320);
}
static uint8_t DecryptByte(XADZipCryptHandle *self)
{
uint16_t temp=self->key2|2;
return (temp*(temp^1))>>8;
}
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length password:(NSData *)passdata testByte:(uint8_t)testbyte
{
if((self=[super initWithHandle:handle length:length-12]))
{
password=[passdata retain];
test=testbyte;
}
return self;
}
-(void)dealloc
{
[password release];
[super dealloc];
}
-(void)resetByteStream
{
key0=305419896;
key1=591751049;
key2=878082192;
int passlength=[password length];
const uint8_t *passbytes=[password bytes];
for(int i=0;i<passlength;i++) UpdateKeys(self,passbytes[i]);
for(int i=0;i<12;i++)
{
uint8_t b=CSInputNextByte(input)^DecryptByte(self);
UpdateKeys(self,b);
if(i==11&&b!=test) [XADException raisePasswordException];
}
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
uint8_t b=CSInputNextByte(input)^DecryptByte(self);
//NSLog(@"%02x",b);
UpdateKeys(self,b);
return b;
}
@end
|