forked from locutusjs/locutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_uudecode.js
More file actions
57 lines (50 loc) · 1.46 KB
/
Copy pathconvert_uudecode.js
File metadata and controls
57 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function convert_uudecode (str) {
// http://kevin.vanzonneveld.net
// + original by: Ole Vrijenhoek
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// - depends on: is_scalar
// - depends on: rtrim
// * example 1: convert_uudecode('+22!L;W9E(%!(4\"$`\n`');
// * returns 1: 'I love PHP'
// Not working perfectly
// shortcut
var chr = function (c) {
return String.fromCharCode(c);
};
if (!str || str==="") {
return chr(0);
} else if (!this.is_scalar(str)) {
return false;
} else if (str.length < 8) {
return false;
}
var decoded = "", tmp1 = "", tmp2 = "";
var c = 0, i = 0, j = 0, a = 0;
var line = str.split("\n");
var bytes = [];
for (i in line) {
c = line[i].charCodeAt(0);
bytes = line[i].substr(1);
// Convert each char in bytes[] to a 6-bit
for (j in bytes) {
tmp1 = bytes[j].charCodeAt(0)-32;
tmp1 = tmp1.toString(2);
while (tmp1.length < 6) {
tmp1 = "0" + tmp1;
}
tmp2 += tmp1
}
for (i=0; i<=(tmp2.length/8)-1; i++) {
tmp1 = tmp2.substr(a, 8);
if (tmp1 == "01100000") {
decoded += chr(0);
} else {
decoded += chr(parseInt(tmp1, 2));
}
a += 8;
}
a = 0;
tmp2 = "";
}
return this.rtrim(decoded, "\0");
}