Merge datapack updates from SoureMod

This commit is contained in:
Valentin Grünbacher 2015-03-15 22:38:49 +01:00
parent 32e1908859
commit 4fbf4ee03d
3 changed files with 56 additions and 15 deletions

View File

@ -74,9 +74,12 @@ void CDataPack::ResetSize()
size_t CDataPack::CreateMemory(size_t size, void **addr) size_t CDataPack::CreateMemory(size_t size, void **addr)
{ {
CheckSize(sizeof(size_t) + size); CheckSize(sizeof(char) + sizeof(size_t) + size);
size_t pos = m_curptr - m_pBase; size_t pos = m_curptr - m_pBase;
*(char *)m_curptr = Raw;
m_curptr += sizeof(char);
*(size_t *)m_curptr = size; *(size_t *)m_curptr = size;
m_curptr += sizeof(size_t); m_curptr += sizeof(size_t);
@ -86,14 +89,17 @@ size_t CDataPack::CreateMemory(size_t size, void **addr)
} }
m_curptr += size; m_curptr += size;
m_size += sizeof(size_t) + size; m_size += sizeof(char) + sizeof(size_t) + size;
return pos; return pos;
} }
void CDataPack::PackCell(cell cells) void CDataPack::PackCell(cell cells)
{ {
CheckSize(sizeof(size_t) + sizeof(cell)); CheckSize(sizeof(char) + sizeof(size_t) + sizeof(cell));
*(char *)m_curptr = DataPackType::Cell;
m_curptr += sizeof(char);
*(size_t *)m_curptr = sizeof(cell); *(size_t *)m_curptr = sizeof(cell);
m_curptr += sizeof(size_t); m_curptr += sizeof(size_t);
@ -101,12 +107,15 @@ void CDataPack::PackCell(cell cells)
*(cell *)m_curptr = cells; *(cell *)m_curptr = cells;
m_curptr += sizeof(cell); m_curptr += sizeof(cell);
m_size += sizeof(size_t) + sizeof(cell); m_size += sizeof(char) + sizeof(size_t) + sizeof(cell);
} }
void CDataPack::PackFloat(float val) void CDataPack::PackFloat(float val)
{ {
CheckSize(sizeof(size_t) + sizeof(float)); CheckSize(sizeof(char) + sizeof(size_t) + sizeof(float));
*(char *)m_curptr = DataPackType::Float;
m_curptr += sizeof(char);
*(size_t *)m_curptr = sizeof(float); *(size_t *)m_curptr = sizeof(float);
m_curptr += sizeof(size_t); m_curptr += sizeof(size_t);
@ -114,15 +123,18 @@ void CDataPack::PackFloat(float val)
*(float *)m_curptr = val; *(float *)m_curptr = val;
m_curptr += sizeof(float); m_curptr += sizeof(float);
m_size += sizeof(size_t) + sizeof(float); m_size += sizeof(char) + sizeof(size_t) + sizeof(float);
} }
void CDataPack::PackString(const char *string) void CDataPack::PackString(const char *string)
{ {
size_t len = strlen(string); size_t len = strlen(string);
size_t maxsize = sizeof(size_t) + len + 1; size_t maxsize = sizeof(char) + sizeof(size_t) + len + 1;
CheckSize(maxsize); CheckSize(maxsize);
*(char *)m_curptr = DataPackType::String;
m_curptr += sizeof(char);
// Pack the string length first for buffer overrun checking. // Pack the string length first for buffer overrun checking.
*(size_t *)m_curptr = len; *(size_t *)m_curptr = len;
m_curptr += sizeof(size_t); m_curptr += sizeof(size_t);
@ -158,10 +170,16 @@ bool CDataPack::SetPosition(size_t pos) const
cell CDataPack::ReadCell() const cell CDataPack::ReadCell() const
{ {
if (!IsReadable(sizeof(size_t) + sizeof(cell))) if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell)))
{ {
return 0; return 0;
} }
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Cell)
{
return 0;
}
m_curptr += sizeof(char);
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell)) if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell))
{ {
return 0; return 0;
@ -176,10 +194,16 @@ cell CDataPack::ReadCell() const
float CDataPack::ReadFloat() const float CDataPack::ReadFloat() const
{ {
if (!IsReadable(sizeof(size_t) + sizeof(float))) if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float)))
{ {
return 0; return 0;
} }
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Float)
{
return 0;
}
m_curptr += sizeof(char);
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float)) if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
{ {
return 0; return 0;
@ -199,10 +223,15 @@ bool CDataPack::IsReadable(size_t bytes) const
const char *CDataPack::ReadString(size_t *len) const const char *CDataPack::ReadString(size_t *len) const
{ {
if (!IsReadable(sizeof(size_t))) if (!IsReadable(sizeof(char) + sizeof(size_t)))
{ {
return NULL; return NULL;
} }
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::String)
{
return NULL;
}
m_curptr += sizeof(char);
size_t real_len = *(size_t *)m_curptr; size_t real_len = *(size_t *)m_curptr;
@ -235,6 +264,11 @@ void *CDataPack::ReadMemory(size_t *size) const
{ {
return NULL; return NULL;
} }
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Raw)
{
return NULL;
}
m_curptr += sizeof(char);
size_t bytecount = *(size_t *)m_curptr; size_t bytecount = *(size_t *)m_curptr;
m_curptr += sizeof(size_t); m_curptr += sizeof(size_t);

View File

@ -160,6 +160,13 @@ private:
mutable char *m_curptr; mutable char *m_curptr;
size_t m_capacity; size_t m_capacity;
size_t m_size; size_t m_size;
enum DataPackType {
Raw,
Cell,
Float,
String,
};
}; };
class CDataPackHandles class CDataPackHandles

View File

@ -94,7 +94,7 @@ static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params)
return 0; return 0;
} }
if (!d->IsReadable(sizeof(size_t) + sizeof(cell))) if (!d->IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell)))
{ {
LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds."); LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds.");
return 0; return 0;
@ -113,7 +113,7 @@ static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params)
return 0; return 0;
} }
if (!d->IsReadable(sizeof(size_t) + sizeof(float))) if (!d->IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float)))
{ {
LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds."); LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds.");
return 0; return 0;