Back to index

php5  5.3.10
mbfilter_iso2022_jp_ms.c
Go to the documentation of this file.
00001 /*
00002  * "streamable kanji code filter and converter"
00003  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
00004  *
00005  * LICENSE NOTICES
00006  *
00007  * This file is part of "streamable kanji code filter and converter",
00008  * which is distributed under the terms of GNU Lesser General Public 
00009  * License (version 2) as published by the Free Software Foundation.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with "streamable kanji code filter and converter";
00018  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
00019  * Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * The author of this file:
00022  *
00023  */
00024 /*
00025  * The source code included in this files was separated from mbfilter_ja.c
00026  * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
00027  * 
00028  */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include "config.h"
00032 #endif
00033 
00034 #include "mbfilter.h"
00035 #include "mbfilter_iso2022_jp_ms.h"
00036 
00037 #include "unicode_table_cp932_ext.h"
00038 #include "unicode_table_jis.h"
00039 #include "cp932_table.h"
00040 
00041 static int mbfl_filt_ident_2022jpms(int c, mbfl_identify_filter *filter);
00042 
00043 static const char *mbfl_encoding_2022jpms_aliases[] = {"ISO2022JPMS", NULL};
00044 
00045 const mbfl_encoding mbfl_encoding_2022jpms = {
00046        mbfl_no_encoding_2022jpms,
00047        "ISO-2022-JP-MS",
00048        "ISO-2022-JP",
00049        (const char *(*)[])&mbfl_encoding_2022jpms_aliases,
00050        NULL,
00051        MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
00052 };
00053 
00054 const struct mbfl_identify_vtbl vtbl_identify_2022jpms = {
00055        mbfl_no_encoding_2022jpms,
00056        mbfl_filt_ident_common_ctor,
00057        mbfl_filt_ident_common_dtor,
00058        mbfl_filt_ident_2022jpms
00059 };
00060 
00061 const struct mbfl_convert_vtbl vtbl_2022jpms_wchar = {
00062        mbfl_no_encoding_2022jpms,
00063        mbfl_no_encoding_wchar,
00064        mbfl_filt_conv_common_ctor,
00065        mbfl_filt_conv_common_dtor,
00066        mbfl_filt_conv_2022jpms_wchar,
00067        mbfl_filt_conv_common_flush
00068 };
00069 
00070 const struct mbfl_convert_vtbl vtbl_wchar_2022jpms = {
00071        mbfl_no_encoding_wchar,
00072        mbfl_no_encoding_2022jpms,
00073        mbfl_filt_conv_common_ctor,
00074        mbfl_filt_conv_common_dtor,
00075        mbfl_filt_conv_wchar_2022jpms,
00076        mbfl_filt_conv_any_2022jpms_flush
00077 };
00078 
00079 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00080 
00081 #define sjistoidx(c1, c2) \
00082         (((c1) > 0x9f) \
00083         ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
00084         : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
00085 #define idxtojis1(c) (((c) / 94) + 0x21)
00086 #define idxtojis2(c) (((c) % 94) + 0x21)
00087 
00088 /*
00089  * ISO-2022-JP-MS => wchar
00090  */
00091 int
00092 mbfl_filt_conv_2022jpms_wchar(int c, mbfl_convert_filter *filter)
00093 {
00094        int c1, s, w;
00095 
00096 retry:
00097        switch (filter->status & 0xf) {
00098 /*     case 0x00:     ASCII */
00099 /*     case 0x10:     X 0201 latin */
00100 /*     case 0x20:     X 0201 kana */
00101 /*     case 0x80:     X 0208 */
00102 /*     case 0xa0:     UDC */
00103        case 0:
00104               if (c == 0x1b) {
00105                      filter->status += 2;
00106               } else if (filter->status == 0x20 && c > 0x20 && c < 0x60) {          /* kana */
00107                      CK((*filter->output_function)(0xff40 + c, filter->data));
00108               } else if ((filter->status == 0x80 || filter->status == 0xa0) && c > 0x20 && c < 0x80) {          /* kanji first char */
00109                      filter->cache = c;
00110                      filter->status += 1;
00111               } else if (c >= 0 && c < 0x80) {          /* latin, CTLs */
00112                      CK((*filter->output_function)(c, filter->data));
00113               } else if (c > 0xa0 && c < 0xe0) { /* GR kana */
00114                      CK((*filter->output_function)(0xfec0 + c, filter->data));
00115               } else {
00116                      w = c & MBFL_WCSGROUP_MASK;
00117                      w |= MBFL_WCSGROUP_THROUGH;
00118                      CK((*filter->output_function)(w, filter->data));
00119               }
00120               break;
00121 
00122 /*     case 0x81:     X 0208 second char */
00123 /*     case 0xa1:     UDC second char */
00124        case 1:
00125               w = 0;
00126               filter->status &= ~0xf;
00127               c1 = filter->cache;
00128               if (c > 0x20 && c < 0x7f) {
00129                      s = (c1 - 0x21)*94 + c - 0x21;
00130                      if (filter->status == 0x80) {
00131                             if (s <= 137) {
00132                                    if (s == 31) {
00133                                           w = 0xff3c;                 /* FULLWIDTH REVERSE SOLIDUS */
00134                                    } else if (s == 32) {
00135                                           w = 0xff5e;                 /* FULLWIDTH TILDE */
00136                                    } else if (s == 33) {
00137                                           w = 0x2225;                 /* PARALLEL TO */
00138                                    } else if (s == 60) {
00139                                           w = 0xff0d;                 /* FULLWIDTH HYPHEN-MINUS */
00140                                    } else if (s == 80) {
00141                                           w = 0xffe0;                 /* FULLWIDTH CENT SIGN */
00142                                    } else if (s == 81) {
00143                                           w = 0xffe1;                 /* FULLWIDTH POUND SIGN */
00144                                    } else if (s == 137) {
00145                                           w = 0xffe2;                 /* FULLWIDTH NOT SIGN */
00146                                    }
00147                             }
00148                             if (w == 0) {
00149                                    if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {           /* vendor ext1 (13ku) */
00150                                           w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
00151                                    } else if (s >= 0 && s < jisx0208_ucs_table_size) {
00152                                           w = jisx0208_ucs_table[s];
00153                                    } else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) {           /* vendor ext2 (89ku - 92ku) */
00154                                           w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
00155                                    } else {
00156                                           w = 0;
00157                                    }
00158                             }
00159                             if (w <= 0) {
00160                                    w = (c1 << 8) | c;
00161                                    w &= MBFL_WCSPLANE_MASK;
00162                                    w |= MBFL_WCSPLANE_JIS0208;
00163                             }
00164                             CK((*filter->output_function)(w, filter->data));
00165                      } else {
00166                             if (c1 > 0x20 && c1 < 0x35) {
00167                                    w = 0xe000 + (c1 - 0x21)*94 + c - 0x21;
00168                             }
00169                             if (w <= 0) {
00170                                    w = (((c1 - 0x21) + 0x7f) << 8) | c;
00171                                    w &= MBFL_WCSPLANE_MASK;
00172                                    w |= MBFL_WCSPLANE_JIS0208;
00173                             }
00174                             CK((*filter->output_function)(w, filter->data));
00175                      }
00176               } else if (c == 0x1b) {
00177                      filter->status += 2;
00178               } else if ((c >= 0 && c < 0x21) || c == 0x7f) {         /* CTLs */
00179                      CK((*filter->output_function)(c, filter->data));
00180               } else {
00181                      w = (c1 << 8) | c;
00182                      w &= MBFL_WCSGROUP_MASK;
00183                      w |= MBFL_WCSGROUP_THROUGH;
00184                      CK((*filter->output_function)(w, filter->data));
00185               }
00186               break;
00187 
00188        /* ESC */
00189 /*     case 0x02:    */
00190 /*     case 0x12:    */
00191 /*     case 0x22:    */
00192 /*     case 0x82:    */
00193 /*     case 0xa2:    */
00194        case 2:
00195               if (c == 0x24) {            /* '$' */
00196                      filter->status++;
00197               } else if (c == 0x28) {            /* '(' */
00198                      filter->status += 3;
00199               } else {
00200                      filter->status &= ~0xf;
00201                      CK((*filter->output_function)(0x1b, filter->data));
00202                      goto retry;
00203               }
00204               break;
00205 
00206        /* ESC $ */
00207 /*     case 0x03:    */
00208 /*     case 0x13:    */
00209 /*     case 0x23:    */
00210 /*     case 0x83:    */
00211 /*     case 0xa3:    */
00212        case 3:
00213               if (c == 0x40 || c == 0x42) {      /* '@' or 'B' */
00214                      filter->status = 0x80;
00215               } else if (c == 0x28) {     /* '(' */
00216                      filter->status++;
00217               } else {
00218                      filter->status &= ~0xf;
00219                      CK((*filter->output_function)(0x1b, filter->data));
00220                      CK((*filter->output_function)(0x24, filter->data));
00221                      goto retry;
00222               }
00223               break;
00224 
00225        /* ESC $ ( */
00226 /*     case 0x04:    */
00227 /*     case 0x14:    */
00228 /*     case 0x24:    */
00229 /*     case 0x84:    */
00230 /*     case 0xa4:    */
00231        case 4:
00232               if (c == 0x40 || c == 0x42) {      /* '@' or 'B' */
00233                      filter->status = 0x80;
00234               } else if (c == 0x3f) {                   /* '?' */
00235                      filter->status = 0xa0;
00236               } else {
00237                      filter->status &= ~0xf;
00238                      CK((*filter->output_function)(0x1b, filter->data));
00239                      CK((*filter->output_function)(0x24, filter->data));
00240                      CK((*filter->output_function)(0x28, filter->data));
00241                      goto retry;
00242               }
00243               break;
00244 
00245        /* ESC ( */
00246 /*     case 0x05:    */
00247 /*     case 0x15:    */
00248 /*     case 0x25:    */
00249 /*     case 0x85:    */
00250 /*     case 0xa5:    */
00251        case 5:
00252               if (c == 0x42) {            /* 'B' */
00253                      filter->status = 0;
00254               } else if (c == 0x4a) {            /* 'J' */
00255                      filter->status = 0;
00256               } else if (c == 0x49) {            /* 'I' */
00257                      filter->status = 0x20;
00258               } else {
00259                      filter->status &= ~0xf;
00260                      CK((*filter->output_function)(0x1b, filter->data));
00261                      CK((*filter->output_function)(0x28, filter->data));
00262                      goto retry;
00263               }
00264               break;
00265 
00266        default:
00267               filter->status = 0;
00268               break;
00269        }
00270 
00271        return c;
00272 }
00273 
00274 static int
00275 cp932ext3_cp932ext2_jis(int c)
00276 {
00277        int idx;
00278 
00279        idx = sjistoidx(0xfa, 0x40) + c;
00280        if (idx >= sjistoidx(0xfa, 0x5c))
00281               idx -=  sjistoidx(0xfa, 0x5c) - sjistoidx(0xed, 0x40);
00282        else if (idx >= sjistoidx(0xfa, 0x55))
00283               idx -=  sjistoidx(0xfa, 0x55) - sjistoidx(0xee, 0xfa);
00284        else if (idx >= sjistoidx(0xfa, 0x40))
00285               idx -=  sjistoidx(0xfa, 0x40) - sjistoidx(0xee, 0xef);
00286        return idxtojis1(idx) << 8 | idxtojis2(idx);
00287 }
00288 
00289 /*
00290  * wchar => ISO-2022-JP-MS
00291  */
00292 int
00293 mbfl_filt_conv_wchar_2022jpms(int c, mbfl_convert_filter *filter)
00294 {
00295        int c1, c2, s1, s2;
00296 
00297        s1 = 0;
00298        s2 = 0;
00299        if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
00300               s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
00301        } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
00302               s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
00303        } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
00304               s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
00305        } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
00306               s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
00307        } else if (c >= 0xe000 && c < (0xe000 + 20*94)) {       /* user  (95ku - 114ku) */
00308               s1 = c - 0xe000;
00309               c1 = s1/94 + 0x7f;
00310               c2 = s1%94 + 0x21;
00311               s1 = (c1 << 8) | c2;
00312        }
00313        if (s1 <= 0) {
00314               c1 = c & ~MBFL_WCSPLANE_MASK;
00315               if (c1 == MBFL_WCSPLANE_WINCP932) {
00316                      s1 = c & MBFL_WCSPLANE_MASK;
00317                      s2 = 1;
00318               } else if (c1 == MBFL_WCSPLANE_JIS0208) {
00319                      s1 = c & MBFL_WCSPLANE_MASK;
00320               } else if (c1 == MBFL_WCSPLANE_JIS0212) {
00321                      s1 = c & MBFL_WCSPLANE_MASK;
00322                      s1 |= 0x8080;
00323               } else if (c == 0xa5) {            /* YEN SIGN */
00324                      s1 = 0x216f;              /* FULLWIDTH YEN SIGN */
00325               } else if (c == 0x203e) {   /* OVER LINE */
00326                      s1 = 0x2131;  /* FULLWIDTH MACRON */
00327               } else if (c == 0xff3c) {   /* FULLWIDTH REVERSE SOLIDUS */
00328                      s1 = 0x2140;
00329               } else if (c == 0xff5e) {   /* FULLWIDTH TILDE */
00330                      s1 = 0x2141;
00331               } else if (c == 0x2225) {   /* PARALLEL TO */
00332                      s1 = 0x2142;
00333               } else if (c == 0xff0d) {   /* FULLWIDTH HYPHEN-MINUS */
00334                      s1 = 0x215d;
00335               } else if (c == 0xffe0) {   /* FULLWIDTH CENT SIGN */
00336                      s1 = 0x2171;
00337               } else if (c == 0xffe1) {   /* FULLWIDTH POUND SIGN */
00338                      s1 = 0x2172;
00339               } else if (c == 0xffe2) {   /* FULLWIDTH NOT SIGN */
00340                      s1 = 0x224c;
00341               }
00342        }
00343        if ((s1 <= 0) || (s1 >= 0xa1a1 && s2 == 0)) { /* not found or X 0212 */
00344               s1 = -1;
00345               c1 = 0;
00346               c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
00347               while (c1 < c2) {           /* CP932 vendor ext1 (13ku) */
00348                      if (c == cp932ext1_ucs_table[c1]) {
00349                             s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
00350                             break;
00351                      }
00352                      c1++;
00353               }
00354               if (s1 <= 0) {
00355                      c1 = 0;
00356                      c2 = cp932ext3_ucs_table_max - cp932ext3_ucs_table_min;
00357                      while (c1 < c2) {           /* CP932 vendor ext3 (115ku - 119ku) */
00358                             if (c == cp932ext3_ucs_table[c1]) {
00359                                    s1 = cp932ext3_cp932ext2_jis(c1);
00360                                    break;
00361                             }
00362                             c1++;
00363                      }
00364               }
00365               if (c == 0) {
00366                      s1 = 0;
00367               } else if (s1 <= 0) {
00368                      s1 = -1;
00369               }
00370        }
00371        if (s1 >= 0) {
00372               if (s1 < 0x80) { /* latin */
00373                      if ((filter->status & 0xff00) != 0) {
00374                             CK((*filter->output_function)(0x1b, filter->data));            /* ESC */
00375                             CK((*filter->output_function)(0x28, filter->data));            /* '(' */
00376                             CK((*filter->output_function)(0x42, filter->data));            /* 'B' */
00377                      }
00378                      CK((*filter->output_function)(s1, filter->data));
00379                      filter->status = 0;
00380               } else if (s1 > 0xa0 && s1 < 0xe0) { /* kana */
00381                      if ((filter->status & 0xff00) != 0x100) {
00382                             CK((*filter->output_function)(0x1b, filter->data));            /* ESC */
00383                             CK((*filter->output_function)(0x28, filter->data));            /* '(' */
00384                             CK((*filter->output_function)(0x49, filter->data));            /* 'I' */
00385                      }
00386                      filter->status = 0x100;
00387                      CK((*filter->output_function)(s1 & 0x7f, filter->data));
00388               } else if (s1 < 0x7e7f) { /* X 0208 */
00389                      if ((filter->status & 0xff00) != 0x200) {
00390                             CK((*filter->output_function)(0x1b, filter->data));            /* ESC */
00391                             CK((*filter->output_function)(0x24, filter->data));            /* '$' */
00392                             CK((*filter->output_function)(0x42, filter->data));            /* 'B' */
00393                      }
00394                      filter->status = 0x200;
00395                      CK((*filter->output_function)((s1 >> 8) & 0xff, filter->data));
00396                      CK((*filter->output_function)(s1 & 0x7f, filter->data));
00397               } else if (s1 < 0x927f) { /* UDC */
00398                      if ((filter->status & 0xff00) != 0x800) {
00399                             CK((*filter->output_function)(0x1b, filter->data));            /* ESC */
00400                             CK((*filter->output_function)(0x24, filter->data));            /* '$' */
00401                             CK((*filter->output_function)(0x28, filter->data));            /* '(' */
00402                             CK((*filter->output_function)(0x3f, filter->data));            /* '?' */
00403                      }
00404                      filter->status = 0x800;
00405                      CK((*filter->output_function)(((s1 >> 8) - 0x5e) & 0x7f, filter->data));
00406                      CK((*filter->output_function)(s1 & 0x7f, filter->data));
00407               }
00408        } else {
00409               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00410                      CK(mbfl_filt_conv_illegal_output(c, filter));
00411               }
00412        }
00413 
00414        return c;
00415 }
00416 
00417 int
00418 mbfl_filt_conv_any_2022jpms_flush(mbfl_convert_filter *filter)
00419 {
00420        /* back to latin */
00421        if ((filter->status & 0xff00) != 0) {
00422               CK((*filter->output_function)(0x1b, filter->data));            /* ESC */
00423               CK((*filter->output_function)(0x28, filter->data));            /* '(' */
00424               CK((*filter->output_function)(0x42, filter->data));            /* 'B' */
00425        }
00426 
00427        filter->status &= 0xff;
00428 
00429        if (filter->flush_function != NULL) {
00430               return (*filter->flush_function)(filter->data);
00431        }
00432 
00433        return 0;
00434 }
00435 
00436 static int mbfl_filt_ident_2022jpms(int c, mbfl_identify_filter *filter)
00437 {
00438 retry:
00439        switch (filter->status & 0xf) {
00440 /*     case 0x00:     ASCII */
00441 /*     case 0x10:     X 0201 latin */
00442 /*     case 0x20:     X 0201 kana */
00443 /*     case 0x80:     X 0208 */
00444 /*     case 0xa0:     X UDC */
00445        case 0:
00446               if (c == 0x1b) {
00447                      filter->status += 2;
00448               } else if ((filter->status == 0x80 || filter->status == 0xa0) && c > 0x20 && c < 0x80) {          /* kanji first char */
00449                      filter->status += 1;
00450               } else if (c >= 0 && c < 0x80) {          /* latin, CTLs */
00451                      ;
00452               } else {
00453                      filter->flag = 1;    /* bad */
00454               }
00455               break;
00456 
00457 /*     case 0x81:     X 0208 second char */
00458 /*     case 0xa1:     UDC second char */
00459        case 1:
00460               filter->status &= ~0xf;
00461               if (c == 0x1b) {
00462                      goto retry;
00463               } else if (c < 0x21 || c > 0x7e) {        /* bad */
00464                      filter->flag = 1;
00465               }
00466               break;
00467 
00468        /* ESC */
00469        case 2:
00470               if (c == 0x24) {            /* '$' */
00471                      filter->status++;
00472               } else if (c == 0x28) {            /* '(' */
00473                      filter->status += 3;
00474               } else {
00475                      filter->flag = 1;    /* bad */
00476                      filter->status &= ~0xf;
00477                      goto retry;
00478               }
00479               break;
00480 
00481        /* ESC $ */
00482        case 3:
00483               if (c == 0x40 || c == 0x42) {             /* '@' or 'B' */
00484                      filter->status = 0x80;
00485               } else if (c == 0x28) {     /* '(' */
00486                      filter->status++;
00487               } else {
00488                      filter->flag = 1;    /* bad */
00489                      filter->status &= ~0xf;
00490                      goto retry;
00491               }
00492               break;
00493 
00494        /* ESC $ ( */
00495        case 4:
00496               if (c == 0x40 || c == 0x42) {             /* '@' or 'B' */
00497                      filter->status = 0x80;
00498               } else if (c == 0x3f) {            /* '?' */
00499                      filter->status = 0xa0;
00500               } else {
00501                      filter->flag = 1;    /* bad */
00502                      filter->status &= ~0xf;
00503                      goto retry;
00504               }
00505               break;
00506 
00507        /* ESC ( */
00508        case 5:
00509               if (c == 0x42) {            /* 'B' */
00510                      filter->status = 0;
00511               } else if (c == 0x4a) {            /* 'J' */
00512                      filter->status = 0;
00513               } else if (c == 0x49) {            /* 'I' */
00514                      filter->status = 0x20;
00515               } else {
00516                      filter->flag = 1;    /* bad */
00517                      filter->status &= ~0xf;
00518                      goto retry;
00519               }
00520               break;
00521 
00522        default:
00523               filter->status = 0;
00524               break;
00525        }
00526 
00527        return c;
00528 }