ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include
ds@1952: #include
ds@1952: #include "CDataPack.h"
ds@1952:
ds@1952: #define DATAPACK_INITIAL_SIZE 512
ds@1952:
ds@1952: CDataPack::CDataPack()
ds@1952: {
ds@1952: m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
ds@1952: m_capacity = DATAPACK_INITIAL_SIZE;
ds@1952: Initialize();
ds@1952: }
ds@1952:
ds@1952: CDataPack::~CDataPack()
ds@1952: {
ds@1952: free(m_pBase);
ds@1952: }
ds@1952:
ds@1952: void CDataPack::Initialize()
ds@1952: {
ds@1952: m_curptr = m_pBase;
ds@1952: m_size = 0;
ds@1952: }
ds@1952:
ds@1952: void CDataPack::CheckSize(size_t typesize)
ds@1952: {
ds@1952: if (m_curptr - m_pBase + typesize <= m_capacity)
ds@1952: {
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: size_t pos = m_curptr - m_pBase;
ds@1952: do
ds@1952: {
ds@1952: m_capacity *= 2;
ds@1952: m_pBase = (char *)realloc(m_pBase, m_capacity);
ds@1952: m_curptr = m_pBase + pos;
ds@1952: } while (m_curptr - m_pBase + typesize > m_capacity);
ds@1952: }
ds@1952:
ds@1952: void CDataPack::ResetSize()
ds@1952: {
ds@1952: m_size = 0;
ds@1952: }
ds@1952:
ds@1952: size_t CDataPack::CreateMemory(size_t size, void **addr)
ds@1952: {
ds@1952: CheckSize(sizeof(size_t) + size);
ds@1952: size_t pos = m_curptr - m_pBase;
ds@1952:
ds@1952: *(size_t *)m_curptr = size;
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: if (addr)
ds@1952: {
ds@1952: *addr = m_curptr;
ds@1952: }
ds@1952:
ds@1952: m_curptr += size;
ds@1952: m_size += sizeof(size_t) + size;
ds@1952:
ds@1952: return pos;
ds@1952: }
ds@1952:
ds@1952: void CDataPack::PackCell(cell_t cell)
ds@1952: {
ds@1952: CheckSize(sizeof(size_t) + sizeof(cell_t));
ds@1952:
ds@1952: *(size_t *)m_curptr = sizeof(cell_t);
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: *(cell_t *)m_curptr = cell;
ds@1952: m_curptr += sizeof(cell_t);
ds@1952:
ds@1952: m_size += sizeof(size_t) + sizeof(cell_t);
ds@1952: }
ds@1952:
ds@1952: void CDataPack::PackFloat(float val)
ds@1952: {
ds@1952: CheckSize(sizeof(size_t) + sizeof(float));
ds@1952:
ds@1952: *(size_t *)m_curptr = sizeof(float);
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: *(float *)m_curptr = val;
ds@1952: m_curptr += sizeof(float);
ds@1952:
ds@1952: m_size += sizeof(size_t) + sizeof(float);
ds@1952: }
ds@1952:
ds@1952: void CDataPack::PackString(const char *string)
ds@1952: {
ds@1952: size_t len = strlen(string);
ds@1952: size_t maxsize = sizeof(size_t) + len + 1;
ds@1952: CheckSize(maxsize);
ds@1952:
ds@1952: // Pack the string length first for buffer overrun checking.
ds@1952: *(size_t *)m_curptr = len;
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: // Now pack the string.
ds@1952: memcpy(m_curptr, string, len);
ds@1952: m_curptr[len] = '\0';
ds@1952: m_curptr += len + 1;
ds@1952:
ds@1952: m_size += maxsize;
ds@1952: }
ds@1952:
ds@1952: void CDataPack::Reset() const
ds@1952: {
ds@1952: m_curptr = m_pBase;
ds@1952: }
ds@1952:
ds@1952: size_t CDataPack::GetPosition() const
ds@1952: {
ds@1952: return static_cast(m_curptr - m_pBase);
ds@1952: }
ds@1952:
ds@1952: bool CDataPack::SetPosition(size_t pos) const
ds@1952: {
ds@1952: if (pos > m_size-1)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952: m_curptr = m_pBase + pos;
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: cell_t CDataPack::ReadCell() const
ds@1952: {
ds@1952: if (!IsReadable(sizeof(size_t) + sizeof(cell_t)))
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952: if (*reinterpret_cast(m_curptr) != sizeof(cell_t))
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: cell_t val = *reinterpret_cast(m_curptr);
ds@1952: m_curptr += sizeof(cell_t);
ds@1952: return val;
ds@1952: }
ds@1952:
ds@1952: float CDataPack::ReadFloat() const
ds@1952: {
ds@1952: if (!IsReadable(sizeof(size_t) + sizeof(float)))
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952: if (*reinterpret_cast(m_curptr) != sizeof(float))
ds@1952: {
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: float val = *reinterpret_cast(m_curptr);
ds@1952: m_curptr += sizeof(float);
ds@1952: return val;
ds@1952: }
ds@1952:
ds@1952: bool CDataPack::IsReadable(size_t bytes) const
ds@1952: {
ds@1952: return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
ds@1952: }
ds@1952:
ds@1952: const char *CDataPack::ReadString(size_t *len) const
ds@1952: {
ds@1952: if (!IsReadable(sizeof(size_t)))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: size_t real_len = *(size_t *)m_curptr;
ds@1952:
ds@1952: m_curptr += sizeof(size_t);
ds@1952: char *str = (char *)m_curptr;
ds@1952:
ds@1952: if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: if (len)
ds@1952: {
ds@1952: *len = real_len;
ds@1952: }
ds@1952:
ds@1952: m_curptr += real_len + 1;
ds@1952:
ds@1952: return str;
ds@1952: }
ds@1952:
ds@1952: void *CDataPack::GetMemory() const
ds@1952: {
ds@1952: return m_curptr;
ds@1952: }
ds@1952:
ds@1952: void *CDataPack::ReadMemory(size_t *size) const
ds@1952: {
ds@1952: if (!IsReadable(sizeof(size_t)))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: size_t bytecount = *(size_t *)m_curptr;
ds@1952: m_curptr += sizeof(size_t);
ds@1952:
ds@1952: if (!IsReadable(bytecount))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: void *ptr = m_curptr;
ds@1952:
ds@1952: if (size)
ds@1952: {
ds@1952: *size = bytecount;
ds@1952: }
ds@1952:
ds@1952: m_curptr += bytecount;
ds@1952:
ds@1952: return ptr;
ds@1952: }