Back to index

php5  5.3.10
mbfilter_utf7.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_utf7.h"
00036 
00037 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter);
00038 
00039 static const unsigned char mbfl_base64_table[] = {
00040  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
00041    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
00042  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
00043    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
00044  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
00045    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
00046  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
00047    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
00048  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
00049    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
00050 };
00051 
00052 static const char *mbfl_encoding_utf7_aliases[] = {"utf7", NULL};
00053 
00054 const mbfl_encoding mbfl_encoding_utf7 = {
00055        mbfl_no_encoding_utf7,
00056        "UTF-7",
00057        "UTF-7",
00058        (const char *(*)[])&mbfl_encoding_utf7_aliases,
00059        NULL,
00060        MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
00061 };
00062 
00063 const struct mbfl_identify_vtbl vtbl_identify_utf7 = {
00064        mbfl_no_encoding_utf7,
00065        mbfl_filt_ident_common_ctor,
00066        mbfl_filt_ident_common_dtor,
00067        mbfl_filt_ident_utf7
00068 };
00069 
00070 const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
00071        mbfl_no_encoding_utf7,
00072        mbfl_no_encoding_wchar,
00073        mbfl_filt_conv_common_ctor,
00074        mbfl_filt_conv_common_dtor,
00075        mbfl_filt_conv_utf7_wchar,
00076        mbfl_filt_conv_common_flush
00077 };
00078 
00079 const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
00080        mbfl_no_encoding_wchar,
00081        mbfl_no_encoding_utf7,
00082        mbfl_filt_conv_common_ctor,
00083        mbfl_filt_conv_common_dtor,
00084        mbfl_filt_conv_wchar_utf7,
00085        mbfl_filt_conv_wchar_utf7_flush
00086 };
00087 
00088 
00089 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00090 
00091 /*
00092  * UTF-7 => wchar
00093  */
00094 int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
00095 {
00096        int s, n;
00097 
00098        n = -1;
00099        if (filter->status != 0) {         /* Modified Base64 */
00100               if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
00101                      n = c - 65;
00102               } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
00103                      n = c - 71;
00104               } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
00105                      n = c + 4;
00106               } else if (c == 0x2b) {                   /* '+' */
00107                      n = 62;
00108               } else if (c == 0x2f) {                   /* '/' */
00109                      n = 63;
00110               }
00111               if (n < 0 || n > 63) {
00112                      if (c == 0x2d) {
00113                             if (filter->status == 1) {         /* "+-" -> "+" */
00114                                    CK((*filter->output_function)(0x2b, filter->data));
00115                             }
00116                      } else if (c >= 0 && c < 0x80) {   /* ASCII exclude '-' */
00117                             CK((*filter->output_function)(c, filter->data));
00118                      } else {             /* illegal character */
00119                             s = c & MBFL_WCSGROUP_MASK;
00120                             s |= MBFL_WCSGROUP_THROUGH;
00121                             CK((*filter->output_function)(s, filter->data));
00122                      }
00123                      filter->cache = 0;
00124                      filter->status = 0;
00125                      return c;
00126               }
00127        }
00128 
00129        switch (filter->status) {
00130        /* directly encoded characters */
00131        case 0:
00132               if (c == 0x2b) {     /* '+'  shift character */
00133                      filter->status = 1;
00134               } else if (c >= 0 && c < 0x80) {   /* ASCII */
00135                      CK((*filter->output_function)(c, filter->data));
00136               } else {             /* illegal character */
00137                      s = c & MBFL_WCSGROUP_MASK;
00138                      s |= MBFL_WCSGROUP_THROUGH;
00139                      CK((*filter->output_function)(s, filter->data));
00140               }
00141               break;
00142 
00143        /* decode Modified Base64 */
00144        case 1:
00145        case 2:
00146               filter->cache |= n << 10;
00147               filter->status = 3;
00148               break;
00149        case 3:
00150               filter->cache |= n << 4;
00151               filter->status = 4;
00152               break;
00153        case 4:
00154               s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
00155               n = (n & 0x3) << 14;
00156               filter->status = 5;
00157               if (s >= 0xd800 && s < 0xdc00) {
00158                      s = (((s & 0x3ff) << 16) + 0x400000) | n;
00159                      filter->cache = s;
00160               } else if (s >= 0xdc00 && s < 0xe000) {
00161                      s &= 0x3ff;
00162                      s |= (filter->cache & 0xfff0000) >> 6;
00163                      filter->cache = n;
00164                      if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
00165                             CK((*filter->output_function)(s, filter->data));
00166                      } else {             /* illegal character */
00167                             s &= MBFL_WCSGROUP_MASK;
00168                             s |= MBFL_WCSGROUP_THROUGH;
00169                             CK((*filter->output_function)(s, filter->data));
00170                      }
00171               } else {
00172                      filter->cache = n;
00173                      CK((*filter->output_function)(s, filter->data));
00174               }
00175               break;
00176 
00177        case 5:
00178               filter->cache |= n << 8;
00179               filter->status = 6;
00180               break;
00181        case 6:
00182               filter->cache |= n << 2;
00183               filter->status = 7;
00184               break;
00185        case 7:
00186               s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
00187               n = (n & 0xf) << 12;
00188               filter->status = 8;
00189               if (s >= 0xd800 && s < 0xdc00) {
00190                      s = (((s & 0x3ff) << 16) + 0x400000) | n;
00191                      filter->cache = s;
00192               } else if (s >= 0xdc00 && s < 0xe000) {
00193                      s &= 0x3ff;
00194                      s |= (filter->cache & 0xfff0000) >> 6;
00195                      filter->cache = n;
00196                      if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
00197                             CK((*filter->output_function)(s, filter->data));
00198                      } else {             /* illegal character */
00199                             s &= MBFL_WCSGROUP_MASK;
00200                             s |= MBFL_WCSGROUP_THROUGH;
00201                             CK((*filter->output_function)(s, filter->data));
00202                      }
00203               } else {
00204                      filter->cache = n;
00205                      CK((*filter->output_function)(s, filter->data));
00206               }
00207               break;
00208 
00209        case 8:
00210               filter->cache |= n << 6;
00211               filter->status = 9;
00212               break;
00213        case 9:
00214               s = n | (filter->cache & 0xffff);
00215               filter->status = 2;
00216               if (s >= 0xd800 && s < 0xdc00) {
00217                      s = (((s & 0x3ff) << 16) + 0x400000);
00218                      filter->cache = s;
00219               } else if (s >= 0xdc00 && s < 0xe000) {
00220                      s &= 0x3ff;
00221                      s |= (filter->cache & 0xfff0000) >> 6;
00222                      filter->cache = 0;
00223                      if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
00224                             CK((*filter->output_function)(s, filter->data));
00225                      } else {             /* illegal character */
00226                             s &= MBFL_WCSGROUP_MASK;
00227                             s |= MBFL_WCSGROUP_THROUGH;
00228                             CK((*filter->output_function)(s, filter->data));
00229                      }
00230               } else {
00231                      filter->cache = 0;
00232                      CK((*filter->output_function)(s, filter->data));
00233               }
00234               break;
00235 
00236        default:
00237               filter->status = 0;
00238               break;
00239        }
00240 
00241        return c;
00242 }
00243 
00244 /*
00245  * wchar => UTF-7
00246  */
00247 int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
00248 {
00249        int s, n;
00250 
00251        n = 0;
00252        if (c >= 0 && c < 0x80) {   /* ASCII */
00253               if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
00254                      n = 1;
00255               } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
00256                      n = 1;
00257               } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
00258                      n = 1;
00259               } else if (c == '\0') {                   /* '\0' */
00260                      n = 1;
00261               } else if (c == 0x2f) {                   /* '/' */
00262                      n = 1;
00263               } else if (c == 0x2d) {                   /* '-' */
00264                      n = 1;
00265               } else if (c == 0x20) {                   /* SPACE */
00266                      n = 2;
00267               } else if (c == 0x09) {                   /* HTAB */
00268                      n = 2;
00269               } else if (c == 0x0d) {                   /* CR */
00270                      n = 2;
00271               } else if (c == 0x0a) {                   /* LF */
00272                      n = 2;
00273               } else if (c == 0x27) {                   /* "'" */
00274                      n = 2;
00275               } else if (c == 0x28) {                   /* '(' */
00276                      n = 2;
00277               } else if (c == 0x29) {                   /* ')' */
00278                      n = 2;
00279               } else if (c == 0x2c) {                   /* ',' */
00280                      n = 2;
00281               } else if (c == 0x2e) {                   /* '.' */
00282                      n = 2;
00283               } else if (c == 0x3a) {                   /* ':' */
00284                      n = 2;
00285               } else if (c == 0x3f) {                   /* '?' */
00286                      n = 2;
00287               }
00288        } else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
00289               ;
00290        } else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
00291               s = ((c >> 10) - 0x40) | 0xd800;
00292               CK((*filter->filter_function)(s, filter));
00293               s = (c & 0x3ff) | 0xdc00;
00294               CK((*filter->filter_function)(s, filter));
00295               return c;
00296        } else {
00297               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00298                      CK(mbfl_filt_conv_illegal_output(c, filter));
00299               }
00300               return c;
00301        }
00302 
00303        switch (filter->status) {
00304        case 0:
00305               if (n != 0) { /* directly encode characters */
00306                      CK((*filter->output_function)(c, filter->data));
00307               } else {      /* Modified Base64 */
00308                      CK((*filter->output_function)(0x2b, filter->data));            /* '+' */
00309                      filter->status++;
00310                      filter->cache = c;
00311               }
00312               break;
00313 
00314        /* encode Modified Base64 */
00315        case 1:
00316               s = filter->cache;
00317               CK((*filter->output_function)(mbfl_base64_table[(s >> 10) & 0x3f], filter->data));
00318               CK((*filter->output_function)(mbfl_base64_table[(s >> 4) & 0x3f], filter->data));
00319               if (n != 0) {
00320                      CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
00321                      if (n == 1) {
00322                             CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00323                      }
00324                      CK((*filter->output_function)(c, filter->data));
00325                      filter->status = 0;
00326               } else {
00327                      filter->status++;
00328                      filter->cache = ((s & 0xf) << 16) | c;
00329               }
00330               break;
00331 
00332        case 2:
00333               s = filter->cache;
00334               CK((*filter->output_function)(mbfl_base64_table[(s >> 14) & 0x3f], filter->data));
00335               CK((*filter->output_function)(mbfl_base64_table[(s >> 8) & 0x3f], filter->data));
00336               CK((*filter->output_function)(mbfl_base64_table[(s >> 2) & 0x3f], filter->data));
00337               if (n != 0) {
00338                      CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
00339                      if (n == 1) {
00340                             CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00341                      }
00342                      CK((*filter->output_function)(c, filter->data));
00343                      filter->status = 0;
00344               } else {
00345                      filter->status++;
00346                      filter->cache = ((s & 0x3) << 16) | c;
00347               }
00348               break;
00349 
00350        case 3:
00351               s = filter->cache;
00352               CK((*filter->output_function)(mbfl_base64_table[(s >> 12) & 0x3f], filter->data));
00353               CK((*filter->output_function)(mbfl_base64_table[(s >> 6) & 0x3f], filter->data));
00354               CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
00355               if (n != 0) {
00356                      if (n == 1) {
00357                             CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00358                      }
00359                      CK((*filter->output_function)(c, filter->data));
00360                      filter->status = 0;
00361               } else {
00362                      filter->status = 1;
00363                      filter->cache = c;
00364               }
00365               break;
00366 
00367        default:
00368               filter->status = 0;
00369               break;
00370        }
00371 
00372        return c;
00373 
00374 }
00375 
00376 int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
00377 {
00378        int status, cache;
00379 
00380        status = filter->status;
00381        cache = filter->cache;
00382        filter->status = 0;
00383        filter->cache = 0;
00384        /* flush fragments */
00385        switch (status) {
00386        case 1:
00387               CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
00388               CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
00389               CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
00390               CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00391               break;
00392 
00393        case 2:
00394               CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
00395               CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
00396               CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
00397               CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
00398               CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00399               break;
00400 
00401        case 3:
00402               CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
00403               CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
00404               CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
00405               CK((*filter->output_function)(0x2d, filter->data));            /* '-' */
00406               break;
00407        }
00408 
00409        if (filter->flush_function != NULL) {
00410               (*filter->flush_function)(filter->data);
00411        }
00412 
00413        return 0;
00414 }
00415 
00416 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter)
00417 {
00418        int n;
00419 
00420        switch (filter->status) {
00421        /* directly encoded characters */
00422        case 0:
00423               if (c == 0x2b) {     /* '+'  shift character */
00424                      filter->status++;
00425               } else if (c == 0x5c || c == 0x7e || c < 0 || c > 0x7f) {      /* illegal character */
00426                      filter->flag = 1;    /* bad */
00427               }
00428               break;
00429 
00430        /* Modified Base64 */
00431        case 1:
00432        case 2:
00433               n = 0;
00434               if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
00435                      n = 1;
00436               } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
00437                      n = 1;
00438               } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
00439                      n = 1;
00440               } else if (c == 0x2b) {                   /* '+' */
00441                      n = 1;
00442               } else if (c == 0x2f) {                   /* '/' */
00443                      n = 1;
00444               }
00445               if (n <= 0) {
00446                      if (filter->status == 1 && c != 0x2d) {
00447                             filter->flag = 1;    /* bad */
00448                      } else if (c < 0 || c > 0x7f) {
00449                             filter->flag = 1;    /* bad */
00450                      }
00451                      filter->status = 0;
00452               } else {
00453                      filter->status = 2;
00454               }
00455               break;
00456 
00457        default:
00458               filter->status = 0;
00459               break;
00460        }
00461 
00462        return c;
00463 }
00464 
00465