header.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "header.h"
  2. #include "flags.h"
  3. #include "util.h"
  4. #include "nu/ByteReader.h"
  5. #include "nu/ByteWriter.h"
  6. #include <limits.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. static char apev2_preamble[] = { 'A', 'P', 'E', 'T', 'A', 'G', 'E', 'X' };
  10. APEv2::Header::Header()
  11. {
  12. memcpy(preamble, apev2_preamble, 8);
  13. version=2000;
  14. size=0;
  15. items=0;
  16. flags=0;
  17. reserved=0;
  18. }
  19. APEv2::Header::Header(const void *data)
  20. {
  21. bytereader_value_t byte_reader;
  22. bytereader_init(&byte_reader, data, Header::SIZE);
  23. bytereader_read_n(&byte_reader, preamble, 8);
  24. version=bytereader_read_u32_le(&byte_reader);
  25. size=bytereader_read_u32_le(&byte_reader);
  26. items=bytereader_read_u32_le(&byte_reader);
  27. flags=bytereader_read_u32_le(&byte_reader);
  28. bytereader_read_n(&byte_reader, &reserved, 8);
  29. }
  30. uint32_t APEv2::Header::GetFlags() const
  31. {
  32. return flags;
  33. }
  34. bool APEv2::Header::Valid() const
  35. {
  36. return !memcmp(preamble, apev2_preamble, 8) && reserved == 0;
  37. }
  38. uint32_t APEv2::Header::TagSize() const
  39. {
  40. size_t size = this->size;
  41. if (IsHeader() && HasFooter())
  42. size+=SIZE;
  43. if (IsFooter() && HasHeader())
  44. size+=SIZE;
  45. if (size > ULONG_MAX)
  46. return 0;
  47. return (uint32_t)size;
  48. }
  49. bool APEv2::Header::HasHeader() const
  50. {
  51. return !!(flags & FLAG_HEADER_HAS_HEADER);
  52. }
  53. bool APEv2::Header::HasFooter() const
  54. {
  55. return !(flags & FLAG_HEADER_NO_FOOTER);
  56. }
  57. bool APEv2::Header::IsFooter() const
  58. {
  59. return !(flags & FLAG_HEADER_IS_HEADER);
  60. }
  61. bool APEv2::Header::IsHeader() const
  62. {
  63. return !!(flags & FLAG_HEADER_IS_HEADER);
  64. }
  65. int APEv2::Header::Encode(bytewriter_t byte_writer) const
  66. {
  67. if (bytewriter_size(byte_writer) < 32)
  68. return NErr_Insufficient;
  69. bytewriter_write_n(byte_writer, apev2_preamble, 8);
  70. bytewriter_write_u32_le(byte_writer, version);
  71. bytewriter_write_u32_le(byte_writer, size);
  72. bytewriter_write_u32_le(byte_writer, items);
  73. bytewriter_write_u32_le(byte_writer, flags);
  74. bytewriter_write_zero_n(byte_writer, 8);
  75. return NErr_Success;
  76. }