ddnet/src/engine/shared/snapshot.cpp

684 lines
15 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include "snapshot.h"
#include "compression.h"
#include "uuid_manager.h"
2010-05-29 07:25:38 +00:00
#include <climits>
2022-06-16 15:23:36 +00:00
#include <cstdlib>
2022-05-29 12:03:16 +00:00
#include <base/system.h>
2020-06-12 19:22:54 +00:00
#include <game/generated/protocolglue.h>
2020-03-29 02:36:38 +00:00
2010-05-29 07:25:38 +00:00
// CSnapshot
CSnapshotItem *CSnapshot::GetItem(int Index) const
2010-05-29 07:25:38 +00:00
{
return (CSnapshotItem *)(DataStart() + Offsets()[Index]);
}
int CSnapshot::GetItemSize(int Index) const
2010-05-29 07:25:38 +00:00
{
if(Index == m_NumItems - 1)
return (m_DataSize - Offsets()[Index]) - sizeof(CSnapshotItem);
return (Offsets()[Index + 1] - Offsets()[Index]) - sizeof(CSnapshotItem);
2010-05-29 07:25:38 +00:00
}
int CSnapshot::GetItemType(int Index) const
{
int InternalType = GetItem(Index)->Type();
return GetExternalItemType(InternalType);
}
int CSnapshot::GetExternalItemType(int InternalType) const
{
if(InternalType < OFFSET_UUID_TYPE)
{
return InternalType;
}
int TypeItemIndex = GetItemIndex((0 << 16) | InternalType); // NETOBJTYPE_EX
if(TypeItemIndex == -1 || GetItemSize(TypeItemIndex) < (int)sizeof(CUuid))
{
return InternalType;
}
CSnapshotItem *pTypeItem = GetItem(TypeItemIndex);
CUuid Uuid;
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
int_to_bytes_be(&Uuid.m_aData[i * 4], pTypeItem->Data()[i]);
return g_UuidManager.LookupUuid(Uuid);
}
int CSnapshot::GetItemIndex(int Key) const
2010-05-29 07:25:38 +00:00
{
// TODO: OPT: this should not be a linear search. very bad
for(int i = 0; i < m_NumItems; i++)
{
if(GetItem(i)->Key() == Key)
return i;
}
return -1;
2010-05-29 07:25:38 +00:00
}
void *CSnapshot::FindItem(int Type, int ID) const
{
int InternalType = Type;
if(Type >= OFFSET_UUID)
{
CUuid TypeUuid = g_UuidManager.GetUuid(Type);
int aTypeUuidItem[sizeof(CUuid) / 4];
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
aTypeUuidItem[i] = bytes_be_to_int(&TypeUuid.m_aData[i * 4]);
bool Found = false;
for(int i = 0; i < m_NumItems; i++)
{
CSnapshotItem *pItem = GetItem(i);
if(pItem->Type() == 0 && pItem->ID() >= OFFSET_UUID_TYPE) // NETOBJTYPE_EX
{
if(mem_comp(pItem->Data(), aTypeUuidItem, sizeof(CUuid)) == 0)
{
InternalType = pItem->ID();
Found = true;
break;
}
}
}
if(!Found)
{
return nullptr;
}
}
int Index = GetItemIndex((InternalType << 16) | ID);
return Index < 0 ? nullptr : GetItem(Index)->Data();
}
unsigned CSnapshot::Crc()
2010-05-29 07:25:38 +00:00
{
unsigned int Crc = 0;
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_NumItems; i++)
{
CSnapshotItem *pItem = GetItem(i);
int Size = GetItemSize(i);
for(int b = 0; b < Size / 4; b++)
2010-05-29 07:25:38 +00:00
Crc += pItem->Data()[b];
}
return Crc;
}
void CSnapshot::DebugDump()
{
dbg_msg("snapshot", "data_size=%d num_items=%d", m_DataSize, m_NumItems);
for(int i = 0; i < m_NumItems; i++)
{
CSnapshotItem *pItem = GetItem(i);
int Size = GetItemSize(i);
dbg_msg("snapshot", "\ttype=%d id=%d", pItem->Type(), pItem->ID());
for(int b = 0; b < Size / 4; b++)
2010-05-29 07:25:38 +00:00
dbg_msg("snapshot", "\t\t%3d %12d\t%08x", b, pItem->Data()[b], pItem->Data()[b]);
}
}
Validate snapshot size and member variables and demo snapshots Add `CSnapshot::IsValid` to check if a snapshot unpacked from a snapshot delta or demo is valid: - ensure number of items and data size are not negative - ensure that the actual size of the snapshot matches the size derived from its member variables - ensure item offsets are within the valid range - ensure item sizes are not negative Add `CSnapshot::TotalSize` and `CSnapshot::OffsetSize` utility functions. Minor improvements to related error messages. Fixes buffer overflow: ``` ==47744==ERROR: AddressSanitizer: global-buffer-overflow on address 0x558618e3767f at pc 0x558614b9bdfb bp 0x7ffe58a32cd0 sp 0x7ffe58a32cc0 READ of size 4 at 0x558618e3767f thread T0 0x558614b9bdfa in CSnapshotItem::Type() const src/engine/shared/snapshot.h:16 0x558615c3c911 in CSnapshot::GetItemType(int) const src/engine/shared/snapshot.cpp:29 0x558614aebaba in CClient::UnpackAndValidateSnapshot(CSnapshot*, CSnapshot*) src/engine/client/client.cpp:2264 0x558614af87cb in CClient::OnDemoPlayerSnapshot(void*, int) src/engine/client/client.cpp:2598 0x558615b9db1a in CDemoPlayer::DoTick() src/engine/shared/demo.cpp:659 0x558615babd3f in CDemoPlayer::Update(bool) src/engine/shared/demo.cpp:1007 0x558614afb08b in CClient::Update() src/engine/client/client.cpp:2686 0x558614b1d9eb in CClient::Run() src/engine/client/client.cpp:3296 0x558614b8e64f in main src/engine/client/client.cpp:4761 ``` And fixes a buffer overflow that manifests itself as an internal ASan error: ``` ================================================================= ==4755==AddressSanitizer CHECK failed: ../../../../src/libsanitizer/asan/asan_descriptions.cc:79 "((0 && "Address is not in memory and not in shadow?")) != (0)" (0x0, 0x0) 0x7f0bf5f368be in AsanCheckFailed ../../../../src/libsanitizer/asan/asan_rtl.cc:72 0x7f0bf5f54eee in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) ../../../../src/libsanitizer/sanitizer_common/sanitizer_termination.cc:77 0x7f0bf5e4cb6f in GetShadowKind ../../../../src/libsanitizer/asan/asan_descriptions.cc:79 0x7f0bf5e4cb6f in __asan::GetShadowAddressInformation(unsigned long, __asan::ShadowAddressDescription*) ../../../../src/libsanitizer/asan/asan_descriptions.cc:95 0x7f0bf5e4cb6f in __asan::GetShadowAddressInformation(unsigned long, __asan::ShadowAddressDescription*) ../../../../src/libsanitizer/asan/asan_descriptions.cc:92 0x7f0bf5e4e386 in __asan::AddressDescription::AddressDescription(unsigned long, unsigned long, bool) ../../../../src/libsanitizer/asan/asan_descriptions.cc:440 0x7f0bf5e50e94 in __asan::ErrorGeneric::ErrorGeneric(unsigned int, unsigned long, unsigned long, unsigned long, unsigned long, bool, unsigned long) ../../../../src/libsanitizer/asan/asan_errors.cc:380 0x7f0bf5f35f4d in __asan::ReportGenericError(unsigned long, unsigned long, unsigned long, unsigned long, bool, unsigned long, unsigned int, bool) ../../../../src/libsanitizer/asan/asan_report.cc:460 0x7f0bf5e86f5e in __interceptor_memset ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:762 0x558234873f1d in mem_zero src/base/system.cpp:213 0x55823481fc27 in CSnapshotBuilder::NewItem(int, int, int) src/engine/shared/snapshot.cpp:675 0x55823481be65 in CSnapshotDelta::UnpackDelta(CSnapshot*, CSnapshot*, void const*, int) src/engine/shared/snapshot.cpp:380 0x558234776641 in CDemoPlayer::DoTick() src/engine/shared/demo.cpp:631 0x5582347861a9 in CDemoPlayer::Update(bool) src/engine/shared/demo.cpp:1007 0x5582336d4c7d in CClient::Update() src/engine/client/client.cpp:2695 0x5582336f75dd in CClient::Run() src/engine/client/client.cpp:3305 0x558233768241 in main src/engine/client/client.cpp:4770 ```
2022-07-22 22:01:46 +00:00
bool CSnapshot::IsValid(size_t ActualSize) const
{
// validate total size
if(ActualSize < sizeof(CSnapshot) || m_NumItems < 0 || m_DataSize < 0 || ActualSize != TotalSize())
return false;
// validate item offsets
const int *pOffsets = Offsets();
for(int Index = 0; Index < m_NumItems; Index++)
if(pOffsets[Index] < 0 || pOffsets[Index] > m_DataSize)
return false;
// validate item sizes
for(int Index = 0; Index < m_NumItems; Index++)
if(GetItemSize(Index) < 0) // the offsets must be validated before using this
return false;
return true;
}
2010-05-29 07:25:38 +00:00
// CSnapshotDelta
struct CItemList
{
int m_Num;
int m_aKeys[64];
int m_aIndex[64];
};
enum
{
HASHLIST_SIZE = 256,
};
static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot)
{
for(int i = 0; i < HASHLIST_SIZE; i++)
pHashlist[i].m_Num = 0;
2010-05-29 07:25:38 +00:00
for(int i = 0; i < pSnapshot->NumItems(); i++)
{
int Key = pSnapshot->GetItem(i)->Key();
int HashID = ((Key >> 12) & 0xf0) | (Key & 0xf);
2010-05-29 07:25:38 +00:00
if(pHashlist[HashID].m_Num != 64)
{
pHashlist[HashID].m_aIndex[pHashlist[HashID].m_Num] = i;
pHashlist[HashID].m_aKeys[pHashlist[HashID].m_Num] = Key;
pHashlist[HashID].m_Num++;
}
}
}
static int GetItemIndexHashed(int Key, const CItemList *pHashlist)
{
int HashID = ((Key >> 12) & 0xf0) | (Key & 0xf);
for(int i = 0; i < pHashlist[HashID].m_Num; i++)
{
if(pHashlist[HashID].m_aKeys[i] == Key)
return pHashlist[HashID].m_aIndex[i];
2010-05-29 07:25:38 +00:00
}
2010-05-29 07:25:38 +00:00
return -1;
}
int CSnapshotDelta::DiffItem(int *pPast, int *pCurrent, int *pOut, int Size)
2010-05-29 07:25:38 +00:00
{
int Needed = 0;
while(Size)
{
*pOut = *pCurrent - *pPast;
2010-05-29 07:25:38 +00:00
Needed |= *pOut;
pOut++;
pPast++;
pCurrent++;
Size--;
}
2010-05-29 07:25:38 +00:00
return Needed;
}
void CSnapshotDelta::UndiffItem(int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate)
2010-05-29 07:25:38 +00:00
{
while(Size)
{
*pOut = *pPast + *pDiff;
2010-05-29 07:25:38 +00:00
if(*pDiff == 0)
*pDataRate += 1;
2010-05-29 07:25:38 +00:00
else
{
unsigned char aBuf[CVariableInt::MAX_BYTES_PACKED];
unsigned char *pEnd = CVariableInt::Pack(aBuf, *pDiff, sizeof(aBuf));
*pDataRate += (int)(pEnd - (unsigned char *)aBuf) * 8;
2010-05-29 07:25:38 +00:00
}
2010-05-29 07:25:38 +00:00
pOut++;
pPast++;
pDiff++;
Size--;
}
}
CSnapshotDelta::CSnapshotDelta()
{
mem_zero(m_aItemSizes, sizeof(m_aItemSizes));
mem_zero(m_aSnapshotDataRate, sizeof(m_aSnapshotDataRate));
mem_zero(m_aSnapshotDataUpdates, sizeof(m_aSnapshotDataUpdates));
mem_zero(&m_Empty, sizeof(m_Empty));
}
2020-10-27 17:57:14 +00:00
CSnapshotDelta::CSnapshotDelta(const CSnapshotDelta &Old)
{
2020-10-27 17:57:14 +00:00
mem_copy(m_aItemSizes, Old.m_aItemSizes, sizeof(m_aItemSizes));
mem_copy(m_aSnapshotDataRate, Old.m_aSnapshotDataRate, sizeof(m_aSnapshotDataRate));
mem_copy(m_aSnapshotDataUpdates, Old.m_aSnapshotDataUpdates, sizeof(m_aSnapshotDataUpdates));
mem_zero(&m_Empty, sizeof(m_Empty));
}
2010-05-29 07:25:38 +00:00
void CSnapshotDelta::SetStaticsize(int ItemType, int Size)
{
if(ItemType < 0 || ItemType >= MAX_NETOBJSIZES)
return;
2010-05-29 07:25:38 +00:00
m_aItemSizes[ItemType] = Size;
}
const CSnapshotDelta::CData *CSnapshotDelta::EmptyDelta() const
2010-05-29 07:25:38 +00:00
{
return &m_Empty;
}
// TODO: OPT: this should be made much faster
int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData)
{
CData *pDelta = (CData *)pDstData;
2020-10-27 17:57:14 +00:00
int *pData = (int *)pDelta->m_aData;
2010-05-29 07:25:38 +00:00
pDelta->m_NumDeletedItems = 0;
pDelta->m_NumUpdateItems = 0;
pDelta->m_NumTempItems = 0;
2020-10-27 17:57:14 +00:00
CItemList aHashlist[HASHLIST_SIZE];
GenerateHash(aHashlist, pTo);
2010-05-29 07:25:38 +00:00
// pack deleted stuff
for(int i = 0; i < pFrom->NumItems(); i++)
2010-05-29 07:25:38 +00:00
{
const CSnapshotItem *pFromItem = pFrom->GetItem(i);
2020-10-27 17:57:14 +00:00
if(GetItemIndexHashed(pFromItem->Key(), aHashlist) == -1)
2010-05-29 07:25:38 +00:00
{
// deleted
pDelta->m_NumDeletedItems++;
*pData = pFromItem->Key();
pData++;
}
}
2020-10-27 17:57:14 +00:00
GenerateHash(aHashlist, pFrom);
2010-05-29 07:25:38 +00:00
// fetch previous indices
// we do this as a separate pass because it helps the cache
int aPastIndices[CSnapshot::MAX_ITEMS];
const int NumItems = pTo->NumItems();
for(int i = 0; i < NumItems; i++)
2010-05-29 07:25:38 +00:00
{
const CSnapshotItem *pCurItem = pTo->GetItem(i); // O(1) .. O(n)
2020-10-27 17:57:14 +00:00
aPastIndices[i] = GetItemIndexHashed(pCurItem->Key(), aHashlist); // O(n) .. O(n^n)
2010-05-29 07:25:38 +00:00
}
for(int i = 0; i < NumItems; i++)
2010-05-29 07:25:38 +00:00
{
// do delta
const int ItemSize = pTo->GetItemSize(i); // O(1) .. O(n)
CSnapshotItem *pCurItem = pTo->GetItem(i); // O(1) .. O(n)
const int PastIndex = aPastIndices[i];
const bool IncludeSize = pCurItem->Type() >= MAX_NETOBJSIZES || !m_aItemSizes[pCurItem->Type()];
2010-05-29 07:25:38 +00:00
if(PastIndex != -1)
{
int *pItemDataDst = pData + 3;
CSnapshotItem *pPastItem = pFrom->GetItem(PastIndex);
if(!IncludeSize)
pItemDataDst = pData + 2;
if(DiffItem(pPastItem->Data(), pCurItem->Data(), pItemDataDst, ItemSize / 4))
2010-05-29 07:25:38 +00:00
{
*pData++ = pCurItem->Type();
*pData++ = pCurItem->ID();
if(IncludeSize)
*pData++ = ItemSize / 4;
pData += ItemSize / 4;
2010-05-29 07:25:38 +00:00
pDelta->m_NumUpdateItems++;
}
}
else
{
*pData++ = pCurItem->Type();
*pData++ = pCurItem->ID();
if(IncludeSize)
*pData++ = ItemSize / 4;
2010-05-29 07:25:38 +00:00
mem_copy(pData, pCurItem->Data(), ItemSize);
pData += ItemSize / 4;
2010-05-29 07:25:38 +00:00
pDelta->m_NumUpdateItems++;
}
}
2010-05-29 07:25:38 +00:00
if(!pDelta->m_NumDeletedItems && !pDelta->m_NumUpdateItems && !pDelta->m_NumTempItems)
return 0;
return (int)((char *)pData - (char *)pDstData);
2010-05-29 07:25:38 +00:00
}
static int RangeCheck(void *pEnd, void *pPtr, int Size)
{
if((const char *)pPtr + Size > (const char *)pEnd)
return -1;
return 0;
}
int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pSrcData, int DataSize)
2010-05-29 07:25:38 +00:00
{
CData *pDelta = (CData *)pSrcData;
2020-10-27 17:57:14 +00:00
int *pData = (int *)pDelta->m_aData;
2010-05-29 07:25:38 +00:00
int *pEnd = (int *)(((char *)pSrcData + DataSize));
CSnapshotBuilder Builder;
2010-05-29 07:25:38 +00:00
Builder.Init();
2010-05-29 07:25:38 +00:00
// unpack deleted stuff
int *pDeleted = pData;
if(pDelta->m_NumDeletedItems < 0)
return -1;
2010-05-29 07:25:38 +00:00
pData += pDelta->m_NumDeletedItems;
if(pData > pEnd)
return -1;
// copy all non deleted stuff
for(int i = 0; i < pFrom->NumItems(); i++)
{
CSnapshotItem *pFromItem = pFrom->GetItem(i);
const int ItemSize = pFrom->GetItemSize(i);
bool Keep = true;
2010-05-29 07:25:38 +00:00
for(int d = 0; d < pDelta->m_NumDeletedItems; d++)
{
if(pDeleted[d] == pFromItem->Key())
{
Keep = false;
2010-05-29 07:25:38 +00:00
break;
}
}
2010-05-29 07:25:38 +00:00
if(Keep)
{
void *pObj = Builder.NewItem(pFromItem->Type(), pFromItem->ID(), ItemSize);
if(!pObj)
return -4;
2010-05-29 07:25:38 +00:00
// keep it
mem_copy(pObj, pFromItem->Data(), ItemSize);
2010-05-29 07:25:38 +00:00
}
}
2010-05-29 07:25:38 +00:00
// unpack updated stuff
for(int i = 0; i < pDelta->m_NumUpdateItems; i++)
{
if(pData + 2 > pEnd)
2010-05-29 07:25:38 +00:00
return -1;
const int Type = *pData++;
if(Type < 0 || Type > CSnapshot::MAX_TYPE)
return -3;
const int ID = *pData++;
if(ID < 0 || ID > CSnapshot::MAX_ID)
return -3;
int ItemSize;
if(Type < MAX_NETOBJSIZES && m_aItemSizes[Type])
2010-05-29 07:25:38 +00:00
ItemSize = m_aItemSizes[Type];
else
{
if(pData + 1 > pEnd)
2010-05-29 07:25:38 +00:00
return -2;
if(*pData < 0 || *pData > INT_MAX / 4)
return -3;
2010-05-29 07:25:38 +00:00
ItemSize = (*pData++) * 4;
}
if(ItemSize < 0 || RangeCheck(pEnd, pData, ItemSize))
return -3;
const int Key = (Type << 16) | ID;
2010-05-29 07:25:38 +00:00
// create the item if needed
int *pNewData = Builder.GetItemData(Key);
2010-05-29 07:25:38 +00:00
if(!pNewData)
pNewData = (int *)Builder.NewItem(Type, ID, ItemSize);
2010-05-29 07:25:38 +00:00
if(!pNewData)
return -4;
const int FromIndex = pFrom->GetItemIndex(Key);
2010-05-29 07:25:38 +00:00
if(FromIndex != -1)
{
// we got an update so we need to apply the diff
UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / 4, &m_aSnapshotDataRate[Type]);
2010-05-29 07:25:38 +00:00
}
else // no previous, just copy the pData
{
mem_copy(pNewData, pData, ItemSize);
m_aSnapshotDataRate[Type] += ItemSize * 8;
2010-05-29 07:25:38 +00:00
}
m_aSnapshotDataUpdates[Type]++;
pData += ItemSize / 4;
2010-05-29 07:25:38 +00:00
}
2010-05-29 07:25:38 +00:00
// finish up
return Builder.Finish(pTo);
}
// CSnapshotStorage
void CSnapshotStorage::Init()
{
m_pFirst = 0;
m_pLast = 0;
}
void CSnapshotStorage::PurgeAll()
{
CHolder *pHolder = m_pFirst;
while(pHolder)
{
CHolder *pNext = pHolder->m_pNext;
free(pHolder);
2010-05-29 07:25:38 +00:00
pHolder = pNext;
}
// no more snapshots in storage
m_pFirst = 0;
m_pLast = 0;
}
void CSnapshotStorage::PurgeUntil(int Tick)
{
CHolder *pHolder = m_pFirst;
2010-05-29 07:25:38 +00:00
while(pHolder)
{
CHolder *pNext = pHolder->m_pNext;
2010-05-29 07:25:38 +00:00
if(pHolder->m_Tick >= Tick)
return; // no more to remove
free(pHolder);
2010-05-29 07:25:38 +00:00
// did we come to the end of the list?
if(!pNext)
break;
2010-05-29 07:25:38 +00:00
m_pFirst = pNext;
pNext->m_pPrev = 0x0;
2010-05-29 07:25:38 +00:00
pHolder = pNext;
}
2010-05-29 07:25:38 +00:00
// no more snapshots in storage
m_pFirst = 0;
m_pLast = 0;
}
void CSnapshotStorage::Add(int Tick, int64_t Tagtime, int DataSize, void *pData, int AltDataSize, void *pAltData)
2010-05-29 07:25:38 +00:00
{
// allocate memory for holder + snapshot_data
int TotalSize = sizeof(CHolder) + DataSize;
if(AltDataSize > 0)
{
TotalSize += AltDataSize;
}
CHolder *pHolder = (CHolder *)malloc(TotalSize);
2010-05-29 07:25:38 +00:00
// set data
pHolder->m_Tick = Tick;
pHolder->m_Tagtime = Tagtime;
pHolder->m_SnapSize = DataSize;
pHolder->m_pSnap = (CSnapshot *)(pHolder + 1);
2010-05-29 07:25:38 +00:00
mem_copy(pHolder->m_pSnap, pData, DataSize);
if(AltDataSize > 0) // create alternative if wanted
2010-05-29 07:25:38 +00:00
{
pHolder->m_pAltSnap = (CSnapshot *)(((char *)pHolder->m_pSnap) + DataSize);
mem_copy(pHolder->m_pAltSnap, pAltData, AltDataSize);
pHolder->m_AltSnapSize = AltDataSize;
2010-05-29 07:25:38 +00:00
}
else
{
2010-05-29 07:25:38 +00:00
pHolder->m_pAltSnap = 0;
pHolder->m_AltSnapSize = 0;
}
2010-05-29 07:25:38 +00:00
// link
pHolder->m_pNext = 0;
pHolder->m_pPrev = m_pLast;
if(m_pLast)
m_pLast->m_pNext = pHolder;
else
m_pFirst = pHolder;
m_pLast = pHolder;
}
2021-06-23 05:05:49 +00:00
int CSnapshotStorage::Get(int Tick, int64_t *pTagtime, CSnapshot **ppData, CSnapshot **ppAltData)
2010-05-29 07:25:38 +00:00
{
CHolder *pHolder = m_pFirst;
2010-05-29 07:25:38 +00:00
while(pHolder)
{
if(pHolder->m_Tick == Tick)
{
if(pTagtime)
*pTagtime = pHolder->m_Tagtime;
if(ppData)
*ppData = pHolder->m_pSnap;
if(ppAltData)
*ppAltData = pHolder->m_pAltSnap;
2010-05-29 07:25:38 +00:00
return pHolder->m_SnapSize;
}
2010-05-29 07:25:38 +00:00
pHolder = pHolder->m_pNext;
}
2010-05-29 07:25:38 +00:00
return -1;
}
// CSnapshotBuilder
CSnapshotBuilder::CSnapshotBuilder()
{
m_NumExtendedItemTypes = 0;
}
2010-05-29 07:25:38 +00:00
2020-03-29 02:36:38 +00:00
void CSnapshotBuilder::Init(bool Sixup)
2010-05-29 07:25:38 +00:00
{
m_DataSize = 0;
m_NumItems = 0;
2020-03-29 02:36:38 +00:00
m_Sixup = Sixup;
for(int i = 0; i < m_NumExtendedItemTypes; i++)
{
AddExtendedItemType(i);
}
2010-05-29 07:25:38 +00:00
}
CSnapshotItem *CSnapshotBuilder::GetItem(int Index)
2010-05-29 07:25:38 +00:00
{
return (CSnapshotItem *)&(m_aData[m_aOffsets[Index]]);
}
int *CSnapshotBuilder::GetItemData(int Key)
{
for(int i = 0; i < m_NumItems; i++)
2010-05-29 07:25:38 +00:00
{
if(GetItem(i)->Key() == Key)
return GetItem(i)->Data();
2010-05-29 07:25:38 +00:00
}
return 0;
}
2020-10-27 17:57:14 +00:00
int CSnapshotBuilder::Finish(void *pSnapData)
2010-05-29 07:25:38 +00:00
{
// flattern and make the snapshot
2020-10-27 17:57:14 +00:00
CSnapshot *pSnap = (CSnapshot *)pSnapData;
int OffsetSize = sizeof(int) * m_NumItems;
2010-05-29 07:25:38 +00:00
pSnap->m_DataSize = m_DataSize;
pSnap->m_NumItems = m_NumItems;
mem_copy(pSnap->Offsets(), m_aOffsets, OffsetSize);
mem_copy(pSnap->DataStart(), m_aData, m_DataSize);
return sizeof(CSnapshot) + OffsetSize + m_DataSize;
}
int CSnapshotBuilder::GetTypeFromIndex(int Index)
{
return CSnapshot::MAX_TYPE - Index;
}
void CSnapshotBuilder::AddExtendedItemType(int Index)
{
dbg_assert(0 <= Index && Index < m_NumExtendedItemTypes, "index out of range");
int TypeID = m_aExtendedItemTypes[Index];
CUuid Uuid = g_UuidManager.GetUuid(TypeID);
int *pUuidItem = (int *)NewItem(0, GetTypeFromIndex(Index), sizeof(Uuid)); // NETOBJTYPE_EX
if(pUuidItem)
{
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
pUuidItem[i] = bytes_be_to_int(&Uuid.m_aData[i * 4]);
}
}
int CSnapshotBuilder::GetExtendedItemTypeIndex(int TypeID)
{
for(int i = 0; i < m_NumExtendedItemTypes; i++)
{
if(m_aExtendedItemTypes[i] == TypeID)
{
return i;
}
}
dbg_assert(m_NumExtendedItemTypes < MAX_EXTENDED_ITEM_TYPES, "too many extended item types");
int Index = m_NumExtendedItemTypes;
m_aExtendedItemTypes[Index] = TypeID;
m_NumExtendedItemTypes++;
return Index;
}
2010-05-29 07:25:38 +00:00
void *CSnapshotBuilder::NewItem(int Type, int ID, int Size)
{
if(ID == -1)
{
return 0;
}
if(m_DataSize + sizeof(CSnapshotItem) + Size >= CSnapshot::MAX_SIZE ||
m_NumItems + 1 >= CSnapshot::MAX_ITEMS)
{
dbg_assert(m_DataSize < CSnapshot::MAX_SIZE, "too much data");
dbg_assert(m_NumItems < CSnapshot::MAX_ITEMS, "too many items");
return 0;
}
bool Extended = false;
if(Type >= OFFSET_UUID)
{
Extended = true;
Type = GetTypeFromIndex(GetExtendedItemTypeIndex(Type));
}
2010-05-29 07:25:38 +00:00
CSnapshotItem *pObj = (CSnapshotItem *)(m_aData + m_DataSize);
if(m_Sixup && !Extended)
2020-03-29 02:36:38 +00:00
{
2020-06-12 19:22:54 +00:00
if(Type >= 0)
Type = Obj_SixToSeven(Type);
else
Type *= -1;
if(Type < 0)
return pObj;
2020-03-29 02:36:38 +00:00
}
2010-05-29 07:25:38 +00:00
mem_zero(pObj, sizeof(CSnapshotItem) + Size);
pObj->m_TypeAndID = (Type << 16) | ID;
2010-05-29 07:25:38 +00:00
m_aOffsets[m_NumItems] = m_DataSize;
m_DataSize += sizeof(CSnapshotItem) + Size;
m_NumItems++;
return pObj->Data();
}