mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-22 16:29:23 +00:00
Started Move to KeePassKit - Project compiles but does NOT work properly
Removed MiniKeePass Categories. Moved Random streams form MiniKeePass to KeePassKit Changed MacPass to use KeePassKit
This commit is contained in:
@@ -8,169 +8,21 @@
|
||||
|
||||
#import "MPDocument.h"
|
||||
|
||||
#import "NSMutableData+Base64.h"
|
||||
#import "NSData+Gzip.h"
|
||||
|
||||
#import "Kdb3Node.h"
|
||||
#import "Kdb3Entry+KVOAdditions.h"
|
||||
#import "Kdb4Node.h"
|
||||
#import "Kdb4Entry+KVOAdditions.h"
|
||||
#import "KPKEntry.h"
|
||||
#import "KPKBinary.h"
|
||||
|
||||
@implementation MPDocument (Attachments)
|
||||
|
||||
- (void)addAttachment:(NSURL *)location toEntry:(KdbEntry *)anEntry {
|
||||
- (void)addAttachment:(NSURL *)location toEntry:(KPKEntry *)anEntry {
|
||||
NSError *error = nil;
|
||||
NSDictionary *resourceKeys = [location resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
|
||||
if([resourceKeys[ NSURLIsDirectoryKey ] boolValue] == YES ) {
|
||||
return; // We do not add whol directories
|
||||
return; // We do not add whole directories
|
||||
}
|
||||
NSString *fileName = [location lastPathComponent];
|
||||
if([anEntry isKindOfClass:[Kdb3Entry class]]) {
|
||||
Kdb3Entry *entry = (Kdb3Entry *)anEntry;
|
||||
NSData *binaryData = [NSData dataWithContentsOfURL:location options:NSDataReadingUncached error:&error];
|
||||
if(!binaryData) {
|
||||
[NSApp presentError:error];
|
||||
binaryData = nil;
|
||||
error = nil;
|
||||
return; // failed
|
||||
}
|
||||
entry.binary = binaryData;
|
||||
entry.binaryDesc = fileName;
|
||||
[entry insertObject:@"" inBinariesAtIndex:1];
|
||||
KPKBinary *binary = [[KPKBinary alloc] initWithContentsOfURL:location];
|
||||
if(binary) {
|
||||
[anEntry addBinary:binary];
|
||||
}
|
||||
if( [anEntry isKindOfClass:[Kdb4Entry class]]) {
|
||||
Kdb4Entry *entry = (Kdb4Entry *)anEntry;
|
||||
NSData *fileData = [NSData dataWithContentsOfURL:location options:NSDataReadingMappedIfSafe error:&error];
|
||||
if(!fileData) {
|
||||
[NSApp presentError:error];
|
||||
fileData = nil;
|
||||
error = nil;
|
||||
return; // failed
|
||||
}
|
||||
Binary *binary = [[Binary alloc] init];
|
||||
NSUInteger nextId = [self nextBinaryId];
|
||||
if(nextId == NSNotFound) {
|
||||
binary = nil;
|
||||
return; // No id found. Something went wrong
|
||||
}
|
||||
binary.binaryId = nextId;
|
||||
binary.compressed = (self.treeV4.compressionAlgorithm != KPLCompressionNone);
|
||||
NSData *encodedData;
|
||||
if(binary.compressed) {
|
||||
switch(self.treeV4.compressionAlgorithm) {
|
||||
case KPLCompressionGzip: {
|
||||
NSData *compressedData = [fileData gzipDeflate];
|
||||
encodedData = [NSMutableData mutableDataWithBase64EncodedData:compressedData];
|
||||
break;
|
||||
}
|
||||
default:
|
||||
NSAssert(NO, @"Unsupported Compression Algorithm");
|
||||
binary = nil;
|
||||
encodedData = nil;
|
||||
fileData = nil;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
encodedData = fileData;
|
||||
}
|
||||
binary.data = [[NSString alloc] initWithData:encodedData encoding:NSUTF8StringEncoding];
|
||||
|
||||
[self.treeV4.binaries addObject:binary];
|
||||
BinaryRef *ref = [[BinaryRef alloc] init];
|
||||
ref.key = fileName;
|
||||
ref.ref = binary.binaryId;
|
||||
[entry insertObject:ref inBinariesAtIndex:[entry.binaries count]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)removeAttachment:(BinaryRef *)reference fromEntry:(KdbEntry *)anEntry {
|
||||
if(self.version != MPDatabaseVersion4) {
|
||||
return; // Wrong Database version;
|
||||
}
|
||||
Binary *binary = [self findBinary:reference];
|
||||
Kdb4Entry *entry = (Kdb4Entry *)anEntry;
|
||||
NSUInteger index = [entry.binaries indexOfObject:reference];
|
||||
if(index == NSNotFound) {
|
||||
return; // No Reference for this entry found
|
||||
}
|
||||
[entry removeObjectFromBinariesAtIndex:index];
|
||||
[self.treeV4.binaries removeObject:binary];
|
||||
}
|
||||
|
||||
- (void)removeAttachmentFromEntry:(KdbEntry *)anEntry {
|
||||
if(self.version != MPDatabaseVersion3) {
|
||||
return;
|
||||
}
|
||||
Kdb3Entry *entry = (Kdb3Entry *)anEntry;
|
||||
[entry removeObjectFromBinariesAtIndex:0];
|
||||
}
|
||||
|
||||
- (Binary *)findBinary:(BinaryRef *)reference {
|
||||
if(self.version != MPDatabaseVersion4) {
|
||||
return nil;
|
||||
}
|
||||
NSPredicate *filterPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
|
||||
Binary *binaryFile = evaluatedObject;
|
||||
return (binaryFile.binaryId == reference.ref);
|
||||
}];
|
||||
NSArray *filteredBinary = [self.treeV4.binaries filteredArrayUsingPredicate:filterPredicate];
|
||||
return [filteredBinary lastObject];
|
||||
}
|
||||
|
||||
- (void)saveAttachmentForItem:(id)item toLocation:(NSURL *)location {
|
||||
NSData *fileData = [self attachmentDataForItem:item];
|
||||
if(!fileData) {
|
||||
return; // No data to save;
|
||||
}
|
||||
NSError *error = nil;
|
||||
if( ![fileData writeToURL:location options:NSDataWritingAtomic error:&error] ) {
|
||||
[NSApp presentError:error];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSData *)attachmentDataForItem:(id)item {
|
||||
if([item isKindOfClass:[Kdb3Entry class]]) {
|
||||
Kdb3Entry *entry = (Kdb3Entry *)item;
|
||||
return entry.binary;
|
||||
}
|
||||
else if([item isKindOfClass:[BinaryRef class]]) {
|
||||
Binary *binary = [self findBinary:item];
|
||||
NSData *rawData = nil;
|
||||
if(binary) {
|
||||
if(binary.compressed) {
|
||||
rawData = [NSMutableData mutableDataWithBase64DecodedData:[binary.data dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
rawData = [rawData gzipInflate];
|
||||
}
|
||||
else {
|
||||
rawData = [NSMutableData mutableDataWithBase64DecodedData:[binary.data dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
}
|
||||
return rawData;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSString *)attachmenFileNameForItem:(id)item {
|
||||
if([item isKindOfClass:[Kdb3Entry class]]) {
|
||||
Kdb3Entry *entry = (Kdb3Entry *)item;
|
||||
return entry.binaryDesc;
|
||||
}
|
||||
else if([item isKindOfClass:[BinaryRef class]]) {
|
||||
return ((BinaryRef *)item).key;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSUInteger)nextBinaryId {
|
||||
if(self.version != MPDatabaseVersion4) {
|
||||
return NSNotFound;
|
||||
}
|
||||
NSUInteger maxKey = 0;
|
||||
for(Binary *binary in self.treeV4.binaries) {
|
||||
maxKey = MAX(binary.binaryId, maxKey);
|
||||
}
|
||||
return (maxKey + 1);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user