2 * Load and unload nsscash files
4 * Copyright (C) 2019 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
28 #include <sys/types.h>
32 bool map_file(const char *path, struct file *f) {
33 // Fully initialize the struct for unmap_file() and other users
39 f->fd = open(path, O_RDONLY | O_CLOEXEC);
44 if (fstat(f->fd, &s)) {
47 f->size = (size_t)s.st_size;
49 void *x = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
50 if (x == MAP_FAILED) {
54 const struct header *h = x;
58 if (memcmp(h->magic, MAGIC, sizeof(h->magic))) {
62 // Only version 1 is supported at the moment; this will also prevent
63 // running on big-endian systems which is currently not possible
64 if (h->version != 1) {
72 int save_errno = errno;
79 void unmap_file(struct file *f) {
80 if (f->header != NULL) {
81 munmap((void *)f->header, f->size);