--- /dev/null
+
+/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+**] All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#include "../config.h"
+
+#include <algorithm>
+#if HAVE_BYTESWAP_H
+#include <byteswap.h>
+#endif
+
+#include <arpa/inet.h>
+
+#include <SDL/SDL.h>
+
+#include "Packet.hh"
+
+
+#ifndef bswap_64
+#define bswap_64(x) (((uint64_t)(x) << 56) | \
+ (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
+ (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
+ (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
+ (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
+ (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
+ (((uint64_t)(x) >> 40) & 0xff00ULL) | \
+ ((uint64_t)(x) >> 56))
+#endif
+
+static uint64_t htonll(uint64_t x)
+{
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ return bswap_64(x);
+#endif
+}
+
+static uint64_t ntohll(uint64_t x)
+{
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ return bswap_64(x);
+#endif
+}
+
+
+namespace Mf {
+
+
+Packet::Packet(size_t size) :
+ mR(0),
+ mW(0),
+ mBoolR(0),
+ mBoolW(0),
+ mBoolNumR(0),
+ mBoolNumW(0)
+{
+ mBuffer = boost::shared_array<char>(new char[size]);
+ mSize = size;
+}
+
+Packet::Packet(const char* data, size_t size) :
+ mR(0),
+ mBoolR(0),
+ mBoolW(0),
+ mBoolNumR(0),
+ mBoolNumW(0)
+{
+ mBuffer = boost::shared_array<char>(new char[size]);
+ mSize = size;
+ mW = size;
+ memcpy(mBuffer.get(), data, mSize);
+}
+
+
+Packet& Packet::operator<<(bool value)
+{
+ int bit = mBoolNumW % 8;
+ if (bit == 0)
+ {
+ mBoolW = mW;
+
+ unsigned char byte = 0;
+ if (write(&byte, 1) == 0) return *this;
+ }
+
+ if (value) mBuffer[mBoolW] |= (1 << bit);
+ ++mBoolNumW;
+
+ return *this;
+}
+
+
+Packet& Packet::operator<<(int8_t value)
+{
+ return *this << (uint8_t)value;
+}
+
+Packet& Packet::operator<<(int16_t value)
+{
+ return *this << (uint16_t)value;
+}
+
+Packet& Packet::operator<<(int32_t value)
+{
+ return *this << (uint32_t)value;
+}
+
+Packet& Packet::operator<<(int64_t value)
+{
+ return *this << (uint64_t)value;
+}
+
+
+Packet& Packet::operator<<(uint8_t value)
+{
+ write(&value, sizeof(value));
+ return *this;
+}
+
+Packet& Packet::operator<<(uint16_t value)
+{
+ value = htons(value);
+ write(&value, sizeof(value));
+ return *this;
+}
+
+Packet& Packet::operator<<(uint32_t value)
+{
+ value = htonl(value);
+ write(&value, sizeof(value));
+ return *this;
+}
+
+Packet& Packet::operator<<(uint64_t value)
+{
+ value = htonll(value);
+ write(&value, sizeof(value));
+ return *this;
+}
+
+//Packet& Packet::operator<<(float value)
+//{
+//}
+
+//Packet& Packet::operator<<(double value)
+//{
+//}
+
+//Packet& Packet::operator<<(long double value)
+//{
+//}
+
+
+size_t Packet::write(const void* bytes, size_t size)
+{
+ size_t nBytes = std::min(size, mSize - mW);
+ memcpy(&mBuffer[mW], bytes, nBytes);
+ mW += nBytes;
+ return nBytes;
+}
+
+
+Packet& Packet::operator>>(bool& value)
+{
+ int bit = mBoolNumR % 8;
+ if (bit == 0)
+ {
+ mBoolR = mR;
+
+ unsigned char byte = 0;
+ if (read(&byte, 1) == 0) return *this;
+ }
+
+ value = 1 & (mBuffer[mBoolR] >> bit);
+ ++mBoolNumR;
+
+ return *this;
+}
+
+Packet& Packet::operator>>(int8_t& value)
+{
+ return *this >> (uint8_t&)value;
+}
+
+Packet& Packet::operator>>(int16_t& value)
+{
+ return *this >> (uint16_t&)value;
+}
+
+Packet& Packet::operator>>(int32_t& value)
+{
+ return *this >> (uint32_t&)value;
+}
+
+Packet& Packet::operator>>(int64_t& value)
+{
+ return *this >> (uint64_t&)value;
+}
+
+Packet& Packet::operator>>(uint8_t& value)
+{
+ read(&value, sizeof(value));
+ return *this;
+}
+
+Packet& Packet::operator>>(uint16_t& value)
+{
+ read(&value, sizeof(value));
+ value = ntohs(value);
+ return *this;
+}
+
+Packet& Packet::operator>>(uint32_t& value)
+{
+ read(&value, sizeof(value));
+ value = ntohl(value);
+ return *this;
+}
+
+Packet& Packet::operator>>(uint64_t& value)
+{
+ read(&value, sizeof(value));
+ value = ntohll(value);
+ return *this;
+}
+
+
+size_t Packet::read(void* bytes, size_t size)
+{
+ size_t nBytes = std::min(size, mW - mR);
+ memcpy(bytes, &mBuffer[mR], nBytes);
+ mR += nBytes;
+ return nBytes;
+}
+
+
+} // namespace Mf
+