3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.shorten.php //
12 // module for analyzing Shorten Audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
20 class getid3_shorten
extends getid3_handler
25 public function Analyze() {
26 $info = &$this->getid3
->info
;
28 $this->fseek($info['avdataoffset']);
30 $ShortenHeader = $this->fread(8);
32 if (substr($ShortenHeader, 0, 4) != $magic) {
33 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"');
36 $info['fileformat'] = 'shn';
37 $info['audio']['dataformat'] = 'shn';
38 $info['audio']['lossless'] = true;
39 $info['audio']['bitrate_mode'] = 'vbr';
41 $info['shn']['version'] = getid3_lib
::LittleEndian2Int(substr($ShortenHeader, 4, 1));
43 $this->fseek($info['avdataend'] - 12);
44 $SeekTableSignatureTest = $this->fread(12);
45 $info['shn']['seektable']['present'] = substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK';
46 if ($info['shn']['seektable']['present']) {
47 $info['shn']['seektable']['length'] = getid3_lib
::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
48 $info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
49 $this->fseek($info['shn']['seektable']['offset']);
50 $SeekTableMagic = $this->fread(4);
52 if ($SeekTableMagic != $magic) {
54 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib
::PrintHexBytes($SeekTableMagic).'"');
59 // typedef struct tag_TSeekEntry
61 // unsigned long SampleNumber;
62 // unsigned long SHNFileByteOffset;
63 // unsigned long SHNLastBufferReadPosition;
64 // unsigned short SHNByteGet;
65 // unsigned short SHNBufferOffset;
66 // unsigned short SHNFileBitOffset;
67 // unsigned long SHNGBuffer;
68 // unsigned short SHNBitShift;
75 $SeekTableData = $this->fread($info['shn']['seektable']['length'] - 16);
76 $info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
77 //$info['shn']['seektable']['entries'] = array();
78 //$SeekTableOffset = 0;
79 //for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
80 // $SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
81 // $SeekTableOffset += 4;
82 // $SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
83 // $SeekTableOffset += 4;
84 // $SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
85 // $SeekTableOffset += 4;
86 // $SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
87 // $SeekTableOffset += 2;
88 // $SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
89 // $SeekTableOffset += 2;
90 // $SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
91 // $SeekTableOffset += 2;
92 // $SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
93 // $SeekTableOffset += 4;
94 // $SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
95 // $SeekTableOffset += 2;
96 // for ($j = 0; $j < 3; $j++) {
97 // $SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
98 // $SeekTableOffset += 4;
100 // for ($j = 0; $j < 3; $j++) {
101 // $SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
102 // $SeekTableOffset += 4;
104 // for ($j = 0; $j < 4; $j++) {
105 // $SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
106 // $SeekTableOffset += 4;
108 // for ($j = 0; $j < 4; $j++) {
109 // $SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
110 // $SeekTableOffset += 4;
113 // $info['shn']['seektable']['entries'][] = $SeekTableEntry;
120 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
121 $this->error('PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files');
125 if (GETID3_OS_ISWINDOWS
) {
127 $RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
128 foreach ($RequiredFiles as $required_file) {
129 if (!is_readable(GETID3_HELPERAPPSDIR
.$required_file)) {
130 $this->error(GETID3_HELPERAPPSDIR
.$required_file.' does not exist');
134 $commandline = GETID3_HELPERAPPSDIR
.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR
.'head.exe -c 64';
135 $commandline = str_replace('/', '\\', $commandline);
139 static $shorten_present;
140 if (!isset($shorten_present)) {
141 $shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`
;
143 if (!$shorten_present) {
144 $this->error('shorten binary was not found in path or /usr/local/bin');
147 $commandline = (file_exists('/usr/local/bin/shorten') ?
'/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
151 $output = `
$commandline`
;
153 if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
155 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
158 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
160 $fmt_size = getid3_lib
::LittleEndian2Int(substr($output, 16, 4));
161 $DecodedWAVFORMATEX = getid3_riff
::parseWAVEFORMATex(substr($output, 20, $fmt_size));
162 $info['audio']['channels'] = $DecodedWAVFORMATEX['channels'];
163 $info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
164 $info['audio']['sample_rate'] = $DecodedWAVFORMATEX['sample_rate'];
166 if (substr($output, 20 +
$fmt_size, 4) == 'data') {
168 $info['playtime_seconds'] = getid3_lib
::LittleEndian2Int(substr($output, 20 +
4 +
$fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
172 $this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
177 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
181 $this->error('shorten failed to decode file to WAV for parsing');