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}
20
21class getid3_shorten extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		$this->fseek($info['avdataoffset']);
30
31		$ShortenHeader = $this->fread(8);
32		$magic = 'ajkg';
33		if (substr($ShortenHeader, 0, 4) != $magic) {
34			$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"');
35			return false;
36		}
37		$info['fileformat']            = 'shn';
38		$info['audio']['dataformat']   = 'shn';
39		$info['audio']['lossless']     = true;
40		$info['audio']['bitrate_mode'] = 'vbr';
41
42		$info['shn']['version'] = getid3_lib::LittleEndian2Int(substr($ShortenHeader, 4, 1));
43
44		$this->fseek($info['avdataend'] - 12);
45		$SeekTableSignatureTest = $this->fread(12);
46		$info['shn']['seektable']['present'] = substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK';
47		if ($info['shn']['seektable']['present']) {
48			$info['shn']['seektable']['length'] = getid3_lib::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
49			$info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
50			$this->fseek($info['shn']['seektable']['offset']);
51			$SeekTableMagic = $this->fread(4);
52			$magic = 'SEEK';
53			if ($SeekTableMagic != $magic) {
54
55				$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib::PrintHexBytes($SeekTableMagic).'"');
56				return false;
57
58			} else {
59
60				// typedef struct tag_TSeekEntry
61				// {
62				//   unsigned long SampleNumber;
63				//   unsigned long SHNFileByteOffset;
64				//   unsigned long SHNLastBufferReadPosition;
65				//   unsigned short SHNByteGet;
66				//   unsigned short SHNBufferOffset;
67				//   unsigned short SHNFileBitOffset;
68				//   unsigned long SHNGBuffer;
69				//   unsigned short SHNBitShift;
70				//   long CBuf0[3];
71				//   long CBuf1[3];
72				//   long Offset0[4];
73				//   long Offset1[4];
74				// }TSeekEntry;
75
76				$SeekTableData = $this->fread($info['shn']['seektable']['length'] - 16);
77				$info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
78				//$info['shn']['seektable']['entries'] = array();
79				//$SeekTableOffset = 0;
80				//for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
81				//	$SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
82				//	$SeekTableOffset += 4;
83				//	$SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
84				//	$SeekTableOffset += 4;
85				//	$SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
86				//	$SeekTableOffset += 4;
87				//	$SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
88				//	$SeekTableOffset += 2;
89				//	$SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
90				//	$SeekTableOffset += 2;
91				//	$SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
92				//	$SeekTableOffset += 2;
93				//	$SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
94				//	$SeekTableOffset += 4;
95				//	$SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
96				//	$SeekTableOffset += 2;
97				//	for ($j = 0; $j < 3; $j++) {
98				//		$SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
99				//		$SeekTableOffset += 4;
100				//	}
101				//	for ($j = 0; $j < 3; $j++) {
102				//		$SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
103				//		$SeekTableOffset += 4;
104				//	}
105				//	for ($j = 0; $j < 4; $j++) {
106				//		$SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
107				//		$SeekTableOffset += 4;
108				//	}
109				//	for ($j = 0; $j < 4; $j++) {
110				//		$SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
111				//		$SeekTableOffset += 4;
112				//	}
113				//
114				//	$info['shn']['seektable']['entries'][] = $SeekTableEntry;
115				//}
116
117			}
118
119		}
120
121		if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
122			$this->error('PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files');
123			return false;
124		}
125
126		if (GETID3_OS_ISWINDOWS) {
127
128			$RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
129			foreach ($RequiredFiles as $required_file) {
130				if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
131					$this->error(GETID3_HELPERAPPSDIR.$required_file.' does not exist');
132					return false;
133				}
134			}
135			$commandline = GETID3_HELPERAPPSDIR.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR.'head.exe -c 64';
136			$commandline = str_replace('/', '\\', $commandline);
137
138		} else {
139
140			static $shorten_present;
141			if (!isset($shorten_present)) {
142				$shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`;
143			}
144			if (!$shorten_present) {
145				$this->error('shorten binary was not found in path or /usr/local/bin');
146				return false;
147			}
148			$commandline = (file_exists('/usr/local/bin/shorten') ? '/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
149
150		}
151
152		$output = `$commandline`;
153
154		if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
155
156			if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
157				exit;
158			}
159			getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
160
161			$fmt_size = getid3_lib::LittleEndian2Int(substr($output, 16, 4));
162			$DecodedWAVFORMATEX = getid3_riff::parseWAVEFORMATex(substr($output, 20, $fmt_size));
163			$info['audio']['channels']        = $DecodedWAVFORMATEX['channels'];
164			$info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
165			$info['audio']['sample_rate']     = $DecodedWAVFORMATEX['sample_rate'];
166
167			if (substr($output, 20 + $fmt_size, 4) == 'data') {
168
169				$info['playtime_seconds'] = getid3_lib::LittleEndian2Int(substr($output, 20 + 4 + $fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
170
171			} else {
172
173				$this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
174				return false;
175
176			}
177
178			$info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
179
180		} else {
181
182			$this->error('shorten failed to decode file to WAV for parsing');
183			return false;
184
185		}
186
187		return true;
188	}
189
190}
191