PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - mbstring/libmbfl/filters - mbfilter_base64.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 82
Code covered: 93.9 % Executed lines: 77
Legend: not executed executed

       1                 :  /*
       2                 :  * "streamable kanji code filter and converter"
       3                 :  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
       4                 :  *
       5                 :  * LICENSE NOTICES
       6                 :  *
       7                 :  * This file is part of "streamable kanji code filter and converter",
       8                 :  * which is distributed under the terms of GNU Lesser General Public 
       9                 :  * License (version 2) as published by the Free Software Foundation.
      10                 :  *
      11                 :  * This software is distributed in the hope that it will be useful,
      12                 :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :  * GNU Lesser General Public License for more details.
      15                 :  *
      16                 :  * You should have received a copy of the GNU Lesser General Public
      17                 :  * License along with "streamable kanji code filter and converter";
      18                 :  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
      19                 :  * Suite 330, Boston, MA  02111-1307  USA
      20                 :  *
      21                 :  * The author of this file:
      22                 :  *
      23                 :  */
      24                 : /*
      25                 :  * The source code included in this files was separated from mbfilter.c
      26                 :  * by Moriyoshi Koizumi <moriyoshi@php.net> on 4 Dec 2002. The file
      27                 :  * mbfilter.c is included in this package .
      28                 :  *
      29                 :  */
      30                 : 
      31                 : #ifdef HAVE_CONFIG_H
      32                 : #include "config.h"
      33                 : #endif
      34                 : 
      35                 : #include "mbfilter.h"
      36                 : #include "mbfilter_base64.h"
      37                 : 
      38                 : const mbfl_encoding mbfl_encoding_base64 = {
      39                 :         mbfl_no_encoding_base64,
      40                 :         "BASE64",
      41                 :         "BASE64",
      42                 :         NULL,
      43                 :         NULL,
      44                 :         MBFL_ENCTYPE_SBCS
      45                 : };
      46                 : 
      47                 : const struct mbfl_convert_vtbl vtbl_8bit_b64 = {
      48                 :         mbfl_no_encoding_8bit,
      49                 :         mbfl_no_encoding_base64,
      50                 :         mbfl_filt_conv_common_ctor,
      51                 :         mbfl_filt_conv_common_dtor,
      52                 :         mbfl_filt_conv_base64enc,
      53                 :         mbfl_filt_conv_base64enc_flush
      54                 : };
      55                 : 
      56                 : const struct mbfl_convert_vtbl vtbl_b64_8bit = {
      57                 :         mbfl_no_encoding_base64,
      58                 :         mbfl_no_encoding_8bit,
      59                 :         mbfl_filt_conv_common_ctor,
      60                 :         mbfl_filt_conv_common_dtor,
      61                 :         mbfl_filt_conv_base64dec,
      62                 :         mbfl_filt_conv_base64dec_flush
      63                 : };
      64                 : 
      65                 : 
      66                 : #define CK(statement)   do { if ((statement) < 0) return (-1); } while (0)
      67                 : 
      68                 : /*
      69                 :  * any => BASE64
      70                 :  */
      71                 : static const unsigned char mbfl_base64_table[] = {
      72                 :  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
      73                 :    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
      74                 :  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
      75                 :    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
      76                 :  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
      77                 :    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
      78                 :  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
      79                 :    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
      80                 :  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
      81                 :    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
      82                 : };
      83                 : 
      84                 : int mbfl_filt_conv_base64enc(int c, mbfl_convert_filter *filter)
      85           20548 : {
      86                 :         int n;
      87                 : 
      88           20548 :         n = (filter->status & 0xff);
      89           20548 :         if (n == 0) {
      90            7003 :                 filter->status++;
      91            7003 :                 filter->cache = (c & 0xff) << 16;
      92           13545 :         } else if (n == 1) {
      93            6727 :                 filter->status++;
      94            6727 :                 filter->cache |= (c & 0xff) << 8;
      95                 :         } else {
      96            6818 :                 filter->status &= ~0xff;
      97            6818 :                 if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
      98               8 :                         n = (filter->status & 0xff00) >> 8;
      99               8 :                         if (n > 72) {
     100               0 :                                 CK((*filter->output_function)(0x0d, filter->data));               /* CR */
     101               0 :                                 CK((*filter->output_function)(0x0a, filter->data));               /* LF */
     102               0 :                                 filter->status &= ~0xff00;
     103                 :                         }
     104               8 :                         filter->status += 0x400;
     105                 :                 }
     106            6818 :                 n = filter->cache | (c & 0xff);
     107            6818 :                 CK((*filter->output_function)(mbfl_base64_table[(n >> 18) & 0x3f], filter->data));
     108            6818 :                 CK((*filter->output_function)(mbfl_base64_table[(n >> 12) & 0x3f], filter->data));
     109            6818 :                 CK((*filter->output_function)(mbfl_base64_table[(n >> 6) & 0x3f], filter->data));
     110            6818 :                 CK((*filter->output_function)(mbfl_base64_table[n & 0x3f], filter->data));
     111                 :         }
     112                 : 
     113           20548 :         return c;
     114                 : }
     115                 : 
     116                 : int mbfl_filt_conv_base64enc_flush(mbfl_convert_filter *filter)
     117            6403 : {
     118                 :         int status, cache, len;
     119                 : 
     120            6403 :         status = filter->status & 0xff;
     121            6403 :         cache = filter->cache;
     122            6403 :         len = (filter->status & 0xff00) >> 8;
     123            6403 :         filter->status &= ~0xffff;
     124            6403 :         filter->cache = 0;
     125                 :         /* flush fragments */
     126            6403 :         if (status >= 1) {
     127            4067 :                 if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
     128               8 :                         if (len > 72){
     129               0 :                                 CK((*filter->output_function)(0x0d, filter->data));               /* CR */
     130               0 :                                 CK((*filter->output_function)(0x0a, filter->data));               /* LF */
     131                 :                         }
     132                 :                 }
     133            4067 :                 CK((*filter->output_function)(mbfl_base64_table[(cache >> 18) & 0x3f], filter->data));
     134            4067 :                 CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
     135            4067 :                 if (status == 1) {
     136            2185 :                         CK((*filter->output_function)(0x3d, filter->data));               /* '=' */
     137            2185 :                         CK((*filter->output_function)(0x3d, filter->data));               /* '=' */
     138                 :                 } else {
     139            1882 :                         CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
     140            1882 :                         CK((*filter->output_function)(0x3d, filter->data));               /* '=' */
     141                 :                 }
     142                 :         }
     143            6403 :         return 0;
     144                 : }
     145                 : 
     146                 : /*
     147                 :  * BASE64 => any
     148                 :  */
     149                 : int mbfl_filt_conv_base64dec(int c, mbfl_convert_filter *filter)
     150             284 : {
     151                 :         int n;
     152                 : 
     153             284 :         if (c == 0x0d || c == 0x0a || c == 0x20 || c == 0x09 || c == 0x3d) {    /* CR or LF or SPACE or HTAB or '=' */
     154               6 :                 return c;
     155                 :         }
     156                 : 
     157             278 :         n = 0;
     158             393 :         if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
     159             115 :                 n = c - 65;
     160             273 :         } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
     161             110 :                 n = c - 71;
     162             101 :         } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
     163              48 :                 n = c + 4;
     164               5 :         } else if (c == 0x2b) {                 /* '+' */
     165               3 :                 n = 62;
     166               2 :         } else if (c == 0x2f) {                 /* '/' */
     167               2 :                 n = 63;
     168                 :         }
     169             278 :         n &= 0x3f;
     170                 : 
     171             278 :         switch (filter->status) {
     172                 :         case 0:
     173              71 :                 filter->status = 1;
     174              71 :                 filter->cache = n << 18;
     175              71 :                 break;
     176                 :         case 1:
     177              71 :                 filter->status = 2;
     178              71 :                 filter->cache |= n << 12;
     179              71 :                 break;
     180                 :         case 2:
     181              69 :                 filter->status = 3;
     182              69 :                 filter->cache |= n << 6;
     183              69 :                 break;
     184                 :         default:
     185              67 :                 filter->status = 0;
     186              67 :                 n |= filter->cache;
     187              67 :                 CK((*filter->output_function)((n >> 16) & 0xff, filter->data));
     188              67 :                 CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
     189              67 :                 CK((*filter->output_function)(n & 0xff, filter->data));
     190                 :                 break;
     191                 :         }
     192                 : 
     193             278 :         return c;
     194                 : }
     195                 : 
     196                 : int mbfl_filt_conv_base64dec_flush(mbfl_convert_filter *filter)
     197               6 : {
     198                 :         int status, cache;
     199                 : 
     200               6 :         status = filter->status;
     201               6 :         cache = filter->cache;
     202               6 :         filter->status = 0;
     203               6 :         filter->cache = 0;
     204                 :         /* flush fragments */
     205               6 :         if (status >= 2) {
     206               4 :                 CK((*filter->output_function)((cache >> 16) & 0xff, filter->data));
     207               4 :                 if (status >= 3) {
     208               2 :                         CK((*filter->output_function)((cache >> 8) & 0xff, filter->data));
     209                 :                 }
     210                 :         }
     211               6 :         return 0;
     212                 : }
     213                 : 
     214                 : 

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:11 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.