11cb0ef41Sopenharmony_civar jis0208CPs = []; // index is unicode cp, value is pointer
21cb0ef41Sopenharmony_cifor (var p = 0; p < jis0208.length; p++) {
31cb0ef41Sopenharmony_ci    if (jis0208[p] != null && jis0208CPs[jis0208[p]] == null) {
41cb0ef41Sopenharmony_ci        jis0208CPs[jis0208[p]] = p;
51cb0ef41Sopenharmony_ci    }
61cb0ef41Sopenharmony_ci}
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cifunction chars2cps(chars) {
91cb0ef41Sopenharmony_ci    // this is needed because of javascript's handling of supplementary characters
101cb0ef41Sopenharmony_ci    // char: a string of unicode characters
111cb0ef41Sopenharmony_ci    // returns an array of decimal code point values
121cb0ef41Sopenharmony_ci    var haut = 0;
131cb0ef41Sopenharmony_ci    var out = [];
141cb0ef41Sopenharmony_ci    for (var i = 0; i < chars.length; i++) {
151cb0ef41Sopenharmony_ci        var b = chars.charCodeAt(i);
161cb0ef41Sopenharmony_ci        if (b < 0 || b > 0xffff) {
171cb0ef41Sopenharmony_ci            alert(
181cb0ef41Sopenharmony_ci                "Error in chars2cps: byte out of range " + b.toString(16) + "!"
191cb0ef41Sopenharmony_ci            );
201cb0ef41Sopenharmony_ci        }
211cb0ef41Sopenharmony_ci        if (haut != 0) {
221cb0ef41Sopenharmony_ci            if (0xdc00 <= b && b <= 0xdfff) {
231cb0ef41Sopenharmony_ci                out.push(0x10000 + ((haut - 0xd800) << 10) + (b - 0xdc00));
241cb0ef41Sopenharmony_ci                haut = 0;
251cb0ef41Sopenharmony_ci                continue;
261cb0ef41Sopenharmony_ci            } else {
271cb0ef41Sopenharmony_ci                alert(
281cb0ef41Sopenharmony_ci                    "Error in chars2cps: surrogate out of range " +
291cb0ef41Sopenharmony_ci                        haut.toString(16) +
301cb0ef41Sopenharmony_ci                        "!"
311cb0ef41Sopenharmony_ci                );
321cb0ef41Sopenharmony_ci                haut = 0;
331cb0ef41Sopenharmony_ci            }
341cb0ef41Sopenharmony_ci        }
351cb0ef41Sopenharmony_ci        if (0xd800 <= b && b <= 0xdbff) {
361cb0ef41Sopenharmony_ci            haut = b;
371cb0ef41Sopenharmony_ci        } else {
381cb0ef41Sopenharmony_ci            out.push(b);
391cb0ef41Sopenharmony_ci        }
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci    return out;
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cifunction eucjpEncoder(stream) {
451cb0ef41Sopenharmony_ci    var cps = chars2cps(stream);
461cb0ef41Sopenharmony_ci    var out = "";
471cb0ef41Sopenharmony_ci    var cp;
481cb0ef41Sopenharmony_ci    var finished = false;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    while (!finished) {
511cb0ef41Sopenharmony_ci        if (cps.length == 0) {
521cb0ef41Sopenharmony_ci            finished = true;
531cb0ef41Sopenharmony_ci            continue;
541cb0ef41Sopenharmony_ci        } else cp = cps.shift();
551cb0ef41Sopenharmony_ci        if (cp >= 0x00 && cp <= 0x7f) {
561cb0ef41Sopenharmony_ci            // ASCII
571cb0ef41Sopenharmony_ci            out += " " + cp.toString(16).toUpperCase();
581cb0ef41Sopenharmony_ci            continue;
591cb0ef41Sopenharmony_ci        }
601cb0ef41Sopenharmony_ci        if (cp == 0xa5) {
611cb0ef41Sopenharmony_ci            out += " 5C";
621cb0ef41Sopenharmony_ci            continue;
631cb0ef41Sopenharmony_ci        }
641cb0ef41Sopenharmony_ci        if (cp == 0x203e) {
651cb0ef41Sopenharmony_ci            out += " 7E";
661cb0ef41Sopenharmony_ci            continue;
671cb0ef41Sopenharmony_ci        }
681cb0ef41Sopenharmony_ci        if (cp >= 0xff61 && cp <= 0xff9f) {
691cb0ef41Sopenharmony_ci            var temp = cp - 0xff61 + 0xa1;
701cb0ef41Sopenharmony_ci            out += " 8E " + temp.toString(16).toUpperCase();
711cb0ef41Sopenharmony_ci            continue;
721cb0ef41Sopenharmony_ci        }
731cb0ef41Sopenharmony_ci        if (cp == 0x2212) {
741cb0ef41Sopenharmony_ci            cp = 0xff0d;
751cb0ef41Sopenharmony_ci        }
761cb0ef41Sopenharmony_ci        var ptr = jis0208CPs[cp];
771cb0ef41Sopenharmony_ci        if (ptr == null) {
781cb0ef41Sopenharmony_ci            return null;
791cb0ef41Sopenharmony_ci            //          out += ' &#'+cp+';'
801cb0ef41Sopenharmony_ci            //          continue
811cb0ef41Sopenharmony_ci        }
821cb0ef41Sopenharmony_ci        var lead = Math.floor(ptr / 94) + 0xa1;
831cb0ef41Sopenharmony_ci        var trail = ptr % 94 + 0xa1;
841cb0ef41Sopenharmony_ci        out +=
851cb0ef41Sopenharmony_ci            " " +
861cb0ef41Sopenharmony_ci            lead.toString(16).toUpperCase() +
871cb0ef41Sopenharmony_ci            " " +
881cb0ef41Sopenharmony_ci            trail.toString(16).toUpperCase();
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci    return out.trim();
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cifunction convertToHex(str) {
941cb0ef41Sopenharmony_ci    // converts a string of ASCII characters to hex byte codes
951cb0ef41Sopenharmony_ci    var out = "";
961cb0ef41Sopenharmony_ci    var result;
971cb0ef41Sopenharmony_ci    for (var c = 0; c < str.length; c++) {
981cb0ef41Sopenharmony_ci        result =
991cb0ef41Sopenharmony_ci            str
1001cb0ef41Sopenharmony_ci                .charCodeAt(c)
1011cb0ef41Sopenharmony_ci                .toString(16)
1021cb0ef41Sopenharmony_ci                .toUpperCase() + " ";
1031cb0ef41Sopenharmony_ci        out += result;
1041cb0ef41Sopenharmony_ci    }
1051cb0ef41Sopenharmony_ci    return out;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_cifunction normalizeStr(str) {
1091cb0ef41Sopenharmony_ci    var out = "";
1101cb0ef41Sopenharmony_ci    for (var c = 0; c < str.length; c++) {
1111cb0ef41Sopenharmony_ci        if (str.charAt(c) == "%") {
1121cb0ef41Sopenharmony_ci            out += String.fromCodePoint(
1131cb0ef41Sopenharmony_ci                parseInt(str.charAt(c + 1) + str.charAt(c + 2), 16)
1141cb0ef41Sopenharmony_ci            );
1151cb0ef41Sopenharmony_ci            c += 2;
1161cb0ef41Sopenharmony_ci        } else out += str.charAt(c);
1171cb0ef41Sopenharmony_ci    }
1181cb0ef41Sopenharmony_ci    var result = "";
1191cb0ef41Sopenharmony_ci    for (var o = 0; o < out.length; o++) {
1201cb0ef41Sopenharmony_ci        result +=
1211cb0ef41Sopenharmony_ci            "%" +
1221cb0ef41Sopenharmony_ci            out
1231cb0ef41Sopenharmony_ci                .charCodeAt(o)
1241cb0ef41Sopenharmony_ci                .toString(16)
1251cb0ef41Sopenharmony_ci                .toUpperCase();
1261cb0ef41Sopenharmony_ci    }
1271cb0ef41Sopenharmony_ci    return result.replace(/%1B%28%42$/, "");
1281cb0ef41Sopenharmony_ci}
129