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