1 /*
2  * Copyright (c) 2018, 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 Scratch and associated data structures.
31  *
32  * This header gets pulled into many places (many deep, slow to compile
33  * places). Try to keep the included headers under control.
34  */
35 
36 #ifndef CH_SCRATCH_H_
37 #define CH_SCRATCH_H_
38 
39 #include "ch_common.h"
40 #include "ch_runtime.h"
41 
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46 
47 #define CH_SCRATCH_MAGIC 0x554F4259 //!< Magic number stored in \ref ch_scratch
48 
49 struct queue_item {
50     int from; /** \brief used to store the start location. */
51     int to; /** \brief used to store the current location. */
52     u32 id; /**< pattern index. */
53 };
54 
55 struct match_pq {
56     struct queue_item *item;
57     u32 size; /**< current size of the priority queue */
58 };
59 
60 /** \brief Information about a pattern stored at runtime when a match is
61  * encountered. */
62 struct ch_patterndata {
63     struct ch_capture *match; //!< buffered group info
64     u32 groupCount; //!< number of capturing groups
65     u32 scanStart; //!< start of match window (still to be single-scanned).
66 };
67 
68 /** \brief Scratch space header for Chimera. */
69 struct ch_scratch {
70     u32 magic; //!< must be \ref CH_SCRATCH_MAGIC
71     u8 in_use; /**< non-zero when being used by an API call. */
72     struct hs_scratch *multi_scratch; //!< for hyperscan scatch.
73     int *ovector; //!< maximally-sized ovector for PCRE usage.
74     struct ch_capture *captured; //!< max-sized capture group struct.
75     u8 *active; //!< active multibit.
76     struct ch_patterndata *patternData; //!< per-pattern match data, indexed by
77                                         // pattern ID.
78     struct match_pq pq; //!< priority queue to ensure matching ordering
79     u32 patternCount; //!< number of patterns, used to size active multibit
80     u32 activeSize;   //!< size of active multibit
81     u32 maxCaptureGroups; //!< largest num of capturing groups required
82     u32 scratchSize; //!< size of allocation
83     int ret;  //!< return value in Hyperscan callback
84     char *scratch_alloc; /* user allocated scratch object */
85 };
86 
87 /**
88  * \brief Mark scratch as in use.
89  *
90  * Returns non-zero if it was already in use, zero otherwise.
91  */
92 static really_inline
markScratchInUse(struct ch_scratch * scratch)93 char markScratchInUse(struct ch_scratch *scratch) {
94     DEBUG_PRINTF("marking scratch as in use\n");
95     assert(scratch && scratch->magic == CH_SCRATCH_MAGIC);
96     if (scratch->in_use) {
97         DEBUG_PRINTF("scratch already in use!\n");
98         return 1;
99     }
100     scratch->in_use = 1;
101     return 0;
102 }
103 
104 /**
105  * \brief Mark scratch as no longer in use.
106  */
107 static really_inline
unmarkScratchInUse(struct ch_scratch * scratch)108 void unmarkScratchInUse(struct ch_scratch *scratch) {
109     DEBUG_PRINTF("marking scratch as not in use\n");
110     assert(scratch && scratch->magic == CH_SCRATCH_MAGIC);
111     assert(scratch->in_use == 1);
112     scratch->in_use = 0;
113 }
114 
115 #ifdef __cplusplus
116 } /* extern "C" */
117 #endif
118 
119 #endif /* CH_SCRATCH_H_ */
120