2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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
25 #include "Utilities/ByteConverter.h"
30 const static size_t DEFAULT_SIZE
= 0x1000;
33 ByteBuffer(): _rpos(0), _wpos(0)
35 _storage
.reserve(DEFAULT_SIZE
);
39 ByteBuffer(size_t res
): _rpos(0), _wpos(0)
41 _storage
.reserve(res
);
45 ByteBuffer(const ByteBuffer
&buf
): _rpos(buf
._rpos
), _wpos(buf
._wpos
), _storage(buf
._storage
) { }
53 template <typename T
> void append(T value
)
56 append((uint8
*)&value
, sizeof(value
));
59 template <typename T
> void put(size_t pos
,T value
)
62 put(pos
,(uint8
*)&value
,sizeof(value
));
65 ByteBuffer
&operator<<(uint8 value
)
71 ByteBuffer
&operator<<(uint16 value
)
73 append
<uint16
>(value
);
77 ByteBuffer
&operator<<(uint32 value
)
79 append
<uint32
>(value
);
83 ByteBuffer
&operator<<(uint64 value
)
85 append
<uint64
>(value
);
89 // signed as in 2e complement
90 ByteBuffer
&operator<<(int8 value
)
96 ByteBuffer
&operator<<(int16 value
)
102 ByteBuffer
&operator<<(int32 value
)
104 append
<int32
>(value
);
108 ByteBuffer
&operator<<(int64 value
)
110 append
<int64
>(value
);
115 ByteBuffer
&operator<<(float value
)
117 append
<float>(value
);
121 ByteBuffer
&operator<<(double value
)
123 append
<double>(value
);
127 ByteBuffer
&operator<<(const std::string
&value
)
129 append((uint8
const *)value
.c_str(), value
.length());
134 ByteBuffer
&operator<<(const char *str
)
136 append((uint8
const *)str
, str
? strlen(str
) : 0);
141 ByteBuffer
&operator>>(bool &value
)
143 value
= read
<char>() > 0 ? true : false;
147 ByteBuffer
&operator>>(uint8
&value
)
149 value
= read
<uint8
>();
153 ByteBuffer
&operator>>(uint16
&value
)
155 value
= read
<uint16
>();
159 ByteBuffer
&operator>>(uint32
&value
)
161 value
= read
<uint32
>();
165 ByteBuffer
&operator>>(uint64
&value
)
167 value
= read
<uint64
>();
171 //signed as in 2e complement
172 ByteBuffer
&operator>>(int8
&value
)
174 value
= read
<int8
>();
178 ByteBuffer
&operator>>(int16
&value
)
180 value
= read
<int16
>();
184 ByteBuffer
&operator>>(int32
&value
)
186 value
= read
<int32
>();
190 ByteBuffer
&operator>>(int64
&value
)
192 value
= read
<int64
>();
196 ByteBuffer
&operator>>(float &value
)
198 value
= read
<float>();
202 ByteBuffer
&operator>>(double &value
)
204 value
= read
<double>();
208 ByteBuffer
&operator>>(std::string
& value
)
211 while (rpos() < size()) // prevent crash at wrong string format in packet
213 char c
= read
<char>();
221 uint8
operator[](size_t pos
) const
223 return read
<uint8
>(pos
);
226 size_t rpos() const { return _rpos
; }
228 size_t rpos(size_t rpos_
)
234 size_t wpos() const { return _wpos
; }
236 size_t wpos(size_t wpos_
)
242 template <typename T
> T
read()
244 T r
= read
<T
>(_rpos
);
249 template <typename T
> T
read(size_t pos
) const
251 ASSERT(pos
+ sizeof(T
) <= size() || PrintPosError(false, pos
, sizeof(T
)));
252 T val
= *((T
const*)&_storage
[pos
]);
257 void read(uint8
*dest
, size_t len
)
259 ASSERT(_rpos
+ len
<= size() || PrintPosError(false, _rpos
, len
));
260 memcpy(dest
, &_storage
[_rpos
], len
);
264 bool readPackGUID(uint64
& guid
)
266 if(rpos() + 1 > size())
274 for(int i
= 0; i
< 8; ++i
)
276 if(guidmark
& (uint8(1) << i
))
278 if(rpos() + 1 > size())
283 guid
|= (uint64(bit
) << (i
* 8));
290 const uint8
*contents() const { return &_storage
[0]; }
292 size_t size() const { return _storage
.size(); }
293 bool empty() const { return _storage
.empty(); }
295 void resize(size_t newsize
)
297 _storage
.resize(newsize
);
302 void reserve(size_t ressize
)
304 if (ressize
> size())
305 _storage
.reserve(ressize
);
308 void append(const std::string
& str
)
310 append((uint8
const*)str
.c_str(), str
.size() + 1);
313 void append(const char *src
, size_t cnt
)
315 return append((const uint8
*)src
, cnt
);
318 template<class T
> void append(const T
*src
, size_t cnt
)
320 return append((const uint8
*)src
, cnt
* sizeof(T
));
323 void append(const uint8
*src
, size_t cnt
)
328 ASSERT(size() < 10000000);
330 if (_storage
.size() < _wpos
+ cnt
)
331 _storage
.resize(_wpos
+ cnt
);
332 memcpy(&_storage
[_wpos
], src
, cnt
);
336 void append(const ByteBuffer
& buffer
)
339 append(buffer
.contents(), buffer
.wpos());
342 // can be used in SMSG_MONSTER_MOVE opcode
343 void appendPackXYZ(float x
, float y
, float z
)
346 packed
|= ((int)(x
/ 0.25f
) & 0x7FF);
347 packed
|= ((int)(y
/ 0.25f
) & 0x7FF) << 11;
348 packed
|= ((int)(z
/ 0.25f
) & 0x3FF) << 22;
352 void appendPackGUID(uint64 guid
)
354 if (_storage
.size() < _wpos
+ sizeof(guid
) + 1)
355 _storage
.resize(_wpos
+ sizeof(guid
) + 1);
357 size_t mask_position
= wpos();
359 for(uint8 i
= 0; i
< 8; ++i
)
363 _storage
[mask_position
] |= uint8(1 << i
);
364 *this << uint8(guid
& 0xFF);
371 void put(size_t pos
, const uint8
*src
, size_t cnt
)
373 ASSERT(pos
+ cnt
<= size() || PrintPosError(true, pos
, cnt
));
374 memcpy(&_storage
[pos
], src
, cnt
);
377 void print_storage() const
379 if(!sLog
.IsOutDebug()) // optimize disabled debug output
382 sLog
.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
383 for(uint32 i
= 0; i
< size(); ++i
)
384 sLog
.outDebugInLine("%u - ", read
<uint8
>(i
) );
388 void textlike() const
390 if(!sLog
.IsOutDebug()) // optimize disabled debug output
393 sLog
.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
394 for(uint32 i
= 0; i
< size(); ++i
)
395 sLog
.outDebugInLine("%c", read
<uint8
>(i
) );
401 if(!sLog
.IsOutDebug()) // optimize disabled debug output
405 sLog
.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
407 if(sLog
.IsIncludeTime())
408 sLog
.outDebugInLine(" ");
410 for(uint32 i
= 0; i
< size(); ++i
)
412 if ((i
== (j
* 8)) && ((i
!= (k
* 16))))
414 if (read
<uint8
>(i
) < 0x10)
416 sLog
.outDebugInLine("| 0%X ", read
<uint8
>(i
) );
420 sLog
.outDebugInLine("| %X ", read
<uint8
>(i
) );
424 else if (i
== (k
* 16))
426 if (read
<uint8
>(i
) < 0x10)
428 sLog
.outDebugInLine("\n");
429 if(sLog
.IsIncludeTime())
430 sLog
.outDebugInLine(" ");
432 sLog
.outDebugInLine("0%X ", read
<uint8
>(i
) );
436 sLog
.outDebugInLine("\n");
437 if(sLog
.IsIncludeTime())
438 sLog
.outDebugInLine(" ");
440 sLog
.outDebugInLine("%X ", read
<uint8
>(i
) );
448 if (read
<uint8
>(i
) < 0x10)
450 sLog
.outDebugInLine("0%X ", read
<uint8
>(i
) );
454 sLog
.outDebugInLine("%X ", read
<uint8
>(i
) );
458 sLog
.outDebugInLine("\n");
462 bool PrintPosError(bool add
, size_t pos
, size_t esize
) const
464 sLog
.outError("ERROR: Attempt %s in ByteBuffer (pos: %lu size: %lu) value with size: %lu",(add
? "put" : "get"),(unsigned long)pos
, (unsigned long)size(), (unsigned long)esize
);
466 // assert must fail after function call
471 std::vector
<uint8
> _storage
;
474 template <typename T
> ByteBuffer
&operator<<(ByteBuffer
&b
, std::vector
<T
> v
)
476 b
<< (uint32
)v
.size();
477 for (typename
std::vector
<T
>::iterator i
= v
.begin(); i
!= v
.end(); ++i
)
484 template <typename T
> ByteBuffer
&operator>>(ByteBuffer
&b
, std::vector
<T
> &v
)
498 template <typename T
> ByteBuffer
&operator<<(ByteBuffer
&b
, std::list
<T
> v
)
500 b
<< (uint32
)v
.size();
501 for (typename
std::list
<T
>::iterator i
= v
.begin(); i
!= v
.end(); ++i
)
508 template <typename T
> ByteBuffer
&operator>>(ByteBuffer
&b
, std::list
<T
> &v
)
522 template <typename K
, typename V
> ByteBuffer
&operator<<(ByteBuffer
&b
, std::map
<K
, V
> &m
)
524 b
<< (uint32
)m
.size();
525 for (typename
std::map
<K
, V
>::iterator i
= m
.begin(); i
!= m
.end(); ++i
)
527 b
<< i
->first
<< i
->second
;
532 template <typename K
, typename V
> ByteBuffer
&operator>>(ByteBuffer
&b
, std::map
<K
, V
> &m
)
542 m
.insert(make_pair(k
, v
));