source-class-Com.Tecnick.Pdf.Filter.Type.AsciiHex

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
 1: <?php
 2: /**
 3:  * AsciiHex.php
 4:  *
 5:  * @since       2011-05-23
 6:  * @category    Library
 7:  * @package     PdfFilter
 8:  * @author      Nicola Asuni <info@tecnick.com>
 9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
12:  *
13:  * This file is part of tc-lib-pdf-filter software library.
14:  */
15: 
16: namespace Com\Tecnick\Pdf\Filter\Type;
17: 
18: use \Com\Tecnick\Pdf\Filter\Exception as PPException;
19: 
20: /**
21:  * Com\Tecnick\Pdf\Filter\Type\AsciiHex
22:  *
23:  * ASCIIHex
24:  * Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data.
25:  *
26:  * @since       2011-05-23
27:  * @category    Library
28:  * @package     PdfFilter
29:  * @author      Nicola Asuni <info@tecnick.com>
30:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
31:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
32:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
33:  */
34: class AsciiHex
35: {
36:     /**
37:      * Decode the data
38:      *
39:      * @param string $data Data to decode.
40:      *
41:      * @return string Decoded data string.
42:      */
43:     public function decode($data)
44:     {
45:         // initialize string to return
46:         $decoded = '';
47:         // all white-space characters shall be ignored
48:         $data = preg_replace('/[\s]/', '', $data);
49:         // check for EOD character: GREATER-THAN SIGN (3Eh)
50:         $eod = strpos($data, '>');
51:         if ($eod !== false) {
52:             // remove EOD and extra data (if any)
53:             $data = substr($data, 0, $eod);
54:             $eod = true;
55:         }
56:         // get data length
57:         $data_length = strlen($data);
58:         if (($data_length % 2) != 0) {
59:             // odd number of hexadecimal digits
60:             if ($eod) {
61:                 // EOD shall behave as if a 0 (zero) followed the last digit
62:                 $data = substr($data, 0, -1).'0'.substr($data, -1);
63:             } else {
64:                 throw new PPException('invalid code');
65:             }
66:         }
67:         // check for invalid characters
68:         if (preg_match('/[^a-fA-F\d]/', $data) > 0) {
69:             throw new PPException('invalid code');
70:         }
71:         // get one byte of binary data for each pair of ASCII hexadecimal digits
72:         $decoded = pack('H*', $data);
73:         return $decoded;
74:     }
75: }
76: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy