aboutsummaryrefslogtreecommitdiff
path: root/src/tools/extractor_common/CascHandles.cpp
blob: f2964a550a56dce0c8b1e74d165e856a44712df8 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "CascHandles.h"
#include "IoContext.h"
#include "Resolver.h"
#include <CascLib.h>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/filesystem/operations.hpp>
#include <string>

char const* CASC::HumanReadableCASCError(uint32 error)
{
    switch (error)
    {
        case ERROR_SUCCESS: return "SUCCESS";
        case ERROR_FILE_CORRUPT: return "FILE_CORRUPT";
        case ERROR_CAN_NOT_COMPLETE: return "CAN_NOT_COMPLETE";
        case ERROR_HANDLE_EOF: return "HANDLE_EOF";
        case ERROR_NO_MORE_FILES: return "NO_MORE_FILES";
        case ERROR_BAD_FORMAT: return "BAD_FORMAT";
        case ERROR_INSUFFICIENT_BUFFER: return "INSUFFICIENT_BUFFER";
        case ERROR_ALREADY_EXISTS: return "ALREADY_EXISTS";
        case ERROR_DISK_FULL: return "DISK_FULL";
        case ERROR_INVALID_PARAMETER: return "INVALID_PARAMETER";
        case ERROR_NOT_SUPPORTED: return "NOT_SUPPORTED";
        case ERROR_NOT_ENOUGH_MEMORY: return "NOT_ENOUGH_MEMORY";
        case ERROR_INVALID_HANDLE: return "INVALID_HANDLE";
        case ERROR_ACCESS_DENIED: return "ACCESS_DENIED";
        case ERROR_FILE_NOT_FOUND: return "FILE_NOT_FOUND";
        case ERROR_FILE_ENCRYPTED: return "FILE_ENCRYPTED";
        case ERROR_FILE_OFFLINE: return "FILE_OFFLINE";
        default: return "UNKNOWN";
    }
}

namespace
{
    Optional<std::string> DownloadFile(std::string const& serverName, int16 port, std::string const& getCommand)
    {
        boost::system::error_code error;
        Trinity::Asio::IoContext ioContext;
        boost::asio::ssl::context sslContext(boost::asio::ssl::context::sslv23);
        sslContext.set_options(boost::asio::ssl::context::no_sslv2, error);
        sslContext.set_options(boost::asio::ssl::context::no_sslv3, error);
        sslContext.set_options(boost::asio::ssl::context::no_tlsv1, error);
        sslContext.set_options(boost::asio::ssl::context::no_tlsv1_1, error);
        sslContext.set_default_verify_paths(error);

        Trinity::Net::Resolver resolver(ioContext);

        Optional<boost::asio::ip::tcp::endpoint> endpoint = resolver.Resolve(boost::asio::ip::tcp::v4(), serverName, std::to_string(port));
        if (!endpoint)
            return {};

        boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(ioContext, sslContext);
        socket.set_verify_mode(boost::asio::ssl::verify_none, error);
        if (error)
            return {};

        socket.lowest_layer().connect(*endpoint, error);
        if (error)
            return {};

        if (!SSL_set_tlsext_host_name(socket.native_handle(), serverName.c_str()))
            return {};

        socket.handshake(boost::asio::ssl::stream_base::client, error);
        if (error)
            return {};

        boost::asio::streambuf request;
        std::ostream request_stream(&request);

        request_stream << "GET " << getCommand << " HTTP/1.0\r\n";
        request_stream << "Host: " << serverName << "\r\n";
        request_stream << "Connection: close\r\n\r\n";

        // Send the request.
        boost::asio::write(socket, request);

        // Read the response status line.
        boost::asio::streambuf response;
        boost::asio::read_until(socket, response, "\r\n", error);
        if (error)
        {
            printf("Downloading tact key list failed to read HTTP response status %s", error.message().c_str());
            return {};
        }

        // Check that response is OK.
        std::string http_version;
        uint32 status_code;
        std::string status_message;
        std::istream response_stream(&response);

        response_stream >> http_version;
        response_stream >> status_code;
        std::getline(response_stream, status_message);

        if (status_code != 200)
        {
            printf("Downloading tact key list failed with server response %u %s", status_code, status_message.c_str());
            return {};
        }

        // Read the response headers, which are terminated by a blank line.
        boost::asio::read_until(socket, response, "\r\n\r\n");
        if (error)
        {
            printf("Downloading tact key list failed to read HTTP response headers %s", error.message().c_str());
            return {};
        }

        // Process the response headers.
        std::string header;
        while (std::getline(response_stream, header) && header != "\r")
        {
        }

        std::stringstream rawBody;

        // Write whatever content we already have to output.
        if (response.size() > 0)
            rawBody << &response;

        // Read until EOF, writing data to output as we go.
        while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
            rawBody << &response;

        return rawBody.str();
    }

    template<typename T>
    bool GetStorageInfo(HANDLE storage, CASC_STORAGE_INFO_CLASS storageInfoClass, T* value)
    {
        size_t infoDataSizeNeeded = 0;
        return ::CascGetStorageInfo(storage, storageInfoClass, value, sizeof(T), &infoDataSizeNeeded);
    }
}

namespace CASC
{
using CASCCharType = std::remove_const_t<std::remove_pointer_t<decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)>>;
using CASCStringType = std::basic_string<CASCCharType>;

Storage::Storage(HANDLE handle) : _handle(handle)
{
}

bool Storage::LoadOnlineTactKeys()
{
    // attempt to download only once, not every storage opening
    static Optional<std::string> const tactKeys = DownloadFile("raw.githubusercontent.com", 443, "/wowdev/TACTKeys/master/WoW.txt");

    return tactKeys && CascImportKeysFromString(_handle, tactKeys->c_str());
}

Storage::~Storage()
{
    ::CascCloseStorage(_handle);
}

Storage* Storage::Open(boost::filesystem::path const& path, uint32 localeMask, char const* product)
{
    CASCStringType strPath = path.template string<CASCStringType>();
    CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
    CASC_OPEN_STORAGE_ARGS args = {};
    args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
    args.szLocalPath = strPath.c_str();
    args.szCodeName = strProduct.c_str();
    args.dwLocaleMask = localeMask;
    HANDLE handle = nullptr;
    if (!CascOpenStorageEx(nullptr, &args, false, &handle))
    {
        DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
        printf("Error opening casc storage '%s': %s\n", path.string().c_str(), HumanReadableCASCError(lastError));
        CascCloseStorage(handle);
        SetCascError(lastError);
        return nullptr;
    }

    printf("Opened casc storage '%s'\n", path.string().c_str());
    Storage* storage = new Storage(handle);

    if (!storage->LoadOnlineTactKeys())
        printf("Failed to load additional online encryption keys, some files might not be extracted.\n");

    return storage;
}

Storage* Storage::OpenRemote(boost::filesystem::path const& path, uint32 localeMask, char const* product, char const* region)
{
    CASCStringType strPath = path.template string<CASCStringType>();
    CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
    CASCStringType strRegion(region, region + strlen(region)); // dumb conversion from char to wchar, always ascii
    CASC_OPEN_STORAGE_ARGS args = {};
    args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
    args.szLocalPath = strPath.c_str();
    args.szCodeName = strProduct.c_str();
    args.szRegion = strRegion.c_str();
    args.dwLocaleMask = localeMask;

    HANDLE handle = nullptr;
    if (!::CascOpenStorageEx(nullptr, &args, true, &handle))
    {
        DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
        printf("Error opening remote casc storage: %s\n", HumanReadableCASCError(lastError));
        CascCloseStorage(handle);
        SetCascError(lastError);
        return nullptr;
    }

    DWORD features = 0;
    if (!GetStorageInfo(handle, CascStorageFeatures, &features) || !(features & CASC_FEATURE_ONLINE))
    {
        printf("Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", path.string().c_str());
        CascCloseStorage(handle);
        SetCascError(ERROR_FILE_OFFLINE);
        return nullptr;
    }

    printf("Opened remote casc storage '%s'\n", path.string().c_str());
    Storage* storage = new Storage(handle);

    if (!storage->LoadOnlineTactKeys())
        printf("Failed to load additional online encryption keys, some files might not be extracted.\n");

    return storage;
}

uint32 Storage::GetBuildNumber() const
{
    CASC_STORAGE_PRODUCT product;
    if (GetStorageInfo(_handle, CascStorageProduct, &product))
        return product.BuildNumber;

    return 0;
}

uint32 Storage::GetInstalledLocalesMask() const
{
    DWORD locales;
    if (GetStorageInfo(_handle, CascStorageInstalledLocales, &locales))
        return locales;

    return 0;
}

bool Storage::HasTactKey(uint64 keyLookup) const
{
    return CascFindEncryptionKey(_handle, keyLookup) != nullptr;
}

File* Storage::OpenFile(char const* fileName, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
{
    DWORD openFlags = CASC_OPEN_BY_NAME;
    if (zerofillEncryptedParts)
        openFlags |= CASC_OVERCOME_ENCRYPTED;

    HANDLE handle = nullptr;
    if (!::CascOpenFile(_handle, fileName, localeMask, openFlags, &handle))
    {
        DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
        if (printErrors)
            fprintf(stderr, "Failed to open '%s' in CASC storage: %s\n", fileName, HumanReadableCASCError(lastError));

        CascCloseFile(handle);
        SetCascError(lastError);
        return nullptr;
    }

    return new File(handle);
}

File* Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
{
    DWORD openFlags = CASC_OPEN_BY_FILEID;
    if (zerofillEncryptedParts)
        openFlags |= CASC_OVERCOME_ENCRYPTED;

    HANDLE handle = nullptr;
    if (!::CascOpenFile(_handle, CASC_FILE_DATA_ID(fileDataId), localeMask, openFlags, &handle))
    {
        DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
        if (printErrors)
            fprintf(stderr, "Failed to open 'FileDataId %u' in CASC storage: %s\n", fileDataId, HumanReadableCASCError(lastError));

        CascCloseFile(handle);
        SetCascError(lastError);
        return nullptr;
    }

    return new File(handle);
}

File::File(HANDLE handle) : _handle(handle)
{
}

File::~File()
{
    ::CascCloseFile(_handle);
}

uint32 File::GetId() const
{
    CASC_FILE_FULL_INFO info;
    if (!::CascGetFileInfo(_handle, CascFileFullInfo, &info, sizeof(info), nullptr))
        return CASC_INVALID_ID;

    return info.FileDataId;
}

int64 File::GetSize() const
{
    ULONGLONG size;
    if (!::CascGetFileSize64(_handle, &size))
        return -1;

    return int64(size);
}

int64 File::GetPointer() const
{
    ULONGLONG position;
    if (!::CascSetFilePointer64(_handle, 0, &position, FILE_CURRENT))
        return -1;

    return int64(position);
}

bool File::SetPointer(int64 position)
{
    LONG parts[2];
    memcpy(parts, &position, sizeof(parts));
    return ::CascSetFilePointer64(_handle, position, nullptr, FILE_BEGIN);
}

bool File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
{
    DWORD bytesReadDWORD;
    if (!::CascReadFile(_handle, buffer, bytes, &bytesReadDWORD))
        return false;

    if (bytesRead)
        *bytesRead = bytesReadDWORD;

    return true;
}
}