mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 01:28:20 +00:00
Merge remote-tracking branch 'origin/master' into topic/bernhard/input-update
This commit is contained in:
commit
f1c91f02ce
20 changed files with 419 additions and 70 deletions
|
@ -1,10 +1,48 @@
|
|||
#include "config.h"
|
||||
#include "Base64.h"
|
||||
#include <math.h>
|
||||
|
||||
int Base64Decoder::default_base64_table[256];
|
||||
const string Base64Decoder::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
int Base64Converter::default_base64_table[256];
|
||||
const string Base64Converter::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
int* Base64Decoder::InitBase64Table(const string& alphabet)
|
||||
void Base64Converter::Encode(int len, const unsigned char* data, int* pblen, char** pbuf)
|
||||
{
|
||||
int blen;
|
||||
char *buf;
|
||||
|
||||
if ( ! pbuf )
|
||||
reporter->InternalError("nil pointer to encoding result buffer");
|
||||
|
||||
if ( *pbuf && (*pblen % 4 != 0) )
|
||||
reporter->InternalError("Base64 encode buffer not a multiple of 4");
|
||||
|
||||
if ( *pbuf )
|
||||
{
|
||||
buf = *pbuf;
|
||||
blen = *pblen;
|
||||
}
|
||||
else
|
||||
{
|
||||
blen = (int)(4 * ceil((double)len / 3));
|
||||
*pbuf = buf = new char[blen];
|
||||
*pblen = blen;
|
||||
}
|
||||
|
||||
for ( int i = 0, j = 0; (i < len) && ( j < blen ); )
|
||||
{
|
||||
uint32_t bit32 = ((i < len ? data[i++] : 0) << 16) +
|
||||
((i < len ? data[i++] : 0 & i++) << 8) +
|
||||
( i < len ? data[i++] : 0 & i++);
|
||||
|
||||
buf[j++] = alphabet[(bit32 >> 18) & 0x3f];
|
||||
buf[j++] = alphabet[(bit32 >> 12) & 0x3f];
|
||||
buf[j++] = (i == (len+2)) ? '=' : alphabet[(bit32 >> 6) & 0x3f];
|
||||
buf[j++] = (i >= (len+1)) ? '=' : alphabet[bit32 & 0x3f];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int* Base64Converter::InitBase64Table(const string& alphabet)
|
||||
{
|
||||
assert(alphabet.size() == 64);
|
||||
|
||||
|
@ -44,26 +82,42 @@ int* Base64Decoder::InitBase64Table(const string& alphabet)
|
|||
return base64_table;
|
||||
}
|
||||
|
||||
Base64Decoder::Base64Decoder(Analyzer* arg_analyzer, const string& alphabet)
|
||||
|
||||
|
||||
Base64Converter::Base64Converter(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;
|
||||
analyzer = arg_analyzer;
|
||||
}
|
||||
|
||||
Base64Decoder::~Base64Decoder()
|
||||
Base64Converter::~Base64Converter()
|
||||
{
|
||||
if ( base64_table != default_base64_table )
|
||||
delete base64_table;
|
||||
}
|
||||
|
||||
int Base64Decoder::Decode(int len, const char* data, int* pblen, char** pbuf)
|
||||
int Base64Converter::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 )
|
||||
base64_table = InitBase64Table(alphabet);
|
||||
|
||||
if ( ! pbuf )
|
||||
reporter->InternalError("nil pointer to decoding result buffer");
|
||||
|
||||
|
@ -145,7 +199,7 @@ int Base64Decoder::Decode(int len, const char* data, int* pblen, char** pbuf)
|
|||
return dlen;
|
||||
}
|
||||
|
||||
int Base64Decoder::Done(int* pblen, char** pbuf)
|
||||
int Base64Converter::Done(int* pblen, char** pbuf)
|
||||
{
|
||||
const char* padding = "===";
|
||||
|
||||
|
@ -177,7 +231,7 @@ BroString* decode_base64(const BroString* s, const BroString* a)
|
|||
int rlen2, rlen = buf_len;
|
||||
char* rbuf2, *rbuf = new char[rlen];
|
||||
|
||||
Base64Decoder dec(0, a ? a->CheckString() : "");
|
||||
Base64Converter dec(0, a ? a->CheckString() : "");
|
||||
if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf) == -1 )
|
||||
goto err;
|
||||
|
||||
|
@ -195,3 +249,21 @@ err:
|
|||
delete [] rbuf;
|
||||
return 0;
|
||||
}
|
||||
|
||||
BroString* encode_base64(const BroString* s, const BroString* a)
|
||||
{
|
||||
if ( a && a->Len() != 64 )
|
||||
{
|
||||
reporter->Error("base64 alphabet is not 64 characters: %s",
|
||||
a->CheckString());
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* outbuf = 0;
|
||||
int outlen = 0;
|
||||
Base64Converter enc(0, a ? a->CheckString() : "");
|
||||
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);
|
||||
|
||||
return new BroString(1, (u_char*)outbuf, outlen);
|
||||
}
|
||||
|
||||
|
|
23
src/Base64.h
23
src/Base64.h
|
@ -10,14 +10,13 @@
|
|||
#include "Analyzer.h"
|
||||
|
||||
// Maybe we should have a base class for generic decoders?
|
||||
|
||||
class Base64Decoder {
|
||||
class Base64Converter {
|
||||
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();
|
||||
Base64Converter(Analyzer* analyzer, const string& alphabet = "");
|
||||
~Base64Converter();
|
||||
|
||||
// A note on Decode():
|
||||
//
|
||||
|
@ -30,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; }
|
||||
|
@ -39,7 +39,7 @@ public:
|
|||
|
||||
const char* ErrorMsg() const { return error_msg; }
|
||||
void IllegalEncoding(const char* msg)
|
||||
{
|
||||
{
|
||||
// strncpy(error_msg, msg, sizeof(error_msg));
|
||||
if ( analyzer )
|
||||
analyzer->Weird("base64_illegal_encoding", msg);
|
||||
|
@ -51,19 +51,22 @@ 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];
|
||||
int base64_group_next;
|
||||
int base64_padding;
|
||||
int base64_after_padding;
|
||||
int* base64_table;
|
||||
int errored; // if true, we encountered an error - skip further processing
|
||||
Analyzer* analyzer;
|
||||
int* base64_table;
|
||||
|
||||
static int* InitBase64Table(const string& alphabet);
|
||||
static int default_base64_table[256];
|
||||
static const string default_alphabet;
|
||||
};
|
||||
|
||||
BroString* decode_base64(const BroString* s, const BroString* a = 0);
|
||||
BroString* encode_base64(const BroString* s, const BroString* a = 0);
|
||||
|
||||
#endif /* base64_h */
|
||||
|
|
|
@ -810,7 +810,7 @@ void MIME_Entity::StartDecodeBase64()
|
|||
if ( base64_decoder )
|
||||
reporter->InternalError("previous Base64 decoder not released!");
|
||||
|
||||
base64_decoder = new Base64Decoder(message->GetAnalyzer());
|
||||
base64_decoder = new Base64Converter(message->GetAnalyzer());
|
||||
}
|
||||
|
||||
void MIME_Entity::FinishDecodeBase64()
|
||||
|
|
|
@ -163,7 +163,7 @@ protected:
|
|||
MIME_Entity* parent;
|
||||
MIME_Entity* current_child_entity;
|
||||
|
||||
Base64Decoder* base64_decoder;
|
||||
Base64Converter* base64_decoder;
|
||||
|
||||
int data_buf_length;
|
||||
char* data_buf_data;
|
||||
|
|
46
src/bro.bif
46
src/bro.bif
|
@ -2829,13 +2829,55 @@ function bytestring_to_hexstr%(bytestring: string%): string
|
|||
return new StringVal(hexstr);
|
||||
%}
|
||||
|
||||
## Encodes a Base64-encoded string.
|
||||
##
|
||||
## s: The string to encode
|
||||
##
|
||||
## Returns: The encoded version of *s*.
|
||||
##
|
||||
## .. bro:see:: encode_base64_custom, decode_base64
|
||||
function encode_base64%(s: string%): string
|
||||
%{
|
||||
BroString* t = encode_base64(s->AsString());
|
||||
if ( t )
|
||||
return new StringVal(t);
|
||||
else
|
||||
{
|
||||
reporter->Error("error in encoding string %s", s->CheckString());
|
||||
return new StringVal("");
|
||||
}
|
||||
%}
|
||||
|
||||
## Encodes a Base64-encoded string with a custom alphabet.
|
||||
##
|
||||
## s: The string to encode
|
||||
##
|
||||
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||
## length of *a* must be 64. For example, a custom alphabet could be
|
||||
## ``"!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?"``.
|
||||
##
|
||||
## Returns: The encoded version of *s*.
|
||||
##
|
||||
## .. bro:see:: encode_base64, decode_base64_custom
|
||||
function encode_base64_custom%(s: string, a: string%): string
|
||||
%{
|
||||
BroString* t = encode_base64(s->AsString(), a->AsString());
|
||||
if ( t )
|
||||
return new StringVal(t);
|
||||
else
|
||||
{
|
||||
reporter->Error("error in encoding string %s", s->CheckString());
|
||||
return new StringVal("");
|
||||
}
|
||||
%}
|
||||
|
||||
## Decodes a Base64-encoded string.
|
||||
##
|
||||
## s: The Base64-encoded string.
|
||||
##
|
||||
## Returns: The decoded version of *s*.
|
||||
##
|
||||
## .. bro:see:: decode_base64_custom
|
||||
## .. bro:see:: decode_base64_custom, encode_base64
|
||||
function decode_base64%(s: string%): string
|
||||
%{
|
||||
BroString* t = decode_base64(s->AsString());
|
||||
|
@ -2858,7 +2900,7 @@ function decode_base64%(s: string%): string
|
|||
##
|
||||
## Returns: The decoded version of *s*.
|
||||
##
|
||||
## .. bro:see:: decode_base64
|
||||
## .. bro:see:: decode_base64, encode_base64_custom
|
||||
function decode_base64_custom%(s: string, a: string%): string
|
||||
%{
|
||||
BroString* t = decode_base64(s->AsString(), a->AsString());
|
||||
|
|
|
@ -265,6 +265,7 @@ _nb_dns_mkquery(register struct nb_dns_info *nd, register const char *name,
|
|||
default:
|
||||
snprintf(errstr, NB_DNS_ERRSIZE,
|
||||
"_nb_dns_mkquery: bad family %d", atype);
|
||||
free(ne);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue