BinaryData.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "BinaryData.h"
  5. #include "DatException.h"
  6. extern "C++"
  7. {
  8. namespace LOTRO_DAT {
  9. BinaryData::BinaryData() {
  10. data_ = nullptr;
  11. size_ = 0;
  12. }
  13. BinaryData::BinaryData(const BinaryData &d) {
  14. size_ = d.size_;
  15. data_ = new unsigned char[size_];
  16. memcpy(data_, d.data_, size_);
  17. }
  18. BinaryData::BinaryData(unsigned int size) {
  19. data_ = new unsigned char[size];
  20. size_ = size;
  21. }
  22. BinaryData::~BinaryData() {
  23. if (size_ != 0 && data_ != nullptr)
  24. delete[] data_;
  25. }
  26. unsigned char& BinaryData::operator[](const unsigned int &pos) {
  27. if (pos >= size_)
  28. throw DatException("Bad BinaryData::operator[]. Position is out of range.");
  29. return data_[pos];
  30. }
  31. // Translates data into specified number type due to UTF-16LE encoding of the .dat file
  32. template<int T>
  33. long long BinaryData::ToNumber(const long long &pos) const {
  34. try {
  35. long long ans = 0;
  36. if (pos + 3 >= size_)
  37. throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);
  38. for (int i = T - 1; i >= 0; i--)
  39. ans = ((ans << 8ll) | (unsigned char)(data_[pos + i]));
  40. return ans;
  41. } catch (...) {
  42. throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
  43. }
  44. }
  45. // Makes data from specified number type
  46. template<int T>
  47. void BinaryData::FromNumber(const long long &number) {
  48. if (T < 0)
  49. throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0");
  50. try {
  51. if (size_ != 0 && data_ != nullptr)
  52. delete[] data_;
  53. size_ = size_t(T);
  54. data_ = new unsigned char[size_];
  55. for (size_t i = 0; i < size_; i++)
  56. {
  57. data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
  58. }
  59. } catch (...) {
  60. throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
  61. }
  62. }
  63. BinaryData &BinaryData::operator=(const BinaryData &data) {
  64. if (&data == this)
  65. return *this;
  66. if (size_ != 0 && data_ != nullptr)
  67. delete[] data_;
  68. size_ = data.size_;
  69. data_ = nullptr;
  70. if (size_ != 0) {
  71. data_ = new unsigned char[size_];
  72. memcpy(data_, data.data_, size_);
  73. }
  74. return *this;
  75. }
  76. size_t BinaryData::size() const {
  77. return size_;
  78. }
  79. unsigned char *BinaryData::data() const {
  80. return data_;
  81. }
  82. bool BinaryData::WriteToFile(const char *filename) const {
  83. FILE *f;
  84. fopen_s(&f, filename, "wb");
  85. if (f == nullptr) {
  86. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  87. return false;
  88. }
  89. fwrite(data(), size(), 1, f);
  90. fclose(f);
  91. return true;
  92. }
  93. bool BinaryData::WriteToFile(const std::string &filename) const {
  94. FILE *f;
  95. fopen_s(&f, filename.c_str(), "wb");
  96. if (f == nullptr) {
  97. throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
  98. return false;
  99. }
  100. fwrite(data(), size(), 1, f);
  101. fclose(f);
  102. return true;
  103. }
  104. template long long BinaryData::ToNumber<1>(long long const&) const;
  105. template long long BinaryData::ToNumber<2>(long long const&) const;
  106. template long long BinaryData::ToNumber<4>(long long const&) const;
  107. template long long BinaryData::ToNumber<8>(long long const&) const;
  108. template void BinaryData::FromNumber<1>(const long long &);
  109. template void BinaryData::FromNumber<2>(const long long &);
  110. template void BinaryData::FromNumber<4>(const long long &);
  111. template void BinaryData::FromNumber<8>(const long long &);
  112. }
  113. }