Skip to content

Commit

Permalink
COMMON: Use ScopedArray in our Base64 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent cb2e531 commit 9ccb002
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions src/common/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cassert>

#include "src/common/base64.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/error.h"
#include "src/common/readstream.h"
Expand Down Expand Up @@ -205,41 +206,28 @@ void encodeBase64(ReadStream &data, std::list<UString> &base64, size_t lineLengt

SeekableReadStream *decodeBase64(const UString &base64) {
const size_t dataLength = (countLength(base64) / 4) * 3;
byte *data = new byte[dataLength];
Common::ScopedArray<byte> data(new byte[dataLength]);

MemoryWriteStream output(data, dataLength);
MemoryWriteStream output(data.get(), dataLength);

try {
UString overhang;

decodeBase64(output, base64, overhang);

} catch (...) {
delete[] data;
throw;
}
UString overhang;
decodeBase64(output, base64, overhang);

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

SeekableReadStream *decodeBase64(const std::list<UString> &base64) {
const size_t dataLength = (countLength(base64) / 4) * 3;
byte *data = new byte[dataLength];
Common::ScopedArray<byte> data(new byte[dataLength]);

MemoryWriteStream output(data, dataLength);
MemoryWriteStream output(data.get(), dataLength);

try {
UString overhang;

for (std::list<UString>::const_iterator b = base64.begin(); b != base64.end(); ++b)
decodeBase64(output, *b, overhang);
UString overhang;

} catch (...) {
delete[] data;
throw;
}
for (std::list<UString>::const_iterator b = base64.begin(); b != base64.end(); ++b)
decodeBase64(output, *b, overhang);

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

} // End of namespace Common

0 comments on commit 9ccb002

Please sign in to comment.