1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_PACKER_H
4#define ENGINE_SHARED_PACKER_H
5
6class CPacker
7{
8public:
9 enum
10 {
11 PACKER_BUFFER_SIZE = 1024 * 2
12 };
13
14private:
15 unsigned char m_aBuffer[PACKER_BUFFER_SIZE];
16 unsigned char *m_pCurrent;
17 unsigned char *m_pEnd;
18 bool m_Error;
19
20public:
21 void Reset();
22 void AddInt(int i);
23 void AddString(const char *pStr, int Limit = PACKER_BUFFER_SIZE);
24 void AddRaw(const void *pData, int Size);
25
26 int Size() const { return (int)(m_pCurrent - m_aBuffer); }
27 const unsigned char *Data() const { return m_aBuffer; }
28 bool Error() const { return m_Error; }
29};
30
31class CUnpacker
32{
33 const unsigned char *m_pStart;
34 const unsigned char *m_pCurrent;
35 const unsigned char *m_pEnd;
36 bool m_Error;
37
38public:
39 enum
40 {
41 SANITIZE = 1,
42 SANITIZE_CC = 2,
43 SKIP_START_WHITESPACES = 4
44 };
45
46 void Reset(const void *pData, int Size);
47 int GetInt();
48 int GetIntOrDefault(int Default);
49 int GetUncompressedInt();
50 int GetUncompressedIntOrDefault(int Default);
51 const char *GetString(int SanitizeType = SANITIZE);
52 const unsigned char *GetRaw(int Size);
53 bool Error() const { return m_Error; }
54
55 int CompleteSize() const { return m_pEnd - m_pStart; }
56 const unsigned char *CompleteData() const { return m_pStart; }
57};
58
59#endif
60