You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

amf3.c 1.5KB

5 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "amf3.h"
  2. #include <string.h>
  3. #include <assert.h>
  4. static double s_double = 1.0; // 3ff0 0000 0000 0000
  5. const uint8_t* AMF3ReadNull(const uint8_t* ptr, const uint8_t* end)
  6. {
  7. (void)end;
  8. return ptr;
  9. }
  10. const uint8_t* AMF3ReadBoolean(const uint8_t* ptr, const uint8_t* end)
  11. {
  12. (void)end;
  13. return ptr;
  14. }
  15. const uint8_t* AMF3ReadInteger(const uint8_t* ptr, const uint8_t* end, int32_t* value)
  16. {
  17. int i;
  18. int32_t v = 0;
  19. for (i = 0; i < 3 && ptr + i < end && (0x80 & ptr[i]); i++)
  20. {
  21. v <<= 7;
  22. v |= (ptr[i] & 0x7F);
  23. }
  24. if (ptr + i >= end)
  25. return NULL;
  26. if (3 == i)
  27. {
  28. v <<= 8;
  29. v |= ptr[i];
  30. if (v >= (1 << 28))
  31. v -= (1 << 29);
  32. }
  33. else
  34. {
  35. v <<= 7;
  36. v |= ptr[i];
  37. }
  38. *value = v;
  39. return ptr + i + 1;
  40. }
  41. const uint8_t* AMF3ReadDouble(const uint8_t* ptr, const uint8_t* end, double* value)
  42. {
  43. uint8_t* p = (uint8_t*)value;
  44. if (!ptr || end - ptr < 8)
  45. return NULL;
  46. if (value)
  47. {
  48. if (0x00 == *(char*)&s_double)
  49. {// Little-Endian
  50. *p++ = ptr[7];
  51. *p++ = ptr[6];
  52. *p++ = ptr[5];
  53. *p++ = ptr[4];
  54. *p++ = ptr[3];
  55. *p++ = ptr[2];
  56. *p++ = ptr[1];
  57. *p++ = ptr[0];
  58. }
  59. else
  60. {
  61. memcpy(&value, ptr, 8);
  62. }
  63. }
  64. return ptr + 8;
  65. }
  66. const uint8_t* AMF3ReadString(const uint8_t* ptr, const uint8_t* end, char* string, uint32_t* length)
  67. {
  68. uint32_t v;
  69. ptr = AMF3ReadInteger(ptr, end, (int32_t*)&v);
  70. if (v & 0x01)
  71. {
  72. // reference
  73. return ptr;
  74. }
  75. else
  76. {
  77. *length = v >> 1;
  78. memcpy(string, ptr, *length);
  79. string[*length] = 0;
  80. return ptr + *length;
  81. }
  82. }