BinaryData.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 (int 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. void 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. try {
  82. delete[] data_;
  83. size_ = size_t(T);
  84. data_ = new unsigned char[size_];
  85. for (size_t i = 0; i < size_; i++)
  86. {
  87. data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  88. }
  89. } catch (...) {
  90. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  91. }
  92. }
  93. // Makes data from specified T bytes of number in raw
  94. template<int T>
  95. void BinaryData::FromNumberRAW(const long long &number) {
  96. if (T <= 0)
  97. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes <= 0");
  98. try {
  99. FromNumber<T>(number);
  100. std::reverse(data_, data_ + size());
  101. } catch (...) {
  102. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  103. }
  104. }
  105. BinaryData &BinaryData::operator=(const BinaryData &data) {
  106. if (&data == this)
  107. return *this;
  108. if (size_ != 0 && data_ != nullptr)
  109. delete[] data_;
  110. size_ = data.size_;
  111. data_ = nullptr;
  112. if (size_ != 0) {
  113. data_ = new unsigned char[size_];
  114. memcpy(data_, data.data_, size_);
  115. }
  116. return *this;
  117. }
  118. size_t BinaryData::size() const {
  119. return size_;
  120. }
  121. unsigned char *BinaryData::data() const {
  122. return data_;
  123. }
  124. bool BinaryData::WriteToFile(const char *filename) const {
  125. FILE *f;
  126. fopen_s(&f, filename, "wb");
  127. if (f == nullptr) {
  128. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  129. }
  130. fwrite(data(), size(), 1, f);
  131. fclose(f);
  132. return true;
  133. }
  134. bool BinaryData::WriteToFile(const std::string &filename) const {
  135. return WriteToFile(filename.c_str());
  136. }
  137. void BinaryData::ReadFromFile(const char *filename) {
  138. FILE *f;
  139. fopen_s(&f, filename, "rb");
  140. if (f == nullptr) {
  141. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  142. }
  143. _fseeki64(f, 0, SEEK_END);
  144. long long file_size = ftell(f);
  145. _fseeki64(f, 0, SEEK_SET);
  146. BinaryData temp_data = BinaryData(unsigned(file_size));
  147. fread(temp_data.data_, temp_data.size_, 1, f);
  148. *this = temp_data;
  149. fclose(f);
  150. }
  151. void BinaryData::ReadFromFile(const std::string &filename) {
  152. ReadFromFile(filename.c_str());
  153. }
  154. bool BinaryData::CheckCompression() const {
  155. if (size() < 10)
  156. return false;
  157. auto header = ToNumberRAW<2>(4);
  158. return (header == 0x7801 || header == 0x789C || header == 0x78DA);
  159. }
  160. BinaryData BinaryData::DecompressData(unsigned int offset) const {
  161. const unsigned max_unpacked_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum unpacked data;
  162. BinaryData decompressed(max_unpacked_size);
  163. uLongf new_size = max_unpacked_size;
  164. int res = uncompress(decompressed.data_, &new_size, data_ + offset, size_ - offset);
  165. if (res != 0) {
  166. throw DatException("Bad BinaryData::DecompressData() - uncompress() failed!", DATA_EXCEPTION);
  167. }
  168. decompressed.size_ = (unsigned int)new_size;
  169. return decompressed;
  170. }
  171. BinaryData BinaryData::CompressData(unsigned int offset) const {
  172. const unsigned max_file_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum packed data;
  173. BinaryData compressed(max_file_size);
  174. uLongf new_size = max_file_size;
  175. int res = compress2(compressed.data_, &new_size, data_ + offset, size_ - offset, 9);
  176. if (res != 0) {
  177. throw DatException("Bad BinaryData::CompressData() - compress failed!", DATA_EXCEPTION);
  178. }
  179. compressed.size_ = (unsigned int)new_size;
  180. return compressed;
  181. }
  182. BinaryData BinaryData::CutData(long long first, long long last) const {
  183. if (last < 0)
  184. last = size();
  185. if (last > size())
  186. throw DatException("Bad BinaryData::CutData() - parameter 'last' is out of range");
  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 (int 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. template long long BinaryData::ToNumber<1>(long long const&) const;
  203. template long long BinaryData::ToNumber<2>(long long const&) const;
  204. template long long BinaryData::ToNumber<4>(long long const&) const;
  205. template long long BinaryData::ToNumber<8>(long long const&) const;
  206. template void BinaryData::FromNumber<1>(const long long &);
  207. template void BinaryData::FromNumber<2>(const long long &);
  208. template void BinaryData::FromNumber<4>(const long long &);
  209. template void BinaryData::FromNumber<8>(const long long &);
  210. template long long BinaryData::ToNumberRAW<1>(long long const&) const;
  211. template long long BinaryData::ToNumberRAW<2>(long long const&) const;
  212. template long long BinaryData::ToNumberRAW<4>(long long const&) const;
  213. template long long BinaryData::ToNumberRAW<8>(long long const&) const;
  214. template void BinaryData::FromNumberRAW<1>(const long long &);
  215. template void BinaryData::FromNumberRAW<2>(const long long &);
  216. template void BinaryData::FromNumberRAW<4>(const long long &);
  217. template void BinaryData::FromNumberRAW<8>(const long long &);
  218. }
  219. }