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#ifndef FDR_CONFIRM_RUNTIME_H
30#define FDR_CONFIRM_RUNTIME_H
31
32#include "scratch.h"
33#include "fdr_internal.h"
34#include "fdr_loadval.h"
35#include "hwlm/hwlm.h"
36#include "ue2common.h"
37#include "util/bitutils.h"
38#include "util/compare.h"
39
40// this is ordinary confirmation function which runs through
41// the whole confirmation procedure
42static really_inline
43void confWithBit(const struct FDRConfirm *fdrc, const struct FDR_Runtime_Args *a,
44 size_t i, hwlmcb_rv_t *control, u32 *last_match,
45 u64a conf_key, u64a *conf, u8 bit) {
46 assert(i < a->len);
47 assert(i >= a->start_offset);
48 assert(ISALIGNED(fdrc));
49
50 const u8 * buf = a->buf;
51 u32 c = CONF_HASH_CALL(conf_key, fdrc->andmsk, fdrc->mult,
52 fdrc->nBits);
53 u32 start = getConfirmLitIndex(fdrc)[c];
54 if (likely(!start)) {
55 return;
56 }
57
58 const struct LitInfo *li
59 = (const struct LitInfo *)((const u8 *)fdrc + start);
60
61 struct hs_scratch *scratch = a->scratch;
62 assert(!scratch->fdr_conf);
63 scratch->fdr_conf = conf;
64 scratch->fdr_conf_offset = bit;
65 u8 oldNext; // initialized in loop
66 do {
67 assert(ISALIGNED(li));
68 scratch->pure = li->pure;
69
70 if (unlikely((conf_key & li->msk) != li->v)) {
71 goto out;
72 }
73
74 if ((*last_match == li->id) && (li->flags & FDR_LIT_FLAG_NOREPEAT)) {
75 goto out;
76 }
77
78 const u8 *loc = buf + i - li->size + 1;
79
80 if (loc < buf) {
81 u32 full_overhang = buf - loc;
82 size_t len_history = a->len_history;
83
84 // can't do a vectored confirm either if we don't have
85 // the bytes
86 if (full_overhang > len_history) {
87 goto out;
88 }
89 }
90 assert(li->size <= sizeof(CONF_TYPE));
91
92 if (unlikely(!(li->groups & *control))) {
93 goto out;
94 }
95
96 *last_match = li->id;
97 *control = a->cb(i, li->id, scratch);
98 out:
99 oldNext = li->next; // oldNext is either 0 or an 'adjust' value
100 li++;
101 } while (oldNext);
102 scratch->fdr_conf = NULL;
103 scratch->pure = 0;
104}
105
106#endif
107