Back to index

php5  5.3.10
Defines | Functions | Variables
mbfilter_base64.c File Reference
#include "mbfilter.h"
#include "mbfilter_base64.h"

Go to the source code of this file.

Defines

#define CK(statement)   do { if ((statement) < 0) return (-1); } while (0)

Functions

int mbfl_filt_conv_base64enc (int c, mbfl_convert_filter *filter)
int mbfl_filt_conv_base64enc_flush (mbfl_convert_filter *filter)
int mbfl_filt_conv_base64dec (int c, mbfl_convert_filter *filter)
int mbfl_filt_conv_base64dec_flush (mbfl_convert_filter *filter)

Variables

const mbfl_encoding mbfl_encoding_base64
static const unsigned char mbfl_base64_table []

Define Documentation

#define CK (   statement)    do { if ((statement) < 0) return (-1); } while (0)

Definition at line 66 of file mbfilter_base64.c.


Function Documentation

int mbfl_filt_conv_base64dec ( int  c,
mbfl_convert_filter *  filter 
)

Definition at line 149 of file mbfilter_base64.c.

{
       int n;

       if (c == 0x0d || c == 0x0a || c == 0x20 || c == 0x09 || c == 0x3d) {  /* CR or LF or SPACE or HTAB or '=' */
              return c;
       }

       n = 0;
       if (c >= 0x41 && c <= 0x5a) {             /* A - Z */
              n = c - 65;
       } else if (c >= 0x61 && c <= 0x7a) {      /* a - z */
              n = c - 71;
       } else if (c >= 0x30 && c <= 0x39) {      /* 0 - 9 */
              n = c + 4;
       } else if (c == 0x2b) {                   /* '+' */
              n = 62;
       } else if (c == 0x2f) {                   /* '/' */
              n = 63;
       }
       n &= 0x3f;

       switch (filter->status) {
       case 0:
              filter->status = 1;
              filter->cache = n << 18;
              break;
       case 1:
              filter->status = 2;
              filter->cache |= n << 12;
              break;
       case 2:
              filter->status = 3;
              filter->cache |= n << 6;
              break;
       default:
              filter->status = 0;
              n |= filter->cache;
              CK((*filter->output_function)((n >> 16) & 0xff, filter->data));
              CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
              CK((*filter->output_function)(n & 0xff, filter->data));
              break;
       }

       return c;
}
int mbfl_filt_conv_base64dec_flush ( mbfl_convert_filter *  filter)

Definition at line 196 of file mbfilter_base64.c.

{
       int status, cache;

       status = filter->status;
       cache = filter->cache;
       filter->status = 0;
       filter->cache = 0;
       /* flush fragments */
       if (status >= 2) {
              CK((*filter->output_function)((cache >> 16) & 0xff, filter->data));
              if (status >= 3) {
                     CK((*filter->output_function)((cache >> 8) & 0xff, filter->data));
              }
       }
       return 0;
}
int mbfl_filt_conv_base64enc ( int  c,
mbfl_convert_filter *  filter 
)

Definition at line 84 of file mbfilter_base64.c.

{
       int n;

       n = (filter->status & 0xff);
       if (n == 0) {
              filter->status++;
              filter->cache = (c & 0xff) << 16;
       } else if (n == 1) {
              filter->status++;
              filter->cache |= (c & 0xff) << 8;
       } else {
              filter->status &= ~0xff;
              if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
                     n = (filter->status & 0xff00) >> 8;
                     if (n > 72) {
                            CK((*filter->output_function)(0x0d, filter->data));            /* CR */
                            CK((*filter->output_function)(0x0a, filter->data));            /* LF */
                            filter->status &= ~0xff00;
                     }
                     filter->status += 0x400;
              }
              n = filter->cache | (c & 0xff);
              CK((*filter->output_function)(mbfl_base64_table[(n >> 18) & 0x3f], filter->data));
              CK((*filter->output_function)(mbfl_base64_table[(n >> 12) & 0x3f], filter->data));
              CK((*filter->output_function)(mbfl_base64_table[(n >> 6) & 0x3f], filter->data));
              CK((*filter->output_function)(mbfl_base64_table[n & 0x3f], filter->data));
       }

       return c;
}
int mbfl_filt_conv_base64enc_flush ( mbfl_convert_filter *  filter)

Definition at line 116 of file mbfilter_base64.c.

{
       int status, cache, len;

       status = filter->status & 0xff;
       cache = filter->cache;
       len = (filter->status & 0xff00) >> 8;
       filter->status &= ~0xffff;
       filter->cache = 0;
       /* flush fragments */
       if (status >= 1) {
              if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
                     if (len > 72){
                            CK((*filter->output_function)(0x0d, filter->data));            /* CR */
                            CK((*filter->output_function)(0x0a, filter->data));            /* LF */
                     }
              }
              CK((*filter->output_function)(mbfl_base64_table[(cache >> 18) & 0x3f], filter->data));
              CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
              if (status == 1) {
                     CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
                     CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
              } else {
                     CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
                     CK((*filter->output_function)(0x3d, filter->data));            /* '=' */
              }
       }
       return 0;
}

Variable Documentation

const unsigned char mbfl_base64_table[] [static]
Initial value:
 {
 
   0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
 
   0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
 
   0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
 
   0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
 
   0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
}

Definition at line 71 of file mbfilter_base64.c.

Initial value:
 {
       mbfl_no_encoding_base64,
       "BASE64",
       "BASE64",
       NULL,
       NULL,
       MBFL_ENCTYPE_SBCS
}

Definition at line 38 of file mbfilter_base64.c.