Skip to content

Instantly share code, notes, and snippets.

@syberkitten
Last active August 29, 2015 14:02
Show Gist options
  • Save syberkitten/d22b3579357fce376686 to your computer and use it in GitHub Desktop.
Save syberkitten/d22b3579357fce376686 to your computer and use it in GitHub Desktop.
Unescape function - TSSCRIPT
function binary.pow(number,power)
{
local i,base;
if(power==0)
{
number=1;
}
else
{
base=number;
for(i=1;i<power;i++)
number=number*base;
}
return (number);
}
function dec2hex(dec){
c.hexDigit='0123456789abcdef`;
c.hex='`;
while(dec>0){
c.h1=dec % 16;
dec=dec/16;
c.hex=c.hexDigit.pos(c.h1)|c.hex;
}
return(c.hex);
}
function hex2dec(str) {
#str = str.O_LOWER();
# add 1808
# or sub 496
# or sub 1264
converter.hex2dec['0`]=0;
converter.hex2dec['1`]=1;
converter.hex2dec['2`]=2;
converter.hex2dec['3`]=3;
converter.hex2dec['4`]=4;
converter.hex2dec['5`]=5;
converter.hex2dec['6`]=6;
converter.hex2dec['7`]=7;
converter.hex2dec['8`]=8;
converter.hex2dec['9`]=9;
converter.hex2dec['A`]=10;
converter.hex2dec['B`]=11;
converter.hex2dec['C`]=12;
converter.hex2dec['D`]=13;
converter.hex2dec['E`]=14;
converter.hex2dec['F`]=15;
converter.hex2dec['a`]=10;
converter.hex2dec['b`]=11;
converter.hex2dec['c`]=12;
converter.hex2dec['d`]=13;
converter.hex2dec['e`]=14;
converter.hex2dec['f`]=15;
val = 0;
i.i2 = 0;
for (i.i=0;i.i<str.length;i.i++){
i.i2 = str.length - i.i-1;
byteVal = converter.hex2dec[str.pos(i.i)];
val=val+(byteVal*binary.pow(16,i.i2));
}
return(val);
}
function result2(str){
length1 = str.length;
return (length1);
}
function unescape(str) {
# ECMA-262 Rev 5.1
# http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
org_length = str.length;
R='`;
k=0;
J=1;
J_STOP=0;
DEBUG=0;
# 5
#debug.showTree('string:`|str|' length:`|result2(str));
if (k==result2(str)) {
debug.showTree('step (5) k=result2(str)`);
return R;
}
#debug.showTree('starting while`);
while(J){
# 6
c=str.substring(k,k+1);
if (DEBUG) debug.showTree('step (6) c:`|c);
#7
if (c!='%`) {
# 18
R=R|c;
# 19
k=k+1;
# 5
if (k==result2(str)) {
J=J_STOP;
}
#8
} else {
if (DEBUG) debug.showTree('step (8) k:`|k);
if (k > result2(str)-6) { # 14
if (DEBUG) debug.showTree('step (14) k:`|k);
if (k > result2(str)-3) {
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#15
if (DEBUG) debug.showTree('step (15) k:`|k);
k1 = hex2dec(str.substring(k+1,k+2));
k2 = hex2dec(str.substring(k+2,k+3));
if (DEBUG) debug.showTree('step (15) k1:`|k1|' k2:`|k2);
if (k1 == 0 && k2 == 0) {
#18
if (DEBUG) debug.showTree('step (18) k:`|k);
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#16
if (DEBUG) debug.showTree('step (16) k:`|k);
c = char.from_asc(hex2dec('00`|str.substring(k+1,k+2)|str.substring(k+2,k+3)));
if (DEBUG) debug.showTree('step (16) char value:`|c|' char:`|c);
#17
k=k+2;
#18
R=R|c;
#19
k=k+1;
#5
if (DEBUG) debug.showTree('step (16 to 5) k:`|k);
if (k==result2(str)) {
J=J_STOP;
}
}
}
} else {
# 9
if (DEBUG) debug.showTree('step (9) k:`|k);
if (str.substring(k+1,k+2)!='u`) {
#14
if (DEBUG) debug.showTree('step (14) k:`|k|' result2(str)-3 :`|result2(str)-3);
if (k > result2(str)-3) {
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#15
k1 = hex2dec(str.substring(k+1,k+2));
k2 = hex2dec(str.substring(k+2,k+3));
if (k1 == 0 && k2 == 0) {
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#16
c = char.from_asc(hex2dec('00`|str.substring(k+1,k+2)|str.substring(k+2,k+3)));
if (DEBUG) debug.showTree('step (16) c:`|c|' R:`|R);
#17
k=k+2;
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
}
}
# 10
} else {
k2 = hex2dec(str.substring(k+2,k+3));
k3 = hex2dec(str.substring(k+3,k+4));
k4 = hex2dec(str.substring(k+4,k+5));
k5 = hex2dec(str.substring(k+5,k+6));
if (k2==0 & k3==0 && k4==0 && k5==0) {
#14
if (k > result2(str)-3) {
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#15
k1 = hex2dec(str.substring(k+1,k+2));
k2 = hex2dec(str.substring(k+2,k+3));
if (k1 == 0 && k2 == 0) {
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
} else {
#16
c = char.from_asc(hex2dec('00`|str.substring(k+1,k+2)|str.substring(k+2,k+3)));
if (DEBUG) debug.showTree('c:`|c);
#17
k=k+2;
#18
R=R|c;
#19
k=k+1;
#5
if (k==result2(str)) {
J=J_STOP;
}
}
}
}
}
}
}
}
return (R);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment