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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
tohex(c: int): int
{
if(c > 9)
return c-10+'A';
return c+'0';
}
fromhex(ex: ref Exec, c1: int, c2: int): int
{
c1 = hexdigit(c1);
c2 = hexdigit(c2);
if(c1 < 0 || c2 < 0)
runtime(ex, URIError, "bad hex digit");
return 16*c1+c2;
}
isres(c: int): int
{
return c == ';' || c == '/' || c == '?' || c == ':' || c == '@' || c == '&' || c == '=' || c == '+' || c == '$' || c == ',' || c == '#'; # add '#' here for convenience
}
isunesc(c: int): int
{
return isalpha(c) || isdigit(c) || c == '-' || c == '_' || c == '.' || c == '!' || c == '~' || c == '*' || c == ''' || c == '(' || c == ')';
}
encode(ex: ref Exec, s: string, flag: int): string
{
m := len s;
r := "";
n := len r;
for(k := 0; k < m; k++){
c := s[k];
if(isunesc(c) || (flag && isres(c)))
r[n++] = c;
else{
if(c >= 16rdc00 && c <= 16rdfff)
runtime(ex, URIError, "char out of range");
if(c < 16rd800 || c > 16rdbff)
;
else{
if(++k == m)
runtime(ex, URIError, "char missing");
if(s[k] < 16rdc00 || s[k] > 16rdfff)
runtime(ex, URIError, "char out of range");
c = (c-16rd800)*16r400 + (s[k]-16rdc00) + 16r10000;
}
s1 := "Z";
s1[0] = c;
o := array of byte s1;
for(j := 0; j < len o; j++){
r += sys->sprint("%%%c%c", tohex(int o[j]/16), tohex(int o[j]%16));
n += 3;
}
}
}
return r;
}
decode(ex: ref Exec, s: string, flag: int): string
{
m := len s;
r := "";
n := len r;
for(k := 0; k < m; k++){
c := s[k];
if(c != '%')
r[n++] = c;
else{
start := k;
if(k+2 >= m)
runtime(ex, URIError, "char missing");
c = fromhex(ex, s[k+1], s[k+2]);
k += 2;
if((c&16r80 == 0)){
if(flag && isres(c)){
r += s[start: k+1];
n += k+1-start;
}
else
r[n++] = c;
}
else{
for(i := 1; ((c<<i)&16r80) == 0; i++)
;
if(i == 1 || i > 4)
runtime(ex, URIError, "bad hex number");
o := array[i] of byte;
o[0] = byte c;
if(k+3*(n-1) >= m)
runtime(ex, URIError, "char missing");
for(j := 1; j < i; j++){
if(s[++k] != '%')
runtime(ex, URIError, "% missing");
c = fromhex(ex, s[k+1], s[k+2]);
k += 2;
if((c&16rc0) != 2)
runtime(ex, URIError, "bad hex number");
o[j] = byte c;
}
(c, nil, nil) = sys->byte2char(o, 0);
if(c < 16r10000){
if(flag && isres(c)){
r += s[start: k+1];
n += k+1-start;
}
else
r[n++] = c;
}
else if(c > 16r10ffff)
runtime(ex, URIError, "bad byte sequence");
else{
r[n++] = ((c-16r10000)&16r3ff)+16rdc00;
r[n++] = (((c-16r10000)>>10)&16r3ff)+16rd800;
}
}
}
}
return r;
}
cdecodeuri(ex: ref Exec, nil, nil: ref Ecmascript->Obj, args: array of ref Val): ref Val
{
return strval(decode(ex, toString(ex, biarg(args, 0)), 1));
}
cdecodeuric(ex: ref Exec, nil, nil: ref Ecmascript->Obj, args: array of ref Val): ref Val
{
return strval(decode(ex, toString(ex, biarg(args, 0)), 0));
}
cencodeuri(ex: ref Exec, nil, nil: ref Ecmascript->Obj, args: array of ref Val): ref Val
{
return strval(encode(ex, toString(ex, biarg(args, 0)), 1));
}
cencodeuric(ex: ref Exec, nil, nil: ref Ecmascript->Obj, args: array of ref Val): ref Val
{
return strval(encode(ex, toString(ex, biarg(args, 0)), 0));
}
|