michael@0: const charset = "GB2312"; michael@0: const ScriptableUnicodeConverter = michael@0: Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", michael@0: "nsIScriptableUnicodeConverter"); michael@0: var gConverter; michael@0: michael@0: function error(inString, outString, msg) { michael@0: var dispIn = ""; michael@0: var dispOut = ""; michael@0: var i; michael@0: for (i = 0; i < inString.length; ++i) { michael@0: dispIn += " x" + inString.charCodeAt(i).toString(16); michael@0: } michael@0: if (outString.length == 0) { michael@0: dispOut = ""; michael@0: } else { michael@0: for (i = 0; i < outString.length; ++i) { michael@0: dispOut += " x" + outString.charCodeAt(i).toString(16); michael@0: } michael@0: } michael@0: dump("\"" + dispIn + "\" ==> \"" + dispOut + "\"\n"); michael@0: do_throw("security risk: " + msg); michael@0: } michael@0: michael@0: function IsASCII(charCode) { michael@0: return (charCode <= 0x7e); michael@0: } michael@0: michael@0: function test(inString) { michael@0: var outString = gConverter.ConvertToUnicode(inString) + michael@0: gConverter.Finish(); michael@0: michael@0: var outLen = outString.length; michael@0: for (var pos = 1; pos < 3; ++pos) { michael@0: outPos = outLen - (9 - pos); michael@0: if (outPos < 0) { michael@0: outPos = 0; michael@0: } michael@0: c0 = inString.charCodeAt(0); michael@0: c1 = inString.charCodeAt(1); michael@0: c2 = inString.charCodeAt(2); michael@0: c3 = inString.charCodeAt(3); michael@0: if (IsASCII(inString.charCodeAt(pos)) && michael@0: !(outString.charCodeAt(outPos) == inString.charCodeAt(pos) || michael@0: (outString.charCodeAt(outPos) != 0xFFFD) || michael@0: // legal 4 byte range michael@0: (0x81 <= c0 && c0 <= 0xfe && michael@0: 0x30 <= c1 && c1 <= 0x39 && michael@0: 0x81 <= c2 && c2 <= 0xfe && michael@0: 0x30 <= c3 && c3 <= 0x39))) { michael@0: dump("pos = " + pos + "; outPos = " + outPos + "\n"); michael@0: error(inString, outString, "ASCII input eaten"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: gConverter = new ScriptableUnicodeConverter(); michael@0: gConverter.charset = charset; michael@0: michael@0: var byte1, byte2, byte3, byte4; michael@0: michael@0: // 2-byte michael@0: for (byte1 = 1; byte1 < 0x100; ++byte1) { michael@0: for (byte2 = 1; byte2 < 0x100; ++byte2) { michael@0: test(String.fromCharCode(byte1, byte2) + " foo"); michael@0: } michael@0: } michael@0: // 4-byte (limited) michael@0: for (byte1 = 0x80; byte1 < 0x90; ++byte1) { michael@0: for (byte2 = 0x20; byte2 < 0x40; ++byte2) { michael@0: for (byte3 = 0x80; byte3 < 0x90; ++byte3) { michael@0: for (byte4 = 0x20; byte4 < 0x40; ++byte4) { michael@0: test(String.fromCharCode(byte1, byte2, byte3, byte4) + " foo"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: }