Skip to content

Commit

Permalink
Move schemas to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 committed May 23, 2024
1 parent 43a049f commit dd9176a
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 115 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Checks: '*,misc-const-correctness,-bugprone-reserved-identifier,-hicpp-signed-bitwise,-llvmlibc-restrict-system-libc-headers,-altera-unroll-loops,-hicpp-named-parameter,-cert-dcl37-c,-cert-dcl51-cpp,-read,-cppcoreguidelines-init-variables,-cppcoreguidelines-avoid-non-const-global-variables,-altera-id-dependent-backward-branch,-performance-no-int-to-ptr,-altera-struct-pack-align,-google-readability-casting,-modernize-use-trailing-return-type,-llvmlibc-implementation-in-namespace,-llvmlibc-callee-namespace,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-fuchsia-default-arguments-declarations,-fuchsia-overloaded-operator,-cppcoreguidelines-pro-type-union-access,-fuchsia-default-arguments-calls,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-google-readability-todo,-llvm-header-guard,-readability-function-cognitive-complexity,-readability-identifier-length,-cppcoreguidelines-owning-memory,-cert-err58-cpp,-fuchsia-statically-constructed-objects,-google-build-using-namespace,-hicpp-avoid-goto,-cppcoreguidelines-avoid-goto,-hicpp-no-array-decay,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-abseil-string-find-str-contains,-bugprone-unchecked-optional-access,-readability-use-anyofallof'
WarningsAsErrors: '*'
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
CheckOptions:
- key: readability-function-cognitive-complexity.Threshold
value: 35
Expand Down
5 changes: 3 additions & 2 deletions src/builder/processor_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ template <> struct typed_processor_builder<generator::extract_schema> {
std::shared_ptr<base_processor> build(const auto &spec, const auto &scanners)
{
auto ref_scanners = references_to_scanners(spec.scanners, scanners);
return std::make_shared<processor<generator_type>>(spec.id, generator_type{}, spec.expr,
spec.mappings, std::move(ref_scanners), spec.evaluate, spec.output);
return std::make_shared<processor<generator_type>>(spec.id,
generator_type{std::move(ref_scanners)}, spec.expr, spec.mappings, spec.evaluate,
spec.output);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/builder/processor_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "indexer.hpp"
#include "parser/specification.hpp"
#include "processor.hpp"
#include "processor/base.hpp"

namespace ddwaf {

Expand Down
3 changes: 1 addition & 2 deletions src/generator/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class base {
base &operator=(const base &) = default;
base &operator=(base &&) = default;

virtual ddwaf_object generate(const ddwaf_object *input,
const std::set<const scanner *> &scanners, ddwaf::timer &deadline) const = 0;
virtual ddwaf_object generate(const ddwaf_object *input, ddwaf::timer &deadline) const = 0;
};

} // namespace ddwaf::generator
5 changes: 2 additions & 3 deletions src/generator/extract_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,13 @@ ddwaf_object generate(

} // namespace schema

ddwaf_object extract_schema::generate(const ddwaf_object *input,
const std::set<const scanner *> &scanners, ddwaf::timer &deadline) const
ddwaf_object extract_schema::generate(const ddwaf_object *input, ddwaf::timer &deadline) const
{
if (input == nullptr) {
return {};
}

return schema::generate(input, scanners, deadline);
return schema::generate(input, scanners_, deadline);
}

} // namespace ddwaf::generator
8 changes: 5 additions & 3 deletions src/generator/extract_schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ class extract_schema : public base {
static constexpr std::size_t max_array_nodes = 10;
static constexpr std::size_t max_record_nodes = 255;

extract_schema() = default;
explicit extract_schema(std::set<const scanner *> scanners) : scanners_(std::move(scanners)) {}
~extract_schema() override = default;
extract_schema(const extract_schema &) = delete;
extract_schema(extract_schema &&) = default;
extract_schema &operator=(const extract_schema &) = delete;
extract_schema &operator=(extract_schema &&) = default;

ddwaf_object generate(const ddwaf_object *input, const std::set<const scanner *> &scanners,
ddwaf::timer &deadline) const override;
ddwaf_object generate(const ddwaf_object *input, ddwaf::timer &deadline) const override;

protected:
std::set<const scanner *> scanners_;
};

} // namespace ddwaf::generator
2 changes: 1 addition & 1 deletion src/parser/specification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "exclusion/rule_filter.hpp"
#include "expression.hpp"
#include "parameter.hpp"
#include "processor.hpp"
#include "processor/base.hpp"
#include "rule.hpp"
#include "scanner.hpp"

Expand Down
16 changes: 6 additions & 10 deletions src/processor.hpp → src/processor/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "exception.hpp"
#include "expression.hpp"
#include "generator/base.hpp"
#include "generator/base.hpp" // IWYU pragma: keep
#include "object_store.hpp"
#include "utils.hpp"

Expand Down Expand Up @@ -53,18 +53,15 @@ class base_processor {
template <typename T> class processor : public base_processor {
public:
processor(std::string id, T &&generator, std::shared_ptr<expression> expr,
std::vector<processor_mapping> mappings, std::set<const scanner *> scanners, bool evaluate,
bool output)
std::vector<processor_mapping> mappings, bool evaluate, bool output)
: id_(std::move(id)), generator_(std::move(generator)), expr_(std::move(expr)),
mappings_(std::move(mappings)), scanners_(std::move(scanners)), evaluate_(evaluate),
output_(output)
mappings_(std::move(mappings)), evaluate_(evaluate), output_(output)
{}

processor(std::string id, std::shared_ptr<expression> expr,
std::vector<processor_mapping> mappings, std::set<const scanner *> scanners, bool evaluate,
bool output)
std::vector<processor_mapping> mappings, bool evaluate, bool output)
: id_(std::move(id)), generator_(), expr_(std::move(expr)), mappings_(std::move(mappings)),
scanners_(std::move(scanners)), evaluate_(evaluate), output_(output)
evaluate_(evaluate), output_(output)
{}

processor(const processor &) = delete;
Expand Down Expand Up @@ -109,7 +106,7 @@ template <typename T> class processor : public base_processor {
cache.generated.emplace(mapping.output.index);
}

auto object = generator_.generate(input, scanners_, deadline);
auto object = generator_.generate(input, deadline);
if (object.type == DDWAF_OBJ_INVALID) {
continue;
}
Expand Down Expand Up @@ -147,7 +144,6 @@ template <typename T> class processor : public base_processor {
T generator_;
std::shared_ptr<expression> expr_;
std::vector<processor_mapping> mappings_;
std::set<const scanner *> scanners_;
bool evaluate_{false};
bool output_{true};
};
Expand Down
2 changes: 1 addition & 1 deletion src/ruleset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "exclusion/input_filter.hpp"
#include "exclusion/rule_filter.hpp"
#include "obfuscator.hpp"
#include "processor.hpp"
#include "processor/base.hpp"
#include "rule.hpp"
#include "scanner.hpp"

Expand Down
Loading

0 comments on commit dd9176a

Please sign in to comment.