From 085707078097f6def993b20a794001d6c06f1492 Mon Sep 17 00:00:00 2001 From: Gillen Daniel Date: Wed, 7 Sep 2011 12:20:24 +0100 Subject: [PATCH] Replacement mmap function for Win32. (Updates by RWMJ) --- configure.ac | 3 +++ lib/Makefile.am | 3 ++- lib/hivex-internal.h | 5 ++++ lib/hivex.c | 8 +++++- lib/mmap.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/mmap.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 lib/mmap.c create mode 100644 lib/mmap.h diff --git a/configure.ac b/configure.ac index d7520e7..9d04e35 100644 --- a/configure.ac +++ b/configure.ac @@ -133,6 +133,9 @@ AC_CHECK_SIZEOF([long]) dnl Headers. AC_CHECK_HEADERS([byteswap.h endian.h libintl.h]) +dnl Check for mmap +AC_REPLACE_FUNCS([mmap]) + dnl Functions. AC_CHECK_FUNCS([bindtextdomain]) diff --git a/lib/Makefile.am b/lib/Makefile.am index b6a35d2..cfd2e05 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -29,9 +29,10 @@ libhivex_la_SOURCES = \ hivex-internal.h \ byte_conversions.h \ gettext.h \ + mmap.h \ hivex.syms -libhivex_la_LIBADD = ../gnulib/lib/libgnu.la +libhivex_la_LIBADD = ../gnulib/lib/libgnu.la $(LTLIBOBJS) libhivex_la_LDFLAGS = \ -version-info 0:0:0 \ $(VERSION_SCRIPT_FLAGS)$(srcdir)/hivex.syms \ diff --git a/lib/hivex-internal.h b/lib/hivex-internal.h index dc23ba5..43b1a76 100644 --- a/lib/hivex-internal.h +++ b/lib/hivex-internal.h @@ -59,6 +59,11 @@ struct hive_h { /* For writing. */ size_t endblocks; /* Offset to next block allocation (0 if not allocated anything yet). */ + +#ifndef HAVE_MMAP + /* Internal data for mmap replacement */ + void *p_winmap; +#endif }; #define STREQ(a,b) (strcmp((a),(b)) == 0) diff --git a/lib/hivex.c b/lib/hivex.c index 5b3772b..bf1a860 100644 --- a/lib/hivex.c +++ b/lib/hivex.c @@ -30,10 +30,16 @@ #include #include #include -#include #include #include +#ifdef HAVE_MMAP +#include +#else +/* On systems without mmap (and munmap), use a replacement function. */ +#include "mmap.h" +#endif + #include "c-ctype.h" #include "full-read.h" #include "full-write.h" diff --git a/lib/mmap.c b/lib/mmap.c new file mode 100644 index 0000000..c68ec32 --- /dev/null +++ b/lib/mmap.c @@ -0,0 +1,69 @@ +/* mmap replacement for mingw. + * + * Copyright (C) 2011 by Daniel Gillen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#include "hivex.h" +#include "hivex-internal.h" +#include "mmap.h" + +#include +#include + +void * +hivex__rpl_mmap (hive_h *h, + void *p_addr, size_t len, int prot, int flags, int fd, off_t offset) +{ + void *p_map; + + // Check parameters for unsupported values + if (p_addr != NULL) + return MAP_FAILED; + if (prot != PROT_READ) + return MAP_FAILED; + if (flags != MAP_SHARED) + return MAP_FAILED; + + // Create file mapping + h->p_winmap = CreateFileMapping ((HANDLE)_get_osfhandle(fd), + NULL, PAGE_READONLY, 0, 0, NULL); + if (h->p_winmap == NULL) + return MAP_FAILED; + + // Create map view + p_map = MapViewOfFile (h->p_winmap, FILE_MAP_READ, 0, 0, len); + if (p_map == NULL) { + CloseHandle (h->p_winmap); + return MAP_FAILED; + } + + return p_map; +} + +int +hivex__rpl_munmap (hive_h *h, void *p_addr, size_t len) +{ + if (p_addr == NULL || h->p_winmap == NULL) + return -1; + + // Close map view + if (UnmapViewOfFile (p_addr) == 0) + return -1; + + // Close file mapping + if (CloseHandle (h->p_winmap) == 0) + return -1; + + h->p_winmap = NULL; + return 0; +} diff --git a/lib/mmap.h b/lib/mmap.h new file mode 100644 index 0000000..0a693f3 --- /dev/null +++ b/lib/mmap.h @@ -0,0 +1,74 @@ +/* mmap replacement for mingw. + * + * Copyright (C) 2011 by Daniel Gillen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#ifndef HIVEX_MMAP_H_ +#define HIVEX_MMAP_H_ + +#include +#include + +#include "hivex.h" +#include "hivex-internal.h" + +/* Hack to pass the hive handle to the replacement mmap and munmap + * functions. XXX + */ +#define mmap(a,b,c,d,e,f) hivex__rpl_mmap(h,(a),(b),(c),(d),(e),(f)) +#define munmap(a,b) hivex__rpl_munmap(h,(a),(b)) + +// Supported map protections. +#define PROT_READ 0x1 /* Page can be read. */ + +// Supported sharing types (must choose one and only one of these). +#define MAP_SHARED 0x01 /* Share changes. */ + +// Value that is returned when mapping failed +#define MAP_FAILED NULL + +/* + * hivex replacement mmap + * + * Parameters: + * h : Hive handle + * void *p_addr : Preferred starting address for the mapping. Unsupported + * and must be NULL. + * size_t len : Mapping length (From offset to offset+len-1). + * int prot : Flags that control what kind of access is permitted. + * Must be PROT_READ. + * int flags : Flags that control the nature of the map. Must be + * MAP_SHARED. + * int fd : File descriptor of file to be mapped. + * off_t offset : Mapping offset. + * + * Returns: + * Map address on success or MAP_FAILED on error. + */ +extern void *hivex__rpl_mmap (hive_h *h, void *p_addr, size_t len, int prot, int flags, int fd, off_t offset); + +/* + * hivex replacement munmap + * + * Parameters: + * h : Hive handle + * void *p_addr : Startaddress of mapping created with mmap + * size_t len : Lenght of mapping to be unmapped. Unsupported. The whole + * mapping will always be unmapped. + * + * Returns: + * 0 on success or -1 on error. + */ +extern int hivex__rpl_munmap (hive_h *h, void *p_addr, size_t len); + +#endif /* HIVEX_MMAP_H_ */ -- 1.8.3.1