-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
181 lines (156 loc) · 3.92 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* search.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <string.h>
#include "kdumpid.h"
static void
compute_badchar(ssize_t *badchar, const unsigned char *s, ssize_t len)
{
size_t i = 1;
while (i < len)
badchar[*s++] = i++;
}
static void
compute_sfx(ssize_t *sfx, const unsigned char *s, ssize_t len)
{
ssize_t f, g, i;
sfx[len - 1] = len;
f = 0; /* bogus assignment to silence a warning */
g = len - 1;
for (i = len - 2; i >= 0; --i) {
if (i > g && sfx[i + len - 1 - f] < i - g)
sfx[i] = sfx[i + len - 1 - f];
else {
if (i < g)
g = i;
f = i;
while (g >= 0 && s[g] == s[g + len - 1 - f])
--g;
sfx[i] = f - g;
}
}
}
static void
compute_goodsfx(ssize_t *goodsfx, const unsigned char *s, ssize_t len)
{
ssize_t i, j, *sfx = goodsfx + len;
compute_sfx(sfx, s, len);
for (i = 0; i < len; ++i)
goodsfx[i] = len;
j = 0;
for (i = len - 1; i >= 0; --i)
if (sfx[i] == i + 1)
for (; j < len - 1 - i; ++j)
if (goodsfx[j] == len)
goodsfx[j] = len - 1 - i;
for (i = 0; i <= len - 2; ++i)
goodsfx[len - 1 - sfx[i]] = len - 1 - i;
}
/* A helper function for doing cpin forwards or backwards inside the
* find_bytestr() inner loop
*/
static inline void*
search_cpin(struct dump_desc *dd, void *buf, uint64_t addr, size_t len)
{
if (!dump_cpin(dd, buf, addr, len))
return buf;
else if (dd->flags & DIF_FORCE) {
memset(buf, 0, len);
return buf;
} else
return NULL;
}
/* Search for a constant byte string using the Boyer-Moore algorithm.
*/
static inline unsigned char*
search_buf(unsigned char *buf, size_t buflen,
const unsigned char *needle, size_t maxidx,
ssize_t *badchar, ssize_t *goodsfx)
{
if (!maxidx)
return memchr(buf, *needle, buflen);
while (buflen > maxidx) {
unsigned char *p;
ssize_t shift, i;
for (p = buf + maxidx, i = maxidx; i >= 0; --p, --i)
if (needle[i] != *p)
break;
if (i < 0)
return buf;
shift = i + 1 - badchar[*p];
if (shift < goodsfx[i])
shift = goodsfx[i];
buf += shift;
buflen -= shift;
}
return NULL;
}
/* Search for a constant byte string using the Boyer-Moore algorithm. */
uint64_t
dump_search_range(struct dump_desc *dd,
uint64_t start, uint64_t end,
const unsigned char *needle, size_t len)
{
void *dynalloc;
ssize_t *badchar, *goodsfx;
unsigned char *readbuf;
if (len > 1) {
dynalloc = calloc(sizeof(ssize_t) * (256 + 2*len)
+ 2*(len-1), 1);
if (!dynalloc)
return INVALID_ADDR;
badchar = dynalloc;
goodsfx = badchar + 256;
readbuf = dynalloc + sizeof(ssize_t) * (256 + 2*len);
compute_badchar(badchar, needle, len);
compute_goodsfx(goodsfx, needle, len);
} else {
dynalloc = NULL;
badchar = goodsfx = NULL;
readbuf = NULL;
}
--len; /* simplify offset computing */
while (start < end) {
off_t remain;
unsigned char *p, *q;
remain = dd->page_size - (start & (dd->page_size - 1));
if (remain > end - start)
remain = end - start;
if (remain > len) {
if (read_page(dd, start / dd->page_size)) {
if (! (dd->flags & DIF_FORCE))
break;
memset(dd->page, 0, dd->page_size);
}
p = dd->page + (start & (dd->page_size - 1));
} else {
remain += len;
p = search_cpin(dd, readbuf, start, remain);
if (!p)
break;
}
start += remain;
q = search_buf(p, remain, needle, len,
badchar, goodsfx);
if (q) {
if (dynalloc)
free(dynalloc);
return start + q - p - remain;
}
start -= len;
}
if (dynalloc)
free(dynalloc);
return INVALID_ADDR;
}