sources/network/bytestream.cpp

Mon, 11 Jan 2016 16:27:53 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 11 Jan 2016 16:27:53 +0200
changeset 124
c5ff5a4704dd
parent 109
e4966d7e615d
child 137
485cb6d6b98c
permissions
-rw-r--r--

Use tags as a compare basis for updating the revision if available, so that when a revision gets tagged, hginfo.h gets updated.

/*
	Copyright 2014 - 2016 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "bytestream.h"
#include <string.h>
BEGIN_ZFC_NAMESPACE

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (unsigned long length) :
	m_data (nullptr)
{
	resize (length);
	clear();
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const unsigned char* data, unsigned long length) :
	m_data (nullptr)
{
	init (data, length);
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const Vector<unsigned char>& bytes) :
	m_data (nullptr)
{
	init (bytes.data(), bytes.size());
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const Bytestream& other) :
	m_data (nullptr)
{
	init (other.data(), other.written_length());
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::~Bytestream()
{
	delete[] m_data;
}

// -------------------------------------------------------------------------------------------------
Bytestream& Bytestream::operator= (const Bytestream& other)
{
	init (other.data(), other.written_length());
	return *this;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::resize (unsigned long newsize)
{
	Vector<unsigned char> olddata;
	unsigned long oldsize = 0L;

	if (m_data != nullptr)
	{
		oldsize = allocated_size();
		olddata.resize (oldsize);
		memcpy (olddata.data(), m_data, oldsize);
	}

	delete[] m_data;
	m_allocatedSize = newsize;
	m_data = new unsigned char[newsize];

	if (oldsize > 0L)
		memcpy (m_data, olddata, min (oldsize, newsize));
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::init (const unsigned char* data, unsigned long length)
{
	resize (length);
	memcpy (m_data, data, length);
	m_cursor = &m_data[0];
	m_writtenLength = length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::clear()
{
	m_cursor = &m_data[0];
	m_writtenLength = 0;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::ensure_read_space (unsigned int bytes)
{
	if (bytes_left() < bytes)
	{
		int bytesPast = bytes - bytes_left();

		String message;
		message.sprintf ("attempted to read %d byte%s past the end of bytestream",
			bytesPast, bytesPast != -1 ? "s" : "");
		throw IOError (message);
	}
}

// -------------------------------------------------------------------------------------------------
//
int8_t Bytestream::read_byte()
{
	ensure_read_space (1);
	return *m_cursor++;
}

// -------------------------------------------------------------------------------------------------
//
int16_t Bytestream::read_short()
{
	ensure_read_space (2);
	short int result = 0;

	for (int i = 0; i < 2; ++i)
		result |= m_cursor[i] << (i * 8);

	m_cursor += 2;
	return result;
}

// -------------------------------------------------------------------------------------------------
//
int32_t Bytestream::read_long()
{
	ensure_read_space (4);
	long int result = 0;

	for (int i = 0; i < 4; ++i)
		result |= m_cursor[i] << (i * 8);

	m_cursor += 4;
	return result;
}

// -------------------------------------------------------------------------------------------------
//
float Bytestream::read_float()
{
	float value;
	int intvalue = read_long();
	memcpy (&value, &intvalue, sizeof intvalue);
	return value;
}

// -------------------------------------------------------------------------------------------------
//
String Bytestream::read_string()
{
	// Zandronum sends strings of maximum 2048 characters, though it only
	// reads 2047-character long ones so I guess we can follow up and do
	// the same :-)
	static char buffer[MAX_NETWORK_STRING];
	unsigned char* stringEnd;
	unsigned char* stringBegin = m_cursor;
	unsigned char* end = m_data + allocated_size();

	// Where's the end of the string?
	for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
	{
		if (stringEnd == end)
		{
			// Past the end of the buffer
			throw IOError ("unterminated or too long string in packet");
		}
	}

	unsigned int length = stringEnd - m_cursor;
	m_cursor = stringEnd + 1;
	memcpy (buffer, stringBegin, length);
	buffer[length] = '\0';
	return String (buffer);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::read (unsigned char* buffer, unsigned long length)
{
	ensure_read_space (length);
	memcpy (buffer, m_cursor, length);
	m_cursor += length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write (unsigned char val)
{
	*m_cursor++ = val;
	m_writtenLength++;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write (const unsigned char* val, unsigned int length)
{
	grow_to_fit (length);
	memcpy (m_cursor, val, length);
	m_cursor += length;
	m_writtenLength += length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::grow_to_fit (unsigned long bytes)
{
	if (space_left() < bytes)
		resize (allocated_size() + bytes + 128);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_byte (int8_t val)
{
	grow_to_fit (1);
	write (val);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_short (int16_t val)
{
	grow_to_fit (2);

	for (int i = 0; i < 2; ++i)
		write ((val >> (i * 8)) & 0xFF);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_long (int32_t val)
{
	grow_to_fit (4);

	for (int i = 0; i < 4; ++i)
		write ((val >> (i * 8)) & 0xFF);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_float (float val)
{
	// I know this is probably dangerous but this is what Zandronum does so yeah
	int intvalue;
	memcpy (&intvalue, &val, sizeof val);
	write_long (intvalue);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_string (const String& val)
{
	grow_to_fit (val.length() + 1);
	write (reinterpret_cast<const unsigned char*> (val.chars()), val.length());
	write (0);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_buffer (const Bytestream& other)
{
	write (other.data(), other.written_length());
}

END_ZFC_NAMESPACE

mercurial