root/ext/mbstring/libmbfl/filters/mbfilter_euc_jp_win.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mbfl_filt_conv_eucjpwin_wchar
  2. mbfl_filt_conv_wchar_eucjpwin
  3. mbfl_filt_ident_eucjp_win

   1 /*
   2  * "streamable kanji code filter and converter"
   3  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
   4  *
   5  * LICENSE NOTICES
   6  *
   7  * This file is part of "streamable kanji code filter and converter",
   8  * which is distributed under the terms of GNU Lesser General Public
   9  * License (version 2) as published by the Free Software Foundation.
  10  *
  11  * This software is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU Lesser General Public License for more details.
  15  *
  16  * You should have received a copy of the GNU Lesser General Public
  17  * License along with "streamable kanji code filter and converter";
  18  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  19  * Suite 330, Boston, MA  02111-1307  USA
  20  *
  21  * The author of this file:
  22  *
  23  */
  24 /*
  25  * The source code included in this files was separated from mbfilter_ja.c
  26  * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
  27  *
  28  */
  29 
  30 #ifdef HAVE_CONFIG_H
  31 #include "config.h"
  32 #endif
  33 
  34 #include "mbfilter.h"
  35 #include "mbfilter_euc_jp_win.h"
  36 
  37 #include "unicode_table_cp932_ext.h"
  38 #include "unicode_table_jis.h"
  39 #include "cp932_table.h"
  40 
  41 static int mbfl_filt_ident_eucjp_win(int c, mbfl_identify_filter *filter);
  42 
  43 static const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
  44   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  46   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
  53   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  59   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
  60 };
  61 
  62 
  63 static const char *mbfl_encoding_eucjp_win_aliases[] = {"eucJP-open",
  64                                                         "eucJP-ms", NULL};
  65 
  66 const struct mbfl_identify_vtbl vtbl_identify_eucjpwin = {
  67         mbfl_no_encoding_eucjp_win,
  68         mbfl_filt_ident_common_ctor,
  69         mbfl_filt_ident_common_dtor,
  70         mbfl_filt_ident_eucjp_win
  71 };
  72 
  73 const mbfl_encoding mbfl_encoding_eucjp_win = {
  74         mbfl_no_encoding_eucjp_win,
  75         "eucJP-win",
  76         "EUC-JP",
  77         (const char *(*)[])&mbfl_encoding_eucjp_win_aliases,
  78         mblen_table_eucjp,
  79         MBFL_ENCTYPE_MBCS
  80 };
  81 
  82 const struct mbfl_convert_vtbl vtbl_eucjpwin_wchar = {
  83         mbfl_no_encoding_eucjp_win,
  84         mbfl_no_encoding_wchar,
  85         mbfl_filt_conv_common_ctor,
  86         mbfl_filt_conv_common_dtor,
  87         mbfl_filt_conv_eucjpwin_wchar,
  88         mbfl_filt_conv_common_flush
  89 };
  90 
  91 const struct mbfl_convert_vtbl vtbl_wchar_eucjpwin = {
  92         mbfl_no_encoding_wchar,
  93         mbfl_no_encoding_eucjp_win,
  94         mbfl_filt_conv_common_ctor,
  95         mbfl_filt_conv_common_dtor,
  96         mbfl_filt_conv_wchar_eucjpwin,
  97         mbfl_filt_conv_common_flush
  98 };
  99 
 100 #define CK(statement)   do { if ((statement) < 0) return (-1); } while (0)
 101 
 102 /*
 103  * eucJP-win => wchar
 104  */
 105 int
 106 mbfl_filt_conv_eucjpwin_wchar(int c, mbfl_convert_filter *filter)
 107 {
 108         int c1, s, w, n;
 109 
 110         switch (filter->status) {
 111         case 0:
 112                 if (c >= 0 && c < 0x80) {       /* latin */
 113                         CK((*filter->output_function)(c, filter->data));
 114                 } else if (c > 0xa0 && c < 0xff) {      /* CP932 first char */
 115                         filter->status = 1;
 116                         filter->cache = c;
 117                 } else if (c == 0x8e) { /* kana first char */
 118                         filter->status = 2;
 119                 } else if (c == 0x8f) { /* X 0212 first char */
 120                         filter->status = 3;
 121                 } else {
 122                         w = c & MBFL_WCSGROUP_MASK;
 123                         w |= MBFL_WCSGROUP_THROUGH;
 124                         CK((*filter->output_function)(w, filter->data));
 125                 }
 126                 break;
 127 
 128         case 1: /* got first half */
 129                 filter->status = 0;
 130                 c1 = filter->cache;
 131                 if (c > 0xa0 && c < 0xff) {
 132                         w = 0;
 133                         s = (c1 - 0xa1)*94 + c - 0xa1;
 134                         if (s <= 137) {
 135                                 if (s == 31) {
 136                                         w = 0xff3c;                     /* FULLWIDTH REVERSE SOLIDUS */
 137                                 } else if (s == 32) {
 138                                         w = 0xff5e;                     /* FULLWIDTH TILDE */
 139                                 } else if (s == 33) {
 140                                         w = 0x2225;                     /* PARALLEL TO */
 141                                 } else if (s == 60) {
 142                                         w = 0xff0d;                     /* FULLWIDTH HYPHEN-MINUS */
 143                                 } else if (s == 80) {
 144                                         w = 0xffe0;                     /* FULLWIDTH CENT SIGN */
 145                                 } else if (s == 81) {
 146                                         w = 0xffe1;                     /* FULLWIDTH POUND SIGN */
 147                                 } else if (s == 137) {
 148                                         w = 0xffe2;                     /* FULLWIDTH NOT SIGN */
 149                                 }
 150                         }
 151                         if (w == 0) {
 152                                 if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {              /* vendor ext1 (13ku) */
 153                                         w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
 154                                 } else if (s >= 0 && s < jisx0208_ucs_table_size) {             /* X 0208 */
 155                                         w = jisx0208_ucs_table[s];
 156                                 } else if (s >= (84 * 94)) {            /* user (85ku - 94ku) */
 157                                         w = s - (84 * 94) + 0xe000;
 158                                 }
 159                         }
 160                         if (w <= 0) {
 161                                 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
 162                                 w &= MBFL_WCSPLANE_MASK;
 163                                 w |= MBFL_WCSPLANE_WINCP932;
 164                         }
 165                         CK((*filter->output_function)(w, filter->data));
 166                 } else if ((c >= 0 && c < 0x21) || c == 0x7f) {         /* CTLs */
 167                         CK((*filter->output_function)(c, filter->data));
 168                 } else {
 169                         w = (c1 << 8) | c;
 170                         w &= MBFL_WCSGROUP_MASK;
 171                         w |= MBFL_WCSGROUP_THROUGH;
 172                         CK((*filter->output_function)(w, filter->data));
 173                 }
 174                 break;
 175 
 176         case 2: /* got 0x8e, X0201 kana */
 177                 filter->status = 0;
 178                 if (c > 0xa0 && c < 0xe0) {
 179                         w = 0xfec0 + c;
 180                         CK((*filter->output_function)(w, filter->data));
 181                 } else if ((c >= 0 && c < 0x21) || c == 0x7f) {         /* CTLs */
 182                         CK((*filter->output_function)(c, filter->data));
 183                 } else {
 184                         w = 0x8e00 | c;
 185                         w &= MBFL_WCSGROUP_MASK;
 186                         w |= MBFL_WCSGROUP_THROUGH;
 187                         CK((*filter->output_function)(w, filter->data));
 188                 }
 189                 break;
 190 
 191         case 3: /* got 0x8f,  X 0212 first char */
 192                 if ((c >= 0 && c < 0x21) || c == 0x7f) {                /* CTLs */
 193                         CK((*filter->output_function)(c, filter->data));
 194                         filter->status = 0;
 195                 } else {
 196                         filter->status++;
 197                         filter->cache = c;
 198                 }
 199                 break;
 200         case 4: /* got 0x8f,  X 0212 second char */
 201                 filter->status = 0;
 202                 c1 = filter->cache;
 203                 if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
 204                         s = (c1 - 0xa1)*94 + c - 0xa1;
 205                         if (s >= 0 && s < jisx0212_ucs_table_size) {
 206                                 w = jisx0212_ucs_table[s];
 207                                 if (w == 0x007e) {
 208                                         w = 0xff5e;             /* FULLWIDTH TILDE */
 209                                 }
 210                         } else if (s >= (82*94) && s < (84*94)) {       /* vender ext3 (83ku - 84ku) <-> CP932 (115ku -120ku) */
 211                                 s = (c1<< 8) | c;
 212                                 w = 0;
 213                                 n = 0;
 214                                 while (n < cp932ext3_eucjp_table_size) {
 215                                         if (s == cp932ext3_eucjp_table[n]) {
 216                                                 if (n < (cp932ext3_ucs_table_max - cp932ext3_ucs_table_min)) {
 217                                                         w = cp932ext3_ucs_table[n];
 218                                                 }
 219                                                 break;
 220                                         }
 221                                         n++;
 222                                 }
 223                         } else if (s >= (84*94)) {              /* user (85ku - 94ku) */
 224                                 w = s - (84*94) + (0xe000 + (94*10));
 225                         } else {
 226                                 w = 0;
 227                         }
 228                         if (w == 0x00A6) {
 229                                 w = 0xFFE4;             /* FULLWIDTH BROKEN BAR */
 230                         }
 231                         if (w <= 0) {
 232                                 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
 233                                 w &= MBFL_WCSPLANE_MASK;
 234                                 w |= MBFL_WCSPLANE_JIS0212;
 235                         }
 236                         CK((*filter->output_function)(w, filter->data));
 237                 } else if ((c >= 0 && c < 0x21) || c == 0x7f) {         /* CTLs */
 238                         CK((*filter->output_function)(c, filter->data));
 239                 } else {
 240                         w = (c1 << 8) | c | 0x8f0000;
 241                         w &= MBFL_WCSGROUP_MASK;
 242                         w |= MBFL_WCSGROUP_THROUGH;
 243                         CK((*filter->output_function)(w, filter->data));
 244                 }
 245                 break;
 246 
 247         default:
 248                 filter->status = 0;
 249                 break;
 250         }
 251 
 252         return c;
 253 }
 254 
 255 /*
 256  * wchar => eucJP-win
 257  */
 258 int
 259 mbfl_filt_conv_wchar_eucjpwin(int c, mbfl_convert_filter *filter)
 260 {
 261         int c1, c2, s1;
 262 
 263         s1 = 0;
 264         if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
 265                 s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
 266         } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
 267                 s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
 268         } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
 269                 s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
 270         } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
 271                 s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
 272         } else if (c >= 0xe000 && c < (0xe000 + 10*94)) {       /* user  (X0208  85ku - 94ku) */
 273                 s1 = c - 0xe000;
 274                 c1 = s1/94 + 0x75;
 275                 c2 = s1%94 + 0x21;
 276                 s1 = (c1 << 8) | c2;
 277         } else if (c >= (0xe000 + 10*94) && c < (0xe000 + 20*94)) {     /* user  (X0212  85ku - 94ku) */
 278                 s1 = c - (0xe000 + 10*94);
 279                 c1 = s1/94 + 0xf5;
 280                 c2 = s1%94 + 0xa1;
 281                 s1 = (c1 << 8) | c2;
 282         }
 283         if (s1 == 0xa2f1) {
 284                 s1 = 0x2d62;            /* NUMERO SIGN */
 285         }
 286         if (s1 <= 0) {
 287                 c1 = c & ~MBFL_WCSPLANE_MASK;
 288                 if (c1 == MBFL_WCSPLANE_WINCP932) {
 289                         s1 = c & MBFL_WCSPLANE_MASK;
 290                         if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 120ku */
 291                                 s1 = -1;
 292                         }
 293                 } else if (c1 == MBFL_WCSPLANE_JIS0208) {
 294                         s1 = c & MBFL_WCSPLANE_MASK;
 295                         if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 94ku */
 296                                 s1 = -1;
 297                         }
 298                 } else if (c1 == MBFL_WCSPLANE_JIS0212) {
 299                         s1 = c & MBFL_WCSPLANE_MASK;
 300                         if (s1 >= ((83 + 0x20) << 8)) { /* 83ku - 94ku */
 301                                 s1 = -1;
 302                         } else {
 303                                 s1 |= 0x8080;
 304                         }
 305                 } else if (c == 0xa5) {         /* YEN SIGN */
 306                         s1 = 0x216f;                    /* FULLWIDTH YEN SIGN */
 307                 } else if (c == 0x203e) {       /* OVER LINE */
 308                         s1 = 0x2131;                    /* FULLWIDTH MACRON */
 309                 } else if (c == 0xff3c) {       /* FULLWIDTH REVERSE SOLIDUS */
 310                         s1 = 0x2140;
 311                 } else if (c == 0xff5e) {       /* FULLWIDTH TILDE */
 312                         s1 = 0x2141;
 313                 } else if (c == 0x2225) {       /* PARALLEL TO */
 314                         s1 = 0x2142;
 315                 } else if (c == 0xff0d) {       /* FULLWIDTH HYPHEN-MINUS */
 316                         s1 = 0x215d;
 317                 } else if (c == 0xffe0) {       /* FULLWIDTH CENT SIGN */
 318                         s1 = 0x2171;
 319                 } else if (c == 0xffe1) {       /* FULLWIDTH POUND SIGN */
 320                         s1 = 0x2172;
 321                 } else if (c == 0xffe2) {       /* FULLWIDTH NOT SIGN */
 322                         s1 = 0x224c;
 323                 } else if (c == 0xff5e) {       /* FULLWIDTH TILDE */
 324                         s1 = 0x2141;
 325                 } else {
 326                         s1 = -1;
 327                         c1 = 0;
 328                         c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
 329                         while (c1 < c2) {               /* CP932 vendor ext1 (13ku) */
 330                                 const int oh = cp932ext1_ucs_table_min / 94;
 331 
 332                                 if (c == cp932ext1_ucs_table[c1]) {
 333                                         s1 = ((c1 / 94 + oh + 0x21) << 8) + (c1 % 94 + 0x21);
 334                                         break;
 335                                 }
 336                                 c1++;
 337                         }
 338                         if (s1 < 0) {
 339                                 c1 = 0;
 340                                 c2 = cp932ext3_ucs_table_max - cp932ext3_ucs_table_min;
 341                                 while (c1 < c2) {               /* CP932 vendor ext3 (115ku - 119ku) */
 342                                         if (c == cp932ext3_ucs_table[c1]) {
 343                                                 if (c1 < cp932ext3_eucjp_table_size) {
 344                                                         s1 = cp932ext3_eucjp_table[c1];
 345                                                 }
 346                                                 break;
 347                                         }
 348                                         c1++;
 349                                 }
 350                         }
 351                 }
 352                 if (c == 0) {
 353                         s1 = 0;
 354                 } else if (s1 <= 0) {
 355                         s1 = -1;
 356                 }
 357         }
 358 
 359         if (s1 >= 0) {
 360                 if (s1 < 0x80) {        /* latin */
 361                         CK((*filter->output_function)(s1, filter->data));
 362                 } else if (s1 < 0x100) {        /* kana */
 363                         CK((*filter->output_function)(0x8e, filter->data));
 364                         CK((*filter->output_function)(s1, filter->data));
 365                 } else if (s1 < 0x8080)  {      /* X 0208 */
 366                         CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
 367                         CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
 368                 } else {        /* X 0212 */
 369                         CK((*filter->output_function)(0x8f, filter->data));
 370                         CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
 371                         CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
 372                 }
 373         } else {
 374                 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
 375                         CK(mbfl_filt_conv_illegal_output(c, filter));
 376                 }
 377         }
 378 
 379         return c;
 380 }
 381 
 382 static int mbfl_filt_ident_eucjp_win(int c, mbfl_identify_filter *filter)
 383 {
 384         switch (filter->status) {
 385         case  0:        /* latin */
 386                 if (c >= 0 && c < 0x80) {       /* ok */
 387                         ;
 388                 } else if (c > 0xa0 && c < 0xff) {      /* kanji first char */
 389                         filter->status = 1;
 390                 } else if (c == 0x8e) {                         /* kana first char */
 391                         filter->status = 2;
 392                 } else if (c == 0x8f) {                         /* X 0212 first char */
 393                         filter->status = 3;
 394                 } else {                                                        /* bad */
 395                         filter->flag = 1;
 396                 }
 397                 break;
 398 
 399         case  1:        /* got first half */
 400                 if (c < 0xa1 || c > 0xfe) {             /* bad */
 401                         filter->flag = 1;
 402                 }
 403                 filter->status = 0;
 404                 break;
 405 
 406         case  2:        /* got 0x8e */
 407                 if (c < 0xa1 || c > 0xdf) {             /* bad */
 408                         filter->flag = 1;
 409                 }
 410                 filter->status = 0;
 411                 break;
 412 
 413         case  3:        /* got 0x8f */
 414                 if (c < 0xa1 || c > 0xfe) {             /* bad */
 415                         filter->flag = 1;
 416                 }
 417                 filter->status++;
 418                 break;
 419         case  4:        /* got 0x8f */
 420                 if (c < 0xa1 || c > 0xfe) {             /* bad */
 421                         filter->flag = 1;
 422                 }
 423                 filter->status = 0;
 424                 break;
 425 
 426         default:
 427                 filter->status = 0;
 428                 break;
 429         }
 430 
 431         return c;
 432 }
 433 
 434 

/* [<][>][^][v][top][bottom][index][help] */