Skip to content

Add option to start decoding after start index #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module 'base64-arraybuffer' {
export function encode(buffer: ArrayBuffer): string;
export function decode(str: string): ArrayBuffer;
export function decode(str: string, start?: number): ArrayBuffer;
}
12 changes: 8 additions & 4 deletions lib/base64-arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
return base64;
};

exports.decode = function(base64) {
var bufferLength = base64.length * 0.75,
len = base64.length, i, p = 0,
exports.decode = function(base64, start) {
var len = base64.length
var i = 0;
if(start < len && start > 0) i = start;
else if(start > len) i = len;

var bufferLength = (len - i) * 0.75, p = 0,
encoded1, encoded2, encoded3, encoded4;

if (base64[base64.length - 1] === "=") {
Expand All @@ -51,7 +55,7 @@
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);

for (i = 0; i < len; i+=4) {
for (i; i < len; i+=4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i+1)];
encoded3 = lookup[base64.charCodeAt(i+2)];
Expand Down
2 changes: 2 additions & 0 deletions test/base64-arraybuffer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ describe('decode', () => {
it('decode "Man"', () => ok(testArrayBuffers(decode("TWFu"), stringArrayBuffer("Man"))));
it('decode "Hello world"', () => ok(testArrayBuffers(decode("SGVsbG8gd29ybGQ="), stringArrayBuffer("Hello world"))));
it('decode "Hello worlds!"', () => ok(testArrayBuffers(decode("SGVsbG8gd29ybGRzIQ=="), stringArrayBuffer("Hello worlds!"))));
it('decode "Hello world" - discard start < 0', () => ok(testArrayBuffers(decode("SGVsbG8gd29ybGQ=", -1), stringArrayBuffer("Hello world"))));
it('decode "Hello world" after header when start < length', () => ok(testArrayBuffers(decode("data:image/bmp;base64,SGVsbG8gd29ybGQ=", 22), stringArrayBuffer("Hello world"))));
it('decode all binary characters', () => ok(testArrayBuffers(decode("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="), rangeArrayBuffer())));
});