Skip to content

Commit

Permalink
Padd base64 strings with = up-to modulo 4 length.
Browse files Browse the repository at this point in the history
fixes #27
  • Loading branch information
toots committed Aug 26, 2013
1 parent 4b50f1d commit 51951a2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ exports.SlowBuffer = Buffer;
Buffer.poolSize = 8192;
exports.INSPECT_MAX_BYTES = 50;

function stringtrim(str) {
if (str.trim) return str.trim();
return str.replace(/^\s+|\s+$/g, '');
}

function Buffer(subject, encoding, offset) {
if(!assert) assert= require('assert');
if (!(this instanceof Buffer)) {
Expand All @@ -12,6 +17,16 @@ function Buffer(subject, encoding, offset) {
this.parent = this;
this.offset = 0;

// Work-around: node's base64 implementation
// allows for non-padded strings while base64-js
// does not..
if (encoding == "base64" && typeof subject == "string") {
subject = stringtrim(subject);
while (subject.length % 4 != 0) {
subject = subject + "=";
}
}

var type;

// Are we slicing?
Expand Down Expand Up @@ -496,13 +511,8 @@ function asciiToBytes(str) {
return byteArray;
}

function stringtrim(str) {
if (str.trim) return str.trim();
return str.replace(/^\s+|\s+$/g, '');
}

function base64ToBytes(str) {
return require("base64-js").toByteArray(stringtrim(str));
return require("base64-js").toByteArray(str);
}

function blitBuffer(src, dst, offset, length) {
Expand Down

0 comments on commit 51951a2

Please sign in to comment.