From 4fbf4ee03dab01fda21f00851954bedc8dbc515a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Gr=C3=BCnbacher?= Date: Sun, 15 Mar 2015 22:38:49 +0100 Subject: [PATCH] Merge datapack updates from SoureMod --- amxmodx/CDataPack.cpp | 56 ++++++++++++++++++++++++++++++++++--------- amxmodx/CDataPack.h | 11 +++++++-- amxmodx/datapacks.cpp | 4 ++-- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/amxmodx/CDataPack.cpp b/amxmodx/CDataPack.cpp index 658e8484..417bf69d 100644 --- a/amxmodx/CDataPack.cpp +++ b/amxmodx/CDataPack.cpp @@ -8,7 +8,7 @@ * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, version 3.0, as published by the * Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more @@ -74,9 +74,12 @@ void CDataPack::ResetSize() 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; + *(char *)m_curptr = Raw; + m_curptr += sizeof(char); + *(size_t *)m_curptr = size; m_curptr += sizeof(size_t); @@ -86,14 +89,17 @@ size_t CDataPack::CreateMemory(size_t size, void **addr) } m_curptr += size; - m_size += sizeof(size_t) + size; + m_size += sizeof(char) + sizeof(size_t) + size; return pos; } 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); m_curptr += sizeof(size_t); @@ -101,12 +107,15 @@ void CDataPack::PackCell(cell cells) *(cell *)m_curptr = cells; 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) { - 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); m_curptr += sizeof(size_t); @@ -114,15 +123,18 @@ void CDataPack::PackFloat(float val) *(float *)m_curptr = val; 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) { 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); + *(char *)m_curptr = DataPackType::String; + m_curptr += sizeof(char); + // Pack the string length first for buffer overrun checking. *(size_t *)m_curptr = len; m_curptr += sizeof(size_t); @@ -158,10 +170,16 @@ bool CDataPack::SetPosition(size_t pos) const cell CDataPack::ReadCell() const { - if (!IsReadable(sizeof(size_t) + sizeof(cell))) + if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell))) { return 0; } + if (*reinterpret_cast(m_curptr) != DataPackType::Cell) + { + return 0; + } + m_curptr += sizeof(char); + if (*reinterpret_cast(m_curptr) != sizeof(cell)) { return 0; @@ -176,10 +194,16 @@ cell CDataPack::ReadCell() const float CDataPack::ReadFloat() const { - if (!IsReadable(sizeof(size_t) + sizeof(float))) + if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float))) { return 0; } + if (*reinterpret_cast(m_curptr) != DataPackType::Float) + { + return 0; + } + m_curptr += sizeof(char); + if (*reinterpret_cast(m_curptr) != sizeof(float)) { return 0; @@ -199,10 +223,15 @@ bool CDataPack::IsReadable(size_t bytes) const const char *CDataPack::ReadString(size_t *len) const { - if (!IsReadable(sizeof(size_t))) + if (!IsReadable(sizeof(char) + sizeof(size_t))) { return NULL; } + if (*reinterpret_cast(m_curptr) != DataPackType::String) + { + return NULL; + } + m_curptr += sizeof(char); size_t real_len = *(size_t *)m_curptr; @@ -235,6 +264,11 @@ void *CDataPack::ReadMemory(size_t *size) const { return NULL; } + if (*reinterpret_cast(m_curptr) != DataPackType::Raw) + { + return NULL; + } + m_curptr += sizeof(char); size_t bytecount = *(size_t *)m_curptr; m_curptr += sizeof(size_t); diff --git a/amxmodx/CDataPack.h b/amxmodx/CDataPack.h index 618680d7..2908ee06 100644 --- a/amxmodx/CDataPack.h +++ b/amxmodx/CDataPack.h @@ -8,7 +8,7 @@ * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, version 3.0, as published by the * Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more @@ -79,7 +79,7 @@ public: /** * @brief Returns whether or not a specified number of bytes from the current stream * position to the end can be read. - * + * * @param bytes Number of bytes to simulate reading. * @return True if can be read, false otherwise. */ @@ -160,6 +160,13 @@ private: mutable char *m_curptr; size_t m_capacity; size_t m_size; + + enum DataPackType { + Raw, + Cell, + Float, + String, + }; }; class CDataPackHandles diff --git a/amxmodx/datapacks.cpp b/amxmodx/datapacks.cpp index 82a82034..c886ae02 100644 --- a/amxmodx/datapacks.cpp +++ b/amxmodx/datapacks.cpp @@ -94,7 +94,7 @@ static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params) 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."); return 0; @@ -113,7 +113,7 @@ static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params) 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."); return 0;