Browse Source

fix memory with malloc

Marino Faggiana 7 years ago
parent
commit
70f6b2b8d7
1 changed files with 15 additions and 12 deletions
  1. 15 12
      iOSClient/Security/NCClientEncryption.m

+ 15 - 12
iOSClient/Security/NCClientEncryption.m

@@ -255,24 +255,25 @@ cleanup:
     NSData *initVectorData = [[NSData alloc] initWithBase64EncodedString:@"rTBECYNekKF+a1HR7z32/Q==" options:0];
     
     // Encrypt
-    NSData *dataEncrypt = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/iosencrypted.jpg", activeUrl]];
-    NSData *encryptedData = cipherOperation(dataEncrypt, keyData, initVectorData, kCCEncrypt);
-    if (encryptedData != nil)
-        [encryptedData writeToFile:[NSString stringWithFormat:@"%@/%@", activeUrl, @"crypted.dms"] atomically:YES];
+    //NSData *dataEncrypt = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/iosencrypted.jpg", activeUrl]];
+    //NSData *encryptedData = cipherOperation(dataEncrypt, keyData, initVectorData, kCCEncrypt);
+    //if (encryptedData != nil)
+    //    [encryptedData writeToFile:[NSString stringWithFormat:@"%@/%@", activeUrl, @"crypted.dms"] atomically:YES];
     
     // Decrypt
     //NSData *dataDecrypt = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/crypted.dms", activeUrl]];
-    //NSData *dataDecrypt = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/%@", activeUrl, metadata.fileID]];
-    //NSData *decryptedData = cipherOperation(dataDecrypt, keyData, initVectorData, kCCDecrypt);
+    NSData *dataDecrypt = [[NSFileManager defaultManager] contentsAtPath:[NSString stringWithFormat:@"%@/%@", activeUrl, metadata.fileID]];
+    NSData *decryptedData = cipherOperation(dataDecrypt, keyData, initVectorData, kCCDecrypt);
 
-    //if (decryptedData != nil)
-    //    [decryptedData writeToFile:[NSString stringWithFormat:@"%@/%@", activeUrl, @"decrypted.jpg"] atomically:YES];
+    if (decryptedData != nil)
+        [decryptedData writeToFile:[NSString stringWithFormat:@"%@/%@", activeUrl, @"decrypted.jpg"] atomically:YES];
+    
 }
 
 NSData *cipherOperation(NSData *contentData, NSData *keyData, NSData *initVectorData, CCOperation operation)
 {
-    NSMutableData *dataOut = [NSMutableData dataWithLength:contentData.length];
-    
+    void *operationBytes = malloc(contentData.length);
+        
     // setup key
     unsigned char cKey[kCCKeySizeAES128];
     bzero(cKey, sizeof(cKey));
@@ -296,14 +297,16 @@ NSData *cipherOperation(NSData *contentData, NSData *keyData, NSData *initVector
                                                0,
                                                contentData.bytes,
                                                contentData.length,
-                                               dataOut.mutableBytes,
+                                               operationBytes,
                                                tag.bytes,
                                                &tagLength);
     
     if (cryptStatus == kCCSuccess) {
-        return [NSData dataWithBytesNoCopy:(void *)dataOut.bytes length:dataOut.length];
+        return [NSData dataWithBytesNoCopy:operationBytes length:contentData.length];
     }
     
+    free(operationBytes);
+    
     return nil;
 }