BinaryData.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "BinaryData.h"
  5. #include "ZLib/zlib.h"
  6. #include "EasyLogging++/easylogging++.h"
  7. #include <algorithm>
  8. #include <BinaryData.h>
  9. extern "C++"
  10. {
  11. namespace LOTRO_DAT {
  12. BinaryData::BinaryData() {
  13. data_ = nullptr;
  14. size_ = 0;
  15. }
  16. BinaryData::BinaryData(const BinaryData &d) {
  17. size_ = d.size_;
  18. data_ = new unsigned char[size_];
  19. memcpy(data_, d.data_, size_);
  20. }
  21. BinaryData::BinaryData(BinaryData &&other) noexcept {
  22. size_ = other.size_;
  23. data_ = other.data_;
  24. other.size_ = 0;
  25. other.data_ = nullptr;
  26. }
  27. BinaryData::BinaryData(const char* data, unsigned int size) {
  28. size_ = size;
  29. data_ = new unsigned char[size_];
  30. memcpy(data_, data, size_);
  31. }
  32. BinaryData::BinaryData(unsigned int size) {
  33. data_ = new unsigned char[size];
  34. for (size_t i = 0; i < size; i++)
  35. data_[i] = 0;
  36. size_ = size;
  37. }
  38. BinaryData::~BinaryData() {
  39. delete[] data_;
  40. }
  41. unsigned char& BinaryData::operator[](const unsigned int &pos) {
  42. if (pos >= size_)
  43. LOG(ERROR) << "Bad BinaryData::operator[]. Position " << pos
  44. << " is out of range in BinaryData with size " << size_;
  45. return data_[pos];
  46. }
  47. BinaryData BinaryData::operator +(const BinaryData &b) {
  48. BinaryData res(size_ + b.size());
  49. if (size_ > 0)
  50. memcpy(res.data_, data_, size_);
  51. if (b.size() > 0)
  52. memcpy(res.data_ + size_, b.data_, b.size_);
  53. return res;
  54. }
  55. // Translates T bytes from data into number using UTF-16LE encoding of the .dat file
  56. template<unsigned int T>
  57. long long BinaryData::ToNumber(const long long &pos) const {
  58. unsigned long long ans = 0;
  59. if (pos + T > size_) {
  60. LOG(ERROR) << "Reading " << T << " bytes from " << pos << " offset with BinaryData size " << size_
  61. << " Reached end of BinaryData!";
  62. return 0;
  63. }
  64. for (int i = T - 1; i >= 0; i--)
  65. ans = ((ans << 8ull) | data_[pos + i]);
  66. return ans;
  67. }
  68. // Translates T bytes from data into number in raw format
  69. template<unsigned int T>
  70. long long BinaryData::ToNumberRAW(const long long &pos) const {
  71. long long ans = 0;
  72. if (pos + T > size_) {
  73. LOG(ERROR) << "Reading " << T << " bytes from " << pos << " offset with BinaryData size " << size_
  74. << " Reached end of BinaryData!";
  75. return 0;
  76. }
  77. for (unsigned i = 0; i < T; i++)
  78. ans = ((ans << 8ll) | data_[pos + i]);
  79. return ans;
  80. }
  81. // Makes data from specified T bytes of number in Little Endian encoding
  82. template<unsigned int T>
  83. BinaryData BinaryData::FromNumber(const long long &number) {
  84. if (T <= 0) {
  85. LOG(ERROR) << "Trying to make data from amount of bytes < 0";
  86. return BinaryData(0);
  87. }
  88. BinaryData data(T);
  89. for (size_t i = 0; i < T; i++)
  90. data.data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  91. return data;
  92. }
  93. // Makes data from specified T bytes of number in raw
  94. template<unsigned int T>
  95. BinaryData BinaryData::FromNumberRAW(const long long &number) {
  96. if (T <= 0) {
  97. LOG(ERROR) << "Trying to make data from amount of bytes < 0";
  98. return BinaryData(0);
  99. }
  100. BinaryData data = FromNumber<T>(number);
  101. std::reverse(data.data_, data.data_ + data.size());
  102. return data;
  103. }
  104. BinaryData &BinaryData::operator=(const BinaryData &data) {
  105. if (&data == this)
  106. return *this;
  107. if (size_ != 0 && data_ != nullptr)
  108. delete[] data_;
  109. size_ = data.size_;
  110. data_ = nullptr;
  111. if (size_ != 0) {
  112. data_ = new unsigned char[size_];
  113. memcpy(data_, data.data_, size_);
  114. }
  115. return *this;
  116. }
  117. size_t BinaryData::size() const {
  118. return size_;
  119. }
  120. unsigned char *BinaryData::data() const {
  121. return data_;
  122. }
  123. bool BinaryData::WriteToFile(const char *filename) const {
  124. FILE *f;
  125. //f = fopen64(filename, "wb");
  126. f = fopen(filename, "wb");
  127. if (f == nullptr) {
  128. LOG(ERROR) << "File " << std::string(filename) << " doesn't exist or is unreachable.. Cannot write data";
  129. return false;
  130. }
  131. fwrite(data(), size(), 1, f);
  132. fclose(f);
  133. return true;
  134. }
  135. bool BinaryData::WriteToFile(const std::string &filename) const {
  136. return WriteToFile(filename.c_str());
  137. }
  138. void BinaryData::ReadFromFile(const char *filename) {
  139. FILE *f;
  140. fopen_s(&f, filename, "rb");
  141. if (f == nullptr) {
  142. LOG(ERROR) << "File " << std::string(filename) << " doesn't exist.. Retuning null data";
  143. size_ = 0;
  144. delete[] data_;
  145. return;
  146. }
  147. _fseeki64(f, 0, SEEK_END);
  148. long long file_size = ftell(f);
  149. _fseeki64(f, 0, SEEK_SET);
  150. BinaryData temp_data = BinaryData(unsigned(file_size));
  151. fread(temp_data.data_, temp_data.size_, 1, f);
  152. *this = temp_data;
  153. fclose(f);
  154. }
  155. void BinaryData::ReadFromFile(const std::string &filename) {
  156. ReadFromFile(filename.c_str());
  157. }
  158. bool BinaryData::CheckCompression() const {
  159. if (size() < 10)
  160. return false;
  161. auto header = ToNumberRAW<2>(4);
  162. return (header == 0x7801 || header == 0x789C || header == 0x78DA);
  163. }
  164. BinaryData BinaryData::DecompressData(unsigned int offset) const {
  165. const unsigned max_unpacked_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum unpacked data;
  166. BinaryData decompressed(max_unpacked_size);
  167. uLongf new_size = max_unpacked_size;
  168. int res = uncompress(decompressed.data_, &new_size, data_ + offset, size_ - offset);
  169. if (res != 0) {
  170. LOG(ERROR) << "Failed to decompress. Function returned " << res;
  171. return BinaryData(0);
  172. }
  173. decompressed.size_ = (unsigned int)new_size;
  174. return decompressed;
  175. }
  176. BinaryData BinaryData::CompressData(unsigned int offset) const {
  177. const unsigned max_file_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum packed data;
  178. BinaryData compressed(max_file_size);
  179. uLongf new_size = max_file_size;
  180. int res = compress2(compressed.data_, &new_size, data_ + offset, size_ - offset, 9);
  181. if (res != 0) {
  182. LOG(ERROR) << "Failed to compress. Function returned " << res;
  183. return BinaryData(0);
  184. }
  185. compressed.size_ = (unsigned int)new_size;
  186. return compressed;
  187. }
  188. BinaryData BinaryData::CutData(long long first, long long last) const {
  189. if (last < 0)
  190. last = size();
  191. if (last > size()) {
  192. LOG(ERROR) << "Unable to cut data - parameter last is out of range";
  193. return BinaryData(0);
  194. }
  195. BinaryData newdata(unsigned(last - first));
  196. memcpy(newdata.data(), data() + first, newdata.size());
  197. return newdata;
  198. }
  199. bool BinaryData::operator==(const BinaryData &b) const {
  200. if (size() != b.size())
  201. return false;
  202. for (size_t i = 0; i < size(); i++)
  203. if (data_[i] != b.data_[i])
  204. return false;
  205. return true;
  206. }
  207. bool BinaryData::operator!=(const BinaryData &b) const {
  208. return !(*this == b);
  209. }
  210. bool BinaryData::Empty() const {
  211. return size_ == 0;
  212. }
  213. void BinaryData::Append(const BinaryData &b, size_t append_offset) {
  214. if (append_offset + b.size() > size()) {
  215. LOG(ERROR) << "data for appending has more bytes than BinaryData size!";
  216. return;
  217. }
  218. memcpy(data_ + append_offset, b.data_, b.size_);
  219. }
  220. template long long BinaryData::ToNumber<1>(long long const&) const;
  221. template long long BinaryData::ToNumber<2>(long long const&) const;
  222. template long long BinaryData::ToNumber<4>(long long const&) const;
  223. template long long BinaryData::ToNumber<8>(long long const&) const;
  224. template BinaryData BinaryData::FromNumber<1>(const long long &);
  225. template BinaryData BinaryData::FromNumber<2>(const long long &);
  226. template BinaryData BinaryData::FromNumber<4>(const long long &);
  227. template BinaryData BinaryData::FromNumber<8>(const long long &);
  228. template long long BinaryData::ToNumberRAW<1>(long long const&) const;
  229. template long long BinaryData::ToNumberRAW<2>(long long const&) const;
  230. template long long BinaryData::ToNumberRAW<4>(long long const&) const;
  231. template long long BinaryData::ToNumberRAW<8>(long long const&) const;
  232. template BinaryData BinaryData::FromNumberRAW<1>(const long long &);
  233. template BinaryData BinaryData::FromNumberRAW<2>(const long long &);
  234. template BinaryData BinaryData::FromNumberRAW<4>(const long long &);
  235. template BinaryData BinaryData::FromNumberRAW<8>(const long long &);
  236. }
  237. }