[3215] Implement received packet size check before parsing to prevent crash at wrong...
[mangos-git.git] / src / shared / ByteBuffer.h
blobb45ac852d052ad0a7406ecd44ec6a65a828fdbd1
1 /*
2 * Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _BYTEBUFFER_H
20 #define _BYTEBUFFER_H
22 #include "Common.h"
23 #include "Errors.h"
25 class ByteBuffer
27 public:
28 class error
32 const static size_t DEFAULT_SIZE = 0x1000;
34 // constructor
35 ByteBuffer(): _rpos(0), _wpos(0)
37 _storage.reserve(DEFAULT_SIZE);
39 // constructor
40 ByteBuffer(size_t res): _rpos(0), _wpos(0)
42 _storage.reserve(res);
44 // copy constructor
45 ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
47 void clear()
49 _storage.clear();
50 _rpos = _wpos = 0;
53 template <typename T> void append(T value)
55 append((uint8 *)&value, sizeof(value));
57 template <typename T> void put(size_t pos,T value)
59 put(pos,(uint8 *)&value,sizeof(value));
62 ByteBuffer &operator<<(bool value)
64 append<char>((char)value);
65 return *this;
67 ByteBuffer &operator<<(uint8 value)
69 append<uint8>(value);
70 return *this;
72 ByteBuffer &operator<<(uint16 value)
74 append<uint16>(value);
75 return *this;
77 ByteBuffer &operator<<(uint32 value)
79 append<uint32>(value);
80 return *this;
82 ByteBuffer &operator<<(uint64 value)
84 append<uint64>(value);
85 return *this;
88 // signed as in 2e complement
89 ByteBuffer &operator<<(int8 value)
91 append<int8>(value);
92 return *this;
94 ByteBuffer &operator<<(int16 value)
96 append<int16>(value);
97 return *this;
99 ByteBuffer &operator<<(int32 value)
101 append<int32>(value);
102 return *this;
104 ByteBuffer &operator<<(int64 value)
106 append<int64>(value);
107 return *this;
110 // floating points
111 ByteBuffer &operator<<(float value)
113 append<float>(value);
114 return *this;
116 ByteBuffer &operator<<(double value)
118 append<double>(value);
119 return *this;
121 ByteBuffer &operator<<(const std::string &value)
123 append((uint8 *)value.c_str(), value.length());
124 append((uint8)0);
125 return *this;
127 ByteBuffer &operator<<(const char *str)
129 append((uint8 *)str, str ? strlen(str) : 0);
130 append((uint8)0);
131 return *this;
134 ByteBuffer &operator>>(bool &value)
136 value = read<char>() > 0 ? true : false;
137 return *this;
139 ByteBuffer &operator>>(uint8 &value)
141 value = read<uint8>();
142 return *this;
144 ByteBuffer &operator>>(uint16 &value)
146 value = read<uint16>();
147 return *this;
149 ByteBuffer &operator>>(uint32 &value)
151 value = read<uint32>();
152 return *this;
154 ByteBuffer &operator>>(uint64 &value)
156 value = read<uint64>();
157 return *this;
160 //signed as in 2e complement
161 ByteBuffer &operator>>(int8 &value)
163 value = read<int8>();
164 return *this;
166 ByteBuffer &operator>>(int16 &value)
168 value = read<int16>();
169 return *this;
171 ByteBuffer &operator>>(int32 &value)
173 value = read<int32>();
174 return *this;
176 ByteBuffer &operator>>(int64 &value)
178 value = read<int64>();
179 return *this;
182 ByteBuffer &operator>>(float &value)
184 value = read<float>();
185 return *this;
187 ByteBuffer &operator>>(double &value)
189 value = read<double>();
190 return *this;
192 ByteBuffer &operator>>(std::string& value)
194 value.clear();
195 while (rpos() < size()) // prevent crash at wrong string format in packet
197 char c=read<char>();
198 if (c==0)
199 break;
200 value+=c;
202 return *this;
205 uint8 operator[](size_t pos)
207 return read<uint8>(pos);
210 size_t rpos()
212 return _rpos;
215 size_t rpos(size_t rpos)
217 _rpos = rpos;
218 return _rpos;
221 size_t wpos()
223 return _wpos;
226 size_t wpos(size_t wpos)
228 _wpos = wpos;
229 return _wpos;
232 template <typename T> T read()
234 T r=read<T>(_rpos);
235 _rpos += sizeof(T);
236 return r;
238 template <typename T> T read(size_t pos) const
240 ASSERT(pos + sizeof(T) <= size() || PrintPosError(false,pos,sizeof(T)));
241 return *((T*)&_storage[pos]);
244 void read(uint8 *dest, size_t len)
246 ASSERT(_rpos + len <= size() || PrintPosError(false,_rpos,len));
247 memcpy(dest, &_storage[_rpos], len);
248 _rpos += len;
251 const uint8 *contents() const { return &_storage[0]; };
253 inline size_t size() const { return _storage.size(); };
255 void resize(size_t newsize)
257 _storage.resize(newsize);
258 _rpos = 0;
259 _wpos = size();
261 void reserve(size_t ressize)
263 if (ressize > size()) _storage.reserve(ressize);
266 void append(const std::string& str)
268 append((uint8 *)str.c_str(),str.size() + 1);
270 void append(const char *src, size_t cnt)
272 return append((const uint8 *)src, cnt);
274 void append(const uint8 *src, size_t cnt)
276 if (!cnt) return;
278 ASSERT(size() < 10000000);
280 if (_storage.size() < _wpos + cnt)
281 _storage.resize(_wpos + cnt);
282 memcpy(&_storage[_wpos], src, cnt);
283 _wpos += cnt;
285 void append(const ByteBuffer& buffer)
287 if(buffer.size()) append(buffer.contents(),buffer.size());
290 void put(size_t pos, const uint8 *src, size_t cnt)
292 ASSERT(pos + cnt <= size() || PrintPosError(true,pos,cnt));
293 memcpy(&_storage[pos], src, cnt);
295 void print_storage()
297 printf("STORAGE_SIZE: %u\n", size() );
298 for(uint32 i = 0; i < size(); i++)
299 printf("%u - ", read<uint8>(i) );
300 printf("\n");
303 void textlike()
305 printf("STORAGE_SIZE: %u\n", size() );
306 for(uint32 i = 0; i < size(); i++)
307 printf("%c", read<uint8>(i) );
308 printf("\n");
311 void hexlike()
314 uint32 j = 1, k = 1;
315 printf("STORAGE_SIZE: %u\n", size() );
316 for(uint32 i = 0; i < size(); i++)
318 if ((i == (j*8)) && ((i != (k*16))))
320 if (read<uint8>(i) < 0x0F)
322 printf("| 0%X ", read<uint8>(i) );
324 else
326 printf("| %X ", read<uint8>(i) );
328 j++;
330 else if (i == (k*16))
332 if (read<uint8>(i) < 0x0F)
334 printf("\n0%X ", read<uint8>(i) );
336 else
338 printf("\n%X ", read<uint8>(i) );
341 k++;
342 j++;
344 else
346 if (read<uint8>(i) < 0x0F)
348 printf("0%X ", read<uint8>(i) );
350 else
352 printf("%X ", read<uint8>(i) );
356 printf("\n");
359 protected:
360 bool PrintPosError(bool add, size_t pos, size_t esize) const
362 printf("ERROR: Attempt %s in ByteBuffer (pos: %u size: %u) value with size: %u",(add ? "put" : "get"),pos, size(), esize);
364 // assert must fail after function call
365 return false;
368 size_t _rpos, _wpos;
369 std::vector<uint8> _storage;
372 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
374 b << (uint32)v.size();
375 for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++)
377 b << *i;
379 return b;
382 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v)
384 uint32 vsize;
385 b >> vsize;
386 v.clear();
387 while(vsize--)
389 T t;
390 b >> t;
391 v.push_back(t);
393 return b;
396 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
398 b << (uint32)v.size();
399 for (typename std::list<T>::iterator i = v.begin(); i != v.end(); i++)
401 b << *i;
403 return b;
406 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v)
408 uint32 vsize;
409 b >> vsize;
410 v.clear();
411 while(vsize--)
413 T t;
414 b >> t;
415 v.push_back(t);
417 return b;
420 template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
422 b << (uint32)m.size();
423 for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); i++)
425 b << i->first << i->second;
427 return b;
430 template <typename K, typename V> ByteBuffer &operator>>(ByteBuffer &b, std::map<K, V> &m)
432 uint32 msize;
433 b >> msize;
434 m.clear();
435 while(msize--)
437 K k;
438 V v;
439 b >> k >> v;
440 m.insert(make_pair(k, v));
442 return b;
444 #endif