Debian Codegraph @

vendor/dompdf/php-font-lib/src/FontLib/BinaryStream.php

raw
1 <?php
2 /**
3 * @package php-font-lib
4 * @link https://github.com/dompdf/php-font-lib
5 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
6 */
7
8 namespace FontLib;
9
10 /**
11 * Generic font file binary stream.
12 *
13 * @package php-font-lib
14 */
15 class BinaryStream {
16 /**
17 * @var resource The file pointer
18 */
19 protected $f;
20
21 const uint8 = 1;
22 const int8 = 2;
23 const uint16 = 3;
24 const int16 = 4;
25 const uint32 = 5;
26 const int32 = 6;
27 const shortFrac = 7;
28 const Fixed = 8;
29 const FWord = 9;
30 const uFWord = 10;
31 const F2Dot14 = 11;
32 const longDateTime = 12;
33 const char = 13;
34
35 const modeRead = "rb";
36 const modeWrite = "wb";
37 const modeReadWrite = "rb+";
38
39 static function backtrace() {
40 var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
41 }
42
43 /**
44 * Open a font file in read mode
45 *
46 * @param string $filename The file name of the font to open
47 *
48 * @return bool
49 */
50 public function load($filename) {
51 return $this->open($filename, self::modeRead);
52 }
53
54 /**
55 * Open a font file in a chosen mode
56 *
57 * @param string $filename The file name of the font to open
58 * @param string $mode The opening mode
59 *
60 * @throws \Exception
61 * @return bool
62 */
63 public function open($filename, $mode = self::modeRead) {
64 if (!in_array($mode, array(self::modeRead, self::modeWrite, self::modeReadWrite))) {
65 throw new \Exception("Unknown file open mode");
66 }
67
68 $this->f = fopen($filename, $mode);
69
70 return $this->f != false;
71 }
72
73 /**
74 * Close the internal file pointer
75 */
76 public function close() {
77 return fclose($this->f) != false;
78 }
79
80 /**
81 * Change the internal file pointer
82 *
83 * @param resource $fp
84 *
85 * @throws \Exception
86 */
87 public function setFile($fp) {
88 if (!is_resource($fp)) {
89 throw new \Exception('$fp is not a valid resource');
90 }
91
92 $this->f = $fp;
93 }
94
95 /**
96 * Create a temporary file in write mode
97 *
98 * @param bool $allow_memory Allow in-memory files
99 *
100 * @return resource the temporary file pointer resource
101 */
102 public static function getTempFile($allow_memory = true) {
103 $f = null;
104
105 if ($allow_memory) {
106 $f = fopen("php://temp", "rb+");
107 }
108 else {
109 $f = fopen(tempnam(sys_get_temp_dir(), "fnt"), "rb+");
110 }
111
112 return $f;
113 }
114
115 /**
116 * Move the internal file pinter to $offset bytes
117 *
118 * @param int $offset
119 *
120 * @return bool True if the $offset position exists in the file
121 */
122 public function seek($offset) {
123 return fseek($this->f, (int)$offset, SEEK_SET) == 0;
124 }
125
126 /**
127 * Gives the current position in the file
128 *
129 * @return int The current position
130 */
131 public function pos() {
132 return ftell($this->f);
133 }
134
135 public function skip($n) {
136 fseek($this->f, $n, SEEK_CUR);
137 }
138
139 /**
140 * @param int $n The number of bytes to read
141 *
142 * @return string
143 */
144 public function read($n) {
145 if ($n < 1) {
146 return "";
147 }
148
149 return (string) fread($this->f, $n);
150 }
151
152 public function write($data, $length = null) {
153 if ($data === null || $data === "" || $data === false) {
154 return 0;
155 }
156
157 return fwrite($this->f, $data, $length);
158 }
159
160 public function readUInt8() {
161 $byte = $this->read(1);
162 if ($byte === '') {
163 return 0;
164 }
165 return ord($byte);
166 }
167
168 public function readUInt8Many($count) {
169 return array_values(unpack("C*", $this->read($count)));
170 }
171
172 public function writeUInt8($data) {
173 return $this->write(chr($data), 1);
174 }
175
176 public function readInt8() {
177 $v = $this->readUInt8();
178
179 if ($v >= 0x80) {
180 $v -= 0x100;
181 }
182
183 return $v;
184 }
185
186 public function readInt8Many($count) {
187 return array_values(unpack("c*", $this->read($count)));
188 }
189
190 public function writeInt8($data) {
191 if ($data < 0) {
192 $data += 0x100;
193 }
194
195 return $this->writeUInt8($data);
196 }
197
198 public function readUInt16() {
199 $a = unpack("nn", $this->read(2));
200
201 return $a["n"];
202 }
203
204 public function readUInt16Many($count) {
205 return array_values(unpack("n*", $this->read($count * 2)));
206 }
207
208 public function readUFWord() {
209 return $this->readUInt16();
210 }
211
212 public function writeUInt16($data) {
213 return $this->write(pack("n", $data), 2);
214 }
215
216 public function writeUFWord($data) {
217 return $this->writeUInt16($data);
218 }
219
220 public function readInt16() {
221 $a = unpack("nn", $this->read(2));
222 $v = $a["n"];
223
224 if ($v >= 0x8000) {
225 $v -= 0x10000;
226 }
227
228 return $v;
229 }
230
231 public function readInt16Many($count) {
232 $vals = array_values(unpack("n*", $this->read($count * 2)));
233 foreach ($vals as &$v) {
234 if ($v >= 0x8000) {
235 $v -= 0x10000;
236 }
237 }
238
239 return $vals;
240 }
241
242 public function readFWord() {
243 return $this->readInt16();
244 }
245
246 public function writeInt16($data) {
247 if ($data < 0) {
248 $data += 0x10000;
249 }
250
251 return $this->writeUInt16($data);
252 }
253
254 public function writeFWord($data) {
255 return $this->writeInt16($data);
256 }
257
258 public function readUInt32() {
259 $a = unpack("NN", $this->read(4));
260
261 return $a["N"];
262 }
263
264 public function writeUInt32($data) {
265 return $this->write(pack("N", $data), 4);
266 }
267
268 public function readFixed() {
269 $d = $this->readInt16();
270 $d2 = $this->readUInt16();
271
272 return round($d + $d2 / 0x10000, 4);
273 }
274
275 public function writeFixed($data) {
276 $left = floor($data);
277 $right = ($data - $left) * 0x10000;
278
279 return $this->writeInt16($left) + $this->writeUInt16($right);
280 }
281
282 public function readLongDateTime() {
283 $this->readUInt32(); // ignored
284 $date = $this->readUInt32() - 2082844800;
285
286 # PHP_INT_MIN isn't defined in PHP < 7.0
287 $php_int_min = defined("PHP_INT_MIN") ? PHP_INT_MIN : ~PHP_INT_MAX;
288
289 if (is_string($date) || $date > PHP_INT_MAX || $date < $php_int_min) {
290 $date = 0;
291 }
292
293 return date("Y-m-d H:i:s", $date);
294 }
295
296 public function writeLongDateTime($data) {
297 $date = strtotime($data);
298 $date += 2082844800;
299
300 return $this->writeUInt32(0) + $this->writeUInt32($date);
301 }
302
303 public function unpack($def) {
304 $d = array();
305 foreach ($def as $name => $type) {
306 $d[$name] = $this->r($type);
307 }
308
309 return $d;
310 }
311
312 public function pack($def, $data) {
313 $bytes = 0;
314 foreach ($def as $name => $type) {
315 $bytes += $this->w($type, $data[$name]);
316 }
317
318 return $bytes;
319 }
320
321 /**
322 * Read a data of type $type in the file from the current position
323 *
324 * @param mixed $type The data type to read
325 *
326 * @return mixed The data that was read
327 */
328 public function r($type) {
329 switch ($type) {
330 case self::uint8:
331 return $this->readUInt8();
332 case self::int8:
333 return $this->readInt8();
334 case self::uint16:
335 return $this->readUInt16();
336 case self::int16:
337 return $this->readInt16();
338 case self::uint32:
339 return $this->readUInt32();
340 case self::int32:
341 return $this->readUInt32();
342 case self::shortFrac:
343 return $this->readFixed();
344 case self::Fixed:
345 return $this->readFixed();
346 case self::FWord:
347 return $this->readInt16();
348 case self::uFWord:
349 return $this->readUInt16();
350 case self::F2Dot14:
351 return $this->readInt16();
352 case self::longDateTime:
353 return $this->readLongDateTime();
354 case self::char:
355 return $this->read(1);
356 default:
357 if (is_array($type)) {
358 if ($type[0] == self::char) {
359 return $this->read($type[1]);
360 }
361 if ($type[0] == self::uint16) {
362 return $this->readUInt16Many($type[1]);
363 }
364 if ($type[0] == self::int16) {
365 return $this->readInt16Many($type[1]);
366 }
367 if ($type[0] == self::uint8) {
368 return $this->readUInt8Many($type[1]);
369 }
370 if ($type[0] == self::int8) {
371 return $this->readInt8Many($type[1]);
372 }
373
374 $ret = array();
375 for ($i = 0; $i < $type[1]; $i++) {
376 $ret[] = $this->r($type[0]);
377 }
378
379 return $ret;
380 }
381
382 return null;
383 }
384 }
385
386 /**
387 * Write $data of type $type in the file from the current position
388 *
389 * @param mixed $type The data type to write
390 * @param mixed $data The data to write
391 *
392 * @return int The number of bytes read
393 */
394 public function w($type, $data) {
395 switch ($type) {
396 case self::uint8:
397 return $this->writeUInt8($data);
398 case self::int8:
399 return $this->writeInt8($data);
400 case self::uint16:
401 return $this->writeUInt16($data);
402 case self::int16:
403 return $this->writeInt16($data);
404 case self::uint32:
405 return $this->writeUInt32($data);
406 case self::int32:
407 return $this->writeUInt32($data);
408 case self::shortFrac:
409 return $this->writeFixed($data);
410 case self::Fixed:
411 return $this->writeFixed($data);
412 case self::FWord:
413 return $this->writeInt16($data);
414 case self::uFWord:
415 return $this->writeUInt16($data);
416 case self::F2Dot14:
417 return $this->writeInt16($data);
418 case self::longDateTime:
419 return $this->writeLongDateTime($data);
420 case self::char:
421 return $this->write($data, 1);
422 default:
423 if (is_array($type)) {
424 if ($type[0] == self::char) {
425 return $this->write($data, $type[1]);
426 }
427
428 $ret = 0;
429 for ($i = 0; $i < $type[1]; $i++) {
430 if (isset($data[$i])) {
431 $ret += $this->w($type[0], $data[$i]);
432 }
433 }
434
435 return $ret;
436 }
437
438 return null;
439 }
440 }
441
442 /**
443 * Converts a Uint32 value to string
444 *
445 * @param int $uint32
446 *
447 * @return string The string
448 */
449 public function convertUInt32ToStr($uint32) {
450 return chr(($uint32 >> 24) & 0xFF) . chr(($uint32 >> 16) & 0xFF) . chr(($uint32 >> 8) & 0xFF) . chr($uint32 & 0xFF);
451 }
452 }