11cb0ef41Sopenharmony_cifunction dec2char(n) {
21cb0ef41Sopenharmony_ci    // converts a decimal number to a Unicode character
31cb0ef41Sopenharmony_ci    // n: the dec codepoint value to be converted
41cb0ef41Sopenharmony_ci    if (n <= 0xffff) {
51cb0ef41Sopenharmony_ci        out = String.fromCharCode(n);
61cb0ef41Sopenharmony_ci    } else if (n <= 0x10ffff) {
71cb0ef41Sopenharmony_ci        n -= 0x10000;
81cb0ef41Sopenharmony_ci        out =
91cb0ef41Sopenharmony_ci            String.fromCharCode(0xd800 | (n >> 10)) +
101cb0ef41Sopenharmony_ci            String.fromCharCode(0xdc00 | (n & 0x3ff));
111cb0ef41Sopenharmony_ci    } else out = "dec2char error: Code point out of range: " + n;
121cb0ef41Sopenharmony_ci    return out;
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction eucjpDecoder(stream) {
161cb0ef41Sopenharmony_ci    stream = stream.replace(/%/g, " ");
171cb0ef41Sopenharmony_ci    stream = stream.replace(/[\s]+/g, " ").trim();
181cb0ef41Sopenharmony_ci    var bytes = stream.split(" ");
191cb0ef41Sopenharmony_ci    for (var i = 0; i < bytes.length; i++) bytes[i] = parseInt(bytes[i], 16);
201cb0ef41Sopenharmony_ci    var out = "";
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci    var lead, byte, offset, ptr, cp;
231cb0ef41Sopenharmony_ci    var jis0212flag = false;
241cb0ef41Sopenharmony_ci    var eucjpLead = 0x00;
251cb0ef41Sopenharmony_ci    var endofstream = 2000000;
261cb0ef41Sopenharmony_ci    var finished = false;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    while (!finished) {
291cb0ef41Sopenharmony_ci        if (bytes.length == 0) byte = endofstream;
301cb0ef41Sopenharmony_ci        else byte = bytes.shift();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci        if (byte == endofstream && eucjpLead != 0x00) {
331cb0ef41Sopenharmony_ci            eucjpLead = 0x00;
341cb0ef41Sopenharmony_ci            out += "�";
351cb0ef41Sopenharmony_ci            continue;
361cb0ef41Sopenharmony_ci        }
371cb0ef41Sopenharmony_ci        if (byte == endofstream && eucjpLead == 0x00) {
381cb0ef41Sopenharmony_ci            finished = true;
391cb0ef41Sopenharmony_ci            continue;
401cb0ef41Sopenharmony_ci        }
411cb0ef41Sopenharmony_ci        if (eucjpLead == 0x8e && byte >= 0xa1 && byte <= 0xdf) {
421cb0ef41Sopenharmony_ci            eucjpLead = 0x00;
431cb0ef41Sopenharmony_ci            out += dec2char(0xff61 + byte - 0xa1);
441cb0ef41Sopenharmony_ci            continue;
451cb0ef41Sopenharmony_ci        }
461cb0ef41Sopenharmony_ci        if (eucjpLead == 0x8f && byte >= 0xa1 && byte <= 0xfe) {
471cb0ef41Sopenharmony_ci            jis0212flag = true;
481cb0ef41Sopenharmony_ci            eucjpLead = byte;
491cb0ef41Sopenharmony_ci            continue;
501cb0ef41Sopenharmony_ci        }
511cb0ef41Sopenharmony_ci        if (eucjpLead != 0x00) {
521cb0ef41Sopenharmony_ci            lead = eucjpLead;
531cb0ef41Sopenharmony_ci            eucjpLead = 0x00;
541cb0ef41Sopenharmony_ci            cp = null;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci            if (
571cb0ef41Sopenharmony_ci                lead >= 0xa1 &&
581cb0ef41Sopenharmony_ci                lead <= 0xfe &&
591cb0ef41Sopenharmony_ci                (byte >= 0xa1 && byte <= 0xfe)
601cb0ef41Sopenharmony_ci            ) {
611cb0ef41Sopenharmony_ci                ptr = (lead - 0xa1) * 94 + byte - 0xa1;
621cb0ef41Sopenharmony_ci                if (jis0212flag) cp = jis0212[ptr];
631cb0ef41Sopenharmony_ci                else cp = jis0208[ptr];
641cb0ef41Sopenharmony_ci            }
651cb0ef41Sopenharmony_ci            jis0212flag = false;
661cb0ef41Sopenharmony_ci            if (cp != null) {
671cb0ef41Sopenharmony_ci                out += dec2char(cp);
681cb0ef41Sopenharmony_ci                continue;
691cb0ef41Sopenharmony_ci            }
701cb0ef41Sopenharmony_ci            if (byte >= 0x00 && byte <= 0x7f) bytes.unshift(byte);
711cb0ef41Sopenharmony_ci            out += "�";
721cb0ef41Sopenharmony_ci            continue;
731cb0ef41Sopenharmony_ci        }
741cb0ef41Sopenharmony_ci        if (byte >= 0x00 && byte <= 0x7f) {
751cb0ef41Sopenharmony_ci            out += dec2char(byte);
761cb0ef41Sopenharmony_ci            continue;
771cb0ef41Sopenharmony_ci        }
781cb0ef41Sopenharmony_ci        if (byte == 0x8e || byte == 0x8f || (byte >= 0xa1 && byte <= 0xfe)) {
791cb0ef41Sopenharmony_ci            eucjpLead = byte;
801cb0ef41Sopenharmony_ci            continue;
811cb0ef41Sopenharmony_ci        }
821cb0ef41Sopenharmony_ci        out += "�";
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci    return out;
851cb0ef41Sopenharmony_ci}
86