Example #1
0
    void Resize(SIZE_T count)
    {
        if (NULL == m_buffer)
        {
            if (count > STACKCOUNT)
            {
                ReallocateBuffer(count);
            }
            else
            {
                m_size = STACKCOUNT+1;
                m_buffer = m_innerBuffer;
                m_count = count;
            }
        }
        else if (m_innerBuffer == m_buffer)
        {
            if (count > STACKCOUNT)
            {
                ReallocateBuffer(count);
            }
            else
            {
                m_count = count;
                m_size = STACKCOUNT+1;
            }
        }
        else
        {
            ReallocateBuffer(count);
        }

        return;
    }
Example #2
0
void PEFile::AddSection(char const* name, size_t size)
{
	if (strlen(name) > 8)
		throw std::runtime_error("PEFile::AddSection - Section name is too long.");
	for (int i = 0; i < m_ntHeaders->FileHeader.NumberOfSections; i++)
	{
		if (strcmp(reinterpret_cast<char*>(m_firstSectionHeader[i].Name), name) == 0)
			throw std::runtime_error("PEFile::AddSection - Section with this name already exists.");
	}
	IMAGE_SECTION_HEADER* newSection = m_firstSectionHeader + m_ntHeaders->FileHeader.NumberOfSections;
	IMAGE_SECTION_HEADER* lastSection = newSection - 1;

	if (((m_fileBuffer + m_firstSectionHeader->PointerToRawData) - reinterpret_cast<char*>(newSection)) < sizeof(IMAGE_SECTION_HEADER))
		throw std::runtime_error("PEFile::AddSection - Not enough space for another section header.");
	
	size_t sectionAligned = Tools::AlignSize(size, m_ntHeaders->OptionalHeader.SectionAlignment);
	if (m_fileSize + sectionAligned > m_bufferSize)
		ReallocateBuffer((m_fileSize + sectionAligned) * 2);

	memset(newSection, 0, sizeof(IMAGE_SECTION_HEADER)); //just incase
	memcpy(newSection->Name, name, strlen(name));

	newSection->Misc.VirtualSize = 0;
	newSection->SizeOfRawData = Tools::AlignSize(size, m_ntHeaders->OptionalHeader.FileAlignment);
	newSection->PointerToRawData = lastSection->PointerToRawData + lastSection->SizeOfRawData;
	newSection->VirtualAddress = lastSection->VirtualAddress + Tools::AlignSize(lastSection->Misc.VirtualSize, m_ntHeaders->OptionalHeader.SectionAlignment); // as some section have virtualSize > rawSize, we align virtual size instead of using rawSize

	newSection->Characteristics |= IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ;
	m_ntHeaders->FileHeader.NumberOfSections++;
	m_ntHeaders->OptionalHeader.SizeOfImage = newSection->VirtualAddress + newSection->Misc.VirtualSize;
	m_fileSize += sectionAligned;
}
Example #3
0
/*----------------------------------------------------------------------
|   NPT_DataBuffer::SetBufferSize
+---------------------------------------------------------------------*/
NPT_Result
NPT_DataBuffer::SetBufferSize(NPT_Size buffer_size)
{
    if (m_BufferIsLocal) {
        return ReallocateBuffer(buffer_size);
    } else {
        return NPT_ERROR_NOT_SUPPORTED; // you cannot change the
                                        // buffer management mode
    }
}
Example #4
0
/*----------------------------------------------------------------------
|   NPT_DataBuffer::SetData
+---------------------------------------------------------------------*/
NPT_Result
NPT_DataBuffer::SetData(const NPT_Byte* data, NPT_Size size)
{
    if (size > m_BufferSize) {
        if (m_BufferIsLocal) {
            NPT_CHECK(ReallocateBuffer(size));
        } else {
            return NPT_ERROR_INVALID_STATE;
        }
    }
    if (data) NPT_CopyMemory(m_Buffer, data, size);
    m_DataSize = size;

    return NPT_SUCCESS;
}
Example #5
0
/*----------------------------------------------------------------------
|   NPT_DataBuffer::SetDataSize
+---------------------------------------------------------------------*/
NPT_Result
NPT_DataBuffer::SetDataSize(NPT_Size size)
{
    if (size > m_BufferSize) {
        // the buffer is too small, we need to reallocate it
        if (m_BufferIsLocal) {
            NPT_CHECK(ReallocateBuffer(size));
        } else { 
            // we cannot reallocate an external buffer
            return NPT_ERROR_NOT_SUPPORTED;
        }
    }
    m_DataSize = size;
    return NPT_SUCCESS;
}
Example #6
0
static void debugReadFile(char* name)
{
    char tmp[tmpBufSize];
    size_t bufSize = 0;
    FILE* input = fopen(name, "r");
    buf1 = 0;
    while (fgets(tmp, tmpBufSize, input)) {
        size_t tmpLen = strlen(tmp);
        size_t oldSize = bufSize;
        if (!bufSize) bufSize++;
        bufSize += tmpLen;
        buf1 = ReallocateBuffer(buf1, bufSize);
        buf1[oldSize] = 0;
        strcat(buf1, tmp);
    }
    fclose(input);
}
Example #7
0
void PEFile::ExpandLastSection(size_t size)
{
	IMAGE_SECTION_HEADER* lastSection = m_firstSectionHeader + m_ntHeaders->FileHeader.NumberOfSections - 1;
	
	if (m_fileSize + size > m_bufferSize)
		ReallocateBuffer((m_fileSize + size) * 2);

	size_t newVirtualSize = lastSection->Misc.VirtualSize + size;
	if (newVirtualSize > lastSection->SizeOfRawData)
	{
		size_t alignedSize = Tools::AlignSize(lastSection->Misc.VirtualSize + size, m_ntHeaders->OptionalHeader.FileAlignment);
		lastSection->SizeOfRawData = alignedSize;

		m_fileSize += Tools::AlignSize(alignedSize, m_ntHeaders->OptionalHeader.SectionAlignment);
	}
	lastSection->Misc.VirtualSize = newVirtualSize;
	m_ntHeaders->OptionalHeader.SizeOfImage = lastSection->VirtualAddress + lastSection->Misc.VirtualSize;
}