Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions base64.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <new>
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
Expand All @@ -27,7 +28,11 @@ base64_encode(const unsigned char *input, int length)
unsigned char char_array_3[3], char_array_4[4];

int b64len = (length+2 - ((length+2)%3))*4/3;
char *b64str = new char[b64len + 1];
char *b64str = new (std::nothrow) char[b64len + 1];

if (b64str == NULL) {
return NULL;
}

while (length--) {
char_array_3[i++] = *(input++);
Expand Down Expand Up @@ -75,7 +80,11 @@ base64_decode(const char *input, int length, int *outlen)
int r = 0;
int idx = 0;
unsigned char char_array_4[4], char_array_3[3];
unsigned char *output = new unsigned char[length*3/4];
unsigned char *output = new (std::nothrow) unsigned char[length*3/4];

if (output == NULL) {
return NULL;
}

while (length-- && input[idx] != '=') {
//skip invalid or padding based chars
Expand Down Expand Up @@ -141,6 +150,11 @@ base64_encode_binding(const Arguments &args)
else
return VException("Argument should be a buffer or a string");


if (str == NULL) {
return VException("Unable to allocate enough memory");
}

Local<String> ret = String::New(str);
delete [] str;
return scope.Close(ret);
Expand Down Expand Up @@ -171,6 +185,10 @@ base64_decode_binding(const Arguments &args)
decoded = base64_decode(*b64data, b64data.length(), &outlen);
}

if (decoded == NULL) {
return VException("Unable to allocate enough memory");
}

if (args.Length() > 1 && args[1]->IsString()) {
String::AsciiValue flag(args[1]->ToString());
if (strcmp("utf8", *flag) == 0) encflag = UTF8;
Expand Down