1 | /* |
2 | * Copyright (c) 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 | #include "stream_compress.h" |
30 | |
31 | #include "state.h" |
32 | #include "nfa/nfa_internal.h" |
33 | #include "rose/rose_internal.h" |
34 | #include "util/multibit.h" |
35 | #include "util/multibit_compress.h" |
36 | #include "util/uniform_ops.h" |
37 | |
38 | #include <string.h> |
39 | |
40 | #define COPY_IN(p, sz) do { \ |
41 | assert(currOffset + sz <= buf_size); \ |
42 | memcpy(buf + currOffset, p, sz); \ |
43 | currOffset += sz; \ |
44 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
45 | } while (0); |
46 | |
47 | #define COPY_OUT(p, sz) do { \ |
48 | if (currOffset + sz > buf_size) { \ |
49 | return 0; \ |
50 | } \ |
51 | memcpy(p, buf + currOffset, sz); \ |
52 | currOffset += sz; \ |
53 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
54 | } while (0); |
55 | |
56 | #define SIZE_COPY_IN(p, sz) do { \ |
57 | currOffset += sz; \ |
58 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
59 | } while (0); |
60 | |
61 | #define COPY_MULTIBIT_IN(p, total_bits) do { \ |
62 | size_t sz; \ |
63 | STREAM_QUAL u8 *bits = (STREAM_QUAL u8 *)p; \ |
64 | BUF_QUAL u8 *comp = (BUF_QUAL u8 *)(buf + currOffset); \ |
65 | if (!mmbit_compress(bits, total_bits, comp, &sz, \ |
66 | buf_size - currOffset)) { \ |
67 | return 0; /* error */ \ |
68 | } \ |
69 | currOffset += sz; \ |
70 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
71 | } while (0); |
72 | |
73 | #define COPY_MULTIBIT_OUT(p, total_bits) do { \ |
74 | size_t sz; \ |
75 | STREAM_QUAL u8 *bits = (STREAM_QUAL u8 *)p; \ |
76 | BUF_QUAL u8 *comp = (BUF_QUAL u8 *)(buf + currOffset); \ |
77 | if (!mmbit_decompress(bits, total_bits, comp, &sz, \ |
78 | buf_size - currOffset)) { \ |
79 | return 0; /* error */ \ |
80 | } \ |
81 | currOffset += sz; \ |
82 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
83 | } while (0); |
84 | |
85 | #define COPY_MULTIBIT_SIZE(p, total_bits) do { \ |
86 | STREAM_QUAL u8 *bits = (STREAM_QUAL u8 *)p; \ |
87 | size_t sz = mmbit_compsize(bits, total_bits); \ |
88 | currOffset += sz; \ |
89 | DEBUG_PRINTF("co = %zu\n", currOffset); \ |
90 | } while (0); |
91 | |
92 | #define COPY COPY_OUT |
93 | #define COPY_MULTIBIT COPY_MULTIBIT_OUT |
94 | #define ASSIGN(lhs, rhs) do { lhs = rhs; } while (0) |
95 | #define FN_SUFFIX expand |
96 | #define STREAM_QUAL |
97 | #define BUF_QUAL const |
98 | #include "stream_compress_impl.h" |
99 | |
100 | int expand_stream(struct hs_stream *stream, const struct RoseEngine *rose, |
101 | const char *buf, size_t buf_size) { |
102 | return sc_expand(rose, stream, buf, buf_size); |
103 | } |
104 | |
105 | #define COPY COPY_IN |
106 | #define COPY_MULTIBIT COPY_MULTIBIT_IN |
107 | #define ASSIGN(lhs, rhs) do { } while (0) |
108 | #define FN_SUFFIX compress |
109 | #define STREAM_QUAL const |
110 | #define BUF_QUAL |
111 | #include "stream_compress_impl.h" |
112 | |
113 | size_t compress_stream(char *buf, size_t buf_size, |
114 | const struct RoseEngine *rose, |
115 | const struct hs_stream *stream) { |
116 | return sc_compress(rose, stream, buf, buf_size); |
117 | } |
118 | |
119 | #define COPY SIZE_COPY_IN |
120 | #define COPY_MULTIBIT COPY_MULTIBIT_SIZE |
121 | #define ASSIGN(lhs, rhs) do { } while (0) |
122 | #define FN_SUFFIX size |
123 | #define STREAM_QUAL const |
124 | #define BUF_QUAL UNUSED |
125 | #include "stream_compress_impl.h" |
126 | |
127 | size_t size_compress_stream(const struct RoseEngine *rose, |
128 | const struct hs_stream *stream) { |
129 | return sc_size(rose, stream, NULL, 0); |
130 | } |
131 | |