blob: 5519898df0a003db1746c0dc20d44d4c02d9791f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/* (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. */
#include <base/system.h>
#include "packer.h"
#include "compression.h"
#include "engine.h"
#include "config.h"
void CPacker::Reset()
{
m_Error = 0;
m_pCurrent = m_aBuffer;
m_pEnd = m_pCurrent + PACKER_BUFFER_SIZE;
}
void CPacker::AddInt(int i)
{
if(m_Error)
return;
// make sure that we have space enough
if(m_pEnd - m_pCurrent < 6)
{
dbg_break();
m_Error = 1;
}
else
m_pCurrent = CVariableInt::Pack(m_pCurrent, i);
}
void CPacker::AddString(const char *pStr, int Limit)
{
if(m_Error)
return;
//
if(Limit > 0)
{
while(*pStr && Limit != 0)
{
*m_pCurrent++ = *pStr++;
Limit--;
if(m_pCurrent >= m_pEnd)
{
m_Error = 1;
break;
}
}
*m_pCurrent++ = 0;
}
else
{
while(*pStr)
{
*m_pCurrent++ = *pStr++;
if(m_pCurrent >= m_pEnd)
{
m_Error = 1;
break;
}
}
*m_pCurrent++ = 0;
}
}
void CPacker::AddRaw(const void *pData, int Size)
{
if(m_Error)
return;
if(m_pCurrent+Size >= m_pEnd)
{
m_Error = 1;
return;
}
const unsigned char *pSrc = (const unsigned char *)pData;
while(Size)
{
*m_pCurrent++ = *pSrc++;
Size--;
}
}
void CUnpacker::Reset(const void *pData, int Size)
{
m_Error = 0;
m_pStart = (const unsigned char *)pData;
m_pEnd = m_pStart + Size;
m_pCurrent = m_pStart;
}
int CUnpacker::GetInt()
{
if(m_Error)
return 0;
if(m_pCurrent >= m_pEnd)
{
m_Error = 1;
return 0;
}
int i;
m_pCurrent = CVariableInt::Unpack(m_pCurrent, &i);
if(m_pCurrent > m_pEnd)
{
m_Error = 1;
return 0;
}
return i;
}
const char *CUnpacker::GetString(int SanitizeType)
{
if(m_Error || m_pCurrent >= m_pEnd)
return "";
char *pPtr = (char *)m_pCurrent;
while(*m_pCurrent) // skip the string
{
m_pCurrent++;
if(m_pCurrent == m_pEnd)
{
m_Error = 1;;
return "";
}
}
m_pCurrent++;
// sanitize all strings
if(SanitizeType&SANITIZE)
str_sanitize(pPtr);
else if(SanitizeType&SANITIZE_CC)
str_sanitize_cc(pPtr);
return SanitizeType&SKIP_START_WHITESPACES ? str_skip_whitespaces(pPtr) : pPtr;
}
const unsigned char *CUnpacker::GetRaw(int Size)
{
const unsigned char *pPtr = m_pCurrent;
if(m_Error)
return 0;
// check for nasty sizes
if(Size < 0 || m_pCurrent+Size > m_pEnd)
{
m_Error = 1;
return 0;
}
// "unpack" the data
m_pCurrent += Size;
return pPtr;
}
|