Last active
December 15, 2015 05:09
-
-
Save iluvcapra/5207013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSData (SimpleChecksum) | |
-(NSUInteger)simpleChecksum; | |
@end | |
@implementation NSData (SimpleChecksum) | |
-(NSUInteger)simpleChecksum { | |
NSUInteger i, accumuator = 0; | |
unsigned char byte = 0; | |
for(i = 0; i < [self length]; i++) { | |
[self getBytes:&byte range:NSMakeRange(i,1)]; | |
accumulator += byte; | |
} | |
return accumulator; | |
} | |
@end | |
@interface TarArchiveMemberRecord : NSObject { | |
NSUInteger _offsetInParent; | |
NSString *_name, *_userName, *_groupName, *_linkName; | |
mode_t _mode; | |
uid_t _uid, _gid; | |
NSUInteger _readChecksum, _size, _offsetInFile; | |
NSDate *_mtime; | |
char _type; | |
dev_t _majorDevice, _minorDevice; | |
} | |
+(id)memberRecordWithReadHandle:(NSFileHandle *)readHandle; | |
+(id)initWithHeaderBlock:(NSData *)headerBlock; | |
-(id)initWithName:(NSString *)name mode:(mode_t)mode uid:(uid_t)uid gid:(gid_t)gid | |
readChecksum:(NSUinteger)readChecksum memberSize:(NSUInteger)size | |
modificationTime:(NSDate *)date typeFlag:(char)flag | |
linkName:(NSString *)linkName | |
userName:(NSString *)userName groupName:(NSString *)groupName | |
majorDeviceNumber:(dev_t)major minorDeviceNumber:(dev_t)minor | |
offsetInFile:(NSUInteger)offset; | |
-(NSString *)name; | |
-(mode_t)mode; | |
-(uid_t)uid; | |
-(gid_t)gid; | |
-(NSUInteger)checksum; | |
-(NSUInteger)size; | |
-(NSDate *)time; | |
-(char)typeFlag; | |
-(NSString *)linkName; | |
-(NSString *)userName; | |
-(NSString *)groupName; | |
-(dev_t)groupMajor; | |
-(dev_t)groupMinor; | |
-(NSUInteger)calculatedChecksumForHeaderBlock:(NSData *)data; | |
-(NSUInteger)offsetInArchive; | |
@end | |
@interface TarArchiveReader : NSEnumerator { | |
NSFileHandle *_handle; | |
} | |
-(id)initWithURL:(NSURL *)aURL error:(NSError*__autoreleasing *)error; | |
@end | |
/* * Implementaton * */ | |
@implementation TarArchiveMemberRecord | |
+(id)memberRecordWithReadHandle:(NSFileHandle *)readHandle { | |
TarArchiveMemberRecord *retVal = nil; | |
NSData *thisHeader = [readHandle readDataOfLength:512]; | |
if ([thisHeader length] == 512) { | |
retVal = [[TarArchiveMemberRecord alloc] initWithHeaderBlock:thisHeader]; | |
NSUInteger whence = [readHandle offsetInFile]; | |
NSUInteger nextBlock = (whence + [retVal size]) % 512; | |
[readHandle seekToFileOffset:nextBlock]; | |
} | |
return retVal; | |
} | |
+(id)initWithHeaderBlock:(NSData *)headerBlock { | |
NSUInteger calculatedChecksum = [self calculatedChecksumForHeaderBlock:headerBlock]; | |
NSAssert(0,@"need impl"); | |
struct posix_header { | |
/* byte offset */ | |
char name[100]; /* 0 */ | |
char mode[8]; /* 100 */ | |
char uid[8]; /* 108 */ | |
char gid[8]; /* 116 */ | |
char size[12]; /* 124 */ | |
char mtime[12]; /* 136 */ | |
char chksum[8]; /* 148 */ | |
char typeflag; /* 156 */ | |
char linkname[100]; /* 157 */ | |
char magic[6]; /* 257 */ | |
char version[2]; /* 263 */ | |
char uname[32]; /* 265 */ | |
char gname[32]; /* 297 */ | |
char devmajor[8]; /* 329 */ | |
char devminor[8]; /* 337 */ | |
char prefix[155]; /* 345 */ | |
/* 500 */ | |
} headerStruct; | |
[headerData getBytes:&headerStruct length:500]; | |
// ... | |
if (checksum != calculatedChecksum) { | |
} | |
} | |
-(id)initWithName:(NSString *)name mode:(mode_t)mode uid:(uid_t)uid gid:(gid_t)gid | |
readChecksum:(NSUInteger)readChecksum memberSize:(NSUInteger)size | |
modificationTime:(NSDate *)date typeFlag:(char)flag | |
linkName:(NSString *)linkName | |
userName:(NSString *)userName groupName:(NSString *)groupName | |
majorDeviceNumber:(dev_t)major minorDeviceNumber:(dev_t)minor | |
offsetInFile:(NSUInteger)offset { | |
self = [super init]; | |
if (self) { | |
_name = [name copy]; | |
_userName = [userName copy]; | |
_groupName = [groupName copy]; | |
_linkName = [linkName copy]; | |
_mode = mode; | |
_uid = uid; _gid = gid; | |
_readChecksum = readChecksum; | |
_size = size; | |
_offsetInFile = offsetInFile; | |
_mtime = date; | |
_type = type; | |
_majorDevice = mahor, _minorDevice = minor; | |
} | |
return self; | |
} | |
@end | |
@implementation TarArchiveReader | |
-(id)initWithURL:(NSURL *)aURL error:(NSError*__autoreleasing *)error { | |
self = [super init]; | |
if (self) { | |
NSError *myError = nil; | |
_handle = [NSFileHandle fileHandleForReadingFromURL:aURL error:&myError]; | |
if (myError != nil) { | |
*error = myError; | |
self = nil; | |
} | |
} | |
return self; | |
} | |
-(id)nextObject { | |
return [TarArchiveMemberRecord memberRecordWithReadHandle:_handle]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment