2 * Regular Pattern Analyzer Toolkit (RPA/Tk)
3 * Copyright (c) 2009-2012 Martin Stoilov
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Martin Stoilov <martin@rpasearch.com>
24 #include "rlib/rbuffer.h"
28 void rex_buffer_unmap_file(rbuffer_t *buf)
30 HANDLE hFile = INVALID_HANDLE_VALUE;
35 hFile = (HANDLE) buf->userdata;
36 hMapping = (HANDLE) buf->userdata1;
39 CloseHandle(hMapping);
40 if (hFile != INVALID_HANDLE_VALUE)
43 UnmapViewOfFile(buf->s);
48 rbuffer_t * rex_buffer_map_file(LPCTSTR pFileName)
51 char *pMappedView = 0;
52 HANDLE hFile = INVALID_HANDLE_VALUE;
54 unsigned __int64 fileSize;
57 buf = (rbuffer_t *)malloc(sizeof(rbuffer_t));
60 hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0);
61 if (INVALID_HANDLE_VALUE == hFile)
63 sizeLo = GetFileSize(hFile, &sizeHi);
64 fileSize = (UINT64)sizeLo | ((UINT64)sizeHi << 32);
65 hMapping = CreateFileMapping(hFile, 0, PAGE_READONLY, sizeHi, sizeLo, 0);
68 pMappedView = (char*)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
69 if (NULL == pMappedView)
71 buf->userdata = (void*)hFile;
72 buf->userdata1 = (void*)hMapping;
74 buf->size = (unsigned long)fileSize;
75 buf->alt_destroy = rex_buffer_unmap_file;
80 CloseHandle(hMapping);
81 if (hFile != INVALID_HANDLE_VALUE)
84 UnmapViewOfFile(pMappedView);