summaryrefslogtreecommitdiffstats
path: root/contrib/mdocml/dbm_map.c
blob: d158302bad6619164926de0f40ff5c19b5570989 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
/*	$Id: dbm_map.c,v 1.7 2016/10/22 10:09:27 schwarze Exp $ */
/*
 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Low-level routines for the map-based version
 * of the mandoc database, for read-only access.
 * The interface is defined in "dbm_map.h".
 */
#include "config.h"

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

#if HAVE_ENDIAN
#include <endian.h>
#elif HAVE_SYS_ENDIAN
#include <sys/endian.h>
#elif HAVE_NTOHL
#include <arpa/inet.h>
#endif
#if HAVE_ERR
#include <err.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "mansearch.h"
#include "dbm_map.h"
#include "dbm.h"

static struct stat	 st;
static char		*dbm_base;
static int		 ifd;
static int32_t		 max_offset;

/*
 * Open a disk-based database for read-only access.
 * Validate the file format as far as it is not mandoc-specific.
 * Return 0 on success.  Return -1 and set errno on failure.
 */
int
dbm_map(const char *fname)
{
	int		 save_errno;
	const int32_t	*magic;

	if ((ifd = open(fname, O_RDONLY)) == -1)
		return -1;
	if (fstat(ifd, &st) == -1)
		goto fail;
	if (st.st_size < 5) {
		warnx("dbm_map(%s): File too short", fname);
		errno = EFTYPE;
		goto fail;
	}
	if (st.st_size > INT32_MAX) {
		errno = EFBIG;
		goto fail;
	}
	if ((dbm_base = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
	    ifd, 0)) == MAP_FAILED)
		goto fail;
	magic = dbm_getint(0);
	if (be32toh(*magic) != MANDOCDB_MAGIC) {
		if (strncmp(dbm_base, "SQLite format 3", 15))
			warnx("dbm_map(%s): "
			    "Bad initial magic %x (expected %x)",
			    fname, be32toh(*magic), MANDOCDB_MAGIC);
		else
			warnx("dbm_map(%s): "
			    "Obsolete format based on SQLite 3",
			    fname);
		errno = EFTYPE;
		goto fail;
	}
	magic = dbm_getint(1);
	if (be32toh(*magic) != MANDOCDB_VERSION) {
		warnx("dbm_map(%s): Bad version number %d (expected %d)",
		    fname, be32toh(*magic), MANDOCDB_VERSION);
		errno = EFTYPE;
		goto fail;
	}
	max_offset = be32toh(*dbm_getint(3)) + sizeof(int32_t);
	if (st.st_size != max_offset) {
		warnx("dbm_map(%s): Inconsistent file size %lld (expected %d)",
		    fname, (long long)st.st_size, max_offset);
		errno = EFTYPE;
		goto fail;
	}
	if ((magic = dbm_get(*dbm_getint(3))) == NULL) {
		errno = EFTYPE;
		goto fail;
	}
	if (be32toh(*magic) != MANDOCDB_MAGIC) {
		warnx("dbm_map(%s): Bad final magic %x (expected %x)",
		    fname, be32toh(*magic), MANDOCDB_MAGIC);
		errno = EFTYPE;
		goto fail;
	}
	return 0;

fail:
	save_errno = errno;
	close(ifd);
	errno = save_errno;
	return -1;
}

void
dbm_unmap(void)
{
	if (munmap(dbm_base, st.st_size) == -1)
		warn("dbm_unmap: munmap");
	if (close(ifd) == -1)
		warn("dbm_unmap: close");
	dbm_base = (char *)-1;
}

/*
 * Take a raw integer as it was read from the database.
 * Interpret it as an offset into the database file
 * and return a pointer to that place in the file.
 */
void *
dbm_get(int32_t offset)
{
	offset = be32toh(offset);
	if (offset < 0) {
		warnx("dbm_get: Database corrupt: offset %d", offset);
		return NULL;
	}
	if (offset >= max_offset) {
		warnx("dbm_get: Database corrupt: offset %d > %d",
		    offset, max_offset);
		return NULL;
	}
	return dbm_base + offset;
}

/*
 * Assume the database starts with some integers.
 * Assume they are numbered starting from 0, increasing.
 * Get a pointer to one with the number "offset".
 */
int32_t *
dbm_getint(int32_t offset)
{
	return (int32_t *)dbm_base + offset;
}

/*
 * The reverse of dbm_get().
 * Take pointer into the database file
 * and convert it to the raw integer
 * that would be used to refer to that place in the file.
 */
int32_t
dbm_addr(const void *p)
{
	return htobe32((char *)p - dbm_base);
}

int
dbm_match(const struct dbm_match *match, const char *str)
{
	switch (match->type) {
	case DBM_EXACT:
		return strcmp(str, match->str) == 0;
	case DBM_SUB:
		return strcasestr(str, match->str) != NULL;
	case DBM_REGEX:
		return regexec(match->re, str, 0, NULL, 0) == 0;
	default:
		abort();
	}
}
OpenPOWER on IntegriCloud