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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#import "XADArchiveParser.h"
#import "XADTestUtilities.h"
#import "CSFileHandle.h"
#import "XADRegex.h"
CSHandle *HandleForLocators(NSArray *locators,NSString **nameptr);
@interface EntryFinder:NSObject
{
int count,entrynum;
XADRegex *regex;
NSDictionary *entry;
}
-(id)initWithLocator:(NSString *)string;
@end
@interface XADTest:NSObject {}
+(void)testByte:(uint8_t)byte atOffset:(off_t)offset;
@end
//[NSClassFromString(@"XADTest") testByte:byte atOffset:pos];
const char *cstr1,*cstr2;
CSHandle *correcthandle;
off_t correctoffset;
const uint8_t *correctbytes;
off_t correctlength;
int main(int argc,char **argv)
{
NSAutoreleasePool *pool=[NSAutoreleasePool new];
if(argc==2)
{
cstr1=argv[1];
NSString *filename=[NSString stringWithUTF8String:cstr1];
NSArray *locators=[filename componentsSeparatedByString:@":"];
CSHandle *fh=HandleForLocators(locators,NULL);
if(!fh)
{
fprintf(stderr,"Failed to open %s.\n",cstr1);
exit(1);
}
off_t size=0;
while(![fh atEndOfFile])
{
uint8_t b=[fh readUInt8];
putc(b,stdout);
size++;
}
fflush(stdout);
fprintf(stderr,"\nRead %ld bytes from %s.\n",(long)size,cstr1);
}
else if(argc==3)
{
cstr1=argv[1];
cstr2=argv[2];
NSString *filename1=[NSString stringWithUTF8String:cstr1];
NSArray *locators1=[filename1 componentsSeparatedByString:@":"];
CSHandle *fh=HandleForLocators(locators1,NULL);
if(!fh)
{
fprintf(stderr,"Failed to open %s.\n",cstr1);
exit(1);
}
NSString *filename2=[NSString stringWithUTF8String:cstr2];
NSArray *locators2=[filename2 componentsSeparatedByString:@":"];
if([locators2 count]>1)
{
correctbytes=NULL;
correcthandle=HandleForLocators(locators2,NULL);
if(!correcthandle)
{
fprintf(stderr,"Failed to open %s.\n",cstr2);
exit(1);
}
}
else
{
correcthandle=nil;
NSData *data=[NSData dataWithContentsOfMappedFile:filename2];
correctbytes=[data bytes];
correctlength=[data length];
}
if([fh isKindOfClass:[CSSubHandle class]])
{
correctoffset=[(CSSubHandle *)fh startOffsetInParent];
}
else
{
correctoffset=0;
}
off_t size=0;
while(![fh atEndOfFile])
{
uint8_t b=[fh readUInt8];
[XADTest testByte:b atOffset:size];
size++;
}
if(correcthandle && ![correcthandle atEndOfFile])
{
fprintf(stderr,"%s ended before %s, after %ld bytes.\n",cstr1,cstr2,(long)size);
exit(1);
}
fprintf(stderr,"Read %ld bytes from %s and %s, which are identical.\n",
(long)size,cstr1,cstr2);
}
else
{
printf("Usage: %s file[:archiveentry[:...]] [comparefile[:archiveentry[:...]]]\n",argv[0]);
exit(1);
}
[pool release];
return 0;
}
@implementation XADTest
+(void)testByte:(uint8_t)byte atOffset:(off_t)offset
{
//offset-=correctoffset;
if(offset<0) [NSException raise:NSInvalidArgumentException format:@"Offset before start of solid segment"];
if(correctbytes)
{
if(offset>=correctlength)
{
fprintf(stderr,"%s ended before %s, after %ld bytes.\n",cstr2,cstr1,(long)offset);
exit(1);
}
uint8_t correctbyte=correctbytes[offset];
if(byte!=correctbyte)
{
fprintf(stderr,"Mismatch between %s and %s, starting at byte "
"%ld (%02x vs. %02x).\n",cstr1,cstr2,(long)offset,byte,correctbyte);
exit(1);
}
}
else
{
[correcthandle seekToFileOffset:offset];
if([correcthandle atEndOfFile])
{
fprintf(stderr,"%s ended before %s, after %ld bytes.\n",cstr2,cstr1,(long)offset);
exit(1);
}
uint8_t correctbyte=[correcthandle readUInt8];
if(byte!=correctbyte)
{
fprintf(stderr,"Mismatch between %s and %s, starting at byte "
"%ld (%02x vs. %02x).\n",cstr1,cstr2,(long)offset,byte,correctbyte);
exit(1);
}
}
}
@end
@implementation EntryFinder
CSHandle *HandleForLocators(NSArray *locators,NSString **nameptr)
{
if([locators count]==1)
{
NSString *filename=[locators lastObject];
if(nameptr) *nameptr=filename;
return [CSFileHandle fileHandleForReadingAtPath:filename];
}
else
{
NSString *locator=[locators lastObject];
NSArray *parentlocators=[locators subarrayWithRange:NSMakeRange(0,[locators count]-1)];
NSString *parentname;
CSHandle *parenthandle=HandleForLocators(parentlocators,&parentname);
if(!parenthandle) return nil;
XADArchiveParser *parser=[XADArchiveParser archiveParserForHandle:parenthandle name:parentname];
NSString *pass=FigureOutPassword(parentname);
if(pass) [parser setPassword:pass];
EntryFinder *finder=[[[EntryFinder alloc] initWithLocator:locator] autorelease];
[parser setDelegate:finder];
[parser parse];
if(!finder->entry) return nil;
if(nameptr) *nameptr=[[finder->entry objectForKey:XADFileNameKey] string];
return [parser handleForEntryWithDictionary:finder->entry wantChecksum:NO];
}
}
-(id)initWithLocator:(NSString *)locator
{
if((self=[super init]))
{
count=-1;
entrynum=-1;
regex=nil;
entry=nil;
NSArray *matches=[locator substringsCapturedByPattern:@"^#([0-9]+)$"];
if(matches)
{
entrynum=[[matches objectAtIndex:1] intValue];
}
else
{
regex=[[XADRegex regexWithPattern:[XADRegex patternForGlob:locator]] retain];
}
}
return self;
}
-(void)dealloc
{
[regex release];
[entry release];
[super dealloc];
}
-(void)archiveParser:(XADArchiveParser *)parser foundEntryWithDictionary:(NSDictionary *)dict
{
count++;
NSNumber *dir=[dict objectForKey:XADIsDirectoryKey];
if(entrynum>=0 && entrynum==count)
{
entry=[dict retain];
return;
}
if(dir&&[dir boolValue]) return;
if(regex && [regex matchesString:[[dict objectForKey:XADFileNameKey] string]])
{
entry=[dict retain];
return;
}
}
-(BOOL)archiveParsingShouldStop:(XADArchiveParser *)parser
{
return entry!=nil;
}
@end
|