]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | /** | |
3 | * A Record of a PDB file | |
4 | * | |
5 | * @author Sander | |
6 | */ | |
7 | class PalmRecord extends FileObject { | |
8 | /** | |
9 | * @var FileElement | |
10 | */ | |
11 | private $elements; | |
12 | ||
13 | public function __construct($settings, $records, $textRecords, $textLength, $images){ | |
14 | $this->elements = new FileElement(array( | |
15 | "compression"=>new FileShort(), | |
16 | "unused"=>new FileShort(), | |
17 | "textLength"=>new FileInt(), | |
18 | "recordCount"=>new FileShort(), | |
19 | "recordSize"=>new FileShort(), | |
20 | "encryptionType"=>new FileShort(), | |
21 | "unused2"=>new FileShort(), | |
22 | //MOBI Header | |
23 | "mobiIdentifier"=>new FileString("MOBI", 4), | |
24 | "mobiHeaderLength"=>new FileInt(), | |
25 | "mobiType"=>new FileInt(), | |
26 | "textEncoding"=>new FileInt(), | |
27 | "uniqueID"=>new FileInt(), | |
28 | "fileVersion"=>new FileInt(), | |
29 | "reserved"=>new FileString(40), | |
30 | "firstNonBookIndex"=>new FileInt(), | |
31 | "fullNameOffset"=>new FileInt(), | |
32 | "fullNameLength"=>new FileInt(), | |
33 | "locale"=>new FileInt(), | |
34 | "inputLanguage"=>new FileInt(), | |
35 | "outputLanguage"=>new FileInt(), | |
36 | "minimumVersion"=>new FileInt(), | |
37 | "firstImageIndex"=>new FileInt(), | |
38 | "huffmanRecordOffset"=>new FileInt(), | |
39 | "huffmanRecordCount"=>new FileInt(), | |
40 | "unused3"=>new FileString(8), | |
41 | "exthFlags"=>new FileInt(0x50), | |
42 | "unknown"=>new FileString(32), | |
43 | "drmOffset"=>new FileInt(0xFFFFFFFF), | |
44 | "drmCount"=>new FileInt(0xFFFFFFFF), | |
45 | "drmSize"=>new FileInt(), | |
46 | "drmFlags"=>new FileInt(), | |
47 | "mobiFiller"=>new FileString(12), | |
48 | "offset192"=>new FileShort(0x01), | |
49 | "offset194"=>new FileShort(), | |
50 | "offset196"=>new FileInt(0x01), | |
51 | "offset200"=>new FileInt(), | |
52 | "offset204"=>new FileInt(0x01), | |
53 | "offset208"=>new FileInt(), | |
54 | "offset212"=>new FileInt(0x01), | |
55 | "offset216"=>new FileString(8), | |
56 | "offset224"=>new FileInt(0xFFFFFFFF), | |
57 | "offset228"=>new FileInt(), | |
58 | "offset232"=>new FileString(8), | |
59 | "offset240"=>new FileInt(0x01), | |
60 | "offset244"=>new FileInt(0xFFFFFFFF), | |
61 | //EXTH Header | |
62 | "exthIdentifier"=>new FileString("EXTH", 4), | |
63 | "exthHeaderLength"=>new FileInt(), | |
64 | "exthRecordCount"=>new FileInt(), | |
65 | "exthRecords"=>new FileElement(), | |
66 | "exthPadding"=>new FileString(),//added the 2 extra pad bytes that comes before name/title | |
67 | //"fullNamePadding"=>new FileString(100), | |
68 | "fullName"=>new FileString() | |
69 | )); | |
70 | ||
71 | //Set values from the info block | |
72 | foreach($settings->values as $name => $val){ | |
73 | //echo $name.", "; | |
74 | if($this->elements->exists($name)){ | |
75 | $this->elements->get($name)->set($settings->get($name)); | |
76 | } | |
77 | } | |
78 | ||
79 | $els = $settings->values; | |
80 | ||
81 | $exthElems = new FileElement(); | |
82 | $i = 0; | |
83 | $l = 0; | |
84 | foreach($els as $name=>$val){ | |
85 | $type = EXTHHelper::textToType($name); | |
86 | if($type !== false){ | |
87 | $type = new FileInt($type); | |
88 | $length = new FileInt(8+strlen($val)); | |
89 | $data = new FileString($val); | |
90 | $l += 8+strlen($val); | |
91 | $exthElems->add("type".$i, $type); | |
92 | $exthElems->add("length".$i, $length); | |
93 | $exthElems->add("data".$i, $data); | |
94 | $i++; | |
95 | } | |
96 | } | |
97 | ||
98 | if($images > 0){ | |
99 | $this->elements->get("firstImageIndex")->set($textRecords+2); | |
100 | } | |
101 | $this->elements->get("firstNonBookIndex")->set($textRecords+2+$images); | |
102 | $this->elements->get("reserved")->set(str_pad("", 40, chr(255), STR_PAD_RIGHT)); | |
103 | $this->elements->get("exthRecordCount")->set($i); | |
104 | $this->elements->set("exthRecords", $exthElems); | |
105 | $pad = $l%4; | |
106 | $pad = (4-$pad)%4; | |
107 | $this->elements->get("exthPadding")->set(str_pad("", $pad+2, "\0", STR_PAD_RIGHT)); | |
108 | $this->elements->get("exthHeaderLength")->set(12+$l+$pad); | |
109 | ||
110 | ||
111 | $this->elements->get("recordCount")->set($textRecords); | |
112 | ||
113 | $this->elements->get("fullNameOffset")->set($this->elements->offsetToEntry("fullName"));//need to be checked | |
114 | $this->elements->get("fullNameLength")->set(strlen($settings->get("title"))); | |
115 | $this->elements->get("fullName")->set($settings->get("title")); | |
116 | $this->elements->get("textLength")->set($textLength); | |
117 | ||
118 | $this->elements->get("offset194")->set($textRecords+2+$images); | |
119 | $this->elements->get("offset200")->set($textRecords+4+$images); | |
120 | $this->elements->get("offset208")->set($textRecords+3+$images); | |
121 | $this->elements->get("offset232")->set(str_pad("", 8, chr(255), STR_PAD_RIGHT)); | |
122 | } | |
123 | ||
124 | public function getByteLength(){ | |
125 | return $this->getLength(); | |
126 | } | |
127 | ||
128 | public function getLength(){ | |
129 | return $this->elements->getByteLength(); | |
130 | } | |
131 | ||
132 | public function get(){ | |
133 | return $this; | |
134 | } | |
135 | ||
136 | public function set($elements){ | |
137 | throw new Exception("Unallowed set"); | |
138 | } | |
139 | ||
140 | public function serialize() { | |
141 | return $this->elements->serialize(); | |
142 | } | |
143 | ||
144 | public function unserialize($data) { | |
145 | $this->elements->unserialize($data); | |
146 | } | |
147 | ||
148 | public function __toString(){ | |
149 | $output = "PalmDoc Record (".$this->getByteLength()." bytes):\n"; | |
150 | $output .= $this->elements; | |
151 | return $output; | |
152 | } | |
153 | } | |
154 | ?> |