]>
git.cameronkatri.com Git - mandoc.git/blob - dbm_map.c
1 /* $Id: dbm_map.c,v 1.8 2017/02/17 14:43:54 schwarze Exp $ */
3 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
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".
25 #include <sys/types.h>
30 #include <sys/endian.h>
32 #include <arpa/inet.h>
45 #include "mansearch.h"
49 static struct stat st
;
50 static char *dbm_base
;
52 static int32_t max_offset
;
55 * Open a disk-based database for read-only access.
56 * Validate the file format as far as it is not mandoc-specific.
57 * Return 0 on success. Return -1 and set errno on failure.
60 dbm_map(const char *fname
)
65 if ((ifd
= open(fname
, O_RDONLY
)) == -1)
67 if (fstat(ifd
, &st
) == -1)
70 warnx("dbm_map(%s): File too short", fname
);
74 if (st
.st_size
> INT32_MAX
) {
78 if ((dbm_base
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
,
79 ifd
, 0)) == MAP_FAILED
)
81 magic
= dbm_getint(0);
82 if (be32toh(*magic
) != MANDOCDB_MAGIC
) {
83 if (strncmp(dbm_base
, "SQLite format 3", 15))
85 "Bad initial magic %x (expected %x)",
86 fname
, be32toh(*magic
), MANDOCDB_MAGIC
);
89 "Obsolete format based on SQLite 3",
94 magic
= dbm_getint(1);
95 if (be32toh(*magic
) != MANDOCDB_VERSION
) {
96 warnx("dbm_map(%s): Bad version number %d (expected %d)",
97 fname
, be32toh(*magic
), MANDOCDB_VERSION
);
101 max_offset
= be32toh(*dbm_getint(3)) + sizeof(int32_t);
102 if (st
.st_size
!= max_offset
) {
103 warnx("dbm_map(%s): Inconsistent file size %lld (expected %d)",
104 fname
, (long long)st
.st_size
, max_offset
);
108 if ((magic
= dbm_get(*dbm_getint(3))) == NULL
) {
112 if (be32toh(*magic
) != MANDOCDB_MAGIC
) {
113 warnx("dbm_map(%s): Bad final magic %x (expected %x)",
114 fname
, be32toh(*magic
), MANDOCDB_MAGIC
);
130 if (munmap(dbm_base
, st
.st_size
) == -1)
131 warn("dbm_unmap: munmap");
132 if (close(ifd
) == -1)
133 warn("dbm_unmap: close");
134 dbm_base
= (char *)-1;
138 * Take a raw integer as it was read from the database.
139 * Interpret it as an offset into the database file
140 * and return a pointer to that place in the file.
143 dbm_get(int32_t offset
)
145 offset
= be32toh(offset
);
147 warnx("dbm_get: Database corrupt: offset %d", offset
);
150 if (offset
>= max_offset
) {
151 warnx("dbm_get: Database corrupt: offset %d > %d",
155 return dbm_base
+ offset
;
159 * Assume the database starts with some integers.
160 * Assume they are numbered starting from 0, increasing.
161 * Get a pointer to one with the number "offset".
164 dbm_getint(int32_t offset
)
166 return (int32_t *)dbm_base
+ offset
;
170 * The reverse of dbm_get().
171 * Take pointer into the database file
172 * and convert it to the raw integer
173 * that would be used to refer to that place in the file.
176 dbm_addr(const void *p
)
178 return htobe32((const char *)p
- dbm_base
);
182 dbm_match(const struct dbm_match
*match
, const char *str
)
184 switch (match
->type
) {
186 return strcmp(str
, match
->str
) == 0;
188 return strcasestr(str
, match
->str
) != NULL
;
190 return regexec(match
->re
, str
, 0, NULL
, 0) == 0;