Back to index

php5  5.3.10
mbfilter_byte4.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. 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 #include "mbfilter.h"
00036 #include "mbfilter_byte4.h"
00037 
00038 const mbfl_encoding mbfl_encoding_byte4be = {
00039        mbfl_no_encoding_byte4be,
00040        "byte4be",
00041        NULL,
00042        NULL,
00043        NULL,
00044        MBFL_ENCTYPE_SBCS
00045 };
00046 
00047 const mbfl_encoding mbfl_encoding_byte4le = {
00048        mbfl_no_encoding_byte4le,
00049        "byte4le",
00050        NULL,
00051        NULL,
00052        NULL,
00053        MBFL_ENCTYPE_SBCS
00054 };
00055 
00056 const struct mbfl_convert_vtbl vtbl_byte4be_wchar = {
00057        mbfl_no_encoding_byte4be,
00058        mbfl_no_encoding_wchar,
00059        mbfl_filt_conv_common_ctor,
00060        mbfl_filt_conv_common_dtor,
00061        mbfl_filt_conv_byte4be_wchar,
00062        mbfl_filt_conv_common_flush
00063 };
00064 
00065 const struct mbfl_convert_vtbl vtbl_wchar_byte4be = {
00066        mbfl_no_encoding_wchar,
00067        mbfl_no_encoding_byte4be,
00068        mbfl_filt_conv_common_ctor,
00069        mbfl_filt_conv_common_dtor,
00070        mbfl_filt_conv_wchar_byte4be,
00071        mbfl_filt_conv_common_flush };
00072 
00073 const struct mbfl_convert_vtbl vtbl_byte4le_wchar = {
00074        mbfl_no_encoding_byte4le,
00075        mbfl_no_encoding_wchar,
00076        mbfl_filt_conv_common_ctor,
00077        mbfl_filt_conv_common_dtor,
00078        mbfl_filt_conv_byte4le_wchar,
00079        mbfl_filt_conv_common_flush
00080 };
00081 
00082 const struct mbfl_convert_vtbl vtbl_wchar_byte4le = {
00083        mbfl_no_encoding_wchar,
00084        mbfl_no_encoding_byte4le,
00085        mbfl_filt_conv_common_ctor,
00086        mbfl_filt_conv_common_dtor,
00087        mbfl_filt_conv_wchar_byte4le,
00088        mbfl_filt_conv_common_flush
00089 };
00090 
00091 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00092 
00093 int mbfl_filt_conv_byte4be_wchar(int c, mbfl_convert_filter *filter)
00094 {
00095        int n;
00096 
00097        if (filter->status == 0) {
00098               filter->status = 1;
00099               n = (c & 0xff) << 24;
00100               filter->cache = n;
00101        } else if (filter->status == 1) {
00102               filter->status = 2;
00103               n = (c & 0xff) << 16;
00104               filter->cache |= n;
00105        } else if (filter->status == 2) {
00106               filter->status = 3;
00107               n = (c & 0xff) << 8;
00108               filter->cache |= n;
00109        } else {
00110               filter->status = 0;
00111               n = (c & 0xff) | filter->cache;
00112               CK((*filter->output_function)(n, filter->data));
00113        }
00114        return c;
00115 }
00116 
00117 int mbfl_filt_conv_wchar_byte4be(int c, mbfl_convert_filter *filter)
00118 {
00119        CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
00120        CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
00121        CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00122        CK((*filter->output_function)(c & 0xff, filter->data));
00123        return c;
00124 }
00125 
00126 int mbfl_filt_conv_byte4le_wchar(int c, mbfl_convert_filter *filter)
00127 {
00128        int n;
00129 
00130        if (filter->status == 0) {
00131               filter->status = 1;
00132               n = (c & 0xff);
00133               filter->cache = n;
00134        } else if (filter->status == 1) {
00135               filter->status = 2;
00136               n = (c & 0xff) << 8;
00137               filter->cache |= n;
00138        } else if (filter->status == 2) {
00139               filter->status = 3;
00140               n = (c & 0xff) << 16;
00141               filter->cache |= n;
00142        } else {
00143               filter->status = 0;
00144               n = ((c & 0xff) << 24) | filter->cache;
00145               CK((*filter->output_function)(n, filter->data));
00146        }
00147        return c;
00148 }
00149 
00150 int mbfl_filt_conv_wchar_byte4le(int c, mbfl_convert_filter *filter)
00151 {
00152        CK((*filter->output_function)(c & 0xff, filter->data));
00153        CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
00154        CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
00155        CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
00156        return c;
00157 }
00158 
00159