1function 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 15function sjisDecoder(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 var lead, byte, leadoffset, offset, ptr, cp; 22 var sjisLead = 0x00; 23 var endofstream = 2000000; 24 var finished = false; 25 26 while (!finished) { 27 if (bytes.length == 0) byte = endofstream; 28 else byte = bytes.shift(); 29 30 if (byte == endofstream && sjisLead != 0x00) { 31 sjisLead = 0x00; 32 out += "�"; 33 continue; 34 } 35 if (byte == endofstream && sjisLead == 0x00) { 36 finished = true; 37 continue; 38 } 39 if (sjisLead != 0x00) { 40 lead = sjisLead; 41 ptr = null; 42 sjisLead = 0x00; 43 if (byte < 0x7f) offset = 0x40; 44 else offset = 0x41; 45 if (lead < 0xa0) leadoffset = 0x81; 46 else leadoffset = 0xc1; 47 if ((byte >= 0x40 && byte <= 0x7e) || (byte >= 0x80 && byte <= 0xfc)) 48 ptr = (lead - leadoffset) * 188 + byte - offset; 49 if (ptr != null && ptr >= 8836 && ptr <= 10715) { 50 out += dec2char(0xe000 + ptr - 8836); 51 continue; 52 } 53 if (ptr == null) cp = null; 54 else cp = jis0208[ptr]; 55 if (cp == null && byte >= 0x00 && byte <= 0x7f) { 56 bytes.unshift(byte); 57 } 58 if (cp == null) { 59 out += "�"; 60 continue; 61 } 62 out += dec2char(cp); 63 continue; 64 } 65 if ((byte >= 0x00 && byte <= 0x7f) || byte == 0x80) { 66 out += dec2char(byte); 67 continue; 68 } 69 if (byte >= 0xa1 && byte <= 0xdf) { 70 out += dec2char(0xff61 + byte - 0xa1); 71 continue; 72 } 73 if ((byte >= 0x81 && byte <= 0x9f) || (byte >= 0xe0 && byte <= 0xfc)) { 74 sjisLead = byte; 75 continue; 76 } 77 out += "�"; 78 } 79 return out; 80} 81