BinaryData.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "BinaryData.h"
  5. #include "Common/DatException.h"
  6. #include "Common/ZLib/zlib.h"
  7. extern "C++"
  8. {
  9. namespace LOTRO_DAT {
  10. BinaryData::BinaryData() {
  11. data_ = nullptr;
  12. size_ = 0;
  13. }
  14. BinaryData::BinaryData(const BinaryData &d) {
  15. size_ = d.size_;
  16. data_ = new unsigned char[size_];
  17. memcpy(data_, d.data_, size_);
  18. }
  19. BinaryData::BinaryData(unsigned int size) {
  20. data_ = new unsigned char[size];
  21. size_ = size;
  22. }
  23. BinaryData::~BinaryData() {
  24. if (size_ != 0 && data_ != nullptr)
  25. delete[] data_;
  26. }
  27. unsigned char& BinaryData::operator[](const unsigned int &pos) {
  28. if (pos >= size_)
  29. throw DatException("Bad BinaryData::operator[]. Position is out of range.");
  30. return data_[pos];
  31. }
  32. BinaryData BinaryData::operator +(const BinaryData &b) {
  33. BinaryData res(size_ + b.size());
  34. memcpy(res.data_, data_, size_);
  35. memcpy(res.data_ + size_, b.data_, b.size_);
  36. return res;
  37. }
  38. // Translates T bytes from data into number using UTF-16LE encoding of the .dat file
  39. template<int T>
  40. long long BinaryData::ToNumber(const long long &pos) const {
  41. try {
  42. long long ans = 0;
  43. if (pos + T > size_)
  44. throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);
  45. for (int i = T - 1; i >= 0; i--)
  46. ans = ((ans << 8ll) | data_[pos + i]);
  47. return ans;
  48. } catch (...) {
  49. throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
  50. }
  51. }
  52. // Translates T bytes from data into number in raw format
  53. template<int T>
  54. long long BinaryData::ToNumberRAW(const long long &pos) const {
  55. try {
  56. long long ans = 0;
  57. if (pos + T >= size_)
  58. throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);
  59. for (int i = 0; i < T; i++)
  60. ans = ((ans << 8ll) | data_[pos + i]);
  61. return ans;
  62. } catch (...) {
  63. throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
  64. }
  65. }
  66. // Makes data from specified T bytes of number in Little Endian encoding
  67. template<int T>
  68. void BinaryData::FromNumber(const long long &number) {
  69. if (T < 0)
  70. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0");
  71. try {
  72. if (size_ != 0 && data_ != nullptr)
  73. delete[] data_;
  74. size_ = size_t(T);
  75. data_ = new unsigned char[size_];
  76. for (size_t i = 0; i < size_; i++)
  77. {
  78. data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  79. }
  80. } catch (...) {
  81. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  82. }
  83. }
  84. // Makes data from specified T bytes of number in raw
  85. template<int T>
  86. void BinaryData::FromNumberRAW(const long long &number) {
  87. if (T < 0)
  88. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0");
  89. try {
  90. if (size_ != 0 && data_ != nullptr)
  91. delete[] data_;
  92. size_ = size_t(T);
  93. data_ = new unsigned char[size_];
  94. for (size_t i = size_ - 1; i >= 0; i--)
  95. {
  96. data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  97. }
  98. } catch (...) {
  99. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  100. }
  101. }
  102. BinaryData &BinaryData::operator=(const BinaryData &data) {
  103. if (&data == this)
  104. return *this;
  105. if (size_ != 0 && data_ != nullptr)
  106. delete[] data_;
  107. size_ = data.size_;
  108. data_ = nullptr;
  109. if (size_ != 0) {
  110. data_ = new unsigned char[size_];
  111. memcpy(data_, data.data_, size_);
  112. }
  113. return *this;
  114. }
  115. size_t BinaryData::size() const {
  116. return size_;
  117. }
  118. unsigned char *BinaryData::data() const {
  119. return data_;
  120. }
  121. bool BinaryData::WriteToFile(const char *filename) const {
  122. FILE *f;
  123. fopen_s(&f, filename, "wb");
  124. if (f == nullptr) {
  125. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  126. }
  127. fwrite(data(), size(), 1, f);
  128. fclose(f);
  129. return true;
  130. }
  131. bool BinaryData::WriteToFile(const std::string &filename) const {
  132. return WriteToFile(filename.c_str());
  133. }
  134. bool BinaryData::CheckCompression() const {
  135. if (size() < 10)
  136. return false;
  137. auto header = ToNumberRAW<2>(4);
  138. return (header == 0x7801 || header == 0x789C || header == 0x78DA);
  139. }
  140. void BinaryData::DecompressData(unsigned int offset) {
  141. const unsigned max_unpacked_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum unpacked data;
  142. BinaryData decompressed(max_unpacked_size);
  143. uLongf new_size = max_unpacked_size;
  144. int res = uncompress(decompressed.data_, &new_size, data_ + offset, size_ - offset);
  145. if (res != 0) {
  146. throw DatException("Bad BinaryData::DecompressData() - uncompress() failed!", DATA_EXCEPTION);
  147. }
  148. decompressed.size_ = (unsigned int)new_size;
  149. *this = decompressed;
  150. return;
  151. }
  152. BinaryData BinaryData::CutData(long long first, long long last) {
  153. if (last < 0)
  154. last = size();
  155. if (last > size())
  156. throw DatException("Bad BinaryData::CutData() - parameter 'last' is out of range");
  157. BinaryData newdata(unsigned(last - first));
  158. memcpy(newdata.data(), data() + first, newdata.size());
  159. return newdata;
  160. }
  161. template long long BinaryData::ToNumber<1>(long long const&) const;
  162. template long long BinaryData::ToNumber<2>(long long const&) const;
  163. template long long BinaryData::ToNumber<4>(long long const&) const;
  164. template long long BinaryData::ToNumber<8>(long long const&) const;
  165. template void BinaryData::FromNumber<1>(const long long &);
  166. template void BinaryData::FromNumber<2>(const long long &);
  167. template void BinaryData::FromNumber<4>(const long long &);
  168. template void BinaryData::FromNumber<8>(const long long &);
  169. template long long BinaryData::ToNumberRAW<1>(long long const&) const;
  170. template long long BinaryData::ToNumberRAW<2>(long long const&) const;
  171. template long long BinaryData::ToNumberRAW<4>(long long const&) const;
  172. template long long BinaryData::ToNumberRAW<8>(long long const&) const;
  173. template void BinaryData::FromNumberRAW<1>(const long long &);
  174. template void BinaryData::FromNumberRAW<2>(const long long &);
  175. template void BinaryData::FromNumberRAW<4>(const long long &);
  176. template void BinaryData::FromNumberRAW<8>(const long long &);
  177. }
  178. }