1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
/*
* mmap.cpp
*
* This file is a part of NSIS.
*
* Copyright (C) 1999-2018 Nullsoft and Contributors
*
* Licensed under the zlib/libpng license (the "License");
* you may not use this file except in compliance with the License.
*
* Licence details can be found in the file COPYING.
*
* This software is provided 'as-is', without any express or implied
* warranty.
*/
#include "mmap.h"
#include <cstdio> // for f*
#include <cassert> // for assert
#include "tchar.h"
#include <limits.h>
#ifndef _WIN32
# include <sys/types.h> // for freebsd
# include <sys/mman.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#include "util.h"
// ========
// MMapFile
// ========
int MMapFile::m_iAllocationGranularity = 0;
MMapFile::MMapFile()
{
#ifdef _WIN32
m_hFile = INVALID_HANDLE_VALUE;
m_hFileMap = NULL;
#else
m_hFile = NULL;
m_hFileDesc = -1;
#endif
m_pView = NULL;
m_iSize = 0;
m_bReadOnly = FALSE;
m_bTempHandle = FALSE;
if (!m_iAllocationGranularity)
{
#ifdef _WIN32
SYSTEM_INFO si;
GetSystemInfo(&si);
m_iAllocationGranularity = (int) si.dwAllocationGranularity;
#else
m_iAllocationGranularity = getpagesize();
#endif
}
}
MMapFile::~MMapFile()
{
clear();
}
void MMapFile::clear()
{
release();
#ifdef _WIN32
if (m_hFileMap)
CloseHandle(m_hFileMap);
if (m_bTempHandle && m_hFile != INVALID_HANDLE_VALUE)
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
m_hFileMap = 0;
#else
if (m_bTempHandle && m_hFile)
fclose(m_hFile);
m_hFile = NULL;
#endif
}
void MMapFile::setro(BOOL bRO)
{
m_bReadOnly = bRO;
}
#ifdef _WIN32
int MMapFile::setfile(HANDLE hFile, DWORD dwSize)
#else
int MMapFile::setfile(int hFile, DWORD dwSize)
#endif
{
clear();
#ifdef _WIN32
m_hFile = hFile;
#else
m_hFileDesc = hFile;
#endif
m_bTempHandle = FALSE;
#ifdef _WIN32
if (m_hFile == INVALID_HANDLE_VALUE)
#else
if (m_hFileDesc == -1)
#endif
return 0;
m_iSize = (int) dwSize;
if (m_iSize <= 0)
return 0;
#ifdef _WIN32
m_hFileMap = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, m_iSize, NULL);
if (!m_hFileMap)
return 0;
#endif
m_bReadOnly = TRUE;
return 1;
}
void MMapFile::resize(int newsize)
{
release();
if (newsize > m_iSize)
{
#ifdef _WIN32
if (m_hFileMap)
CloseHandle(m_hFileMap);
m_hFileMap = 0;
#endif
m_iSize = newsize;
#ifdef _WIN32
if (m_hFile == INVALID_HANDLE_VALUE)
{
TCHAR buf[MAX_PATH], buf2[MAX_PATH];
GetTempPath(MAX_PATH, buf);
GetTempFileName(buf, _T("nsd"), 0, buf2);
m_hFile = CreateFile(
buf2,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
NULL
);
m_bTempHandle = TRUE;
}
if (m_hFile != INVALID_HANDLE_VALUE)
{
m_hFileMap = CreateFileMapping(
m_hFile,
NULL,
m_bReadOnly ? PAGE_READONLY : PAGE_READWRITE,
0,
m_iSize,
NULL
);
}
#else
if (m_hFile == NULL)
{
m_hFile = tmpfile();
if (m_hFile != NULL)
{
m_hFileDesc = fileno(m_hFile);
m_bTempHandle = TRUE;
}
}
// resize
if (m_hFileDesc != -1)
{
unsigned char c = 0;
if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1)
{
if (read(m_hFileDesc, &c, 1) != -1)
{
if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1)
{
if (write(m_hFileDesc, &c, 1) != -1)
{
return; // no errors
}
}
}
}
}
m_hFileDesc = -1; // some error occurred, bail
#endif
#ifdef _WIN32
if (!m_hFileMap)
#else
if (m_hFileDesc == -1)
#endif
{
extern void quit(); extern int g_display_errors;
if (g_display_errors)
{
PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error creating mmap the size of %d.\n"), m_iSize);
}
quit();
}
}
}
int MMapFile::getsize() const
{
return m_iSize;
}
void *MMapFile::get(int offset, int size) const
{
return get(offset, &size);
}
void *MMapFile::get(int offset, int *sizep) const
{
if (!sizep)
return NULL;
assert(!m_pView);
int size = *sizep;
if (!m_iSize || offset + size > m_iSize)
{
extern void quit(); extern int g_display_errors;
if (g_display_errors)
{
PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping file (%d, %d) is out of range.\n"), offset, size);
}
quit();
}
// fix offset
int alignedoffset = offset - (offset % m_iAllocationGranularity);
size += offset - alignedoffset;
#ifdef _WIN32
m_pView = MapViewOfFile(m_hFileMap, m_bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, alignedoffset, size);
#else
m_pView = mmap(0, size, m_bReadOnly ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, m_hFileDesc, alignedoffset);
m_iMappedSize = *sizep = size;
#endif
#ifdef _WIN32
if (!m_pView)
#else
if (m_pView == MAP_FAILED)
#endif
{
extern void quit(); extern int g_display_errors;
if (g_display_errors)
{
PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping datablock to %d.\n"), size);
}
quit();
}
return (void *)((char *)m_pView + offset - alignedoffset);
}
void *MMapFile::getmore(int offset, int size) const
{
void *pView;
void *pViewBackup = m_pView;
#ifndef _WIN32
int iMappedSizeBackup = m_iMappedSize;
#endif
m_pView = 0;
pView = get(offset, size);
m_pView = pViewBackup;
#ifndef _WIN32
m_iMappedSize = iMappedSizeBackup;
#endif
return pView;
}
void MMapFile::release()
{
if (!m_pView)
return;
#ifdef _WIN32
UnmapViewOfFile(m_pView);
#else
munmap((char *)m_pView, m_iMappedSize);
#endif
m_pView = NULL;
}
void MMapFile::release(void *pView, int size)
{
if (!pView)
return;
unsigned int alignment = ((ULONG_PTR)pView) % m_iAllocationGranularity;
pView = (char *)pView - alignment;
size += alignment;
#ifdef _WIN32
UnmapViewOfFile(pView);
#else
munmap((char *)pView, size);
#endif
}
void MMapFile::flush(int num)
{
if (m_pView)
#ifdef _WIN32
{} // improving performance by commenting: FlushViewOfFile(m_pView, num);
#else
msync((char *)m_pView, num, MS_SYNC);
#endif
}
// ========
// MMapFake
// ========
MMapFake::MMapFake()
{
m_pMem = NULL;
m_iSize = 0;
}
void MMapFake::set(const char *pMem, int iSize)
{
m_pMem = pMem;
m_iSize = iSize;
}
int MMapFake::getsize() const
{
return m_iSize;
}
void *MMapFake::get(int offset, int size) const
{
return get(offset, &size);
}
void *MMapFake::get(int offset, int *size) const
{
if (!size || (offset + *size > m_iSize))
return NULL;
return (void *)(m_pMem + offset);
}
void *MMapFake::getmore(int offset, int size) const
{
return get(offset, size);
}
void MMapFake::resize(int n) {}
void MMapFake::release() {}
void MMapFake::release(void *p, int size) {}
void MMapFake::clear() {}
void MMapFake::setro(BOOL b) {}
void MMapFake::flush(BOOL b) {}
// =======
// MMapBuf
// =======
MMapBuf::MMapBuf()
{
m_gb_u=0;
m_alloc=m_used=0;
}
MMapBuf::~MMapBuf()
{
m_fm.release();
}
int MMapBuf::add(const void *data, int len)
{
if (len <= 0) return 0;
resize(getlen() + len);
memcpy((char*)get(getlen() - len, len), data, len);
release();
return getlen() - len;
}
void MMapBuf::setro(BOOL bRO)
{
m_fm.setro(bRO);
}
void MMapBuf::resize(int newlen)
{
if (!m_gb_u && newlen < (16 << 20)) // still in db mode
{
m_gb.resize(newlen);
return;
}
// not in db mode
m_gb_u = 1;
m_used = newlen;
if (newlen > m_alloc)
{
m_alloc = newlen + (16 << 20); // add 16mb to top of mapping
if (m_alloc < 0) // we've hit a signed integer overflow
m_alloc = INT_MAX;
m_fm.resize(m_alloc);
if (m_gb.getlen())
{
memcpy(m_fm.get(0, m_gb.getlen()), m_gb.get(), m_gb.getlen());
m_fm.flush(m_gb.getlen());
m_fm.release();
m_gb.resize(0);
}
}
}
int MMapBuf::getsize() const
{
if (m_gb_u)
return m_fm.getsize();
return m_gb.getlen();
}
int MMapBuf::getlen() const
{
if (m_gb_u)
return m_used;
return m_gb.getlen();
}
void *MMapBuf::get() const
{
return get(0, m_alloc);
}
void *MMapBuf::get(int offset, int *sizep) const
{
if (!sizep)
return NULL;
int size = *sizep;
return get(offset, size);
}
void *MMapBuf::get(int offset, int size) const
{
if (m_gb_u)
return m_fm.get(offset, size);
return (void *) ((char *) m_gb.get() + offset);
}
void *MMapBuf::getmore(int offset, int size) const
{
if (m_gb_u)
return m_fm.getmore(offset, size);
return (void *) ((char *) m_gb.get() + offset);
}
void MMapBuf::release()
{
if (m_gb_u)
m_fm.release();
}
void MMapBuf::release(void *pView, int size)
{
if (m_gb_u)
m_fm.release(pView, size);
}
void MMapBuf::clear()
{
if (m_gb_u)
m_fm.clear();
}
void MMapBuf::flush(int num)
{
if (m_gb_u)
m_fm.flush(num);
}
|