1
0

extendedheader.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "extendedheader.h"
  2. #include "util.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "foundation/error.h"
  6. ID3v2_21::ExtendedHeaderBase::ExtendedHeaderBase(const ID3v2::Header &_tagHeader) : tagHeader(_tagHeader)
  7. {
  8. memset(&headerData, 0, sizeof(ExtendedHeaderData));
  9. data = 0;
  10. data_size = 0;
  11. }
  12. uint32_t ID3v2_21::ExtendedHeaderBase::Size() const
  13. {
  14. return headerData.size;
  15. }
  16. int ID3v2_21::ExtendedHeaderBase::Parse(const void *_data, size_t len, size_t *bytes_read)
  17. {
  18. if (len < SIZE)
  19. return 1;
  20. if (tagHeader.Unsynchronised())
  21. {
  22. *bytes_read = ID3v2::Util::UnsynchroniseTo(&headerData, _data, SIZE);
  23. }
  24. else
  25. {
  26. memcpy(&headerData, _data, SIZE);
  27. *bytes_read = SIZE;
  28. }
  29. _data = (const uint8_t *)_data+SIZE;
  30. /* read any data after the header */
  31. data_size = Size();
  32. if (data_size)
  33. {
  34. /* sanity check size */
  35. if (tagHeader.Unsynchronised())
  36. {
  37. if (ID3v2::Util::UnsynchronisedInputSize(_data, data_size) > len)
  38. return 1;
  39. }
  40. else if (data_size > len)
  41. return 1;
  42. /* allocate and read data */
  43. data = malloc(data_size);
  44. if (tagHeader.Unsynchronised())
  45. {
  46. *bytes_read += ID3v2::Util::UnsynchroniseTo(data, _data, data_size);
  47. }
  48. else
  49. {
  50. memcpy(data, _data, data_size);
  51. *bytes_read += data_size;
  52. }
  53. }
  54. return 0;
  55. }
  56. /* === ID3v2.3 === */
  57. ID3v2_3::ExtendedHeader::ExtendedHeader(const ID3v2::Header &_tagHeader) : ID3v2_21::ExtendedHeaderBase(_tagHeader)
  58. {
  59. }
  60. /* === ID3v2.4 === */
  61. ID3v2_4::ExtendedHeader::ExtendedHeader(const ID3v2::Header &_tagHeader) : ID3v2_21::ExtendedHeaderBase(_tagHeader)
  62. {
  63. }
  64. uint32_t ID3v2_4::ExtendedHeader::Size() const
  65. {
  66. return ID3v2::Util::Int28To32(headerData.size);
  67. }
  68. int ID3v2_4::ExtendedHeader::Parse(const void *_data, size_t len, size_t *bytes_read)
  69. {
  70. if (len < SIZE)
  71. return 1;
  72. memcpy(&headerData, _data, SIZE);
  73. *bytes_read = SIZE;
  74. _data = (const uint8_t *)_data+SIZE;
  75. /* read any data after the header */
  76. data_size = Size();
  77. if (data_size)
  78. {
  79. /* sanity check size */
  80. if (data_size > len)
  81. return 1;
  82. /* allocate and read data */
  83. data = malloc(data_size);
  84. if (!data)
  85. return NErr_OutOfMemory;
  86. memcpy(data, _data, data_size);
  87. *bytes_read += data_size;
  88. }
  89. return 0;
  90. }