GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/dbm_map.c Lines: 58 66 87.9 %
Date: 2017-11-13 Branches: 26 34 76.5 %

Line Branch Exec Source
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
408
	if ((ifd = open(fname, O_RDONLY)) == -1)
56
13
		return -1;
57
191
	if (fstat(ifd, &st) == -1)
58
		goto fail;
59
191
	if (st.st_size < 5) {
60
2
		warnx("dbm_map(%s): File too short", fname);
61
2
		errno = EFTYPE;
62
2
		goto fail;
63
	}
64
189
	if (st.st_size > INT32_MAX) {
65
		errno = EFBIG;
66
		goto fail;
67
	}
68
378
	if ((dbm_base = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
69
378
	    ifd, 0)) == MAP_FAILED)
70
		goto fail;
71
189
	magic = dbm_getint(0);
72
189
	if (be32toh(*magic) != MANDOCDB_MAGIC) {
73
2
		if (strncmp(dbm_base, "SQLite format 3", 15))
74
2
			warnx("dbm_map(%s): "
75
			    "Bad initial magic %x (expected %x)",
76
2
			    fname, be32toh(*magic), MANDOCDB_MAGIC);
77
		else
78
			warnx("dbm_map(%s): "
79
			    "Obsolete format based on SQLite 3",
80
			    fname);
81
2
		errno = EFTYPE;
82
2
		goto fail;
83
	}
84
187
	magic = dbm_getint(1);
85
187
	if (be32toh(*magic) != MANDOCDB_VERSION) {
86
2
		warnx("dbm_map(%s): Bad version number %d (expected %d)",
87
		    fname, be32toh(*magic), MANDOCDB_VERSION);
88
2
		errno = EFTYPE;
89
2
		goto fail;
90
	}
91
185
	max_offset = be32toh(*dbm_getint(3)) + sizeof(int32_t);
92
185
	if (st.st_size != max_offset) {
93
2
		warnx("dbm_map(%s): Inconsistent file size %lld (expected %d)",
94
		    fname, (long long)st.st_size, max_offset);
95
2
		errno = EFTYPE;
96
2
		goto fail;
97
	}
98
183
	if ((magic = dbm_get(*dbm_getint(3))) == NULL) {
99
		errno = EFTYPE;
100
		goto fail;
101
	}
102
183
	if (be32toh(*magic) != MANDOCDB_MAGIC) {
103
2
		warnx("dbm_map(%s): Bad final magic %x (expected %x)",
104
		    fname, be32toh(*magic), MANDOCDB_MAGIC);
105
2
		errno = EFTYPE;
106
2
		goto fail;
107
	}
108
181
	return 0;
109
110
fail:
111
10
	save_errno = errno;
112
10
	close(ifd);
113
10
	errno = save_errno;
114
10
	return -1;
115
204
}
116
117
void
118
dbm_unmap(void)
119
{
120
362
	if (munmap(dbm_base, st.st_size) == -1)
121
		warn("dbm_unmap: munmap");
122
181
	if (close(ifd) == -1)
123
		warn("dbm_unmap: close");
124
181
	dbm_base = (char *)-1;
125
181
}
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
22614
	offset = be32toh(offset);
136
11307
	if (offset < 0) {
137
52
		warnx("dbm_get: Database corrupt: offset %d", offset);
138
52
		return NULL;
139
	}
140
11255
	if (offset >= max_offset) {
141
4
		warnx("dbm_get: Database corrupt: offset %d > %d",
142
		    offset, max_offset);
143
4
		return NULL;
144
	}
145
11251
	return dbm_base + offset;
146
11307
}
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
2714
	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
514
	return htobe32((const char *)p - dbm_base);
169
}
170
171
int
172
dbm_match(const struct dbm_match *match, const char *str)
173
{
174

124844
	switch (match->type) {
175
	case DBM_EXACT:
176
62274
		return strcmp(str, match->str) == 0;
177
	case DBM_SUB:
178
138
		return strcasestr(str, match->str) != NULL;
179
	case DBM_REGEX:
180
10
		return regexec(match->re, str, 0, NULL, 0) == 0;
181
	default:
182
		abort();
183
	}
184
62422
}