Projet

Général

Profil

Paste
Télécharger (26,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / tcpdf-version / tcpdf_parser.php @ 41cc1b08

1
<?php
2
//============================================================+
3
// File name   : tcpdf_parser.php
4
// Version     : 1.0.010
5
// Begin       : 2011-05-23
6
// Last Update : 2013-09-25
7
// Author      : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
8
// License     : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3
9
// -------------------------------------------------------------------
10
// Copyright (C) 2011-2013 Nicola Asuni - Tecnick.com LTD
11
//
12
// This file is part of TCPDF software library.
13
//
14
// TCPDF is free software: you can redistribute it and/or modify it
15
// under the terms of the GNU Lesser General Public License as
16
// published by the Free Software Foundation, either version 3 of the
17
// License, or (at your option) any later version.
18
//
19
// TCPDF is distributed in the hope that it will be useful, but
20
// WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
// See the GNU Lesser General Public License for more details.
23
//
24
// You should have received a copy of the License
25
// along with TCPDF. If not, see
26
// <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
27
//
28
// See LICENSE.TXT file for more information.
29
// -------------------------------------------------------------------
30
//
31
// Description : This is a PHP class for parsing PDF documents.
32
//
33
//============================================================+
34

    
35
/**
36
 * @file
37
 * This is a PHP class for parsing PDF documents.<br>
38
 * @package com.tecnick.tcpdf
39
 * @author Nicola Asuni
40
 * @version 1.0.010
41
 */
42

    
43
// include class for decoding filters
44
require_once(dirname(__FILE__).'/include/tcpdf_filters.php');
45

    
46
/**
47
 * @class TCPDF_PARSER
48
 * This is a PHP class for parsing PDF documents.<br>
49
 * @package com.tecnick.tcpdf
50
 * @brief This is a PHP class for parsing PDF documents..
51
 * @version 1.0.010
52
 * @author Nicola Asuni - info@tecnick.com
53
 */
54
class TCPDF_PARSER {
55

    
56
        /**
57
         * Raw content of the PDF document.
58
         * @private
59
         */
60
        private $pdfdata = '';
61

    
62
        /**
63
         * XREF data.
64
         * @protected
65
         */
66
        protected $xref = array();
67

    
68
        /**
69
         * Array of PDF objects.
70
         * @protected
71
         */
72
        protected $objects = array();
73

    
74
        /**
75
         * Class object for decoding filters.
76
         * @private
77
         */
78
        private $FilterDecoders;
79

    
80
        /**
81
         * Array of configuration parameters.
82
         * @private
83
         */
84
        private $cfg = array(
85
                'die_for_errors' => false,
86
                'ignore_filter_decoding_errors' => true,
87
                'ignore_missing_filter_decoders' => true,
88
        );
89

    
90
// -----------------------------------------------------------------------------
91

    
92
        /**
93
         * Parse a PDF document an return an array of objects.
94
         * @param $data (string) PDF data to parse.
95
         * @param $cfg (array) Array of configuration parameters:
96
         *                         'die_for_errors' : if true termitate the program execution in case of error, otherwise thows an exception;
97
         *                         'ignore_filter_decoding_errors' : if true ignore filter decoding errors;
98
         *                         'ignore_missing_filter_decoders' : if true ignore missing filter decoding errors.
99
         * @public
100
         * @since 1.0.000 (2011-05-24)
101
         */
102
        public function __construct($data, $cfg=array()) {
103
                if (empty($data)) {
104
                        $this->Error('Empty PDF data.');
105
                }
106
                // set configuration parameters
107
                $this->setConfig($cfg);
108
                // get PDF content string
109
                $this->pdfdata = $data;
110
                // get length
111
                $pdflen = strlen($this->pdfdata);
112
                // get xref and trailer data
113
                $this->xref = $this->getXrefData();
114
                // parse all document objects
115
                $this->objects = array();
116
                foreach ($this->xref['xref'] as $obj => $offset) {
117
                        if (!isset($this->objects[$obj]) AND ($offset > 0)) {
118
                                // decode objects with positive offset
119
                                $this->objects[$obj] = $this->getIndirectObject($obj, $offset, true);
120
                        }
121
                }
122
                // release some memory
123
                unset($this->pdfdata);
124
                $this->pdfdata = '';
125
        }
126

    
127
        /**
128
         * Set the configuration parameters.
129
         * @param $cfg (array) Array of configuration parameters:
130
         *                         'die_for_errors' : if true termitate the program execution in case of error, otherwise thows an exception;
131
         *                         'ignore_filter_decoding_errors' : if true ignore filter decoding errors;
132
         *                         'ignore_missing_filter_decoders' : if true ignore missing filter decoding errors.
133
         * @public
134
         */
135
        protected function setConfig($cfg) {
136
                if (isset($cfg['die_for_errors'])) {
137
                        $this->cfg['die_for_errors'] = !!$cfg['die_for_errors'];
138
                }
139
                if (isset($cfg['ignore_filter_decoding_errors'])) {
140
                        $this->cfg['ignore_filter_decoding_errors'] = !!$cfg['ignore_filter_decoding_errors'];
141
                }
142
                if (isset($cfg['ignore_missing_filter_decoders'])) {
143
                        $this->cfg['ignore_missing_filter_decoders'] = !!$cfg['ignore_missing_filter_decoders'];
144
                }
145
        }
146

    
147
        /**
148
         * Return an array of parsed PDF document objects.
149
         * @return (array) Array of parsed PDF document objects.
150
         * @public
151
         * @since 1.0.000 (2011-06-26)
152
         */
153
        public function getParsedData() {
154
                return array($this->xref, $this->objects);
155
        }
156

    
157
        /**
158
         * Get Cross-Reference (xref) table and trailer data from PDF document data.
159
         * @param $offset (int) xref offset (if know).
160
         * @param $xref (array) previous xref array (if any).
161
         * @return Array containing xref and trailer data.
162
         * @protected
163
         * @since 1.0.000 (2011-05-24)
164
         */
165
        protected function getXrefData($offset=0, $xref=array()) {
166
                if ($offset == 0) {
167
                        // find last startxref
168
                        if (preg_match_all('/[\r\n]startxref[\s]*[\r\n]+([0-9]+)[\s]*[\r\n]+%%EOF/i', $this->pdfdata, $matches, PREG_SET_ORDER, $offset) == 0) {
169
                                $this->Error('Unable to find startxref');
170
                        }
171
                        $matches = array_pop($matches);
172
                        $startxref = $matches[1];
173
                } elseif (strpos($this->pdfdata, 'xref', $offset) == $offset) {
174
                        // Already pointing at the xref table
175
                        $startxref = $offset;
176
                } elseif (preg_match('/([0-9]+[\s][0-9]+[\s]obj)/i', $this->pdfdata, $matches, PREG_OFFSET_CAPTURE, $offset)) {
177
                        // Cross-Reference Stream object
178
                        $startxref = $offset;
179
                } elseif (preg_match('/[\r\n]startxref[\s]*[\r\n]+([0-9]+)[\s]*[\r\n]+%%EOF/i', $this->pdfdata, $matches, PREG_OFFSET_CAPTURE, $offset)) {
180
                        // startxref found
181
                        $startxref = $matches[1][0];
182
                } else {
183
                        $this->Error('Unable to find startxref');
184
                }
185
                // check xref position
186
                if (strpos($this->pdfdata, 'xref', $startxref) == $startxref) {
187
                        // Cross-Reference
188
                        $xref = $this->decodeXref($startxref, $xref);
189
                } else {
190
                        // Cross-Reference Stream
191
                        $xref = $this->decodeXrefStream($startxref, $xref);
192
                }
193
                if (empty($xref)) {
194
                        $this->Error('Unable to find xref');
195
                }
196
                return $xref;
197
        }
198

    
199
        /**
200
         * Decode the Cross-Reference section
201
         * @param $startxref (int) Offset at which the xref section starts (position of the 'xref' keyword).
202
         * @param $xref (array) Previous xref array (if any).
203
         * @return Array containing xref and trailer data.
204
         * @protected
205
         * @since 1.0.000 (2011-06-20)
206
         */
207
        protected function decodeXref($startxref, $xref=array()) {
208
                $startxref += 4; // 4 is the lenght of the word 'xref'
209
                // skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP)
210
                $offset = $startxref + strspn($this->pdfdata, "\x00\x09\x0a\x0c\x0d\x20", $startxref);
211
                // initialize object number
212
                $obj_num = 0;
213
                // search for cross-reference entries or subsection
214
                while (preg_match('/([0-9]+)[\x20]([0-9]+)[\x20]?([nf]?)(\r\n|[\x20]?[\r\n])/', $this->pdfdata, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
215
                        if ($matches[0][1] != $offset) {
216
                                // we are on another section
217
                                break;
218
                        }
219
                        $offset += strlen($matches[0][0]);
220
                        if ($matches[3][0] == 'n') {
221
                                // create unique object index: [object number]_[generation number]
222
                                $index = $obj_num.'_'.intval($matches[2][0]);
223
                                // check if object already exist
224
                                if (!isset($xref['xref'][$index])) {
225
                                        // store object offset position
226
                                        $xref['xref'][$index] = intval($matches[1][0]);
227
                                }
228
                                ++$obj_num;
229
                        } elseif ($matches[3][0] == 'f') {
230
                                ++$obj_num;
231
                        } else {
232
                                // object number (index)
233
                                $obj_num = intval($matches[1][0]);
234
                        }
235
                }
236
                // get trailer data
237
                if (preg_match('/trailer[\s]*<<(.*)>>[\s]*[\r\n]+startxref[\s]*[\r\n]+/isU', $this->pdfdata, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
238
                        $trailer_data = $matches[1][0];
239
                        if (!isset($xref['trailer']) OR empty($xref['trailer'])) {
240
                                // get only the last updated version
241
                                $xref['trailer'] = array();
242
                                // parse trailer_data
243
                                if (preg_match('/Size[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
244
                                        $xref['trailer']['size'] = intval($matches[1]);
245
                                }
246
                                if (preg_match('/Root[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
247
                                        $xref['trailer']['root'] = intval($matches[1]).'_'.intval($matches[2]);
248
                                }
249
                                if (preg_match('/Encrypt[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
250
                                        $xref['trailer']['encrypt'] = intval($matches[1]).'_'.intval($matches[2]);
251
                                }
252
                                if (preg_match('/Info[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) {
253
                                        $xref['trailer']['info'] = intval($matches[1]).'_'.intval($matches[2]);
254
                                }
255
                                if (preg_match('/ID[\s]*[\[][\s]*[<]([^>]*)[>][\s]*[<]([^>]*)[>]/i', $trailer_data, $matches) > 0) {
256
                                        $xref['trailer']['id'] = array();
257
                                        $xref['trailer']['id'][0] = $matches[1];
258
                                        $xref['trailer']['id'][1] = $matches[2];
259
                                }
260
                        }
261
                        if (preg_match('/Prev[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
262
                                // get previous xref
263
                                $xref = $this->getXrefData(intval($matches[1]), $xref);
264
                        }
265
                } else {
266
                        $this->Error('Unable to find trailer');
267
                }
268
                return $xref;
269
        }
270

    
271
        /**
272
         * Decode the Cross-Reference Stream section
273
         * @param $startxref (int) Offset at which the xref section starts.
274
         * @param $xref (array) Previous xref array (if any).
275
         * @return Array containing xref and trailer data.
276
         * @protected
277
         * @since 1.0.003 (2013-03-16)
278
         */
279
        protected function decodeXrefStream($startxref, $xref=array()) {
280
                // try to read Cross-Reference Stream
281
                $xrefobj = $this->getRawObject($startxref);
282
                $xrefcrs = $this->getIndirectObject($xrefobj[1], $startxref, true);
283
                if (!isset($xref['trailer']) OR empty($xref['trailer'])) {
284
                        // get only the last updated version
285
                        $xref['trailer'] = array();
286
                        $filltrailer = true;
287
                } else {
288
                        $filltrailer = false;
289
                }
290
                $valid_crs = false;
291
                $sarr = $xrefcrs[0][1];
292
                foreach ($sarr as $k => $v) {
293
                        if (($v[0] == '/') AND ($v[1] == 'Type') AND (isset($sarr[($k +1)]) AND ($sarr[($k +1)][0] == '/') AND ($sarr[($k +1)][1] == 'XRef'))) {
294
                                $valid_crs = true;
295
                        } elseif (($v[0] == '/') AND ($v[1] == 'Index') AND (isset($sarr[($k +1)]))) {
296
                                // first object number in the subsection
297
                                $index_first = intval($sarr[($k +1)][1][0][1]);
298
                                // number of entries in the subsection
299
                                $index_entries = intval($sarr[($k +1)][1][1][1]);
300
                        } elseif (($v[0] == '/') AND ($v[1] == 'Prev') AND (isset($sarr[($k +1)]) AND ($sarr[($k +1)][0] == 'numeric'))) {
301
                                // get previous xref offset
302
                                $prevxref = intval($sarr[($k +1)][1]);
303
                        } elseif (($v[0] == '/') AND ($v[1] == 'W') AND (isset($sarr[($k +1)]))) {
304
                                // number of bytes (in the decoded stream) of the corresponding field
305
                                $wb = array();
306
                                $wb[0] = intval($sarr[($k +1)][1][0][1]);
307
                                $wb[1] = intval($sarr[($k +1)][1][1][1]);
308
                                $wb[2] = intval($sarr[($k +1)][1][2][1]);
309
                        } elseif (($v[0] == '/') AND ($v[1] == 'DecodeParms') AND (isset($sarr[($k +1)][1]))) {
310
                                $decpar = $sarr[($k +1)][1];
311
                                foreach ($decpar as $kdc => $vdc) {
312
                                        if (($vdc[0] == '/') AND ($vdc[1] == 'Columns') AND (isset($decpar[($kdc +1)]) AND ($decpar[($kdc +1)][0] == 'numeric'))) {
313
                                                $columns = intval($decpar[($kdc +1)][1]);
314
                                        } elseif (($vdc[0] == '/') AND ($vdc[1] == 'Predictor') AND (isset($decpar[($kdc +1)]) AND ($decpar[($kdc +1)][0] == 'numeric'))) {
315
                                                $predictor = intval($decpar[($kdc +1)][1]);
316
                                        }
317
                                }
318
                        } elseif ($filltrailer) {
319
                                if (($v[0] == '/') AND ($v[1] == 'Size') AND (isset($sarr[($k +1)]) AND ($sarr[($k +1)][0] == 'numeric'))) {
320
                                        $xref['trailer']['size'] = $sarr[($k +1)][1];
321
                                } elseif (($v[0] == '/') AND ($v[1] == 'Root') AND (isset($sarr[($k +1)]) AND ($sarr[($k +1)][0] == 'ojbref'))) {
322
                                        $xref['trailer']['root'] = $sarr[($k +1)][1];
323
                                } elseif (($v[0] == '/') AND ($v[1] == 'Info') AND (isset($sarr[($k +1)]) AND ($sarr[($k +1)][0] == 'ojbref'))) {
324
                                        $xref['trailer']['info'] = $sarr[($k +1)][1];
325
                                } elseif (($v[0] == '/') AND ($v[1] == 'ID') AND (isset($sarr[($k +1)]))) {
326
                                        $xref['trailer']['id'] = array();
327
                                        $xref['trailer']['id'][0] = $sarr[($k +1)][1][0][1];
328
                                        $xref['trailer']['id'][1] = $sarr[($k +1)][1][1][1];
329
                                }
330
                        }
331
                }
332
                // decode data
333
                if ($valid_crs AND isset($xrefcrs[1][3][0])) {
334
                        // number of bytes in a row
335
                        $rowlen = ($columns + 1);
336
                        // convert the stream into an array of integers
337
                        $sdata = unpack('C*', $xrefcrs[1][3][0]);
338
                        // split the rows
339
                        $sdata = array_chunk($sdata, $rowlen);
340
                        // initialize decoded array
341
                        $ddata = array();
342
                        // initialize first row with zeros
343
                        $prev_row = array_fill (0, $rowlen, 0);
344
                        // for each row apply PNG unpredictor
345
                        foreach ($sdata as $k => $row) {
346
                                // initialize new row
347
                                $ddata[$k] = array();
348
                                // get PNG predictor value
349
                                $predictor = (10 + $row[0]);
350
                                // for each byte on the row
351
                                for ($i=1; $i<=$columns; ++$i) {
352
                                        // new index
353
                                        $j = ($i - 1);
354
                                        $row_up = $prev_row[$j];
355
                                        if ($i == 1) {
356
                                                $row_left = 0;
357
                                                $row_upleft = 0;
358
                                        } else {
359
                                                $row_left = $row[($i - 1)];
360
                                                $row_upleft = $prev_row[($j - 1)];
361
                                        }
362
                                        switch ($predictor) {
363
                                                case 10: { // PNG prediction (on encoding, PNG None on all rows)
364
                                                        $ddata[$k][$j] = $row[$i];
365
                                                        break;
366
                                                }
367
                                                case 11: { // PNG prediction (on encoding, PNG Sub on all rows)
368
                                                        $ddata[$k][$j] = (($row[$i] + $row_left) & 0xff);
369
                                                        break;
370
                                                }
371
                                                case 12: { // PNG prediction (on encoding, PNG Up on all rows)
372
                                                        $ddata[$k][$j] = (($row[$i] + $row_up) & 0xff);
373
                                                        break;
374
                                                }
375
                                                case 13: { // PNG prediction (on encoding, PNG Average on all rows)
376
                                                        $ddata[$k][$j] = (($row[$i] + (($row_left + $row_up) / 2)) & 0xff);
377
                                                        break;
378
                                                }
379
                                                case 14: { // PNG prediction (on encoding, PNG Paeth on all rows)
380
                                                        // initial estimate
381
                                                        $p = ($row_left + $row_up - $row_upleft);
382
                                                        // distances
383
                                                        $pa = abs($p - $row_left);
384
                                                        $pb = abs($p - $row_up);
385
                                                        $pc = abs($p - $row_upleft);
386
                                                        $pmin = min($pa, $pb, $pc);
387
                                                        // return minumum distance
388
                                                        switch ($pmin) {
389
                                                                case $pa: {
390
                                                                        $ddata[$k][$j] = (($row[$i] + $row_left) & 0xff);
391
                                                                        break;
392
                                                                }
393
                                                                case $pb: {
394
                                                                        $ddata[$k][$j] = (($row[$i] + $row_up) & 0xff);
395
                                                                        break;
396
                                                                }
397
                                                                case $pc: {
398
                                                                        $ddata[$k][$j] = (($row[$i] + $row_upleft) & 0xff);
399
                                                                        break;
400
                                                                }
401
                                                        }
402
                                                        break;
403
                                                }
404
                                                default: { // PNG prediction (on encoding, PNG optimum)
405
                                                        $this->Error('Unknown PNG predictor');
406
                                                        break;
407
                                                }
408
                                        }
409
                                }
410
                                $prev_row = $ddata[$k];
411
                        } // end for each row
412
                        // complete decoding
413
                        $sdata = array();
414
                        // for every row
415
                        foreach ($ddata as $k => $row) {
416
                                // initialize new row
417
                                $sdata[$k] = array(0, 0, 0);
418
                                if ($wb[0] == 0) {
419
                                        // default type field
420
                                        $sdata[$k][0] = 1;
421
                                }
422
                                $i = 0; // count bytes on the row
423
                                // for every column
424
                                for ($c = 0; $c < 3; ++$c) {
425
                                        // for every byte on the column
426
                                        for ($b = 0; $b < $wb[$c]; ++$b) {
427
                                                $sdata[$k][$c] += ($row[$i] << (($wb[$c] - 1 - $b) * 8));
428
                                                ++$i;
429
                                        }
430
                                }
431
                        }
432
                        $ddata = array();
433
                        // fill xref
434
                        if (isset($index_first)) {
435
                                $obj_num = $index_first;
436
                        } else {
437
                                $obj_num = 0;
438
                        }
439
                        foreach ($sdata as $k => $row) {
440
                                switch ($row[0]) {
441
                                        case 0: { // (f) linked list of free objects
442
                                                ++$obj_num;
443
                                                break;
444
                                        }
445
                                        case 1: { // (n) objects that are in use but are not compressed
446
                                                // create unique object index: [object number]_[generation number]
447
                                                $index = $obj_num.'_'.$row[2];
448
                                                // check if object already exist
449
                                                if (!isset($xref['xref'][$index])) {
450
                                                        // store object offset position
451
                                                        $xref['xref'][$index] = $row[1];
452
                                                }
453
                                                ++$obj_num;
454
                                                break;
455
                                        }
456
                                        case 2: { // compressed objects
457
                                                // $row[1] = object number of the object stream in which this object is stored
458
                                                // $row[2] = index of this object within the object stream
459
                                                $index = $row[1].'_0_'.$row[2];
460
                                                $xref['xref'][$index] = -1;
461
                                                break;
462
                                        }
463
                                        default: { // null objects
464
                                                break;
465
                                        }
466
                                }
467
                        }
468
                } // end decoding data
469
                if (isset($prevxref)) {
470
                        // get previous xref
471
                        $xref = $this->getXrefData($prevxref, $xref);
472
                }
473
                return $xref;
474
        }
475

    
476
        /**
477
         * Get object type, raw value and offset to next object
478
         * @param $offset (int) Object offset.
479
         * @return array containing object type, raw value and offset to next object
480
         * @protected
481
         * @since 1.0.000 (2011-06-20)
482
         */
483
        protected function getRawObject($offset=0) {
484
                $objtype = ''; // object type to be returned
485
                $objval = ''; // object value to be returned
486
                // skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP)
487
                $offset += strspn($this->pdfdata, "\x00\x09\x0a\x0c\x0d\x20", $offset);
488
                // get first char
489
                $char = $this->pdfdata[$offset];
490
                // get object type
491
                switch ($char) {
492
                        case '%': { // \x25 PERCENT SIGN
493
                                // skip comment and search for next token
494
                                $next = strcspn($this->pdfdata, "\r\n", $offset);
495
                                if ($next > 0) {
496
                                        $offset += $next;
497
                                        return $this->getRawObject($this->pdfdata, $offset);
498
                                }
499
                                break;
500
                        }
501
                        case '/': { // \x2F SOLIDUS
502
                                // name object
503
                                $objtype = $char;
504
                                ++$offset;
505
                                if (preg_match('/^([^\x00\x09\x0a\x0c\x0d\x20\s\x28\x29\x3c\x3e\x5b\x5d\x7b\x7d\x2f\x25]+)/', substr($this->pdfdata, $offset, 256), $matches) == 1) {
506
                                        $objval = $matches[1]; // unescaped value
507
                                        $offset += strlen($objval);
508
                                }
509
                                break;
510
                        }
511
                        case '(':   // \x28 LEFT PARENTHESIS
512
                        case ')': { // \x29 RIGHT PARENTHESIS
513
                                // literal string object
514
                                $objtype = $char;
515
                                ++$offset;
516
                                $strpos = $offset;
517
                                if ($char == '(') {
518
                                        $open_bracket = 1;
519
                                        while ($open_bracket > 0) {
520
                                                if (!isset($this->pdfdata{$strpos})) {
521
                                                        break;
522
                                                }
523
                                                $ch = $this->pdfdata{$strpos};
524
                                                switch ($ch) {
525
                                                        case '\\': { // REVERSE SOLIDUS (5Ch) (Backslash)
526
                                                                // skip next character
527
                                                                ++$strpos;
528
                                                                break;
529
                                                        }
530
                                                        case '(': { // LEFT PARENHESIS (28h)
531
                                                                ++$open_bracket;
532
                                                                break;
533
                                                        }
534
                                                        case ')': { // RIGHT PARENTHESIS (29h)
535
                                                                --$open_bracket;
536
                                                                break;
537
                                                        }
538
                                                }
539
                                                ++$strpos;
540
                                        }
541
                                        $objval = substr($this->pdfdata, $offset, ($strpos - $offset - 1));
542
                                        $offset = $strpos;
543
                                }
544
                                break;
545
                        }
546
                        case '[':   // \x5B LEFT SQUARE BRACKET
547
                        case ']': { // \x5D RIGHT SQUARE BRACKET
548
                                // array object
549
                                $objtype = $char;
550
                                ++$offset;
551
                                if ($char == '[') {
552
                                        // get array content
553
                                        $objval = array();
554
                                        do {
555
                                                // get element
556
                                                $element = $this->getRawObject($offset);
557
                                                $offset = $element[2];
558
                                                $objval[] = $element;
559
                                        } while ($element[0] != ']');
560
                                        // remove closing delimiter
561
                                        array_pop($objval);
562
                                }
563
                                break;
564
                        }
565
                        case '<':   // \x3C LESS-THAN SIGN
566
                        case '>': { // \x3E GREATER-THAN SIGN
567
                                if (isset($this->pdfdata{($offset + 1)}) AND ($this->pdfdata{($offset + 1)} == $char)) {
568
                                        // dictionary object
569
                                        $objtype = $char.$char;
570
                                        $offset += 2;
571
                                        if ($char == '<') {
572
                                                // get array content
573
                                                $objval = array();
574
                                                do {
575
                                                        // get element
576
                                                        $element = $this->getRawObject($offset);
577
                                                        $offset = $element[2];
578
                                                        $objval[] = $element;
579
                                                } while ($element[0] != '>>');
580
                                                // remove closing delimiter
581
                                                array_pop($objval);
582
                                        }
583
                                } else {
584
                                        // hexadecimal string object
585
                                        $objtype = $char;
586
                                        ++$offset;
587
                                        if (($char == '<') AND (preg_match('/^([0-9A-Fa-f\x09\x0a\x0c\x0d\x20]+)>/iU', substr($this->pdfdata, $offset), $matches) == 1)) {
588
                                                // remove white space characters
589
                                                $objval = strtr($matches[1], "\x09\x0a\x0c\x0d\x20", '');
590
                                                $offset += strlen($matches[0]);
591
                                        }
592
                                }
593
                                break;
594
                        }
595
                        default: {
596
                                if (substr($this->pdfdata, $offset, 6) == 'endobj') {
597
                                        // indirect object
598
                                        $objtype = 'endobj';
599
                                        $offset += 6;
600
                                } elseif (substr($this->pdfdata, $offset, 4) == 'null') {
601
                                        // null object
602
                                        $objtype = 'null';
603
                                        $offset += 4;
604
                                        $objval = 'null';
605
                                } elseif (substr($this->pdfdata, $offset, 4) == 'true') {
606
                                        // boolean true object
607
                                        $objtype = 'boolean';
608
                                        $offset += 4;
609
                                        $objval = 'true';
610
                                } elseif (substr($this->pdfdata, $offset, 5) == 'false') {
611
                                        // boolean false object
612
                                        $objtype = 'boolean';
613
                                        $offset += 5;
614
                                        $objval = 'false';
615
                                } elseif (substr($this->pdfdata, $offset, 6) == 'stream') {
616
                                        // start stream object
617
                                        $objtype = 'stream';
618
                                        $offset += 6;
619
                                        if (preg_match('/^([\r]?[\n])/isU', substr($this->pdfdata, $offset), $matches) == 1) {
620
                                                $offset += strlen($matches[0]);
621
                                                if (preg_match('/([\r]?[\n])?(endstream)[\x09\x0a\x0c\x0d\x20]/isU', substr($this->pdfdata, $offset), $matches, PREG_OFFSET_CAPTURE) == 1) {
622
                                                        $objval = substr($this->pdfdata, $offset, $matches[0][1]);
623
                                                        $offset += $matches[2][1];
624
                                                }
625
                                        }
626
                                } elseif (substr($this->pdfdata, $offset, 9) == 'endstream') {
627
                                        // end stream object
628
                                        $objtype = 'endstream';
629
                                        $offset += 9;
630
                                } elseif (preg_match('/^([0-9]+)[\s]+([0-9]+)[\s]+R/iU', substr($this->pdfdata, $offset, 33), $matches) == 1) {
631
                                        // indirect object reference
632
                                        $objtype = 'ojbref';
633
                                        $offset += strlen($matches[0]);
634
                                        $objval = intval($matches[1]).'_'.intval($matches[2]);
635
                                } elseif (preg_match('/^([0-9]+)[\s]+([0-9]+)[\s]+obj/iU', substr($this->pdfdata, $offset, 33), $matches) == 1) {
636
                                        // object start
637
                                        $objtype = 'ojb';
638
                                        $objval = intval($matches[1]).'_'.intval($matches[2]);
639
                                        $offset += strlen ($matches[0]);
640
                                } elseif (($numlen = strspn($this->pdfdata, '+-.0123456789', $offset)) > 0) {
641
                                        // numeric object
642
                                        $objtype = 'numeric';
643
                                        $objval = substr($this->pdfdata, $offset, $numlen);
644
                                        $offset += $numlen;
645
                                }
646
                                break;
647
                        }
648
                }
649
                return array($objtype, $objval, $offset);
650
        }
651

    
652
        /**
653
         * Get content of indirect object.
654
         * @param $obj_ref (string) Object number and generation number separated by underscore character.
655
         * @param $offset (int) Object offset.
656
         * @param $decoding (boolean) If true decode streams.
657
         * @return array containing object data.
658
         * @protected
659
         * @since 1.0.000 (2011-05-24)
660
         */
661
        protected function getIndirectObject($obj_ref, $offset=0, $decoding=true) {
662
                $obj = explode('_', $obj_ref);
663
                if (($obj === false) OR (count($obj) != 2)) {
664
                        $this->Error('Invalid object reference: '.$obj);
665
                        return;
666
                }
667
                $objref = $obj[0].' '.$obj[1].' obj';
668
                if (strpos($this->pdfdata, $objref, $offset) != $offset) {
669
                        // an indirect reference to an undefined object shall be considered a reference to the null object
670
                        return array('null', 'null', $offset);
671
                }
672
                // starting position of object content
673
                $offset += strlen($objref);
674
                // get array of object content
675
                $objdata = array();
676
                $i = 0; // object main index
677
                do {
678
                        // get element
679
                        $element = $this->getRawObject($offset);
680
                        $offset = $element[2];
681
                        // decode stream using stream's dictionary information
682
                        if ($decoding AND ($element[0] == 'stream') AND (isset($objdata[($i - 1)][0])) AND ($objdata[($i - 1)][0] == '<<')) {
683
                                $element[3] = $this->decodeStream($objdata[($i - 1)][1], $element[1]);
684
                        }
685
                        $objdata[$i] = $element;
686
                        ++$i;
687
                } while ($element[0] != 'endobj');
688
                // remove closing delimiter
689
                array_pop($objdata);
690
                // return raw object content
691
                return $objdata;
692
        }
693

    
694
        /**
695
         * Get the content of object, resolving indect object reference if necessary.
696
         * @param $obj (string) Object value.
697
         * @return array containing object data.
698
         * @protected
699
         * @since 1.0.000 (2011-06-26)
700
         */
701
        protected function getObjectVal($obj) {
702
                if ($obj[0] == 'objref') {
703
                        // reference to indirect object
704
                        if (isset($this->objects[$obj[1]])) {
705
                                // this object has been already parsed
706
                                return $this->objects[$obj[1]];
707
                        } elseif (isset($this->xref[$obj[1]])) {
708
                                // parse new object
709
                                $this->objects[$obj[1]] = $this->getIndirectObject($obj[1], $this->xref[$obj[1]], false);
710
                                return $this->objects[$obj[1]];
711
                        }
712
                }
713
                return $obj;
714
        }
715

    
716
        /**
717
         * Decode the specified stream.
718
         * @param $sdic (array) Stream's dictionary array.
719
         * @param $stream (string) Stream to decode.
720
         * @return array containing decoded stream data and remaining filters.
721
         * @protected
722
         * @since 1.0.000 (2011-06-22)
723
         */
724
        protected function decodeStream($sdic, $stream) {
725
                // get stream lenght and filters
726
                $slength = strlen($stream);
727
                if ($slength <= 0) {
728
                        return array('', array());
729
                }
730
                $filters = array();
731
                foreach ($sdic as $k => $v) {
732
                        if ($v[0] == '/') {
733
                                if (($v[1] == 'Length') AND (isset($sdic[($k + 1)])) AND ($sdic[($k + 1)][0] == 'numeric')) {
734
                                        // get declared stream lenght
735
                                        $declength = intval($sdic[($k + 1)][1]);
736
                                        if ($declength < $slength) {
737
                                                $stream = substr($stream, 0, $declength);
738
                                                $slength = $declength;
739
                                        }
740
                                } elseif (($v[1] == 'Filter') AND (isset($sdic[($k + 1)]))) {
741
                                        // resolve indirect object
742
                                        $objval = $this->getObjectVal($sdic[($k + 1)]);
743
                                        if ($objval[0] == '/') {
744
                                                // single filter
745
                                                $filters[] = $objval[1];
746
                                        } elseif ($objval[0] == '[') {
747
                                                // array of filters
748
                                                foreach ($objval[1] as $flt) {
749
                                                        if ($flt[0] == '/') {
750
                                                                $filters[] = $flt[1];
751
                                                        }
752
                                                }
753
                                        }
754
                                }
755
                        }
756
                }
757
                // decode the stream
758
                $remaining_filters = array();
759
                foreach ($filters as $filter) {
760
                        if (in_array($filter, TCPDF_FILTERS::getAvailableFilters())) {
761
                                try {
762
                                        $stream = TCPDF_FILTERS::decodeFilter($filter, $stream);
763
                                } catch (Exception $e) {
764
                                        $emsg = $e->getMessage();
765
                                        if ((($emsg[0] == '~') AND !$this->cfg['ignore_missing_filter_decoders'])
766
                                                OR (($emsg[0] != '~') AND !$this->cfg['ignore_filter_decoding_errors'])) {
767
                                                $this->Error($e->getMessage());
768
                                        }
769
                                }
770
                        } else {
771
                                // add missing filter to array
772
                                $remaining_filters[] = $filter;
773
                        }
774
                }
775
                return array($stream, $remaining_filters);
776
        }
777

    
778
        /**
779
         * Throw an exception or print an error message and die if the K_TCPDF_PARSER_THROW_EXCEPTION_ERROR constant is set to true.
780
         * @param $msg (string) The error message
781
         * @public
782
         * @since 1.0.000 (2011-05-23)
783
         */
784
        public function Error($msg) {
785
                if ($this->cfg['die_for_errors']) {
786
                        die('<strong>TCPDF_PARSER ERROR: </strong>'.$msg);
787
                } else {
788
                        throw new Exception('TCPDF_PARSER ERROR: '.$msg);
789
                }
790
        }
791

    
792
} // END OF TCPDF_PARSER CLASS
793

    
794
//============================================================+
795
// END OF FILE
796
//============================================================+