]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
noodle_build: update interface to use hwlmLiteral
authorJustin Viiret <justin.viiret@intel.com>
Fri, 22 Apr 2016 06:09:39 +0000 (16:09 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 18 May 2016 06:22:32 +0000 (16:22 +1000)
src/hwlm/hwlm_build.cpp
src/hwlm/noodle_build.cpp
src/hwlm/noodle_build.h
unit/internal/noodle.cpp

index 0de120bad6214efb6349a842b415cde23aca5cde..f86a70d264aea6ccdbf35c74be4d9a9479b6d2b2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -526,8 +526,7 @@ aligned_unique_ptr<HWLM> hwlmBuild(const vector<hwlmLiteral> &lits,
         DEBUG_PRINTF("build noodle table\n");
         engType = HWLM_ENGINE_NOOD;
         const hwlmLiteral &lit = lits.front();
-        auto noodle = noodBuildTable((const u8 *)lit.s.c_str(), lit.s.length(),
-                                     lit.nocase, lit.id);
+        auto noodle = noodBuildTable(lit);
         if (noodle) {
             engSize = noodSize(noodle.get());
         }
index 9e087211af08f660d48fd54a7c8ff3c5100cac52..d2b4e3f2091e9b3c6706b6447da79184aecd6667 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/** \file
+/**
+ * \file
  * \brief Noodle literal matcher: build code.
  */
-#include <cstring> // for memcpy
 
 #include "noodle_build.h"
+
+#include "hwlm_literal.h"
 #include "noodle_internal.h"
-#include "ue2common.h"
 #include "util/alloc.h"
 #include "util/compare.h"
 #include "util/verify_types.h"
+#include "ue2common.h"
+
+#include <cstring> // for memcpy
 
 namespace ue2 {
 
 static
-size_t findNoodFragOffset(const u8 *lit, size_t len, bool nocase) {
+size_t findNoodFragOffset(const hwlmLiteral &lit) {
+    const auto &s = lit.s;
+    const size_t len = lit.s.length();
+
     size_t offset = 0;
     for (size_t i = 0; i + 1 < len; i++) {
         int diff = 0;
-        const char c = lit[i];
-        const char d = lit[i + 1];
-        if (nocase && ourisalpha(c)) {
+        const char c = s[i];
+        const char d = s[i + 1];
+        if (lit.nocase && ourisalpha(c)) {
             diff = (mytoupper(c) != mytoupper(d));
         } else {
             diff = (c != d);
@@ -60,21 +67,24 @@ size_t findNoodFragOffset(const u8 *lit, size_t len, bool nocase) {
     return offset;
 }
 
-/** \brief Construct a Noodle matcher for the given literal. */
-aligned_unique_ptr<noodTable> noodBuildTable(const u8 *lit, size_t len,
-                                             bool nocase, u32 id) {
-    size_t noodle_len = sizeof(noodTable) + len;
-    aligned_unique_ptr<noodTable> n =
-        aligned_zmalloc_unique<noodTable>(noodle_len);
+aligned_unique_ptr<noodTable> noodBuildTable(const hwlmLiteral &lit) {
+    if (!lit.msk.empty()) {
+        DEBUG_PRINTF("noodle can't handle supplementary masks\n");
+        return nullptr;
+    }
+
+    const auto &s = lit.s;
+    size_t noodle_len = sizeof(noodTable) + s.length();
+    auto n = aligned_zmalloc_unique<noodTable>(noodle_len);
     assert(n);
 
-    size_t key_offset = findNoodFragOffset(lit, len, nocase);
+    size_t key_offset = findNoodFragOffset(lit);
 
-    n->id = id;
-    n->len = verify_u32(len);
+    n->id = lit.id;
+    n->len = verify_u32(s.length());
     n->key_offset = verify_u32(key_offset);
-    n->nocase = nocase ? 1 : 0;
-    memcpy(n->str, lit, len);
+    n->nocase = lit.nocase ? 1 : 0;
+    memcpy(n->str, s.c_str(), s.length());
 
     return n;
 }
index 3e8f5cb528580a9fd3d164acb2a77ad09c3e48dc..1a41695f736fa4fa61e00fcd4cfbf5b4a80f5b3c 100644 (file)
@@ -40,9 +40,10 @@ struct noodTable;
 
 namespace ue2 {
 
+struct hwlmLiteral;
+
 /** \brief Construct a Noodle matcher for the given literal. */
-ue2::aligned_unique_ptr<noodTable> noodBuildTable(const u8 *lit, size_t len,
-                                                  bool nocase, u32 id);
+ue2::aligned_unique_ptr<noodTable> noodBuildTable(const hwlmLiteral &lit);
 
 size_t noodSize(const noodTable *n);
 
index d1d9a1b47aedf17e4f606fc671ccfa1ea86edc54..5df662369b1b5142b8a7d4f816df53a285be8d2e 100644 (file)
@@ -32,6 +32,7 @@
 #include "hwlm/noodle_build.h"
 #include "hwlm/noodle_engine.h"
 #include "hwlm/hwlm.h"
+#include "hwlm/hwlm_literal.h"
 #include "util/alloc.h"
 #include "util/ue2string.h"
 
@@ -65,15 +66,11 @@ hwlmcb_rv_t hlmSimpleCallback(size_t from, size_t to, u32 id, void *context) {
 }
 
 static
-void noodleMatch(const u8 *data, size_t data_len, const char *lit,
+void noodleMatch(const u8 *data, size_t data_len, const char *lit_str,
                  size_t lit_len, char nocase, HWLMCallback cb, void *ctxt) {
-    // Coerce to upper-case if nocase.
-    std::string s(lit, lit_len);
-    if (nocase) {
-        upperString(s);
-    }
-
-    auto n = noodBuildTable((const u8 *)s.c_str(), s.length(), nocase, 0);
+    u32 id = 1000;
+    hwlmLiteral lit(std::string(lit_str, lit_len), nocase, id);
+    auto n = noodBuildTable(lit);
     ASSERT_TRUE(n != nullptr);
 
     hwlm_error_t rv;