frame_apic.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "nsid3v2.h"
  2. #include "nsid3v2/header.h"
  3. #include "nsid3v2/tag.h"
  4. #include "nsid3v2/frame_utils.h"
  5. #include "nu/ByteReader.h"
  6. #include "nu/ByteWriter.h"
  7. #include "nx/nxstring.h"
  8. struct ParsedPicture
  9. {
  10. ParsedString mime;
  11. uint8_t picture_type;
  12. ParsedString description;
  13. const void *picture_data;
  14. size_t picture_byte_length;
  15. };
  16. static int ParsePicture(const void *data, size_t data_len, ParsedPicture &parsed)
  17. {
  18. int ret;
  19. if (data_len < 4)
  20. return NErr_NeedMoreData;
  21. bytereader_value_t byte_reader;
  22. bytereader_init(&byte_reader, data, data_len);
  23. uint8_t encoding = bytereader_read_u8(&byte_reader);
  24. /* mime type is always latin-1 */
  25. ret = ParseNullTerminatedString(&byte_reader, 0, parsed.mime);
  26. if (ret != NErr_Success)
  27. return ret;
  28. if (bytereader_size(&byte_reader) < 2)
  29. return NErr_NeedMoreData;
  30. parsed.picture_type = bytereader_read_u8(&byte_reader);
  31. ret = ParseNullTerminatedString(&byte_reader, encoding, parsed.description);
  32. if (ret != NErr_Success)
  33. return ret;
  34. parsed.picture_data = bytereader_pointer(&byte_reader);
  35. parsed.picture_byte_length = bytereader_size(&byte_reader);
  36. return NErr_Success;
  37. }
  38. static int ParsePicturev2_2(const void *data, size_t data_len, ParsedPicture &parsed)
  39. {
  40. int ret;
  41. if (data_len < 6)
  42. return NErr_NeedMoreData;
  43. bytereader_value_t byte_reader;
  44. bytereader_init(&byte_reader, data, data_len);
  45. uint8_t encoding = bytereader_read_u8(&byte_reader);
  46. /* three byte "Image Format" field */
  47. parsed.mime.encoding = 0;
  48. parsed.mime.data = bytereader_pointer(&byte_reader);
  49. parsed.mime.byte_length = 3;
  50. bytereader_advance(&byte_reader, 3);
  51. parsed.picture_type = bytereader_read_u8(&byte_reader);
  52. ret = ParseNullTerminatedString(&byte_reader, encoding, parsed.description);
  53. if (ret != NErr_Success)
  54. return ret;
  55. parsed.picture_data = bytereader_pointer(&byte_reader);
  56. parsed.picture_byte_length = bytereader_size(&byte_reader);
  57. return NErr_Success;
  58. }
  59. int NSID3v2_Frame_Picture_Get(const nsid3v2_frame_t f, nx_string_t *mime, uint8_t *picture_type, nx_string_t *description, const void **picture_data, size_t *length, int text_flags)
  60. {
  61. const ID3v2::Frame *frame = (const ID3v2::Frame *)f;
  62. if (frame)
  63. {
  64. const void *data;
  65. size_t data_len;
  66. ParsedPicture parsed;
  67. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0)
  68. {
  69. int ret;
  70. if (frame->GetVersion() == 2)
  71. ret = ParsePicturev2_2(data, data_len, parsed);
  72. else
  73. ret = ParsePicture(data, data_len, parsed);
  74. if (ret == NErr_Success)
  75. {
  76. int ret;
  77. if (mime)
  78. {
  79. ret = NXStringCreateFromParsedString(mime, parsed.mime, text_flags);
  80. if (ret != NErr_Success)
  81. return ret;
  82. }
  83. if (description)
  84. {
  85. ret = NXStringCreateFromParsedString(description, parsed.description, text_flags);
  86. if (ret != NErr_Success)
  87. return ret;
  88. }
  89. if (picture_type)
  90. *picture_type = parsed.picture_type;
  91. if (picture_data)
  92. *picture_data = parsed.picture_data;
  93. if (length)
  94. *length = parsed.picture_byte_length;
  95. return NErr_Success;
  96. }
  97. else
  98. {
  99. return ret;
  100. }
  101. }
  102. }
  103. return NErr_Empty;
  104. }
  105. /* ---------------- Setters ---------------- */
  106. static const char *GetMIME2_2(nx_string_t mime)
  107. {
  108. if (!mime)
  109. return "\0\0\0";
  110. if (NXStringKeywordCompareWithCString(mime, "image/jpeg") == NErr_True || NXStringKeywordCompareWithCString(mime, "image/jpg") == NErr_True)
  111. return "JPG";
  112. if (NXStringKeywordCompareWithCString(mime, "image/png") == NErr_True)
  113. return "PNG";
  114. if (NXStringKeywordCompareWithCString(mime, "image/gif") == NErr_True)
  115. return "GIF";
  116. if (NXStringKeywordCompareWithCString(mime, "image/bmp") == NErr_True)
  117. return "BMP";
  118. return "\0\0\0";
  119. }
  120. int NSID3v2_Frame_Picture_Set(nsid3v2_frame_t f, nx_string_t mime, uint8_t picture_type, nx_string_t description, const void *picture_data, size_t length, int text_flags)
  121. {
  122. ID3v2::Frame *frame = (ID3v2::Frame *)f;
  123. if (frame)
  124. {
  125. if (frame->GetVersion() == 2)
  126. {
  127. /* first, we need to get the total encoded size */
  128. size_t byte_count_description=0;
  129. if (description)
  130. {
  131. int ret = NXStringGetBytesSize(&byte_count_description, description, nx_charset_latin1, 0);
  132. if (ret != NErr_DirectPointer && ret != NErr_Success)
  133. return ret;
  134. }
  135. size_t total_size = 1 /* text encoding */
  136. + 3 /* Image Format is 3 bytes in ID3v2.2*/
  137. + 1 /* picture type */
  138. + byte_count_description + 1 /* description + null terminator */
  139. + length; /* picture length */
  140. void *data;
  141. size_t data_size;
  142. int ret = frame->NewData(total_size, &data, &data_size);
  143. if (ret != NErr_Success)
  144. return ret;
  145. size_t bytes_copied;
  146. bytewriter_s byte_writer;
  147. bytewriter_init(&byte_writer, data, data_size);
  148. bytewriter_write_u8(&byte_writer, 0); /* mark as Latin-1 */
  149. bytewriter_write_n(&byte_writer, GetMIME2_2(mime), 3);
  150. bytewriter_write_u8(&byte_writer, picture_type);
  151. if (description)
  152. {
  153. NXStringGetBytes(&bytes_copied, description, bytewriter_pointer(&byte_writer), bytewriter_size(&byte_writer), nx_charset_latin1, 0);
  154. bytewriter_advance(&byte_writer, bytes_copied);
  155. }
  156. bytewriter_write_u8(&byte_writer, 0); /* description null terminator */
  157. bytewriter_write_n(&byte_writer, picture_data, length);
  158. return NErr_Success;
  159. }
  160. else
  161. {
  162. /* first, we need to get the total encoded size */
  163. size_t byte_count_mime=0;
  164. if (mime)
  165. {
  166. int ret = NXStringGetBytesSize(&byte_count_mime, mime, nx_charset_latin1, 0);
  167. if (ret != NErr_DirectPointer && ret != NErr_Success)
  168. return ret;
  169. }
  170. size_t byte_count_description=0;
  171. if (description)
  172. {
  173. int ret = NXStringGetBytesSize(&byte_count_description, description, nx_charset_latin1, 0);
  174. if (ret != NErr_DirectPointer && ret != NErr_Success)
  175. return ret;
  176. }
  177. size_t total_size = 1 /* text encoding */
  178. + byte_count_mime + 1 /* mime + null terminator */
  179. + 1 /* picture type */
  180. + byte_count_description + 1 /* description + null terminator */
  181. + length; /* picture length */
  182. void *data;
  183. size_t data_size;
  184. int ret = frame->NewData(total_size, &data, &data_size);
  185. if (ret != NErr_Success)
  186. return ret;
  187. size_t bytes_copied;
  188. bytewriter_s byte_writer;
  189. bytewriter_init(&byte_writer, data, data_size);
  190. bytewriter_write_u8(&byte_writer, 0); /* mark as Latin-1 */
  191. if (mime)
  192. {
  193. NXStringGetBytes(&bytes_copied, mime, bytewriter_pointer(&byte_writer), bytewriter_size(&byte_writer), nx_charset_latin1, 0);
  194. bytewriter_advance(&byte_writer, bytes_copied);
  195. }
  196. bytewriter_write_u8(&byte_writer, 0); /* MIME null terminator */
  197. bytewriter_write_u8(&byte_writer, picture_type);
  198. if (description)
  199. {
  200. NXStringGetBytes(&bytes_copied, description, bytewriter_pointer(&byte_writer), bytewriter_size(&byte_writer), nx_charset_latin1, 0);
  201. bytewriter_advance(&byte_writer, bytes_copied);
  202. }
  203. bytewriter_write_u8(&byte_writer, 0); /* description null terminator */
  204. bytewriter_write_n(&byte_writer, picture_data, length);
  205. return NErr_Success;
  206. }
  207. }
  208. return NErr_Empty;
  209. }