1<?php
2
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/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.audio.shorten.php                                    //
12// module for analyzing Shorten Audio files                    //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20class getid3_shorten extends getid3_handler
21{
22	/**
23	 * @return bool
24	 */
25	public function Analyze() {
26		$info = &$this->getid3->info;
27
28		$this->fseek($info['avdataoffset']);
29
30		$ShortenHeader = $this->fread(8);
31		$magic = 'ajkg';
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)).'"');
34			return false;
35		}
36		$info['fileformat']            = 'shn';
37		$info['audio']['dataformat']   = 'shn';
38		$info['audio']['lossless']     = true;
39		$info['audio']['bitrate_mode'] = 'vbr';
40
41		$info['shn']['version'] = getid3_lib::LittleEndian2Int(substr($ShortenHeader, 4, 1));
42
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);
51			$magic = 'SEEK';
52			if ($SeekTableMagic != $magic) {
53
54				$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib::PrintHexBytes($SeekTableMagic).'"');
55				return false;
56
57			} else {
58
59				// typedef struct tag_TSeekEntry
60				// {
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;
69				//   long CBuf0[3];
70				//   long CBuf1[3];
71				//   long Offset0[4];
72				//   long Offset1[4];
73				// }TSeekEntry;
74
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;
99				//	}
100				//	for ($j = 0; $j < 3; $j++) {
101				//		$SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
102				//		$SeekTableOffset += 4;
103				//	}
104				//	for ($j = 0; $j < 4; $j++) {
105				//		$SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
106				//		$SeekTableOffset += 4;
107				//	}
108				//	for ($j = 0; $j < 4; $j++) {
109				//		$SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
110				//		$SeekTableOffset += 4;
111				//	}
112				//
113				//	$info['shn']['seektable']['entries'][] = $SeekTableEntry;
114				//}
115
116			}
117
118		}
119
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');
122			return false;
123		}
124
125		if (GETID3_OS_ISWINDOWS) {
126
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');
131					return false;
132				}
133			}
134			$commandline = GETID3_HELPERAPPSDIR.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR.'head.exe -c 64';
135			$commandline = str_replace('/', '\\', $commandline);
136
137		} else {
138
139			static $shorten_present;
140			if (!isset($shorten_present)) {
141				$shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`;
142			}
143			if (!$shorten_present) {
144				$this->error('shorten binary was not found in path or /usr/local/bin');
145				return false;
146			}
147			$commandline = (file_exists('/usr/local/bin/shorten') ? '/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
148
149		}
150
151		$output = `$commandline`;
152
153		if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
154
155			if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
156				exit;
157			}
158			getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
159
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'];
165
166			if (substr($output, 20 + $fmt_size, 4) == 'data') {
167
168				$info['playtime_seconds'] = getid3_lib::LittleEndian2Int(substr($output, 20 + 4 + $fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
169
170			} else {
171
172				$this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
173				return false;
174
175			}
176
177			$info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
178
179		} else {
180
181			$this->error('shorten failed to decode file to WAV for parsing');
182			return false;
183
184		}
185
186		return true;
187	}
188
189}
190