TrinityCore
Loading...
Searching...
No Matches
CascHandles.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "CascHandles.h"
19#include "IoContext.h"
20#include "Resolver.h"
21#include <CascLib.h>
22#include <boost/asio/streambuf.hpp>
23#include <boost/asio/read.hpp>
24#include <boost/asio/read_until.hpp>
25#include <boost/asio/write.hpp>
26#include <boost/asio/ssl/stream.hpp>
27#include <boost/filesystem/operations.hpp>
28#include <string>
29
31{
32 switch (error)
33 {
34 case ERROR_SUCCESS: return "SUCCESS";
35 case ERROR_FILE_CORRUPT: return "FILE_CORRUPT";
36 case ERROR_CAN_NOT_COMPLETE: return "CAN_NOT_COMPLETE";
37 case ERROR_HANDLE_EOF: return "HANDLE_EOF";
38 case ERROR_NO_MORE_FILES: return "NO_MORE_FILES";
39 case ERROR_BAD_FORMAT: return "BAD_FORMAT";
40 case ERROR_INSUFFICIENT_BUFFER: return "INSUFFICIENT_BUFFER";
41 case ERROR_ALREADY_EXISTS: return "ALREADY_EXISTS";
42 case ERROR_DISK_FULL: return "DISK_FULL";
43 case ERROR_INVALID_PARAMETER: return "INVALID_PARAMETER";
44 case ERROR_NOT_SUPPORTED: return "NOT_SUPPORTED";
45 case ERROR_NOT_ENOUGH_MEMORY: return "NOT_ENOUGH_MEMORY";
46 case ERROR_INVALID_HANDLE: return "INVALID_HANDLE";
47 case ERROR_ACCESS_DENIED: return "ACCESS_DENIED";
48 case ERROR_FILE_NOT_FOUND: return "FILE_NOT_FOUND";
49 case ERROR_FILE_ENCRYPTED: return "FILE_ENCRYPTED";
50 case ERROR_FILE_OFFLINE: return "FILE_OFFLINE";
51 default: return "UNKNOWN";
52 }
53}
54
55namespace
56{
57 Optional<std::string> DownloadFile(std::string const& serverName, int16 port, std::string const& getCommand)
58 {
59 boost::system::error_code error;
61 boost::asio::ssl::context sslContext(boost::asio::ssl::context::sslv23);
62 sslContext.set_options(boost::asio::ssl::context::no_sslv2, error);
63 sslContext.set_options(boost::asio::ssl::context::no_sslv3, error);
64 sslContext.set_options(boost::asio::ssl::context::no_tlsv1, error);
65 sslContext.set_options(boost::asio::ssl::context::no_tlsv1_1, error);
66 sslContext.set_default_verify_paths(error);
67
68 Trinity::Net::Resolver resolver(ioContext);
69
70 Optional<boost::asio::ip::tcp::endpoint> endpoint = resolver.Resolve(boost::asio::ip::tcp::v4(), serverName, std::to_string(port));
71 if (!endpoint)
72 return {};
73
74 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(ioContext, sslContext);
75 socket.set_verify_mode(boost::asio::ssl::verify_none, error);
76 if (error)
77 return {};
78
79 socket.lowest_layer().connect(*endpoint, error);
80 if (error)
81 return {};
82
83 if (!SSL_set_tlsext_host_name(socket.native_handle(), serverName.c_str()))
84 return {};
85
86 socket.handshake(boost::asio::ssl::stream_base::client, error);
87 if (error)
88 return {};
89
90 boost::asio::streambuf request;
91 std::ostream request_stream(&request);
92
93 request_stream << "GET " << getCommand << " HTTP/1.0\r\n";
94 request_stream << "Host: " << serverName << "\r\n";
95 request_stream << "Connection: close\r\n\r\n";
96
97 // Send the request.
98 boost::asio::write(socket, request);
99
100 // Read the response status line.
101 boost::asio::streambuf response;
102 boost::asio::read_until(socket, response, "\r\n", error);
103 if (error)
104 {
105 printf("Downloading tact key list failed to read HTTP response status %s", error.message().c_str());
106 return {};
107 }
108
109 // Check that response is OK.
110 std::string http_version;
111 uint32 status_code;
112 std::string status_message;
113 std::istream response_stream(&response);
114
115 response_stream >> http_version;
116 response_stream >> status_code;
117 std::getline(response_stream, status_message);
118
119 if (status_code != 200)
120 {
121 printf("Downloading tact key list failed with server response %u %s", status_code, status_message.c_str());
122 return {};
123 }
124
125 // Read the response headers, which are terminated by a blank line.
126 boost::asio::read_until(socket, response, "\r\n\r\n");
127 if (error)
128 {
129 printf("Downloading tact key list failed to read HTTP response headers %s", error.message().c_str());
130 return {};
131 }
132
133 // Process the response headers.
134 std::string header;
135 while (std::getline(response_stream, header) && header != "\r")
136 {
137 }
138
139 std::stringstream rawBody;
140
141 // Write whatever content we already have to output.
142 if (response.size() > 0)
143 rawBody << &response;
144
145 // Read until EOF, writing data to output as we go.
146 while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
147 rawBody << &response;
148
149 return rawBody.str();
150 }
151
152 template<typename T>
153 bool GetStorageInfo(HANDLE storage, CASC_STORAGE_INFO_CLASS storageInfoClass, T* value)
154 {
155 size_t infoDataSizeNeeded = 0;
156 return ::CascGetStorageInfo(storage, storageInfoClass, value, sizeof(T), &infoDataSizeNeeded);
157 }
158}
159
160namespace CASC
161{
162using CASCCharType = std::remove_const_t<std::remove_pointer_t<decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)>>;
163using CASCStringType = std::basic_string<CASCCharType>;
164
165Storage::Storage(HANDLE handle) : _handle(handle)
166{
167}
168
170{
171 // attempt to download only once, not every storage opening
172 static Optional<std::string> const tactKeys = DownloadFile("raw.githubusercontent.com", 443, "/wowdev/TACTKeys/master/WoW.txt");
173
174 return tactKeys && CascImportKeysFromString(_handle, tactKeys->c_str());
175}
176
178{
179 ::CascCloseStorage(_handle);
180}
181
182Storage* Storage::Open(boost::filesystem::path const& path, uint32 localeMask, char const* product)
183{
184 CASCStringType strPath = path.template string<CASCStringType>();
185 CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
186 CASC_OPEN_STORAGE_ARGS args = {};
187 args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
188 args.szLocalPath = strPath.c_str();
189 args.szCodeName = strProduct.c_str();
190 args.dwLocaleMask = localeMask;
191 HANDLE handle = nullptr;
192 if (!CascOpenStorageEx(nullptr, &args, false, &handle))
193 {
194 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
195 printf("Error opening casc storage '%s': %s\n", path.string().c_str(), HumanReadableCASCError(lastError));
196 CascCloseStorage(handle);
197 SetCascError(lastError);
198 return nullptr;
199 }
200
201 printf("Opened casc storage '%s'\n", path.string().c_str());
202 Storage* storage = new Storage(handle);
203
204 if (!storage->LoadOnlineTactKeys())
205 printf("Failed to load additional online encryption keys, some files might not be extracted.\n");
206
207 return storage;
208}
209
210Storage* Storage::OpenRemote(boost::filesystem::path const& path, uint32 localeMask, char const* product, char const* region)
211{
212 CASCStringType strPath = path.template string<CASCStringType>();
213 CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
214 CASCStringType strRegion(region, region + strlen(region)); // dumb conversion from char to wchar, always ascii
215 CASC_OPEN_STORAGE_ARGS args = {};
216 args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
217 args.szLocalPath = strPath.c_str();
218 args.szCodeName = strProduct.c_str();
219 args.szRegion = strRegion.c_str();
220 args.dwLocaleMask = localeMask;
221
222 HANDLE handle = nullptr;
223 if (!::CascOpenStorageEx(nullptr, &args, true, &handle))
224 {
225 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
226 printf("Error opening remote casc storage: %s\n", HumanReadableCASCError(lastError));
227 CascCloseStorage(handle);
228 SetCascError(lastError);
229 return nullptr;
230 }
231
232 DWORD features = 0;
233 if (!GetStorageInfo(handle, CascStorageFeatures, &features) || !(features & CASC_FEATURE_ONLINE))
234 {
235 printf("Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", path.string().c_str());
236 CascCloseStorage(handle);
237 SetCascError(ERROR_FILE_OFFLINE);
238 return nullptr;
239 }
240
241 printf("Opened remote casc storage '%s'\n", path.string().c_str());
242 Storage* storage = new Storage(handle);
243
244 if (!storage->LoadOnlineTactKeys())
245 printf("Failed to load additional online encryption keys, some files might not be extracted.\n");
246
247 return storage;
248}
249
251{
252 CASC_STORAGE_PRODUCT product;
253 if (GetStorageInfo(_handle, CascStorageProduct, &product))
254 return product.BuildNumber;
255
256 return 0;
257}
258
260{
261 DWORD locales;
262 if (GetStorageInfo(_handle, CascStorageInstalledLocales, &locales))
263 return locales;
264
265 return 0;
266}
267
268bool Storage::HasTactKey(uint64 keyLookup) const
269{
270 return CascFindEncryptionKey(_handle, keyLookup) != nullptr;
271}
272
273File* Storage::OpenFile(char const* fileName, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
274{
275 DWORD openFlags = CASC_OPEN_BY_NAME;
276 if (zerofillEncryptedParts)
277 openFlags |= CASC_OVERCOME_ENCRYPTED;
278
279 HANDLE handle = nullptr;
280 if (!::CascOpenFile(_handle, fileName, localeMask, openFlags, &handle))
281 {
282 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
283 if (printErrors)
284 fprintf(stderr, "Failed to open '%s' in CASC storage: %s\n", fileName, HumanReadableCASCError(lastError));
285
286 CascCloseFile(handle);
287 SetCascError(lastError);
288 return nullptr;
289 }
290
291 return new File(handle);
292}
293
294File* Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
295{
296 DWORD openFlags = CASC_OPEN_BY_FILEID;
297 if (zerofillEncryptedParts)
298 openFlags |= CASC_OVERCOME_ENCRYPTED;
299
300 HANDLE handle = nullptr;
301 if (!::CascOpenFile(_handle, CASC_FILE_DATA_ID(fileDataId), localeMask, openFlags, &handle))
302 {
303 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
304 if (printErrors)
305 fprintf(stderr, "Failed to open 'FileDataId %u' in CASC storage: %s\n", fileDataId, HumanReadableCASCError(lastError));
306
307 CascCloseFile(handle);
308 SetCascError(lastError);
309 return nullptr;
310 }
311
312 return new File(handle);
313}
314
315File::File(HANDLE handle) : _handle(handle)
316{
317}
318
320{
321 ::CascCloseFile(_handle);
322}
323
325{
326 CASC_FILE_FULL_INFO info;
327 if (!::CascGetFileInfo(_handle, CascFileFullInfo, &info, sizeof(info), nullptr))
328 return CASC_INVALID_ID;
329
330 return info.FileDataId;
331}
332
334{
335 ULONGLONG size;
336 if (!::CascGetFileSize64(_handle, &size))
337 return -1;
338
339 return int64(size);
340}
341
343{
344 ULONGLONG position;
345 if (!::CascSetFilePointer64(_handle, 0, &position, FILE_CURRENT))
346 return -1;
347
348 return int64(position);
349}
350
352{
353 LONG parts[2];
354 memcpy(parts, &position, sizeof(parts));
355 return ::CascSetFilePointer64(_handle, position, nullptr, FILE_BEGIN);
356}
357
358bool File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
359{
360 DWORD bytesReadDWORD;
361 if (!::CascReadFile(_handle, buffer, bytes, &bytesReadDWORD))
362 return false;
363
364 if (bytesRead)
365 *bytesRead = bytesReadDWORD;
366
367 return true;
368}
369}
int64_t int64
Definition Define.h:149
int16_t int16
Definition Define.h:151
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
uint32 GetId() const
int64 GetSize() const
bool SetPointer(int64 position)
int64 GetPointer() const
HANDLE _handle
Definition CascHandles.h:78
bool ReadFile(void *buffer, uint32 bytes, uint32 *bytesRead)
File(HANDLE handle)
static Storage * Open(boost::filesystem::path const &path, uint32 localeMask, char const *product)
static Storage * OpenRemote(boost::filesystem::path const &path, uint32 localeMask, char const *product, char const *region)
File * OpenFile(char const *fileName, uint32 localeMask, bool printErrors=false, bool zerofillEncryptedParts=false) const
bool HasTactKey(uint64 keyLookup) const
uint32 GetBuildNumber() const
Storage(HANDLE handle)
uint32 GetInstalledLocalesMask() const
bool LoadOnlineTactKeys()
std::remove_const_t< std::remove_pointer_t< decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)> > CASCCharType
char const * HumanReadableCASCError(uint32 error)
std::basic_string< CASCCharType > CASCStringType