mirror of
https://github.com/zeek/zeek.git
synced 2025-10-10 10:38:20 +00:00
re-unify classes
This commit is contained in:
parent
cfada61672
commit
2b28c3a578
2 changed files with 25 additions and 37 deletions
|
@ -3,22 +3,9 @@
|
|||
#include <math.h>
|
||||
|
||||
int Base64Decoder::default_base64_table[256];
|
||||
const string Base64::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
const string Base64Decoder::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
Base64Encoder::Base64Encoder(const string& arg_alphabet)
|
||||
{
|
||||
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)
|
||||
void Base64Decoder::Encode(int len, const unsigned char* data, int* pblen, char** pbuf)
|
||||
{
|
||||
int blen;
|
||||
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_padding = base64_after_padding = 0;
|
||||
errored = 0;
|
||||
|
@ -117,6 +114,10 @@ int Base64Decoder::Decode(int len, const char* data, int* pblen, char** pbuf)
|
|||
int blen;
|
||||
char* buf;
|
||||
|
||||
// initialization of table on first_time call of Decode
|
||||
if ( base64_table == 0 )
|
||||
base64_table = InitBase64Table(alphabet);
|
||||
|
||||
if ( ! pbuf )
|
||||
reporter->InternalError("nil pointer to decoding result buffer");
|
||||
|
||||
|
@ -260,7 +261,7 @@ BroString* encode_base64(const BroString* s, const BroString* a)
|
|||
|
||||
char* outbuf = 0;
|
||||
int outlen = 0;
|
||||
Base64Encoder enc;
|
||||
Base64Decoder enc(0);
|
||||
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);
|
||||
|
||||
return new BroString(1, (u_char*)outbuf, outlen);
|
||||
|
|
25
src/Base64.h
25
src/Base64.h
|
@ -10,27 +10,10 @@
|
|||
#include "Analyzer.h"
|
||||
|
||||
// Maybe we should have a base class for generic decoders?
|
||||
|
||||
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 {
|
||||
class Base64Decoder {
|
||||
public:
|
||||
// <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.
|
||||
Base64Decoder(Analyzer* analyzer, const string& alphabet = "");
|
||||
~Base64Decoder();
|
||||
|
@ -46,6 +29,7 @@ public:
|
|||
// is not enough output buffer space.
|
||||
|
||||
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 HasData() const { return base64_group_next != 0; }
|
||||
|
@ -67,6 +51,9 @@ protected:
|
|||
char error_msg[256];
|
||||
|
||||
protected:
|
||||
static const string default_alphabet;
|
||||
string alphabet;
|
||||
|
||||
static int* InitBase64Table(const string& alphabet);
|
||||
static int default_base64_table[256];
|
||||
char base64_group[4];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue