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 Acceleration: data structures and common definitions.
31  */
32 
33 #ifndef ACCEL_H
34 #define ACCEL_H
35 
36 #include "ue2common.h"
37 
38 /* run time defs */
39 #define BAD_ACCEL_DIST      4
40 #define SMALL_ACCEL_PENALTY 8
41 #define BIG_ACCEL_PENALTY   32
42 
43 /// Minimum length of the scan buffer for us to attempt acceleration.
44 #define ACCEL_MIN_LEN       16
45 
46 enum AccelType {
47     ACCEL_NONE,
48     ACCEL_VERM,
49     ACCEL_VERM_NOCASE,
50     ACCEL_DVERM,
51     ACCEL_DVERM_NOCASE,
52     ACCEL_RVERM,
53     ACCEL_RVERM_NOCASE,
54     ACCEL_RDVERM,
55     ACCEL_RDVERM_NOCASE,
56     ACCEL_REOD,
57     ACCEL_REOD_NOCASE,
58     ACCEL_RDEOD,
59     ACCEL_RDEOD_NOCASE,
60     ACCEL_SHUFTI,
61     ACCEL_DSHUFTI,
62     ACCEL_TRUFFLE,
63     ACCEL_RED_TAPE,
64     ACCEL_DVERM_MASKED,
65 };
66 
67 /** \brief Structure for accel framework. */
68 union AccelAux {
69     u8 accel_type;
70     struct {
71         u8 accel_type;
72         u8 offset;
73     } generic;
74     struct {
75         u8 accel_type;
76         u8 offset;
77         u8 c; // uppercase if nocase
78     } verm;
79     struct {
80         u8 accel_type;
81         u8 offset;
82         u8 c1; // uppercase if nocase
83         u8 c2; // uppercase if nocase
84         u8 m1; // masked variant
85         u8 m2; // masked variant
86     } dverm;
87     struct {
88         u8 accel_type;
89         u8 offset;
90         u8 c; // uppercase if nocase
91         u8 len;
92     } mverm;
93     struct {
94         u8 accel_type;
95         u8 offset;
96         u8 c; // uppercase if nocase
97         u8 len1;
98         u8 len2;
99     } mdverm;
100     struct {
101         u8 accel_type;
102         u8 offset;
103         m128 lo;
104         m128 hi;
105     } shufti;
106     struct {
107         u8 accel_type;
108         u8 offset;
109         m128 lo1;
110         m128 hi1;
111         m128 lo2;
112         m128 hi2;
113     } dshufti;
114     struct {
115         u8 accel_type;
116         u8 offset;
117         m128 mask1;
118         m128 mask2;
119     } truffle;
120 };
121 
122 /**
123  * Runs the specified acceleration scheme between c and c_end, returns a point
124  * such that the acceleration scheme does not match before.
125  */
126 const u8 *run_accel(const union AccelAux *accel, const u8 *c, const u8 *c_end);
127 
128 #endif
129