BinaryData.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "BinaryData.h"
  5. #include "DatException.h"
  6. #include "zlib.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(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. if (size_ != 0 && data_ != nullptr)
  33. delete[] data_;
  34. }
  35. unsigned char& BinaryData::operator[](const unsigned int &pos) {
  36. if (pos >= size_)
  37. throw DatException("Bad BinaryData::operator[]. Position is out of range.");
  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<int T>
  50. long long BinaryData::ToNumber(const long long &pos) const {
  51. try {
  52. long long ans = 0;
  53. if (pos + T > size_)
  54. throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);
  55. for (int i = T - 1; i >= 0; i--)
  56. ans = ((ans << 8ll) | data_[pos + i]);
  57. return ans;
  58. } catch (...) {
  59. throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
  60. }
  61. }
  62. // Translates T bytes from data into number in raw format
  63. template<int T>
  64. long long BinaryData::ToNumberRAW(const long long &pos) const {
  65. try {
  66. long long ans = 0;
  67. if (pos + T >= size_)
  68. throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);
  69. for (int i = 0; i < T; i++)
  70. ans = ((ans << 8ll) | data_[pos + i]);
  71. return ans;
  72. } catch (...) {
  73. throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
  74. }
  75. }
  76. // Makes data from specified T bytes of number in Little Endian encoding
  77. template<int T>
  78. BinaryData BinaryData::FromNumber(const long long &number) {
  79. if (T < 0)
  80. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0");
  81. BinaryData data(T);
  82. try {
  83. for (size_t i = 0; i < T; i++)
  84. data.data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  85. } catch (...) {
  86. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  87. }
  88. return data;
  89. }
  90. // Makes data from specified T bytes of number in raw
  91. template<int T>
  92. BinaryData BinaryData::FromNumberRAW(const long long &number) {
  93. if (T <= 0)
  94. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes <= 0");
  95. BinaryData data = FromNumber<T>(number);
  96. try {
  97. std::reverse(data.data_, data.data_ + data.size());
  98. } catch (...) {
  99. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  100. }
  101. return data;
  102. }
  103. BinaryData &BinaryData::operator=(const BinaryData &data) {
  104. if (&data == this)
  105. return *this;
  106. if (size_ != 0 && data_ != nullptr)
  107. delete[] data_;
  108. size_ = data.size_;
  109. data_ = nullptr;
  110. if (size_ != 0) {
  111. data_ = new unsigned char[size_];
  112. memcpy(data_, data.data_, size_);
  113. }
  114. return *this;
  115. }
  116. size_t BinaryData::size() const {
  117. return size_;
  118. }
  119. unsigned char *BinaryData::data() const {
  120. return data_;
  121. }
  122. bool BinaryData::WriteToFile(const char *filename) const {
  123. FILE *f;
  124. fopen_s(&f, filename, "wb");
  125. if (f == nullptr) {
  126. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  127. }
  128. fwrite(data(), size(), 1, f);
  129. fclose(f);
  130. return true;
  131. }
  132. bool BinaryData::WriteToFile(const std::string &filename) const {
  133. return WriteToFile(filename.c_str());
  134. }
  135. void BinaryData::ReadFromFile(const char *filename) {
  136. FILE *f;
  137. fopen_s(&f, filename, "rb");
  138. if (f == nullptr) {
  139. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  140. }
  141. _fseeki64(f, 0, SEEK_END);
  142. long long file_size = ftell(f);
  143. _fseeki64(f, 0, SEEK_SET);
  144. BinaryData temp_data = BinaryData(unsigned(file_size));
  145. fread(temp_data.data_, temp_data.size_, 1, f);
  146. *this = temp_data;
  147. fclose(f);
  148. }
  149. void BinaryData::ReadFromFile(const std::string &filename) {
  150. ReadFromFile(filename.c_str());
  151. }
  152. bool BinaryData::CheckCompression() const {
  153. if (size() < 10)
  154. return false;
  155. auto header = ToNumberRAW<2>(4);
  156. return (header == 0x7801 || header == 0x789C || header == 0x78DA);
  157. }
  158. BinaryData BinaryData::DecompressData(unsigned int offset) const {
  159. const unsigned max_unpacked_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum unpacked data;
  160. BinaryData decompressed(max_unpacked_size);
  161. uLongf new_size = max_unpacked_size;
  162. int res = uncompress(decompressed.data_, &new_size, data_ + offset, size_ - offset);
  163. if (res != 0) {
  164. throw DatException("Bad BinaryData::DecompressData() - uncompress() failed!", DATA_EXCEPTION);
  165. }
  166. decompressed.size_ = (unsigned int)new_size;
  167. return decompressed;
  168. }
  169. BinaryData BinaryData::CompressData(unsigned int offset) const {
  170. const unsigned max_file_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum packed data;
  171. BinaryData compressed(max_file_size);
  172. uLongf new_size = max_file_size;
  173. int res = compress2(compressed.data_, &new_size, data_ + offset, size_ - offset, 9);
  174. if (res != 0) {
  175. throw DatException("Bad BinaryData::CompressData() - compress failed!", DATA_EXCEPTION);
  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. throw DatException("Bad BinaryData::CutData() - parameter 'last' is out of range");
  185. BinaryData newdata(unsigned(last - first));
  186. memcpy(newdata.data(), data() + first, newdata.size());
  187. return newdata;
  188. }
  189. bool BinaryData::operator==(const BinaryData &b) const {
  190. if (size() != b.size())
  191. return false;
  192. for (size_t i = 0; i < size(); i++)
  193. if (data_[i] != b.data_[i])
  194. return false;
  195. return true;
  196. }
  197. bool BinaryData::operator!=(const BinaryData &b) const {
  198. return !(*this == b);
  199. }
  200. template long long BinaryData::ToNumber<1>(long long const&) const;
  201. template long long BinaryData::ToNumber<2>(long long const&) const;
  202. template long long BinaryData::ToNumber<4>(long long const&) const;
  203. template long long BinaryData::ToNumber<8>(long long const&) const;
  204. template BinaryData BinaryData::FromNumber<1>(const long long &);
  205. template BinaryData BinaryData::FromNumber<2>(const long long &);
  206. template BinaryData BinaryData::FromNumber<4>(const long long &);
  207. template BinaryData BinaryData::FromNumber<8>(const long long &);
  208. template long long BinaryData::ToNumberRAW<1>(long long const&) const;
  209. template long long BinaryData::ToNumberRAW<2>(long long const&) const;
  210. template long long BinaryData::ToNumberRAW<4>(long long const&) const;
  211. template long long BinaryData::ToNumberRAW<8>(long long const&) const;
  212. template BinaryData BinaryData::FromNumberRAW<1>(const long long &);
  213. template BinaryData BinaryData::FromNumberRAW<2>(const long long &);
  214. template BinaryData BinaryData::FromNumberRAW<4>(const long long &);
  215. template BinaryData BinaryData::FromNumberRAW<8>(const long long &);
  216. }
  217. }