mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 17:48:21 +00:00
Proof of Concept on Bugprone Narrowing Conversions: Base64
This commit is contained in:
parent
8ba19cd7d1
commit
cad6f881eb
3 changed files with 24 additions and 20 deletions
|
@ -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(int len, const unsigned char* data, int* pblen, char** pbuf)
|
void Base64Converter::Encode(size_t len, const unsigned char* data, size_t* pblen, char** pbuf)
|
||||||
{
|
{
|
||||||
int blen;
|
size_t blen;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
if ( ! pbuf )
|
if ( ! pbuf )
|
||||||
|
@ -23,12 +23,12 @@ void Base64Converter::Encode(int len, const unsigned char* data, int* pblen, cha
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
blen = (int)(4 * ceil((double)len / 3));
|
blen = (size_t)(4 * ceil((double)len / 3));
|
||||||
*pbuf = buf = new char[blen];
|
*pbuf = buf = new char[blen];
|
||||||
*pblen = blen;
|
*pblen = blen;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int i = 0, j = 0; (i < len) && ( j < blen ); )
|
for ( size_t 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Base64Converter::Decode(int len, const char* data, int* pblen, char** pbuf)
|
size_t Base64Converter::Decode(size_t len, const char* data, size_t* pblen, char** pbuf)
|
||||||
{
|
{
|
||||||
int blen;
|
size_t 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 @@ int Base64Converter::Decode(int len, const char* data, int* pblen, char** pbuf)
|
||||||
{
|
{
|
||||||
// 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 = int((len + base64_group_next + 3) / 4) * 3 + 1;
|
blen = size_t((len + base64_group_next + 3) / 4) * 3 + 1;
|
||||||
*pbuf = buf = new char[blen];
|
*pbuf = buf = new char[blen];
|
||||||
}
|
}
|
||||||
|
|
||||||
int dlen = 0;
|
size_t dlen = 0;
|
||||||
|
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{
|
{
|
||||||
|
@ -183,7 +183,7 @@ int Base64Converter::Decode(int len, const char* data, int* pblen, char** pbuf)
|
||||||
|
|
||||||
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++] = k;
|
base64_group[base64_group_next++] = static_cast<char> (k);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( ++errored == 1 )
|
if ( ++errored == 1 )
|
||||||
|
@ -197,7 +197,7 @@ int Base64Converter::Decode(int len, const char* data, int* pblen, char** pbuf)
|
||||||
return dlen;
|
return dlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Base64Converter::Done(int* pblen, char** pbuf)
|
int Base64Converter::Done(size_t* pblen, char** pbuf)
|
||||||
{
|
{
|
||||||
const char* padding = "===";
|
const char* padding = "===";
|
||||||
|
|
||||||
|
@ -225,8 +225,8 @@ BroString* decode_base64(const BroString* s, const BroString* a, Connection* con
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int buf_len = int((s->Len() + 3) / 4) * 3 + 1;
|
size_t buf_len = size_t((s->Len() + 3) / 4) * 3 + 1;
|
||||||
int rlen2, rlen = buf_len;
|
size_t 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() : "");
|
||||||
|
@ -258,7 +258,7 @@ BroString* encode_base64(const BroString* s, const BroString* a, Connection* con
|
||||||
}
|
}
|
||||||
|
|
||||||
char* outbuf = 0;
|
char* outbuf = 0;
|
||||||
int outlen = 0;
|
size_t 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);
|
||||||
|
|
||||||
|
|
11
src/Base64.h
11
src/Base64.h
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#ifndef base64_h
|
||||||
|
#define base64_h
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -30,10 +31,10 @@ public:
|
||||||
// input bytes processed, since the decoding will stop when there
|
// input bytes processed, since the decoding will stop when there
|
||||||
// is not enough output buffer space.
|
// is not enough output buffer space.
|
||||||
|
|
||||||
int Decode(int len, const char* data, int* blen, char** buf);
|
size_t Decode(size_t len, const char* data, size_t* pblen, char** buf);
|
||||||
void Encode(int len, const unsigned char* data, int* blen, char** buf);
|
void Encode(size_t len, const unsigned char* data, size_t* blen, char** buf);
|
||||||
|
|
||||||
int Done(int* pblen, char** pbuf);
|
int Done(size_t* 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.
|
||||||
|
@ -70,3 +71,5 @@ protected:
|
||||||
|
|
||||||
BroString* decode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
BroString* decode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
||||||
BroString* encode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
BroString* encode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
||||||
|
|
||||||
|
#endif /* base64_h */
|
||||||
|
|
|
@ -1131,7 +1131,8 @@ void MIME_Entity::DecodeBase64(int len, const char* data)
|
||||||
{
|
{
|
||||||
rlen = 128;
|
rlen = 128;
|
||||||
char* prbuf = rbuf;
|
char* prbuf = rbuf;
|
||||||
int decoded = base64_decoder->Decode(len, data, &rlen, &prbuf);
|
size_t x = static_cast<size_t> (rlen);
|
||||||
|
int decoded = base64_decoder->Decode(len, data, &x, &prbuf); // typecasting for now
|
||||||
DataOctets(rlen, rbuf);
|
DataOctets(rlen, rbuf);
|
||||||
len -= decoded; data += decoded;
|
len -= decoded; data += decoded;
|
||||||
}
|
}
|
||||||
|
@ -1164,8 +1165,8 @@ void MIME_Entity::FinishDecodeBase64()
|
||||||
int rlen = 128;
|
int rlen = 128;
|
||||||
char rbuf[128];
|
char rbuf[128];
|
||||||
char* prbuf = rbuf;
|
char* prbuf = rbuf;
|
||||||
|
size_t x = static_cast<size_t> (rlen);
|
||||||
if ( base64_decoder->Done(&rlen, &prbuf) )
|
if ( base64_decoder->Done(&x, &prbuf) ) // typecasting for now
|
||||||
{ // some remaining data
|
{ // some remaining data
|
||||||
if ( rlen > 0 )
|
if ( rlen > 0 )
|
||||||
DataOctets(rlen, rbuf);
|
DataOctets(rlen, rbuf);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue