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 Runtime code for hs_database manipulation.
31 */
32
33#ifndef DATABASE_H_D467FD6F343DDE
34#define DATABASE_H_D467FD6F343DDE
35
36#ifdef __cplusplus
37extern "C"
38{
39#endif
40
41#include "hs_compile.h" // for HS_MODE_ flags
42#include "hs_version.h"
43#include "ue2common.h"
44#include "util/arch.h"
45
46#define HS_DB_VERSION HS_VERSION_32BIT
47#define HS_DB_MAGIC (0xdbdbdbdbU)
48
49// Values in here cannot (easily) change - add new ones!
50
51// CPU type is the low 6 bits (we can't need more than 64, surely!)
52
53#define HS_PLATFORM_INTEL 1
54#define HS_PLATFORM_CPU_MASK 0x3F
55
56#define HS_PLATFORM_NOAVX2 (4<<13)
57#define HS_PLATFORM_NOAVX512 (8<<13)
58
59/** \brief Platform features bitmask. */
60typedef u64a platform_t;
61
62static UNUSED
63const platform_t hs_current_platform = {
64#if !defined(HAVE_AVX2)
65 HS_PLATFORM_NOAVX2 |
66#endif
67#if !defined(HAVE_AVX512)
68 HS_PLATFORM_NOAVX512 |
69#endif
70 0,
71};
72
73static UNUSED
74const platform_t hs_current_platform_no_avx2 = {
75 HS_PLATFORM_NOAVX2 |
76 HS_PLATFORM_NOAVX512 |
77 0,
78};
79
80static UNUSED
81const platform_t hs_current_platform_no_avx512 = {
82 HS_PLATFORM_NOAVX512 |
83 0,
84};
85
86/*
87 * a header to enclose the actual bytecode - useful for keeping info about the
88 * compiled data.
89 */
90struct hs_database {
91 u32 magic;
92 u32 version;
93 u32 length;
94 u64a platform;
95 u32 crc32;
96 u32 reserved0;
97 u32 reserved1;
98 u32 bytecode; // offset relative to db start
99 u32 padding[16];
100 char bytes[];
101};
102
103static really_inline
104const void *hs_get_bytecode(const struct hs_database *db) {
105 return ((const char *)db + db->bytecode);
106}
107
108/**
109 * Cheap database sanity checks used in block mode scan calls and streaming
110 * mode open calls.
111 */
112static really_inline
113hs_error_t validDatabase(const hs_database_t *db) {
114 if (!db || db->magic != HS_DB_MAGIC) {
115 return HS_INVALID;
116 }
117 if (db->version != HS_DB_VERSION) {
118 return HS_DB_VERSION_ERROR;
119 }
120
121 return HS_SUCCESS;
122}
123
124hs_error_t dbIsValid(const struct hs_database *db);
125
126#ifdef __cplusplus
127} /* extern "C" */
128#endif
129
130#endif /* DATABASE_H_D467FD6F343DDE */
131