1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* EUC-JP <=> Unicode translate functions.
* by Hatuka*nezumi - IKEDA Soji <nezumi@jca.apc.org>
*
*/
#include <stdio.h>
#include <string.h>
#include "unicode.h"
extern const unicode_char* jisx0208_to_uni_tbls[];
extern const unicode_char* jisx0212_to_uni_tbls[];
extern const unsigned* uni_to_jisx0208_tbls[];
extern const unsigned* uni_to_jisx0212_tbls[];
static unicode_char *c2u(const struct unicode_info *u,
const char *eucjp_str, int *err)
{
unicode_char *uc=0;
unsigned char hi=0, lo=0;
int len=0;
int i=0;
int pos=0;
if(err) *err = -1;
len = strlen(eucjp_str);
uc = (unicode_char*)malloc((len+1) * sizeof(unicode_char) *2);
if (!uc)
return NULL;
for(i=0; i<len;) {
/* US-ASCII */
if((unsigned char)eucjp_str[i] < 0x80)
{
uc[pos++] = (unicode_char)(eucjp_str[i]);
i++;
}
/* JIS X 0201 GR; SS2 */
else if ((unsigned char)eucjp_str[i] == 0x8e
&& (unsigned char)eucjp_str[i+1] >= 0xa1
&& (unsigned char)eucjp_str[i+1] <= 0xdf)
{
lo = (unsigned char)eucjp_str[i+1];
/* EUCJP -> JIS */
lo -= 0x80;
uc[pos++] = (unicode_char)(lo+(unsigned)0xff40);
i+=2;
}
/* JIS X 0212; SS3 */
else if ((unsigned char)eucjp_str[i] == 0x8f
&& (unsigned char)eucjp_str[i+1] >= 0xa1
&& (unsigned char)eucjp_str[i+2] >= 0xa1)
{
hi = (unsigned char)eucjp_str[i+1];
lo = (unsigned char)eucjp_str[i+2];
/* EUCJP -> JIS */
hi -= 0x80;
lo -= 0x80;
if (jisx0212_to_uni_tbls[hi-0x21] != NULL
&& jisx0212_to_uni_tbls[hi-0x21][lo-0x21] != 0x003f)
uc[pos++] = jisx0212_to_uni_tbls[hi-0x21][lo-0x21];
else if (err)
{
*err = i;
free(uc);
return NULL;
}
else
uc[pos++] = (unicode_char)0xfffd;
i+=3;
}
/* JIS X 0208 */
else if ((unsigned char)eucjp_str[i] >= 0xa1
&& (unsigned char)eucjp_str[i+1] >= 0xa1)
{
hi = (unsigned char)eucjp_str[i];
lo = (unsigned char)eucjp_str[i+1];
/* EUCJP -> JIS */
hi -= 0x80;
lo -= 0x80;
/* JIS -> Unicode */
if (jisx0208_to_uni_tbls[hi-0x21] != NULL
&& jisx0208_to_uni_tbls[hi-0x21][lo-0x21] != 0x003f)
uc[pos++] = jisx0208_to_uni_tbls[hi-0x21][lo-0x21];
else if (err)
{
*err = i;
free(uc);
return NULL;
}
else
uc[pos++] = (unicode_char)0xfffd;
i+=2;
}
/* Not found */
else if (err)
{
*err = i;
free(uc);
return NULL;
}
else
{
uc[pos++] = (unicode_char)0xfffd;
i++;
}
}
uc[pos++] = 0;
return uc;
}
static char *u2c(const struct unicode_info *u,
const unicode_char *str, int *err)
{
int i=0;
int pos=0;
int len=0;
char* s;
if(err) *err = -1;
while(str[len])
len++;
s = malloc((len+1)*2);
if (!s)
return NULL;
for(i=0; str[i]; i++)
{
int jis_char = 0;
unsigned char hi=0, lo=0;
unsigned char str_i_high=str[i] >> 8;
/* EUC-JP is mapped inside BMP range. */
if (str[i] >= (unicode_char)0x10000)
{
if (err)
{
*err = i;
free(s);
return NULL;
}
s[pos++] = '?';
}
/* US-ASCII */
else if (str[i] < (unicode_char)0x0080)
s[pos++] = str[i];
/* For compatibility: 2 characters replaced by JIS X 0201 */
else if (str[i] == (unicode_char)0x00A5) /* YEN SIGN */
s[pos++] = 0x5C;
else if (str[i] == (unicode_char)0x203E) /* OVERLINE */
s[pos++] = 0x7E;
/* JIS X 0201 GR */
else if (str[i] >= (unicode_char)0xff61
&& str[i] <= (unicode_char)0xff9f)
{
lo = (unsigned char)(str[i] - (unsigned)0xff40);
/* JIS -> EUCJP */
lo += 0x80;
s[pos++] = 0x8e;
s[pos++] = lo;
}
/* JIS X 0208 */
else if (uni_to_jisx0208_tbls[str_i_high] != NULL
&& uni_to_jisx0208_tbls[str_i_high][str[i] & 0xff] != 0x003F)
{
/* Unicode -> JIS */
jis_char = uni_to_jisx0208_tbls[str_i_high][str[i] & 0xff];
hi = jis_char >> 8;
lo = jis_char & 0xff;
if (hi)
{
/* JIS -> EUCJP */
hi += 0x80;
lo += 0x80;
s[pos++] = hi;
s[pos++] = lo;
}
else if (err)
{
*err = i;
free(s);
return NULL;
}
else
s[pos++] = '?';
}
/* Otherwise, search on JIS X 0212 */
else if (uni_to_jisx0212_tbls[str_i_high] != NULL
&& uni_to_jisx0212_tbls[str_i_high][str[i] & 0xff] != 0x003F)
{
/* Unicode -> JIS */
jis_char = uni_to_jisx0212_tbls[str_i_high][str[i] & 0xff];
hi = jis_char >> 8;
lo = jis_char & 0xff;
if (hi) {
/* JIS -> EUCJP */
hi += 0x80;
lo += 0x80;
s[pos++] = 0x8f;
s[pos++] = hi;
s[pos++] = lo;
}
else if (err)
{
*err = i;
free(s);
return NULL;
}
else
s[pos++] = '?';
}
/* Not found */
else if (err)
{
*err = i;
free(s);
return NULL;
}
else
s[pos++] = '?';
}
s[pos] = 0;
return s;
}
static char *toupper_func(const struct unicode_info *u,
const char *cp, int *ip)
{
unicode_char *uc = c2u(u, cp, ip);
char *s;
size_t i;
if (!uc)
return (NULL);
for (i=0; uc[i] && i<10000; i++) {
if ((unicode_char)'a' <= uc[i] && uc[i] <= (unicode_char)'z')
uc[i] = uc[i] - ((unicode_char)'a' - (unicode_char)'A');
}
s = u2c(u, uc, NULL);
free(uc);
return (s);
}
static char *tolower_func(const struct unicode_info *u,
const char *cp, int *ip)
{
unicode_char *uc = c2u(u, cp, ip);
char *s;
size_t i;
if (!uc)
return (NULL);
for (i=0; uc[i]; i++) {
if ((unicode_char)'A' <= uc[i] && uc[i] <= (unicode_char)'Z')
uc[i] = uc[i] + ((unicode_char)'a' - (unicode_char)'A');
}
s = u2c(u, uc, NULL);
free(uc);
return (s);
}
static char *totitle_func(const struct unicode_info *u,
const char *cp, int *ip)
{
unicode_char *uc = c2u(u, cp, ip);
char *s;
if (!uc)
return (NULL);
/* Uh, sorry, what's "title" char? */
/*
* for (i=0; uc[i]; i++)
* uc[i] = unicode_tc(uc[i]);
*/
s = u2c(u, uc, NULL);
free(uc);
return (s);
}
extern const struct unicode_info unicode_UTF8;
const struct unicode_info unicode_EUC_JP = {
"EUC-JP",
UNICODE_MB | UNICODE_REPLACEABLE | UNICODE_USASCII |
UNICODE_HEADER_BASE64 | UNICODE_BODY_BASE64,
c2u,
u2c,
toupper_func,
tolower_func,
totitle_func,
&unicode_UTF8
};
|