Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Base64Decoder Class Reference

#include <Base64.h>

Collaboration diagram for Base64Decoder:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Base64Decoder (Connection *conn)
int Decode (int len, const char *data, int *blen, char **buf)
int Done (int *pblen, char **pbuf)
int HasData () const
int Errored () const
const char * ErrorMsg () const
void IllegalEncoding (const char *msg)

Protected Attributes

char error_msg [256]
char base64_group [4]
int base64_group_next
int base64_padding
int base64_after_padding
int errored
Connectionconn

Constructor & Destructor Documentation

Base64Decoder::Base64Decoder Connection conn  ) 
 

Definition at line 34 of file Base64.cc.

References base64_after_padding, base64_group_next, base64_padding, conn, errored, and init_base64_table().

00035         {
00036         init_base64_table();
00037         base64_group_next = 0;
00038         base64_padding = base64_after_padding = 0;
00039         errored = 0;
00040         conn = arg_conn;
00041         }


Member Function Documentation

int Base64Decoder::Decode int  len,
const char *  data,
int *  blen,
char **  buf
 

Definition at line 43 of file Base64.cc.

References base64_after_padding, base64_group, base64_group_next, base64_padding, base64_table, errored, fmt(), IllegalEncoding(), int, internal_error(), len, and uint32.

Referenced by decode_base64(), MIME_Entity::DecodeBase64(), and Done().

00044         {
00045         int blen;
00046         char* buf;
00047 
00048         if ( ! pbuf )
00049                 internal_error("nil pointer to decoding result buffer");
00050 
00051         if ( *pbuf )
00052                 {
00053                 buf = *pbuf;
00054                 blen = *pblen;
00055                 }
00056         else
00057                 {
00058                 // Estimate the maximal number of 3-byte groups needed,
00059                 // plus 1 byte for the optional ending NUL.
00060                 blen = int((len + base64_group_next + 3) / 4) * 3 + 1;
00061                 *pbuf = buf = new char[blen];
00062                 }
00063 
00064         int dlen = 0;
00065 
00066         while ( 1 )
00067                 {
00068                 if ( base64_group_next == 4 )
00069                         {
00070                         // For every group of 4 6-bit numbers,
00071                         // write the decoded 3 bytes to the buffer.
00072                         if ( base64_after_padding )
00073                                 {
00074                                 if ( ++errored == 1 )
00075                                         IllegalEncoding("extra base64 groups after '=' padding are ignored");
00076                                 base64_group_next = 0;
00077                                 continue;
00078                                 }
00079 
00080                         int num_octets = 3 - base64_padding;
00081 
00082                         if ( buf + num_octets > *pbuf + blen )
00083                                 break;
00084 
00085                         uint32 bit32 =
00086                                 ((base64_group[0] & 0x3f) << 18) |
00087                                 ((base64_group[1] & 0x3f) << 12) |
00088                                 ((base64_group[2] & 0x3f) << 6)  |
00089                                 ((base64_group[3] & 0x3f));
00090 
00091                         if ( --num_octets >= 0 )
00092                                 *buf++ = char((bit32 >> 16) & 0xff);
00093 
00094                         if ( --num_octets >= 0 )
00095                                 *buf++ = char((bit32 >> 8) & 0xff);
00096 
00097                         if ( --num_octets >= 0 )
00098                                 *buf++ = char((bit32) & 0xff);
00099 
00100                         if ( base64_padding > 0 )
00101                                 base64_after_padding = 1;
00102 
00103                         base64_group_next = 0;
00104                         base64_padding = 0;
00105                         }
00106 
00107                 if ( dlen >= len )
00108                         break;
00109 
00110                 if ( data[dlen] == '=' )
00111                         ++base64_padding;
00112 
00113                 int k = base64_table[(unsigned char) data[dlen]];
00114                 if ( k >= 0 )
00115                         base64_group[base64_group_next++] = k;
00116                 else
00117                         {
00118                         if ( ++errored == 1 )
00119                                 IllegalEncoding(fmt("character %d ignored by Base64 decoding", (int) (data[dlen])));
00120                         }
00121 
00122                 ++dlen;
00123                 }
00124 
00125         *pblen = buf - *pbuf;
00126         return dlen;
00127         }

int Base64Decoder::Done int *  pblen,
char **  pbuf
 

Definition at line 129 of file Base64.cc.

References base64_group_next, Decode(), fmt(), and IllegalEncoding().

Referenced by decode_base64(), and MIME_Entity::FinishDecodeBase64().

00130         {
00131         const char* padding = "===";
00132 
00133         if ( base64_group_next != 0 )
00134                 {
00135                 if ( base64_group_next < 4 )
00136                         IllegalEncoding(fmt("incomplete base64 group, padding with %d bits of 0", (4-base64_group_next) * 6));
00137                 Decode(4 - base64_group_next, padding, pblen, pbuf);
00138                 return -1;
00139                 }
00140 
00141         if ( pblen )
00142                 *pblen = 0;
00143 
00144         return 0;
00145         }

int Base64Decoder::Errored  )  const [inline]
 

Definition at line 39 of file Base64.h.

References errored.

00039 { return errored; }

const char* Base64Decoder::ErrorMsg  )  const [inline]
 

Definition at line 41 of file Base64.h.

References error_msg.

00041 { return error_msg; }

int Base64Decoder::HasData  )  const [inline]
 

Definition at line 36 of file Base64.h.

References base64_group_next.

00036 { return base64_group_next != 0; }

void Base64Decoder::IllegalEncoding const char *  msg  )  [inline]
 

Definition at line 42 of file Base64.h.

References conn, run_time(), and Connection::Weird().

Referenced by Decode(), and Done().

00043                 { 
00044                 // strncpy(error_msg, msg, sizeof(error_msg));
00045                 if ( conn )
00046                         conn->Weird("base64_illegal_encoding", msg);
00047                 else
00048                         run_time(msg);
00049                 }


Member Data Documentation

int Base64Decoder::base64_after_padding [protected]
 

Definition at line 58 of file Base64.h.

Referenced by Base64Decoder(), and Decode().

char Base64Decoder::base64_group[4] [protected]
 

Definition at line 55 of file Base64.h.

Referenced by Decode().

int Base64Decoder::base64_group_next [protected]
 

Definition at line 56 of file Base64.h.

Referenced by Base64Decoder(), Decode(), Done(), and HasData().

int Base64Decoder::base64_padding [protected]
 

Definition at line 57 of file Base64.h.

Referenced by Base64Decoder(), and Decode().

Connection* Base64Decoder::conn [protected]
 

Definition at line 60 of file Base64.h.

Referenced by Base64Decoder(), and IllegalEncoding().

char Base64Decoder::error_msg[256] [protected]
 

Definition at line 52 of file Base64.h.

Referenced by ErrorMsg().

int Base64Decoder::errored [protected]
 

Definition at line 59 of file Base64.h.

Referenced by Base64Decoder(), Decode(), and Errored().


The documentation for this class was generated from the following files:
Generated on Wed Sep 14 03:07:53 2005 for bro_docs by doxygen 1.3.5