00001 // $Id: Base64.h,v 1.2 2004/08/31 19:29:04 vern Exp $ 00002 00003 #ifndef base64_h 00004 #define base64_h 00005 00006 #include <assert.h> 00007 #include <stdio.h> 00008 #include <string.h> 00009 00010 #include "util.h" 00011 #include "BroString.h" 00012 #include "Conn.h" 00013 00014 // Maybe we should have a base class for generic decoders? 00015 00016 class Base64Decoder { 00017 public: 00018 // <conn> is used for error reporting, and it should be zero 00019 // when the decoder is called by the built-in function 00020 // decode_base64(). 00021 Base64Decoder(Connection* conn); 00022 00023 // A note on Decode(): 00024 // 00025 // The input is specified by <len> and <data> and the output 00026 // buffer by <blen> and <buf>. If *buf is nil, a buffer of 00027 // an appropriate size will be new'd and *buf will point 00028 // to the buffer on return. *blen holds the length of 00029 // decoded data on return. The function returns the number of 00030 // input bytes processed, since the decoding will stop when there 00031 // is not enough output buffer space. 00032 00033 int Decode(int len, const char* data, int* blen, char** buf); 00034 00035 int Done(int* pblen, char** pbuf); 00036 int HasData() const { return base64_group_next != 0; } 00037 00038 // True if an error has occurred. 00039 int Errored() const { return errored; } 00040 00041 const char* ErrorMsg() const { return error_msg; } 00042 void IllegalEncoding(const char* msg) 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 } 00050 00051 protected: 00052 char error_msg[256]; 00053 00054 protected: 00055 char base64_group[4]; 00056 int base64_group_next; 00057 int base64_padding; 00058 int base64_after_padding; 00059 int errored; // if true, we encountered an error - skip further processing 00060 Connection* conn; 00061 }; 00062 00063 BroString* decode_base64(BroString* s); 00064 00065 #endif /* base64_h */
1.3.5