aboutsummaryrefslogtreecommitdiff
path: root/src/game/AddonHandler.cpp
blob: 37fd719253f23a2df9fd8286250857676937aab4 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
 *
 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "AddonHandler.h"
#include "Database/DatabaseEnv.h"
#include "Opcodes.h"
#include "Log.h"
#include "Policies/SingletonImp.h"
#include "zlib/zlib.h"

INSTANTIATE_SINGLETON_1( AddonHandler );

AddonHandler::AddonHandler()
{
}

AddonHandler::~AddonHandler()
{
}

bool AddonHandler::BuildAddonPacket(WorldPacket *Source, WorldPacket *Target)
{
    ByteBuffer AddOnPacked;
    uLongf AddonRealSize;
    uint32 CurrentPosition;
    uint32 TempValue;

    unsigned char tdata[256] =
    {
        0xC3, 0x5B, 0x50, 0x84, 0xB9, 0x3E, 0x32, 0x42, 0x8C, 0xD0, 0xC7, 0x48, 0xFA, 0x0E, 0x5D, 0x54,
        0x5A, 0xA3, 0x0E, 0x14, 0xBA, 0x9E, 0x0D, 0xB9, 0x5D, 0x8B, 0xEE, 0xB6, 0x84, 0x93, 0x45, 0x75,
        0xFF, 0x31, 0xFE, 0x2F, 0x64, 0x3F, 0x3D, 0x6D, 0x07, 0xD9, 0x44, 0x9B, 0x40, 0x85, 0x59, 0x34,
        0x4E, 0x10, 0xE1, 0xE7, 0x43, 0x69, 0xEF, 0x7C, 0x16, 0xFC, 0xB4, 0xED, 0x1B, 0x95, 0x28, 0xA8,
        0x23, 0x76, 0x51, 0x31, 0x57, 0x30, 0x2B, 0x79, 0x08, 0x50, 0x10, 0x1C, 0x4A, 0x1A, 0x2C, 0xC8,
        0x8B, 0x8F, 0x05, 0x2D, 0x22, 0x3D, 0xDB, 0x5A, 0x24, 0x7A, 0x0F, 0x13, 0x50, 0x37, 0x8F, 0x5A,
        0xCC, 0x9E, 0x04, 0x44, 0x0E, 0x87, 0x01, 0xD4, 0xA3, 0x15, 0x94, 0x16, 0x34, 0xC6, 0xC2, 0xC3,
        0xFB, 0x49, 0xFE, 0xE1, 0xF9, 0xDA, 0x8C, 0x50, 0x3C, 0xBE, 0x2C, 0xBB, 0x57, 0xED, 0x46, 0xB9,
        0xAD, 0x8B, 0xC6, 0xDF, 0x0E, 0xD6, 0x0F, 0xBE, 0x80, 0xB3, 0x8B, 0x1E, 0x77, 0xCF, 0xAD, 0x22,
        0xCF, 0xB7, 0x4B, 0xCF, 0xFB, 0xF0, 0x6B, 0x11, 0x45, 0x2D, 0x7A, 0x81, 0x18, 0xF2, 0x92, 0x7E,
        0x98, 0x56, 0x5D, 0x5E, 0x69, 0x72, 0x0A, 0x0D, 0x03, 0x0A, 0x85, 0xA2, 0x85, 0x9C, 0xCB, 0xFB,
        0x56, 0x6E, 0x8F, 0x44, 0xBB, 0x8F, 0x02, 0x22, 0x68, 0x63, 0x97, 0xBC, 0x85, 0xBA, 0xA8, 0xF7,
        0xB5, 0x40, 0x68, 0x3C, 0x77, 0x86, 0x6F, 0x4B, 0xD7, 0x88, 0xCA, 0x8A, 0xD7, 0xCE, 0x36, 0xF0,
        0x45, 0x6E, 0xD5, 0x64, 0x79, 0x0F, 0x17, 0xFC, 0x64, 0xDD, 0x10, 0x6F, 0xF3, 0xF5, 0xE0, 0xA6,
        0xC3, 0xFB, 0x1B, 0x8C, 0x29, 0xEF, 0x8E, 0xE5, 0x34, 0xCB, 0xD1, 0x2A, 0xCE, 0x79, 0xC3, 0x9A,
        0x0D, 0x36, 0xEA, 0x01, 0xE0, 0xAA, 0x91, 0x20, 0x54, 0xF0, 0x72, 0xD8, 0x1E, 0xC7, 0x89, 0xD2
    };

    // broken addon packet, can't be received from real client
    if (Source->rpos() + 4 > Source->size())
        return false;

    *Source >> TempValue;                                   // get real size of the packed structure

    // empty addon packet, nothing process, can't be received from real client
    if(!TempValue)
        return false;

    AddonRealSize = TempValue;                              // temp value because ZLIB only excepts uLongf

    CurrentPosition = Source->rpos();                       // get the position of the pointer in the structure

    AddOnPacked.resize(AddonRealSize);                      // resize target for zlib action

    if (!uncompress(const_cast<uint8*>(AddOnPacked.contents()), &AddonRealSize, const_cast<uint8*>((*Source).contents() + CurrentPosition), (*Source).size() - CurrentPosition)!= Z_OK)
    {
        Target->Initialize(SMSG_ADDON_INFO);

        uint32 addonsCount;
        AddOnPacked >> addonsCount;                         // addons count?

        for(uint32 i = 0; i < addonsCount; ++i)
        {
            std::string addonName;
            uint8 enabled;
            uint32 crc, unk2;

            // check next addon data format correctness
            if(AddOnPacked.rpos()+1 > AddOnPacked.size())
                return false;

            AddOnPacked >> addonName;

            // recheck next addon data format correctness
            if(AddOnPacked.rpos()+1+4+4 > AddOnPacked.size())
                return false;

            AddOnPacked >> enabled >> crc >> unk2;

            sLog.outDebug("ADDON: Name: %s, Enabled: 0x%x, CRC: 0x%x, Unknown2: 0x%x", addonName.c_str(), enabled, crc, unk2);

            uint8 state = (enabled ? 2 : 1);
            *Target << uint8(state);

            uint8 unk1 = (enabled ? 1 : 0);
            *Target << uint8(unk1);
            if (unk1)
            {
                uint8 unk2 = (crc != 0x4c1c776d);           // If addon is Standard addon CRC
                *Target << uint8(unk2);
                if (unk2)
                    Target->append(tdata, sizeof(tdata));

                *Target << uint32(0);
            }

            uint8 unk3 = (enabled ? 0 : 1);
            *Target << uint8(unk3);
            if (unk3)
            {
                // String, 256 (null terminated?)
                *Target << uint8(0);
            }
        }

        uint32 unk4;
        AddOnPacked >> unk4;

        uint32 count = 0;
        *Target << uint32(count);
        /*for(uint32 i = 0; i < count; ++i)
        {
            uint32
            string (16 bytes)
            string (16 bytes)
            uint32
        }*/

        if(AddOnPacked.rpos() != AddOnPacked.size())
            sLog.outDebug("packet under read!");
    }
    else
    {
        sLog.outError("Addon packet uncompress error :(");
        return false;
    }
    return true;
}

/* Code use in 1.10.2 when client not ignore ban state sended for addons. Saved for reference if client switch to use server ban state information
void AddonHandler::BuildAddonPacket(WorldPacket* Source, WorldPacket* Target, uint32 Packetoffset)
{
    ByteBuffer AddOnPacked;
    uLongf AddonRealSize;
    uint32 CurrentPosition;
    uint32 TempValue;

    *Source >> TempValue;                                   //get real size of the packed structure

    AddonRealSize = TempValue;                              //temp value becouse ZLIB only excepts uLongf

    CurrentPosition = Source->rpos();                       //get the position of the pointer in the structure

    AddOnPacked.resize(AddonRealSize);                      //resize target for zlib action

    if (!uncompress((uint8*)AddOnPacked.contents(), &AddonRealSize, (uint8*)(*Source).contents() + CurrentPosition, (*Source).size() - CurrentPosition)!= Z_OK)
    {
        bool* AddonAllowed = new bool;                      //handle addon check and enable-ing

        uint32 Unknown1;
        uint8 Unknown0;

        AddOnPacked >> Unknown0;
        AddOnPacked >> Unknown1;

        Target->Initialize(SMSG_ADDON_INFO);

        uint32 i = 5;                                       //offset for addon extraction
        while(i != AddOnPacked.size())
        {
            std::string AddonNames;
            AddOns* Addonstr = new AddOns;
            uint8 unk6;
            uint64 CRCCHECK;
            AddOnPacked >> AddonNames >> CRCCHECK >> unk6;

            //sLog.outDebug("ADDON:    Name:%s CRC:%x Unknown:%x",AddonNames.c_str(), CRCCHECK,unk6);

            Addonstr->Name = AddonNames;
            Addonstr->CRC = CRCCHECK;

            //if not allowed but unknown added to list
            if (GetAddonStatus(Addonstr, AddonAllowed))     // If addon is new
            {
                Addonstr->Enabled = m_Addon_Default;        // by default new addons are set from Config file
                *AddonAllowed = m_Addon_Default;            // Set addon allowed on default value
                _AddAddon(Addonstr);
                sLog.outDetail("Found new Addon, Name:%s CRC:%x Unknown:%x",AddonNames.c_str(), CRCCHECK, unk6);
            }

            if (CRCCHECK == 0x4C1C776D01LL)                 //If addon is Standard addon CRC
            {
                                                            //value's standard Addons
                *Target << uint8(0) << uint8(2) << uint8(1) << uint8(0) << uint32(0);
            }
            else if (*AddonAllowed)                         //if addon is Custom addons
                                                            //value's enable addon
                *Target << uint8(0x00) << uint8(0x01) << uint8(0x00) << uint8(0x01);
            else
                                                            //value's disable addom
                *Target << uint8(0x00) << uint8(0x0) << uint8(0x00) << uint8(0x0);

            i += AddonNames.size() + 10;
        }
        *Target << uint8(0x0);

        //delete mem allocation
        delete AddonAllowed;
    }
    else
    {
        //handle uncompress error
    }
}
*/