From 51951a2b55318ebd9a9d8f7a9e4acf6a252b7d37 Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Mon, 26 Aug 2013 10:03:01 -0500 Subject: [PATCH] Padd base64 strings with = up-to modulo 4 length. fixes #27 --- index.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 284ed1d..9ca7cf7 100644 --- a/index.js +++ b/index.js @@ -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)) { @@ -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? @@ -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) { -- 2.34.1