Browse files

Allow base64.encode() to accept strings as well as buffers.

  • Loading branch information...
1 parent f25e86f commit 1fd832bdf3ef1b1f35944b8c71fc9ddaa1bfb57d @brianloveswords brianloveswords committed Mar 16, 2011
Showing with 19 additions and 9 deletions.
  1. +19 −9 base64.cc
View
28 base64.cc
@@ -121,18 +121,28 @@ Handle<Value>
base64_encode_binding(const Arguments &args)
{
HandleScope scope;
-
+ char * str;
+
if (args.Length() != 1)
- return VException("One argument required - buffer with data to encode");
+ return VException("One argument required - buffer or string with data to encode");
- if (!Buffer::HasInstance(args[0]))
- return VException("Argument should be a buffer");
+ if (Buffer::HasInstance(args[0])) {
+ v8::Handle<v8::Object> buffer = args[0]->ToObject();
+ str = base64_encode(
+ (unsigned char *) Buffer::Data(buffer),
+ Buffer::Length(buffer)
+ );
+ }
+ else if (args[0]->IsString()) {
+ Local<String> v8str = args[0]->ToString();
+ char * buffer = (char *) malloc(v8str->Utf8Length());
+ v8str->WriteUtf8(buffer);
+ str = base64_encode((unsigned char *)buffer, strlen(buffer));
+ free(buffer);
+ }
+ else
+ return VException("Argument should be a buffer or a string");
- v8::Handle<v8::Object> buffer = args[0]->ToObject();
- char *str = base64_encode(
- (unsigned char *) Buffer::Data(buffer),
- Buffer::Length(buffer)
- );
Local<String> ret = String::New(str);
delete [] str;
return scope.Close(ret);

0 comments on commit 1fd832b

Please sign in to comment.