Skip to content
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

Build leaking strings with push/join instead of string concatenation. #48

Merged
merged 1 commit into from Mar 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 49 additions & 46 deletions libs/base64-string.js
Expand Up @@ -12,7 +12,7 @@
var Base64String = {

compressToUTF16 : function (input) {
var output = "",
var output = [],
i,c,
current,
status = 0;
Expand All @@ -23,74 +23,74 @@ var Base64String = {
c = input.charCodeAt(i);
switch (status++) {
case 0:
output += String.fromCharCode((c >> 1)+32);
output.push(String.fromCharCode((c >> 1)+32));
current = (c & 1) << 14;
break;
case 1:
output += String.fromCharCode((current + (c >> 2))+32);
output.push(String.fromCharCode((current + (c >> 2))+32));
current = (c & 3) << 13;
break;
case 2:
output += String.fromCharCode((current + (c >> 3))+32);
output.push(String.fromCharCode((current + (c >> 3))+32));
current = (c & 7) << 12;
break;
case 3:
output += String.fromCharCode((current + (c >> 4))+32);
output.push(String.fromCharCode((current + (c >> 4))+32));
current = (c & 15) << 11;
break;
case 4:
output += String.fromCharCode((current + (c >> 5))+32);
output.push(String.fromCharCode((current + (c >> 5))+32));
current = (c & 31) << 10;
break;
case 5:
output += String.fromCharCode((current + (c >> 6))+32);
output.push(String.fromCharCode((current + (c >> 6))+32));
current = (c & 63) << 9;
break;
case 6:
output += String.fromCharCode((current + (c >> 7))+32);
output.push(String.fromCharCode((current + (c >> 7))+32));
current = (c & 127) << 8;
break;
case 7:
output += String.fromCharCode((current + (c >> 8))+32);
output.push(String.fromCharCode((current + (c >> 8))+32));
current = (c & 255) << 7;
break;
case 8:
output += String.fromCharCode((current + (c >> 9))+32);
output.push(String.fromCharCode((current + (c >> 9))+32));
current = (c & 511) << 6;
break;
case 9:
output += String.fromCharCode((current + (c >> 10))+32);
output.push(String.fromCharCode((current + (c >> 10))+32));
current = (c & 1023) << 5;
break;
case 10:
output += String.fromCharCode((current + (c >> 11))+32);
output.push(String.fromCharCode((current + (c >> 11))+32));
current = (c & 2047) << 4;
break;
case 11:
output += String.fromCharCode((current + (c >> 12))+32);
output.push(String.fromCharCode((current + (c >> 12))+32));
current = (c & 4095) << 3;
break;
case 12:
output += String.fromCharCode((current + (c >> 13))+32);
output.push(String.fromCharCode((current + (c >> 13))+32));
current = (c & 8191) << 2;
break;
case 13:
output += String.fromCharCode((current + (c >> 14))+32);
output.push(String.fromCharCode((current + (c >> 14))+32));
current = (c & 16383) << 1;
break;
case 14:
output += String.fromCharCode((current + (c >> 15))+32, (c & 32767)+32);
output.push(String.fromCharCode((current + (c >> 15))+32, (c & 32767)+32));
status = 0;
break;
}
}

return output + String.fromCharCode(current + 32);
output.push(String.fromCharCode(current + 32));
return output.join('');
},


decompressFromUTF16 : function (input) {
var output = "",
var output = [],
current,c,
status=0,
i = 0;
Expand All @@ -103,63 +103,63 @@ var Base64String = {
current = c << 1;
break;
case 1:
output += String.fromCharCode(current | (c >> 14));
output.push(String.fromCharCode(current | (c >> 14)));
current = (c&16383) << 2;
break;
case 2:
output += String.fromCharCode(current | (c >> 13));
output.push(String.fromCharCode(current | (c >> 13)));
current = (c&8191) << 3;
break;
case 3:
output += String.fromCharCode(current | (c >> 12));
output.push(String.fromCharCode(current | (c >> 12)));
current = (c&4095) << 4;
break;
case 4:
output += String.fromCharCode(current | (c >> 11));
output.push(String.fromCharCode(current | (c >> 11)));
current = (c&2047) << 5;
break;
case 5:
output += String.fromCharCode(current | (c >> 10));
output.push(String.fromCharCode(current | (c >> 10)));
current = (c&1023) << 6;
break;
case 6:
output += String.fromCharCode(current | (c >> 9));
output.push(String.fromCharCode(current | (c >> 9)));
current = (c&511) << 7;
break;
case 7:
output += String.fromCharCode(current | (c >> 8));
output.push(String.fromCharCode(current | (c >> 8)));
current = (c&255) << 8;
break;
case 8:
output += String.fromCharCode(current | (c >> 7));
output.push(String.fromCharCode(current | (c >> 7)));
current = (c&127) << 9;
break;
case 9:
output += String.fromCharCode(current | (c >> 6));
output.push(String.fromCharCode(current | (c >> 6)));
current = (c&63) << 10;
break;
case 10:
output += String.fromCharCode(current | (c >> 5));
output.push(String.fromCharCode(current | (c >> 5)));
current = (c&31) << 11;
break;
case 11:
output += String.fromCharCode(current | (c >> 4));
output.push(String.fromCharCode(current | (c >> 4)));
current = (c&15) << 12;
break;
case 12:
output += String.fromCharCode(current | (c >> 3));
output.push(String.fromCharCode(current | (c >> 3)));
current = (c&7) << 13;
break;
case 13:
output += String.fromCharCode(current | (c >> 2));
output.push(String.fromCharCode(current | (c >> 2)));
current = (c&3) << 14;
break;
case 14:
output += String.fromCharCode(current | (c >> 1));
output.push(String.fromCharCode(current | (c >> 1)));
current = (c&1) << 15;
break;
case 15:
output += String.fromCharCode(current | c);
output.push(String.fromCharCode(current | c));
status=0;
break;
}
Expand All @@ -168,7 +168,7 @@ var Base64String = {
i++;
}

return this.decompress(output);
return this.decompress(output.join(''));
//return output;

},
Expand All @@ -178,7 +178,7 @@ var Base64String = {
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

decompress : function (input) {
var output = "";
var output = [];
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 1;
var odd = input.charCodeAt(0) >> 8;
Expand Down Expand Up @@ -213,17 +213,17 @@ var Base64String = {
enc4 = 64;
}

output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

output.push(this._keyStr.charAt(enc1));
output.push(this._keyStr.charAt(enc2));
output.push(this._keyStr.charAt(enc3));
output.push(this._keyStr.charAt(enc4));
}

return output;
return output.join('');
},

compress : function (input) {
var output = "",
var output = [],
ol = 1,
output_,
chr1, chr2, chr3,
Expand All @@ -248,32 +248,35 @@ var Base64String = {
flush = true;

if (enc3 != 64) {
output += String.fromCharCode(output_ | chr2);
output.push(String.fromCharCode(output_ | chr2));
flush = false;
}
if (enc4 != 64) {
output_ = chr3 << 8;
flush = true;
}
} else {
output = output + String.fromCharCode(output_ | chr1);
output.push(String.fromCharCode(output_ | chr1));
flush = false;

if (enc3 != 64) {
output_ = chr2 << 8;
flush = true;
}
if (enc4 != 64) {
output += String.fromCharCode(output_ | chr3);
output.push(String.fromCharCode(output_ | chr3));
flush = false;
}
}
ol+=3;
}

if (flush) {
output += String.fromCharCode(output_);
output.push(String.fromCharCode(output_));
output = output.join('');
output = String.fromCharCode(output.charCodeAt(0)|256) + output.substring(1);
} else {
output = output.join('');
}

return output;
Expand Down