| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,6 +77,9 @@ Buffer.prototype.write = function (string) { | |||
| 77 | 77 | case 'binary': | |
| 78 | 78 | return this.binaryWrite(string, offset); | |
| 79 | 79 | ||
| 80 | + case 'base64': | ||
| 81 | + return this.base64Write(string, offset); | ||
| 82 | + | ||
| 80 | 83 | default: | |
| 81 | 84 | throw new Error('Unknown encoding'); | |
| 82 | 85 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -275,6 +275,16 @@ Handle<Value> Buffer::Utf8Slice(const Arguments &args) { | |||
| 275 | 275 | static char* base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
| 276 | 276 | "abcdefghijklmnopqrstuvwxyz" | |
| 277 | 277 | "0123456789+/"; | |
| 278 | + static int unbase64_table[] = | ||
| 279 | + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
| 280 | + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 | ||
| 281 | + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63 | ||
| 282 | + ,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1 | ||
| 283 | + ,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14 | ||
| 284 | + ,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1 | ||
| 285 | + ,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 | ||
| 286 | + ,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1 | ||
| 287 | + }; | ||
| 278 | 288 | ||
| 279 | 289 | ||
| 280 | 290 | Handle<Value> Buffer::Base64Slice(const Arguments &args) { | |
@@ -485,6 +495,91 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) { | |||
| 485 | 495 | } | |
| 486 | 496 | ||
| 487 | 497 | ||
| 498 | + // var bytesWritten = buffer.base64Write(string, offset); | ||
| 499 | + Handle<Value> Buffer::Base64Write(const Arguments &args) { | ||
| 500 | + HandleScope scope; | ||
| 501 | + | ||
| 502 | + assert(unbase64_table['/'] == 63); | ||
| 503 | + assert(unbase64_table['+'] == 62); | ||
| 504 | + assert(unbase64_table['T'] == 19); | ||
| 505 | + assert(unbase64_table['Z'] == 25); | ||
| 506 | + assert(unbase64_table['t'] == 45); | ||
| 507 | + assert(unbase64_table['z'] == 51); | ||
| 508 | + | ||
| 509 | + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This()); | ||
| 510 | + | ||
| 511 | + if (!args[0]->IsString()) { | ||
| 512 | + return ThrowException(Exception::TypeError(String::New( | ||
| 513 | + "Argument must be a string"))); | ||
| 514 | + } | ||
| 515 | + | ||
| 516 | + String::AsciiValue s(args[0]->ToString()); | ||
| 517 | + size_t offset = args[1]->Int32Value(); | ||
| 518 | + | ||
| 519 | + if (offset >= buffer->length_) { | ||
| 520 | + return ThrowException(Exception::TypeError(String::New( | ||
| 521 | + "Offset is out of bounds"))); | ||
| 522 | + } | ||
| 523 | + | ||
| 524 | + char *input = *s; | ||
| 525 | + int input_len = s.length(); | ||
| 526 | + | ||
| 527 | + char *start = buffer->data() + offset; | ||
| 528 | + char *p = start; | ||
| 529 | + const char *pe = buffer->data() + buffer->length_; | ||
| 530 | + | ||
| 531 | + int i = 0; | ||
| 532 | + char a,b,c,d; | ||
| 533 | + | ||
| 534 | + bool b_oob, c_oob; | ||
| 535 | + | ||
| 536 | + while (i < input_len && p < pe) { | ||
| 537 | + if (input[i] == '=' || i >= input_len) break; | ||
| 538 | + a = unbase64_table[input[i]]; | ||
| 539 | + i++; | ||
| 540 | + | ||
| 541 | + if (input[i] == '=' || i >= input_len) { | ||
| 542 | + b = 0; | ||
| 543 | + b_oob = true; | ||
| 544 | + } else { | ||
| 545 | + b = unbase64_table[input[i]]; | ||
| 546 | + b_oob = false; | ||
| 547 | + } | ||
| 548 | + i++; | ||
| 549 | + | ||
| 550 | + if (b_oob || input[i] == '=' || i >= input_len) { | ||
| 551 | + c = 0; | ||
| 552 | + c_oob = true; | ||
| 553 | + } else { | ||
| 554 | + c = unbase64_table[input[i]]; | ||
| 555 | + c_oob = false; | ||
| 556 | + } | ||
| 557 | + i++; | ||
| 558 | + | ||
| 559 | + if (c_oob || input[i] == '=' || i >= input_len) { | ||
| 560 | + d = 0; | ||
| 561 | + } else { | ||
| 562 | + d = unbase64_table[input[i]]; | ||
| 563 | + } | ||
| 564 | + i++; | ||
| 565 | + | ||
| 566 | + | ||
| 567 | + *p = (a << 2) | ((b & 0x30) >> 4); | ||
| 568 | + if (++p >= pe) break; | ||
| 569 | + | ||
| 570 | + if (b_oob) break; | ||
| 571 | + *p = ((b & 0x0F) << 4) | ((c & 0x3c) >> 2); | ||
| 572 | + if (++p >= pe) break; | ||
| 573 | + | ||
| 574 | + if (c_oob) break; | ||
| 575 | + *p = ((c & 0x03) << 6) | (d & 0x3f); | ||
| 576 | + if (++p >= pe) break; | ||
| 577 | + } | ||
| 578 | + | ||
| 579 | + return scope.Close(Integer::New(p - start)); | ||
| 580 | + } | ||
| 581 | + | ||
| 582 | + | ||
| 488 | 583 | Handle<Value> Buffer::BinaryWrite(const Arguments &args) { | |
| 489 | 584 | HandleScope scope; | |
| 490 | 585 | ||
@@ -619,6 +714,7 @@ void Buffer::Initialize(Handle<Object> target) { | |||
| 619 | 714 | NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Buffer::Utf8Write); | |
| 620 | 715 | NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite); | |
| 621 | 716 | NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite); | |
| 717 | + NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Write", Buffer::Base64Write); | ||
| 622 | 718 | NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack); | |
| 623 | 719 | NODE_SET_PROTOTYPE_METHOD(constructor_template, "copy", Buffer::Copy); | |
| 624 | 720 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,7 @@ class Buffer : public ObjectWrap { | |||
| 57 | 57 | static v8::Handle<v8::Value> Base64Slice(const v8::Arguments &args); | |
| 58 | 58 | static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args); | |
| 59 | 59 | static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args); | |
| 60 | + static v8::Handle<v8::Value> Base64Write(const v8::Arguments &args); | ||
| 60 | 61 | static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args); | |
| 61 | 62 | static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args); | |
| 62 | 63 | static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -241,5 +241,10 @@ expected = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBi | |||
| 241 | 241 | assert.equal(expected, (new Buffer(quote)).toString('base64')); | |
| 242 | 242 | ||
| 243 | 243 | ||
| 244 | + b = new Buffer(1024); | ||
| 245 | + bytesWritten = b.write(expected, 0, 'base64'); | ||
| 246 | + assert.equal(quote, b.toString('ascii', 0, quote.length)); | ||
| 247 | + assert.equal(quote.length+1, bytesWritten); // writes a \0 too | ||
| 248 | + | ||
| 244 | 249 | ||
| 245 | 250 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments