Back to index

php5  5.3.10
mbfilter_cp1254.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 part: Haluk AKIN <halukakin@gmail.com>
00022  *
00023  */
00024 /*
00025  * The source code included in this files was separated from mbfilter_ru.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_cp1254.h"
00036 #include "unicode_table_cp1254.h"
00037 
00038 static int mbfl_filt_ident_cp1254(int c, mbfl_identify_filter *filter);
00039 
00040 static const char *mbfl_encoding_cp1254_aliases[] = {"CP1254", "CP-1254", "WINDOWS-1254", NULL};
00041 
00042 const mbfl_encoding mbfl_encoding_cp1254 = {
00043        mbfl_no_encoding_cp1254,
00044        "Windows-1254",
00045        "Windows-1254",
00046        (const char *(*)[])&mbfl_encoding_cp1254_aliases,
00047        NULL,
00048        MBFL_ENCTYPE_SBCS
00049 };
00050 
00051 const struct mbfl_identify_vtbl vtbl_identify_cp1254 = {
00052        mbfl_no_encoding_cp1254,
00053        mbfl_filt_ident_common_ctor,
00054        mbfl_filt_ident_common_dtor,
00055        mbfl_filt_ident_cp1254
00056 };
00057 
00058 const struct mbfl_convert_vtbl vtbl_cp1254_wchar = {
00059        mbfl_no_encoding_cp1254,
00060        mbfl_no_encoding_wchar,
00061        mbfl_filt_conv_common_ctor,
00062        mbfl_filt_conv_common_dtor,
00063        mbfl_filt_conv_cp1254_wchar,
00064        mbfl_filt_conv_common_flush
00065 };
00066 
00067 const struct mbfl_convert_vtbl vtbl_wchar_cp1254 = {
00068        mbfl_no_encoding_wchar,
00069        mbfl_no_encoding_cp1254,
00070        mbfl_filt_conv_common_ctor,
00071        mbfl_filt_conv_common_dtor,
00072        mbfl_filt_conv_wchar_cp1254,
00073        mbfl_filt_conv_common_flush
00074 };
00075 
00076 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00077 
00078 /*
00079  * wchar => cp1254
00080  */
00081 int
00082 mbfl_filt_conv_wchar_cp1254(int c, mbfl_convert_filter *filter)
00083 {
00084        int s, n;
00085 
00086        if (c < 0x80) {
00087               s = c;
00088        } else {
00089               s = -1;
00090               n = cp1254_ucs_table_len-1;
00091               while (n >= 0) {
00092                      if (c == cp1254_ucs_table[n] && c != 0xfffe) {
00093                             s = cp1254_ucs_table_min + n;
00094                             break;
00095                      }
00096                      n--;
00097               }
00098               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_CP1254) {
00099                      s = c & MBFL_WCSPLANE_MASK;
00100               }
00101        }
00102 
00103        if (s >= 0) {
00104               CK((*filter->output_function)(s, filter->data));
00105        } else {
00106               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00107                      CK(mbfl_filt_conv_illegal_output(c, filter));
00108               }
00109        }
00110 
00111        return c;
00112 }
00113 
00114 /*
00115  * cp1254 => wchar
00116  */
00117 int
00118 mbfl_filt_conv_cp1254_wchar(int c, mbfl_convert_filter *filter)
00119 {
00120        int s;
00121 
00122        if (c >= 0 && c < cp1254_ucs_table_min) {
00123               s = c;
00124        } else if (c >= cp1254_ucs_table_min && c < 0x100) {
00125               s = cp1254_ucs_table[c - cp1254_ucs_table_min];
00126               if (s <= 0) {
00127                      s = c;
00128                      s &= MBFL_WCSPLANE_MASK;
00129                      s |= MBFL_WCSPLANE_CP1254;
00130               }
00131        } else {
00132               s = c;
00133               s &= MBFL_WCSGROUP_MASK;
00134               s |= MBFL_WCSGROUP_THROUGH;
00135        }
00136 
00137        CK((*filter->output_function)(s, filter->data));
00138 
00139        return c;
00140 }
00141 
00142 /* We only distinguish the MS extensions to ISO-8859-1.
00143  * Actually, this is pretty much a NO-OP, since the identification
00144  * system doesn't allow us to discriminate between a positive match,
00145  * a possible match and a definite non-match.
00146  * The problem here is that cp1254 looks like SJIS for certain chars.
00147  * */
00148 static int mbfl_filt_ident_cp1254(int c, mbfl_identify_filter *filter)
00149 {
00150        if (c >= 0x80 && c < 0xff)
00151               filter->flag = 0;
00152        else
00153               filter->flag = 1; /* not it */
00154        return c;     
00155 }
00156 
00157