-/* $Id: dbm_map.c,v 1.2 2016/07/20 00:23:14 schwarze Exp $ */
+/* $Id: dbm_map.c,v 1.8 2017/02/17 14:43:54 schwarze Exp $ */
/*
* Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
*
#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
goto fail;
magic = dbm_getint(0);
if (be32toh(*magic) != MANDOCDB_MAGIC) {
- warnx("dbm_map(%s): Bad initial magic %x (expected %x)",
- fname, 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;
}
}
max_offset = be32toh(*dbm_getint(3)) + sizeof(int32_t);
if (st.st_size != max_offset) {
- warnx("dbm_map(%s): Inconsistent file size %llu (expected %d)",
- fname, 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;
}
dbm_get(int32_t offset)
{
offset = be32toh(offset);
- if (offset < 0 || offset >= max_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;
int32_t
dbm_addr(const void *p)
{
- return htobe32((char *)p - dbm_base);
+ return htobe32((const char *)p - dbm_base);
}
int