1
0

header.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "header.h"
  2. #include "flags.h"
  3. #include "util.h"
  4. #include <limits.h>
  5. static char preamble[] = { 'A', 'P', 'E', 'T', 'A', 'G', 'E', 'X' };
  6. APEv2::Header::Header(void *data)
  7. {
  8. memcpy(&headerData, data, sizeof(headerData));
  9. // covert to host endian
  10. //headerData.preamble=htons(headerData.preamble);
  11. headerData.version=ATON16(headerData.version);
  12. headerData.size=ATON32(headerData.size);
  13. headerData.items=ATON32(headerData.items);
  14. headerData.flags=ATON32(headerData.flags);
  15. }
  16. APEv2::Header::Header(const HeaderData *data)
  17. {
  18. memcpy(&headerData.preamble, preamble, sizeof(headerData.preamble));
  19. headerData.version = NTOA32(2000);
  20. headerData.size = data->size;
  21. headerData.items = data->items;
  22. headerData.flags = data->flags;
  23. headerData.reserved = 0;
  24. }
  25. uint32_t APEv2::Header::GetFlags()
  26. {
  27. return headerData.flags;
  28. }
  29. bool APEv2::Header::Valid()
  30. {
  31. return !memcmp(&headerData.preamble, preamble, 8) && headerData.reserved == 0;
  32. }
  33. uint32_t APEv2::Header::TagSize()
  34. {
  35. size_t size = headerData.size;
  36. if (IsHeader() && HasFooter())
  37. size+=SIZE;
  38. if (IsFooter() && HasHeader())
  39. size+=SIZE;
  40. if (size > ULONG_MAX)
  41. return 0;
  42. return (uint32_t)size;
  43. }
  44. bool APEv2::Header::HasHeader()
  45. {
  46. return !!(headerData.flags & FLAG_HEADER_HAS_HEADER);
  47. }
  48. bool APEv2::Header::HasFooter()
  49. {
  50. return !(headerData.flags & FLAG_HEADER_NO_FOOTER);
  51. }
  52. bool APEv2::Header::IsFooter()
  53. {
  54. return !(headerData.flags & FLAG_HEADER_IS_HEADER);
  55. }
  56. bool APEv2::Header::IsHeader()
  57. {
  58. return !!(headerData.flags & FLAG_HEADER_IS_HEADER);
  59. }
  60. int APEv2::Header::Encode(void *data, size_t len)
  61. {
  62. if (len < 32)
  63. return APEV2_TOO_SMALL;
  64. HeaderData endianCorrectData = headerData;
  65. endianCorrectData.version=NTOA16(endianCorrectData.version);
  66. endianCorrectData.size=NTOA32(endianCorrectData.size);
  67. endianCorrectData.items=NTOA32(endianCorrectData.items);
  68. endianCorrectData.flags=NTOA32(endianCorrectData.flags);
  69. memcpy(data, &endianCorrectData, sizeof(endianCorrectData));
  70. return APEV2_SUCCESS;
  71. }