Back to index

php5  5.3.10
mbfilter_base64.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_base64.h"
00037 
00038 const mbfl_encoding mbfl_encoding_base64 = {
00039        mbfl_no_encoding_base64,
00040        "BASE64",
00041        "BASE64",
00042        NULL,
00043        NULL,
00044        MBFL_ENCTYPE_SBCS
00045 };
00046 
00047 const struct mbfl_convert_vtbl vtbl_8bit_b64 = {
00048        mbfl_no_encoding_8bit,
00049        mbfl_no_encoding_base64,
00050        mbfl_filt_conv_common_ctor,
00051        mbfl_filt_conv_common_dtor,
00052        mbfl_filt_conv_base64enc,
00053        mbfl_filt_conv_base64enc_flush
00054 };
00055 
00056 const struct mbfl_convert_vtbl vtbl_b64_8bit = {
00057        mbfl_no_encoding_base64,
00058        mbfl_no_encoding_8bit,
00059        mbfl_filt_conv_common_ctor,
00060        mbfl_filt_conv_common_dtor,
00061        mbfl_filt_conv_base64dec,
00062        mbfl_filt_conv_base64dec_flush
00063 };
00064 
00065 
00066 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00067 
00068 /*
00069  * any => BASE64
00070  */
00071 static const unsigned char mbfl_base64_table[] = {
00072  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
00073    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
00074  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
00075    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
00076  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
00077    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
00078  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
00079    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
00080  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
00081    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
00082 };
00083 
00084 int mbfl_filt_conv_base64enc(int c, mbfl_convert_filter *filter)
00085 {
00086        int n;
00087 
00088        n = (filter->status & 0xff);
00089        if (n == 0) {
00090               filter->status++;
00091               filter->cache = (c & 0xff) << 16;
00092        } else if (n == 1) {
00093               filter->status++;
00094               filter->cache |= (c & 0xff) << 8;
00095        } else {
00096               filter->status &= ~0xff;
00097               if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
00098                      n = (filter->status & 0xff00) >> 8;
00099                      if (n > 72) {
00100                             CK((*filter->output_function)(0x0d, filter->data));            /* CR */
00101                             CK((*filter->output_function)(0x0a, filter->data));            /* LF */
00102                             filter->status &= ~0xff00;
00103                      }
00104                      filter->status += 0x400;
00105               }
00106               n = filter->cache | (c & 0xff);
00107               CK((*filter->output_function)(mbfl_base64_table[(n >> 18) & 0x3f], filter->data));
00108               CK((*filter->output_function)(mbfl_base64_table[(n >> 12) & 0x3f], filter->data));
00109               CK((*filter->output_function)(mbfl_base64_table[(n >> 6) & 0x3f], filter->data));
00110               CK((*filter->output_function)(mbfl_base64_table[n & 0x3f], filter->data));
00111        }
00112 
00113        return c;
00114 }
00115 
00116 int mbfl_filt_conv_base64enc_flush(mbfl_convert_filter *filter)
00117 {
00118        int status, cache, len;
00119 
00120        status = filter->status & 0xff;
00121        cache = filter->cache;
00122        len = (filter->status & 0xff00) >> 8;
00123        filter->status &= ~0xffff;
00124        filter->cache = 0;
00125        /* flush fragments */
00126        if (status >= 1) {
00127               if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
00128                      if (len > 72){
00129                             CK((*filter->output_function)(0x0d, filter->data));            /* CR */
00130                             CK((*filter->output_function)(0x0a, filter->data));            /* LF */
00131                      }
00132               }
00133               CK((*filter->output_function)(mbfl_base64_table[(cache >> 18) & 0x3f], filter->data));
00134               CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
00135               if (status == 1) {
00136                      CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
00137                      CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
00138               } else {
00139                      CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
00140                      CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
00141               }
00142        }
00143        return 0;
00144 }
00145 
00146 /*
00147  * BASE64 => any
00148  */
00149 int mbfl_filt_conv_base64dec(int c, mbfl_convert_filter *filter)
00150 {
00151        int n;
00152 
00153        if (c == 0x0d || c == 0x0a || c == 0x20 || c == 0x09 || c == 0x3d) {  /* CR or LF or SPACE or HTAB or '=' */
00154               return c;
00155        }
00156 
00157        n = 0;
00158        if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
00159               n = c - 65;
00160        } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
00161               n = c - 71;
00162        } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
00163               n = c + 4;
00164        } else if (c == 0x2b) {                   /* '+' */
00165               n = 62;
00166        } else if (c == 0x2f) {                   /* '/' */
00167               n = 63;
00168        }
00169        n &= 0x3f;
00170 
00171        switch (filter->status) {
00172        case 0:
00173               filter->status = 1;
00174               filter->cache = n << 18;
00175               break;
00176        case 1:
00177               filter->status = 2;
00178               filter->cache |= n << 12;
00179               break;
00180        case 2:
00181               filter->status = 3;
00182               filter->cache |= n << 6;
00183               break;
00184        default:
00185               filter->status = 0;
00186               n |= filter->cache;
00187               CK((*filter->output_function)((n >> 16) & 0xff, filter->data));
00188               CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
00189               CK((*filter->output_function)(n & 0xff, filter->data));
00190               break;
00191        }
00192 
00193        return c;
00194 }
00195 
00196 int mbfl_filt_conv_base64dec_flush(mbfl_convert_filter *filter)
00197 {
00198        int status, cache;
00199 
00200        status = filter->status;
00201        cache = filter->cache;
00202        filter->status = 0;
00203        filter->cache = 0;
00204        /* flush fragments */
00205        if (status >= 2) {
00206               CK((*filter->output_function)((cache >> 16) & 0xff, filter->data));
00207               if (status >= 3) {
00208                      CK((*filter->output_function)((cache >> 8) & 0xff, filter->data));
00209               }
00210        }
00211        return 0;
00212 }
00213 
00214