Permalink
Browse files

Adding support to password protected files.

  • Loading branch information...
1 parent 89252d0 commit 839360ec391587d29c49e0848a25974b550c5ed4 @ararog committed Oct 7, 2012
View
@@ -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;
View
@@ -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;
@@ -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;
@@ -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) {
@@ -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];
@@ -148,6 +166,7 @@ -(BOOL) unrarCloseFile {
-(void) dealloc {
[filename release];
+ [password release];
[super dealloc];
}

0 comments on commit 839360e

Please sign in to comment.