Skip to content

Commit 7a2ae53

Browse files
authored
Add files via upload
1 parent 8bdbed5 commit 7a2ae53

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

StringEncode.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ String.prototype.toAscii85 = function(){
1717
}
1818
let txt = [...this];
1919
let bin = "";
20-
for(let i = 0; i < txt.length; i++)
21-
bin += encodeToBinary(txt[i]);
20+
for(let i = 0; i < txt.length; i++) bin += encodeToBinary(txt[i]);
2221

2322
/* Pad the binary so it length is exactly divisible by 32
2423
* because we are going to divide it into chucks of 32's
@@ -56,16 +55,14 @@ String.prototype.toAscii85 = function(){
5655
sub = sub1 + sub2 + sub3 + sub4 + sub5;
5756

5857
//If chunk is just bunch of 0's just represent it with z
59-
if(sub == "!!!!!")
60-
sub = "z";
58+
if(sub == "!!!!!") sub = "z";
6159

6260
ASCII += sub;
6361
}
6462

6563
//Remove (padding/8) numbers of character from the final encoded characters
6664
for(let i = 0; (padding/8) != 0 && i < (padding/8); i++){
67-
if(ASCII == "z")
68-
ASCII = "!!!!!";
65+
if(ASCII == "z") ASCII = "!!!!!";
6966
ASCII = ASCII.substr(0, ASCII.length-1);
7067
}
7168
return "<~" + ASCII + "~>";
@@ -90,13 +87,11 @@ String.prototype.fromAscii85 = function(){
9087
*/
9188
let padding = 5 - (code.length%5);
9289
if(padding%5 == 0) padding = 0; //If length is exactly divisible by 5 padding returns 5, so we reset it to 0
93-
for(let i = 0; i < padding; i++)
94-
code += "u";
90+
code += "u".repeat(padding);
9591

9692
//Divide padded data to chunk of 5 characters
9793
let chunk = [];
98-
for(let i = 5; i <= code.length; i += 5)
99-
chunk.push(code.slice(i-5, i));
94+
for(let i = 5; i <= code.length; i += 5) chunk.push(code.slice(i-5, i));
10095

10196
/*
10297
* Take each character in a chunk
@@ -116,11 +111,8 @@ String.prototype.fromAscii85 = function(){
116111
let w5 = ((chunk[i][4]).charCodeAt(0)-33);
117112
let sub = w1+w2+w3+w4+w5;
118113
sub = sub.toString(2);
114+
sub = "0".repeat(32-sub.length)+sub;
119115

120-
for(let v = sub.length; v < 32; v++)//Pad to 8 digits
121-
{
122-
sub = "0" + sub;
123-
}
124116
w1 = String.fromCharCode(parseInt(sub.slice(0, 8), 2));
125117
w2 = String.fromCharCode(parseInt(sub.slice(8, 16), 2));
126118
w3 = String.fromCharCode(parseInt(sub.slice(16, 24), 2));
@@ -129,8 +121,8 @@ String.prototype.fromAscii85 = function(){
129121
}
130122

131123
//Remove the number of padding added above from the strings
132-
for(let i = 0; i < padding; i++)
133-
txt = txt.substr(0, txt.length-1);
124+
for(let i = 0; i < padding; i++) txt = txt.substr(0, txt.length-1);
125+
134126
return txt;
135127
}
136128

@@ -157,8 +149,7 @@ String.prototype.toBase64 = function(){
157149

158150
let txt = [...this];
159151
let bin = "";
160-
for(let i = 0; i < txt.length; i++)
161-
bin += encodeToBinary(txt[i]);
152+
for(let i = 0; i < txt.length; i++) bin += encodeToBinary(txt[i]);
162153

163154
/* Pad the data so it length is exactly divisible by 24
164155
* because we are going to divide it into chucks of 24's
@@ -171,8 +162,7 @@ String.prototype.toBase64 = function(){
171162

172163
//Divide padded data to chunk of 24 characters
173164
let chunk = [];
174-
for(let i = 24; i <= bin.length; i += 24)
175-
chunk.push(bin.slice(i-24, i));
165+
for(let i = 24; i <= bin.length; i += 24) chunk.push(bin.slice(i-24, i));
176166

177167
/*
178168
* Slice the 24 characters in each chunk into 6 characters in 4 places
@@ -206,24 +196,21 @@ String.prototype.fromBase64 = function(){
206196
* Convert it to binary and padd it to 6 chcracters when necessary
207197
*/
208198
let decode = "";
209-
for(let i = 0; i < txt.length; i++)
210-
{
199+
for(let i = 0; i < txt.length; i++){
211200
let tx = ((__codeString.indexOf(txt[i])>0)?__codeString.indexOf(txt[i]):0).toString(2);
212201
tx = "0".repeat(6-tx.length) + tx;
213202
decode += tx;
214203
}
215204

216205
//Break the whole binary into chunks of 8 characters
217206
let chunk = [];
218-
for(let i = 8; i <= decode.length; i += 8)
219-
chunk.push(decode.slice(i-8, i));
207+
for(let i = 8; i <= decode.length; i += 8) chunk.push(decode.slice(i-8, i));
220208

221209
//Take each chunk of 8 characters and convert it to base10 and to the corresponding character of ASCII
222210
let realText = "";
223211
for(let i = 0; i < chunk.length; i++){
224-
if(parseInt(chunk[i],2) == 0)
225-
continue;
212+
if(parseInt(chunk[i],2) == 0) continue;
226213
realText += String.fromCharCode(parseInt(chunk[i],2));
227214
}
228215
return realText;
229-
}
216+
}

0 commit comments

Comments
 (0)