Back to index

php5  5.3.10
mbfilter_ucs2.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_ucs2.h"
00036 
00037 static const char *mbfl_encoding_ucs2_aliases[] = {"ISO-10646-UCS-2", "UCS2" , "UNICODE", NULL};
00038 
00039 const mbfl_encoding mbfl_encoding_ucs2 = {
00040        mbfl_no_encoding_ucs2,
00041        "UCS-2",
00042        "UCS-2",
00043        (const char *(*)[])&mbfl_encoding_ucs2_aliases,
00044        NULL,
00045        MBFL_ENCTYPE_WCS2BE
00046 };
00047 
00048 const mbfl_encoding mbfl_encoding_ucs2be = {
00049        mbfl_no_encoding_ucs2be,
00050        "UCS-2BE",
00051        "UCS-2BE",
00052        NULL,
00053        NULL,
00054        MBFL_ENCTYPE_WCS2BE
00055 };
00056 
00057 const mbfl_encoding mbfl_encoding_ucs2le = {
00058        mbfl_no_encoding_ucs2le,
00059        "UCS-2LE",
00060        "UCS-2LE",
00061        NULL,
00062        NULL,
00063        MBFL_ENCTYPE_WCS2LE
00064 };
00065 
00066 const struct mbfl_convert_vtbl vtbl_ucs2_wchar = {
00067        mbfl_no_encoding_ucs2,
00068        mbfl_no_encoding_wchar,
00069        mbfl_filt_conv_common_ctor,
00070        mbfl_filt_conv_common_dtor,
00071        mbfl_filt_conv_ucs2_wchar,
00072        mbfl_filt_conv_common_flush
00073 };
00074 
00075 const struct mbfl_convert_vtbl vtbl_wchar_ucs2 = {
00076        mbfl_no_encoding_wchar,
00077        mbfl_no_encoding_ucs2,
00078        mbfl_filt_conv_common_ctor,
00079        mbfl_filt_conv_common_dtor,
00080        mbfl_filt_conv_wchar_ucs2be,
00081        mbfl_filt_conv_common_flush
00082 };
00083 
00084 const struct mbfl_convert_vtbl vtbl_ucs2be_wchar = {
00085        mbfl_no_encoding_ucs2be,
00086        mbfl_no_encoding_wchar,
00087        mbfl_filt_conv_common_ctor,
00088        mbfl_filt_conv_common_dtor,
00089        mbfl_filt_conv_ucs2be_wchar,
00090        mbfl_filt_conv_common_flush
00091 };
00092 
00093 const struct mbfl_convert_vtbl vtbl_wchar_ucs2be = {
00094        mbfl_no_encoding_wchar,
00095        mbfl_no_encoding_ucs2be,
00096        mbfl_filt_conv_common_ctor,
00097        mbfl_filt_conv_common_dtor,
00098        mbfl_filt_conv_wchar_ucs2be,
00099        mbfl_filt_conv_common_flush
00100 };
00101 
00102 const struct mbfl_convert_vtbl vtbl_ucs2le_wchar = {
00103        mbfl_no_encoding_ucs2le,
00104        mbfl_no_encoding_wchar,
00105        mbfl_filt_conv_common_ctor,
00106        mbfl_filt_conv_common_dtor,
00107        mbfl_filt_conv_ucs2le_wchar,
00108        mbfl_filt_conv_common_flush
00109 };
00110 
00111 const struct mbfl_convert_vtbl vtbl_wchar_ucs2le = {
00112        mbfl_no_encoding_wchar,
00113        mbfl_no_encoding_ucs2le,
00114        mbfl_filt_conv_common_ctor,
00115        mbfl_filt_conv_common_dtor,
00116        mbfl_filt_conv_wchar_ucs2le,
00117        mbfl_filt_conv_common_flush
00118 };
00119 
00120 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00121 
00122 /*
00123  * UCS-2 => wchar
00124  */
00125 int mbfl_filt_conv_ucs2_wchar(int c, mbfl_convert_filter *filter)
00126 {
00127        int n, endian;
00128 
00129        endian = filter->status & 0xff00;
00130        switch (filter->status & 0xff) {
00131        case 0:
00132               if (endian) {
00133                      n = c & 0xff;
00134               } else {
00135                      n = (c & 0xff) << 8;
00136               }
00137               filter->cache = n;
00138               filter->status++;
00139               break;
00140        default:
00141               if (endian) {
00142                      n = (c & 0xff) << 8;
00143               } else {
00144                      n = c & 0xff;
00145               }
00146               n |= filter->cache;
00147               if (n == 0xfffe) {
00148                      if (endian) {
00149                             filter->status = 0;         /* big-endian */
00150                      } else {
00151                             filter->status = 0x100;            /* little-endian */
00152                      }
00153                      CK((*filter->output_function)(0xfeff, filter->data));
00154               } else {
00155                      filter->status &= ~0xff;
00156                      CK((*filter->output_function)(n, filter->data));
00157               }
00158               break;
00159        }
00160 
00161        return c;
00162 }
00163 
00164 /*
00165  * UCS-2BE => wchar
00166  */
00167 int mbfl_filt_conv_ucs2be_wchar(int c, mbfl_convert_filter *filter)
00168 {
00169        int n;
00170 
00171        if (filter->status == 0) {
00172               filter->status = 1;
00173               n = (c & 0xff) << 8;
00174               filter->cache = n;
00175        } else {
00176               filter->status = 0;
00177               n = (c & 0xff) | filter->cache;
00178               CK((*filter->output_function)(n, filter->data));
00179        }
00180        return c;
00181 }
00182 
00183 /*
00184  * wchar => UCS-2BE
00185  */
00186 int mbfl_filt_conv_wchar_ucs2be(int c, mbfl_convert_filter *filter)
00187 {
00188        if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
00189               CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00190               CK((*filter->output_function)(c & 0xff, filter->data));
00191        } else {
00192               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00193                      CK(mbfl_filt_conv_illegal_output(c, filter));
00194               }
00195        }
00196 
00197        return c;
00198 }
00199 
00200 /*
00201  * UCS-2LE => wchar
00202  */
00203 int mbfl_filt_conv_ucs2le_wchar(int c, mbfl_convert_filter *filter)
00204 {
00205        int n;
00206 
00207        if (filter->status == 0) {
00208               filter->status = 1;
00209               n = c & 0xff;
00210               filter->cache = n;
00211        } else {
00212               filter->status = 0;
00213               n = ((c & 0xff) << 8) | filter->cache;
00214               CK((*filter->output_function)(n, filter->data));
00215        }
00216        return c;
00217 }
00218 
00219 
00220 /*
00221  * wchar => UCS-2LE
00222  */
00223 int mbfl_filt_conv_wchar_ucs2le(int c, mbfl_convert_filter *filter)
00224 {
00225        if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
00226               CK((*filter->output_function)(c & 0xff, filter->data));
00227               CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00228        } else {
00229               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00230                      CK(mbfl_filt_conv_illegal_output(c, filter));
00231               }
00232        }
00233 
00234        return c;
00235 }
00236 
00237 
00238