module.audio.bonk.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.la.php //
  11. // module for analyzing BONK audio files //
  12. // dependencies: module.tag.id3v2.php (optional) //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_bonk extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. // shortcut
  26. $info['bonk'] = array();
  27. $thisfile_bonk = &$info['bonk'];
  28. $thisfile_bonk['dataoffset'] = $info['avdataoffset'];
  29. $thisfile_bonk['dataend'] = $info['avdataend'];
  30. if (!getid3_lib::intValueSupported($thisfile_bonk['dataend'])) {
  31. $this->warning('Unable to parse BONK file from end (v0.6+ preferred method) because PHP filesystem functions only support up to '.round(PHP_INT_MAX / 1073741824).'GB');
  32. } else {
  33. // scan-from-end method, for v0.6 and higher
  34. $this->fseek($thisfile_bonk['dataend'] - 8);
  35. $PossibleBonkTag = $this->fread(8);
  36. while ($this->BonkIsValidTagName(substr($PossibleBonkTag, 4, 4), true)) {
  37. $BonkTagSize = getid3_lib::LittleEndian2Int(substr($PossibleBonkTag, 0, 4));
  38. $this->fseek(0 - $BonkTagSize, SEEK_CUR);
  39. $BonkTagOffset = $this->ftell();
  40. $TagHeaderTest = $this->fread(5);
  41. if (($TagHeaderTest[0] != "\x00") || (substr($PossibleBonkTag, 4, 4) != strtolower(substr($PossibleBonkTag, 4, 4)))) {
  42. $this->error('Expecting "'.getid3_lib::PrintHexBytes("\x00".strtoupper(substr($PossibleBonkTag, 4, 4))).'" at offset '.$BonkTagOffset.', found "'.getid3_lib::PrintHexBytes($TagHeaderTest).'"');
  43. return false;
  44. }
  45. $BonkTagName = substr($TagHeaderTest, 1, 4);
  46. $thisfile_bonk[$BonkTagName]['size'] = $BonkTagSize;
  47. $thisfile_bonk[$BonkTagName]['offset'] = $BonkTagOffset;
  48. $this->HandleBonkTags($BonkTagName);
  49. $NextTagEndOffset = $BonkTagOffset - 8;
  50. if ($NextTagEndOffset < $thisfile_bonk['dataoffset']) {
  51. if (empty($info['audio']['encoder'])) {
  52. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  53. }
  54. return true;
  55. }
  56. $this->fseek($NextTagEndOffset);
  57. $PossibleBonkTag = $this->fread(8);
  58. }
  59. }
  60. // seek-from-beginning method for v0.4 and v0.5
  61. if (empty($thisfile_bonk['BONK'])) {
  62. $this->fseek($thisfile_bonk['dataoffset']);
  63. do {
  64. $TagHeaderTest = $this->fread(5);
  65. switch ($TagHeaderTest) {
  66. case "\x00".'BONK':
  67. if (empty($info['audio']['encoder'])) {
  68. $info['audio']['encoder'] = 'BONK v0.4';
  69. }
  70. break;
  71. case "\x00".'INFO':
  72. $info['audio']['encoder'] = 'Extended BONK v0.5';
  73. break;
  74. default:
  75. break 2;
  76. }
  77. $BonkTagName = substr($TagHeaderTest, 1, 4);
  78. $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
  79. $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
  80. $this->HandleBonkTags($BonkTagName);
  81. } while (true);
  82. }
  83. // parse META block for v0.6 - v0.8
  84. if (empty($thisfile_bonk['INFO']) && isset($thisfile_bonk['META']['tags']['info'])) {
  85. $this->fseek($thisfile_bonk['META']['tags']['info']);
  86. $TagHeaderTest = $this->fread(5);
  87. if ($TagHeaderTest == "\x00".'INFO') {
  88. $info['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
  89. $BonkTagName = substr($TagHeaderTest, 1, 4);
  90. $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
  91. $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
  92. $this->HandleBonkTags($BonkTagName);
  93. }
  94. }
  95. if (empty($info['audio']['encoder'])) {
  96. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  97. }
  98. if (empty($thisfile_bonk['BONK'])) {
  99. unset($info['bonk']);
  100. }
  101. return true;
  102. }
  103. /**
  104. * @param string $BonkTagName
  105. */
  106. public function HandleBonkTags($BonkTagName) {
  107. $info = &$this->getid3->info;
  108. switch ($BonkTagName) {
  109. case 'BONK':
  110. // shortcut
  111. $thisfile_bonk_BONK = &$info['bonk']['BONK'];
  112. $BonkData = "\x00".'BONK'.$this->fread(17);
  113. $thisfile_bonk_BONK['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
  114. $thisfile_bonk_BONK['number_samples'] = getid3_lib::LittleEndian2Int(substr($BonkData, 6, 4));
  115. $thisfile_bonk_BONK['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BonkData, 10, 4));
  116. $thisfile_bonk_BONK['channels'] = getid3_lib::LittleEndian2Int(substr($BonkData, 14, 1));
  117. $thisfile_bonk_BONK['lossless'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 15, 1));
  118. $thisfile_bonk_BONK['joint_stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 16, 1));
  119. $thisfile_bonk_BONK['number_taps'] = getid3_lib::LittleEndian2Int(substr($BonkData, 17, 2));
  120. $thisfile_bonk_BONK['downsampling_ratio'] = getid3_lib::LittleEndian2Int(substr($BonkData, 19, 1));
  121. $thisfile_bonk_BONK['samples_per_packet'] = getid3_lib::LittleEndian2Int(substr($BonkData, 20, 2));
  122. $info['avdataoffset'] = $thisfile_bonk_BONK['offset'] + 5 + 17;
  123. $info['avdataend'] = $thisfile_bonk_BONK['offset'] + $thisfile_bonk_BONK['size'];
  124. $info['fileformat'] = 'bonk';
  125. $info['audio']['dataformat'] = 'bonk';
  126. $info['audio']['bitrate_mode'] = 'vbr'; // assumed
  127. $info['audio']['channels'] = $thisfile_bonk_BONK['channels'];
  128. $info['audio']['sample_rate'] = $thisfile_bonk_BONK['sample_rate'];
  129. $info['audio']['channelmode'] = ($thisfile_bonk_BONK['joint_stereo'] ? 'joint stereo' : 'stereo');
  130. $info['audio']['lossless'] = $thisfile_bonk_BONK['lossless'];
  131. $info['audio']['codec'] = 'bonk';
  132. $info['playtime_seconds'] = $thisfile_bonk_BONK['number_samples'] / ($thisfile_bonk_BONK['sample_rate'] * $thisfile_bonk_BONK['channels']);
  133. if ($info['playtime_seconds'] > 0) {
  134. $info['audio']['bitrate'] = (($info['bonk']['dataend'] - $info['bonk']['dataoffset']) * 8) / $info['playtime_seconds'];
  135. }
  136. break;
  137. case 'INFO':
  138. // shortcut
  139. $thisfile_bonk_INFO = &$info['bonk']['INFO'];
  140. $thisfile_bonk_INFO['version'] = getid3_lib::LittleEndian2Int($this->fread(1));
  141. $thisfile_bonk_INFO['entries_count'] = 0;
  142. $NextInfoDataPair = $this->fread(5);
  143. if (!$this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
  144. while (!feof($this->getid3->fp)) {
  145. //$CurrentSeekInfo['offset'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 0, 4));
  146. //$CurrentSeekInfo['nextbit'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 4, 1));
  147. //$thisfile_bonk_INFO[] = $CurrentSeekInfo;
  148. $NextInfoDataPair = $this->fread(5);
  149. if ($this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
  150. $this->fseek(-5, SEEK_CUR);
  151. break;
  152. }
  153. $thisfile_bonk_INFO['entries_count']++;
  154. }
  155. }
  156. break;
  157. case 'META':
  158. $BonkData = "\x00".'META'.$this->fread($info['bonk']['META']['size'] - 5);
  159. $info['bonk']['META']['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
  160. $MetaTagEntries = floor(((strlen($BonkData) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
  161. $offset = 6;
  162. for ($i = 0; $i < $MetaTagEntries; $i++) {
  163. $MetaEntryTagName = substr($BonkData, $offset, 4);
  164. $offset += 4;
  165. $MetaEntryTagOffset = getid3_lib::LittleEndian2Int(substr($BonkData, $offset, 4));
  166. $offset += 4;
  167. $info['bonk']['META']['tags'][$MetaEntryTagName] = $MetaEntryTagOffset;
  168. }
  169. break;
  170. case ' ID3':
  171. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  172. // ID3v2 checking is optional
  173. if (class_exists('getid3_id3v2')) {
  174. $getid3_temp = new getID3();
  175. $getid3_temp->openfile($this->getid3->filename);
  176. $getid3_id3v2 = new getid3_id3v2($getid3_temp);
  177. $getid3_id3v2->StartingOffset = $info['bonk'][' ID3']['offset'] + 2;
  178. $info['bonk'][' ID3']['valid'] = $getid3_id3v2->Analyze();
  179. if ($info['bonk'][' ID3']['valid']) {
  180. $info['id3v2'] = $getid3_temp->info['id3v2'];
  181. }
  182. unset($getid3_temp, $getid3_id3v2);
  183. }
  184. break;
  185. default:
  186. $this->warning('Unexpected Bonk tag "'.$BonkTagName.'" at offset '.$info['bonk'][$BonkTagName]['offset']);
  187. break;
  188. }
  189. }
  190. /**
  191. * @param string $PossibleBonkTag
  192. * @param bool $ignorecase
  193. *
  194. * @return bool
  195. */
  196. public static function BonkIsValidTagName($PossibleBonkTag, $ignorecase=false) {
  197. static $BonkIsValidTagName = array('BONK', 'INFO', ' ID3', 'META');
  198. foreach ($BonkIsValidTagName as $validtagname) {
  199. if ($validtagname == $PossibleBonkTag) {
  200. return true;
  201. } elseif ($ignorecase && (strtolower($validtagname) == strtolower($PossibleBonkTag))) {
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. }