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