Skip to content

Commit

Permalink
COMMON: Use ScopedArray in our Blowfish implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent 775ce23 commit 00fd3b5
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/common/blowfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/scopedptr.h"
#include "src/common/memreadstream.h"
#include "src/common/blowfish.h"

Expand Down Expand Up @@ -343,31 +344,26 @@ MemoryReadStream *blowfishEBC(SeekableReadStream &input, const std::vector<byte>

// Round up to the next multiple of the block size
const size_t outputSize = ((inputSize + kBlockSize - 1) / kBlockSize) * kBlockSize;
byte *output = new byte[outputSize];

try {
byte buffer[kBlockSize];
byte *data = output;
while (inputSize > 0) {
const size_t toRead = MIN<size_t>(inputSize, kBlockSize);
ScopedArray<byte> output(new byte[outputSize]);

if (input.read(buffer, toRead) != toRead)
throw Exception(kReadError);
byte buffer[kBlockSize];
byte *data = output.get();
while (inputSize > 0) {
const size_t toRead = MIN<size_t>(inputSize, kBlockSize);

std::memset(buffer + toRead, 0, kBlockSize - toRead);
if (input.read(buffer, toRead) != toRead)
throw Exception(kReadError);

blowfishECB(ctx, mode, buffer, data);
std::memset(buffer + toRead, 0, kBlockSize - toRead);

data += toRead;
inputSize -= toRead;
}
blowfishECB(ctx, mode, buffer, data);

} catch (...) {
delete[] output;
throw;
data += toRead;
inputSize -= toRead;
}

return new MemoryReadStream(output, outputSize, true);
return new MemoryReadStream(output.release(), outputSize, true);
}

MemoryReadStream *encryptBlowfishEBC(SeekableReadStream &input, const std::vector<byte> &key) {
Expand Down

0 comments on commit 00fd3b5

Please sign in to comment.