binarydata.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "binarydata.h"
  2. #include "EasyLogging++/easylogging++.h"
  3. #include <algorithm>
  4. extern "C++"
  5. {
  6. namespace LOTRO_DAT {
  7. BinaryData::BinaryData() {
  8. data_ = nullptr;
  9. size_ = 0;
  10. }
  11. BinaryData::BinaryData(const BinaryData &d) {
  12. size_ = d.size_;
  13. data_ = new unsigned char[size_];
  14. memcpy(data_, d.data_, size_);
  15. }
  16. BinaryData::BinaryData(BinaryData &&other) noexcept {
  17. size_ = other.size_;
  18. data_ = other.data_;
  19. other.size_ = 0;
  20. other.data_ = nullptr;
  21. }
  22. BinaryData::BinaryData(const char* data, unsigned int size) {
  23. size_ = size;
  24. data_ = new unsigned char[size_];
  25. memcpy(data_, data, size_);
  26. }
  27. BinaryData::BinaryData(unsigned int size) {
  28. data_ = new unsigned char[size];
  29. for (size_t i = 0; i < size; i++)
  30. data_[i] = 0;
  31. size_ = size;
  32. }
  33. BinaryData::~BinaryData() {
  34. delete[] data_;
  35. }
  36. unsigned char& BinaryData::operator[](const unsigned int &pos) {
  37. if (pos >= size_)
  38. LOG(ERROR) << "Bad BinaryData::operator[]. Position " << pos
  39. << " is out of range in BinaryData with size " << size_;
  40. return data_[pos];
  41. }
  42. const unsigned char& BinaryData::operator[] (const unsigned int &pos) const {
  43. if (pos >= size_)
  44. LOG(ERROR) << "Bad BinaryData::operator[]. Position " << pos
  45. << " is out of range in BinaryData with size " << size_;
  46. return data_[pos];
  47. }
  48. BinaryData BinaryData::operator +(const BinaryData &b) {
  49. BinaryData res(size_ + b.size());
  50. if (size_ > 0)
  51. memcpy(res.data_, data_, size_);
  52. if (b.size() > 0)
  53. memcpy(res.data_ + size_, b.data_, b.size_);
  54. return res;
  55. }
  56. // Translates T bytes from data into number using UTF-16LE encoding of the .dat file
  57. template<unsigned int T>
  58. long long BinaryData::ToNumber(const long long &pos) const {
  59. unsigned long long ans = 0;
  60. if (pos + T > size_) {
  61. LOG(ERROR) << "Reading " << T << " bytes from " << pos << " offset with BinaryData size " << size_
  62. << " Reached end of BinaryData!";
  63. return 0;
  64. }
  65. for (int i = T - 1; i >= 0; i--)
  66. ans = ((ans << 8ull) | data_[pos + i]);
  67. return ans;
  68. }
  69. // Translates T bytes from data into number in raw format
  70. template<unsigned int T>
  71. long long BinaryData::ToNumberRAW(const long long &pos) const {
  72. long long ans = 0;
  73. if (pos + T > size_) {
  74. LOG(ERROR) << "Reading " << T << " bytes from " << pos << " offset with BinaryData size " << size_
  75. << " Reached end of BinaryData!";
  76. return 0;
  77. }
  78. for (unsigned i = 0; i < T; i++)
  79. ans = ((ans << 8ll) | data_[pos + i]);
  80. return ans;
  81. }
  82. // Makes data from specified T bytes of number in Little Endian encoding
  83. template<unsigned int T>
  84. BinaryData BinaryData::FromNumber(const long long &number) {
  85. if (T <= 0) {
  86. LOG(ERROR) << "Trying to make data from amount of bytes < 0";
  87. return BinaryData(0);
  88. }
  89. BinaryData data(T);
  90. for (size_t i = 0; i < T; i++)
  91. data.data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  92. return data;
  93. }
  94. // Makes data from specified T bytes of number in raw
  95. template<unsigned int T>
  96. BinaryData BinaryData::FromNumberRAW(const long long &number) {
  97. if (T <= 0) {
  98. LOG(ERROR) << "Trying to make data from amount of bytes < 0";
  99. return BinaryData(0);
  100. }
  101. BinaryData data = FromNumber<T>(number);
  102. std::reverse(data.data_, data.data_ + data.size());
  103. return data;
  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. //f = fopen64(filename, "wb");
  127. f = fopen(filename, "wb");
  128. if (f == nullptr) {
  129. LOG(ERROR) << "File " << std::string(filename) << " doesn't exist or is unreachable.. Cannot write data";
  130. return false;
  131. }
  132. fwrite(data(), size(), 1, f);
  133. fclose(f);
  134. return true;
  135. }
  136. bool BinaryData::WriteToFile(const std::string &filename) const {
  137. return WriteToFile(filename.c_str());
  138. }
  139. void BinaryData::ReadFromFile(const char *filename) {
  140. FILE *f;
  141. fopen_s(&f, filename, "rb");
  142. if (f == nullptr) {
  143. LOG(ERROR) << "File " << std::string(filename) << " doesn't exist.. Retuning null data";
  144. size_ = 0;
  145. delete[] data_;
  146. return;
  147. }
  148. _fseeki64(f, 0, SEEK_END);
  149. long long file_size = ftell(f);
  150. _fseeki64(f, 0, SEEK_SET);
  151. BinaryData temp_data = BinaryData(unsigned(file_size));
  152. fread(temp_data.data_, temp_data.size_, 1, f);
  153. *this = temp_data;
  154. fclose(f);
  155. }
  156. void BinaryData::ReadFromFile(const std::string &filename) {
  157. ReadFromFile(filename.c_str());
  158. }
  159. BinaryData BinaryData::CutData(long long first, long long last) const {
  160. if (last < 0)
  161. last = size();
  162. if (last > size()) {
  163. LOG(ERROR) << "Unable to cut data - parameter last is out of range";
  164. return BinaryData(0);
  165. }
  166. BinaryData newdata(unsigned(last - first));
  167. memcpy(newdata.data(), data() + first, newdata.size());
  168. return newdata;
  169. }
  170. bool BinaryData::operator==(const BinaryData &b) const {
  171. if (size() != b.size())
  172. return false;
  173. for (size_t i = 0; i < size(); i++)
  174. if (data_[i] != b.data_[i])
  175. return false;
  176. return true;
  177. }
  178. bool BinaryData::operator!=(const BinaryData &b) const {
  179. return !(*this == b);
  180. }
  181. bool BinaryData::Empty() const {
  182. return size_ == 0;
  183. }
  184. void BinaryData::Append(const BinaryData &b, size_t append_offset) {
  185. if (append_offset + b.size() > size()) {
  186. LOG(ERROR) << "data for appending has more bytes than BinaryData size!";
  187. return;
  188. }
  189. memcpy(data_ + append_offset, b.data_, b.size_);
  190. }
  191. template long long BinaryData::ToNumber<1>(long long const&) const;
  192. template long long BinaryData::ToNumber<2>(long long const&) const;
  193. template long long BinaryData::ToNumber<4>(long long const&) const;
  194. template long long BinaryData::ToNumber<8>(long long const&) const;
  195. template BinaryData BinaryData::FromNumber<1>(const long long &);
  196. template BinaryData BinaryData::FromNumber<2>(const long long &);
  197. template BinaryData BinaryData::FromNumber<4>(const long long &);
  198. template BinaryData BinaryData::FromNumber<8>(const long long &);
  199. template long long BinaryData::ToNumberRAW<1>(long long const&) const;
  200. template long long BinaryData::ToNumberRAW<2>(long long const&) const;
  201. template long long BinaryData::ToNumberRAW<4>(long long const&) const;
  202. template long long BinaryData::ToNumberRAW<8>(long long const&) const;
  203. template BinaryData BinaryData::FromNumberRAW<1>(const long long &);
  204. template BinaryData BinaryData::FromNumberRAW<2>(const long long &);
  205. template BinaryData BinaryData::FromNumberRAW<4>(const long long &);
  206. template BinaryData BinaryData::FromNumberRAW<8>(const long long &);
  207. }
  208. }