]>
git.cameronkatri.com Git - mandoc.git/blob - dbm_map.c
1 /* $Id: dbm_map.c,v 1.2 2016/07/20 00:23:14 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>
39 #include "mansearch.h"
43 static struct stat st
;
44 static char *dbm_base
;
46 static int32_t max_offset
;
49 * Open a disk-based database for read-only access.
50 * Validate the file format as far as it is not mandoc-specific.
51 * Return 0 on success. Return -1 and set errno on failure.
54 dbm_map(const char *fname
)
59 if ((ifd
= open(fname
, O_RDONLY
)) == -1)
61 if (fstat(ifd
, &st
) == -1)
64 warnx("dbm_map(%s): File too short", fname
);
68 if (st
.st_size
> INT32_MAX
) {
72 if ((dbm_base
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
,
73 ifd
, 0)) == MAP_FAILED
)
75 magic
= dbm_getint(0);
76 if (be32toh(*magic
) != MANDOCDB_MAGIC
) {
77 warnx("dbm_map(%s): Bad initial magic %x (expected %x)",
78 fname
, be32toh(*magic
), MANDOCDB_MAGIC
);
82 magic
= dbm_getint(1);
83 if (be32toh(*magic
) != MANDOCDB_VERSION
) {
84 warnx("dbm_map(%s): Bad version number %d (expected %d)",
85 fname
, be32toh(*magic
), MANDOCDB_VERSION
);
89 max_offset
= be32toh(*dbm_getint(3)) + sizeof(int32_t);
90 if (st
.st_size
!= max_offset
) {
91 warnx("dbm_map(%s): Inconsistent file size %llu (expected %d)",
92 fname
, st
.st_size
, max_offset
);
96 if ((magic
= dbm_get(*dbm_getint(3))) == NULL
) {
100 if (be32toh(*magic
) != MANDOCDB_MAGIC
) {
101 warnx("dbm_map(%s): Bad final magic %x (expected %x)",
102 fname
, be32toh(*magic
), MANDOCDB_MAGIC
);
118 if (munmap(dbm_base
, st
.st_size
) == -1)
119 warn("dbm_unmap: munmap");
120 if (close(ifd
) == -1)
121 warn("dbm_unmap: close");
122 dbm_base
= (char *)-1;
126 * Take a raw integer as it was read from the database.
127 * Interpret it as an offset into the database file
128 * and return a pointer to that place in the file.
131 dbm_get(int32_t offset
)
133 offset
= be32toh(offset
);
134 if (offset
< 0 || offset
>= max_offset
) {
135 warnx("dbm_get: Database corrupt: offset %d > %d",
139 return dbm_base
+ offset
;
143 * Assume the database starts with some integers.
144 * Assume they are numbered starting from 0, increasing.
145 * Get a pointer to one with the number "offset".
148 dbm_getint(int32_t offset
)
150 return (int32_t *)dbm_base
+ offset
;
154 * The reverse of dbm_get().
155 * Take pointer into the database file
156 * and convert it to the raw integer
157 * that would be used to refer to that place in the file.
160 dbm_addr(const void *p
)
162 return htobe32((char *)p
- dbm_base
);
166 dbm_match(const struct dbm_match
*match
, const char *str
)
168 switch (match
->type
) {
170 return strcmp(str
, match
->str
) == 0;
172 return strcasestr(str
, match
->str
) != NULL
;
174 return regexec(match
->re
, str
, 0, NULL
, 0) == 0;