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
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it 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; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

#include "MemFile.h"	// Interface declarations


CMemFile::CMemFile(unsigned int growthRate)
{
	m_buffer		= NULL;
	m_BufferSize	= 0;
	m_fileSize		= 0;
	m_growthRate	= growthRate;
	m_position		= 0;
	m_delete		= true;
	m_readonly		= false;
}


CMemFile::CMemFile(uint8* buffer, size_t bufferSize)
{
	MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempted to attach invalid buffer."));

	m_buffer		= buffer;
	m_BufferSize	= bufferSize;
	m_fileSize		= bufferSize;
	m_growthRate	= 0;
	m_position		= 0;
	m_delete		= false;
	m_readonly		= false;
}

CMemFile::CMemFile(const uint8* buffer, size_t bufferSize)
{
	MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempted to attach invalid buffer."));

	m_buffer		= const_cast<uint8*>(buffer);
	m_BufferSize	= bufferSize;
	m_fileSize		= bufferSize;
	m_growthRate	= 0;
	m_position		= 0;
	m_delete		= false;
	m_readonly		= true;
}

CMemFile::~CMemFile()
{
	if (m_delete) {
		free(m_buffer);
	}
}


uint64 CMemFile::GetPosition() const
{
	return m_position;
}


void CMemFile::SetLength(size_t newLen)
{
	MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to change length on a read-only buffer."));

	if (newLen > m_BufferSize) {
		enlargeBuffer(newLen);
	}

	if (newLen < m_position) {
		m_position = newLen;
	}

	m_fileSize = newLen;
}


uint64 CMemFile::GetLength() const
{
	return m_fileSize;
}


void CMemFile::enlargeBuffer(size_t size)
{
	MULE_VALIDATE_PARAMS(size >= m_BufferSize, wxT("CMemFile: Attempted to shrink buffer."));
	MULE_VALIDATE_STATE(m_delete, wxT("CMemFile: Attempted to grow an attached buffer."));
	MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to grow a read-only buffer."));

	size_t newsize;

	if (m_growthRate) {
		newsize = ((size + m_growthRate - 1) / m_growthRate) * m_growthRate;
	} else {
		// No growth-rate specified. Change to exactly the size specified.
		newsize = size;
	}

	uint8 *tmp = (uint8*)realloc(m_buffer, newsize);
	if (tmp) {
		m_buffer = tmp;
		m_BufferSize = newsize;
	}

	MULE_VALIDATE_STATE(tmp, wxT("CMemFile: Failed to (re)allocate buffer"));
}


sint64 CMemFile::doRead(void* buffer, size_t count) const
{
	MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to read to invalid buffer"));

	// Handle reads past EOF
	if (m_position > m_fileSize) {
		return 0;
	} else if (m_position + count > m_fileSize) {
		count = m_fileSize - m_position;
	}

	if (count) {
		memcpy(buffer, m_buffer + m_position, count);
		m_position += count;
	}

	return count;
}


sint64 CMemFile::doWrite(const void* buffer, size_t count)
{
	MULE_VALIDATE_PARAMS(buffer, wxT("CMemFile: Attempting to write to invalid buffer"));
	MULE_VALIDATE_STATE(!m_readonly, wxT("CMemFile: Attempted to write to a read-only buffer."));

	// Needs more space?
	if (m_position + count > m_BufferSize) {
		enlargeBuffer(m_position + count);
	}

	MULE_VALIDATE_STATE(m_position + count <= m_BufferSize, wxT("CMemFile: Buffer not resized to needed size."));

	memcpy(m_buffer + m_position, buffer, count);
	m_position += count;

	if (m_position > m_fileSize) {
		m_fileSize = m_position;
	}

	return count;
}


sint64 CMemFile::doSeek(sint64 offset) const
{
	MULE_VALIDATE_PARAMS(offset >= 0, wxT("CMemFile: Invalid seek, position, must be positive."));

	return m_position = offset;
}


void CMemFile::ResetData()
{
	wxCHECK_RET(!m_readonly, wxT("Trying to reset read-only buffer"));

	memset(m_buffer, 0, m_BufferSize);
	m_fileSize	= 0;
	m_position	= 0;
}

// File_checked_for_headers