Back to index

php5  5.3.10
mbfl_ident.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.c
00026  * by Moriyoshi Koizumi <moriyoshi@php.net> on 20 Dec 2002. The file
00027  * mbfilter.c is included in this package .
00028  *
00029  */
00030 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 
00035 #ifdef HAVE_STDDEF_H
00036 #include <stddef.h>
00037 #endif
00038 
00039 #include "mbfl_ident.h"
00040 #include "mbfl_allocators.h"
00041 #include "mbfilter_pass.h"
00042 #include "mbfilter_8bit.h"
00043 #include "mbfilter_wchar.h"
00044 
00045 #include "filters/mbfilter_euc_cn.h"
00046 #include "filters/mbfilter_hz.h"
00047 #include "filters/mbfilter_euc_tw.h"
00048 #include "filters/mbfilter_big5.h"
00049 #include "filters/mbfilter_uhc.h"
00050 #include "filters/mbfilter_euc_kr.h"
00051 #include "filters/mbfilter_iso2022_kr.h"
00052 #include "filters/mbfilter_sjis.h"
00053 #include "filters/mbfilter_sjis_open.h"
00054 #include "filters/mbfilter_jis.h"
00055 #include "filters/mbfilter_iso2022_jp_ms.h"
00056 #include "filters/mbfilter_euc_jp.h"
00057 #include "filters/mbfilter_euc_jp_win.h"
00058 #include "filters/mbfilter_ascii.h"
00059 #include "filters/mbfilter_koi8r.h"
00060 #include "filters/mbfilter_koi8u.h"
00061 #include "filters/mbfilter_cp866.h"
00062 #include "filters/mbfilter_cp932.h"
00063 #include "filters/mbfilter_cp936.h"
00064 #include "filters/mbfilter_cp1251.h"
00065 #include "filters/mbfilter_cp1252.h"
00066 #include "filters/mbfilter_cp1254.h"
00067 #include "filters/mbfilter_cp51932.h"
00068 #include "filters/mbfilter_cp5022x.h"
00069 #include "filters/mbfilter_iso8859_1.h"
00070 #include "filters/mbfilter_iso8859_2.h"
00071 #include "filters/mbfilter_iso8859_3.h"
00072 #include "filters/mbfilter_iso8859_4.h"
00073 #include "filters/mbfilter_iso8859_5.h"
00074 #include "filters/mbfilter_iso8859_6.h"
00075 #include "filters/mbfilter_iso8859_7.h"
00076 #include "filters/mbfilter_iso8859_8.h"
00077 #include "filters/mbfilter_iso8859_9.h"
00078 #include "filters/mbfilter_iso8859_10.h"
00079 #include "filters/mbfilter_iso8859_13.h"
00080 #include "filters/mbfilter_iso8859_14.h"
00081 #include "filters/mbfilter_iso8859_15.h"
00082 #include "filters/mbfilter_base64.h"
00083 #include "filters/mbfilter_qprint.h"
00084 #include "filters/mbfilter_uuencode.h"
00085 #include "filters/mbfilter_7bit.h"
00086 #include "filters/mbfilter_utf7.h"
00087 #include "filters/mbfilter_utf7imap.h"
00088 #include "filters/mbfilter_utf8.h"
00089 #include "filters/mbfilter_utf16.h"
00090 #include "filters/mbfilter_utf32.h"
00091 #include "filters/mbfilter_byte2.h"
00092 #include "filters/mbfilter_byte4.h"
00093 #include "filters/mbfilter_ucs4.h"
00094 #include "filters/mbfilter_ucs2.h"
00095 #include "filters/mbfilter_htmlent.h"
00096 #include "filters/mbfilter_armscii8.h"
00097 #include "filters/mbfilter_cp850.h"
00098 
00099 static const struct mbfl_identify_vtbl vtbl_identify_false = {
00100        mbfl_no_encoding_pass,
00101        mbfl_filt_ident_false_ctor,
00102        mbfl_filt_ident_common_dtor,
00103        mbfl_filt_ident_false };
00104 
00105 
00106 static const struct mbfl_identify_vtbl *mbfl_identify_filter_list[] = {
00107        &vtbl_identify_utf8,
00108        &vtbl_identify_utf7,
00109        &vtbl_identify_ascii,
00110        &vtbl_identify_eucjp,
00111        &vtbl_identify_sjis,
00112        &vtbl_identify_sjis_open,
00113        &vtbl_identify_eucjpwin,
00114        &vtbl_identify_cp932,
00115        &vtbl_identify_jis,
00116        &vtbl_identify_2022jp,
00117        &vtbl_identify_2022jpms,
00118        &vtbl_identify_cp51932,
00119        &vtbl_identify_euccn,
00120        &vtbl_identify_cp936,
00121        &vtbl_identify_hz,
00122        &vtbl_identify_euctw,
00123        &vtbl_identify_big5,
00124        &vtbl_identify_euckr,
00125        &vtbl_identify_uhc,
00126        &vtbl_identify_2022kr,
00127        &vtbl_identify_cp1251,
00128        &vtbl_identify_cp866,
00129        &vtbl_identify_koi8r,
00130        &vtbl_identify_koi8u,
00131        &vtbl_identify_cp1252,
00132        &vtbl_identify_cp1254,
00133        &vtbl_identify_8859_1,
00134        &vtbl_identify_8859_2,
00135        &vtbl_identify_8859_3,
00136        &vtbl_identify_8859_4,
00137        &vtbl_identify_8859_5,
00138        &vtbl_identify_8859_6,
00139        &vtbl_identify_8859_7,
00140        &vtbl_identify_8859_8,
00141        &vtbl_identify_8859_9,
00142        &vtbl_identify_8859_10,
00143        &vtbl_identify_8859_13,
00144        &vtbl_identify_8859_14,
00145        &vtbl_identify_8859_15,
00146        &vtbl_identify_armscii8,
00147        &vtbl_identify_cp850,
00148        &vtbl_identify_jis_ms,
00149        &vtbl_identify_cp50220,
00150        &vtbl_identify_cp50221,
00151        &vtbl_identify_cp50222,
00152        &vtbl_identify_false,
00153        NULL
00154 };
00155 
00156 
00157 
00158 /*
00159  * identify filter
00160  */
00161 const struct mbfl_identify_vtbl * mbfl_identify_filter_get_vtbl(enum mbfl_no_encoding encoding)
00162 {
00163        const struct mbfl_identify_vtbl * vtbl;
00164        int i;
00165 
00166        i = 0;
00167        while ((vtbl = mbfl_identify_filter_list[i++]) != NULL) {
00168               if (vtbl->encoding == encoding) {
00169                      break;
00170               }
00171        }
00172 
00173        return vtbl;
00174 }
00175 
00176 mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding)
00177 {
00178        mbfl_identify_filter *filter;
00179 
00180        /* allocate */
00181        filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
00182        if (filter == NULL) {
00183               return NULL;
00184        }
00185 
00186        if (mbfl_identify_filter_init(filter, encoding)) {
00187               mbfl_free(filter);
00188               return NULL;
00189        }
00190 
00191        return filter;
00192 }
00193 
00194 int mbfl_identify_filter_init(mbfl_identify_filter *filter, enum mbfl_no_encoding encoding)
00195 {
00196        const struct mbfl_identify_vtbl *vtbl;
00197 
00198        /* encoding structure */
00199        filter->encoding = mbfl_no2encoding(encoding);
00200        if (filter->encoding == NULL) {
00201               filter->encoding = &mbfl_encoding_pass;
00202        }
00203 
00204        filter->status = 0;
00205        filter->flag = 0;
00206        filter->score = 0;
00207 
00208        /* setup the function table */
00209        vtbl = mbfl_identify_filter_get_vtbl(filter->encoding->no_encoding);
00210        if (vtbl == NULL) {
00211               vtbl = &vtbl_identify_false;
00212        }
00213        filter->filter_ctor = vtbl->filter_ctor;
00214        filter->filter_dtor = vtbl->filter_dtor;
00215        filter->filter_function = vtbl->filter_function;
00216 
00217        /* constructor */
00218        (*filter->filter_ctor)(filter);
00219 
00220        return 0;
00221 }
00222 
00223 void mbfl_identify_filter_delete(mbfl_identify_filter *filter)
00224 {
00225        if (filter == NULL) {
00226               return;
00227        }
00228 
00229        mbfl_identify_filter_cleanup(filter);
00230        mbfl_free((void*)filter);
00231 }
00232 
00233 void mbfl_identify_filter_cleanup(mbfl_identify_filter *filter)
00234 {
00235        (*filter->filter_dtor)(filter);
00236 }
00237 
00238 void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter)
00239 {
00240        filter->status = 0;
00241        filter->flag = 0;
00242 }
00243 
00244 void mbfl_filt_ident_common_dtor(mbfl_identify_filter *filter)
00245 {
00246        filter->status = 0;
00247 }
00248 
00249 int mbfl_filt_ident_false(int c, mbfl_identify_filter *filter)
00250 {
00251        filter->flag = 1;    /* bad */
00252        return c;
00253 }
00254 
00255 void mbfl_filt_ident_false_ctor(mbfl_identify_filter *filter)
00256 {
00257        filter->status = 0;
00258        filter->flag = 1;
00259 }
00260 
00261 int mbfl_filt_ident_true(int c, mbfl_identify_filter *filter)
00262 {
00263        return c;
00264 }