ruby: Add unit test for new RLenValue type
[hivex.git] / lib / mmap.c
1 /* mmap replacement for mingw.
2  *
3  * Copyright (C) 2011 by Daniel Gillen <gillen (dot) dan (at) pinguin (dot) lu>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation;
8  * version 2.1 of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  */
15
16 #include "hivex.h"
17 #include "hivex-internal.h"
18 #include "mmap.h"
19
20 #include <windows.h>
21 #include <io.h>
22
23 void *
24 hivex__rpl_mmap (hive_h *h,
25                  void *p_addr, size_t len, int prot, int flags, int fd, off_t offset)
26 {
27   void *p_map;
28
29   // Check parameters for unsupported values
30   if (p_addr != NULL)
31     return MAP_FAILED;
32   if (prot != PROT_READ)
33     return MAP_FAILED;
34   if (flags != MAP_SHARED)
35     return MAP_FAILED;
36
37   // Create file mapping
38   h->p_winmap = CreateFileMapping ((HANDLE)_get_osfhandle(fd),
39                                    NULL, PAGE_READONLY, 0, 0, NULL);
40   if (h->p_winmap == NULL)
41     return MAP_FAILED;
42
43   // Create map view
44   p_map = MapViewOfFile (h->p_winmap, FILE_MAP_READ, 0, 0, len);
45   if (p_map == NULL) {
46     CloseHandle (h->p_winmap);
47     return MAP_FAILED;
48   }
49
50   return p_map;
51 }
52
53 int
54 hivex__rpl_munmap (hive_h *h, void *p_addr, size_t len)
55 {
56   if (p_addr == NULL || h->p_winmap == NULL)
57     return -1;
58
59   // Close map view
60   if (UnmapViewOfFile (p_addr) == 0)
61     return -1;
62
63   // Close file mapping
64   if (CloseHandle (h->p_winmap) == 0)
65     return -1;
66
67   h->p_winmap = NULL;
68   return 0;
69 }