upx/src/mem.cpp

133 lines
3.5 KiB
C++
Raw Normal View History

2000-05-20 00:04:55 +08:00
/* mem.cpp --
This file is part of the UPX executable compressor.
Copyright (C) 1996-2003 Markus Franz Xaver Johannes Oberhumer
Copyright (C) 1996-2003 Laszlo Molnar
All Rights Reserved.
2000-05-20 00:04:55 +08:00
UPX and the UCL library are free software; you can redistribute them
and/or modify them under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Markus F.X.J. Oberhumer Laszlo Molnar
<mfx@users.sourceforge.net> <ml1050@users.sourceforge.net>
2000-05-20 00:04:55 +08:00
*/
#include "conf.h"
#include "mem.h"
/*************************************************************************
//
**************************************************************************/
MemBuffer::MemBuffer(unsigned size) :
ptr(NULL), psize(0)
2000-05-20 00:04:55 +08:00
{
if (size > 0)
alloc(size);
2000-05-20 00:04:55 +08:00
}
MemBuffer::~MemBuffer()
{
this->dealloc();
2000-05-20 00:04:55 +08:00
}
void MemBuffer::dealloc()
2000-05-20 00:04:55 +08:00
{
if (ptr)
{
checkState();
::free(ptr - 8);
}
ptr = NULL;
psize = 0;
2000-05-20 00:04:55 +08:00
}
void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra)
2000-05-20 00:04:55 +08:00
{
assert((int)uncompressed_size > 0);
assert((int)extra >= 0);
alloc(uncompressed_size + uncompressed_size/8 + 256 + extra);
2000-05-20 00:04:55 +08:00
}
void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra)
2000-05-20 00:04:55 +08:00
{
assert((int)uncompressed_size > 0);
assert((int)extra >= 0);
// note: 3 bytes are allowed overrun for the asm_fast decompressors
// alloc(uncompressed_size + 3 + 512 + extra); // 512 safety bytes
alloc(uncompressed_size + 3 + extra);
2000-05-20 00:04:55 +08:00
}
/*************************************************************************
//
**************************************************************************/
2000-05-20 00:04:55 +08:00
#define MAGIC1(p) ((unsigned)(p) ^ 0xfefdbeeb)
#define MAGIC2(p) ((unsigned)(p) ^ 0xfefdbeeb ^ 0x80024001)
2000-05-20 00:04:55 +08:00
unsigned MemBuffer::global_alloc_counter = 0;
void MemBuffer::checkState() const
2000-05-20 00:04:55 +08:00
{
if (!ptr)
throwInternalError("block not allocated");
if (get_be32(ptr - 4) != MAGIC1(ptr))
throwInternalError("memory clobbered before allocated block 1");
if (get_be32(ptr - 8) != psize)
throwInternalError("memory clobbered before allocated block 2");
if (get_be32(ptr + psize) != MAGIC2(ptr))
throwInternalError("memory clobbered past end of allocated block");
2000-05-20 00:04:55 +08:00
}
void MemBuffer::alloc(unsigned size)
2000-05-20 00:04:55 +08:00
{
// NOTE: we don't automaticlly free a used buffer
assert(ptr == NULL);
assert(psize == 0);
//
assert((int)size > 0);
unsigned total = 4 + 4 + size + 4 + 4;
assert((int)total > 0);
unsigned char *p = (unsigned char *) malloc(total);
if (!p)
{
//throw bad_alloc();
throwCantPack("out of memory");
//exit(1);
}
ptr = p + 8;
psize = size;
// store magic state
set_be32(ptr - 8, psize);
set_be32(ptr - 4, MAGIC1(ptr));
set_be32(ptr + psize, MAGIC2(ptr));
set_be32(ptr + psize + 4, global_alloc_counter++);
2000-05-20 00:04:55 +08:00
}
/*
vi:ts=4:et
*/