Back to index

php5  5.3.10
mbfilter_hz.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_cn.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_hz.h"
00036 
00037 #include "unicode_table_cp936.h"
00038 
00039 static int mbfl_filt_ident_hz(int c, mbfl_identify_filter *filter);
00040 
00041 const mbfl_encoding mbfl_encoding_hz = {
00042        mbfl_no_encoding_hz,
00043        "HZ",
00044        "HZ-GB-2312",
00045        NULL,
00046        NULL,
00047        MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
00048 };
00049 
00050 const struct mbfl_identify_vtbl vtbl_identify_hz = {
00051        mbfl_no_encoding_hz,
00052        mbfl_filt_ident_common_ctor,
00053        mbfl_filt_ident_common_dtor,
00054        mbfl_filt_ident_hz
00055 };
00056 
00057 const struct mbfl_convert_vtbl vtbl_hz_wchar = {
00058        mbfl_no_encoding_hz,
00059        mbfl_no_encoding_wchar,
00060        mbfl_filt_conv_common_ctor,
00061        mbfl_filt_conv_common_dtor,
00062        mbfl_filt_conv_hz_wchar,
00063        mbfl_filt_conv_common_flush
00064 };
00065 
00066 const struct mbfl_convert_vtbl vtbl_wchar_hz = {
00067        mbfl_no_encoding_wchar,
00068        mbfl_no_encoding_hz,
00069        mbfl_filt_conv_common_ctor,
00070        mbfl_filt_conv_common_dtor,
00071        mbfl_filt_conv_wchar_hz,
00072        mbfl_filt_conv_any_hz_flush
00073 };
00074 
00075 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00076 
00077 /*
00078  * HZ => wchar
00079  */
00080 int
00081 mbfl_filt_conv_hz_wchar(int c, mbfl_convert_filter *filter)
00082 {
00083        int c1, s, w;
00084 
00085        switch (filter->status & 0xf) {
00086 /*     case 0x00:     ASCII */
00087 /*     case 0x10:     GB2312 */
00088        case 0:
00089               if (c == 0x7e) {
00090                      filter->status += 2;
00091               } else if (filter->status == 0x10 && c > 0x20 && c < 0x7f) {   /* DBCS first char */
00092                      filter->cache = c;
00093                      filter->status += 1;
00094               } else if (c >= 0 && c < 0x80) {          /* latin, CTLs */
00095                      CK((*filter->output_function)(c, filter->data));
00096               } else {
00097                      w = c & MBFL_WCSGROUP_MASK;
00098                      w |= MBFL_WCSGROUP_THROUGH;
00099                      CK((*filter->output_function)(w, filter->data));
00100               }
00101               break;
00102 
00103 /*     case 0x11:     GB2312 second char */
00104        case 1:
00105               filter->status &= ~0xf;
00106               c1 = filter->cache;
00107               if (c1 > 0x20 && c1 < 0x7f && c > 0x20 && c < 0x7f) {
00108                      s = (c1 - 1)*192 + c + 0x40; /* GB2312 */
00109                      if (s >= 0 && s < cp936_ucs_table_size) {
00110                             w = cp936_ucs_table[s];
00111                      } else {
00112                             w = 0;
00113                      }
00114                      if (w <= 0) {
00115                             w = (c1 << 8) | c;
00116                             w &= MBFL_WCSPLANE_MASK;
00117                             w |= MBFL_WCSPLANE_GB2312;
00118                      }
00119                      CK((*filter->output_function)(w, filter->data));
00120               } else if ((c >= 0 && c < 0x21) || c == 0x7f) {         /* CTLs */
00121                      CK((*filter->output_function)(c, filter->data));
00122               } else {
00123                      w = (c1 << 8) | c;
00124                      w &= MBFL_WCSGROUP_MASK;
00125                      w |= MBFL_WCSGROUP_THROUGH;
00126                      CK((*filter->output_function)(w, filter->data));
00127               }
00128               break;
00129 
00130        /* '~' */
00131        case 2:
00132               if (c == 0x7d) {            /* '}' */
00133                      filter->status = 0x0;
00134               } else if (c == 0x7b) {            /* '{' */
00135                      filter->status = 0x10;
00136               } else if (c == 0x7e) { /* '~' */
00137                      filter->status = 0x0;
00138                      CK((*filter->output_function)(0x007e, filter->data));
00139               }
00140               break;
00141 
00142        default:
00143               filter->status = 0;
00144               break;
00145        }
00146 
00147        return c;
00148 }
00149 
00150 /*
00151  * wchar => HZ
00152  */
00153 int
00154 mbfl_filt_conv_wchar_hz(int c, mbfl_convert_filter *filter)
00155 {
00156        int s;
00157 
00158        s = 0;
00159        if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
00160               s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
00161        } else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
00162               s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
00163        } else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
00164               s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
00165        } else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
00166               s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
00167        } else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
00168               s = ucs_hff_cp936_table[c - ucs_hff_cp936_table_min];
00169        }
00170        if (s & 0x8000) {
00171               s -= 0x8080;
00172        }
00173 
00174        if (s <= 0) {
00175               if (c == 0) {
00176                      s = 0;
00177               } else if (s <= 0) {
00178                      s = -1;
00179               }
00180        } else if ((s >= 0x80 && s < 0x2121) || (s > 0x8080)) {
00181               s = -1;
00182        }
00183        if (s >= 0) {
00184               if (s < 0x80) { /* ASCII */
00185                      if ((filter->status & 0xff00) != 0) {
00186                             CK((*filter->output_function)(0x7e, filter->data));            /* '~' */
00187                             CK((*filter->output_function)(0x7d, filter->data));            /* '}' */
00188                      }
00189                      filter->status = 0;
00190                      if (s == 0x7e){
00191                             CK((*filter->output_function)(0x7e, filter->data));
00192                      }
00193                      CK((*filter->output_function)(s, filter->data));
00194               } else { /* GB 2312-80 */
00195                      if ((filter->status & 0xff00) != 0x200) {
00196                             CK((*filter->output_function)(0x7e, filter->data));            /* '~' */
00197                             CK((*filter->output_function)(0x7b, filter->data));            /* '{' */
00198                      }
00199                      filter->status = 0x200;
00200                      CK((*filter->output_function)((s >> 8) & 0x7f, filter->data));
00201                      CK((*filter->output_function)(s & 0x7f, filter->data));
00202               }
00203        } else {
00204               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00205                      CK(mbfl_filt_conv_illegal_output(c, filter));
00206               }
00207        }
00208 
00209        return c;
00210 }
00211 
00212 int
00213 mbfl_filt_conv_any_hz_flush(mbfl_convert_filter *filter)
00214 {
00215        /* back to latin */
00216        if ((filter->status & 0xff00) != 0) {
00217               CK((*filter->output_function)(0x7e, filter->data));            /* ~ */
00218               CK((*filter->output_function)(0x7d, filter->data));            /* '{' */
00219        }
00220        filter->status &= 0xff;
00221        return 0;
00222 }
00223 
00224 static int mbfl_filt_ident_hz(int c, mbfl_identify_filter *filter)
00225 {
00226        switch (filter->status & 0xf) {
00227 /*     case 0x00:     ASCII */
00228 /*     case 0x10:     GB2312 */
00229        case 0:
00230               if (c == 0x7e) {
00231                      filter->status += 2;
00232               } else if (filter->status == 0x10 && c > 0x20 && c < 0x7f) {          /* DBCS first char */
00233                      filter->status += 1;
00234               } else if (c >= 0 && c < 0x80) {          /* latin, CTLs */
00235                      ;
00236               } else {
00237                      filter->flag = 1;    /* bad */
00238               }
00239               break;
00240 
00241 /*     case 0x11:     GB2312 second char */
00242        case 1:
00243               filter->status &= ~0xf;
00244               if (c < 0x21 || c > 0x7e) {        /* bad */
00245                      filter->flag = 1;
00246               }
00247               break;
00248 
00249        case 2:
00250               if (c == 0x7d) {            /* '}' */
00251                      filter->status = 0;
00252               } else if (c == 0x7b) {            /* '{' */
00253                      filter->status = 0x10;
00254               } else if (c == 0x7e) {            /* '~' */
00255                      filter->status = 0;
00256               } else {
00257                      filter->flag = 1;    /* bad */
00258                      filter->status &= ~0xf;
00259               }
00260               break;
00261 
00262        default:
00263               filter->status = 0;
00264               break;
00265        }
00266 
00267        return c;
00268 }
00269 
00270 
00271 /*
00272  * Local variables:
00273  * tab-width: 4
00274  * c-basic-offset: 4
00275  * End:
00276  */