1/*
2 * Copyright (c) 2015-2019, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** \file
30 * \brief Hamster Wheel Literal Matcher: literal representation at build time.
31 */
32#include "hwlm_literal.h"
33#include "util/bitutils.h" // for CASE_BIT
34#include "util/compare.h" // for ourisalpha
35#include "util/ue2string.h" // for escapeString
36
37#include <algorithm>
38#include <iomanip>
39#include <sstream>
40
41using namespace std;
42
43namespace ue2 {
44
45#ifdef DEBUG
46static UNUSED
47std::string dumpMask(const vector<u8> &v) {
48 ostringstream oss;
49 vector<u8>::const_iterator it, ite;
50 for (it = v.begin(), ite = v.end(); it != ite; ++it) {
51 oss << setfill('0') << setw(2) << hex << (unsigned int)*it;
52 }
53 return oss.str();
54}
55#endif
56
57bool maskIsConsistent(const std::string &s, bool nocase, const vector<u8> &msk,
58 const vector<u8> &cmp) {
59 string::const_reverse_iterator si = s.rbegin();
60 vector<u8>::const_reverse_iterator mi = msk.rbegin(), ci = cmp.rbegin();
61
62 for (; si != s.rend() && mi != msk.rend(); ++si, ++mi, ++ci) {
63 u8 c = *si, m = *mi, v = *ci;
64 if (nocase && ourisalpha(c)) {
65 m &= ~CASE_BIT;
66 v &= ~CASE_BIT;
67 }
68
69 assert(ci != cmp.rend());
70 if ((c & m) != v) {
71 DEBUG_PRINTF("c = %02hhx; *ci = %02hhx m =%02hhx\n", c, *ci, m);
72 DEBUG_PRINTF("s = %s; dist = %zd\n", s.c_str(), si - s.rbegin());
73 return false;
74 }
75 }
76
77 return true;
78}
79
80/** \brief Complete constructor, takes group information and msk/cmp.
81 *
82 * This constructor takes a msk/cmp pair. Both must be vectors of length <=
83 * \ref HWLM_MASKLEN. */
84hwlmLiteral::hwlmLiteral(const std::string &s_in, bool nocase_in,
85 bool noruns_in, u32 id_in, hwlm_group_t groups_in,
86 const vector<u8> &msk_in, const vector<u8> &cmp_in,
87 bool pure_in)
88 : s(s_in), id(id_in), nocase(nocase_in), noruns(noruns_in),
89 groups(groups_in), msk(msk_in), cmp(cmp_in), pure(pure_in) {
90 assert(s.size() <= HWLM_LITERAL_MAX_LEN);
91 assert(msk.size() <= HWLM_MASKLEN);
92 assert(msk.size() == cmp.size());
93
94 // If we've been handled a nocase literal, all letter characters must be
95 // upper-case.
96 if (nocase) {
97 upperString(s);
98 }
99
100 DEBUG_PRINTF("literal '%s'%s, msk=%s, cmp=%s\n", escapeString(s).c_str(),
101 nocase ? " (nocase)" : "", dumpMask(msk).c_str(),
102 dumpMask(cmp).c_str());
103
104
105 // Mask and compare vectors MUST be the same size.
106 assert(msk.size() == cmp.size());
107
108 // We must have been passed a msk/cmp that can be applied to s.
109 assert(maskIsConsistent(s, nocase, msk, cmp));
110
111 // In the name of good hygiene, zap msk/cmp if msk is all zeroes.
112 if (all_of(begin(msk), end(msk), [](u8 val) { return val == 0; })) {
113 msk.clear();
114 cmp.clear();
115 }
116}
117
118} // namespace ue2
119