sources/network/bytestream.cpp

Tue, 16 Dec 2014 02:48:18 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 16 Dec 2014 02:48:18 +0200
changeset 58
d175243ad169
parent 18
56a1ac7d931b
child 61
cdf3c8af1545
permissions
-rw-r--r--

- rcon sessions are no longer allocated on the heap

/*
	Copyright 2014 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>

// -------------------------------------------------------------------------------------------------
//
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;
}

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::resize (unsigned long newsize) -> void
{
	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 (olddata > 0L)
		memcpy (m_data, olddata, min (oldsize, newsize));
}

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

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::ensure_read_space (unsigned int bytes) -> void
{
	if (bytes_left() < bytes)
	{
		throw IOError (format ("attempted to read %1 byte(s) past the end of bytestream",
			bytes - bytes_left()));
	}
}

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::read_short() -> short int
{
	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;
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::read_long() -> long int
{
	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;
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::read_float() -> float
{
	int value = read_long();
	return reinterpret_cast<float&> (value);
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::read_string() -> 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! Argh!
			throw IOError ("unterminated string in packet");
	}

	m_cursor = stringEnd + 1;
	unsigned int length = stringEnd - m_cursor;

	// ensure we won't write past the buffer (note: we still moved
	// past the excess bytes in the above statement, those are ignored)
	if (length >= MAX_NETWORK_STRING)
		length = MAX_NETWORK_STRING - 1;

	memcpy (buffer, stringBegin, length);
	buffer[length] = '\0';
	return String (buffer);
}

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

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

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

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::write_byte (char val) -> void
{
	grow_to_fit (1);
	write (val);
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::write_short (short int val) -> void
{
	grow_to_fit (2);

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::write_long (long int val) -> void
{
	grow_to_fit (4);

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

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::write_float (float val) -> void
{
	// I know this is probably dangerous but this is what Zandronum does so yeah
	write_long (reinterpret_cast<int&> (val));
}

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

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

mercurial