michael@0: const charset = "hz-gb-2312"; michael@0: michael@0: function dumpStrings(inString, outString) { 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: } michael@0: michael@0: function error(inString, outString, msg){ michael@0: dumpStrings(inString, outString); michael@0: do_throw("security risk: " + msg); michael@0: } michael@0: michael@0: function run_test() { michael@0: var ScriptableUnicodeConverter = michael@0: Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter", michael@0: "nsIScriptableUnicodeConverter"); michael@0: michael@0: var converter = new ScriptableUnicodeConverter(); michael@0: converter.charset = charset; michael@0: michael@0: var leadByte, trailByte; michael@0: var inString; michael@0: for (leadByte = 1; leadByte < 0x100; ++leadByte) { michael@0: for (trailByte = 1; trailByte < 0x100; ++trailByte) { michael@0: if (leadByte == 0x7e) { michael@0: if (trailByte == 0x7b || michael@0: trailByte == 0xa || michael@0: trailByte == 0x7e) { michael@0: // ignore escape sequences: michael@0: // ~{ (sets HZ-GB mode) michael@0: // ~\n ( ==> \n) michael@0: // ~~ ( ==> ~) michael@0: continue; michael@0: } michael@0: } michael@0: inString = String.fromCharCode(leadByte, trailByte, 65); michael@0: var outString = converter.ConvertToUnicode(inString) + michael@0: converter.Finish(); michael@0: switch (outString.length) { michael@0: case 1: michael@0: error(inString, outString, "2 byte sequence eaten"); michael@0: break; michael@0: case 2: michael@0: if (outString.charCodeAt(0) < 0x80 && michael@0: outString.charCodeAt(1) < 0x80) { michael@0: error(inString, outString, michael@0: "2 byte sequence converted to 1 ASCII"); michael@0: } michael@0: break; michael@0: case 3: michael@0: if (outString != inString && michael@0: outString.charCodeAt(0) < 0x80 && michael@0: outString.charCodeAt(1) < 0x80) { michael@0: error(inString, outString, michael@0: "2 byte sequence converted to 2 ASCII"); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: }