1
0

FLVUtil.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef NULLSOFT_FLVUTIL_H
  2. #define NULLSOFT_FLVUTIL_H
  3. #include <memory.h>
  4. #include <bfc/platform/types.h>
  5. namespace FLV
  6. {
  7. // reads 32 bits from data and converts from big endian
  8. inline uint32_t Read32(uint8_t *data)
  9. {
  10. uint32_t returnVal;
  11. #ifdef __BIG_ENDIAN__
  12. returnVal = *(uint32_t *)(&data[0]);
  13. #else
  14. // need to swap endianness
  15. uint8_t *swap = (uint8_t *)&returnVal;
  16. swap[0]=data[3];
  17. swap[1]=data[2];
  18. swap[2]=data[1];
  19. swap[3]=data[0];
  20. #endif
  21. return returnVal;
  22. }
  23. // reads 24 bits from data, converts from big endian, and returns as a 32bit int
  24. inline uint32_t Read24(uint8_t *data)
  25. {
  26. uint32_t returnVal=0;
  27. uint8_t *swap = (uint8_t *)&returnVal;
  28. #ifdef __BIG_ENDIAN__
  29. swap[1]=data[0];
  30. swap[2]=data[1];
  31. swap[3]=data[2];
  32. returnVal = *(uint32_t *)(&data[0]);
  33. #else
  34. // need to swap endianness
  35. swap[0]=data[2];
  36. swap[1]=data[1];
  37. swap[2]=data[0];
  38. #endif
  39. return returnVal;
  40. }
  41. // reads 16 bits from data and converts from big endian
  42. inline uint16_t Read16(uint8_t *data)
  43. {
  44. uint16_t returnVal;
  45. #ifdef __BIG_ENDIAN__
  46. returnVal = *(uint16_t *)(&data[0]);
  47. #else
  48. // need to swap endianness
  49. uint8_t *swap = (uint8_t *)&returnVal;
  50. swap[0]=data[1];
  51. swap[1]=data[0];
  52. #endif
  53. return returnVal;
  54. }
  55. // reads 16 bits from data and converts from big endian
  56. inline uint8_t Read8(uint8_t *data)
  57. {
  58. uint8_t returnVal;
  59. returnVal = *(uint8_t *)(&data[0]);
  60. return returnVal;
  61. }
  62. // reads a double from data
  63. inline double ReadDouble(uint8_t *data)
  64. {
  65. double returnVal;
  66. #ifdef __BIG_ENDIAN__
  67. memcpy(&returnVal, data, 8);
  68. #else
  69. uint8_t *swap = (uint8_t *)&returnVal;
  70. swap[0]=data[7];
  71. swap[1]=data[6];
  72. swap[2]=data[5];
  73. swap[3]=data[4];
  74. swap[4]=data[3];
  75. swap[5]=data[2];
  76. swap[6]=data[1];
  77. swap[7]=data[0];
  78. #endif
  79. return returnVal;
  80. }
  81. }
  82. #endif