Change int_list in CCL.h to be a vector, fix uses of int_list to match

This commit is contained in:
Tim Wojtulewicz 2019-06-19 15:57:12 -07:00 committed by Jon Siwek
parent e25caa2666
commit a4e2cfa2be
7 changed files with 54 additions and 47 deletions

View file

@ -2,6 +2,8 @@
#include "zeek-config.h" #include "zeek-config.h"
#include <algorithm>
#include "CCL.h" #include "CCL.h"
#include "RE.h" #include "RE.h"
#include "DFA.h" #include "DFA.h"
@ -30,14 +32,14 @@ void CCL::Add(int sym)
ptr_compat_int sym_p = ptr_compat_int(sym); ptr_compat_int sym_p = ptr_compat_int(sym);
// Check to see if the character is already in the ccl. // Check to see if the character is already in the ccl.
for ( int i = 0; i < syms->length(); ++i ) for ( auto sym : *syms )
if ( (*syms)[i] == sym_p ) if ( sym == sym_p )
return; return;
syms->append(sym_p); syms->push_back(sym_p);
} }
void CCL::Sort() void CCL::Sort()
{ {
syms->sort(int_list_cmp); std::sort(syms->begin(), syms->end());
} }

View file

@ -3,9 +3,10 @@
#ifndef ccl_h #ifndef ccl_h
#define ccl_h #define ccl_h
#include <vector>
#include "List.h" #include "List.h"
typedef List<ptr_compat_int> int_list; typedef std::vector<ptr_compat_int> int_list;
class CCL { class CCL {
public: public:
@ -25,7 +26,7 @@ public:
{ delete syms; syms = new_syms; } { delete syms; syms = new_syms; }
unsigned int MemoryAllocation() const unsigned int MemoryAllocation() const
{ return padded_sizeof(*this) + syms->MemoryAllocation(); } { return padded_sizeof(*this) + padded_sizeof(*syms) + pad_size(syms->size() * sizeof(int_list::value_type)); }
protected: protected:
int_list* syms; int_list* syms;

View file

@ -108,11 +108,11 @@ DFA_State* DFA_State::ComputeXtion(int sym, DFA_Machine* machine)
void DFA_State::AppendIfNew(int sym, int_list* sym_list) void DFA_State::AppendIfNew(int sym, int_list* sym_list)
{ {
for ( int i = 0; i < sym_list->length(); ++i ) for ( auto value : *sym_list )
if ( (*sym_list)[i] == sym ) if ( value == sym )
return; return;
sym_list->append(sym); sym_list->push_back(sym);
} }
NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec) NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec)
@ -132,8 +132,8 @@ NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec)
if ( ccl->IsNegated() ) if ( ccl->IsNegated() )
{ {
int j; size_t j;
for ( j = 0; j < syms->length(); ++j ) for ( j = 0; j < syms->size(); ++j )
{ {
// Loop through (sorted) negated // Loop through (sorted) negated
// character class, which has // character class, which has
@ -143,19 +143,19 @@ NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec)
break; break;
} }
if ( j >= syms->length() || (*syms)[j] > ec_sym ) if ( j >= syms->size() || (*syms)[j] > ec_sym )
// Didn't find ec_sym in ccl. // Didn't find ec_sym in ccl.
n->AddXtionsTo(ns); n->AddXtionsTo(ns);
continue; continue;
} }
for ( int j = 0; j < syms->length(); ++j ) for ( auto sym : *syms )
{ {
if ( (*syms)[j] > ec_sym ) if ( sym > ec_sym )
break; break;
if ( (*syms)[j] == ec_sym ) if ( sym == ec_sym )
{ {
n->AddXtionsTo(ns); n->AddXtionsTo(ns);
break; break;

View file

@ -52,11 +52,10 @@ void EquivClass::ConvertCCL(CCL* ccl)
int_list* c_syms = ccl->Syms(); int_list* c_syms = ccl->Syms();
int_list* new_syms = new int_list; int_list* new_syms = new int_list;
for ( int i = 0; i < c_syms->length(); ++i ) for ( auto sym : *c_syms )
{ {
int sym = (*c_syms)[i];
if ( IsRep(sym) ) if ( IsRep(sym) )
new_syms->append(SymEquivClass(sym)); new_syms->push_back(SymEquivClass(sym));
} }
ccl->ReplaceSyms(new_syms); ccl->ReplaceSyms(new_syms);
@ -95,18 +94,18 @@ void EquivClass::CCL_Use(CCL* ccl)
} }
int_list* csyms = ccl->Syms(); int_list* csyms = ccl->Syms();
for ( int i = 0; i < csyms->length(); /* no increment */ ) for ( size_t i = 0; i < csyms->size(); /* no increment */ )
{ {
int sym = (*csyms)[i]; int sym = (*csyms)[i];
int old_ec = bck[sym]; int old_ec = bck[sym];
int new_ec = sym; int new_ec = sym;
int j = i + 1; size_t j = i + 1;
for ( int k = fwd[sym]; k && k < size; k = fwd[k] ) for ( int k = fwd[sym]; k && k < size; k = fwd[k] )
{ // look for the symbol in the character class { // look for the symbol in the character class
for ( ; j < csyms->length(); ++j ) for ( ; j < csyms->size(); ++j )
{ {
if ( (*csyms)[j] > k ) if ( (*csyms)[j] > k )
// Since the character class is sorted, // Since the character class is sorted,
@ -131,7 +130,7 @@ void EquivClass::CCL_Use(CCL* ccl)
} }
} }
if ( j < csyms->length() && (*csyms)[j] == k ) if ( j < csyms->size() && (*csyms)[j] == k )
// We broke out of the above loop by finding // We broke out of the above loop by finding
// an old companion - go to the next symbol. // an old companion - go to the next symbol.
continue; continue;
@ -154,7 +153,7 @@ void EquivClass::CCL_Use(CCL* ccl)
fwd[new_ec] = ec_nil; fwd[new_ec] = ec_nil;
// Find next ccl member to process. // Find next ccl member to process.
for ( ++i; i < csyms->length() && ccl_flags[i]; ++i ) for ( ++i; i < csyms->size() && ccl_flags[i]; ++i )
// Reset "doesn't need processing" flag. // Reset "doesn't need processing" flag.
ccl_flags[i] = 0; ccl_flags[i] = 0;
} }

View file

@ -149,7 +149,7 @@ int Specific_RE_Matcher::Compile(int lazy)
int Specific_RE_Matcher::CompileSet(const string_list& set, const int_list& idx) int Specific_RE_Matcher::CompileSet(const string_list& set, const int_list& idx)
{ {
if ( set.length() != idx.length() ) if ( (size_t)set.length() != idx.size() )
reporter->InternalError("compileset: lengths of sets differ"); reporter->InternalError("compileset: lengths of sets differ");
rem = this; rem = this;

View file

@ -25,6 +25,11 @@
uint32 RuleHdrTest::idcounter = 0; uint32 RuleHdrTest::idcounter = 0;
static bool is_member_of(const int_list& l, int_list::value_type v)
{
return std::find(l.begin(), l.end(), v) != l.end();
}
RuleHdrTest::RuleHdrTest(Prot arg_prot, uint32 arg_offset, uint32 arg_size, RuleHdrTest::RuleHdrTest(Prot arg_prot, uint32 arg_offset, uint32 arg_size,
Comp arg_comp, maskedvalue_list* arg_vals) Comp arg_comp, maskedvalue_list* arg_vals)
{ {
@ -349,7 +354,7 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs,
{ {
Rule::Pattern* p = r->patterns[j]; Rule::Pattern* p = r->patterns[j];
exprs[p->type].append(p->pattern); exprs[p->type].append(p->pattern);
ids[p->type].append(p->id); ids[p->type].push_back(p->id);
} }
} }
@ -374,7 +379,7 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs,
loop_over_list(child_exprs[i], j) loop_over_list(child_exprs[i], j)
{ {
exprs[i].append(child_exprs[i][j]); exprs[i].append(child_exprs[i][j]);
ids[i].append(child_ids[i][j]); ids[i].push_back(child_ids[i][j]);
} }
} }
} }
@ -406,7 +411,7 @@ void RuleMatcher::BuildPatternSets(RuleHdrTest::pattern_set_list* dst,
if ( i < exprs.length() ) if ( i < exprs.length() )
{ {
group_exprs.append(exprs[i]); group_exprs.append(exprs[i]);
group_ids.append(ids[i]); group_ids.push_back(ids[i]);
} }
if ( group_exprs.length() > sig_max_group_size || if ( group_exprs.length() > sig_max_group_size ||
@ -920,7 +925,7 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type,
DBG_LOG(DBG_RULES, "On current node"); DBG_LOG(DBG_RULES, "On current node");
// Skip if rule already fired for this connection. // Skip if rule already fired for this connection.
if ( state->matched_rules.is_member(r->Index()) ) if ( is_member_of(state->matched_rules, r->Index()) )
continue; continue;
// Remember that all patterns have matched. // Remember that all patterns have matched.
@ -971,7 +976,7 @@ void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos)
bool RuleMatcher::ExecRulePurely(Rule* r, BroString* s, bool RuleMatcher::ExecRulePurely(Rule* r, BroString* s,
RuleEndpointState* state, bool eos) RuleEndpointState* state, bool eos)
{ {
if ( state->matched_rules.is_member(r->Index()) ) if ( is_member_of(state->matched_rules, r->Index()) )
return false; return false;
DBG_LOG(DBG_RULES, "Checking rule %s purely", r->ID()); DBG_LOG(DBG_RULES, "Checking rule %s purely", r->ID());
@ -1014,7 +1019,7 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state,
if ( ! pc->negate ) if ( ! pc->negate )
{ {
if ( ! pc_state->matched_rules.is_member(pc->rule->Index()) ) if ( ! is_member_of(pc_state->matched_rules, pc->rule->Index()) )
// Precond rule has not matched yet. // Precond rule has not matched yet.
return false; return false;
} }
@ -1024,7 +1029,7 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state,
if ( ! eos ) if ( ! eos )
return false; return false;
if ( pc_state->matched_rules.is_member(pc->rule->Index()) ) if ( is_member_of(pc_state->matched_rules, pc->rule->Index()) )
return false; return false;
} }
} }
@ -1041,11 +1046,11 @@ void RuleMatcher::ExecRuleActions(Rule* r, RuleEndpointState* state,
const u_char* data, int len, bool eos) const u_char* data, int len, bool eos)
{ {
if ( state->opposite && if ( state->opposite &&
state->opposite->matched_rules.is_member(r->Index()) ) is_member_of(state->opposite->matched_rules, r->Index()) )
// We have already executed the actions. // We have already executed the actions.
return; return;
state->matched_rules.append(r->Index()); state->matched_rules.push_back(r->Index());
loop_over_list(r->actions, i) loop_over_list(r->actions, i)
r->actions[i]->DoAction(r, state, data, len); r->actions[i]->DoAction(r, state, data, len);
@ -1063,7 +1068,7 @@ void RuleMatcher::ExecRuleActions(Rule* r, RuleEndpointState* state,
void RuleMatcher::ExecRule(Rule* rule, RuleEndpointState* state, bool eos) void RuleMatcher::ExecRule(Rule* rule, RuleEndpointState* state, bool eos)
{ {
// Nothing to do if it has already matched. // Nothing to do if it has already matched.
if ( state->matched_rules.is_member(rule->Index()) ) if ( is_member_of(state->matched_rules, rule->Index()) )
return; return;
loop_over_list(state->hdr_tests, i) loop_over_list(state->hdr_tests, i)
@ -1135,10 +1140,10 @@ void RuleMatcher::PrintTreeDebug(RuleHdrTest* node)
RuleHdrTest::PatternSet* set = node->psets[i][j]; RuleHdrTest::PatternSet* set = node->psets[i][j];
fprintf(stderr, fprintf(stderr,
"[%d patterns in %s group %d from %d rules]\n", "[%d patterns in %s group %d from %zu rules]\n",
set->patterns.length(), set->patterns.length(),
Rule::TypeToString((Rule::PatternType) i), j, Rule::TypeToString((Rule::PatternType) i), j,
set->ids.length()); set->ids.size());
} }
} }
@ -1234,9 +1239,9 @@ void RuleMatcher::DumpStateStats(BroFile* f, RuleHdrTest* hdr_test)
set->re->DFA()->NumStates(), set->re->DFA()->NumStates(),
Rule::TypeToString((Rule::PatternType)i), j)); Rule::TypeToString((Rule::PatternType)i), j));
loop_over_list(set->ids, k) for ( auto id : set->ids )
{ {
Rule* r = Rule::rule_table[set->ids[k] - 1]; Rule* r = Rule::rule_table[id - 1];
f->Write(fmt("%s ", r->ID())); f->Write(fmt("%s ", r->ID()));
} }

View file

@ -756,7 +756,7 @@ void do_atif(Expr* expr)
if ( ! val->AsBool() ) if ( ! val->AsBool() )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
} }
@ -769,7 +769,7 @@ void do_atifdef(const char* id)
if ( ! (i = lookup_ID(id, current_module.c_str())) ) if ( ! (i = lookup_ID(id, current_module.c_str())) )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
@ -784,7 +784,7 @@ void do_atifndef(const char *id)
if ( (i = lookup_ID(id, current_module.c_str())) ) if ( (i = lookup_ID(id, current_module.c_str())) )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
@ -796,17 +796,17 @@ void do_atelse()
if ( current_depth == 0 ) if ( current_depth == 0 )
reporter->Error("@else without @if..."); reporter->Error("@else without @if...");
if ( if_stack.length() && current_depth > if_stack.last() ) if ( ! if_stack.empty() && current_depth > if_stack.back() )
return; return;
if ( YY_START == INITIAL ) if ( YY_START == INITIAL )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
else else
{ {
if_stack.get(); if_stack.pop_back();
BEGIN(INITIAL); BEGIN(INITIAL);
} }
} }
@ -816,10 +816,10 @@ void do_atendif()
if ( current_depth == 0 ) if ( current_depth == 0 )
reporter->Error("unbalanced @if... @endif"); reporter->Error("unbalanced @if... @endif");
if ( current_depth == if_stack.last() ) if ( current_depth == if_stack.back() )
{ {
BEGIN(INITIAL); BEGIN(INITIAL);
if_stack.get(); if_stack.pop_back();
} }
--current_depth; --current_depth;