aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/nfagraph/ng_literal_analysis.h
blob: 6bb87556102d5225ba1141eb026e9e8f72c57470 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (c) 2015-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/** \file
 * \brief Literal analysis and scoring.
 */

#ifndef NG_LITERAL_ANALYSIS_H
#define NG_LITERAL_ANALYSIS_H

#include <set>
#include <vector>

#include "ng_holder.h"
#include "util/ue2string.h"

namespace ue2 {

#define NO_LITERAL_AT_EDGE_SCORE  10000000ULL
#define INVALID_EDGE_CAP         100000000ULL /* special-to-special score */

class NGHolder;

/**
 * Fetch the literal set for a given vertex, returning it in \p s. Note: does
 * NOT take into account any constraints due to streaming mode requirements.
 *
 * if only_first_encounter is requested, the output set may drop literals
 * generated by revisiting the destination vertex.
 */
std::set<ue2_literal> getLiteralSet(const NGHolder &g, const NFAVertex &v,
                                    bool only_first_encounter = true);
std::set<ue2_literal> getLiteralSet(const NGHolder &g, const NFAEdge &e);

/**
 * Returns true if we are unable to use a mixed sensitivity literal in rose (as
 * our literal matchers are generally either case sensitive or not).
 *
 * Shortish mixed sensitivity literals can be handled by confirm checks in rose
 * and are not flagged as bad.
 */
bool bad_mixed_sensitivity(const ue2_literal &s);

/**
 * Score all the edges in the given graph, returning them in \p scores indexed
 * by edge_index. */
std::vector<u64a> scoreEdges(const NGHolder &h,
                             const flat_set<NFAEdge> &known_bad = {});

/** Returns a score for a literal set. Lower scores are better. */
u64a scoreSet(const std::set<ue2_literal> &s);

/** Compress a literal set to fewer literals. */
u64a compressAndScore(std::set<ue2_literal> &s);

/**
 * Compress a literal set to fewer literals and replace any long mixed
 * sensitivity literals with supported literals.
 */
u64a sanitizeAndCompressAndScore(std::set<ue2_literal> &s);

bool splitOffLeadingLiteral(const NGHolder &g, ue2_literal *lit_out,
                            NGHolder *rhs);

bool getTrailingLiteral(const NGHolder &g, ue2_literal *lit_out);

/** \brief Returns true if the given literal is the only thing in the graph,
 * from (start or startDs) to accept. */
bool literalIsWholeGraph(const NGHolder &g, const ue2_literal &lit);

} // namespace ue2

#endif