core/CDataPack.cpp
author Michael McKoy <ferret99gt@gmail.com>
Wed Aug 08 23:26:42 2007 +0000 (2007-08-08)
changeset 1280 13c62962a9ef
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Corrected filename in header of basefuncommands.sp
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include <malloc.h>
    34 #include <string.h>
    35 #include "CDataPack.h"
    36 
    37 #define DATAPACK_INITIAL_SIZE		512
    38 
    39 CDataPack::CDataPack()
    40 {
    41 	m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
    42 	m_capacity = DATAPACK_INITIAL_SIZE;
    43 	Initialize();
    44 }
    45 
    46 CDataPack::~CDataPack()
    47 {
    48 	free(m_pBase);
    49 }
    50 
    51 void CDataPack::Initialize()
    52 {
    53 	m_curptr = m_pBase;
    54 	m_size = 0;
    55 }
    56 
    57 void CDataPack::CheckSize(size_t typesize)
    58 {
    59 	if (m_curptr - m_pBase + typesize <= m_capacity)
    60 	{
    61 		return;
    62 	}
    63 
    64 	size_t pos = m_curptr - m_pBase;
    65 	do
    66 	{
    67 		m_capacity *= 2;
    68 		m_pBase = (char *)realloc(m_pBase, m_capacity);
    69 		m_curptr = m_pBase + pos;
    70 	} while (m_curptr - m_pBase + typesize > m_capacity);
    71 }
    72 
    73 void CDataPack::ResetSize()
    74 {
    75 	m_size = 0;
    76 }
    77 
    78 size_t CDataPack::CreateMemory(size_t size, void **addr)
    79 {
    80 	CheckSize(sizeof(size_t) + size);
    81 	size_t pos = m_curptr - m_pBase;
    82 
    83 	*(size_t *)m_curptr = size;
    84 	m_curptr += sizeof(size_t);
    85 
    86 	if (addr)
    87 	{
    88 		*addr = m_curptr;
    89 	}
    90 
    91 	m_curptr += size;
    92 	m_size += sizeof(size_t) + size;
    93 
    94 	return pos;
    95 }
    96 
    97 void CDataPack::PackCell(cell_t cell)
    98 {
    99 	CheckSize(sizeof(size_t) + sizeof(cell_t));
   100 
   101 	*(size_t *)m_curptr = sizeof(cell_t);
   102 	m_curptr += sizeof(size_t);
   103 
   104 	*(cell_t *)m_curptr = cell;
   105 	m_curptr += sizeof(cell_t);
   106 
   107 	m_size += sizeof(size_t) + sizeof(cell_t);
   108 }
   109 
   110 void CDataPack::PackFloat(float val)
   111 {
   112 	CheckSize(sizeof(size_t) + sizeof(float));
   113 
   114 	*(size_t *)m_curptr = sizeof(float);
   115 	m_curptr += sizeof(size_t);
   116 
   117 	*(float *)m_curptr = val;
   118 	m_curptr += sizeof(float);
   119 
   120 	m_size += sizeof(size_t) + sizeof(float);
   121 }
   122 
   123 void CDataPack::PackString(const char *string)
   124 {
   125 	size_t len = strlen(string);
   126 	size_t maxsize = sizeof(size_t) + len + 1;
   127 	CheckSize(maxsize);
   128 
   129 	// Pack the string length first for buffer overrun checking.
   130 	*(size_t *)m_curptr = len;
   131 	m_curptr += sizeof(size_t);
   132 
   133 	// Now pack the string.
   134 	memcpy(m_curptr, string, len);
   135 	m_curptr[len] = '\0';
   136 	m_curptr += len + 1;
   137 
   138 	m_size += maxsize;
   139 }
   140 
   141 void CDataPack::Reset() const
   142 {
   143 	m_curptr = m_pBase;
   144 }
   145 
   146 size_t CDataPack::GetPosition() const
   147 {
   148 	return static_cast<size_t>(m_curptr - m_pBase);
   149 }
   150 
   151 bool CDataPack::SetPosition(size_t pos) const
   152 {
   153 	if (pos > m_size-1)
   154 	{
   155 		return false;
   156 	}
   157 	m_curptr = m_pBase + pos;
   158 
   159 	return true;
   160 }
   161 
   162 cell_t CDataPack::ReadCell() const
   163 {
   164 	if (!IsReadable(sizeof(size_t) + sizeof(cell_t)))
   165 	{
   166 		return 0;
   167 	}
   168 	if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
   169 	{
   170 		return 0;
   171 	}
   172 
   173 	m_curptr += sizeof(size_t);
   174 
   175 	cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
   176 	m_curptr += sizeof(cell_t);
   177 	return val;
   178 }
   179 
   180 float CDataPack::ReadFloat() const
   181 {
   182 	if (!IsReadable(sizeof(size_t) + sizeof(float)))
   183 	{
   184 		return 0;
   185 	}
   186 	if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
   187 	{
   188 		return 0;
   189 	}
   190 
   191 	m_curptr += sizeof(size_t);
   192 
   193 	float val = *reinterpret_cast<float *>(m_curptr);
   194 	m_curptr += sizeof(float);
   195 	return val;
   196 }
   197 
   198 bool CDataPack::IsReadable(size_t bytes) const
   199 {
   200 	return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
   201 }
   202 
   203 const char *CDataPack::ReadString(size_t *len) const
   204 {
   205 	if (!IsReadable(sizeof(size_t)))
   206 	{
   207 		return NULL;
   208 	}
   209 
   210 	size_t real_len = *(size_t *)m_curptr;
   211 
   212 	m_curptr += sizeof(size_t);
   213 	char *str = (char *)m_curptr;
   214 
   215 	if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
   216 	{
   217 		return NULL;
   218 	}
   219 
   220 	if (len)
   221 	{
   222 		*len = real_len;
   223 	}
   224 
   225 	m_curptr += real_len + 1;
   226 
   227 	return str;
   228 }
   229 
   230 void *CDataPack::GetMemory() const
   231 {
   232 	return m_curptr;
   233 }
   234 
   235 void *CDataPack::ReadMemory(size_t *size) const
   236 {
   237 	if (!IsReadable(sizeof(size_t)))
   238 	{
   239 		return NULL;
   240 	}
   241 
   242 	size_t bytecount = *(size_t *)m_curptr;
   243 	m_curptr += sizeof(size_t);
   244 
   245 	if (!IsReadable(bytecount))
   246 	{
   247 		return NULL;
   248 	}
   249 
   250 	void *ptr = m_curptr;
   251 
   252 	if (size)
   253 	{
   254 		*size = bytecount;
   255 	}
   256 
   257 	m_curptr += bytecount;
   258 
   259 	return ptr;
   260 }