1 | /* |
2 | * Copyright (c) 2016-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 | /* |
30 | * In order to use this macro, the following things need to be defined: |
31 | * |
32 | * - SHENG_IMPL (name of the Sheng implementation function) |
33 | * - DEAD_FUNC (name of the function checking for dead states) |
34 | * - ACCEPT_FUNC (name of the function checking for accept state) |
35 | * - STOP_AT_MATCH (can be 1 or 0, enable or disable stop at match) |
36 | */ |
37 | |
38 | /* byte-by-byte version. we don't do byte-by-byte death checking as it's |
39 | * pretty pointless to do it over a buffer that's at most 3 bytes long */ |
40 | static really_inline |
41 | char SHENG_IMPL(u8 *state, NfaCallback cb, void *ctxt, const struct sheng *s, |
42 | u8 *const cached_accept_state, ReportID *const cached_accept_id, |
43 | u8 single, u64a base_offset, const u8 *buf, const u8 *start, |
44 | const u8 *end, const u8 **scan_end) { |
45 | DEBUG_PRINTF("Starting DFA execution in state %u\n" , |
46 | *state & SHENG_STATE_MASK); |
47 | const u8 *cur_buf = start; |
48 | if (DEAD_FUNC(*state)) { |
49 | DEBUG_PRINTF("Dead on arrival\n" ); |
50 | *scan_end = end; |
51 | return MO_CONTINUE_MATCHING; |
52 | } |
53 | DEBUG_PRINTF("Scanning %lli bytes\n" , (s64a)(end - start)); |
54 | |
55 | m128 cur_state = set16x8(*state); |
56 | const m128 *masks = s->shuffle_masks; |
57 | |
58 | while (likely(cur_buf != end)) { |
59 | const u8 c = *cur_buf; |
60 | const m128 shuffle_mask = masks[c]; |
61 | cur_state = pshufb_m128(shuffle_mask, cur_state); |
62 | const u8 tmp = movd(cur_state); |
63 | |
64 | DEBUG_PRINTF("c: %02hhx '%c'\n" , c, ourisprint(c) ? c : '?'); |
65 | DEBUG_PRINTF("s: %u (hi: %u lo: %u)\n" , tmp, (tmp & 0xF0) >> 4, |
66 | tmp & 0xF); |
67 | |
68 | if (unlikely(ACCEPT_FUNC(tmp))) { |
69 | DEBUG_PRINTF("Accept state %u reached\n" , tmp & SHENG_STATE_MASK); |
70 | u64a match_offset = base_offset + (cur_buf - buf) + 1; |
71 | DEBUG_PRINTF("Match @ %llu\n" , match_offset); |
72 | if (STOP_AT_MATCH) { |
73 | DEBUG_PRINTF("Stopping at match @ %lli\n" , |
74 | (u64a)(cur_buf - start)); |
75 | *state = tmp; |
76 | *scan_end = cur_buf; |
77 | return MO_MATCHES_PENDING; |
78 | } |
79 | if (single) { |
80 | if (fireSingleReport(cb, ctxt, s->report, match_offset) == |
81 | MO_HALT_MATCHING) { |
82 | return MO_HALT_MATCHING; |
83 | } |
84 | } else { |
85 | if (fireReports(s, cb, ctxt, tmp, match_offset, |
86 | cached_accept_state, cached_accept_id, |
87 | 0) == MO_HALT_MATCHING) { |
88 | return MO_HALT_MATCHING; |
89 | } |
90 | } |
91 | } |
92 | cur_buf++; |
93 | } |
94 | *state = movd(cur_state); |
95 | *scan_end = cur_buf; |
96 | return MO_CONTINUE_MATCHING; |
97 | } |
98 | |