tor-browser

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

eucjp-decoder.js (2551B)


      1 function dec2char(n) {
      2    // converts a decimal number to a Unicode character
      3    // n: the dec codepoint value to be converted
      4    if (n <= 0xffff) {
      5        out = String.fromCharCode(n);
      6    } else if (n <= 0x10ffff) {
      7        n -= 0x10000;
      8        out =
      9            String.fromCharCode(0xd800 | (n >> 10)) +
     10            String.fromCharCode(0xdc00 | (n & 0x3ff));
     11    } else out = "dec2char error: Code point out of range: " + n;
     12    return out;
     13 }
     14 
     15 function eucjpDecoder(stream) {
     16    stream = stream.replace(/%/g, " ");
     17    stream = stream.replace(/[\s]+/g, " ").trim();
     18    var bytes = stream.split(" ");
     19    for (var i = 0; i < bytes.length; i++) bytes[i] = parseInt(bytes[i], 16);
     20    var out = "";
     21 
     22    var lead, byte, offset, ptr, cp;
     23    var jis0212flag = false;
     24    var eucjpLead = 0x00;
     25    var endofstream = 2000000;
     26    var finished = false;
     27 
     28    while (!finished) {
     29        if (bytes.length == 0) byte = endofstream;
     30        else byte = bytes.shift();
     31 
     32        if (byte == endofstream && eucjpLead != 0x00) {
     33            eucjpLead = 0x00;
     34            out += "�";
     35            continue;
     36        }
     37        if (byte == endofstream && eucjpLead == 0x00) {
     38            finished = true;
     39            continue;
     40        }
     41        if (eucjpLead == 0x8e && byte >= 0xa1 && byte <= 0xdf) {
     42            eucjpLead = 0x00;
     43            out += dec2char(0xff61 + byte - 0xa1);
     44            continue;
     45        }
     46        if (eucjpLead == 0x8f && byte >= 0xa1 && byte <= 0xfe) {
     47            jis0212flag = true;
     48            eucjpLead = byte;
     49            continue;
     50        }
     51        if (eucjpLead != 0x00) {
     52            lead = eucjpLead;
     53            eucjpLead = 0x00;
     54            cp = null;
     55 
     56            if (
     57                lead >= 0xa1 &&
     58                lead <= 0xfe &&
     59                (byte >= 0xa1 && byte <= 0xfe)
     60            ) {
     61                ptr = (lead - 0xa1) * 94 + byte - 0xa1;
     62                if (jis0212flag) cp = jis0212[ptr];
     63                else cp = jis0208[ptr];
     64            }
     65            jis0212flag = false;
     66            if (cp != null) {
     67                out += dec2char(cp);
     68                continue;
     69            }
     70            if (byte >= 0x00 && byte <= 0x7f) bytes.unshift(byte);
     71            out += "�";
     72            continue;
     73        }
     74        if (byte >= 0x00 && byte <= 0x7f) {
     75            out += dec2char(byte);
     76            continue;
     77        }
     78        if (byte == 0x8e || byte == 0x8f || (byte >= 0xa1 && byte <= 0xfe)) {
     79            eucjpLead = byte;
     80            continue;
     81        }
     82        out += "�";
     83    }
     84    return out;
     85 }