Fixed chat trigger crash regression on ep1 (
bug 5394, r=asherkin).
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
34 #include "CDataPack.h"
36 #define DATAPACK_INITIAL_SIZE 512
38 CDataPack::CDataPack()
40 m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
41 m_capacity = DATAPACK_INITIAL_SIZE;
45 CDataPack::~CDataPack()
50 void CDataPack::Initialize()
56 void CDataPack::CheckSize(size_t typesize)
58 if (m_curptr - m_pBase + typesize <= m_capacity)
63 size_t pos = m_curptr - m_pBase;
67 m_pBase = (char *)realloc(m_pBase, m_capacity);
68 m_curptr = m_pBase + pos;
69 } while (m_curptr - m_pBase + typesize > m_capacity);
72 void CDataPack::ResetSize()
77 size_t CDataPack::CreateMemory(size_t size, void **addr)
79 CheckSize(sizeof(size_t) + size);
80 size_t pos = m_curptr - m_pBase;
82 *(size_t *)m_curptr = size;
83 m_curptr += sizeof(size_t);
91 m_size += sizeof(size_t) + size;
96 void CDataPack::PackCell(cell_t cell)
98 CheckSize(sizeof(size_t) + sizeof(cell_t));
100 *(size_t *)m_curptr = sizeof(cell_t);
101 m_curptr += sizeof(size_t);
103 *(cell_t *)m_curptr = cell;
104 m_curptr += sizeof(cell_t);
106 m_size += sizeof(size_t) + sizeof(cell_t);
109 void CDataPack::PackFloat(float val)
111 CheckSize(sizeof(size_t) + sizeof(float));
113 *(size_t *)m_curptr = sizeof(float);
114 m_curptr += sizeof(size_t);
116 *(float *)m_curptr = val;
117 m_curptr += sizeof(float);
119 m_size += sizeof(size_t) + sizeof(float);
122 void CDataPack::PackString(const char *string)
124 size_t len = strlen(string);
125 size_t maxsize = sizeof(size_t) + len + 1;
128 // Pack the string length first for buffer overrun checking.
129 *(size_t *)m_curptr = len;
130 m_curptr += sizeof(size_t);
132 // Now pack the string.
133 memcpy(m_curptr, string, len);
134 m_curptr[len] = '\0';
140 void CDataPack::Reset() const
145 size_t CDataPack::GetPosition() const
147 return static_cast<size_t>(m_curptr - m_pBase);
150 bool CDataPack::SetPosition(size_t pos) const
156 m_curptr = m_pBase + pos;
161 cell_t CDataPack::ReadCell() const
163 if (!IsReadable(sizeof(size_t) + sizeof(cell_t)))
167 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
172 m_curptr += sizeof(size_t);
174 cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
175 m_curptr += sizeof(cell_t);
179 float CDataPack::ReadFloat() const
181 if (!IsReadable(sizeof(size_t) + sizeof(float)))
185 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
190 m_curptr += sizeof(size_t);
192 float val = *reinterpret_cast<float *>(m_curptr);
193 m_curptr += sizeof(float);
197 bool CDataPack::IsReadable(size_t bytes) const
199 return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
202 const char *CDataPack::ReadString(size_t *len) const
204 if (!IsReadable(sizeof(size_t)))
209 size_t real_len = *(size_t *)m_curptr;
211 m_curptr += sizeof(size_t);
212 char *str = (char *)m_curptr;
214 if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
224 m_curptr += real_len + 1;
229 void *CDataPack::GetMemory() const
234 void *CDataPack::ReadMemory(size_t *size) const
236 if (!IsReadable(sizeof(size_t)))
241 size_t bytecount = *(size_t *)m_curptr;
242 m_curptr += sizeof(size_t);
244 if (!IsReadable(bytecount))
249 void *ptr = m_curptr;
256 m_curptr += bytecount;