/thirdparty/liblastfm2/src/types/mbid_mp3.c

http://github.com/tomahawk-player/tomahawk · C · 181 lines · 108 code · 24 blank · 49 comment · 35 complexity · 902a728888d2eb10a8464a6ee3f76301 MD5 · raw file

  1. /*
  2. * LICENSE
  3. *
  4. * Copyright (c) 2006, David Nicolson
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the author nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT UNLESS REQUIRED BY
  24. * LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER OR CONTRIBUTOR
  25. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  26. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  27. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  30. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  31. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef __MBID_MP3_H
  34. #define __MBID_MP3_H
  35. #define MBID_BUFFER_SIZE 37
  36. // -----------------------------------------------------------------------------
  37. void mfile(size_t length, char ret[], FILE *fp, int *s) {
  38. size_t bytes = fread(ret,1,length,fp);
  39. if (bytes != length) {
  40. *s = 0;
  41. }
  42. }
  43. // -----------------------------------------------------------------------------
  44. int to_synch_safe(char bytes[]) {
  45. return ((int)bytes[0] << 21) + ((int)bytes[1] << 14) + ((int)bytes[2] << 7) + (int)bytes[3];
  46. }
  47. int to_integer(char bytes[]) {
  48. size_t size = 0;
  49. uint i;
  50. for (i=0; i < sizeof(bytes); i++) {
  51. size = size * 256 + ((int)bytes[i] & 0x000000FF);
  52. }
  53. return static_cast<int>(size);
  54. }
  55. // -----------------------------------------------------------------------------
  56. int getMP3_MBID(const char *path, char mbid[MBID_BUFFER_SIZE])
  57. {
  58. FILE *fp;
  59. static int s = 1;
  60. char head[3];
  61. char version[2];
  62. char flag[1];
  63. char size[4];
  64. char size_extended[4];
  65. int tag_size = 0;
  66. int extended_size = 0;
  67. char frame[4];
  68. char frame_header[4];
  69. int frame_size;
  70. int version_major, version_minor;
  71. if (path == NULL) {
  72. //debug("Received null path\n");
  73. return -1;
  74. }
  75. fp = fopen(path,"rb");
  76. if (fp == NULL) {
  77. //debug("Failed to open music file: %s\n",path);
  78. return -1;
  79. }
  80. while (s) {
  81. mfile(3,head,fp,&s);
  82. if (!strncmp(head,"ID3",3) == 0) {
  83. //debug("No ID3v2 tag found: %s\n",path);
  84. break;
  85. }
  86. mfile(2,version,fp,&s);
  87. version_major = (int)version[0];
  88. version_minor = (int)version[1];
  89. if (version_major == 2) {
  90. //debug("ID3v2.2.0 does not support MBIDs: %s\n",path);
  91. break;
  92. }
  93. if (version_major != 3 && version_major != 4) {
  94. //debug("Unsupported ID3 version: v2.%d.%d\n",version_major,version_minor);
  95. break;
  96. }
  97. mfile(1,flag,fp,&s);
  98. if ((unsigned int)flag[0] & 0x00000040) {
  99. //debug("Extended header found\n");
  100. if (version[0] == 4) {
  101. mfile(4,size_extended,fp,&s);
  102. extended_size = to_synch_safe(size_extended);
  103. } else {
  104. mfile(4,size_extended,fp,&s);
  105. extended_size = to_integer(size_extended);
  106. }
  107. //debug("Extended header size: %d\n",extended_size);
  108. fseek(fp,extended_size,SEEK_CUR);
  109. }
  110. mfile(4,size,fp,&s);
  111. tag_size = to_synch_safe(size);
  112. //debug("Tag size: %d\n",tag_size);
  113. while (s) {
  114. if (ftell(fp) > tag_size || ftell(fp) > 1048576) {
  115. break;
  116. }
  117. mfile(4,frame,fp,&s);
  118. if (frame[0] == 0x00) {
  119. break;
  120. }
  121. if (version_major == 4) {
  122. mfile(4,frame_header,fp,&s);
  123. frame_size = to_synch_safe(frame_header);
  124. } else {
  125. mfile(4,frame_header,fp,&s);
  126. frame_size = to_integer(frame_header);
  127. }
  128. fseek(fp,2,SEEK_CUR);
  129. //debug("Reading %d bytes from frame %s\n",frame_size,frame);
  130. if (strncmp(frame,"UFID",4) == 0) {
  131. //char frame_data[frame_size];
  132. char frame_data[59];
  133. mfile(59,frame_data,fp,&s);
  134. if (frame_size >= 59 && strncmp(frame_data,"http://musicbrainz.org",22) == 0) {
  135. char *tmbid = frame_data;
  136. tmbid = frame_data + 23;
  137. strncpy(mbid,tmbid,MBID_BUFFER_SIZE-1);
  138. mbid[MBID_BUFFER_SIZE-1] = 0x00;
  139. fclose(fp);
  140. return 0;
  141. }
  142. } else {
  143. fseek(fp,frame_size,SEEK_CUR);
  144. }
  145. }
  146. break;
  147. }
  148. if (fp) {
  149. fclose(fp);
  150. }
  151. //if (!s) {
  152. // debug("Failed to read music file: %s\n",path);
  153. //}
  154. return -1;
  155. }
  156. #endif
  157. // -----------------------------------------------------------------------------