AMFObject.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef NULLSOFT_AMFOBJECT_H
  2. #define NULLSOFT_AMFOBJECT_H
  3. #include "FLVUtil.h"
  4. #include <windows.h>
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. #include <time.h>
  9. class AMFType // no, not bowling, ActionScript Message Format
  10. {
  11. public:
  12. AMFType() : refCount(1) {}
  13. virtual ~AMFType() {}
  14. virtual size_t Read(uint8_t *data, size_t size)=0; // returns number of bytes read, 0 on failure
  15. uint8_t type;
  16. virtual void DebugPrint(int spaces, wchar_t *&str, size_t &len)=0;
  17. enum
  18. {
  19. TYPE_DOUBLE = 0x0,
  20. TYPE_BOOL = 0x1,
  21. TYPE_STRING = 0x2,
  22. TYPE_OBJECT = 0x3,
  23. TYPE_MOVIE = 0x4,
  24. TYPE_NULL = 0x5,
  25. TYPE_REFERENCE = 0x7,
  26. TYPE_MIXEDARRAY = 0x8,
  27. TYPE_TERMINATOR = 0x9,
  28. TYPE_ARRAY = 0xA,
  29. TYPE_DATE = 0xB,
  30. TYPE_LONG_STRING = 0xC,
  31. TYPE_XML = 0xF,
  32. };
  33. ULONG AddRef(void)
  34. {
  35. return InterlockedIncrement((volatile LONG *)&refCount);
  36. }
  37. ULONG Release(void)
  38. {
  39. ULONG count = InterlockedDecrement((volatile LONG *)&refCount);
  40. if (count == 0)
  41. delete this;
  42. return count;
  43. }
  44. private:
  45. ULONG refCount;
  46. };
  47. class AMFString : public AMFType
  48. {
  49. public:
  50. AMFString();
  51. ~AMFString();
  52. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  53. size_t Read(uint8_t *data, size_t size);
  54. wchar_t *str;
  55. };
  56. class AMFLongString : public AMFType
  57. {
  58. public:
  59. AMFLongString();
  60. ~AMFLongString();
  61. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  62. size_t Read(uint8_t *data, size_t size);
  63. wchar_t *str;
  64. };
  65. class AMFObj : public AMFType
  66. {
  67. public:
  68. size_t Read(uint8_t *data, size_t size);
  69. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  70. };
  71. class AMFArray : public AMFType
  72. {
  73. public:
  74. size_t Read(uint8_t *data, size_t size);
  75. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  76. ~AMFArray();
  77. std::vector<AMFType*> array;
  78. };
  79. class AMFMixedArray : public AMFType
  80. {
  81. public:
  82. size_t Read(uint8_t *data, size_t size);
  83. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  84. ~AMFMixedArray();
  85. typedef std::map<std::wstring, AMFType *> AMFTypeList;
  86. AMFTypeList array;
  87. };
  88. class AMFDouble : public AMFType
  89. {
  90. public:
  91. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  92. size_t Read(uint8_t *data, size_t size)
  93. {
  94. if (size < 8)
  95. return 0;
  96. val = FLV::ReadDouble(data);
  97. return 8;
  98. }
  99. double val;
  100. };
  101. class AMFTime : public AMFType
  102. {
  103. public:
  104. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  105. size_t Read(uint8_t *data, size_t size)
  106. {
  107. if (size < 10)
  108. return 0;
  109. val = FLV::ReadDouble(data);
  110. data+=8;
  111. offset = FLV::Read16(data);
  112. return 10;
  113. }
  114. double val; // same epoch as time_t, just stored as a double instead of an unsigned int
  115. int16_t offset; // offset in minutes from UTC. presumably from the encoding machine's timezone
  116. };
  117. class AMFBoolean : public AMFType
  118. {
  119. public:
  120. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  121. size_t Read(uint8_t *data, size_t size)
  122. {
  123. if (size < 1)
  124. return 0;
  125. boolean = !!data[0];
  126. return 1;
  127. }
  128. bool boolean;
  129. };
  130. class AMFTerminator : public AMFType
  131. {
  132. public:
  133. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  134. size_t Read(uint8_t *data, size_t size)
  135. {
  136. return 0;
  137. }
  138. };
  139. class AMFReference : public AMFType
  140. {
  141. public:
  142. void DebugPrint(int spaces, wchar_t *&str, size_t &len);
  143. size_t Read(uint8_t *data, size_t size)
  144. {
  145. if (size < 2)
  146. return 0;
  147. val = FLV::Read16(data);
  148. return 2;
  149. }
  150. uint16_t val;
  151. };
  152. inline double AMFGetDouble(AMFType *obj)
  153. {
  154. if (obj->type == 0x0)
  155. return ((AMFDouble *)obj)->val;
  156. return 0;
  157. }
  158. AMFType *MakeObject(uint8_t type);
  159. #endif