mirror of
https://github.com/alliedmodders/amxmodx.git
synced 2024-12-26 06:45:37 +03:00
commit
0b2c33bcd2
@ -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);
|
||||||
@ -156,17 +168,32 @@ bool CDataPack::SetPosition(size_t pos) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CDataPack::CanReadCell() const
|
||||||
|
{
|
||||||
|
if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Cell)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<size_t *>(m_curptr + sizeof(char)) != sizeof(cell))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
cell CDataPack::ReadCell() const
|
cell CDataPack::ReadCell() const
|
||||||
{
|
{
|
||||||
if (!IsReadable(sizeof(size_t) + sizeof(cell)))
|
if (!CanReadCell())
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell))
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_curptr += sizeof(char);
|
||||||
m_curptr += sizeof(size_t);
|
m_curptr += sizeof(size_t);
|
||||||
|
|
||||||
cell val = *reinterpret_cast<cell *>(m_curptr);
|
cell val = *reinterpret_cast<cell *>(m_curptr);
|
||||||
@ -174,17 +201,32 @@ cell CDataPack::ReadCell() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CDataPack::CanReadFloat() const
|
||||||
|
{
|
||||||
|
if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Float)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<size_t *>(m_curptr + sizeof(char)) != sizeof(float))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
float CDataPack::ReadFloat() const
|
float CDataPack::ReadFloat() const
|
||||||
{
|
{
|
||||||
if (!IsReadable(sizeof(size_t) + sizeof(float)))
|
if (!CanReadFloat())
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_curptr += sizeof(char);
|
||||||
m_curptr += sizeof(size_t);
|
m_curptr += sizeof(size_t);
|
||||||
|
|
||||||
float val = *reinterpret_cast<float *>(m_curptr);
|
float val = *reinterpret_cast<float *>(m_curptr);
|
||||||
@ -197,21 +239,23 @@ bool CDataPack::IsReadable(size_t bytes) const
|
|||||||
return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
|
return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *CDataPack::ReadString(size_t *len) const
|
bool CDataPack::CanReadString(size_t *len) const
|
||||||
{
|
{
|
||||||
if (!IsReadable(sizeof(size_t)))
|
if (!IsReadable(sizeof(char) + sizeof(size_t)))
|
||||||
{
|
{
|
||||||
return NULL;
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::String)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t real_len = *(size_t *)m_curptr;
|
size_t real_len = *(size_t *)(m_curptr + sizeof(char));
|
||||||
|
char *str = (char *)(m_curptr + sizeof(char) + sizeof(size_t));
|
||||||
|
|
||||||
m_curptr += sizeof(size_t);
|
if ((strlen(str) != real_len) || !(IsReadable(sizeof(char) + sizeof(size_t) + real_len + 1)))
|
||||||
char *str = (char *)m_curptr;
|
|
||||||
|
|
||||||
if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
|
|
||||||
{
|
{
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len)
|
if (len)
|
||||||
@ -219,8 +263,28 @@ const char *CDataPack::ReadString(size_t *len) const
|
|||||||
*len = real_len;
|
*len = real_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *CDataPack::ReadString(size_t *len) const
|
||||||
|
{
|
||||||
|
size_t real_len;
|
||||||
|
if (!CanReadString(&real_len))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_curptr += sizeof(char);
|
||||||
|
m_curptr += sizeof(size_t);
|
||||||
|
|
||||||
|
char *str = (char *)m_curptr;
|
||||||
m_curptr += real_len + 1;
|
m_curptr += real_len + 1;
|
||||||
|
|
||||||
|
if (len)
|
||||||
|
{
|
||||||
|
*len = real_len;
|
||||||
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,29 +293,49 @@ void *CDataPack::GetMemory() const
|
|||||||
return m_curptr;
|
return m_curptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *CDataPack::ReadMemory(size_t *size) const
|
bool CDataPack::CanReadMemory(size_t *size) const
|
||||||
{
|
{
|
||||||
if (!IsReadable(sizeof(size_t)))
|
if (!IsReadable(sizeof(char) + sizeof(size_t)))
|
||||||
{
|
{
|
||||||
return NULL;
|
return false;
|
||||||
|
}
|
||||||
|
if (*reinterpret_cast<char *>(m_curptr) != DataPackType::Raw)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t bytecount = *(size_t *)m_curptr;
|
size_t bytecount = *(size_t *)(m_curptr + sizeof(char));
|
||||||
m_curptr += sizeof(size_t);
|
if (!IsReadable(sizeof(char) + sizeof(size_t) + bytecount))
|
||||||
|
|
||||||
if (!IsReadable(bytecount))
|
|
||||||
{
|
{
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ptr = m_curptr;
|
|
||||||
|
|
||||||
if (size)
|
if (size)
|
||||||
{
|
{
|
||||||
*size = bytecount;
|
*size = bytecount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *CDataPack::ReadMemory(size_t *size) const
|
||||||
|
{
|
||||||
|
size_t bytecount;
|
||||||
|
if (!CanReadMemory(&bytecount))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_curptr += sizeof(char);
|
||||||
|
m_curptr += sizeof(size_t);
|
||||||
|
|
||||||
|
void *ptr = m_curptr;
|
||||||
m_curptr += bytecount;
|
m_curptr += bytecount;
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
*size = bytecount;
|
||||||
|
}
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
@ -108,6 +108,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
void *ReadMemory(size_t *size) const;
|
void *ReadMemory(size_t *size) const;
|
||||||
|
|
||||||
|
bool CanReadCell() const;
|
||||||
|
bool CanReadFloat() const;
|
||||||
|
bool CanReadString(size_t *len) const;
|
||||||
|
bool CanReadMemory(size_t *size) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Resets the used size of the stream back to zero.
|
* @brief Resets the used size of the stream back to zero.
|
||||||
@ -160,6 +165,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
|
||||||
|
@ -94,9 +94,9 @@ static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!d->IsReadable(sizeof(size_t) + sizeof(cell)))
|
if (!d->CanReadCell())
|
||||||
{
|
{
|
||||||
LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds.");
|
LogError(amx, AMX_ERR_NATIVE, "Datapack operation is invalid.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,9 +113,9 @@ static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!d->IsReadable(sizeof(size_t) + sizeof(float)))
|
if (!d->CanReadFloat())
|
||||||
{
|
{
|
||||||
LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds.");
|
LogError(amx, AMX_ERR_NATIVE, "Datapack operation is invalid.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,14 +134,15 @@ static cell AMX_NATIVE_CALL ReadPackString(AMX* amx, cell* params)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *str;
|
if (!d->CanReadString(NULL))
|
||||||
size_t len;
|
|
||||||
if (!(str = d->ReadString(&len)))
|
|
||||||
{
|
{
|
||||||
LogError(amx, AMX_ERR_NATIVE, "DataPack operation is out of bounds.");
|
LogError(amx, AMX_ERR_NATIVE, "Datapack operation is invalid.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t len;
|
||||||
|
const char *str = d->ReadString(&len);
|
||||||
|
|
||||||
return set_amxstring_utf8(amx, params[2], str, len, params[3] + 1); // + EOS
|
return set_amxstring_utf8(amx, params[2], str, len, params[3] + 1); // + EOS
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +198,7 @@ static cell AMX_NATIVE_CALL SetPackPosition(AMX* amx, cell* params)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL IsPackReadable(AMX* amx, cell* params)
|
static cell AMX_NATIVE_CALL IsPackEnded(AMX* amx, cell* params)
|
||||||
{
|
{
|
||||||
CDataPack *d = g_DataPackHandles.lookup(params[1]);
|
CDataPack *d = g_DataPackHandles.lookup(params[1]);
|
||||||
|
|
||||||
@ -207,7 +208,7 @@ static cell AMX_NATIVE_CALL IsPackReadable(AMX* amx, cell* params)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return d->IsReadable(params[2]) ? 1 : 0;
|
return d->IsReadable(1) ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL DestroyDataPack(AMX* amx, cell* params)
|
static cell AMX_NATIVE_CALL DestroyDataPack(AMX* amx, cell* params)
|
||||||
@ -243,7 +244,7 @@ AMX_NATIVE_INFO g_DatapackNatives[] =
|
|||||||
{ "ResetPack", ResetPack },
|
{ "ResetPack", ResetPack },
|
||||||
{ "GetPackPosition", GetPackPosition },
|
{ "GetPackPosition", GetPackPosition },
|
||||||
{ "SetPackPosition", SetPackPosition },
|
{ "SetPackPosition", SetPackPosition },
|
||||||
{ "IsPackReadable", IsPackReadable },
|
{ "IsPackEnded", IsPackEnded },
|
||||||
{ "DestroyDataPack", DestroyDataPack },
|
{ "DestroyDataPack", DestroyDataPack },
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -141,6 +141,17 @@ native GetPackPosition(DataPack:pack);
|
|||||||
*/
|
*/
|
||||||
native SetPackPosition(DataPack:pack, position);
|
native SetPackPosition(DataPack:pack, position);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns if the datapack has reached its end and no more data can be read.
|
||||||
|
*
|
||||||
|
* @param pack Datapack handle
|
||||||
|
*
|
||||||
|
* @return True if datapack has reached the end, false otherwise
|
||||||
|
* @error If an invalid handle is provided, or the new position is
|
||||||
|
* out of datapack bounds, an error will be thrown.
|
||||||
|
*/
|
||||||
|
native bool:IsPackEnded(DataPack:pack);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys the datapack and frees its memory.
|
* Destroys the datapack and frees its memory.
|
||||||
*
|
*
|
||||||
|
@ -50,38 +50,44 @@ public datapacktest()
|
|||||||
new refString[] = "I'm a little teapot.";
|
new refString[] = "I'm a little teapot.";
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
WritePackCell(pack, refCell); // 8
|
new cellPos = GetPackPosition(pack);
|
||||||
WritePackString(pack, refString); // 25 (sizeof string + 4)
|
WritePackCell(pack, refCell);
|
||||||
WritePackFloat(pack, refFloat); // 8
|
new floatPos = GetPackPosition(pack);
|
||||||
|
WritePackFloat(pack, refFloat);
|
||||||
|
new strPos = GetPackPosition(pack);
|
||||||
|
WritePackString(pack, refString);
|
||||||
|
new endPos = GetPackPosition(pack);
|
||||||
|
|
||||||
test("Position #1 test", .pass = GetPackPosition(pack) == 41);
|
test("Write position test",
|
||||||
test("Readable #1 test", .pass = !IsPackReadable(pack, 41));
|
.pass = (cellPos != floatPos && cellPos != strPos && cellPos != endPos
|
||||||
|
&& floatPos != strPos && floatPos != endPos && strPos != endPos));
|
||||||
|
|
||||||
//resets the index to the beginning, necessary for read.
|
//resets the index to the beginning, necessary for read.
|
||||||
ResetPack(pack);
|
ResetPack(pack);
|
||||||
|
|
||||||
test("Position #2 test", .pass = GetPackPosition(pack) == 0 );
|
test("Position #1 test", .pass = (GetPackPosition(pack) == cellPos));
|
||||||
test("Readable #2 test", .pass = IsPackReadable(pack, 15));
|
test("Readable #1 test", .pass = !IsPackEnded(pack));
|
||||||
|
|
||||||
// Read
|
|
||||||
new cellValue = ReadPackCell(pack);
|
new cellValue = ReadPackCell(pack);
|
||||||
|
test("Cell test", .pass = (cellValue == refCell));
|
||||||
|
|
||||||
|
test("Position #2 test", .pass = (GetPackPosition(pack) == floatPos));
|
||||||
|
test("Readable #2 test", .pass = !IsPackEnded(pack));
|
||||||
|
|
||||||
|
new Float:floatValue = ReadPackFloat(pack);
|
||||||
|
test("Float test", .pass = (floatValue == refFloat));
|
||||||
|
|
||||||
|
test("Position #3 test", .pass = (GetPackPosition(pack) == strPos));
|
||||||
|
test("Readable #3 test", .pass = !IsPackEnded(pack));
|
||||||
|
|
||||||
new buffer[1024];
|
new buffer[1024];
|
||||||
ReadPackString(pack, buffer, 1024);
|
ReadPackString(pack, buffer, 1024);
|
||||||
new Float:floatvalue = ReadPackFloat(pack);
|
test("String test #1", .pass = bool:equal(buffer, refString));
|
||||||
|
|
||||||
test("Cell test", .pass = cellValue == refCell);
|
test("End test", .pass = IsPackEnded(pack));
|
||||||
test("String test", .pass = bool:equal(buffer, refString));
|
|
||||||
test("Float test #1", .pass = floatvalue == refFloat);
|
|
||||||
|
|
||||||
SetPackPosition(pack, 33);
|
|
||||||
test("Set Position test", .pass = GetPackPosition(pack) == 33);
|
|
||||||
|
|
||||||
WritePackFloat(pack, refFloat + 1);
|
|
||||||
SetPackPosition(pack, 33);
|
|
||||||
test("Float test #2", .pass = ReadPackFloat(pack) == refFloat + 1);
|
|
||||||
|
|
||||||
ResetPack(pack, .clear = true);
|
ResetPack(pack, .clear = true);
|
||||||
test("Clear test", .pass = !IsPackReadable(pack, 15));
|
test("Clear test", .pass = IsPackEnded(pack));
|
||||||
|
|
||||||
DestroyDataPack(pack);
|
DestroyDataPack(pack);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user