fixed a state bug in the gameconf parser that would break parsing after a signature section was done
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
17 #include "CDataPack.h"
19 #define DATAPACK_INITIAL_SIZE 512
21 CDataPack::CDataPack()
23 m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
24 m_capacity = DATAPACK_INITIAL_SIZE;
28 CDataPack::~CDataPack()
33 void CDataPack::Initialize()
39 void CDataPack::CheckSize(size_t typesize)
41 if (m_curptr - m_pBase + typesize <= m_capacity)
46 size_t pos = m_curptr - m_pBase;
50 m_pBase = (char *)realloc(m_pBase, m_capacity);
51 m_curptr = m_pBase + pos;
52 } while (m_curptr - m_pBase + typesize > m_capacity);
55 void CDataPack::ResetSize()
60 size_t CDataPack::CreateMemory(size_t size, void **addr)
62 CheckSize(sizeof(size_t) + size);
63 size_t pos = m_curptr - m_pBase;
65 *(size_t *)m_curptr = size;
66 m_curptr += sizeof(size_t);
74 m_size += sizeof(size_t) + size;
79 void CDataPack::PackCell(cell_t cell)
81 CheckSize(sizeof(size_t) + sizeof(cell_t));
83 *(size_t *)m_curptr = sizeof(cell_t);
84 m_curptr += sizeof(size_t);
86 *(cell_t *)m_curptr = cell;
87 m_curptr += sizeof(cell_t);
89 m_size += sizeof(size_t) + sizeof(cell_t);
92 void CDataPack::PackFloat(float val)
94 CheckSize(sizeof(size_t) + sizeof(float));
96 *(size_t *)m_curptr = sizeof(float);
97 m_curptr += sizeof(size_t);
99 *(float *)m_curptr = val;
100 m_curptr += sizeof(float);
102 m_size += sizeof(size_t) + sizeof(float);
105 void CDataPack::PackString(const char *string)
107 size_t len = strlen(string);
108 size_t maxsize = sizeof(size_t) + len + 1;
111 // Pack the string length first for buffer overrun checking.
112 *(size_t *)m_curptr = len;
113 m_curptr += sizeof(size_t);
115 // Now pack the string.
116 memcpy(m_curptr, string, len);
117 m_curptr[len] = '\0';
123 void CDataPack::Reset() const
128 size_t CDataPack::GetPosition() const
130 return static_cast<size_t>(m_curptr - m_pBase);
133 bool CDataPack::SetPosition(size_t pos) const
139 m_curptr = m_pBase + pos;
144 cell_t CDataPack::ReadCell() const
146 if (!IsReadable(sizeof(size_t) + sizeof(cell_t)))
150 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
155 m_curptr += sizeof(size_t);
157 cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
158 m_curptr += sizeof(cell_t);
162 float CDataPack::ReadFloat() const
164 if (!IsReadable(sizeof(size_t) + sizeof(float)))
168 if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
173 m_curptr += sizeof(size_t);
175 float val = *reinterpret_cast<float *>(m_curptr);
176 m_curptr += sizeof(float);
180 bool CDataPack::IsReadable(size_t bytes) const
182 return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
185 const char *CDataPack::ReadString(size_t *len) const
187 if (!IsReadable(sizeof(size_t)))
192 size_t real_len = *(size_t *)m_curptr;
194 m_curptr += sizeof(size_t);
195 char *str = (char *)m_curptr;
197 if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
207 m_curptr += real_len + 1;
212 void *CDataPack::GetMemory() const
217 void *CDataPack::ReadMemory(size_t *size) const
219 if (!IsReadable(sizeof(size_t)))
224 size_t bytecount = *(size_t *)m_curptr;
225 m_curptr += sizeof(size_t);
227 if (!IsReadable(bytecount))
232 void *ptr = m_curptr;
239 m_curptr += bytecount;