PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/application/third_party/tcpdf/tcpdf_filters.php

https://bitbucket.org/machaven/limesurvey
PHP | 479 lines | 235 code | 22 blank | 222 comment | 41 complexity | 56b192de669db4fd52f7b53d50723ea8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf_filters.php
  4. // Version : 1.0.000
  5. // Begin : 2011-05-23
  6. // Last Update : 2011-06-26
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3 + YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE GENERATED PDF DOCUMENTS.
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2011-2011 Nicola Asuni - Tecnick.com S.r.l.
  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. Additionally,
  18. // YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE
  19. // GENERATED PDF DOCUMENTS.
  20. //
  21. // TCPDF is distributed in the hope that it will be useful, but
  22. // WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. // See the GNU Lesser General Public License for more details.
  25. //
  26. // You should have received a copy of the License
  27. // along with TCPDF. If not, see
  28. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  29. //
  30. // See LICENSE.TXT file for more information.
  31. // -------------------------------------------------------------------
  32. //
  33. // Description : This is a PHP class for decoding common PDF filters (PDF 32000-2008 - 7.4 Filters).
  34. //
  35. //============================================================+
  36. /**
  37. * @file
  38. * This is a PHP class for decoding common PDF filters (PDF 32000-2008 - 7.4 Filters).<br>
  39. * @package com.tecnick.tcpdf
  40. * @author Nicola Asuni
  41. * @version 1.0.000
  42. */
  43. /**
  44. * @class TCPDF_FILTERS
  45. * This is a PHP class for decoding common PDF filters (PDF 32000-2008 - 7.4 Filters).<br>
  46. * @package com.tecnick.tcpdf
  47. * @brief This is a PHP class for decoding common PDF filters.
  48. * @version 1.0.000
  49. * @author Nicola Asuni - info@tecnick.com
  50. */
  51. class TCPDF_FILTERS {
  52. /**
  53. * Define a list of available filter decoders.
  54. * @private
  55. */
  56. private $available_filters = array('ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode');
  57. // -----------------------------------------------------------------------------
  58. /**
  59. * Get a list of available decoding filters.
  60. * @return (array) Array of available filter decoders.
  61. * @public
  62. * @since 1.0.000 (2011-05-23)
  63. */
  64. public function getAvailableFilters() {
  65. return $this->available_filters;
  66. }
  67. /**
  68. * Decode data using the specified filter type.
  69. * @param $filter (string) Filter name.
  70. * @param $data (string) Data to decode.
  71. * @return Decoded data string.
  72. * @public
  73. * @since 1.0.000 (2011-05-23)
  74. */
  75. public function decodeFilter($filter, $data) {
  76. switch ($filter) {
  77. case 'ASCIIHexDecode': {
  78. return $this->decodeFilterASCIIHexDecode($data);
  79. break;
  80. }
  81. case 'ASCII85Decode': {
  82. return $this->decodeFilterASCII85Decode($data);
  83. break;
  84. }
  85. case 'LZWDecode': {
  86. return $this->decodeFilterLZWDecode($data);
  87. break;
  88. }
  89. case 'FlateDecode': {
  90. return $this->decodeFilterFlateDecode($data);
  91. break;
  92. }
  93. case 'RunLengthDecode': {
  94. return $this->decodeFilterRunLengthDecode($data);
  95. break;
  96. }
  97. case 'CCITTFaxDecode': {
  98. return $this->decodeFilterCCITTFaxDecode($data);
  99. break;
  100. }
  101. case 'JBIG2Decode': {
  102. return $this->decodeFilterJBIG2Decode($data);
  103. break;
  104. }
  105. case 'DCTDecode': {
  106. return $this->decodeFilterDCTDecode($data);
  107. break;
  108. }
  109. case 'JPXDecode': {
  110. return $this->decodeFilterJPXDecode($data);
  111. break;
  112. }
  113. case 'Crypt': {
  114. return $this->decodeFilterCrypt($data);
  115. break;
  116. }
  117. default: {
  118. return decodeFilterStandard($data);
  119. break;
  120. }
  121. }
  122. }
  123. // --- FILTERS (PDF 32000-2008 - 7.4 Filters) ------------------------------
  124. /**
  125. * Standard
  126. * Default decoding filter (leaves data unchanged).
  127. * @param $data (string) Data to decode.
  128. * @return Decoded data string.
  129. * @public
  130. * @since 1.0.000 (2011-05-23)
  131. */
  132. public function decodeFilterStandard($data) {
  133. return $data;
  134. }
  135. /**
  136. * ASCIIHexDecode
  137. * Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data.
  138. * @param $data (string) Data to decode.
  139. * @return Decoded data string.
  140. * @public
  141. * @since 1.0.000 (2011-05-23)
  142. */
  143. public function decodeFilterASCIIHexDecode($data) {
  144. // intialize string to return
  145. $decoded = '';
  146. // all white-space characters shall be ignored
  147. $data = preg_replace('/[\s]/', '', $data);
  148. // check for EOD character: GREATER-THAN SIGN (3Eh)
  149. $eod = strpos($data, '>');
  150. if ($eod !== false) {
  151. // remove EOD and extra data (if any)
  152. $data = substr($data, 0, $eod);
  153. $eod = true;
  154. }
  155. // get data length
  156. $data_length = strlen($data);
  157. if (($data_length % 2) != 0) {
  158. // odd number of hexadecimal digits
  159. if ($eod) {
  160. // EOD shall behave as if a 0 (zero) followed the last digit
  161. $data = substr($data, 0, -1).'0'.substr($data, -1);
  162. } else {
  163. $this->Error('decodeASCIIHex: invalid code');
  164. }
  165. }
  166. // check for invalid characters
  167. if (preg_match('/[^a-fA-F\d]/', $data) > 0) {
  168. $this->Error('decodeASCIIHex: invalid code');
  169. }
  170. // get one byte of binary data for each pair of ASCII hexadecimal digits
  171. $decoded = pack('H*', $data);
  172. return $decoded;
  173. }
  174. /**
  175. * ASCII85Decode
  176. * Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data.
  177. * @param $data (string) Data to decode.
  178. * @return Decoded data string.
  179. * @public
  180. * @since 1.0.000 (2011-05-23)
  181. */
  182. public function decodeFilterASCII85Decode($data) {
  183. // intialize string to return
  184. $decoded = '';
  185. // all white-space characters shall be ignored
  186. $data = preg_replace('/[\s]/', '', $data);
  187. // remove start sequence 2-character sequence <~ (3Ch)(7Eh)
  188. if (strpos($data, '<~') !== false) {
  189. // remove EOD and extra data (if any)
  190. $data = substr($data, 2);
  191. }
  192. // check for EOD: 2-character sequence ~> (7Eh)(3Eh)
  193. $eod = strpos($data, '~>');
  194. if ($eod !== false) {
  195. // remove EOD and extra data (if any)
  196. $data = substr($data, 0, $eod);
  197. }
  198. // data length
  199. $data_length = strlen($data);
  200. // check for invalid characters
  201. if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) {
  202. $this->Error('decodeASCII85: invalid code');
  203. }
  204. // z sequence
  205. $zseq = chr(0).chr(0).chr(0).chr(0);
  206. // position inside a group of 4 bytes (0-3)
  207. $group_pos = 0;
  208. $tuple = 0;
  209. $pow85 = array((85*85*85*85), (85*85*85), (85*85), 85, 1);
  210. $last_pos = ($data_length - 1);
  211. // for each byte
  212. for ($i = 0; $i < $data_length; ++$i) {
  213. // get char value
  214. $char = ord($data[$i]);
  215. if ($char == 122) { // 'z'
  216. if ($group_pos == 0) {
  217. $decoded .= $zseq;
  218. } else {
  219. $this->Error('decodeASCII85: invalid code');
  220. }
  221. } else {
  222. // the value represented by a group of 5 characters should never be greater than 2^32 - 1
  223. $tuple += (($char - 33) * $pow85[$group_pos]);
  224. if ($group_pos == 4) {
  225. $decoded .= chr($tuple >> 24).chr($tuple >> 16).chr($tuple >> 8).chr($tuple);
  226. $tuple = 0;
  227. $group_pos = 0;
  228. } else {
  229. ++$group_pos;
  230. }
  231. }
  232. }
  233. if ($group_pos > 1) {
  234. $tuple += $pow85[($group_pos - 1)];
  235. }
  236. // last tuple (if any)
  237. switch ($group_pos) {
  238. case 4: {
  239. $decoded .= chr($tuple >> 24).chr($tuple >> 16).chr($tuple >> 8);
  240. break;
  241. }
  242. case 3: {
  243. $decoded .= chr($tuple >> 24).chr($tuple >> 16);
  244. break;
  245. }
  246. case 2: {
  247. $decoded .= chr($tuple >> 24);
  248. break;
  249. }
  250. case 1: {
  251. $this->Error('decodeASCII85: invalid code');
  252. break;
  253. }
  254. }
  255. return $decoded;
  256. }
  257. /**
  258. * LZWDecode
  259. * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data.
  260. * @param $data (string) Data to decode.
  261. * @return Decoded data string.
  262. * @public
  263. * @since 1.0.000 (2011-05-23)
  264. */
  265. public function decodeFilterLZWDecode($data) {
  266. // intialize string to return
  267. $decoded = '';
  268. // data length
  269. $data_length = strlen($data);
  270. // convert string to binary string
  271. $bitstring = '';
  272. for ($i = 0; $i < $data_length; ++$i) {
  273. $bitstring .= sprintf('%08b', ord($data{$i}));
  274. }
  275. // get the number of bits
  276. $data_length = strlen($bitstring);
  277. // initialize code length in bits
  278. $bitlen = 9;
  279. // initialize dictionary index
  280. $dix = 258;
  281. // initialize the dictionary (with the first 256 entries).
  282. $dictionary = array();
  283. for ($i = 0; $i < 256; ++$i) {
  284. $dictionary[$i] = chr($i);
  285. }
  286. // previous val
  287. $prev_index = 0;
  288. // while we encounter EOD marker (257), read code_length bits
  289. while (($data_length > 0) AND (($index = bindec(substr($bitstring, 0, $bitlen))) != 257)) {
  290. // remove read bits from string
  291. $bitstring = substr($bitstring, $bitlen);
  292. // update number of bits
  293. $data_length -= $bitlen;
  294. if ($index == 256) { // clear-table marker
  295. // reset code length in bits
  296. $bitlen = 9;
  297. // reset dictionary index
  298. $dix = 258;
  299. $prev_index = 256;
  300. // reset the dictionary (with the first 256 entries).
  301. $dictionary = array();
  302. for ($i = 0; $i < 256; ++$i) {
  303. $dictionary[$i] = chr($i);
  304. }
  305. } elseif ($prev_index == 256) {
  306. // first entry
  307. $decoded .= $dictionary[$index];
  308. $prev_index = $index;
  309. } else {
  310. // check if index exist in the dictionary
  311. if ($index < $dix) {
  312. // index exist on dictionary
  313. $decoded .= $dictionary[$index];
  314. $dic_val = $dictionary[$prev_index].$dictionary[$index]{0};
  315. // store current index
  316. $prev_index = $index;
  317. } else {
  318. // index do not exist on dictionary
  319. $dic_val = $dictionary[$prev_index].$dictionary[$prev_index]{0};
  320. $decoded .= $dic_val;
  321. }
  322. // update dictionary
  323. $dictionary[$dix] = $dic_val;
  324. ++$dix;
  325. // change bit length by case
  326. if ($dix == 2047) {
  327. $bitlen = 12;
  328. } elseif ($dix == 1023) {
  329. $bitlen = 11;
  330. } elseif ($dix == 511) {
  331. $bitlen = 10;
  332. }
  333. }
  334. }
  335. return $decoded;
  336. }
  337. /**
  338. * FlateDecode
  339. * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data.
  340. * @param $data (string) Data to decode.
  341. * @return Decoded data string.
  342. * @public
  343. * @since 1.0.000 (2011-05-23)
  344. */
  345. public function decodeFilterFlateDecode($data) {
  346. // intialize string to return
  347. $decoded = gzuncompress($data);
  348. if ($decoded === false) {
  349. $this->Error('decodeFlate: invalid code');
  350. }
  351. return $decoded;
  352. }
  353. /**
  354. * RunLengthDecode
  355. * Decompresses data encoded using a byte-oriented run-length encoding algorithm.
  356. * @param $data (string) Data to decode.
  357. * @public
  358. * @since 1.0.000 (2011-05-23)
  359. */
  360. public function decodeFilterRunLengthDecode($data) {
  361. // intialize string to return
  362. $decoded = '';
  363. // data length
  364. $data_length = strlen($data);
  365. $i = 0;
  366. while($i < $data_length) {
  367. // get current byte value
  368. $byte = ord($data{$i});
  369. if ($byte == 128) {
  370. // a length value of 128 denote EOD
  371. break;
  372. } elseif ($byte < 128) {
  373. // if the length byte is in the range 0 to 127
  374. // the following length + 1 (1 to 128) bytes shall be copied literally during decompression
  375. $decoded .= substr($data, ($i + 1), ($byte + 1));
  376. // move to next block
  377. $i += ($byte + 2);
  378. } else {
  379. // if length is in the range 129 to 255,
  380. // the following single byte shall be copied 257 - length (2 to 128) times during decompression
  381. $decoded .= str_repeat($data{($i + 1)}, (257 - $byte));
  382. // move to next block
  383. $i += 2;
  384. }
  385. }
  386. return $decoded;
  387. }
  388. /**
  389. * CCITTFaxDecode (NOT IMPLEMETED)
  390. * Decompresses data encoded using the CCITT facsimile standard, reproducing the original data (typically monochrome image data at 1 bit per pixel).
  391. * @param $data (string) Data to decode.
  392. * @return Decoded data string.
  393. * @public
  394. * @since 1.0.000 (2011-05-23)
  395. */
  396. public function decodeFilterCCITTFaxDecode($data) {
  397. return $data;
  398. }
  399. /**
  400. * JBIG2Decode (NOT IMPLEMETED)
  401. * Decompresses data encoded using the JBIG2 standard, reproducing the original monochrome (1 bit per pixel) image data (or an approximation of that data).
  402. * @param $data (string) Data to decode.
  403. * @return Decoded data string.
  404. * @public
  405. * @since 1.0.000 (2011-05-23)
  406. */
  407. public function decodeFilterJBIG2Decode($data) {
  408. return $data;
  409. }
  410. /**
  411. * DCTDecode (NOT IMPLEMETED)
  412. * Decompresses data encoded using a DCT (discrete cosine transform) technique based on the JPEG standard, reproducing image sample data that approximates the original data.
  413. * @param $data (string) Data to decode.
  414. * @return Decoded data string.
  415. * @public
  416. * @since 1.0.000 (2011-05-23)
  417. */
  418. public function decodeFilterDCTDecode($data) {
  419. return $data;
  420. }
  421. /**
  422. * JPXDecode (NOT IMPLEMETED)
  423. * Decompresses data encoded using the wavelet-based JPEG2000 standard, reproducing the original image data.
  424. * @param $data (string) Data to decode.
  425. * @return Decoded data string.
  426. * @public
  427. * @since 1.0.000 (2011-05-23)
  428. */
  429. public function decodeFilterJPXDecode($data) {
  430. return $data;
  431. }
  432. /**
  433. * Crypt (NOT IMPLEMETED)
  434. * Decrypts data encrypted by a security handler, reproducing the data as it was before encryption.
  435. * @param $data (string) Data to decode.
  436. * @return Decoded data string.
  437. * @public
  438. * @since 1.0.000 (2011-05-23)
  439. */
  440. public function decodeFilterCrypt($data) {
  441. return $data;
  442. }
  443. // --- END FILTERS SECTION -------------------------------------------------
  444. /**
  445. * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution.
  446. * @param $msg (string) The error message
  447. * @public
  448. * @since 1.0.000 (2011-05-23)
  449. */
  450. public function Error($msg) {
  451. // exit program and print error
  452. die('<strong>TCPDF_FILTERS ERROR: </strong>'.$msg);
  453. }
  454. } // END OF TCPDF_FILTERS CLASS
  455. //============================================================+
  456. // END OF FILE
  457. //============================================================+