BinaryData.cpp 8.7 KB

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