Back to index

php5  5.3.10
mbfilter_ucs4.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 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_ucs4.h"
00036 
00037 static const char *mbfl_encoding_ucs4_aliases[] = {"ISO-10646-UCS-4", "UCS4", NULL};
00038 
00039 const mbfl_encoding mbfl_encoding_ucs4 = {
00040        mbfl_no_encoding_ucs4,
00041        "UCS-4",
00042        "UCS-4",
00043        (const char *(*)[])&mbfl_encoding_ucs4_aliases,
00044        NULL,
00045        MBFL_ENCTYPE_WCS4BE
00046 };
00047 
00048 const mbfl_encoding mbfl_encoding_ucs4be = {
00049        mbfl_no_encoding_ucs4be,
00050        "UCS-4BE",
00051        "UCS-4BE",
00052        NULL,
00053        NULL,
00054        MBFL_ENCTYPE_WCS4BE
00055 };
00056 
00057 const mbfl_encoding mbfl_encoding_ucs4le = {
00058        mbfl_no_encoding_ucs4le,
00059        "UCS-4LE",
00060        "UCS-4LE",
00061        NULL,
00062        NULL,
00063        MBFL_ENCTYPE_WCS4LE
00064 };
00065 
00066 const struct mbfl_convert_vtbl vtbl_ucs4_wchar = {
00067        mbfl_no_encoding_ucs4,
00068        mbfl_no_encoding_wchar,
00069        mbfl_filt_conv_common_ctor,
00070        mbfl_filt_conv_common_dtor,
00071        mbfl_filt_conv_ucs4_wchar,
00072        mbfl_filt_conv_common_flush
00073 };
00074 
00075 const struct mbfl_convert_vtbl vtbl_wchar_ucs4 = {
00076        mbfl_no_encoding_wchar,
00077        mbfl_no_encoding_ucs4,
00078        mbfl_filt_conv_common_ctor,
00079        mbfl_filt_conv_common_dtor,
00080        mbfl_filt_conv_wchar_ucs4be,
00081        mbfl_filt_conv_common_flush
00082 };
00083 
00084 const struct mbfl_convert_vtbl vtbl_ucs4be_wchar = {
00085        mbfl_no_encoding_ucs4be,
00086        mbfl_no_encoding_wchar,
00087        mbfl_filt_conv_common_ctor,
00088        mbfl_filt_conv_common_dtor,
00089        mbfl_filt_conv_ucs4be_wchar,
00090        mbfl_filt_conv_common_flush
00091 };
00092 
00093 const struct mbfl_convert_vtbl vtbl_wchar_ucs4be = {
00094        mbfl_no_encoding_wchar,
00095        mbfl_no_encoding_ucs4be,
00096        mbfl_filt_conv_common_ctor,
00097        mbfl_filt_conv_common_dtor,
00098        mbfl_filt_conv_wchar_ucs4be,
00099        mbfl_filt_conv_common_flush
00100 };
00101 
00102 const struct mbfl_convert_vtbl vtbl_ucs4le_wchar = {
00103        mbfl_no_encoding_ucs4le,
00104        mbfl_no_encoding_wchar,
00105        mbfl_filt_conv_common_ctor,
00106        mbfl_filt_conv_common_dtor,
00107        mbfl_filt_conv_ucs4le_wchar,
00108        mbfl_filt_conv_common_flush
00109 };
00110 
00111 const struct mbfl_convert_vtbl vtbl_wchar_ucs4le = {
00112        mbfl_no_encoding_wchar,
00113        mbfl_no_encoding_ucs4le,
00114        mbfl_filt_conv_common_ctor,
00115        mbfl_filt_conv_common_dtor,
00116        mbfl_filt_conv_wchar_ucs4le,
00117        mbfl_filt_conv_common_flush
00118 };
00119 
00120 
00121 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00122 
00123 /*
00124  * UCS-4 => wchar
00125  */
00126 int mbfl_filt_conv_ucs4_wchar(int c, mbfl_convert_filter *filter)
00127 {
00128        int n, endian;
00129 
00130        endian = filter->status & 0xff00;
00131        switch (filter->status & 0xff) {
00132        case 0:
00133               if (endian) {
00134                      n = c & 0xff;
00135               } else {
00136                      n = (c & 0xff) << 24;
00137               }
00138               filter->cache = n;
00139               filter->status++;
00140               break;
00141        case 1:
00142               if (endian) {
00143                      n = (c & 0xff) << 8;
00144               } else {
00145                      n = (c & 0xff) << 16;
00146               }
00147               filter->cache |= n;
00148               filter->status++;
00149               break;
00150        case 2:
00151               if (endian) {
00152                      n = (c & 0xff) << 16;
00153               } else {
00154                      n = (c & 0xff) << 8;
00155               }
00156               filter->cache |= n;
00157               filter->status++;
00158               break;
00159        default:
00160               if (endian) {
00161                      n = (c & 0xff) << 24;
00162               } else {
00163                      n = c & 0xff;
00164               }
00165               n |= filter->cache;
00166               if ((n & 0xffff) == 0 && ((n >> 16) & 0xffff) == 0xfffe) {
00167                      if (endian) {
00168                             filter->status = 0;         /* big-endian */
00169                      } else {
00170                             filter->status = 0x100;            /* little-endian */
00171                      }
00172                      CK((*filter->output_function)(0xfeff, filter->data));
00173               } else {
00174                      filter->status &= ~0xff;
00175                      CK((*filter->output_function)(n, filter->data));
00176               }
00177               break;
00178        }
00179 
00180        return c;
00181 }
00182 
00183 /*
00184  * UCS-4BE => wchar
00185  */
00186 int mbfl_filt_conv_ucs4be_wchar(int c, mbfl_convert_filter *filter)
00187 {
00188        int n;
00189 
00190        if (filter->status == 0) {
00191               filter->status = 1;
00192               n = (c & 0xff) << 24;
00193               filter->cache = n;
00194        } else if (filter->status == 1) {
00195               filter->status = 2;
00196               n = (c & 0xff) << 16;
00197               filter->cache |= n;
00198        } else if (filter->status == 2) {
00199               filter->status = 3;
00200               n = (c & 0xff) << 8;
00201               filter->cache |= n;
00202        } else {
00203               filter->status = 0;
00204               n = (c & 0xff) | filter->cache;
00205               CK((*filter->output_function)(n, filter->data));
00206        }
00207        return c;
00208 }
00209 
00210 /*
00211  * wchar => UCS-4BE
00212  */
00213 int mbfl_filt_conv_wchar_ucs4be(int c, mbfl_convert_filter *filter)
00214 {
00215        if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) {
00216               CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
00217               CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
00218               CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00219               CK((*filter->output_function)(c & 0xff, filter->data));
00220        } else {
00221               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00222                      CK(mbfl_filt_conv_illegal_output(c, filter));
00223               }
00224        }
00225 
00226        return c;
00227 }
00228 
00229 /*
00230  * UCS-4LE => wchar
00231  */
00232 int mbfl_filt_conv_ucs4le_wchar(int c, mbfl_convert_filter *filter)
00233 {
00234        int n;
00235 
00236        if (filter->status == 0) {
00237               filter->status = 1;
00238               n = (c & 0xff);
00239               filter->cache = n;
00240        } else if (filter->status == 1) {
00241               filter->status = 2;
00242               n = (c & 0xff) << 8;
00243               filter->cache |= n;
00244        } else if (filter->status == 2) {
00245               filter->status = 3;
00246               n = (c & 0xff) << 16;
00247               filter->cache |= n;
00248        } else {
00249               filter->status = 0;
00250               n = ((c & 0xff) << 24) | filter->cache;
00251               CK((*filter->output_function)(n, filter->data));
00252        }
00253        return c;
00254 }
00255 
00256 /*
00257  * wchar => UCS-4LE
00258  */
00259 int mbfl_filt_conv_wchar_ucs4le(int c, mbfl_convert_filter *filter)
00260 {
00261        if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) {
00262               CK((*filter->output_function)(c & 0xff, filter->data));
00263               CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00264               CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
00265               CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
00266        } else {
00267               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00268                      CK(mbfl_filt_conv_illegal_output(c, filter));
00269               }
00270        }
00271 
00272        return c;
00273 }
00274 
00275