1
0

frame_utils.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "frame_utils.h"
  2. #include "foundation/error.h"
  3. #include "nsid3v2/nsid3v2.h"
  4. #if defined(_WIN32) && !defined(strcasecmp)
  5. #define strcasecmp _stricmp
  6. #else
  7. #include <string.h>
  8. #endif
  9. int ParseDescription(const char *&str, size_t &data_len, size_t &str_cch)
  10. {
  11. str_cch=0;
  12. while (data_len && str[str_cch])
  13. {
  14. data_len--;
  15. str_cch++;
  16. }
  17. if (!data_len)
  18. return NErr_Error;
  19. data_len--;
  20. return NErr_Success;
  21. }
  22. int ParseDescription(const wchar_t *&str, size_t &data_len, size_t &str_cch, uint8_t &str_encoding)
  23. {
  24. str_cch=0;
  25. if (data_len > 2 && str[0] == 0xFFFE)
  26. {
  27. str_encoding=2;
  28. str++;
  29. str-=3;
  30. }
  31. else if (data_len > 2 && str[0] == 0xFEFF)
  32. {
  33. str_encoding=1;
  34. str++;
  35. data_len-=3;
  36. }
  37. else
  38. {
  39. data_len--;
  40. }
  41. while (data_len > 1 && str[str_cch])
  42. {
  43. data_len-=2;
  44. str_cch++;
  45. }
  46. if (!data_len)
  47. return NErr_Error;
  48. data_len-=2;
  49. return NErr_Success;
  50. }
  51. static void ParseBOM(bytereader_t reader, uint8_t *encoding, const uint8_t default_encoding)
  52. {
  53. if (bytereader_size(reader) >= 2)
  54. {
  55. uint16_t bom = bytereader_show_u16_le(reader);
  56. if (bom == 0xFFFE)
  57. {
  58. bytereader_advance(reader, 2);
  59. *encoding=2;
  60. }
  61. else if (bom == 0xFEFF)
  62. {
  63. bytereader_advance(reader, 2);
  64. *encoding=1;
  65. }
  66. else
  67. {
  68. *encoding=default_encoding;
  69. }
  70. }
  71. else
  72. {
  73. *encoding=default_encoding;
  74. }
  75. }
  76. int ParseNullTerminatedString(bytereader_t reader, uint8_t encoding, ParsedString &parsed)
  77. {
  78. switch(encoding)
  79. {
  80. case 0: // ISO-8859-1
  81. if (bytereader_size(reader) == 0)
  82. return NErr_Insufficient;
  83. parsed.encoding = 0;
  84. parsed.data = bytereader_pointer(reader);
  85. parsed.byte_length = 0;
  86. while (bytereader_size(reader) && bytereader_read_u8(reader))
  87. parsed.byte_length++;
  88. return NErr_Success;
  89. case 1: // UTF-16
  90. if (bytereader_size(reader) < 2)
  91. return NErr_Insufficient;
  92. parsed.byte_length = 0;
  93. ParseBOM(reader, &parsed.encoding, 1);
  94. parsed.data = bytereader_pointer(reader);
  95. while (bytereader_size(reader) && bytereader_read_u16_le(reader))
  96. parsed.byte_length+=2;
  97. return NErr_Success;
  98. case 2: // UTF-16BE
  99. if (bytereader_size(reader) < 2)
  100. return NErr_Insufficient;
  101. parsed.byte_length = 0;
  102. ParseBOM(reader, &parsed.encoding, 2);
  103. parsed.data = bytereader_pointer(reader);
  104. while (bytereader_size(reader) && bytereader_read_u16_le(reader))
  105. parsed.byte_length+=2;
  106. return NErr_Success;
  107. case 3: // UTF-8
  108. if (bytereader_size(reader) == 0)
  109. return NErr_Insufficient;
  110. parsed.encoding = 3;
  111. parsed.data = bytereader_pointer(reader);
  112. parsed.byte_length = 0;
  113. size_t start = bytereader_size(reader);
  114. #if 0 // TODO
  115. /* check for UTF-8 BOM and skip it */
  116. if (bytereader_size(reader) > 3 && bytereader_read_u8(reader) == 0xEF && bytereader_read_u8(reader) == 0xBB && bytereader_read_u8(reader) == 0xBF)
  117. {
  118. parsed.data = bytereader_pointer(reader);
  119. parsed.byte_length = bytereader_size(reader);
  120. }
  121. else
  122. {
  123. /* no BOM but skip however far we read into the string */
  124. size_t offset = start - bytereader_size(reader);
  125. parsed.data = (const uint8_t *)parsed.data + offset;
  126. parsed.byte_length -= offset;
  127. }
  128. #endif
  129. /* finish it up */
  130. while (bytereader_size(reader) && bytereader_read_u8(reader))
  131. parsed.byte_length++;
  132. return NErr_Success;
  133. }
  134. return NErr_Unknown;
  135. }
  136. int ParseFrameTerminatedString(bytereader_t reader, uint8_t encoding, ParsedString &parsed)
  137. {
  138. switch(encoding)
  139. {
  140. case 0: // ISO-8859-1
  141. parsed.encoding = 0;
  142. parsed.data = bytereader_pointer(reader);
  143. parsed.byte_length = bytereader_size(reader);
  144. return NErr_Success;
  145. case 1: // UTF-16
  146. if ((bytereader_size(reader) & 1) == 1)
  147. return NErr_Error;
  148. ParseBOM(reader, &parsed.encoding, 1);
  149. parsed.data = bytereader_pointer(reader);
  150. parsed.byte_length = bytereader_size(reader);
  151. return NErr_Success;
  152. case 2: // UTF-16BE
  153. if ((bytereader_size(reader) & 1) == 1)
  154. return NErr_Error;
  155. ParseBOM(reader, &parsed.encoding, 2);
  156. parsed.data = bytereader_pointer(reader);
  157. parsed.byte_length = bytereader_size(reader);
  158. return NErr_Success;
  159. case 3: // UTF-8
  160. parsed.encoding = 3;
  161. parsed.data = bytereader_pointer(reader);
  162. parsed.byte_length = bytereader_size(reader);
  163. if (bytereader_size(reader) > 3 && bytereader_read_u8(reader) == 0xEF && bytereader_read_u8(reader) == 0xBB && bytereader_read_u8(reader) == 0xBF)
  164. {
  165. parsed.data = bytereader_pointer(reader);
  166. parsed.byte_length = bytereader_size(reader);
  167. }
  168. return NErr_Success;
  169. }
  170. return NErr_Error;
  171. }
  172. int NXStringCreateFromParsedString(nx_string_t *value, ParsedString &parsed, int text_flags)
  173. {
  174. switch(parsed.encoding)
  175. {
  176. case 0: // ISO-8859-1
  177. if (parsed.byte_length == 0)
  178. return NXStringCreateEmpty(value);
  179. if (text_flags & NSID3V2_TEXT_SYSTEM)
  180. return NXStringCreateWithBytes(value, parsed.data, parsed.byte_length, nx_charset_system);
  181. else
  182. return NXStringCreateWithBytes(value, parsed.data, parsed.byte_length, nx_charset_latin1);
  183. case 1: // UTF-16
  184. if (parsed.byte_length < 2)
  185. return NXStringCreateEmpty(value);
  186. return NXStringCreateWithBytes(value, parsed.data, parsed.byte_length, nx_charset_utf16le);
  187. case 2: // UTF-16BE
  188. if (parsed.byte_length < 2)
  189. return NXStringCreateEmpty(value);
  190. return NXStringCreateWithBytes(value, parsed.data, parsed.byte_length, nx_charset_utf16be);
  191. case 3: // UTF-8
  192. if (parsed.byte_length == 0)
  193. return NXStringCreateEmpty(value);
  194. return NXStringCreateWithBytes(value, parsed.data, parsed.byte_length, nx_charset_utf8);
  195. default:
  196. return NErr_Unknown;
  197. }
  198. }
  199. bool DescriptionMatches(const ParsedString &parsed, const char *description, int text_flags)
  200. {
  201. // see if our description matches
  202. switch(parsed.encoding)
  203. {
  204. case 0: // ISO-8859-1
  205. return !strcasecmp(description, (const char *)parsed.data);
  206. case 1:
  207. {
  208. bytereader_value_t utf16;
  209. bytereader_init(&utf16, parsed.data, parsed.byte_length);
  210. while (*description && bytereader_size(&utf16))
  211. {
  212. if ((*description++ & ~0x20) != (bytereader_read_u16_le(&utf16) & ~0x20))
  213. return false;
  214. }
  215. if (*description == 0 && bytereader_size(&utf16) == 0)
  216. return true;
  217. else
  218. return false;
  219. }
  220. case 2:
  221. {
  222. bytereader_value_t utf16;
  223. bytereader_init(&utf16, parsed.data, parsed.byte_length);
  224. while (*description && bytereader_size(&utf16))
  225. {
  226. if ((*description++ & ~0x20) != (bytereader_read_u16_be(&utf16) & ~0x20))
  227. return false;
  228. }
  229. if (*description == 0 && bytereader_size(&utf16) == 0)
  230. return true;
  231. else
  232. return false;
  233. }
  234. case 3:
  235. return !strcasecmp(description, (const char *)parsed.data);
  236. }
  237. return false;
  238. }