Back to index

php5  5.3.10
mbfilter_cp1252.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: Wez Furlong <wez@thebrainroom.com>
00022  *
00023  */
00024 /*
00025  * the source code included in this files was separated from mbfilter.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_cp1252.h"
00036 #include "unicode_table_cp1252.h"
00037 
00038 static int mbfl_filt_ident_cp1252(int c, mbfl_identify_filter *filter);
00039 
00040 static const char *mbfl_encoding_cp1252_aliases[] = {"cp1252", NULL};
00041 
00042 const mbfl_encoding mbfl_encoding_cp1252 = {
00043        mbfl_no_encoding_cp1252,
00044        "Windows-1252",
00045        "Windows-1252",
00046        (const char *(*)[])&mbfl_encoding_cp1252_aliases,
00047        NULL,
00048        MBFL_ENCTYPE_SBCS
00049 };
00050 
00051 const struct mbfl_identify_vtbl vtbl_identify_cp1252 = {
00052        mbfl_no_encoding_cp1252,
00053        mbfl_filt_ident_common_ctor,
00054        mbfl_filt_ident_common_dtor,
00055        mbfl_filt_ident_cp1252
00056 };
00057 
00058 const struct mbfl_convert_vtbl vtbl_cp1252_wchar = {
00059        mbfl_no_encoding_cp1252,
00060        mbfl_no_encoding_wchar,
00061        mbfl_filt_conv_common_ctor,
00062        mbfl_filt_conv_common_dtor,
00063        mbfl_filt_conv_cp1252_wchar,
00064        mbfl_filt_conv_common_flush
00065 };
00066 
00067 const struct mbfl_convert_vtbl vtbl_wchar_cp1252 = {
00068        mbfl_no_encoding_wchar,
00069        mbfl_no_encoding_cp1252,
00070        mbfl_filt_conv_common_ctor,
00071        mbfl_filt_conv_common_dtor,
00072        mbfl_filt_conv_wchar_cp1252,
00073        mbfl_filt_conv_common_flush
00074 };
00075 
00076 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00077 
00078 /*
00079  * wchar => cp1252
00080  */
00081 int mbfl_filt_conv_wchar_cp1252(int c, mbfl_convert_filter *filter)
00082 {
00083        int s=-1, n;
00084 
00085        if (c >= 0x100)      {
00086               /* look it up from the cp1252 table */
00087               s = -1;
00088               n = 31;
00089               while (n >= 0) {
00090                      if (c == cp1252_ucs_table[n] && c != 0xfffe) {
00091                             s = 0x80 + n;
00092                             break;
00093                      }
00094                      n--;
00095               }
00096               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_8859_1)
00097               {
00098                      s = c & MBFL_WCSPLANE_MASK;
00099               }
00100        }
00101        else if (c >= 0 && c < 0x100) {
00102               s = c;
00103        }
00104        if (s >= 0) {
00105               CK((*filter->output_function)(s, filter->data));
00106        } else {
00107               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00108                      CK(mbfl_filt_conv_illegal_output(c, filter));
00109               }
00110        }
00111        return c;
00112 }
00113 
00114 /*
00115  * cp1252 => wchar
00116  */
00117 int mbfl_filt_conv_cp1252_wchar(int c, mbfl_convert_filter *filter)
00118 {
00119        int s;
00120 
00121        if (c >= 0x80 && c < 0xa0) {
00122               s = cp1252_ucs_table[c - 0x80];
00123        } else {
00124               s = c;
00125        }
00126 
00127        CK((*filter->output_function)(s, filter->data));
00128 
00129        return c;
00130 }
00131 
00132 /* We only distinguish the MS extensions to ISO-8859-1.
00133  * Actually, this is pretty much a NO-OP, since the identification
00134  * system doesn't allow us to discriminate between a positive match,
00135  * a possible match and a definite non-match.
00136  * The problem here is that cp1252 looks like SJIS for certain chars.
00137  * */
00138 static int mbfl_filt_ident_cp1252(int c, mbfl_identify_filter *filter)
00139 {
00140        if (c >= 0x80 && c < 0xa0)
00141               filter->flag = 0;
00142        else
00143               filter->flag = 1; /* not it */
00144        return c;     
00145 }
00146 
00147