re-unify classes

This commit is contained in:
Bernhard Amann 2013-03-12 09:27:59 -07:00
parent cfada61672
commit 2b28c3a578
2 changed files with 25 additions and 37 deletions

View file

@ -3,22 +3,9 @@
#include <math.h> #include <math.h>
int Base64Decoder::default_base64_table[256]; int Base64Decoder::default_base64_table[256];
const string Base64::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const string Base64Decoder::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Base64Encoder::Base64Encoder(const string& arg_alphabet) void Base64Decoder::Encode(int len, const unsigned char* data, int* pblen, char** pbuf)
{
if ( arg_alphabet.size() > 0 )
{
assert(arg_alphabet.size() == 64);
alphabet = arg_alphabet;
}
else
{
alphabet = default_alphabet;
}
}
void Base64Encoder::Encode(int len, const unsigned char* data, int* pblen, char** pbuf)
{ {
int blen; int blen;
char *buf; char *buf;
@ -97,9 +84,19 @@ int* Base64Decoder::InitBase64Table(const string& alphabet)
Base64Decoder::Base64Decoder(Analyzer* arg_analyzer, const string& alphabet) Base64Decoder::Base64Decoder(Analyzer* arg_analyzer, const string& arg_alphabet)
{ {
base64_table = InitBase64Table(alphabet.size() ? alphabet : default_alphabet); if ( arg_alphabet.size() > 0 )
{
assert(arg_alphabet.size() == 64);
alphabet = arg_alphabet;
}
else
{
alphabet = default_alphabet;
}
base64_table = 0;
base64_group_next = 0; base64_group_next = 0;
base64_padding = base64_after_padding = 0; base64_padding = base64_after_padding = 0;
errored = 0; errored = 0;
@ -117,6 +114,10 @@ int Base64Decoder::Decode(int len, const char* data, int* pblen, char** pbuf)
int blen; int blen;
char* buf; char* buf;
// initialization of table on first_time call of Decode
if ( base64_table == 0 )
base64_table = InitBase64Table(alphabet);
if ( ! pbuf ) if ( ! pbuf )
reporter->InternalError("nil pointer to decoding result buffer"); reporter->InternalError("nil pointer to decoding result buffer");
@ -260,7 +261,7 @@ BroString* encode_base64(const BroString* s, const BroString* a)
char* outbuf = 0; char* outbuf = 0;
int outlen = 0; int outlen = 0;
Base64Encoder enc; Base64Decoder enc(0);
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf); enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);
return new BroString(1, (u_char*)outbuf, outlen); return new BroString(1, (u_char*)outbuf, outlen);

View file

@ -10,27 +10,10 @@
#include "Analyzer.h" #include "Analyzer.h"
// Maybe we should have a base class for generic decoders? // Maybe we should have a base class for generic decoders?
class Base64Decoder {
class Base64 {
public:
protected:
static const string default_alphabet;
};
class Base64Encoder : public Base64 {
public:
Base64Encoder(const string& arg_alphabet = "");
void Encode(int len, const unsigned char* data, int* blen, char** buf);
protected:
string alphabet;
};
class Base64Decoder : public Base64 {
public: public:
// <analyzer> is used for error reporting, and it should be zero when // <analyzer> is used for error reporting, and it should be zero when
// the decoder is called by the built-in function decode_base64(). // the decoder is called by the built-in function decode_base64() or encode_base64().
// Empty alphabet indicates the default base64 alphabet. // Empty alphabet indicates the default base64 alphabet.
Base64Decoder(Analyzer* analyzer, const string& alphabet = ""); Base64Decoder(Analyzer* analyzer, const string& alphabet = "");
~Base64Decoder(); ~Base64Decoder();
@ -46,6 +29,7 @@ public:
// is not enough output buffer space. // is not enough output buffer space.
int Decode(int len, const char* data, int* blen, char** buf); int Decode(int len, const char* data, int* blen, char** buf);
void Encode(int len, const unsigned char* data, int* blen, char** buf);
int Done(int* pblen, char** pbuf); int Done(int* pblen, char** pbuf);
int HasData() const { return base64_group_next != 0; } int HasData() const { return base64_group_next != 0; }
@ -67,6 +51,9 @@ protected:
char error_msg[256]; char error_msg[256];
protected: protected:
static const string default_alphabet;
string alphabet;
static int* InitBase64Table(const string& alphabet); static int* InitBase64Table(const string& alphabet);
static int default_base64_table[256]; static int default_base64_table[256];
char base64_group[4]; char base64_group[4];