1 |
|
|
/* $OpenBSD: dbm_map.c,v 1.6 2017/02/09 18:26:17 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> |
4 |
|
|
* |
5 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
6 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
7 |
|
|
* copyright notice and this permission notice appear in all copies. |
8 |
|
|
* |
9 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 |
|
|
* |
17 |
|
|
* Low-level routines for the map-based version |
18 |
|
|
* of the mandoc database, for read-only access. |
19 |
|
|
* The interface is defined in "dbm_map.h". |
20 |
|
|
*/ |
21 |
|
|
#include <sys/mman.h> |
22 |
|
|
#include <sys/stat.h> |
23 |
|
|
#include <sys/types.h> |
24 |
|
|
|
25 |
|
|
#include <endian.h> |
26 |
|
|
#include <err.h> |
27 |
|
|
#include <errno.h> |
28 |
|
|
#include <fcntl.h> |
29 |
|
|
#include <regex.h> |
30 |
|
|
#include <stdint.h> |
31 |
|
|
#include <stdlib.h> |
32 |
|
|
#include <string.h> |
33 |
|
|
#include <unistd.h> |
34 |
|
|
|
35 |
|
|
#include "mansearch.h" |
36 |
|
|
#include "dbm_map.h" |
37 |
|
|
#include "dbm.h" |
38 |
|
|
|
39 |
|
|
static struct stat st; |
40 |
|
|
static char *dbm_base; |
41 |
|
|
static int ifd; |
42 |
|
|
static int32_t max_offset; |
43 |
|
|
|
44 |
|
|
/* |
45 |
|
|
* Open a disk-based database for read-only access. |
46 |
|
|
* Validate the file format as far as it is not mandoc-specific. |
47 |
|
|
* Return 0 on success. Return -1 and set errno on failure. |
48 |
|
|
*/ |
49 |
|
|
int |
50 |
|
|
dbm_map(const char *fname) |
51 |
|
|
{ |
52 |
|
|
int save_errno; |
53 |
|
|
const int32_t *magic; |
54 |
|
|
|
55 |
✓✓ |
1038 |
if ((ifd = open(fname, O_RDONLY)) == -1) |
56 |
|
23 |
return -1; |
57 |
✓✗ |
496 |
if (fstat(ifd, &st) == -1) |
58 |
|
|
goto fail; |
59 |
✓✓ |
496 |
if (st.st_size < 5) { |
60 |
|
5 |
warnx("dbm_map(%s): File too short", fname); |
61 |
|
5 |
errno = EFTYPE; |
62 |
|
5 |
goto fail; |
63 |
|
|
} |
64 |
✗✓ |
491 |
if (st.st_size > INT32_MAX) { |
65 |
|
|
errno = EFBIG; |
66 |
|
|
goto fail; |
67 |
|
|
} |
68 |
✓✗ |
982 |
if ((dbm_base = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, |
69 |
|
982 |
ifd, 0)) == MAP_FAILED) |
70 |
|
|
goto fail; |
71 |
|
491 |
magic = dbm_getint(0); |
72 |
✓✓ |
491 |
if (be32toh(*magic) != MANDOCDB_MAGIC) { |
73 |
✓✗ |
5 |
if (strncmp(dbm_base, "SQLite format 3", 15)) |
74 |
|
5 |
warnx("dbm_map(%s): " |
75 |
|
|
"Bad initial magic %x (expected %x)", |
76 |
|
5 |
fname, be32toh(*magic), MANDOCDB_MAGIC); |
77 |
|
|
else |
78 |
|
|
warnx("dbm_map(%s): " |
79 |
|
|
"Obsolete format based on SQLite 3", |
80 |
|
|
fname); |
81 |
|
5 |
errno = EFTYPE; |
82 |
|
5 |
goto fail; |
83 |
|
|
} |
84 |
|
486 |
magic = dbm_getint(1); |
85 |
✓✓ |
486 |
if (be32toh(*magic) != MANDOCDB_VERSION) { |
86 |
|
5 |
warnx("dbm_map(%s): Bad version number %d (expected %d)", |
87 |
|
|
fname, be32toh(*magic), MANDOCDB_VERSION); |
88 |
|
5 |
errno = EFTYPE; |
89 |
|
5 |
goto fail; |
90 |
|
|
} |
91 |
|
481 |
max_offset = be32toh(*dbm_getint(3)) + sizeof(int32_t); |
92 |
✓✓ |
481 |
if (st.st_size != max_offset) { |
93 |
|
5 |
warnx("dbm_map(%s): Inconsistent file size %lld (expected %d)", |
94 |
|
|
fname, (long long)st.st_size, max_offset); |
95 |
|
5 |
errno = EFTYPE; |
96 |
|
5 |
goto fail; |
97 |
|
|
} |
98 |
✗✓ |
476 |
if ((magic = dbm_get(*dbm_getint(3))) == NULL) { |
99 |
|
|
errno = EFTYPE; |
100 |
|
|
goto fail; |
101 |
|
|
} |
102 |
✓✓ |
476 |
if (be32toh(*magic) != MANDOCDB_MAGIC) { |
103 |
|
5 |
warnx("dbm_map(%s): Bad final magic %x (expected %x)", |
104 |
|
|
fname, be32toh(*magic), MANDOCDB_MAGIC); |
105 |
|
5 |
errno = EFTYPE; |
106 |
|
5 |
goto fail; |
107 |
|
|
} |
108 |
|
471 |
return 0; |
109 |
|
|
|
110 |
|
|
fail: |
111 |
|
25 |
save_errno = errno; |
112 |
|
25 |
close(ifd); |
113 |
|
25 |
errno = save_errno; |
114 |
|
25 |
return -1; |
115 |
|
519 |
} |
116 |
|
|
|
117 |
|
|
void |
118 |
|
|
dbm_unmap(void) |
119 |
|
|
{ |
120 |
✗✓ |
942 |
if (munmap(dbm_base, st.st_size) == -1) |
121 |
|
|
warn("dbm_unmap: munmap"); |
122 |
✗✓ |
471 |
if (close(ifd) == -1) |
123 |
|
|
warn("dbm_unmap: close"); |
124 |
|
471 |
dbm_base = (char *)-1; |
125 |
|
471 |
} |
126 |
|
|
|
127 |
|
|
/* |
128 |
|
|
* Take a raw integer as it was read from the database. |
129 |
|
|
* Interpret it as an offset into the database file |
130 |
|
|
* and return a pointer to that place in the file. |
131 |
|
|
*/ |
132 |
|
|
void * |
133 |
|
|
dbm_get(int32_t offset) |
134 |
|
|
{ |
135 |
|
103564 |
offset = be32toh(offset); |
136 |
✓✓ |
51782 |
if (offset < 0) { |
137 |
|
130 |
warnx("dbm_get: Database corrupt: offset %d", offset); |
138 |
|
130 |
return NULL; |
139 |
|
|
} |
140 |
✓✓ |
51652 |
if (offset >= max_offset) { |
141 |
|
10 |
warnx("dbm_get: Database corrupt: offset %d > %d", |
142 |
|
|
offset, max_offset); |
143 |
|
10 |
return NULL; |
144 |
|
|
} |
145 |
|
51642 |
return dbm_base + offset; |
146 |
|
51782 |
} |
147 |
|
|
|
148 |
|
|
/* |
149 |
|
|
* Assume the database starts with some integers. |
150 |
|
|
* Assume they are numbered starting from 0, increasing. |
151 |
|
|
* Get a pointer to one with the number "offset". |
152 |
|
|
*/ |
153 |
|
|
int32_t * |
154 |
|
|
dbm_getint(int32_t offset) |
155 |
|
|
{ |
156 |
|
7044 |
return (int32_t *)dbm_base + offset; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
/* |
160 |
|
|
* The reverse of dbm_get(). |
161 |
|
|
* Take pointer into the database file |
162 |
|
|
* and convert it to the raw integer |
163 |
|
|
* that would be used to refer to that place in the file. |
164 |
|
|
*/ |
165 |
|
|
int32_t |
166 |
|
|
dbm_addr(const void *p) |
167 |
|
|
{ |
168 |
|
7982 |
return htobe32((const char *)p - dbm_base); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
int |
172 |
|
|
dbm_match(const struct dbm_match *match, const char *str) |
173 |
|
|
{ |
174 |
✓✓✓✗
|
519374 |
switch (match->type) { |
175 |
|
|
case DBM_EXACT: |
176 |
|
259317 |
return strcmp(str, match->str) == 0; |
177 |
|
|
case DBM_SUB: |
178 |
|
345 |
return strcasestr(str, match->str) != NULL; |
179 |
|
|
case DBM_REGEX: |
180 |
|
25 |
return regexec(match->re, str, 0, NULL, 0) == 0; |
181 |
|
|
default: |
182 |
|
|
abort(); |
183 |
|
|
} |
184 |
|
259687 |
} |