Skip to content

Commit

Permalink
Adding support to password protected files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ararog committed Oct 7, 2012
1 parent 89252d0 commit 839360e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Unrar4iOS/Unrar4iOS.h
Expand Up @@ -16,11 +16,14 @@
struct RARHeaderDataEx *header;
struct RAROpenArchiveDataEx *flags;
NSString *filename;
NSString *password;
}

@property(nonatomic, retain) NSString* filename;
@property(nonatomic, retain) NSString* password;

-(BOOL) unrarOpenFile:(NSString*) rarFile;
-(BOOL) unrarOpenFile:(NSString*) rarFile withPassword:(NSString*) aPassword;
-(NSArray *) unrarListFiles;
-(BOOL) unrarFileTo:(NSString*) path overWrite:(BOOL) overwrite;
-(NSData *) extractStream:(NSString *)aFile;
Expand Down
45 changes: 32 additions & 13 deletions Unrar4iOS/Unrar4iOS.mm
Expand Up @@ -9,14 +9,15 @@
#import "Unrar4iOS.h"

@interface Unrar4iOS(PrivateMethods)
-(BOOL)_unrarOpenFile:(NSString*)rarFile mode:(NSInteger)mode;
-(BOOL)_unrarOpenFile:(NSString*)rarFile password:(NSString*)password;
-(BOOL)_unrarOpenFile:(NSString*)rarFile inMode:(NSInteger)mode;
-(BOOL)_unrarOpenFile:(NSString*)rarFile withpassword:(NSString*)password;
-(BOOL)_unrarOpenFile:(NSString*)rarFile inMode:(NSInteger)mode withPassword:(NSString*)password;
-(BOOL)_unrarCloseFile;
@end

@implementation Unrar4iOS

@synthesize filename;
@synthesize filename, password;

int CALLBACK CallbackProc(UINT msg, long UserData, long P1, long P2) {
UInt8 **buffer;
Expand All @@ -38,12 +39,29 @@ int CALLBACK CallbackProc(UINT msg, long UserData, long P1, long P2) {
}

-(BOOL) unrarOpenFile:(NSString*)rarFile {

return [self unrarOpenFile:rarFile withPassword:nil];
}

-(BOOL) unrarOpenFile:(NSString*)rarFile withPassword:(NSString *)aPassword {

self.filename = rarFile;
self.password = aPassword;
return YES;
}

-(BOOL) _unrarOpenFile:(NSString*)rarFile mode:(NSInteger)mode{
-(BOOL) _unrarOpenFile:(NSString*)rarFile inMode:(NSInteger)mode{

return [self _unrarOpenFile:rarFile inMode:mode withPassword:nil];
}

-(BOOL) _unrarOpenFile:(NSString*)rarFile withPassword:(NSString*)aPassword {

return [self _unrarOpenFile:rarFile inMode:RAR_OM_LIST withPassword:aPassword];
}

- (BOOL)_unrarOpenFile:(NSString *)rarFile inMode:(NSInteger)mode withPassword:(NSString *)aPassword {

header = new RARHeaderDataEx;
flags = new RAROpenArchiveDataEx;

Expand All @@ -59,19 +77,19 @@ -(BOOL) _unrarOpenFile:(NSString*)rarFile mode:(NSInteger)mode{
return NO;

header->CmtBuf = NULL;

return YES;
}

-(BOOL) _unrarOpenFile:(NSString*)rarFile password:(NSString*)password {
return NO;
if(aPassword != nil) {
char *_password = (char *) [aPassword UTF8String];
RARSetPassword(_rarFile, _password);
}
return YES;
}

-(NSArray *) unrarListFiles {
int RHCode = 0, PFCode = 0;

[self _unrarOpenFile:filename mode:RAR_OM_LIST];
[self _unrarOpenFile:filename inMode:RAR_OM_LIST withPassword:password];

NSMutableArray *files = [NSMutableArray array];
while ((RHCode = RARReadHeaderEx(_rarFile, header)) == 0) {
Expand Down Expand Up @@ -99,7 +117,7 @@ -(NSData *) extractStream:(NSString *)aFile {

int RHCode = 0, PFCode = 0;

[self _unrarOpenFile:filename mode:RAR_OM_EXTRACT];
[self _unrarOpenFile:filename inMode:RAR_OM_EXTRACT withPassword:password];

while ((RHCode = RARReadHeaderEx(_rarFile, header)) == 0) {
NSString *_filename = [NSString stringWithCString:header->FileName encoding:NSASCIIStringEncoding];
Expand Down Expand Up @@ -148,6 +166,7 @@ -(BOOL) unrarCloseFile {

-(void) dealloc {
[filename release];
[password release];
[super dealloc];
}

Expand Down
Binary file not shown.

0 comments on commit 839360e

Please sign in to comment.