ddnet/src/engine/shared/packer.h

60 lines
1.4 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
#ifndef ENGINE_SHARED_PACKER_H
#define ENGINE_SHARED_PACKER_H
2009-10-27 14:38:53 +00:00
class CPacker
{
2020-09-03 16:50:23 +00:00
public:
2009-10-27 14:38:53 +00:00
enum
{
PACKER_BUFFER_SIZE = 1024 * 2
2009-10-27 14:38:53 +00:00
};
2020-09-03 16:50:23 +00:00
private:
2009-10-27 14:38:53 +00:00
unsigned char m_aBuffer[PACKER_BUFFER_SIZE];
unsigned char *m_pCurrent;
unsigned char *m_pEnd;
int m_Error;
2009-10-27 14:38:53 +00:00
public:
void Reset();
void AddInt(int i);
void AddString(const char *pStr, int Limit);
2010-05-29 07:25:38 +00:00
void AddRaw(const void *pData, int Size);
int Size() const { return (int)(m_pCurrent - m_aBuffer); }
2009-10-27 14:38:53 +00:00
const unsigned char *Data() const { return m_aBuffer; }
bool Error() const { return m_Error; }
};
2009-10-27 14:38:53 +00:00
class CUnpacker
{
2009-10-27 14:38:53 +00:00
const unsigned char *m_pStart;
const unsigned char *m_pCurrent;
const unsigned char *m_pEnd;
int m_Error;
2009-10-27 14:38:53 +00:00
public:
enum
{
SANITIZE = 1,
SANITIZE_CC = 2,
SKIP_START_WHITESPACES = 4
};
2010-05-29 07:25:38 +00:00
void Reset(const void *pData, int Size);
2009-10-27 14:38:53 +00:00
int GetInt();
2020-04-16 08:46:43 +00:00
int GetIntOrDefault(int Default);
int GetUncompressedInt();
int GetUncompressedIntOrDefault(int Default);
const char *GetString(int SanitizeType = SANITIZE);
2009-10-27 14:38:53 +00:00
const unsigned char *GetRaw(int Size);
bool Error() const { return m_Error; }
int CompleteSize() const { return m_pEnd - m_pStart; }
const unsigned char *CompleteData() const { return m_pStart; }
2009-10-27 14:38:53 +00:00
};
2010-05-29 07:25:38 +00:00
#endif