Permalink
Browse files

now works with binary data

  • Loading branch information...
1 parent 5cb742d commit d34d506f42029605f440c96764c098f1b4657182 Brainfucker committed Jun 24, 2010
Showing with 72 additions and 19 deletions.
  1. +21 −0 .goutputstream-VZ8HEV
  2. +35 −15 base64.cc
  3. BIN index.node
  4. +16 −4 test.js
  5. 0 wscript
View
@@ -0,0 +1,21 @@
+var base64 = require("./build/default/base64"),
+ jsbase64 = require("./js_base64_for_comparsion"),
+ sys = require("sys"),
+ Buffer = require('buffer').Buffer;
+
+//sys.puts(base64.encode('wow'));
+
+
+if (base64.encode('What do ya want for nothing?')=='V2hhdCBkbyB5YSB3YW50IGZvciBub3RoaW5nPw==')
+ sys.puts('test 1 PASSED');
+else
+ sys.puts('test 1 FAILS');
+
+if (base64.decode(base64.encode('What do ya want for nothing?'))=='What do ya want for nothing?')
+ sys.puts('test 2 PASSED');
+else
+ sys.puts('test 2 FAILS');
+
+var buf = new Buffer(256);
+
+var str = 'some test string,
View
@@ -8,12 +8,15 @@
*/
#include <v8.h>
+#include <node.h>
+#include <node_buffer.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace v8;
+using namespace node;
static const char base64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
@@ -45,11 +48,11 @@ static const short base64_reverse_table[256] = {
};
-unsigned char* base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
+char* base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
{
const unsigned char *current = str;
- unsigned char *p;
- unsigned char *result;
+ char *p;
+ char *result;
if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
if (ret_length != NULL) {
@@ -58,7 +61,7 @@ unsigned char* base64_encode(const unsigned char *str, int length, int *ret_leng
return NULL;
}
- result = (unsigned char *)malloc((((length + 2) / 3) * 4)*(sizeof(char))+(1));
+ result = (char *)malloc((((length + 2) / 3) * 4)*(sizeof(char))+(1));
if (result == NULL) {
fprintf(stderr, "out of memory!\n");
exit(1);
@@ -95,13 +98,13 @@ unsigned char* base64_encode(const unsigned char *str, int length, int *ret_leng
}
-unsigned char *base64_decode(const unsigned char *str, int length, int *ret_length)
+char *base64_decode(const unsigned char *str, int length, int *ret_length)
{
const unsigned char *current = str;
int ch, i = 0, j = 0, k;
- unsigned char *result;
+ char *result;
- result = (unsigned char *)malloc(length+1);
+ result = (char *)malloc(length+1);
if (result == NULL) {
fprintf(stderr, "out of memory!\n");
exit(1);
@@ -168,11 +171,19 @@ Handle<Value>
base64_encode_binding(const Arguments& args)
{
HandleScope scope;
- String::Utf8Value data(args[0]->ToString());
int len;
- unsigned char* str=base64_encode((unsigned char*)*data,data.length(),&len);
- Local<String> ret = String::New((const char*)str,len);
- free(str);
+ Local<String> ret;
+ if (Buffer::HasInstance(args[0])) {
+ Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+ char *str = base64_encode((unsigned char*)buffer->data(), buffer->length(),&len);
+ ret = String::New(str, len);
+ delete str;
+ } else {
+ String::Utf8Value data(args[0]->ToString());
+ char* str = base64_encode((unsigned char*)*data,data.length(),&len);
+ ret = String::New(str,len);
+ delete str;
+ }
return ret;
}
@@ -181,11 +192,20 @@ Handle<Value>
base64_decode_binding(const Arguments& args)
{
HandleScope scope;
- String::Utf8Value data(args[0]->ToString());
+ Local<String> ret;
int len;
- unsigned char* str=base64_decode((unsigned char*)*data,data.length(),&len);
-
- return String::New((const char*)str,len);
+ if (Buffer::HasInstance(args[0])) {
+ Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+ char *str = base64_decode((unsigned char*)buffer->data(), buffer->length(),&len);
+ ret = String::New(str, len);
+ delete str;
+ } else {
+ String::Utf8Value data(args[0]->ToString());
+ char* str=base64_decode((unsigned char*)*data,data.length(),&len);
+ ret = String::New(str,len);
+ delete str;
+ }
+ return ret;
}
View
Binary file not shown.
View
20 test.js
@@ -1,9 +1,11 @@
-var base64 = require("./build/default/base64");
-var jsbase64 = require("./js_base64_for_comparsion");
-var sys = require("sys");
+var base64 = require("./build/default/base64"),
+ jsbase64 = require("./js_base64_for_comparsion"),
+ Buffer = require('buffer').Buffer,
+ sys = require("sys");
//sys.puts(base64.encode('wow'));
-
+var textBuff = new Buffer('What do ya want for nothing?', 'utf8');
+var baseBuff = new Buffer('V2hhdCBkbyB5YSB3YW50IGZvciBub3RoaW5nPw==', 'utf8');
if (base64.encode('What do ya want for nothing?')=='V2hhdCBkbyB5YSB3YW50IGZvciBub3RoaW5nPw==')
sys.puts('test 1 PASSED');
@@ -15,6 +17,16 @@ if (base64.decode(base64.encode('What do ya want for nothing?'))=='What do ya wa
else
sys.puts('test 2 FAILS');
+if (base64.encode(textBuff)=='V2hhdCBkbyB5YSB3YW50IGZvciBub3RoaW5nPw==')
+ sys.puts('test 3 PASSED');
+else
+ sys.puts('test 3 FAILS');
+
+if (base64.decode(baseBuff)=='What do ya want for nothing?')
+ sys.puts('test 4 PASSED');
+else
+ sys.puts('test 4 FAILS');
+
// C++ base64
var m1=new Date().getTime();
View
0 wscript 100755 → 100644
No changes.

0 comments on commit d34d506

Please sign in to comment.