PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.14.1
Yatra – Travel Booking & Tour Operator Software v3.0.14.1
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / vendor / phenx / php-font-lib / src / FontLib / BinaryStream.php

BinaryStream.php in Yatra – Travel Booking & Tour Operator Software 3.0.14.1, at vendor/phenx/php-font-lib/src/FontLib/BinaryStream.php

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