Skip to content

Commit

Permalink
Ported utf8 chunking mechanism to base64 as well, fixes #800
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jun 9, 2017
1 parent e1f9d98 commit 66d149e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions lib/base64/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,47 @@ for (var i = 0; i < 64;)
* @returns {string} Base64 encoded string
*/
base64.encode = function encode(buffer, start, end) {
var string = []; // alt: new Array(Math.ceil((end - start) / 3) * 4);
var parts = null,
chunk = [];
var i = 0, // output index
j = 0, // goto index
t; // temporary
while (start < end) {
var b = buffer[start++];
switch (j) {
case 0:
string[i++] = b64[b >> 2];
chunk[i++] = b64[b >> 2];
t = (b & 3) << 4;
j = 1;
break;
case 1:
string[i++] = b64[t | b >> 4];
chunk[i++] = b64[t | b >> 4];
t = (b & 15) << 2;
j = 2;
break;
case 2:
string[i++] = b64[t | b >> 6];
string[i++] = b64[b & 63];
chunk[i++] = b64[t | b >> 6];
chunk[i++] = b64[b & 63];
j = 0;
break;
}
if (i > 8191) {
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
i = 0;
}
}
if (j) {
string[i++] = b64[t];
string[i ] = 61;
chunk[i++] = b64[t];
chunk[i++] = 61;
if (j === 1)
string[i + 1] = 61;
chunk[i++] = 61;
}
if (parts) {
if (i)
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
return parts.join("");
}
return String.fromCharCode.apply(String, string);
return String.fromCharCode.apply(String, chunk.slice(0, i));
};

var invalidEncoding = "invalid encoding";
Expand Down
2 changes: 1 addition & 1 deletion lib/base64/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@protobufjs/base64",
"description": "A minimal base64 implementation for number arrays.",
"version": "1.1.1",
"version": "1.1.2",
"author": "Daniel Wirtz <[email protected]>",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"long": "^3.2.0",
"@protobufjs/aspromise": "^1.1.2",
"@protobufjs/base64": "^1.1.1",
"@protobufjs/base64": "^1.1.2",
"@protobufjs/codegen": "^2.0.3",
"@protobufjs/eventemitter": "^1.1.0",
"@protobufjs/fetch": "^1.1.0",
Expand Down

0 comments on commit 66d149e

Please sign in to comment.