tag.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include "tag.h"
  2. #include "frameheader.h"
  3. #include "frames.h"
  4. #include "foundation/error.h"
  5. #include <string.h>
  6. #include <new>
  7. #include "nsid3v2.h" // for serialize flags
  8. /* === ID3v2 common === */
  9. static bool IdentifierMatch(const int8_t *id1, const int8_t *id2)
  10. {
  11. return !memcmp(id1, id2, 4);
  12. }
  13. ID3v2::Tag::Tag(const ID3v2::Header &_header) : Header(_header)
  14. {
  15. }
  16. void ID3v2::Tag::RemoveFrame(ID3v2::Frame *frame)
  17. {
  18. frames.erase(frame);
  19. delete frame;
  20. }
  21. ID3v2::Frame *ID3v2::Tag::FindFirstFrame(const int8_t *id) const
  22. {
  23. if (!id)
  24. return 0;
  25. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  26. {
  27. if (IdentifierMatch(itr->GetIdentifier(), id))
  28. return *itr;
  29. }
  30. return 0;
  31. }
  32. ID3v2::Frame *ID3v2::Tag::FindNextFrame(const Frame *frame) const
  33. {
  34. FrameList::const_iterator itr=frame;
  35. for (itr++;itr != frames.end();itr++)
  36. {
  37. if (IdentifierMatch(itr->GetIdentifier(), frame->GetIdentifier()))
  38. return *itr;
  39. }
  40. return 0;
  41. }
  42. void ID3v2::Tag::RemoveFrames(const int8_t *id)
  43. {
  44. // TODO: not exactly the fastest way
  45. Frame *frame;
  46. while (frame = FindFirstFrame(id))
  47. frames.erase(frame);
  48. }
  49. void ID3v2::Tag::AddFrame(ID3v2::Frame *frame)
  50. {
  51. frames.push_back(frame);
  52. }
  53. ID3v2::Frame *ID3v2::Tag::EnumerateFrame(const ID3v2::Frame *position) const
  54. {
  55. if (!position)
  56. return frames.front();
  57. else
  58. return (ID3v2::Frame *)position->next;
  59. }
  60. /* === ID3v2.2 === */
  61. ID3v2_2::Tag::Tag(const ID3v2::Header &_header) : ID3v2::Tag(_header), extendedHeader(_header)
  62. {
  63. }
  64. ID3v2_2::Tag::~Tag()
  65. {
  66. frames.deleteAll();
  67. }
  68. static inline void Advance(const void *&data, size_t &len, size_t amount)
  69. {
  70. data = (const uint8_t *)data + amount;
  71. len -= amount;
  72. }
  73. int ID3v2_2::Tag::Parse(const void *data, size_t len)
  74. {
  75. /* Is there an extended header? */
  76. if (Header::HasExtendedHeader())
  77. {
  78. size_t read=0;
  79. if (extendedHeader.Parse(data, len, &read) != 0)
  80. {
  81. return 1;
  82. }
  83. Advance(data, len, read);
  84. }
  85. /* Read each frame */
  86. while (len >= FrameHeader::SIZE)
  87. {
  88. /* if next byte is zero, we've hit the padding area, GTFO */
  89. if (*(uint8_t *)data == 0x0)
  90. break;
  91. /* Read frame header first */
  92. FrameHeader frame_header(*this, data);
  93. Advance(data, len, FrameHeader::SIZE);
  94. if (!frame_header.IsValid())
  95. return 1;
  96. /* read frame data */
  97. Frame *new_frame = new (std::nothrow) Frame(frame_header);
  98. if (!new_frame)
  99. return NErr_OutOfMemory;
  100. size_t read=0;
  101. if (new_frame->Parse(data, len, &read) == 0)
  102. {
  103. Advance(data, len, read);
  104. frames.push_back(new_frame);
  105. }
  106. else
  107. {
  108. delete new_frame;
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114. int ID3v2_2::Tag::SerializedSize(uint32_t *length, uint32_t padding_size, int flags) const
  115. {
  116. size_t current_length=0;
  117. Header new_header(*this);
  118. /* TODO: going to clear this, for now */
  119. new_header.ClearExtendedHeader();
  120. switch(flags & Serialize_UnsynchronizeMask)
  121. {
  122. case Serialize_Unsynchronize:
  123. // TODO:
  124. break;
  125. case Serialize_NoUnsynchronize:
  126. new_header.ClearUnsynchronized();
  127. break;
  128. }
  129. current_length += Header::SIZE;
  130. if (new_header.HasExtendedHeader())
  131. {
  132. // TODO: deal with extended header
  133. }
  134. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  135. {
  136. uint32_t written;
  137. const ID3v2_2::Frame *frame = (const ID3v2_2::Frame *)*itr;
  138. int ret = frame->SerializedSize(&written, new_header, flags);
  139. if (ret != NErr_Success)
  140. return ret;
  141. current_length += written;
  142. }
  143. switch(flags & SerializedSize_PaddingMask)
  144. {
  145. case SerializedSize_Padding:
  146. current_length += padding_size;
  147. break;
  148. case SerializedSize_AbsoluteSize:
  149. if (current_length < padding_size)
  150. current_length = padding_size;
  151. break;
  152. case SerializedSize_BlockSize:
  153. {
  154. uint32_t additional = current_length % padding_size;
  155. current_length += additional;
  156. }
  157. break;
  158. }
  159. *length = current_length;
  160. return NErr_Success;
  161. }
  162. int ID3v2_2::Tag::Serialize(void *data, uint32_t len, int flags) const
  163. {
  164. uint8_t *data_itr = (uint8_t *)data;
  165. uint32_t current_length=0;
  166. // write header. note the passed-in length is guaranteed to be correct, as it was generated by SerializedSize
  167. Header new_header(this, len-10);
  168. /* TODO: going to clear this, for now */
  169. new_header.ClearExtendedHeader();
  170. switch(flags & Serialize_UnsynchronizeMask)
  171. {
  172. case Serialize_Unsynchronize:
  173. // TODO:
  174. break;
  175. case Serialize_NoUnsynchronize:
  176. new_header.ClearUnsynchronized();
  177. break;
  178. }
  179. new_header.Serialize(data);
  180. current_length += Header::SIZE;
  181. data_itr += Header::SIZE;
  182. if (new_header.HasExtendedHeader())
  183. {
  184. // TODO: write extended header
  185. }
  186. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  187. {
  188. uint32_t written;
  189. const ID3v2_2::Frame *frame = (const ID3v2_2::Frame *)*itr;
  190. int ret = frame->Serialize((void *)data_itr, &written, new_header, flags);
  191. if (ret != NErr_Success)
  192. return ret;
  193. current_length += written;
  194. data_itr += written;
  195. }
  196. // write padding
  197. memset(data_itr, 0, len-current_length);
  198. return NErr_Success;
  199. }
  200. static bool IdentifierMatch3(const int8_t *id1, const int8_t *id2)
  201. {
  202. return !memcmp(id1, id2, 3);
  203. }
  204. ID3v2_2::Frame *ID3v2_2::Tag::FindFirstFrame(int frame_id) const
  205. {
  206. if (!ValidFrameID(frame_id))
  207. return 0;
  208. return (ID3v2_2::Frame *)ID3v2::Tag::FindFirstFrame(frame_ids[frame_id].v2);
  209. };
  210. void ID3v2_2::Tag::RemoveFrames(int frame_id)
  211. {
  212. // TODO: not exactly the fastest way
  213. Frame *frame;
  214. while (frame = FindFirstFrame(frame_id))
  215. frames.erase(frame);
  216. }
  217. ID3v2_2::Frame *ID3v2_2::Tag::NewFrame(int frame_id, int flags) const
  218. {
  219. if (!ValidFrameID(frame_id) || !frame_ids[frame_id].v2)
  220. return 0;
  221. return new (std::nothrow) ID3v2_2::Frame(*this, frame_ids[frame_id].v2, flags);
  222. }
  223. /* === ID3v2.3 === */
  224. ID3v2_3::Tag::Tag(const ID3v2::Header &_header) : ID3v2::Tag(_header), extendedHeader(_header)
  225. {
  226. }
  227. ID3v2_3::Tag::~Tag()
  228. {
  229. frames.deleteAll();
  230. }
  231. int ID3v2_3::Tag::Parse(const void *data, size_t len)
  232. {
  233. /* Is there an extended header? */
  234. if (Header::HasExtendedHeader())
  235. {
  236. size_t read=0;
  237. if (extendedHeader.Parse(data, len, &read) != 0)
  238. {
  239. return 1;
  240. }
  241. Advance(data, len, read);
  242. }
  243. /* Read each frame */
  244. while (len >= FrameHeader::SIZE)
  245. {
  246. /* if next byte is zero, we've hit the padding area, GTFO */
  247. if (*(uint8_t *)data == 0x0)
  248. break;
  249. /* Read frame header first */
  250. FrameHeader frame_header(*this, data);
  251. Advance(data, len, FrameHeader::SIZE);
  252. if (!frame_header.IsValid())
  253. return 1;
  254. /* read frame data */
  255. Frame *new_frame = new (std::nothrow) Frame(frame_header);
  256. if (!new_frame)
  257. return NErr_OutOfMemory;
  258. size_t read=0;
  259. if (new_frame->Parse(data, len, &read) == 0)
  260. {
  261. Advance(data, len, read);
  262. frames.push_back(new_frame);
  263. }
  264. else
  265. {
  266. delete new_frame;
  267. return 1;
  268. }
  269. }
  270. return 0;
  271. }
  272. int ID3v2_3::Tag::SerializedSize(uint32_t *length, uint32_t padding_size, int flags) const
  273. {
  274. size_t current_length=0;
  275. Header new_header(*this);
  276. /* TODO: going to clear this, for now */
  277. new_header.ClearExtendedHeader();
  278. switch(flags & Serialize_UnsynchronizeMask)
  279. {
  280. case Serialize_Unsynchronize:
  281. // TODO:
  282. break;
  283. case Serialize_NoUnsynchronize:
  284. new_header.ClearUnsynchronized();
  285. break;
  286. }
  287. current_length += Header::SIZE;
  288. if (new_header.HasExtendedHeader())
  289. {
  290. // TODO: deal with extended header
  291. }
  292. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  293. {
  294. uint32_t written;
  295. const ID3v2_3::Frame *frame = (const ID3v2_3::Frame *)*itr;
  296. int ret = frame->SerializedSize(&written, new_header, flags);
  297. if (ret != NErr_Success)
  298. return ret;
  299. current_length += written;
  300. }
  301. switch(flags & SerializedSize_PaddingMask)
  302. {
  303. case SerializedSize_Padding:
  304. current_length += padding_size;
  305. break;
  306. case SerializedSize_AbsoluteSize:
  307. if (current_length < padding_size)
  308. current_length = padding_size;
  309. break;
  310. case SerializedSize_BlockSize:
  311. {
  312. uint32_t additional = padding_size - (current_length % padding_size);
  313. if (additional == padding_size)
  314. additional=0;
  315. current_length += additional;
  316. }
  317. break;
  318. }
  319. *length = current_length;
  320. return NErr_Success;
  321. }
  322. int ID3v2_3::Tag::Serialize(void *data, uint32_t len, int flags) const
  323. {
  324. uint8_t *data_itr = (uint8_t *)data;
  325. uint32_t current_length=0;
  326. // write header. note the passed-in length is guaranteed to be correct, as it was generated by SerializedSize
  327. Header new_header(this, len-10);
  328. /* TODO: going to clear this, for now */
  329. new_header.ClearExtendedHeader();
  330. switch(flags & Serialize_UnsynchronizeMask)
  331. {
  332. case Serialize_Unsynchronize:
  333. // TODO:
  334. break;
  335. case Serialize_NoUnsynchronize:
  336. new_header.ClearUnsynchronized();
  337. break;
  338. }
  339. new_header.Serialize(data);
  340. current_length += Header::SIZE;
  341. data_itr += Header::SIZE;
  342. if (new_header.HasExtendedHeader())
  343. {
  344. // TODO: write extended header
  345. }
  346. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  347. {
  348. uint32_t written;
  349. const ID3v2_3::Frame *frame = (const ID3v2_3::Frame *)*itr;
  350. int ret = frame->Serialize((void *)data_itr, &written, new_header, flags);
  351. if (ret != NErr_Success)
  352. return ret;
  353. current_length += written;
  354. data_itr += written;
  355. }
  356. // write padding
  357. memset(data_itr, 0, len-current_length);
  358. return NErr_Success;
  359. }
  360. ID3v2_3::Frame *ID3v2_3::Tag::FindFirstFrame(int frame_id) const
  361. {
  362. if (!ValidFrameID(frame_id))
  363. return 0;
  364. return (ID3v2_3::Frame *)ID3v2::Tag::FindFirstFrame(frame_ids[frame_id].v3);
  365. };
  366. void ID3v2_3::Tag::RemoveFrames(int frame_id)
  367. {
  368. // TODO: not exactly the fastest way
  369. Frame *frame;
  370. while (frame = FindFirstFrame(frame_id))
  371. frames.erase(frame);
  372. }
  373. ID3v2_3::Frame *ID3v2_3::Tag::NewFrame(int frame_id, int flags) const
  374. {
  375. if (!ValidFrameID(frame_id) || !frame_ids[frame_id].v3)
  376. return 0;
  377. return new (std::nothrow) ID3v2_3::Frame(*this, frame_ids[frame_id].v3, flags);
  378. }
  379. /* === ID3v2.4 === */
  380. ID3v2_4::Tag::Tag(const ID3v2::Header &_header) : ID3v2::Tag(_header), extendedHeader(_header)
  381. {
  382. }
  383. ID3v2_4::Tag::~Tag()
  384. {
  385. frames.deleteAll();
  386. }
  387. int ID3v2_4::Tag::Parse(const void *data, size_t len)
  388. {
  389. /* Is there an extended header? */
  390. if (Header::HasExtendedHeader())
  391. {
  392. size_t read=0;
  393. if (extendedHeader.Parse(data, len, &read) != 0)
  394. {
  395. return 1;
  396. }
  397. Advance(data, len, read);
  398. }
  399. /* Read each frame */
  400. while (len >= FrameHeader::SIZE)
  401. {
  402. /* if next byte is zero, we've hit the padding area, GTFO */
  403. if (*(uint8_t *)data == 0x0)
  404. break;
  405. /* Read frame header first */
  406. FrameHeader frame_header(*this, data);
  407. Advance(data, len, FrameHeader::SIZE);
  408. if (!frame_header.IsValid())
  409. return 1;
  410. /* read frame data */
  411. Frame *new_frame = new (std::nothrow) Frame(frame_header);
  412. if (!new_frame)
  413. return NErr_OutOfMemory;
  414. size_t read=0;
  415. if (new_frame->Parse(data, len, &read) == 0)
  416. {
  417. Advance(data, len, read);
  418. frames.push_back(new_frame);
  419. }
  420. else
  421. {
  422. delete new_frame;
  423. return 1;
  424. }
  425. }
  426. return 0;
  427. }
  428. int ID3v2_4::Tag::SerializedSize(uint32_t *length, uint32_t padding_size, int flags) const
  429. {
  430. size_t current_length=0;
  431. Header new_header(*this);
  432. /* TODO: going to clear this, for now */
  433. new_header.ClearExtendedHeader();
  434. switch(flags & Serialize_UnsynchronizeMask)
  435. {
  436. case Serialize_Unsynchronize:
  437. // TODO:
  438. break;
  439. case Serialize_NoUnsynchronize:
  440. new_header.ClearUnsynchronized();
  441. break;
  442. }
  443. current_length += Header::SIZE;
  444. if (new_header.HasExtendedHeader())
  445. {
  446. // TODO: deal with extended header
  447. }
  448. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  449. {
  450. uint32_t written;
  451. const ID3v2_4::Frame *frame = (const ID3v2_4::Frame *)*itr;
  452. int ret = frame->SerializedSize(&written, new_header, flags);
  453. if (ret != NErr_Success)
  454. return ret;
  455. current_length += written;
  456. }
  457. switch(flags & SerializedSize_PaddingMask)
  458. {
  459. case 0:
  460. /* we can only write a footer if there is no padding */
  461. if (new_header.FooterValid() || new_header.HasFooter())
  462. {
  463. current_length += Header::SIZE;
  464. }
  465. break;
  466. case SerializedSize_Padding:
  467. current_length += padding_size;
  468. break;
  469. case SerializedSize_AbsoluteSize:
  470. if (current_length < padding_size)
  471. current_length = padding_size;
  472. break;
  473. case SerializedSize_BlockSize:
  474. {
  475. uint32_t additional = current_length % padding_size;
  476. current_length += additional;
  477. }
  478. break;
  479. }
  480. *length = current_length;
  481. return NErr_Success;
  482. }
  483. int ID3v2_4::Tag::Serialize(void *data, uint32_t len, int flags) const
  484. {
  485. uint8_t *data_itr = (uint8_t *)data;
  486. uint32_t current_length=0;
  487. // write header. note the passed-in length is guaranteed to be correct, as it was generated by SerializedSize
  488. bool write_footer=false;
  489. if ((flags & SerializedSize_PaddingMask) == 0 && (FooterValid() || HasFooter()))
  490. write_footer=true;
  491. Header new_header(this, write_footer?(len-20):(len-10));
  492. new_header.SetFooter(write_footer);
  493. /* TODO: going to clear this, for now */
  494. new_header.ClearExtendedHeader();
  495. switch(flags & Serialize_UnsynchronizeMask)
  496. {
  497. case Serialize_Unsynchronize:
  498. // TODO:
  499. break;
  500. case Serialize_NoUnsynchronize:
  501. new_header.ClearUnsynchronized();
  502. break;
  503. }
  504. new_header.SerializeAsHeader(data);
  505. current_length += Header::SIZE;
  506. data_itr += Header::SIZE;
  507. if (new_header.HasExtendedHeader())
  508. {
  509. // TODO: write extended header
  510. }
  511. for (FrameList::const_iterator itr=frames.begin();itr != frames.end();itr++)
  512. {
  513. uint32_t written;
  514. const ID3v2_4::Frame *frame = (const ID3v2_4::Frame *)*itr;
  515. int ret = frame->Serialize((void *)data_itr, &written, new_header, flags);
  516. if (ret != NErr_Success)
  517. return ret;
  518. current_length += written;
  519. data_itr += written;
  520. }
  521. if (write_footer)
  522. {
  523. new_header.SerializeAsFooter(data_itr);
  524. current_length += Header::SIZE;
  525. data_itr += Header::SIZE;
  526. }
  527. // write padding
  528. memset(data_itr, 0, len-current_length);
  529. return NErr_Success;
  530. }
  531. ID3v2_4::Frame *ID3v2_4::Tag::FindFirstFrame(int frame_id) const
  532. {
  533. if (!ValidFrameID(frame_id))
  534. return 0;
  535. return (ID3v2_4::Frame *)ID3v2::Tag::FindFirstFrame(frame_ids[frame_id].v4);
  536. };
  537. void ID3v2_4::Tag::RemoveFrames(int frame_id)
  538. {
  539. // TODO: not exactly the fastest way
  540. Frame *frame;
  541. while (frame = FindFirstFrame(frame_id))
  542. frames.erase(frame);
  543. }
  544. ID3v2_4::Frame *ID3v2_4::Tag::NewFrame(int frame_id, int flags) const
  545. {
  546. if (!ValidFrameID(frame_id) || !frame_ids[frame_id].v4)
  547. return 0;
  548. return new (std::nothrow) ID3v2_4::Frame(*this, frame_ids[frame_id].v4, flags);
  549. }