1
0

frame_comments.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "nsid3v2.h"
  2. #include "nsid3v2/header.h"
  3. #include "nsid3v2/tag.h"
  4. #include "nsid3v2/frame_utils.h"
  5. #include "nu/AutoWide.h"
  6. #include "nx/nxstring.h"
  7. #include "nu/ByteWriter.h"
  8. struct ParsedComments
  9. {
  10. char language[3];
  11. ParsedString description;
  12. ParsedString value;
  13. };
  14. static int ParseComments(const void *data, size_t data_len, ParsedComments &parsed)
  15. {
  16. int ret;
  17. if (data_len < 5)
  18. return NErr_Insufficient;
  19. bytereader_value_t byte_reader;
  20. bytereader_init(&byte_reader, data, data_len);
  21. // Get encoding
  22. uint8_t encoding = bytereader_read_u8(&byte_reader);
  23. // Get language
  24. for (int i = 0; i < 3; i++)
  25. parsed.language[i] = bytereader_read_u8(&byte_reader);
  26. // Get description
  27. ret = ParseNullTerminatedString(&byte_reader, encoding, parsed.description);
  28. if (ret != NErr_Success)
  29. return ret;
  30. // Get actual text value
  31. ret = ParseFrameTerminatedString(&byte_reader, encoding, parsed.value);
  32. return ret;
  33. }
  34. int NSID3v2_Tag_Comments_Find(const nsid3v2_tag_t t, const char *description, nsid3v2_frame_t *out_frame, int text_flags)
  35. {
  36. const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
  37. if (!tag)
  38. return NErr_Empty;
  39. const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_COMMENTS);
  40. while (frame)
  41. {
  42. const void *data;
  43. size_t data_len;
  44. ParsedComments parsed;
  45. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success && (!description || DescriptionMatches(parsed.description, description, text_flags)))
  46. {
  47. *out_frame = (nsid3v2_frame_t)frame;
  48. return NErr_Success;
  49. }
  50. frame = tag->FindNextFrame(frame);
  51. }
  52. return NErr_Empty;
  53. }
  54. int NSID3v2_Tag_Comments_Get(const nsid3v2_tag_t t, const char *description, char language[3], nx_string_t *value, int text_flags)
  55. {
  56. const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
  57. if (!tag)
  58. return NErr_Empty;
  59. const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_COMMENTS);
  60. while (frame)
  61. {
  62. const void *data;
  63. size_t data_len;
  64. ParsedComments parsed;
  65. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success && (!description || DescriptionMatches(parsed.description, description, text_flags)))
  66. {
  67. if (language)
  68. memcpy(language, parsed.language, 3);
  69. return NXStringCreateFromParsedString(value, parsed.value, text_flags);
  70. }
  71. frame = tag->FindNextFrame(frame);
  72. }
  73. return NErr_Empty;
  74. }
  75. int NSID3v2_Frame_Comments_Get(const nsid3v2_frame_t f, nx_string_t *description, char language[3], nx_string_t *value, int text_flags)
  76. {
  77. const ID3v2::Frame *frame = (const ID3v2::Frame *)f;
  78. if (frame)
  79. {
  80. const void *data;
  81. size_t data_len;
  82. ParsedComments parsed;
  83. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success)
  84. {
  85. if (language)
  86. memcpy(language, parsed.language, 3);
  87. int ret = NXStringCreateFromParsedString(value, parsed.value, text_flags);
  88. if (ret != NErr_Success)
  89. return ret;
  90. if (description)
  91. return NXStringCreateFromParsedString(description, parsed.description, text_flags);
  92. else
  93. return NErr_Success;
  94. }
  95. }
  96. return NErr_Error;
  97. }
  98. /* ---------------- Setters ---------------- */
  99. int NSID3v2_Frame_Comments_Set(nsid3v2_frame_t f, const char *description, const char language[3], nx_string_t value, int text_flags)
  100. {
  101. ID3v2::Frame *frame = (ID3v2::Frame *)f;
  102. if (frame)
  103. {
  104. /* benski> for now, we're going to store UTF-16LE always. in the future, we'll add functions to NXString to determine a 'best' encoding */
  105. size_t description_length=description?strlen(description):0;
  106. size_t byte_count_value=0;
  107. int ret = NXStringGetBytesSize(&byte_count_value, value, nx_charset_utf16le, 0);
  108. if (ret != NErr_DirectPointer && ret != NErr_Success)
  109. return ret;
  110. /* TODO: overflow check */
  111. size_t total_size = 1 /* encoding */ + 3 /* language */ + 2 /* BOM for description */ + description_length*2 + 2 /* null separator */ + 2 /* BOM for value */ + byte_count_value;
  112. void *data;
  113. size_t data_len;
  114. ret = frame->NewData(total_size, &data, &data_len);
  115. if (ret != NErr_Success)
  116. return ret;
  117. size_t bytes_copied;
  118. bytewriter_s byte_writer;
  119. bytewriter_init(&byte_writer, data, data_len);
  120. bytewriter_write_u8(&byte_writer, 1); /* mark as UTF-16LE */
  121. if (language)
  122. bytewriter_write_n(&byte_writer, language, 3);
  123. else
  124. bytewriter_write_zero_n(&byte_writer, 3);
  125. bytewriter_write_u16_le(&byte_writer, 0xFEFF); /* BOM for description */
  126. for (size_t i=0;i<description_length;i++)
  127. bytewriter_write_u16_le(&byte_writer, description[i]);
  128. bytewriter_write_u16_le(&byte_writer, 0); /* NULL separator*/
  129. bytewriter_write_u16_le(&byte_writer, 0xFEFF); /* BOM for value */
  130. NXStringGetBytes(&bytes_copied, value, bytewriter_pointer(&byte_writer), bytewriter_size(&byte_writer), nx_charset_utf16le, 0);
  131. return NErr_Success;
  132. }
  133. return NErr_Error;
  134. }
  135. int NSID3v2_Tag_Comments_Set(nsid3v2_tag_t t, const char *description, const char language[3], nx_string_t value, int text_flags)
  136. {
  137. ID3v2::Tag *tag = (ID3v2::Tag *)t;
  138. if (!tag)
  139. return NErr_Empty;
  140. ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_COMMENTS);
  141. while (frame)
  142. {
  143. const void *data;
  144. size_t data_len;
  145. ParsedComments parsed;
  146. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success && (!description || DescriptionMatches(parsed.description, description, text_flags)))
  147. {
  148. break;
  149. }
  150. frame = tag->FindNextFrame(frame);
  151. }
  152. if (!frame)
  153. {
  154. frame = tag->NewFrame(NSID3V2_FRAME_COMMENTS, 0);
  155. if (!frame)
  156. return NErr_OutOfMemory;
  157. tag->AddFrame(frame);
  158. }
  159. return NSID3v2_Frame_Comments_Set((nsid3v2_frame_t)frame, description, language, value, text_flags);
  160. }