From 93b897a8ca7797390ec6b7dbd2d49b3f0adaaf51 Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 15:49:52 -0500 Subject: [PATCH 1/3] add failing test for decoding url-safe style base64 --- test/url-safe.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/url-safe.js diff --git a/test/url-safe.js b/test/url-safe.js new file mode 100644 index 0000000..42bc04e --- /dev/null +++ b/test/url-safe.js @@ -0,0 +1,11 @@ +var test = require('tape'), + b64 = require('../lib/b64'); + +test('decode url-safe style base64 strings', function (t) { + var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; + + t.deepEqual(b64.toByteArray('//++/++/++//'), expected); + t.deepEqual(b64.toByteArray('__--_--_--__'), expected); + + t.end(); +}); From e747cd5965dff6b929b9d95ff66931288cde7a7d Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 15:52:53 -0500 Subject: [PATCH 2/3] decode url-safe style base64 --- lib/b64.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/b64.js b/lib/b64.js index 70f12c9..46001d2 100644 --- a/lib/b64.js +++ b/lib/b64.js @@ -12,12 +12,16 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var NUMBER = '0'.charCodeAt(0) var LOWER = 'a'.charCodeAt(0) var UPPER = 'A'.charCodeAt(0) + var PLUS_URL_SAFE = '-'.charCodeAt(0) + var SLASH_URL_SAFE = '_'.charCodeAt(0) function decode (elt) { var code = elt.charCodeAt(0) - if (code === PLUS) + if (code === PLUS || + code === PLUS_URL_SAFE) return 62 // '+' - if (code === SLASH) + if (code === SLASH || + code === SLASH_URL_SAFE) return 63 // '/' if (code < NUMBER) return -1 //no match From faebcaea416467f0b420d81a18df66985b0dd76c Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 20:31:53 -0500 Subject: [PATCH 3/3] make tests pass in node < 0.11 --- test/url-safe.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/url-safe.js b/test/url-safe.js index 42bc04e..dc437e9 100644 --- a/test/url-safe.js +++ b/test/url-safe.js @@ -3,9 +3,16 @@ var test = require('tape'), test('decode url-safe style base64 strings', function (t) { var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff]; - - t.deepEqual(b64.toByteArray('//++/++/++//'), expected); - t.deepEqual(b64.toByteArray('__--_--_--__'), expected); + + var actual = b64.toByteArray('//++/++/++//'); + for (var i = 0; i < actual.length; i++) { + t.equal(actual[i], expected[i]) + } + + actual = b64.toByteArray('__--_--_--__'); + for (var i = 0; i < actual.length; i++) { + t.equal(actual[i], expected[i]) + } t.end(); });