1
0

frame_comments.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "nsid3v2/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 <strsafe.h>
  7. struct ParsedComments
  8. {
  9. const char *language;
  10. uint8_t description_encoding; // 0 - iso-8859-1, 1 - UTF16LE, 2 - UTF16BE, 3 - UTF8
  11. union
  12. {
  13. const char *as8;
  14. const wchar_t *as16;
  15. } description_data;
  16. size_t description_cch;
  17. uint8_t value_encoding; // 0 - iso-8859-1, 1 - UTF16LE, 2 - UTF16BE, 3 - UTF8
  18. union
  19. {
  20. const char *as8;
  21. const wchar_t *as16;
  22. } value_data;
  23. size_t value_cch;
  24. };
  25. static int ParseComments(const void *data, size_t data_len, ParsedComments &parsed)
  26. {
  27. int ret;
  28. if (data_len < 5)
  29. return NErr_Error;
  30. const uint8_t *data8 = (const uint8_t *)data;
  31. switch(data8[0])
  32. {
  33. case 0: // ISO-8859-1
  34. parsed.description_encoding = 0;
  35. parsed.language = (const char *)&data8[1];
  36. parsed.value_encoding = 0;
  37. parsed.description_data.as8 = (const char *)&data8[4];
  38. data_len-=4;
  39. ret = ParseDescription(parsed.description_data.as8, data_len, parsed.description_cch);
  40. if (ret != NErr_Success)
  41. return ret;
  42. parsed.value_data.as8 = parsed.description_data.as8 + 2 + parsed.description_cch;
  43. parsed.value_cch = data_len;
  44. return NErr_Success;
  45. case 1: // UTF-16
  46. parsed.language = (const char *)&data8[1];
  47. parsed.description_encoding=1;
  48. parsed.description_data.as16 = (const wchar_t *)&data8[4];
  49. data_len-=4;
  50. ret = ParseDescription(parsed.description_data.as16, data_len, parsed.description_cch, parsed.description_encoding);
  51. if (ret != NErr_Success)
  52. return ret;
  53. parsed.value_data.as16 = parsed.description_data.as16 + 2 + parsed.description_cch;
  54. parsed.value_cch = data_len/2;
  55. if (parsed.value_cch && parsed.value_data.as16[0] == 0xFFFE)
  56. {
  57. parsed.value_encoding=2;
  58. parsed.value_data.as16++;
  59. parsed.value_cch--;
  60. }
  61. else if (parsed.value_cch && parsed.value_data.as16[0] == 0xFEFF)
  62. {
  63. parsed.value_encoding=1;
  64. parsed.value_data.as16++;
  65. parsed.value_cch--;
  66. }
  67. else
  68. {
  69. parsed.value_encoding=1;
  70. }
  71. return NErr_Success;
  72. case 2: // UTF-16 BE
  73. parsed.language = (const char *)&data8[1];
  74. parsed.description_encoding=2;
  75. parsed.description_data.as16 = (const wchar_t *)&data8[4];
  76. data_len-=3;
  77. ret = ParseDescription(parsed.description_data.as16, data_len, parsed.description_cch, parsed.description_encoding);
  78. if (ret != NErr_Success)
  79. return ret;
  80. parsed.value_data.as16 = parsed.description_data.as16 + 2 + parsed.description_cch;
  81. parsed.value_cch = data_len/2;
  82. parsed.value_encoding=2;
  83. return NErr_Success;
  84. case 3: // UTF-8
  85. parsed.description_encoding = 3;
  86. parsed.language = (const char *)&data8[1];
  87. parsed.value_encoding = 3;
  88. parsed.description_data.as8 = (const char *)&data8[4];
  89. data_len-=4;
  90. ret = ParseDescription(parsed.description_data.as8, data_len, parsed.description_cch);
  91. if (ret != NErr_Success)
  92. return ret;
  93. // check for UTF-8 BOM
  94. if (parsed.description_cch >= 3 && parsed.description_data.as8[0] == 0xEF && parsed.description_data.as8[1] == 0xBB && parsed.description_data.as8[2] == 0xBF)
  95. {
  96. parsed.description_data.as8+=3;
  97. parsed.description_cch-=3;
  98. }
  99. if (!data_len)
  100. return NErr_Error;
  101. parsed.value_data.as8 = parsed.description_data.as8 + 2 + parsed.description_cch;
  102. parsed.value_cch = data_len;
  103. // check for UTF-8 BOM
  104. if (parsed.value_cch >= 3 && parsed.value_data.as8[0] == 0xEF && parsed.value_data.as8[1] == 0xBB && parsed.value_data.as8[2] == 0xBF)
  105. {
  106. parsed.value_data.as8+=3;
  107. parsed.value_cch-=3;
  108. }
  109. return NErr_Success;
  110. }
  111. return NErr_NotImplemented;
  112. }
  113. int NSID3v2_Tag_Comments_GetUTF16(const nsid3v2_tag_t t, const wchar_t *description, wchar_t *buf, size_t buf_cch, int text_flags)
  114. {
  115. const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
  116. const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_COMMENTS);
  117. while (frame)
  118. {
  119. const void *data;
  120. size_t data_len;
  121. ParsedComments parsed;
  122. if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success)
  123. {
  124. // see if our description matches
  125. switch(parsed.description_encoding)
  126. {
  127. case 0: // ISO-8859-1
  128. {
  129. UINT codepage = (text_flags & NSID3V2_TEXT_SYSTEM)?28591:CP_ACP;
  130. if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, AutoWide(parsed.description_data.as8, codepage), -1, description, -1) != CSTR_EQUAL)
  131. goto next_frame;
  132. }
  133. break;
  134. case 1:
  135. if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, parsed.description_data.as16, -1, description, -1) != CSTR_EQUAL)
  136. goto next_frame;
  137. break;
  138. case 2:
  139. // TODO!
  140. goto next_frame;
  141. break;
  142. case 3:
  143. if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, AutoWide(parsed.description_data.as8, CP_UTF8), -1, description, -1) != CSTR_EQUAL)
  144. goto next_frame;
  145. break;
  146. }
  147. switch(parsed.value_encoding)
  148. {
  149. case 0: // ISO-8859-1
  150. {
  151. UINT codepage = (text_flags & NSID3V2_TEXT_SYSTEM)?28591:CP_ACP;
  152. int utf16_len = MultiByteToWideChar(codepage, 0, parsed.value_data.as8, parsed.value_cch, 0, 0);
  153. if (utf16_len)
  154. {
  155. utf16_len = MultiByteToWideChar(codepage, 0, parsed.value_data.as8, parsed.value_cch, buf, utf16_len-1);
  156. buf[utf16_len]=0;
  157. }
  158. else
  159. {
  160. buf[0]=0;
  161. }
  162. }
  163. return NErr_Success;
  164. case 1: // UTF-16
  165. StringCchCopyNW(buf, buf_cch, parsed.value_data.as16, parsed.value_cch);
  166. return NErr_Success;
  167. case 2: // UTF-16BE
  168. {
  169. size_t toCopy = buf_cch-1;
  170. if (parsed.value_cch < toCopy)
  171. toCopy = parsed.value_cch;
  172. for (size_t i=0;i<toCopy;i++)
  173. {
  174. buf[i] = ((parsed.value_data.as16[i] >> 8) & 0xFF) | (((parsed.value_data.as16[i]) & 0xFF) << 8);
  175. }
  176. buf[toCopy]=0;
  177. }
  178. return NErr_Success;
  179. case 3: // UTF-8
  180. {
  181. int utf16_len = MultiByteToWideChar(CP_UTF8, 0, parsed.value_data.as8, parsed.value_cch, 0, 0);
  182. if (utf16_len)
  183. {
  184. utf16_len = MultiByteToWideChar(CP_UTF8, 0, parsed.value_data.as8, parsed.value_cch, buf, utf16_len-1);
  185. buf[utf16_len]=0;
  186. }
  187. else
  188. {
  189. buf[0]=0;
  190. }
  191. }
  192. return NErr_Success;
  193. }
  194. next_frame:
  195. frame = tag->FindNextFrame(frame);
  196. }
  197. }
  198. return NErr_Error;
  199. }