| 1 | /* |
|---|
| 2 | * Copyright (c) 2000 Yuki Sawada |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | * notice, this list of conditions and the following disclaimer. |
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 12 | * documentation and/or other materials provided with the distribution. |
|---|
| 13 | * 3. The name of the author may not be used to endorse or promote products |
|---|
| 14 | * derived from this software without specific prior written permission. |
|---|
| 15 | * |
|---|
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include "codeconv.h" |
|---|
| 29 | #include "codeconv_tbl.h" |
|---|
| 30 | |
|---|
| 31 | unsigned int codeconv_euc_to_jis(unsigned int euc) |
|---|
| 32 | { |
|---|
| 33 | unsigned int hi, low; |
|---|
| 34 | |
|---|
| 35 | hi = (euc >> 8) & 0xff; |
|---|
| 36 | low = euc & 0xff; |
|---|
| 37 | |
|---|
| 38 | if (hi < 0x81) { |
|---|
| 39 | hi = 0; |
|---|
| 40 | } else if (low == 0x8e) |
|---|
| 41 | hi = 0; |
|---|
| 42 | else { |
|---|
| 43 | hi -= 0x80; |
|---|
| 44 | low -= 0x80; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | return (hi << 8) | low; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | static unsigned int codeconv_jis_to_unicode(unsigned int jis) |
|---|
| 51 | { |
|---|
| 52 | int k0, k1; |
|---|
| 53 | |
|---|
| 54 | if (jis < 0x80) return jis; // ASCII |
|---|
| 55 | |
|---|
| 56 | k0 = (jis >> 8) - 0x20; |
|---|
| 57 | |
|---|
| 58 | if (k0 < 1 || k0 > 92) |
|---|
| 59 | return 0; |
|---|
| 60 | |
|---|
| 61 | k1 = (jis % 0x100) - 0x20; |
|---|
| 62 | if (k1 < 1 || k1 > 94) |
|---|
| 63 | return 0; |
|---|
| 64 | |
|---|
| 65 | return unicode_tbl[k0 - 1][k1 - 1]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | unsigned int codeconv_euc_to_unicode(unsigned int euc) |
|---|
| 69 | { |
|---|
| 70 | unsigned int jis, unicode; |
|---|
| 71 | |
|---|
| 72 | jis = codeconv_euc_to_jis(euc); |
|---|
| 73 | unicode = codeconv_jis_to_unicode(jis); |
|---|
| 74 | |
|---|
| 75 | return unicode; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | static unsigned int codeconv_unicode_to_jis(unsigned int unicode) |
|---|
| 79 | { |
|---|
| 80 | int k0, k1; |
|---|
| 81 | unsigned int jis; |
|---|
| 82 | |
|---|
| 83 | k0 = (unicode >> 8) & 0xff; |
|---|
| 84 | k1 = unicode & 0xff; |
|---|
| 85 | jis = unicode_rev_table[k0][k1]; |
|---|
| 86 | |
|---|
| 87 | return jis; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | static unsigned int codeconv_jis_to_euc(unsigned int jis) |
|---|
| 91 | { |
|---|
| 92 | unsigned int hi, low; |
|---|
| 93 | |
|---|
| 94 | hi = (jis >> 8) & 0x7f | 0x80; |
|---|
| 95 | low = jis & 0x7f | 0x80; |
|---|
| 96 | |
|---|
| 97 | return (hi << 8) | low; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | unsigned int codeconv_unicode_to_euc(unsigned int unicode) |
|---|
| 101 | { |
|---|
| 102 | unsigned int jis, euc; |
|---|
| 103 | |
|---|
| 104 | if (unicode >= 0xff61 && unicode <= 0xff9f) |
|---|
| 105 | return unicode - 0xff61 + 0x8ea1; |
|---|
| 106 | |
|---|
| 107 | jis = codeconv_unicode_to_jis(unicode); |
|---|
| 108 | if (jis == 0) |
|---|
| 109 | return 0x7878; |
|---|
| 110 | euc = codeconv_jis_to_euc(jis); |
|---|
| 111 | |
|---|
| 112 | return euc; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | static unsigned int codeconv_jis_to_sjis(unsigned int jis) |
|---|
| 116 | { |
|---|
| 117 | unsigned int hi, low; |
|---|
| 118 | |
|---|
| 119 | hi = (jis >> 8) & 0xff; |
|---|
| 120 | low = jis & 0xff; |
|---|
| 121 | |
|---|
| 122 | low += (hi & 0x01) ? 0x1f : 0x7d; |
|---|
| 123 | if (low >= 0x7f) |
|---|
| 124 | low++; |
|---|
| 125 | hi = ((hi - 0x21) >> 1) + 0x81; |
|---|
| 126 | if (hi > 0x9f) |
|---|
| 127 | hi += 0x40; |
|---|
| 128 | |
|---|
| 129 | return (hi << 8) | low; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | unsigned int codeconv_euc_to_sjis(unsigned int euc) |
|---|
| 133 | { |
|---|
| 134 | unsigned int jis, sjis; |
|---|
| 135 | |
|---|
| 136 | jis = codeconv_euc_to_jis(euc); |
|---|
| 137 | sjis = codeconv_jis_to_sjis(jis); |
|---|
| 138 | |
|---|
| 139 | return sjis; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | static unsigned int codeconv_sjis_to_jis(unsigned int sjis) |
|---|
| 143 | { |
|---|
| 144 | unsigned int hi, low; |
|---|
| 145 | |
|---|
| 146 | hi = (sjis >> 8) & 0xff; |
|---|
| 147 | low = sjis & 0xff; |
|---|
| 148 | |
|---|
| 149 | hi -= (hi <= 0x9f) ? 0x71 : 0xb1; |
|---|
| 150 | hi = (hi << 1) + 1; |
|---|
| 151 | if (low > 0x7f) |
|---|
| 152 | low--; |
|---|
| 153 | if (low >= 0x9e) { |
|---|
| 154 | low -= 0x7d; |
|---|
| 155 | hi++; |
|---|
| 156 | } else low -= 0x1f; |
|---|
| 157 | |
|---|
| 158 | return (hi << 8) | low; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | unsigned int codeconv_sjis_to_euc(unsigned int sjis) |
|---|
| 162 | { |
|---|
| 163 | unsigned int jis, euc; |
|---|
| 164 | |
|---|
| 165 | jis = codeconv_sjis_to_jis(sjis); |
|---|
| 166 | euc = codeconv_jis_to_euc(jis); |
|---|
| 167 | |
|---|
| 168 | return euc; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | unsigned int codeconv_euc_to_latin1(unsigned int euc) |
|---|
| 172 | { |
|---|
| 173 | int high = (euc>>8) & 0xff; |
|---|
| 174 | if (high) return 0; |
|---|
| 175 | return euc & 0xff; |
|---|
| 176 | } |
|---|