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
|
#import "XADCrunchParser.h"
#import "XADCrunchHandles.h"
#import "XADRLE90Handle.h"
#import "XADChecksumHandle.h"
#import "NSDateXAD.h"
@implementation XADCrunchParser
+(NSMutableDictionary *)parseWithHandle:(CSHandle *)fh endOffset:(off_t)end parser:(XADArchiveParser *)parser
{
int magic=[fh readUInt8];
if(magic!=0x76) return nil;
int type=[fh readUInt8];
if(type!=0xfe && type!=0xfd) return nil;
NSMutableData *data=[NSMutableData data];
uint8_t byte;
while((byte=[fh readUInt8])) [data appendBytes:&byte length:1];
const char *bytes=[data bytes];
int length=[data length];
NSData *namepart=data;
NSData *comment=nil;
int namelength=length;
for(int i=0;i<length;i++)
{
if(bytes[i]=='.')
{
namelength=i+4;
if(namelength>length||bytes[namelength-1]==' ') namelength--;
if(namelength>length||bytes[namelength-1]==' ') namelength--;
if(namelength>length||bytes[namelength-1]==' ') namelength--;
namepart=[data subdataWithRange:NSMakeRange(0,namelength)];
if(i+7<length && bytes[i+4]==' ' && bytes[i+5]=='[' && bytes[length-1]==']')
comment=[data subdataWithRange:NSMakeRange(i+6,length-i-7)];
break;
}
}
int version1=[fh readUInt8];
int version2=[fh readUInt8];
int errordetection=[fh readUInt8];
[fh skipBytes:1];
NSString *compname;
BOOL old=(version2&0xf0)==0x10;
if(type==0xfe)
{
if(old) compname=@"LZW 1.0";
else compname=@"LZW 2.0";
}
else
{
if(old) compname=@"LZHUF 1.0";
else compname=@"LZHUF 2.0";
}
off_t dataoffset=[fh offsetInFile];
NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[parser XADPathWithData:namepart separators:XADNoPathSeparator],XADFileNameKey,
[parser XADStringWithString:compname],XADCompressionNameKey,
[NSNumber numberWithLongLong:end-dataoffset-2],XADCompressedSizeKey,
[NSNumber numberWithUnsignedLongLong:dataoffset],XADDataOffsetKey,
[NSNumber numberWithLongLong:end-dataoffset],XADDataLengthKey,
[NSNumber numberWithInt:type],@"CrunchType",
[NSNumber numberWithInt:version1],@"CrunchReferenceRevision",
[NSNumber numberWithInt:version2],@"CrunchSignificantRevision",
[NSNumber numberWithInt:errordetection],@"CrunchErrorDetection",
nil];
if(comment) [dict setObject:[parser XADStringWithData:comment] forKey:XADCommentKey];
return dict;
}
+(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum handle:(CSHandle *)handle
{
int type=[[dict objectForKey:@"CrunchType"] intValue];
int version2=[[dict objectForKey:@"CrunchSignificantRevision"] intValue];
int errordetection=[[dict objectForKey:@"CrunchErrorDetection"] intValue];
BOOL old=(version2&0xf0)==0x10;
BOOL haschecksum=errordetection==0;
if(type==0xfe) handle=[[[XADCrunchZHandle alloc] initWithHandle:handle old:old hasChecksum:haschecksum] autorelease];
else handle=[[[XADCrunchYHandle alloc] initWithHandle:handle old:old hasChecksum:haschecksum] 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<9) return NO;
if(bytes[0]!=0x76 || (bytes[1]!=0xfe && bytes[1]!=0xfd)) return NO;
if(bytes[2]==0) return NO;
for(int i=2;i<length;i++)
{
if(bytes[i]==0)
{
if(i+4>length) return NO;
if(bytes[i+1]<0x10||bytes[i+1]>0x2f) return NO;
if(bytes[i+2]<0x10||bytes[i+2]>0x2f) return NO;
return YES;
}
//if(bytes[i]<32) return NO;
}
return NO;
}
-(void)parse
{
CSHandle *fh=[self handle];
NSMutableDictionary *dict=[XADCrunchParser 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 [XADCrunchParser handleForEntryWithDictionary:dict wantChecksum:checksum handle:handle];
}
-(NSString *)formatName { return @"Crunch"; }
@end
|