Move everything in util.h to zeek::util namespace.

This commit includes renaming a number of methods prefixed with bro_ to be prefixed with zeek_.
This commit is contained in:
Tim Wojtulewicz 2020-08-06 10:40:24 -07:00
parent 8862b585fa
commit 8d2d867a65
179 changed files with 1277 additions and 1033 deletions

View file

@ -20,11 +20,11 @@ BasicThread::BasicThread()
killed = false;
buf_len = STD_FMT_BUF_LEN;
buf = (char*) safe_malloc(buf_len);
buf = (char*) zeek::util::safe_malloc(buf_len);
strerr_buffer = nullptr;
name = copy_string(fmt("thread-%" PRIu64, ++thread_counter));
name = zeek::util::copy_string(zeek::util::fmt("thread-%" PRIu64, ++thread_counter));
thread_mgr->AddThread(this);
}
@ -41,13 +41,13 @@ BasicThread::~BasicThread()
void BasicThread::SetName(const char* arg_name)
{
delete [] name;
name = copy_string(arg_name);
name = zeek::util::copy_string(arg_name);
}
void BasicThread::SetOSName(const char* arg_name)
{
static_assert(std::is_same<std::thread::native_handle_type, pthread_t>::value, "libstdc++ doesn't use pthread_t");
zeek::set_thread_name(arg_name, thread.native_handle());
zeek::util::set_thread_name(arg_name, thread.native_handle());
}
const char* BasicThread::Fmt(const char* format, ...)
@ -55,7 +55,7 @@ const char* BasicThread::Fmt(const char* format, ...)
if ( buf_len > 10 * STD_FMT_BUF_LEN )
{
// Shrink back to normal.
buf = (char*) safe_realloc(buf, STD_FMT_BUF_LEN);
buf = (char*) zeek::util::safe_realloc(buf, STD_FMT_BUF_LEN);
buf_len = STD_FMT_BUF_LEN;
}
@ -67,7 +67,7 @@ const char* BasicThread::Fmt(const char* format, ...)
if ( (unsigned int) n >= buf_len )
{ // Not enough room, grow the buffer.
buf_len = n + 32;
buf = (char*) safe_realloc(buf, buf_len);
buf = (char*) zeek::util::safe_realloc(buf, buf_len);
// Is it portable to restart?
va_start(al, format);
@ -83,7 +83,7 @@ const char* BasicThread::Strerror(int err)
if ( ! strerr_buffer )
strerr_buffer = new char[256];
bro_strerror_r(err, strerr_buffer, 256);
zeek::util::zeek_strerror_r(err, strerr_buffer, 256);
return strerr_buffer;
}

View file

@ -113,7 +113,7 @@ public:
bool Killed() const { return killed; }
/**
* A version of fmt() that the thread can safely use.
* A version of zeek::util::fmt() that the thread can safely use.
*
* This is safe to call from Run() but must not be used from any
* other thread than the current one.

View file

@ -182,7 +182,7 @@ bool Manager::SendEvent(MsgThread* thread, const std::string& name, const int nu
if ( convert_error )
return false;
else if ( handler )
zeek::event_mgr.Enqueue(handler, std::move(vl), SOURCE_LOCAL);
zeek::event_mgr.Enqueue(handler, std::move(vl), zeek::util::SOURCE_LOCAL);
return true;
}

View file

@ -78,7 +78,7 @@ public:
ReporterMessage(Type arg_type, MsgThread* thread, const char* arg_msg)
: OutputMessage<MsgThread>("ReporterMessage", thread)
{ type = arg_type; msg = copy_string(arg_msg); }
{ type = arg_type; msg = zeek::util::copy_string(arg_msg); }
~ReporterMessage() override { delete [] msg; }
@ -112,7 +112,7 @@ class DebugMessage final : public OutputMessage<MsgThread>
public:
DebugMessage(zeek::DebugStream arg_stream, MsgThread* thread, const char* arg_msg)
: OutputMessage<MsgThread>("DebugMessage", thread)
{ stream = arg_stream; msg = copy_string(arg_msg); }
{ stream = arg_stream; msg = zeek::util::copy_string(arg_msg); }
~DebugMessage() override { delete [] msg; }
@ -132,7 +132,7 @@ class SendEventMessage final : public OutputMessage<MsgThread> {
public:
SendEventMessage(MsgThread* thread, const char* name, const int num_vals, Value* *val)
: OutputMessage<MsgThread>("SendEvent", thread),
name(copy_string(name)), num_vals(num_vals), val(val) {}
name(zeek::util::copy_string(name)), num_vals(num_vals), val(val) {}
~SendEventMessage() override { delete [] name; }
@ -303,7 +303,7 @@ void MsgThread::Heartbeat()
if ( child_sent_finish )
return;
SendIn(new detail::HeartbeatMessage(this, zeek::net::network_time, current_time()));
SendIn(new detail::HeartbeatMessage(this, zeek::net::network_time, zeek::util::current_time()));
}
void MsgThread::Finished()

View file

@ -386,7 +386,7 @@ protected:
* mainly for debugging purposes.
*/
explicit Message(const char* arg_name)
{ name = copy_string(arg_name); }
{ name = zeek::util::copy_string(arg_name); }
private:
const char* name;

View file

@ -32,7 +32,7 @@ bool Field::Read(zeek::detail::SerializationFormat* fmt)
if ( ! fmt->Read(&tmp_secondary_name, "secondary_name") )
return false;
secondary_name = copy_string(tmp_secondary_name.c_str());
secondary_name = zeek::util::copy_string(tmp_secondary_name.c_str());
}
else
secondary_name = nullptr;
@ -45,7 +45,7 @@ bool Field::Read(zeek::detail::SerializationFormat* fmt)
if ( ! success )
return false;
name = copy_string(tmp_name.c_str());
name = zeek::util::copy_string(tmp_name.c_str());
type = static_cast<zeek::TypeTag>(t);
subtype = static_cast<zeek::TypeTag>(st);

View file

@ -29,16 +29,16 @@ struct Field {
* Constructor.
*/
Field(const char* name, const char* secondary_name, zeek::TypeTag type, zeek::TypeTag subtype, bool optional)
: name(name ? copy_string(name) : nullptr),
secondary_name(secondary_name ? copy_string(secondary_name) : nullptr),
: name(name ? zeek::util::copy_string(name) : nullptr),
secondary_name(secondary_name ? zeek::util::copy_string(secondary_name) : nullptr),
type(type), subtype(subtype), optional(optional) { }
/**
* Copy constructor.
*/
Field(const Field& other)
: name(other.name ? copy_string(other.name) : nullptr),
secondary_name(other.secondary_name ? copy_string(other.secondary_name) : nullptr),
: name(other.name ? zeek::util::copy_string(other.name) : nullptr),
secondary_name(other.secondary_name ? zeek::util::copy_string(other.secondary_name) : nullptr),
type(other.type), subtype(other.subtype), optional(other.optional) { }
~Field()

View file

@ -22,7 +22,7 @@ static inline bool escapeReservedContent(zeek::ODesc* desc, const string& reserv
return false;
char hex[4] = {'\\', 'x', '0', '0'};
bytetohex(*data, hex + 2);
zeek::util::bytetohex(*data, hex + 2);
desc->AddRaw(hex, 4);
desc->AddN(data + 1, size - 1);
return true;
@ -223,15 +223,15 @@ zeek::threading::Value* Ascii::ParseValue(const string& s, const string& name, z
case zeek::TYPE_ENUM:
case zeek::TYPE_STRING:
{
string unescaped = get_unescaped_string(s);
string unescaped = zeek::util::get_unescaped_string(s);
val->val.string_val.length = unescaped.size();
val->val.string_val.data = copy_string(unescaped.c_str());
val->val.string_val.data = zeek::util::copy_string(unescaped.c_str());
break;
}
case zeek::TYPE_BOOL:
{
auto stripped = strstrip(s);
auto stripped = zeek::util::strstrip(s);
if ( stripped == "T" || stripped == "1" )
val->val.int_val = 1;
else if ( stripped == "F" || stripped == "0" )
@ -267,20 +267,20 @@ zeek::threading::Value* Ascii::ParseValue(const string& s, const string& name, z
case zeek::TYPE_PORT:
{
auto stripped = strstrip(s);
auto stripped = zeek::util::strstrip(s);
val->val.port_val.proto = TRANSPORT_UNKNOWN;
pos = stripped.find('/');
string numberpart;
if ( pos != std::string::npos && stripped.length() > pos + 1 )
{
auto proto = stripped.substr(pos+1);
if ( strtolower(proto) == "tcp" )
if ( zeek::util::strtolower(proto) == "tcp" )
val->val.port_val.proto = TRANSPORT_TCP;
else if ( strtolower(proto) == "udp" )
else if ( zeek::util::strtolower(proto) == "udp" )
val->val.port_val.proto = TRANSPORT_UDP;
else if ( strtolower(proto) == "icmp" )
else if ( zeek::util::strtolower(proto) == "icmp" )
val->val.port_val.proto = TRANSPORT_ICMP;
else if ( strtolower(proto) == "unknown" )
else if ( zeek::util::strtolower(proto) == "unknown" )
val->val.port_val.proto = TRANSPORT_UNKNOWN;
else
GetThread()->Warning(GetThread()->Fmt("Port '%s' contained unknown protocol '%s'", s.c_str(), proto.c_str()));
@ -299,7 +299,7 @@ zeek::threading::Value* Ascii::ParseValue(const string& s, const string& name, z
case zeek::TYPE_SUBNET:
{
string unescaped = strstrip(get_unescaped_string(s));
string unescaped = zeek::util::strstrip(zeek::util::get_unescaped_string(s));
size_t pos = unescaped.find('/');
if ( pos == unescaped.npos )
{
@ -322,14 +322,14 @@ zeek::threading::Value* Ascii::ParseValue(const string& s, const string& name, z
case zeek::TYPE_ADDR:
{
string unescaped = strstrip(get_unescaped_string(s));
string unescaped = zeek::util::strstrip(zeek::util::get_unescaped_string(s));
val->val.addr_val = ParseAddr(unescaped);
break;
}
case zeek::TYPE_PATTERN:
{
string candidate = get_unescaped_string(s);
string candidate = zeek::util::get_unescaped_string(s);
// A string is a candidate pattern iff it begins and ends with
// a '/'. Rather or not the rest of the string is legal will
// be determined later when it is given to the RE engine.
@ -341,7 +341,7 @@ zeek::threading::Value* Ascii::ParseValue(const string& s, const string& name, z
// Remove the '/'s
candidate.erase(0, 1);
candidate.erase(candidate.size() - 1);
val->val.pattern_text_val = copy_string(candidate.c_str());
val->val.pattern_text_val = zeek::util::copy_string(candidate.c_str());
break;
}
}

View file

@ -174,7 +174,8 @@ void JSON::BuildJSON(NullDoubleWriter& writer, zeek::threading::Value* val, cons
case zeek::TYPE_FILE:
case zeek::TYPE_FUNC:
{
writer.String(json_escape_utf8(std::string(val->val.string_val.data, val->val.string_val.length)));
writer.String(zeek::util::json_escape_utf8(
std::string(val->val.string_val.data, val->val.string_val.length)));
break;
}