3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
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.
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.
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/>.
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>.
35 #include "CDataPack.h"
37 #define DATAPACK_INITIAL_SIZE 512
39 CDataPack::CDataPack()
41 m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
42 m_capacity = DATAPACK_INITIAL_SIZE;
46 CDataPack::~CDataPack()
51 void CDataPack::Initialize()
57 void CDataPack::CheckSize(size_t typesize)
59 if (m_curptr - m_pBase + typesize <= m_capacity)
64 size_t pos = m_curptr - m_pBase;
68 m_pBase = (char *)realloc(m_pBase, m_capacity);
69 m_curptr = m_pBase + pos;
70 } while (m_curptr - m_pBase + typesize > m_capacity);
73 void CDataPack::ResetSize()
78 size_t CDataPack::CreateMemory(size_t size, void **addr)
80 CheckSize(sizeof(size_t) + size);
81 size_t pos = m_curptr - m_pBase;
83 *(size_t *)m_curptr = size;
84 m_curptr += sizeof(size_t);
92 m_size += sizeof(size_t) + size;
97 void CDataPack::PackCell(cell_t cell)
99 CheckSize(sizeof(size_t) + sizeof(cell_t));
101 *(size_t *)m_curptr = sizeof(cell_t);
102 m_curptr += sizeof(size_t);
104 *(cell_t *)m_curptr = cell;
105 m_curptr += sizeof(cell_t);
107 m_size += sizeof(size_t) + sizeof(cell_t);
110 void CDataPack::PackFloat(float val)
112 CheckSize(sizeof(size_t) + sizeof(float));
114 *(size_t *)m_curptr = sizeof(float);
115 m_curptr += sizeof(size_t);
117 *(float *)m_curptr = val;
118 m_curptr += sizeof(float);
120 m_size += sizeof(size_t) + sizeof(float);
123 void CDataPack::PackString(const char *string)
125 size_t len = strlen(string);
126 size_t maxsize = sizeof(size_t) + len + 1;
129 // Pack the string length first for buffer overrun checking.
130 *(size_t *)m_curptr = len;
131 m_curptr += sizeof(size_t);
133 // Now pack the string.
134 memcpy(m_curptr, string, len);
135 m_curptr[len] = '\0';
141 void CDataPack::Reset() const
146 size_t CDataPack::GetPosition() const
148 return static_cast<size_t>(m_curptr - m_pBase);
151 bool CDataPack::SetPosition(size_t pos) const
157 m_curptr = m_pBase + pos;
162 cell_t CDataPack::ReadCell() const
164 if (!IsReadable(sizeof(size_t) + sizeof(cell_t)))
168 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
173 m_curptr += sizeof(size_t);
175 cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
176 m_curptr += sizeof(cell_t);
180 float CDataPack::ReadFloat() const
182 if (!IsReadable(sizeof(size_t) + sizeof(float)))
186 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
191 m_curptr += sizeof(size_t);
193 float val = *reinterpret_cast<float *>(m_curptr);
194 m_curptr += sizeof(float);
198 bool CDataPack::IsReadable(size_t bytes) const
200 return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
203 const char *CDataPack::ReadString(size_t *len) const
205 if (!IsReadable(sizeof(size_t)))
210 size_t real_len = *(size_t *)m_curptr;
212 m_curptr += sizeof(size_t);
213 char *str = (char *)m_curptr;
215 if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
225 m_curptr += real_len + 1;
230 void *CDataPack::GetMemory() const
235 void *CDataPack::ReadMemory(size_t *size) const
237 if (!IsReadable(sizeof(size_t)))
242 size_t bytecount = *(size_t *)m_curptr;
243 m_curptr += sizeof(size_t);
245 if (!IsReadable(bytecount))
250 void *ptr = m_curptr;
257 m_curptr += bytecount;