1/*
2 * Copyright (c) 2015-2017, 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 Shared build code for DFAs (McClellan, Haig).
31 */
32
33#ifndef NG_MCCLELLAN_INTERNAL_H
34#define NG_MCCLELLAN_INTERNAL_H
35
36#include "ue2common.h"
37#include "nfa/mcclellancompile.h"
38#include "nfagraph/ng_holder.h"
39#include "util/charreach.h"
40#include "util/graph_range.h"
41#include "util/flat_containers.h"
42
43#include <boost/dynamic_bitset.hpp>
44
45#include <map>
46#include <vector>
47
48namespace ue2 {
49
50struct raw_dfa;
51
52/** Fills alpha, unalpha and returns alphabet size. */
53u16 buildAlphabetFromEquivSets(const std::vector<CharReach> &esets,
54 std::array<u16, ALPHABET_SIZE> &alpha,
55 std::array<u16, ALPHABET_SIZE> &unalpha);
56
57/** \brief Calculates an alphabet remapping based on the symbols which the
58 * graph discriminates on. Throws in some special DFA symbols as well. */
59void calculateAlphabet(const NGHolder &g, std::array<u16, ALPHABET_SIZE> &alpha,
60 std::array<u16, ALPHABET_SIZE> &unalpha, u16 *alphasize);
61
62void getFullTransitionFromState(const raw_dfa &n, u16 state,
63 u16 *out_table);
64
65/** produce a map of states on which it is valid to receive tops */
66void markToppableStarts(const NGHolder &g, const flat_set<NFAVertex> &unused,
67 bool single_trigger,
68 const std::vector<std::vector<CharReach>> &triggers,
69 boost::dynamic_bitset<> *out);
70
71/**
72 * \brief Returns a set of start vertices that will not participate in an
73 * implementation of this graph. These are either starts with no successors or
74 * starts which are redundant with startDs.
75 */
76flat_set<NFAVertex> getRedundantStarts(const NGHolder &g);
77
78template<typename autom>
79void transition_graph(autom &nfa, const std::vector<NFAVertex> &vByStateId,
80 const typename autom::StateSet &in,
81 typename autom::StateSet *next) {
82 typedef typename autom::StateSet StateSet;
83 const NGHolder &graph = nfa.graph;
84 const auto &unused = nfa.unused;
85 const auto &alpha = nfa.alpha;
86 const StateSet &squash = nfa.squash;
87 const std::map<u32, StateSet> &squash_mask = nfa.squash_mask;
88 const std::vector<CharReach> &cr_by_index = nfa.cr_by_index;
89
90 for (symbol_t s = 0; s < nfa.alphasize; s++) {
91 next[s].reset();
92 }
93
94 /* generate top transitions, false -> top = selfloop */
95 bool top_allowed = is_triggered(graph);
96
97 StateSet succ = nfa.dead;
98 for (size_t i = in.find_first(); i != in.npos; i = in.find_next(i)) {
99 NFAVertex u = vByStateId[i];
100
101 for (const auto &v : adjacent_vertices_range(u, graph)) {
102 if (contains(unused, v)) {
103 continue;
104 }
105 succ.set(graph[v].index);
106 }
107
108 if (top_allowed && !nfa.toppable.test(i)) {
109 /* we don't need to generate a top at this location as we are in
110 * an nfa state which cannot be on when a trigger arrives. */
111 top_allowed = false;
112 }
113 }
114
115 StateSet active_squash = succ & squash;
116 if (active_squash.any()) {
117 for (size_t j = active_squash.find_first(); j != active_squash.npos;
118 j = active_squash.find_next(j)) {
119 succ &= squash_mask.find(j)->second;
120 }
121 }
122
123 for (size_t j = succ.find_first(); j != succ.npos; j = succ.find_next(j)) {
124 const CharReach &cr = cr_by_index[j];
125 for (size_t s = cr.find_first(); s != cr.npos; s = cr.find_next(s)) {
126 next[s].set(j); /* already alpha'ed */
127 }
128 }
129
130 next[alpha[TOP]] = in;
131
132 if (top_allowed) {
133 /* we don't add in the anchored starts as the only case as the only
134 * time it is appropriate is if no characters have been consumed.*/
135 next[alpha[TOP]] |= nfa.initDS;
136
137 active_squash = next[alpha[TOP]] & squash;
138 if (active_squash.any()) {
139 for (size_t j = active_squash.find_first(); j != active_squash.npos;
140 j = active_squash.find_next(j)) {
141 next[alpha[TOP]] &= squash_mask.find(j)->second;
142 }
143 }
144 }
145}
146
147} // namespace ue2
148
149#endif
150