binpac: Add pre-commit hooks and run clang-format on everything

This commit is contained in:
Tim Wojtulewicz 2022-07-07 11:49:24 -07:00
parent 090ac0a6e0
commit 090325df40
91 changed files with 3086 additions and 3665 deletions

View file

@ -1,27 +1,28 @@
#ifndef binpac_an_h
#define binpac_an_h
namespace binpac {
namespace binpac
{
// TODO: Add the Done() function
// The interface for a connection analyzer
class ConnectionAnalyzer {
class ConnectionAnalyzer
{
public:
virtual ~ConnectionAnalyzer() {}
virtual void NewData(bool is_orig,
const unsigned char* begin_of_data,
virtual ~ConnectionAnalyzer() { }
virtual void NewData(bool is_orig, const unsigned char* begin_of_data,
const unsigned char* end_of_data) = 0;
};
};
// The interface for a flow analyzer
class FlowAnalyzer {
class FlowAnalyzer
{
public:
virtual ~FlowAnalyzer() {}
virtual void NewData(const unsigned char* begin_of_data,
const unsigned char* end_of_data) = 0;
};
virtual ~FlowAnalyzer() { }
virtual void NewData(const unsigned char* begin_of_data, const unsigned char* end_of_data) = 0;
};
} // namespace binpac
} // namespace binpac
#endif // binpac_an_h
#endif // binpac_an_h

View file

@ -1,20 +1,22 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // for memcpy
#include <stdlib.h>
#include <string.h> // for memcpy
#define binpac_regex_h
#include "binpac.h"
#include "binpac_buffer.h"
namespace binpac {
namespace binpac
{
extern double network_time();
namespace {
const unsigned char CR = '\r';
const unsigned char LF = '\n';
}
namespace
{
const unsigned char CR = '\r';
const unsigned char LF = '\n';
}
binpac::FlowBuffer::Policy binpac::FlowBuffer::policy = {
// max_capacity
@ -67,9 +69,7 @@ void FlowBuffer::NewMessage()
switch ( mode_ )
{
case LINE_MODE:
bytes_to_advance = (frame_length_ +
(linebreak_style_ == STRICT_CRLF ?
2 : 1));
bytes_to_advance = (frame_length_ + (linebreak_style_ == STRICT_CRLF ? 2 : 1));
break;
case FRAME_MODE:
bytes_to_advance = frame_length_;
@ -118,14 +118,13 @@ void FlowBuffer::ExpandBuffer(int length)
if ( length > policy.max_capacity )
{
std::string reason = strfmt("expand past max capacity %d/%d",
length, policy.max_capacity);
std::string reason = strfmt("expand past max capacity %d/%d", length, policy.max_capacity);
throw ExceptionFlowBufferAlloc(reason.c_str());
}
// Allocate a new buffer and copy the existing contents
buffer_length_ = length;
unsigned char* new_buf = (unsigned char *) realloc(buffer_, buffer_length_);
unsigned char* new_buf = (unsigned char*)realloc(buffer_, buffer_length_);
if ( ! new_buf )
throw ExceptionFlowBufferAlloc("expand realloc OOM");
@ -139,7 +138,7 @@ void FlowBuffer::ContractBuffer()
return;
buffer_length_ = policy.min_capacity;
unsigned char* new_buf = (unsigned char *) realloc(buffer_, buffer_length_);
unsigned char* new_buf = (unsigned char*)realloc(buffer_, buffer_length_);
if ( ! new_buf )
throw ExceptionFlowBufferAlloc("contract realloc OOM");
@ -154,7 +153,7 @@ void FlowBuffer::SetLineBreaker(unsigned char* lbreaker)
linebreak_style_ = LINE_BREAKER;
}
void FlowBuffer::UnsetLineBreaker()
void FlowBuffer::UnsetLineBreaker()
{
linebreak_style_ = linebreak_style_default;
}
@ -233,8 +232,7 @@ void FlowBuffer::NewData(const_byteptr begin, const_byteptr end)
ClearPreviousData();
BINPAC_ASSERT((buffer_n_ == 0 && message_complete_) ||
orig_data_begin_ == orig_data_end_);
BINPAC_ASSERT((buffer_n_ == 0 && message_complete_) || orig_data_begin_ == orig_data_end_);
orig_data_begin_ = begin;
orig_data_end_ = end;
@ -273,8 +271,7 @@ void FlowBuffer::ClearPreviousData()
{
if ( frame_length_ > 0 )
{
frame_length_ -=
(orig_data_end_ - orig_data_begin_);
frame_length_ -= (orig_data_end_ - orig_data_begin_);
}
orig_data_begin_ = orig_data_end_;
}
@ -320,14 +317,14 @@ Finite state automaton for CR_OR_LF:
(!--line is complete, *--add to buffer)
CR_OR_LF_0:
CR: CR_OR_LF_1 !
LF: CR_OR_LF_0 !
.: CR_OR_LF_0 *
CR: CR_OR_LF_1 !
LF: CR_OR_LF_0 !
.: CR_OR_LF_0 *
CR_OR_LF_1:
CR: CR_OR_LF_1 !
LF: CR_OR_LF_0
.: CR_OR_LF_0 *
CR: CR_OR_LF_1 !
LF: CR_OR_LF_0
.: CR_OR_LF_0 *
*/
void FlowBuffer::MarkOrCopyLine_CR_OR_LF()
@ -335,8 +332,7 @@ void FlowBuffer::MarkOrCopyLine_CR_OR_LF()
if ( ! (orig_data_begin_ && orig_data_end_) )
return;
if ( state_ == CR_OR_LF_1 &&
orig_data_begin_ < orig_data_end_ && *orig_data_begin_ == LF )
if ( state_ == CR_OR_LF_1 && orig_data_begin_ < orig_data_end_ && *orig_data_begin_ == LF )
{
state_ = CR_OR_LF_0;
++orig_data_begin_;
@ -378,9 +374,8 @@ found_end_of_line:
message_complete_ = true;
#if DEBUG_FLOW_BUFFER
fprintf(stderr, "%.6f Line complete: [%s]\n",
network_time(),
string((const char *) begin(), (const char *) end()).c_str());
fprintf(stderr, "%.6f Line complete: [%s]\n", network_time(),
string((const char*)begin(), (const char*)end()).c_str());
#endif
}
@ -389,14 +384,14 @@ Finite state automaton and STRICT_CRLF:
(!--line is complete, *--add to buffer)
STRICT_CRLF_0:
CR: STRICT_CRLF_1 *
LF: STRICT_CRLF_0 *
.: STRICT_CRLF_0 *
CR: STRICT_CRLF_1 *
LF: STRICT_CRLF_0 *
.: STRICT_CRLF_0 *
STRICT_CRLF_1:
CR: STRICT_CRLF_1 *
LF: STRICT_CRLF_0 ! (--buffer_n_)
.: STRICT_CRLF_0 *
CR: STRICT_CRLF_1 *
LF: STRICT_CRLF_0 ! (--buffer_n_)
.: STRICT_CRLF_0 *
*/
void FlowBuffer::MarkOrCopyLine_STRICT_CRLF()
@ -442,9 +437,8 @@ found_end_of_line:
message_complete_ = true;
#if DEBUG_FLOW_BUFFER
fprintf(stderr, "%.6f Line complete: [%s]\n",
network_time(),
string((const char *) begin(), (const char *) end()).c_str());
fprintf(stderr, "%.6f Line complete: [%s]\n", network_time(),
string((const char*)begin(), (const char*)end()).c_str());
#endif
}
@ -477,13 +471,11 @@ found_end_of_line:
message_complete_ = true;
#if DEBUG_FLOW_BUFFER
fprintf(stderr, "%.6f Line complete: [%s]\n",
network_time(),
string((const char *) begin(), (const char *) end()).c_str());
fprintf(stderr, "%.6f Line complete: [%s]\n", network_time(),
string((const char*)begin(), (const char*)end()).c_str());
#endif
}
// Invariants:
//
// When buffer_n_ == 0:
@ -494,8 +486,7 @@ found_end_of_line:
void FlowBuffer::MarkOrCopyFrame()
{
if ( mode_ == FRAME_MODE && state_ == CR_OR_LF_1 &&
orig_data_begin_ < orig_data_end_ )
if ( mode_ == FRAME_MODE && state_ == CR_OR_LF_1 && orig_data_begin_ < orig_data_end_ )
{
// Skip the lingering LF
if ( *orig_data_begin_ == LF )
@ -508,8 +499,7 @@ void FlowBuffer::MarkOrCopyFrame()
if ( buffer_n_ == 0 )
{
// If there is enough data
if ( frame_length_ >= 0 &&
orig_data_end_ - orig_data_begin_ >= frame_length_ )
if ( frame_length_ >= 0 && orig_data_end_ - orig_data_begin_ >= frame_length_ )
{
// Do nothing except setting the message complete flag
message_complete_ = true;
@ -518,15 +508,14 @@ void FlowBuffer::MarkOrCopyFrame()
{
if ( ! chunked_ )
{
AppendToBuffer(orig_data_begin_,
orig_data_end_ - orig_data_begin_);
AppendToBuffer(orig_data_begin_, orig_data_end_ - orig_data_begin_);
}
message_complete_ = false;
}
}
else
{
BINPAC_ASSERT(!chunked_);
BINPAC_ASSERT(! chunked_);
int bytes_to_copy = orig_data_end_ - orig_data_begin_;
message_complete_ = false;
if ( frame_length_ >= 0 && buffer_n_ + bytes_to_copy >= frame_length_ )
@ -540,10 +529,8 @@ void FlowBuffer::MarkOrCopyFrame()
#if DEBUG_FLOW_BUFFER
if ( message_complete_ )
{
fprintf(stderr, "%.6f frame complete: [%s]\n",
network_time(),
string((const char *) begin(),
(const char *) end()).c_str());
fprintf(stderr, "%.6f frame complete: [%s]\n", network_time(),
string((const char*)begin(), (const char*)end()).c_str());
}
#endif
}
@ -562,4 +549,4 @@ void FlowBuffer::AppendToBuffer(const_byteptr data, int len)
BINPAC_ASSERT(orig_data_begin_ <= orig_data_end_);
}
} // namespace binpac
} // namespace binpac

View file

@ -2,24 +2,29 @@
#define binpac_buffer_h
#include <sys/types.h>
#include "binpac.h"
namespace binpac {
namespace binpac
{
class FlowBuffer {
class FlowBuffer
{
public:
struct Policy {
struct Policy
{
int max_capacity;
int min_capacity;
int contract_threshold;
};
};
enum LineBreakStyle {
CR_OR_LF, // CR or LF or CRLF
STRICT_CRLF, // CR followed by LF
CR_LF_NUL, // CR or LF or CR-LF or CR-NUL
LINE_BREAKER, // User specified linebreaker
};
enum LineBreakStyle
{
CR_OR_LF, // CR or LF or CRLF
STRICT_CRLF, // CR followed by LF
CR_LF_NUL, // CR or LF or CR-LF or CR-NUL
LINE_BREAKER, // User specified linebreaker
};
FlowBuffer(LineBreakStyle linebreak_style = CR_OR_LF);
virtual ~FlowBuffer();
@ -39,13 +44,12 @@ public:
void DiscardData();
// Whether there is enough data for the frame
bool ready() const{ return message_complete_ || mode_ == UNKNOWN_MODE; }
bool ready() const { return message_complete_ || mode_ == UNKNOWN_MODE; }
inline const_byteptr begin() const
{
BINPAC_ASSERT(ready());
return ( buffer_n_ == 0 ) ?
orig_data_begin_ : buffer_;
return (buffer_n_ == 0) ? orig_data_begin_ : buffer_;
}
inline const_byteptr end() const
@ -64,18 +68,17 @@ public:
inline int data_length() const
{
if ( buffer_n_ > 0 )
if ( buffer_n_ > 0 )
return buffer_n_;
if ( frame_length_ < 0 ||
orig_data_begin_ + frame_length_ > orig_data_end_ )
if ( frame_length_ < 0 || orig_data_begin_ + frame_length_ > orig_data_end_ )
return orig_data_end_ - orig_data_begin_;
else
return frame_length_;
}
inline bool data_available() const
{
{
return buffer_n_ > 0 || orig_data_end_ > orig_data_begin_;
}
@ -87,22 +90,20 @@ public:
void GrowFrame(int new_frame_length);
int data_seq() const
{
int data_seq_at_orig_data_begin =
data_seq_at_orig_data_end_ -
(orig_data_end_ - orig_data_begin_);
{
int data_seq_at_orig_data_begin = data_seq_at_orig_data_end_ -
(orig_data_end_ - orig_data_begin_);
if ( buffer_n_ > 0 )
return data_seq_at_orig_data_begin;
else
return data_seq_at_orig_data_begin + data_length();
return data_seq_at_orig_data_begin + data_length();
}
bool eof() const { return eof_; }
bool eof() const { return eof_; }
void set_eof();
bool have_pending_request() const { return have_pending_request_; }
static void init(Policy p)
{ policy = p; }
static void init(Policy p) { policy = p; }
protected:
// Reset the buffer for a new message
@ -110,7 +111,7 @@ protected:
void ClearPreviousData();
// Expand the buffer to at least <length> bytes. If there
// Expand the buffer to at least <length> bytes. If there
// are contents in the existing buffer, copy them to the new
// buffer.
void ExpandBuffer(int length);
@ -128,8 +129,8 @@ protected:
void AppendToBuffer(const_byteptr data, int len);
// MarkOrCopy{Line,Frame} sets message_complete_ and
// marks begin/end pointers if a line/frame is complete,
// otherwise it clears message_complete_ and copies all
// marks begin/end pointers if a line/frame is complete,
// otherwise it clears message_complete_ and copies all
// the original data to the buffer.
//
void MarkOrCopy();
@ -140,41 +141,43 @@ protected:
void MarkOrCopyLine_STRICT_CRLF();
void MarkOrCopyLine_LINEBREAK();
int buffer_n_; // number of bytes in the buffer
int buffer_length_; // size of the buffer
int buffer_n_; // number of bytes in the buffer
int buffer_length_; // size of the buffer
unsigned char* buffer_;
bool message_complete_;
int frame_length_;
bool chunked_;
bool message_complete_;
int frame_length_;
bool chunked_;
const_byteptr orig_data_begin_, orig_data_end_;
LineBreakStyle linebreak_style_;
LineBreakStyle linebreak_style_default;
unsigned char linebreaker_;
unsigned char linebreaker_;
enum {
enum
{
UNKNOWN_MODE,
LINE_MODE,
FRAME_MODE,
} mode_;
} mode_;
enum {
enum
{
CR_OR_LF_0,
CR_OR_LF_1,
STRICT_CRLF_0,
STRICT_CRLF_1,
FRAME_0,
} state_;
} state_;
int data_seq_at_orig_data_end_;
bool eof_;
bool have_pending_request_;
int data_seq_at_orig_data_end_;
bool eof_;
bool have_pending_request_;
static Policy policy;
};
};
typedef FlowBuffer *flow_buffer_t;
typedef FlowBuffer* flow_buffer_t;
} // namespace binpac
} // namespace binpac
#endif // binpac_buffer_h

View file

@ -1,24 +1,25 @@
#define binpac_regex_h
#include <stdlib.h>
#include "binpac_bytestring.h"
namespace binpac
{
#include <stdlib.h>
std::string std_string(bytestring const *s)
namespace binpac
{
return std::string((const char *) s->begin(), (const char *) s->end());
std::string std_string(bytestring const* s)
{
return std::string((const char*)s->begin(), (const char*)s->end());
}
int bytestring_to_int(bytestring const *s)
int bytestring_to_int(bytestring const* s)
{
return atoi((const char *) s->begin());
return atoi((const char*)s->begin());
}
double bytestring_to_double(bytestring const *s)
double bytestring_to_double(bytestring const* s)
{
return atof((const char *) s->begin());
return atof((const char*)s->begin());
}
} // namespace binpac
} // namespace binpac

View file

@ -3,155 +3,118 @@
#include <string.h>
#include <string>
#include "binpac.h"
namespace binpac
{
{
template<class T> class datastring;
template <class T> class datastring;
template <class T>
class const_datastring
{
template <class T> class const_datastring
{
public:
const_datastring()
: begin_(0), end_(0)
{
}
const_datastring() : begin_(0), end_(0) { }
const_datastring(T const *data, int length)
: begin_(data), end_(data + length)
{
}
const_datastring(T const* data, int length) : begin_(data), end_(data + length) { }
const_datastring(const T *begin, const T *end)
: begin_(begin), end_(end)
{
}
const_datastring(const T* begin, const T* end) : begin_(begin), end_(end) { }
const_datastring(datastring<T> const &s)
: begin_(s.begin()), end_(s.end())
{
}
const_datastring(datastring<T> const& s) : begin_(s.begin()), end_(s.end()) { }
void init(const T *data, int length)
void init(const T* data, int length)
{
begin_ = data;
end_ = data + length;
}
T const *begin() const { return begin_; }
T const *end() const { return end_; }
int length() const { return end_ - begin_; }
T const* begin() const { return begin_; }
T const* end() const { return end_; }
int length() const { return end_ - begin_; }
T const &operator[](int index) const
{
return begin()[index];
}
T const& operator[](int index) const { return begin()[index]; }
bool operator==(const_datastring<T> const &s)
bool operator==(const_datastring<T> const& s)
{
if ( length() != s.length() )
return false;
return memcmp((const void *) begin(), (const void *) s.begin(),
sizeof(T) * length()) == 0;
return memcmp((const void*)begin(), (const void*)s.begin(), sizeof(T) * length()) == 0;
}
void set_begin(T const *begin) { begin_ = begin; }
void set_end(T const *end) { end_ = end; }
void set_begin(T const* begin) { begin_ = begin; }
void set_end(T const* end) { end_ = end; }
private:
T const *begin_;
T const *end_;
};
T const* begin_;
T const* end_;
};
typedef const_datastring<uint8> const_bytestring;
typedef const_datastring<uint8> const_bytestring;
template<class T>
class datastring
{
template <class T> class datastring
{
public:
datastring()
{
clear();
}
datastring() { clear(); }
datastring(T *data, int len)
{
set(data, len);
}
datastring(T* data, int len) { set(data, len); }
datastring(T const *begin, T const *end)
{
set_const(begin, end - begin);
}
datastring(T const* begin, T const* end) { set_const(begin, end - begin); }
datastring(datastring<T> const &x)
: data_(x.data()), length_(x.length())
{
}
datastring(datastring<T> const& x) : data_(x.data()), length_(x.length()) { }
explicit datastring(const_datastring<T> const &x)
{
set_const(x.begin(), x.length());
}
explicit datastring(const_datastring<T> const& x) { set_const(x.begin(), x.length()); }
datastring const &operator=(datastring<T> const &x)
datastring const& operator=(datastring<T> const& x)
{
BINPAC_ASSERT(!data_);
BINPAC_ASSERT(! data_);
set(x.data(), x.length());
return *this;
}
void init(T const *begin, int length)
void init(T const* begin, int length)
{
BINPAC_ASSERT(!data_);
BINPAC_ASSERT(! data_);
set_const(begin, length);
}
void clear()
{
data_ = 0; length_ = 0;
data_ = 0;
length_ = 0;
}
void free()
{
if ( data_ )
delete [] data_;
delete[] data_;
clear();
}
void clone()
{
set_const(begin(), length());
}
void clone() { set_const(begin(), length()); }
datastring const &operator=(const_datastring<T> const &x)
datastring const& operator=(const_datastring<T> const& x)
{
BINPAC_ASSERT(!data_);
BINPAC_ASSERT(! data_);
set_const(x.begin(), x.length());
return *this;
}
T const &operator[](int index) const
{
return begin()[index];
}
T const& operator[](int index) const { return begin()[index]; }
T *data() const { return data_; }
int length() const { return length_; }
T* data() const { return data_; }
int length() const { return length_; }
T const *begin() const { return data_; }
T const *end() const { return data_ + length_; }
T const* begin() const { return data_; }
T const* end() const { return data_ + length_; }
private:
void set(T *data, int len)
void set(T* data, int len)
{
data_ = data;
length_ = len;
}
void set_const(T const *data, int len)
void set_const(T const* data, int len)
{
length_ = len;
data_ = new T[len + 1];
@ -159,41 +122,39 @@ private:
data_[len] = 0;
}
T * data_;
T* data_;
int length_;
};
};
typedef datastring<uint8> bytestring;
inline const char *c_str(bytestring const &s)
inline const char* c_str(bytestring const& s)
{
return (const char *) s.begin();
return (const char*)s.begin();
}
inline std::string std_str(const_bytestring const &s)
inline std::string std_str(const_bytestring const& s)
{
return std::string((const char *) s.begin(), (const char *) s.end());
return std::string((const char*)s.begin(), (const char*)s.end());
}
inline bool operator==(bytestring const &s1, const char *s2)
inline bool operator==(bytestring const& s1, const char* s2)
{
return strcmp(c_str(s1), s2) == 0;
}
inline void get_pointers(const_bytestring const &s,
uint8 const **pbegin, uint8 const **pend)
inline void get_pointers(const_bytestring const& s, uint8 const** pbegin, uint8 const** pend)
{
*pbegin = s.begin();
*pend = s.end();
}
inline void get_pointers(bytestring const *s,
uint8 const **pbegin, uint8 const **pend)
inline void get_pointers(bytestring const* s, uint8 const** pbegin, uint8 const** pend)
{
*pbegin = s->begin();
*pend = s->end();
}
} // namespace binpac
} // namespace binpac
#endif // binpac_bytestring_h
#endif // binpac_bytestring_h

View file

@ -1,133 +1,120 @@
#ifndef binpac_exception_h
#define binpac_exception_h
#include <stdint.h>
#include <inttypes.h>
#include <stdint.h>
namespace binpac {
namespace binpac
{
class Exception
{
{
public:
Exception(const char* m = 0)
: msg_("binpac exception: ")
Exception(const char* m = 0) : msg_("binpac exception: ")
{
if ( m )
append(m);
// abort();
}
void append(string m) { msg_ += m; }
string msg() const { return msg_; }
const char* c_msg() const { return msg_.c_str(); }
void append(string m) { msg_ += m; }
string msg() const { return msg_; }
const char* c_msg() const { return msg_.c_str(); }
protected:
string msg_;
};
};
class ExceptionEnforceViolation : public Exception
{
{
public:
ExceptionEnforceViolation(const char* where)
{
append(binpac_fmt("&enforce violation : %s", where));
}
};
};
class ExceptionOutOfBound : public Exception
{
{
public:
ExceptionOutOfBound(const char* where, int len_needed, int len_given)
{
append(binpac_fmt("out_of_bound: %s: %d > %d",
where, len_needed, len_given));
append(binpac_fmt("out_of_bound: %s: %d > %d", where, len_needed, len_given));
}
};
};
class ExceptionInvalidCase : public Exception
{
{
public:
ExceptionInvalidCase(const char* location,
int64_t index,
const char *expected)
: location_(location),
index_(index),
expected_(expected)
ExceptionInvalidCase(const char* location, int64_t index, const char* expected)
: location_(location), index_(index), expected_(expected)
{
append(binpac_fmt("invalid case: %s: %" PRIi64 " (%s)",
location, index, expected));
append(binpac_fmt("invalid case: %s: %" PRIi64 " (%s)", location, index, expected));
}
protected:
const char* location_;
int64_t index_;
string expected_;
};
};
class ExceptionInvalidCaseIndex : public Exception
{
{
public:
ExceptionInvalidCaseIndex(const char* location,
int64_t index)
: location_(location),
index_(index)
ExceptionInvalidCaseIndex(const char* location, int64_t index)
: location_(location), index_(index)
{
append(binpac_fmt("invalid index for case: %s: %" PRIi64,
location, index));
append(binpac_fmt("invalid index for case: %s: %" PRIi64, location, index));
}
protected:
const char* location_;
int64_t index_;
};
};
class ExceptionInvalidOffset : public Exception
{
{
public:
ExceptionInvalidOffset(const char* location,
int min_offset, int offset)
: location_(location),
min_offset_(min_offset), offset_(offset)
ExceptionInvalidOffset(const char* location, int min_offset, int offset)
: location_(location), min_offset_(min_offset), offset_(offset)
{
append(binpac_fmt("invalid offset: %s: min_offset = %d, offset = %d",
location, min_offset, offset));
append(binpac_fmt("invalid offset: %s: min_offset = %d, offset = %d", location, min_offset,
offset));
}
protected:
const char* location_;
int min_offset_, offset_;
};
};
class ExceptionStringMismatch : public Exception
{
{
public:
ExceptionStringMismatch(const char* location,
const char *expected, const char *actual_data)
ExceptionStringMismatch(const char* location, const char* expected, const char* actual_data)
{
append(binpac_fmt("string mismatch at %s: \nexpected pattern: \"%s\"\nactual data: \"%s\"",
location, expected, actual_data));
location, expected, actual_data));
}
};
};
class ExceptionInvalidStringLength : public Exception
{
{
public:
ExceptionInvalidStringLength(const char* location, int len)
{
append(binpac_fmt("invalid length string: %s: %d",
location, len));
append(binpac_fmt("invalid length string: %s: %d", location, len));
}
};
};
class ExceptionFlowBufferAlloc : public Exception
{
{
public:
ExceptionFlowBufferAlloc(const char* reason)
{
append(binpac_fmt("flowbuffer allocation failed: %s", reason));
}
};
};
}
}
#endif // binpac_exception_h
#endif // binpac_exception_h

View file

@ -3,8 +3,9 @@
class RE_Matcher;
namespace binpac {
namespace binpac
{
std::vector<RE_Matcher*>* uncompiled_re_matchers = 0;
}
}

View file

@ -1,13 +1,17 @@
#ifndef binpac_regex_h
#define binpac_regex_h
#include "binpac.h"
#include "zeek/RE.h"
namespace zeek { class RE_Matcher; }
#include "binpac.h"
namespace zeek
{
class RE_Matcher;
}
namespace binpac
{
{
// Must be called before any binpac functionality is used.
//
@ -19,10 +23,10 @@ inline void init(FlowBuffer::Policy* fbp = 0);
// Internal vector recording not yet compiled matchers.
extern std::vector<zeek::RE_Matcher*>* uncompiled_re_matchers;
class RegExMatcher {
class RegExMatcher
{
public:
RegExMatcher(const char *pattern)
: pattern_(pattern)
RegExMatcher(const char* pattern) : pattern_(pattern)
{
if ( ! uncompiled_re_matchers )
uncompiled_re_matchers = new std::vector<zeek::RE_Matcher*>;
@ -31,16 +35,10 @@ public:
uncompiled_re_matchers->push_back(re_matcher_);
}
~RegExMatcher()
{
delete re_matcher_;
}
~RegExMatcher() { delete re_matcher_; }
// Returns the length of longest match, or -1 on mismatch.
int MatchPrefix(const_byteptr data, int len)
{
return re_matcher_->MatchPrefix(data, len);
}
int MatchPrefix(const_byteptr data, int len) { return re_matcher_->MatchPrefix(data, len); }
private:
friend void ::binpac::init(FlowBuffer::Policy*);
@ -49,8 +47,8 @@ private:
static void init();
string pattern_;
zeek::RE_Matcher *re_matcher_;
};
zeek::RE_Matcher* re_matcher_;
};
inline void RegExMatcher::init()
{
@ -77,6 +75,6 @@ inline void init(FlowBuffer::Policy* fbp)
FlowBuffer::init(*fbp);
}
} // namespace binpac
} // namespace binpac
#endif // binpac_regex_h
#endif // binpac_regex_h