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
|
#import "XADSqueezeParser.h"
#import "XADSqueezeHandle.h"
#import "XADRLE90Handle.h"
#import "XADChecksumHandle.h"
#import "NSDateXAD.h"
@implementation XADSqueezeParser
+(NSMutableDictionary *)parseWithHandle:(CSHandle *)fh endOffset:(off_t)end parser:(XADArchiveParser *)parser
{
int magic1=[fh readUInt8];
if(magic1!=0x76) return nil;
int magic2=[fh readUInt8];
if(magic2!=0xff) return nil;
int sum=[fh readUInt16LE];
NSMutableData *data=[NSMutableData data];
uint8_t byte;
while((byte=[fh readUInt8])) [data appendBytes:&byte length:1];
off_t dataoffset=[fh offsetInFile];
NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[parser XADPathWithData:data separators:XADNoPathSeparator],XADFileNameKey,
[parser XADStringWithString:@"Squeeze"],XADCompressionNameKey,
[NSNumber numberWithUnsignedLongLong:dataoffset],XADDataOffsetKey,
[NSNumber numberWithInt:sum],@"SqueezeChecksum",
nil];
[fh seekToFileOffset:end-8];
int marker=[fh readUInt16LE];
if(marker==0xff77)
{
// TODO: Test this.
int date=[fh readUInt16LE];
int time=[fh readUInt16LE];
[dict setObject:[NSDate XADDateWithMSDOSDate:date time:time] forKey:XADLastModificationDateKey];
NSNumber *compsize=[NSNumber numberWithLongLong:end-dataoffset-8];
[dict setObject:compsize forKey:XADCompressedSizeKey];
[dict setObject:compsize forKey:XADDataLengthKey];
}
else
{
NSNumber *compsize=[NSNumber numberWithLongLong:end-dataoffset];
[dict setObject:compsize forKey:XADCompressedSizeKey];
[dict setObject:compsize forKey:XADDataLengthKey];
}
return dict;
}
+(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum handle:(CSHandle *)handle
{
int sum=[[dict objectForKey:@"SqueezeChecksum"] intValue];
handle=[[[XADSqueezeHandle alloc] initWithHandle:handle] autorelease];
handle=[[[XADRLE90Handle alloc] initWithHandle:handle] autorelease];
if(checksum) handle=[[[XADChecksumHandle alloc] initWithHandle:handle
length:CSHandleMaxLength correctChecksum:sum mask:0xffff] autorelease];
return handle;
}
+(int)requiredHeaderSize { return 1024; }
+(BOOL)recognizeFileWithHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
const uint8_t *bytes=[data bytes];
int length=[data length];
if(length<5) return NO;
if(bytes[0]!=0x76||bytes[1]!=0xff) return NO;
if(bytes[4]==0) return NO;
for(int i=4;i<length;i++)
{
if(bytes[i]==0)
{
return YES;
}
if(bytes[i]<32) return NO;
}
return NO;
}
-(void)parse
{
CSHandle *fh=[self handle];
NSMutableDictionary *dict=[XADSqueezeParser parseWithHandle:fh
endOffset:[fh fileSize] parser:self];
XADPath *filename=[dict objectForKey:XADFileNameKey];
NSData *namedata=[filename data];
const char *bytes=[namedata bytes];
int length=[namedata length];
if(length>4)
if(bytes[length-4]=='.')
if(tolower(bytes[length-3])=='l')
if(tolower(bytes[length-2])=='b')
if(tolower(bytes[length-1])=='r')
{
[dict setObject:[NSNumber numberWithBool:YES] forKey:XADIsArchiveKey];
}
[self addEntryWithDictionary:dict];
}
-(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum
{
CSHandle *handle=[self handleAtDataOffsetForDictionary:dict];
return [XADSqueezeParser handleForEntryWithDictionary:dict wantChecksum:checksum handle:handle];
}
-(NSString *)formatName { return @"Squeeze"; }
@end
|