tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug715319.euc_jp.js (2082B)


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