aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/MOBIClass/PalmRecord.php
blob: d0de8dfe9577e1efb4980d6cc179bc3531294b7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * A Record of a PDB file
 *
 * @author Sander
 */
class PalmRecord extends FileObject {
	/**
	 * @var FileElement
	 */
	private $elements;

	public function __construct($settings, $records, $textRecords, $textLength, $images){
		$this->elements = new FileElement(array(
			"compression"=>new FileShort(),
			"unused"=>new FileShort(),
			"textLength"=>new FileInt(),
			"recordCount"=>new FileShort(),
			"recordSize"=>new FileShort(),
			"encryptionType"=>new FileShort(),
			"unused2"=>new FileShort(),
			//MOBI Header
			"mobiIdentifier"=>new FileString("MOBI", 4),
			"mobiHeaderLength"=>new FileInt(),
			"mobiType"=>new FileInt(),
			"textEncoding"=>new FileInt(),
			"uniqueID"=>new FileInt(),
			"fileVersion"=>new FileInt(),
			"reserved"=>new FileString(40),
			"firstNonBookIndex"=>new FileInt(),
			"fullNameOffset"=>new FileInt(),
			"fullNameLength"=>new FileInt(),
			"locale"=>new FileInt(),
			"inputLanguage"=>new FileInt(),
			"outputLanguage"=>new FileInt(),
			"minimumVersion"=>new FileInt(),
			"firstImageIndex"=>new FileInt(),
			"huffmanRecordOffset"=>new FileInt(),
			"huffmanRecordCount"=>new FileInt(),
			"unused3"=>new FileString(8),
			"exthFlags"=>new FileInt(0x40),
			"unknown"=>new FileString(32),
			"drmOffset"=>new FileInt(0xFFFFFFFF),
			"drmCount"=>new FileShort(0xFFFFFFFF),
			"drmSize"=>new FileShort(),
			"drmFlags"=>new FileInt(),
			"mobiFiller"=>new FileString(72),
			//EXTH Header
			"exthIdentifier"=>new FileString("EXTH", 4),
			"exthHeaderLength"=>new FileInt(),
			"exthRecordCount"=>new FileInt(),
			"exthRecords"=>new FileElement(),
			"exthPadding"=>new FileString(),
			//"fullNamePadding"=>new FileString(100),
			"fullName"=>new FileString()
				));

		//Set values from the info block
		foreach($settings->values as $name => $val){
			//echo $name.", ";
			if($this->elements->exists($name)){
				$this->elements->get($name)->set($settings->get($name));
			}
		}

		$els = $settings->values;

		$exthElems = new FileElement();
		$i = 0;
		$l = 0;
		foreach($els as $name=>$val){
			$type = EXTHHelper::textToType($name);
			if($type !== false){
				$type = new FileInt($type);
				$length = new FileInt(8+strlen($val));
				$data = new FileString($val);
				$l += 8+strlen($val);
				$exthElems->add("type".$i, $type);
				$exthElems->add("length".$i, $length);
				$exthElems->add("data".$i, $data);
				$i++;
			}
		}

		if($images > 0){
			$this->elements->get("firstImageIndex")->set($textRecords+1);
		}
		$this->elements->get("firstNonBookIndex")->set($textRecords+2+$images);
		$this->elements->get("reserved")->set(str_pad("", 40, chr(255), STR_PAD_RIGHT));
		$this->elements->get("exthRecordCount")->set($i);
		$this->elements->set("exthRecords", $exthElems);
		$pad = $l%4;
		$pad = (4-$pad)%4;
		$this->elements->get("exthPadding")->set(str_pad("", $pad, "\0", STR_PAD_RIGHT));
		$this->elements->get("exthHeaderLength")->set(12+$l+$pad);


		$this->elements->get("recordCount")->set($textRecords);

		$this->elements->get("fullNameOffset")->set($this->elements->offsetToEntry("fullName"));
		$this->elements->get("fullNameLength")->set(strlen($settings->get("title")));
		$this->elements->get("fullName")->set($settings->get("title"));
		$this->elements->get("textLength")->set($textLength);
	}

	public function getByteLength(){
		return $this->getLength();
	}

	public function getLength(){
		return $this->elements->getByteLength();
	}

	public function get(){
		return $this;
	}

	public function set($elements){
		throw new Exception("Unallowed set");
	}

	public function serialize() {
		return $this->elements->serialize();
	}

	public function unserialize($data) {
		$this->elements->unserialize($data);
	}

	public function __toString(){
		$output = "PalmDoc Record (".$this->getByteLength()." bytes):\n";
		$output .= $this->elements;
		return $output;
	}
}
?>