TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Connection_Patcher::Helper Namespace Reference

Functions

void CopyDir (boost::filesystem::path const &source, boost::filesystem::path const &destination)
 
void DownloadFile (const std::string &serverName, int port, const std::string &getCommand, std::ostream &out)
 
Constants::BinaryTypes GetBinaryType (std::vector< unsigned char > const &data)
 
std::string GetFileChecksum (std::vector< unsigned char > const &data)
 
std::set< size_t > SearchOffset (std::vector< unsigned char > const &binary, std::vector< unsigned char > const &pattern)
 
uint32_t GetBuildNumber (std::vector< unsigned char > const &binary)
 

Function Documentation

void Connection_Patcher::Helper::CopyDir ( boost::filesystem::path const source,
boost::filesystem::path const destination 
)
27  {
28  namespace fs = boost::filesystem;
29  if (!fs::exists(source) || !fs::is_directory(source))
30  throw std::invalid_argument("Source directory " + source.string() + " does not exist or is not a directory.");
31 
32  if (fs::exists(destination))
33  throw std::invalid_argument("Destination directory " + destination.string() + " already exists.");
34 
35  if (!fs::create_directory(destination))
36  throw std::runtime_error("Unable to create destination directory" + destination.string());
37 
38  for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file)
39  {
40  fs::path current(file->path());
41  if (fs::is_directory(current))
42  CopyDir(current, destination / current.filename());
43  else
44  fs::copy_file(current, destination / current.filename());
45  }
46  }
void CopyDir(boost::filesystem::path const &source, boost::filesystem::path const &destination)
Definition: Helper.cpp:26

+ Here is the caller graph for this function:

void Connection_Patcher::Helper::DownloadFile ( const std::string &  serverName,
int  port,
const std::string &  getCommand,
std::ostream &  out 
)
50  {
51  using namespace boost::asio;
52  using boost::asio::ip::tcp;
53 
54  io_service io_service;
55 
56  // Get a list of endpoints corresponding to the server name.
57  tcp::resolver resolver(io_service);
58  tcp::resolver::query query(serverName, std::to_string(port));
59  tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
60  tcp::resolver::iterator end;
61 
62  // Try each endpoint until we successfully establish a connection.
63  tcp::socket socket(io_service);
64  boost::system::error_code error = boost::asio::error::host_not_found;
65  while (error && endpoint_iterator != end)
66  {
67  socket.close();
68  socket.connect(*endpoint_iterator++, error);
69  }
70 
71  boost::asio::streambuf request;
72  std::ostream request_stream(&request);
73 
74  request_stream << "GET " << getCommand << " HTTP/1.0\r\n";
75  request_stream << "Host: " << serverName << ':' << port << "\r\n";
76  request_stream << "Accept: */*\r\n";
77  request_stream << "Connection: close\r\n\r\n";
78 
79  // Send the request.
80  boost::asio::write(socket, request);
81 
82  // Read the response status line.
83  boost::asio::streambuf response;
84  boost::asio::read_until(socket, response, "\r\n");
85 
86  // Check that response is OK.
87  std::istream response_stream(&response);
88  std::string http_version;
89  response_stream >> http_version;
90  unsigned int status_code;
91  response_stream >> status_code;
92  std::string status_message;
93  std::getline(response_stream, status_message);
94 
95  // Read the response headers, which are terminated by a blank line.
96  boost::asio::read_until(socket, response, "\r\n\r\n");
97 
98  // Process the response headers.
99  std::string header;
100  while (std::getline(response_stream, header) && header != "\r")
101  { }
102 
103  // Write whatever content we already have to output.
104  if (response.size() > 0)
105  out << &response;
106 
107  // Read until EOF, writing data to output as we go.
108  while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
109  out << &response;
110  }
char * query(struct soap *soap)
Definition: httpget.cpp:244

+ Here is the call graph for this function:

Constants::BinaryTypes Connection_Patcher::Helper::GetBinaryType ( std::vector< unsigned char > const data)
113  {
114  // Check MS-DOS magic
115  if (*reinterpret_cast<uint16_t const*>(data.data()) == 0x5A4D)
116  {
117  uint32_t const peOffset(*reinterpret_cast<uint32_t const*>(data.data() + 0x3C));
118 
119  // Check PE magic
120  if (*reinterpret_cast<uint32_t const*>(data.data() + peOffset) != 0x4550)
121  throw std::invalid_argument("Not a PE file!");
122 
123  return Constants::BinaryTypes(*reinterpret_cast<uint16_t const*>(data.data() + peOffset + 4));
124  }
125  else
126  return Constants::BinaryTypes(*reinterpret_cast<uint32_t const*>(data.data()));
127  }
unsigned int uint32_t
Definition: stdint.h:80
uint32_t Connection_Patcher::Helper::GetBuildNumber ( std::vector< unsigned char > const binary)
170  {
171  std::set<size_t> offsets = SearchOffset(binary, Patterns::Common::BinaryVersion());
172 
173  if (!offsets.empty())
174  {
175  size_t const verOffset = (*offsets.begin());
176  std::string ver(&binary[verOffset + 16], &binary[verOffset + 21]);
177 
178  return std::stoi(ver);
179  }
180  return 0;
181  }
std::set< size_t > SearchOffset(std::vector< unsigned char > const &binary, std::vector< unsigned char > const &pattern)
Definition: Helper.cpp:138

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string Connection_Patcher::Helper::GetFileChecksum ( std::vector< unsigned char > const data)
130  {
131  SHA256Hash h;
132  h.UpdateData(data.data(), data.size());
133  h.Finalize();
134 
135  return ByteArrayToHexStr(h.GetDigest(), h.GetLength());
136  }
Definition: SHA256.h:28
uint8 * GetDigest(void)
Definition: SHA256.h:44
void UpdateData(const uint8 *dta, int len)
Definition: SHA256.cpp:34
std::string ByteArrayToHexStr(uint8 const *bytes, uint32 arrayLen, bool reverse)
Definition: Util.cpp:509
void Finalize()
Definition: SHA256.cpp:64
int GetLength(void) const
Definition: SHA256.h:45

+ Here is the call graph for this function:

std::set<size_t> Connection_Patcher::Helper::SearchOffset ( std::vector< unsigned char > const binary,
std::vector< unsigned char > const pattern 
)
139  {
140  std::set<size_t> offsets;
141  for (size_t i = 0; (i + pattern.size()) < binary.size(); i++)
142  {
143  size_t matches = 0;
144 
145  for (size_t j = 0; j < pattern.size(); j++)
146  {
147  if (pattern[j] == 0)
148  {
149  matches++;
150  continue;
151  }
152 
153  if (binary[i + j] != pattern[j])
154  break;
155 
156  matches++;
157  }
158 
159  if (matches == pattern.size())
160  {
161  offsets.insert(i);
162  i += matches;
163  }
164  }
165 
166  return offsets.empty() ? throw std::runtime_error("unable to find pattern") : offsets;
167  }

+ Here is the caller graph for this function: