|
1 const charset = "hz-gb-2312"; |
|
2 |
|
3 function dumpStrings(inString, outString) { |
|
4 var dispIn = ""; |
|
5 var dispOut = ""; |
|
6 var i; |
|
7 for (i = 0; i < inString.length; ++i) { |
|
8 dispIn += " x" + inString.charCodeAt(i).toString(16); |
|
9 } |
|
10 if (outString.length == 0) { |
|
11 dispOut = "<empty>"; |
|
12 } else { |
|
13 for (i = 0; i < outString.length; ++i) { |
|
14 dispOut += " x" + outString.charCodeAt(i).toString(16); |
|
15 } |
|
16 } |
|
17 dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); |
|
18 } |
|
19 |
|
20 function error(inString, outString, msg){ |
|
21 dumpStrings(inString, outString); |
|
22 do_throw("security risk: " + msg); |
|
23 } |
|
24 |
|
25 function run_test() { |
|
26 var ScriptableUnicodeConverter = |
|
27 Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", |
|
28 "nsIScriptableUnicodeConverter"); |
|
29 |
|
30 var converter = new ScriptableUnicodeConverter(); |
|
31 converter.charset = charset; |
|
32 |
|
33 var leadByte, trailByte; |
|
34 var inString; |
|
35 for (leadByte = 1; leadByte < 0x100; ++leadByte) { |
|
36 for (trailByte = 1; trailByte < 0x100; ++trailByte) { |
|
37 if (leadByte == 0x7e) { |
|
38 if (trailByte == 0x7b || |
|
39 trailByte == 0xa || |
|
40 trailByte == 0x7e) { |
|
41 // ignore escape sequences: |
|
42 // ~{ (sets HZ-GB mode) |
|
43 // ~\n ( ==> \n) |
|
44 // ~~ ( ==> ~) |
|
45 continue; |
|
46 } |
|
47 } |
|
48 inString = String.fromCharCode(leadByte, trailByte, 65); |
|
49 var outString = converter.ConvertToUnicode(inString) + |
|
50 converter.Finish(); |
|
51 switch (outString.length) { |
|
52 case 1: |
|
53 error(inString, outString, "2 byte sequence eaten"); |
|
54 break; |
|
55 case 2: |
|
56 if (outString.charCodeAt(0) < 0x80 && |
|
57 outString.charCodeAt(1) < 0x80) { |
|
58 error(inString, outString, |
|
59 "2 byte sequence converted to 1 ASCII"); |
|
60 } |
|
61 break; |
|
62 case 3: |
|
63 if (outString != inString && |
|
64 outString.charCodeAt(0) < 0x80 && |
|
65 outString.charCodeAt(1) < 0x80) { |
|
66 error(inString, outString, |
|
67 "2 byte sequence converted to 2 ASCII"); |
|
68 } |
|
69 break; |
|
70 } |
|
71 } |
|
72 } |
|
73 } |