Lyrics3.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <windows.h>
  2. #include "Lyrics3.h"
  3. #include "config.h"
  4. #include <strsafe.h>
  5. // http://www.id3.org/Lyrics3v2
  6. Lyrics3::Lyrics3()
  7. {
  8. artist=0;
  9. title=0;
  10. album=0;
  11. hasData=false;
  12. dirty=false;
  13. }
  14. Lyrics3::~Lyrics3()
  15. {
  16. free(artist);
  17. free(title);
  18. free(album);
  19. }
  20. static wchar_t *CopyField(const uint8_t *buffer, uint32_t size)
  21. {
  22. int converted = MultiByteToWideChar(28591, 0,(LPCSTR)buffer, size, 0, 0);
  23. wchar_t *str = (wchar_t *)calloc((converted+1), sizeof(wchar_t));
  24. if (str)
  25. {
  26. converted = MultiByteToWideChar(28591, 0, (LPCSTR)buffer, size, str, converted);
  27. str[converted]=0;
  28. }
  29. return str;
  30. }
  31. int Lyrics3::Decode(const void *data, size_t datalen)
  32. {
  33. if (!config_parse_lyrics3)
  34. return 1;
  35. if (memcmp(data, "LYRICSBEGIN", 11) == 0)
  36. {
  37. hasData = true;
  38. datalen-=11;
  39. uint8_t *buffer = (uint8_t *)data+11;
  40. while (datalen > 8)
  41. {
  42. uint8_t fid[4] = {0};
  43. uint8_t sizeT[6] = {0};
  44. uint32_t size;
  45. fid[3] = 0;
  46. sizeT[5] = 0;
  47. memcpy(fid, buffer, 3);
  48. buffer+=3; datalen-=3;
  49. memcpy(sizeT, buffer, 5);
  50. buffer+=5; datalen-=5;
  51. size = strtoul((char *)sizeT, 0, 10);
  52. if (datalen >= size)
  53. {
  54. /*if ( memcmp(fid, "IND", 3) == 0) // the IND field
  55. {
  56. if ( buff2[ posn + 8 + 1 ] == '1')
  57. stampsUsed = true;
  58. }
  59. else */
  60. if (memcmp(fid, "ETT", 3) == 0) // the TITLE field
  61. {
  62. title = CopyField(buffer, size);
  63. }
  64. else if (strcmp((char *) fid, "EAR") == 0) // the ARTIST field
  65. {
  66. artist = CopyField(buffer, size);
  67. }
  68. else if (strcmp((char *) fid, "EAL") == 0) // the ALBUM field
  69. {
  70. album = CopyField(buffer, size);
  71. }
  72. /*else if ( strcmp((char *) fid, "LYR") == 0) // the LYRICS field
  73. {
  74. char *text;
  75. luint newSize;
  76. newSize = ID3_CRLFtoLF((char *) & buff2[ posn + 8 ], size);
  77. if ( stampsUsed)
  78. newSize = ID3_StripTimeStamps((char *) & buff2[ posn + 8 ], newSize);
  79. if ( text = (char*)malloc(newSize + 1))
  80. {
  81. text[ newSize ] = 0;
  82. memcpy( text, &buff2[ posn + 8 ], newSize);
  83. ID3_AddLyrics( this, text);
  84. free(text);
  85. }
  86. else
  87. ID3_THROW( ID3E_NoMemory);
  88. }*/
  89. datalen-=size;
  90. buffer+=size;
  91. }
  92. else
  93. break;
  94. }
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. int Lyrics3::GetString(const char *tag, wchar_t *data, int dataLen)
  100. {
  101. if (!hasData)
  102. return 0;
  103. if (!_stricmp(tag, "title"))
  104. {
  105. if (title && *title)
  106. {
  107. StringCchCopyW(data, dataLen, title);
  108. return 1;
  109. }
  110. return -1;
  111. }
  112. else if (!_stricmp(tag, "artist"))
  113. {
  114. if (artist && *artist)
  115. {
  116. StringCchCopyW(data, dataLen, artist);
  117. return 1;
  118. }
  119. return -1;
  120. }
  121. else if (!_stricmp(tag, "album"))
  122. {
  123. if (album && *album)
  124. {
  125. StringCchCopyW(data, dataLen, album);
  126. return 1;
  127. }
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. int Lyrics3::SetString(const char *tag, const wchar_t *data)
  133. {
  134. int ret=0;
  135. if (!_stricmp(tag, "title"))
  136. {
  137. if (title) free(title);
  138. title = _wcsdup(data);
  139. ret = 1;
  140. }
  141. else if (!_stricmp(tag, "artist"))
  142. {
  143. if ( artist ) free(artist);
  144. artist = _wcsdup(data);
  145. ret = 1;
  146. }
  147. else if (!_stricmp(tag, "album"))
  148. {
  149. if ( album ) free(album);
  150. album = _wcsdup(data);
  151. ret = 1;
  152. }
  153. if(ret)
  154. {
  155. hasData=true;
  156. }
  157. return ret;
  158. }
  159. void Lyrics3::Clear()
  160. {
  161. free(artist); artist=0;
  162. free(album); album=0;
  163. free(title); title=0;
  164. dirty=true;
  165. hasData=false;
  166. }