|
1 const charset = "GB2312"; |
|
2 const ScriptableUnicodeConverter = |
|
3 Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", |
|
4 "nsIScriptableUnicodeConverter"); |
|
5 var gConverter; |
|
6 |
|
7 function error(inString, outString, msg) { |
|
8 var dispIn = ""; |
|
9 var dispOut = ""; |
|
10 var i; |
|
11 for (i = 0; i < inString.length; ++i) { |
|
12 dispIn += " x" + inString.charCodeAt(i).toString(16); |
|
13 } |
|
14 if (outString.length == 0) { |
|
15 dispOut = "<empty>"; |
|
16 } else { |
|
17 for (i = 0; i < outString.length; ++i) { |
|
18 dispOut += " x" + outString.charCodeAt(i).toString(16); |
|
19 } |
|
20 } |
|
21 dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); |
|
22 do_throw("security risk: " + msg); |
|
23 } |
|
24 |
|
25 function IsASCII(charCode) { |
|
26 return (charCode <= 0x7e); |
|
27 } |
|
28 |
|
29 function test(inString) { |
|
30 var outString = gConverter.ConvertToUnicode(inString) + |
|
31 gConverter.Finish(); |
|
32 |
|
33 var outLen = outString.length; |
|
34 for (var pos = 1; pos < 3; ++pos) { |
|
35 outPos = outLen - (9 - pos); |
|
36 if (outPos < 0) { |
|
37 outPos = 0; |
|
38 } |
|
39 c0 = inString.charCodeAt(0); |
|
40 c1 = inString.charCodeAt(1); |
|
41 c2 = inString.charCodeAt(2); |
|
42 c3 = inString.charCodeAt(3); |
|
43 if (IsASCII(inString.charCodeAt(pos)) && |
|
44 !(outString.charCodeAt(outPos) == inString.charCodeAt(pos) || |
|
45 (outString.charCodeAt(outPos) != 0xFFFD) || |
|
46 // legal 4 byte range |
|
47 (0x81 <= c0 && c0 <= 0xfe && |
|
48 0x30 <= c1 && c1 <= 0x39 && |
|
49 0x81 <= c2 && c2 <= 0xfe && |
|
50 0x30 <= c3 && c3 <= 0x39))) { |
|
51 dump("pos = " + pos + "; outPos = " + outPos + "\n"); |
|
52 error(inString, outString, "ASCII input eaten"); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 function run_test() { |
|
58 gConverter = new ScriptableUnicodeConverter(); |
|
59 gConverter.charset = charset; |
|
60 |
|
61 var byte1, byte2, byte3, byte4; |
|
62 |
|
63 // 2-byte |
|
64 for (byte1 = 1; byte1 < 0x100; ++byte1) { |
|
65 for (byte2 = 1; byte2 < 0x100; ++byte2) { |
|
66 test(String.fromCharCode(byte1, byte2) + " foo"); |
|
67 } |
|
68 } |
|
69 // 4-byte (limited) |
|
70 for (byte1 = 0x80; byte1 < 0x90; ++byte1) { |
|
71 for (byte2 = 0x20; byte2 < 0x40; ++byte2) { |
|
72 for (byte3 = 0x80; byte3 < 0x90; ++byte3) { |
|
73 for (byte4 = 0x20; byte4 < 0x40; ++byte4) { |
|
74 test(String.fromCharCode(byte1, byte2, byte3, byte4) + " foo"); |
|
75 } |
|
76 } |
|
77 } |
|
78 } |
|
79 } |