Merge remote-tracking branch 'AmazingPP/topic/amazingpp/table-values-and-keys'

* AmazingPP/topic/amazingpp/table-values-and-keys:
  Add more test cases
  Add table_keys function
  Add table_values function
This commit is contained in:
Tim Wojtulewicz 2022-08-11 08:49:22 -07:00
commit 313e303fda
8 changed files with 124 additions and 1 deletions

View file

@ -1,3 +1,9 @@
5.1.0-dev.373 | 2022-08-11 08:49:22 -0700
* Add table_keys function (AmazingPP)
* Add table_values function (AmazingPP)
5.1.0-dev.368 | 2022-08-11 16:01:32 +0200 5.1.0-dev.368 | 2022-08-11 16:01:32 +0200
* GH-1678: Introduce global disabling_analyzer() hook to veto disable_analyzer() (Arne Welzel, Corelight) * GH-1678: Introduce global disabling_analyzer() hook to veto disable_analyzer() (Arne Welzel, Corelight)

View file

@ -1 +1 @@
5.1.0-dev.368 5.1.0-dev.373

View file

@ -932,6 +932,8 @@ public:
const PDict<TableEntryVal>* Get() const { return table_val; } const PDict<TableEntryVal>* Get() const { return table_val; }
const detail::CompositeHash* GetTableHash() const { return table_hash; }
// Returns the size of the table. // Returns the size of the table.
int Size() const; int Size() const;
int RecursiveSize() const; int RecursiveSize() const;

View file

@ -28,6 +28,7 @@
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/input.h" #include "zeek/input.h"
#include "zeek/Hash.h" #include "zeek/Hash.h"
#include "zeek/CompHash.h"
#include "zeek/packet_analysis/Manager.h" #include "zeek/packet_analysis/Manager.h"
using namespace std; using namespace std;
@ -1178,6 +1179,58 @@ function clear_table%(v: any%): any
return nullptr; return nullptr;
%} %}
## Gets all values from a table.
##
## t: The :zeek:type:`table`
##
## Returns: A ``vector of T`` of all the values in t.
##
## .. zeek:see:: table_keys
function table_values%(t: any%): any_vec
%{
if ( ! t->GetType()->IsTable() )
{
zeek::emit_builtin_error("table_values() requires a table argument");
return nullptr;
}
auto vt = zeek::make_intrusive<zeek::VectorType>(t->GetType()->AsTableType()->Yield());
auto vv = zeek::make_intrusive<zeek::VectorVal>(std::move(vt));
for ( const auto& iter : *t->AsTable() )
{
vv->Append(iter.value->GetVal());
}
return vv;
%}
## Gets all keys from a table.
##
## t: The :zeek:type:`table`
##
## Returns: A ``set of T`` of all the keys in t.
##
## .. zeek:see:: table_values
function table_keys%(t: any%): any
%{
if ( ! t->GetType()->IsTable() )
{
zeek::emit_builtin_error("table_keys() requires a table argument");
return nullptr;
}
auto tl = t->GetType()->AsTableType()->GetIndices();
auto st = zeek::make_intrusive<zeek::SetType>(std::move(tl), nullptr);
auto sv = zeek::make_intrusive<zeek::TableVal>(std::move(st));
auto th = t->AsTableVal()->GetTableHash();
for ( const auto& iter : *t->AsTable() )
{
sv->Assign(th->RecoverVals(*iter.GetHashKey()), nullptr);
}
return sv;
%}
## Gets all subnets that contain a given subnet from a set/table[subnet]. ## Gets all subnets that contain a given subnet from a set/table[subnet].
## ##
## search: the subnet to search for. ## search: the subnet to search for.

View file

@ -0,0 +1,13 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
{
http,
https
}
{
[a=<uninitialized>, b=7],
[a=10, b=5]
}
{
[1/tcp, test, T] ,
[2/tcp, example, F]
}

View file

@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[example, test]
[80/tcp, http, 443/tcp, https, 21/tcp, ftp, 23/tcp, telnet]

View file

@ -0,0 +1,29 @@
#
# @TEST-EXEC: zeek -b %INPUT > out
# @TEST-EXEC: btest-diff out
type MyRec: record {
a: count &optional;
b: count;
};
type MyTable: table[MyRec] of string;
event zeek_init()
{
local t1 = table(
["http"] = "http://www.google.com/",
["https"] = "https://www.google.com/");
local t2 = MyTable([[$a=10, $b=5]] = "b5", [[$b=7]] = "b7");
local t3: table[port, string, bool] of string = table(
[1/tcp, "test", T] = "test1",
[2/tcp, "example", F] = "test2");
local v1: set[string] = table_keys(t1);
local v2: set[MyRec] = table_keys(t2);
local v3: set[port, string, bool] = table_keys(t3);
print v1;
print v2;
print v3;
}

View file

@ -0,0 +1,17 @@
#
# @TEST-EXEC: zeek -b %INPUT > out
# @TEST-EXEC: btest-diff out
event zeek_init()
{
local t1: table[count] of string = table([5] = "test", [0] = "example");
local t2 = table(
["web"] = { [80/tcp, "http"], [443/tcp, "https"] },
["login"] = { [21/tcp, "ftp"], [23/tcp, "telnet"] });
local v1: vector of set[string] = table_values(t1);
local v2: vector of set[port, string] = table_values(t2);
print v1;
print v2;
}