|
1 const charset = "EUC-JP"; |
|
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 IsNotGR(charCode) { |
|
30 return (charCode < 0xa1 || charCode > 0xfe); |
|
31 } |
|
32 |
|
33 function test(inString) { |
|
34 var outString = gConverter.ConvertToUnicode(inString) + |
|
35 gConverter.Finish(); |
|
36 |
|
37 var outLen = outString.length; |
|
38 if (IsASCII(inString.charCodeAt(1)) && |
|
39 inString.charCodeAt(1) != outString.charCodeAt(outLen - 5)) { |
|
40 error(inString, outString, "ASCII second byte eaten"); |
|
41 } else if (IsASCII(inString.charCodeAt(2)) && |
|
42 inString.charCodeAt(2) != outString.charCodeAt(outLen - 4)) { |
|
43 error(inString, outString, "ASCII third byte eaten"); |
|
44 } else if (inString.charCodeAt(0) == 0x8f && |
|
45 inString.charCodeAt(1) > 0x7f && |
|
46 IsNotGR(inString.charCodeAt(2)) && |
|
47 (!(outString.charCodeAt(outLen - 4) == 0xFFFD || |
|
48 outString.charCodeAt(outLen - 4) == inString.charCodeAt(2)))) { |
|
49 error(inString, outString, "non-GR third byte eaten"); |
|
50 } |
|
51 } |
|
52 |
|
53 function run_test() { |
|
54 gConverter = new ScriptableUnicodeConverter(); |
|
55 gConverter.charset = charset; |
|
56 |
|
57 var byte1, byte2, byte3; |
|
58 for (byte1 = 1; byte1 < 0x100; ++byte1) { |
|
59 for (byte2 = 1; byte2 < 0x100; ++byte2) { |
|
60 if (byte1 == 0x8f) { |
|
61 for (byte3 = 1; byte3 < 0x100; ++byte3) { |
|
62 test(String.fromCharCode(byte1, byte2, byte3) + "foo"); |
|
63 } |
|
64 } else { |
|
65 test(String.fromCharCode(byte1, byte2) + " foo"); |
|
66 } |
|
67 } |
|
68 } |
|
69 } |