Revert "Merge remote-tracking branch 'origin/topic/dev/bugrpone-narrowing-conversions-base64'"

This reverts commit 705210a035.
The original changes broke the Base64.h API which may be used by
external plugins.

Fixes GH-676
This commit is contained in:
Jon Siwek 2019-11-04 09:43:54 -08:00
parent 33c26e9ea3
commit b6def63167
5 changed files with 35 additions and 28 deletions

View file

@ -1,4 +1,12 @@
3.1.0-dev.241 | 2019-11-04 09:44:20 -0800
* Revert recent changes to Base64.h API. (Jon Siwek, Corelight)
This reverts commit 705210a035d4128ecdf249c7db98607c570bfcd2.
The original changes broke the Base64.h API which may be used by
external plugins.
3.1.0-dev.240 | 2019-11-01 15:58:55 -0700 3.1.0-dev.240 | 2019-11-01 15:58:55 -0700
* Use #pragma once in Base64.h (Jon Siwek, Corelight) * Use #pragma once in Base64.h (Jon Siwek, Corelight)

View file

@ -1 +1 @@
3.1.0-dev.240 3.1.0-dev.241

View file

@ -5,9 +5,9 @@
int Base64Converter::default_base64_table[256]; int Base64Converter::default_base64_table[256];
const string Base64Converter::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const string Base64Converter::default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void Base64Converter::Encode(size_t len, const unsigned char* data, size_t* pblen, char** pbuf) void Base64Converter::Encode(int len, const unsigned char* data, int* pblen, char** pbuf)
{ {
size_t blen; int blen;
char *buf; char *buf;
if ( ! pbuf ) if ( ! pbuf )
@ -23,12 +23,12 @@ void Base64Converter::Encode(size_t len, const unsigned char* data, size_t* pble
} }
else else
{ {
blen = (size_t)(4 * ceil((double)len / 3)); blen = (int)(4 * ceil((double)len / 3));
*pbuf = buf = new char[blen]; *pbuf = buf = new char[blen];
*pblen = blen; *pblen = blen;
} }
for ( size_t i = 0, j = 0; (i < len) && ( j < blen ); ) for ( int i = 0, j = 0; (i < len) && ( j < blen ); )
{ {
uint32_t bit32 = data[i++] << 16; uint32_t bit32 = data[i++] << 16;
bit32 += (i++ < len ? data[i-1] : 0) << 8; bit32 += (i++ < len ? data[i-1] : 0) << 8;
@ -107,9 +107,9 @@ Base64Converter::~Base64Converter()
delete [] base64_table; delete [] base64_table;
} }
std::pair<size_t, bool> Base64Converter::Decode(size_t len, const char* data, size_t* pblen, char** pbuf) int Base64Converter::Decode(int len, const char* data, int* pblen, char** pbuf)
{ {
size_t blen; int blen;
char* buf; char* buf;
// Initialization of table on first_time call of Decode. // Initialization of table on first_time call of Decode.
@ -128,11 +128,11 @@ std::pair<size_t, bool> Base64Converter::Decode(size_t len, const char* data, si
{ {
// Estimate the maximal number of 3-byte groups needed, // Estimate the maximal number of 3-byte groups needed,
// plus 1 byte for the optional ending NUL. // plus 1 byte for the optional ending NUL.
blen = size_t((len + base64_group_next + 3) / 4) * 3 + 1; blen = int((len + base64_group_next + 3) / 4) * 3 + 1;
*pbuf = buf = new char[blen]; *pbuf = buf = new char[blen];
} }
size_t dlen = 0; int dlen = 0;
while ( 1 ) while ( 1 )
{ {
@ -183,23 +183,21 @@ std::pair<size_t, bool> Base64Converter::Decode(size_t len, const char* data, si
int k = base64_table[(unsigned char) data[dlen]]; int k = base64_table[(unsigned char) data[dlen]];
if ( k >= 0 ) if ( k >= 0 )
base64_group[base64_group_next++] = static_cast<char> (k); base64_group[base64_group_next++] = k;
else else
{ {
if ( ++errored == 1 ) if ( ++errored == 1 )
{
IllegalEncoding(fmt("character %d ignored by Base64 decoding", (int) (data[dlen]))); IllegalEncoding(fmt("character %d ignored by Base64 decoding", (int) (data[dlen])));
} }
}
++dlen; ++dlen;
} }
*pblen = buf - *pbuf; *pblen = buf - *pbuf;
return {dlen, errored == 0}; return dlen;
} }
int Base64Converter::Done(size_t* pblen, char** pbuf) int Base64Converter::Done(int* pblen, char** pbuf)
{ {
const char* padding = "==="; const char* padding = "===";
@ -227,12 +225,12 @@ BroString* decode_base64(const BroString* s, const BroString* a, Connection* con
return 0; return 0;
} }
size_t buf_len = size_t((s->Len() + 3) / 4) * 3 + 1; int buf_len = int((s->Len() + 3) / 4) * 3 + 1;
size_t rlen2, rlen = buf_len; int rlen2, rlen = buf_len;
char* rbuf2, *rbuf = new char[rlen]; char* rbuf2, *rbuf = new char[rlen];
Base64Converter dec(conn, a ? a->CheckString() : ""); Base64Converter dec(conn, a ? a->CheckString() : "");
if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf).second == false ) if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf) == -1 )
goto err; goto err;
rlen2 = buf_len - rlen; rlen2 = buf_len - rlen;
@ -247,7 +245,7 @@ BroString* decode_base64(const BroString* s, const BroString* a, Connection* con
err: err:
delete [] rbuf; delete [] rbuf;
return nullptr; return 0;
} }
BroString* encode_base64(const BroString* s, const BroString* a, Connection* conn) BroString* encode_base64(const BroString* s, const BroString* a, Connection* conn)
@ -260,7 +258,7 @@ BroString* encode_base64(const BroString* s, const BroString* a, Connection* con
} }
char* outbuf = 0; char* outbuf = 0;
size_t outlen = 0; int outlen = 0;
Base64Converter enc(conn, a ? a->CheckString() : ""); Base64Converter enc(conn, a ? a->CheckString() : "");
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf); enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);

View file

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <utility>
#include "util.h" #include "util.h"
#include "BroString.h" #include "BroString.h"
@ -28,12 +27,13 @@ public:
// an appropriate size will be new'd and *buf will point // an appropriate size will be new'd and *buf will point
// to the buffer on return. *blen holds the length of // to the buffer on return. *blen holds the length of
// decoded data on return. The function returns the number of // decoded data on return. The function returns the number of
// input bytes processed and a success flag. // input bytes processed, since the decoding will stop when there
// is not enough output buffer space.
std::pair<size_t, bool> Decode(size_t len, const char* data, size_t* pblen, char** buf); int Decode(int len, const char* data, int* blen, char** buf);
void Encode(size_t len, const unsigned char* data, size_t* blen, char** buf); void Encode(int len, const unsigned char* data, int* blen, char** buf);
int Done(size_t* 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; }
// True if an error has occurred. // True if an error has occurred.

View file

@ -1124,14 +1124,14 @@ void MIME_Entity::DecodeQuotedPrintable(int len, const char* data)
void MIME_Entity::DecodeBase64(int len, const char* data) void MIME_Entity::DecodeBase64(int len, const char* data)
{ {
size_t rlen; int rlen;
char rbuf[128]; char rbuf[128];
while ( len > 0 ) while ( len > 0 )
{ {
rlen = 128; rlen = 128;
char* prbuf = rbuf; char* prbuf = rbuf;
int decoded = base64_decoder->Decode(len, data, &rlen, &prbuf).first; // typecasting for now int decoded = base64_decoder->Decode(len, data, &rlen, &prbuf);
DataOctets(rlen, rbuf); DataOctets(rlen, rbuf);
len -= decoded; data += decoded; len -= decoded; data += decoded;
} }
@ -1161,10 +1161,11 @@ void MIME_Entity::FinishDecodeBase64()
if ( ! base64_decoder ) if ( ! base64_decoder )
return; return;
size_t rlen = 128; int rlen = 128;
char rbuf[128]; char rbuf[128];
char* prbuf = rbuf; char* prbuf = rbuf;
if ( base64_decoder->Done(&rlen, &prbuf) ) // typecasting for now
if ( base64_decoder->Done(&rlen, &prbuf) )
{ // some remaining data { // some remaining data
if ( rlen > 0 ) if ( rlen > 0 )
DataOctets(rlen, rbuf); DataOctets(rlen, rbuf);