TrinityCore
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::Asio::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
160CASC::Storage::Storage(HANDLE handle) : _handle(handle)
161{
162}
163
165{
166 // attempt to download only once, not every storage opening
167 static Optional<std::string> const tactKeys = DownloadFile("raw.githubusercontent.com", 443, "/wowdev/TACTKeys/master/WoW.txt");
168
169 return tactKeys && CascImportKeysFromString(_handle, tactKeys->c_str());
170}
171
173{
174 ::CascCloseStorage(_handle);
175}
176
177CASC::Storage* CASC::Storage::Open(boost::filesystem::path const& path, uint32 localeMask, char const* product)
178{
179 std::string strPath = path.string();
180 CASC_OPEN_STORAGE_ARGS args = {};
181 args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
182 args.szLocalPath = strPath.c_str();
183 args.szCodeName = product;
184 args.dwLocaleMask = localeMask;
185 HANDLE handle = nullptr;
186 if (!::CascOpenStorageEx(nullptr, &args, false, &handle))
187 {
188 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
189 printf("Error opening casc storage '%s': %s\n", path.string().c_str(), HumanReadableCASCError(lastError));
190 CascCloseStorage(handle);
191 SetCascError(lastError);
192 return nullptr;
193 }
194
195 printf("Opened casc storage '%s'\n", path.string().c_str());
196 Storage* storage = new Storage(handle);
197
198 if (!storage->LoadOnlineTactKeys())
199 printf("Failed to load additional online encryption keys, some files might not be extracted.\n");
200
201 return storage;
202}
203
204CASC::Storage* CASC::Storage::OpenRemote(boost::filesystem::path const& path, uint32 localeMask, char const* product, char const* region)
205{
206 std::string strPath = path.string();
207 CASC_OPEN_STORAGE_ARGS args = {};
208 args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
209 args.szLocalPath = strPath.c_str();
210 args.szCodeName = product;
211 args.szRegion = region;
212 args.dwLocaleMask = localeMask;
213
214 HANDLE handle = nullptr;
215 if (!::CascOpenStorageEx(nullptr, &args, true, &handle))
216 {
217 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
218 printf("Error opening remote casc storage: %s\n", HumanReadableCASCError(lastError));
219 CascCloseStorage(handle);
220 SetCascError(lastError);
221 return nullptr;
222 }
223
224 DWORD features = 0;
225 if (!GetStorageInfo(handle, CascStorageFeatures, &features) || !(features & CASC_FEATURE_ONLINE))
226 {
227 printf("Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", args.szLocalPath);
228 CascCloseStorage(handle);
229 SetCascError(ERROR_FILE_OFFLINE);
230 return nullptr;
231 }
232
233 printf("Opened remote casc storage '%s'\n", path.string().c_str());
234 Storage* storage = new Storage(handle);
235
236 if (!storage->LoadOnlineTactKeys())
237 printf("Failed to load additional online encryption keys, some files might not be extracted.\n");
238
239 return storage;
240}
241
243{
244 CASC_STORAGE_PRODUCT product;
245 if (GetStorageInfo(_handle, CascStorageProduct, &product))
246 return product.BuildNumber;
247
248 return 0;
249}
250
252{
253 DWORD locales;
254 if (GetStorageInfo(_handle, CascStorageInstalledLocales, &locales))
255 return locales;
256
257 return 0;
258}
259
261{
262 return CascFindEncryptionKey(_handle, keyLookup) != nullptr;
263}
264
265CASC::File* CASC::Storage::OpenFile(char const* fileName, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
266{
267 DWORD openFlags = CASC_OPEN_BY_NAME;
268 if (zerofillEncryptedParts)
269 openFlags |= CASC_OVERCOME_ENCRYPTED;
270
271 HANDLE handle = nullptr;
272 if (!::CascOpenFile(_handle, fileName, localeMask, openFlags, &handle))
273 {
274 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
275 if (printErrors)
276 fprintf(stderr, "Failed to open '%s' in CASC storage: %s\n", fileName, HumanReadableCASCError(lastError));
277
278 CascCloseFile(handle);
279 SetCascError(lastError);
280 return nullptr;
281 }
282
283 return new File(handle);
284}
285
286CASC::File* CASC::Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
287{
288 DWORD openFlags = CASC_OPEN_BY_FILEID;
289 if (zerofillEncryptedParts)
290 openFlags |= CASC_OVERCOME_ENCRYPTED;
291
292 HANDLE handle = nullptr;
293 if (!::CascOpenFile(_handle, CASC_FILE_DATA_ID(fileDataId), localeMask, openFlags, &handle))
294 {
295 DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
296 if (printErrors)
297 fprintf(stderr, "Failed to open 'FileDataId %u' in CASC storage: %s\n", fileDataId, HumanReadableCASCError(lastError));
298
299 CascCloseFile(handle);
300 SetCascError(lastError);
301 return nullptr;
302 }
303
304 return new File(handle);
305}
306
307CASC::File::File(HANDLE handle) : _handle(handle)
308{
309}
310
312{
313 ::CascCloseFile(_handle);
314}
315
317{
318 CASC_FILE_FULL_INFO info;
319 if (!::CascGetFileInfo(_handle, CascFileFullInfo, &info, sizeof(info), nullptr))
320 return CASC_INVALID_ID;
321
322 return info.FileDataId;
323}
324
326{
327 ULONGLONG size;
328 if (!::CascGetFileSize64(_handle, &size))
329 return -1;
330
331 return int64(size);
332}
333
335{
336 ULONGLONG position;
337 if (!::CascSetFilePointer64(_handle, 0, &position, FILE_CURRENT))
338 return -1;
339
340 return int64(position);
341}
342
344{
345 LONG parts[2];
346 memcpy(parts, &position, sizeof(parts));
347 return ::CascSetFilePointer64(_handle, position, nullptr, FILE_BEGIN);
348}
349
350bool CASC::File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
351{
352 DWORD bytesReadDWORD;
353 if (!::CascReadFile(_handle, buffer, bytes, &bytesReadDWORD))
354 return false;
355
356 if (bytesRead)
357 *bytesRead = bytesReadDWORD;
358
359 return true;
360}
int64_t int64
Definition: Define.h:137
int16_t int16
Definition: Define.h:139
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
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
bool ReadFile(void *buffer, uint32 bytes, uint32 *bytesRead)
File(HANDLE handle)
bool HasTactKey(uint64 keyLookup) const
uint32 GetBuildNumber() const
static Storage * OpenRemote(boost::filesystem::path const &path, uint32 localeMask, char const *product, char const *region)
static Storage * Open(boost::filesystem::path const &path, uint32 localeMask, char const *product)
Storage(HANDLE handle)
uint32 GetInstalledLocalesMask() const
File * OpenFile(char const *fileName, uint32 localeMask, bool printErrors=false, bool zerofillEncryptedParts=false) const
bool LoadOnlineTactKeys()
char const * HumanReadableCASCError(uint32 error)
Definition: CascHandles.cpp:30
constexpr std::size_t size()
Definition: UpdateField.h:796