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.archive.hpk.php                                      //
12// module for analyzing HPK 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_hpk extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		$info['fileformat'] = 'hpk';
30
31		$this->fseek($info['avdataoffset']);
32		$HPKheader = $this->fread(36);
33
34		if (substr($HPKheader, 0, 4) == 'BPUL') {
35
36			$info['hpk']['header']['signature']                    =                              substr($HPKheader,  0, 4);
37			$info['hpk']['header']['data_offset']                  = getid3_lib::LittleEndian2Int(substr($HPKheader,  4, 4));
38			$info['hpk']['header']['fragments_per_file']           = getid3_lib::LittleEndian2Int(substr($HPKheader,  8, 4));
39			//$info['hpk']['header']['unknown1']                     = getid3_lib::LittleEndian2Int(substr($HPKheader, 12, 4));
40			$info['hpk']['header']['fragments_residual_offset']    = getid3_lib::LittleEndian2Int(substr($HPKheader, 16, 4));
41			$info['hpk']['header']['fragments_residual_count']     = getid3_lib::LittleEndian2Int(substr($HPKheader, 20, 4));
42			//$info['hpk']['header']['unknown2']                     = getid3_lib::LittleEndian2Int(substr($HPKheader, 24, 4));
43			$info['hpk']['header']['fragmented_filesystem_offset'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 28, 4));
44			$info['hpk']['header']['fragmented_filesystem_length'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 32, 4));
45
46			$info['hpk']['header']['filesystem_entries'] = $info['hpk']['header']['fragmented_filesystem_length'] / ($info['hpk']['header']['fragments_per_file'] * 8);
47			$this->fseek($info['hpk']['header']['fragmented_filesystem_offset']);
48			for ($i = 0; $i < $info['hpk']['header']['filesystem_entries']; $i++) {
49				$offset = getid3_lib::LittleEndian2Int($this->fread(4));
50				$length = getid3_lib::LittleEndian2Int($this->fread(4));
51				$info['hpk']['filesystem'][$i] = array('offset' => $offset, 'length' => $length);
52			}
53
54$this->error('HPK parsing incomplete (and mostly broken) in this version of getID3() ['.$this->getid3->version().']');
55
56/*
57			$filename = '';
58			$dirs = array();
59			foreach ($info['hpk']['filesystem'] as $key => $filesystemdata) {
60				$this->fseek($filesystemdata['offset']);
61				$first4 = $this->fread(4);
62				if (($first4 == 'LZ4 ') || ($first4 == 'ZLIB')) {
63					// actual data, ignore
64					$info['hpk']['toc'][$key] = array(
65						'filename' => ltrim(implode('/', $dirs).'/'.$filename, '/'),
66						'offset'   => $filesystemdata['offset'],
67						'length'   => $filesystemdata['length'],
68					);
69					$filename = '';
70					$dirs = array();
71				} else {
72					$fragment_index = getid3_lib::LittleEndian2Int($first4);
73					$fragment_type  = getid3_lib::LittleEndian2Int($this->fread(4)); // file = 0, directory = 1
74					$name_length    = getid3_lib::LittleEndian2Int($this->fread(2));
75					if ($fragment_type == 1) {
76						$dirs[]   = $this->fread($name_length);
77					} else {
78						$filename = $this->fread($name_length);
79					}
80				}
81			}
82*/
83
84		} else {
85			$this->error('Expecting "BPUL" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($HPKheader, 0, 4)).'"');
86			return false;
87		}
88
89		return true;
90	}
91
92}
93