Skip to content

Commit

Permalink
Fixed a bug calculating output data sizes from core encrypt/decrypt f…
Browse files Browse the repository at this point in the history
…unction.

Credit goes to Brendan Duddrudge for the information on this one.
  • Loading branch information
AlanQuatermain committed Apr 13, 2009
1 parent 6444c94 commit 3df3b8d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CommonCrypto/NSData+CommonCrypto.m
Expand Up @@ -343,13 +343,16 @@ - (NSData *) _runCryptor: (CCCryptorRef) cryptor result: (CCCryptorStatus *) sta
size_t bufsize = CCCryptorGetOutputLength( cryptor, (size_t)[self length], true );
void * buf = malloc( bufsize );
size_t bufused = 0;
size_t bytesTotal = 0;
*status = CCCryptorUpdate( cryptor, [self bytes], (size_t)[self length],
buf, bufsize, &bufused );
if ( *status != kCCSuccess )
{
free( buf );
return ( nil );
}

bytesTotal += bufused;

// From Brent Royal-Gordon (Twitter: architechies):
// Need to update buf ptr past used bytes when calling CCCryptorFinal()
Expand All @@ -359,8 +362,10 @@ - (NSData *) _runCryptor: (CCCryptorRef) cryptor result: (CCCryptorStatus *) sta
free( buf );
return ( nil );
}

bytesTotal += bufused;

return ( [NSData dataWithBytesNoCopy: buf length: bufused] );
return ( [NSData dataWithBytesNoCopy: buf length: bytesTotal] );
}

- (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
Expand Down

0 comments on commit 3df3b8d

Please sign in to comment.