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 big5Decoder(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 var lead, byte, offset, ptr, cp; 221cb0ef41Sopenharmony_ci var big5lead = 0x00; 231cb0ef41Sopenharmony_ci var endofstream = 2000000; 241cb0ef41Sopenharmony_ci var finished = false; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci while (!finished) { 271cb0ef41Sopenharmony_ci if (bytes.length == 0) byte = endofstream; 281cb0ef41Sopenharmony_ci else byte = bytes.shift(); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci if (byte == endofstream && big5lead != 0x00) { 311cb0ef41Sopenharmony_ci big5lead = 0x00; 321cb0ef41Sopenharmony_ci out += "�"; 331cb0ef41Sopenharmony_ci continue; 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci if (byte == endofstream && big5lead == 0x00) { 361cb0ef41Sopenharmony_ci finished = true; 371cb0ef41Sopenharmony_ci continue; 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci if (big5lead != 0x00) { 411cb0ef41Sopenharmony_ci lead = big5lead; 421cb0ef41Sopenharmony_ci ptr = null; 431cb0ef41Sopenharmony_ci big5lead = 0x00; 441cb0ef41Sopenharmony_ci if (byte < 0x7f) offset = 0x40; 451cb0ef41Sopenharmony_ci else offset = 0x62; 461cb0ef41Sopenharmony_ci if ((byte >= 0x40 && byte <= 0x7e) || (byte >= 0xa1 && byte <= 0xfe)) 471cb0ef41Sopenharmony_ci ptr = (lead - 0x81) * 157 + (byte - offset); 481cb0ef41Sopenharmony_ci // "If there is a row in the table below whose first column is pointer, return the two code points listed in its second column" 491cb0ef41Sopenharmony_ci switch (ptr) { 501cb0ef41Sopenharmony_ci case 1133: 511cb0ef41Sopenharmony_ci out += "Ê̄"; 521cb0ef41Sopenharmony_ci continue; 531cb0ef41Sopenharmony_ci case 1135: 541cb0ef41Sopenharmony_ci out += "Ê̌"; 551cb0ef41Sopenharmony_ci continue; 561cb0ef41Sopenharmony_ci case 1164: 571cb0ef41Sopenharmony_ci out += "ê̄"; 581cb0ef41Sopenharmony_ci continue; 591cb0ef41Sopenharmony_ci case 1166: 601cb0ef41Sopenharmony_ci out += "ê̌"; 611cb0ef41Sopenharmony_ci continue; 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci if (ptr == null) cp = null; 641cb0ef41Sopenharmony_ci else cp = big5[ptr]; 651cb0ef41Sopenharmony_ci if (cp == null && byte >= 0x00 && byte <= 0x7f) { 661cb0ef41Sopenharmony_ci bytes.unshift(byte); 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci if (cp == null) { 691cb0ef41Sopenharmony_ci out += "�"; 701cb0ef41Sopenharmony_ci continue; 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci out += dec2char(cp); 731cb0ef41Sopenharmony_ci continue; 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci if (byte >= 0x00 && byte <= 0x7f) { 761cb0ef41Sopenharmony_ci out += dec2char(byte); 771cb0ef41Sopenharmony_ci continue; 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci if (byte >= 0x81 && byte <= 0xfe) { 801cb0ef41Sopenharmony_ci big5lead = byte; 811cb0ef41Sopenharmony_ci continue; 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci out += "�"; 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci return out; 861cb0ef41Sopenharmony_ci} 87