Fixed possible crash
[getmangos.git] / src / game / tools.cpp
blobf5ebde52d19575d4f0511495f041439cb73506df
1 /*
2 * Copyright (C) 2005-2008 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
19 #include "Tools.h"
21 // THIS CAN BE A LOT FASTER
22 bool readGUID(WorldPacket & data, uint64& guid)
24 if(data.rpos()+1 > data.size())
25 return false;
27 uint8 guidmark=0;
28 uint8 bit;
29 uint8 shiftdata=0x1;
30 uint64 Temp=0;
32 guid = 0;
34 data >> guidmark;
35 for(int i=0;i<8;i++)
37 if(guidmark & shiftdata)
39 Temp = 0;
41 if(data.rpos()+1 > data.size())
42 return false;
44 data >> bit;
45 Temp = bit;
46 Temp <<= i*8;
47 guid |= Temp;
49 shiftdata=shiftdata<<1;
52 return true;
55 void writeGUID(WorldPacket & data, uint64 & guid)
57 uint8 RAWmask = 0;
58 uint8 PackedGuid[8] = {0,0,0,0,0,0,0,0};
60 int j = 1;
61 uint8 * test = (uint8*)&guid;
63 if (*test)
65 PackedGuid[j] = *test;
66 RAWmask |= 1;
67 ++j;
69 if (*(test+1))
71 PackedGuid[j] = *(test+1);
72 RAWmask |= 2;
73 ++j;
75 if (*(test+2))
77 PackedGuid[j] = *(test+2);
78 RAWmask |= 4;
79 ++j;
81 if (*(test+3))
83 PackedGuid[j] = *(test+3);
84 RAWmask |= 8;
85 ++j;
87 if (*(test+4))
89 PackedGuid[j] = *(test+4);
90 RAWmask |= 16;
91 ++j;
93 if (*(test+5))
95 PackedGuid[j] = *(test+5);
96 RAWmask |= 32;
97 ++j;
99 if (*(test+6))
101 PackedGuid[j] = *(test+6);
102 RAWmask |= 64;
103 ++j;
105 if (*(test+7))
107 PackedGuid[j] = *(test+7);
108 RAWmask |= 128;
109 ++j;
111 PackedGuid[0] = RAWmask;
113 data.append(PackedGuid,j);