Clone of PhatAC @ https://github.com/floaterxk/PhatAC

BinaryReader.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #define MAX_MEALSTRING_LEN 0x0800
  3. class BinaryReader
  4. {
  5. public:
  6. BinaryReader(BYTE *pData, DWORD dwSize);
  7. ~BinaryReader();
  8. #define BOUND_CHECK(x) \
  9. if ( !(x) ) \
  10. { \
  11. m_dwErrorCode = 1; \
  12. memset(&returnValue, 0, sizeof(returnValue)); \
  13. return returnValue; \
  14. }
  15. template <typename ReturnType> ReturnType Read()
  16. {
  17. ReturnType returnValue;
  18. BOUND_CHECK((m_pData + sizeof(ReturnType)) <= m_pEnd);
  19. returnValue = *((ReturnType *)m_pData);
  20. m_pData += sizeof(ReturnType);
  21. return returnValue;
  22. }
  23. #define STREAM_OUT(func, type) type func() { return Read<type>(); }
  24. STREAM_OUT(ReadChar, BYTE);
  25. STREAM_OUT(ReadShort, BYTE);
  26. STREAM_OUT(ReadLong, BYTE);
  27. STREAM_OUT(ReadBYTE, BYTE);
  28. STREAM_OUT(ReadWORD, WORD);
  29. STREAM_OUT(ReadDWORD, DWORD);
  30. STREAM_OUT(ReadFloat, float);
  31. STREAM_OUT(ReadDouble, double);
  32. STREAM_OUT(ReadByte, BYTE);
  33. STREAM_OUT(ReadInt8, char);
  34. STREAM_OUT(ReadUInt8, BYTE);
  35. STREAM_OUT(ReadInt16, short);
  36. STREAM_OUT(ReadUInt16, WORD);
  37. STREAM_OUT(ReadInt32, int);
  38. STREAM_OUT(ReadUInt32, DWORD);
  39. STREAM_OUT(ReadSingle, float);
  40. __forceinline DWORD ReadPackedDWORD()
  41. {
  42. DWORD returnValue;
  43. BOUND_CHECK((m_pData + sizeof(WORD)) <= m_pEnd);
  44. returnValue = *((WORD *)m_pData);
  45. if (returnValue & 0x8000)
  46. {
  47. BOUND_CHECK((m_pData + sizeof(DWORD)) <= m_pEnd);
  48. DWORD src = *((DWORD *)m_pData);
  49. returnValue = (((src & 0x3FFF) << 16) | (src >> 16));
  50. m_pData += sizeof(DWORD);
  51. }
  52. else
  53. {
  54. m_pData += sizeof(WORD);
  55. }
  56. return returnValue;
  57. }
  58. template<typename A, typename B> std::map<A, B> ReadMap()
  59. {
  60. std::map<A, B> table;
  61. WORD count = ReadWORD();
  62. ReadWORD();
  63. while (count > 0 && !m_dwErrorCode)
  64. {
  65. A theKey = Read<A>();
  66. B theValue = Read<B>();
  67. table.insert(std::pair<A, B>(theKey, theValue));
  68. count--;
  69. }
  70. return table;
  71. }
  72. template<typename A> std::map<A, std::string> ReadMap()
  73. {
  74. std::map<A, std::string> table;
  75. WORD count = ReadWORD();
  76. ReadWORD();
  77. while (count > 0 && !m_dwErrorCode)
  78. {
  79. A theKey = Read<A>();
  80. std::string theValue = ReadString();
  81. table.insert(std::pair<A, std::string>(theKey, theValue));
  82. count--;
  83. }
  84. return table;
  85. }
  86. void ReadAlign(void);
  87. void *ReadArray(size_t size);
  88. char *ReadString(void);
  89. BYTE *GetDataStart(void);
  90. BYTE *GetDataPtr(void);
  91. BYTE *GetDataEnd(void);
  92. DWORD GetDataLen(void);
  93. DWORD GetOffset(void);
  94. DWORD GetLastError(void);
  95. DWORD GetDataRemaining(void);
  96. void SetOffset(DWORD offset);
  97. private:
  98. DWORD m_dwErrorCode;
  99. BYTE *m_pData;
  100. BYTE *m_pStart;
  101. BYTE *m_pEnd;
  102. std::list<char *> m_lStrings;
  103. };