// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#include <bdLobby/bdAuthTicket.h>

bdAuthTicket::bdAuthTicket()
: m_magicNumber(0),
  m_type(0),
  m_titleID(0),
  m_timeIssued(0),
  m_timeExpires(0),
  m_licenseID(0),
  m_userID(0)
{
	bdMemset(m_username, 0, BD_MAX_USERNAME_LENGTH);
	bdMemset(m_sessionKey, 0, BD_ENCRYPTION_KEY_SIZE);
}

bdAuthTicket::~bdAuthTicket()
{
}

void bdAuthTicket::serialize(void *buffer)
{
	// FIXME: Buffer size must be passed in
	const bdUInt bufferSize = BD_AUTH_TICKET_SIZE;
	bdUInt offset = 0;
	bdBool result = true;
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_magicNumber);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_type);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_titleID);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_timeIssued);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_timeExpires);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_licenseID);
	result = result && bdBytePacker::appendBasicType(buffer, bufferSize, offset, offset, m_userID);
	result = result && bdBytePacker::appendBuffer(buffer, bufferSize, offset, offset, m_username, BD_MAX_USERNAME_LENGTH);
	result = result && bdBytePacker::appendBuffer(buffer, bufferSize, offset, offset, m_sessionKey, BD_ENCRYPTION_KEY_SIZE);

	BD_ASSERT(result, "Failed to serialize the ticket.");
}

void bdAuthTicket::deserialize(const void *buffer)
{
	// FIXME: Buffer size must be passed in.
	const bdUInt bufferSize = BD_AUTH_TICKET_SIZE;
	bdUInt offset = 0;
	bdBool result = true;

	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_magicNumber);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_type);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_titleID);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_timeIssued);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_timeExpires);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_licenseID);
	result = result && bdBytePacker::removeBasicType(buffer, bufferSize, offset, offset, m_userID);
	result = result && bdBytePacker::removeBuffer(buffer, bufferSize, offset, offset, m_username, BD_MAX_USERNAME_LENGTH);
	result = result && bdBytePacker::removeBuffer(buffer, bufferSize, offset, offset,
		m_sessionKey, BD_ENCRYPTION_KEY_SIZE);
}
