Annotation of mandoc/dbm_map.c, Revision 1.3
1.3 ! schwarze 1: /* $Id: dbm_map.c,v 1.2 2016/07/20 00:23:14 schwarze Exp $ */
1.1 schwarze 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: */
1.2 schwarze 21: #include "config.h"
22:
1.1 schwarze 23: #include <sys/mman.h>
24: #include <sys/stat.h>
25: #include <sys/types.h>
26:
1.3 ! schwarze 27: #if HAVE_ENDIAN
1.1 schwarze 28: #include <endian.h>
1.3 ! schwarze 29: #elif HAVE_SYS_ENDIAN
! 30: #include <sys/endian.h>
! 31: #elif HAVE_NTOHL
! 32: #include <arpa/inet.h>
! 33: #endif
1.2 schwarze 34: #if HAVE_ERR
1.1 schwarze 35: #include <err.h>
1.2 schwarze 36: #endif
1.1 schwarze 37: #include <errno.h>
38: #include <fcntl.h>
39: #include <regex.h>
40: #include <stdint.h>
41: #include <stdlib.h>
42: #include <string.h>
43: #include <unistd.h>
44:
45: #include "mansearch.h"
46: #include "dbm_map.h"
47: #include "dbm.h"
48:
49: static struct stat st;
50: static char *dbm_base;
51: static int ifd;
52: static int32_t max_offset;
53:
54: /*
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.
58: */
59: int
60: dbm_map(const char *fname)
61: {
62: int save_errno;
63: const int32_t *magic;
64:
65: if ((ifd = open(fname, O_RDONLY)) == -1)
66: return -1;
67: if (fstat(ifd, &st) == -1)
68: goto fail;
69: if (st.st_size < 5) {
70: warnx("dbm_map(%s): File too short", fname);
71: errno = EFTYPE;
72: goto fail;
73: }
74: if (st.st_size > INT32_MAX) {
75: errno = EFBIG;
76: goto fail;
77: }
78: if ((dbm_base = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
79: ifd, 0)) == MAP_FAILED)
80: goto fail;
81: magic = dbm_getint(0);
82: if (be32toh(*magic) != MANDOCDB_MAGIC) {
83: warnx("dbm_map(%s): Bad initial magic %x (expected %x)",
84: fname, be32toh(*magic), MANDOCDB_MAGIC);
85: errno = EFTYPE;
86: goto fail;
87: }
88: magic = dbm_getint(1);
89: if (be32toh(*magic) != MANDOCDB_VERSION) {
90: warnx("dbm_map(%s): Bad version number %d (expected %d)",
91: fname, be32toh(*magic), MANDOCDB_VERSION);
92: errno = EFTYPE;
93: goto fail;
94: }
95: max_offset = be32toh(*dbm_getint(3)) + sizeof(int32_t);
96: if (st.st_size != max_offset) {
97: warnx("dbm_map(%s): Inconsistent file size %llu (expected %d)",
98: fname, st.st_size, max_offset);
99: errno = EFTYPE;
100: goto fail;
101: }
102: if ((magic = dbm_get(*dbm_getint(3))) == NULL) {
103: errno = EFTYPE;
104: goto fail;
105: }
106: if (be32toh(*magic) != MANDOCDB_MAGIC) {
107: warnx("dbm_map(%s): Bad final magic %x (expected %x)",
108: fname, be32toh(*magic), MANDOCDB_MAGIC);
109: errno = EFTYPE;
110: goto fail;
111: }
112: return 0;
113:
114: fail:
115: save_errno = errno;
116: close(ifd);
117: errno = save_errno;
118: return -1;
119: }
120:
121: void
122: dbm_unmap(void)
123: {
124: if (munmap(dbm_base, st.st_size) == -1)
125: warn("dbm_unmap: munmap");
126: if (close(ifd) == -1)
127: warn("dbm_unmap: close");
128: dbm_base = (char *)-1;
129: }
130:
131: /*
132: * Take a raw integer as it was read from the database.
133: * Interpret it as an offset into the database file
134: * and return a pointer to that place in the file.
135: */
136: void *
137: dbm_get(int32_t offset)
138: {
139: offset = be32toh(offset);
140: if (offset < 0 || offset >= max_offset) {
141: warnx("dbm_get: Database corrupt: offset %d > %d",
142: offset, max_offset);
143: return NULL;
144: }
145: return dbm_base + offset;
146: }
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: 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: return htobe32((char *)p - dbm_base);
169: }
170:
171: int
172: dbm_match(const struct dbm_match *match, const char *str)
173: {
174: switch (match->type) {
175: case DBM_EXACT:
176: return strcmp(str, match->str) == 0;
177: case DBM_SUB:
178: return strcasestr(str, match->str) != NULL;
179: case DBM_REGEX:
180: return regexec(match->re, str, 0, NULL, 0) == 0;
181: default:
182: abort();
183: }
184: }
CVSweb