AsciiHex.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfParser\Filter;
  10. /**
  11. * Class for handling ASCII hexadecimal encoded data
  12. */
  13. class AsciiHex implements FilterInterface
  14. {
  15. /**
  16. * Converts an ASCII hexadecimal encoded string into its binary representation.
  17. *
  18. * @param string $data The input string
  19. * @return string
  20. */
  21. public function decode($data)
  22. {
  23. $data = \preg_replace('/[^0-9A-Fa-f]/', '', \rtrim($data, '>'));
  24. if ((\strlen($data) % 2) === 1) {
  25. $data .= '0';
  26. }
  27. return \pack('H*', $data);
  28. }
  29. /**
  30. * Converts a string into ASCII hexadecimal representation.
  31. *
  32. * @param string $data The input string
  33. * @param boolean $leaveEOD
  34. * @return string
  35. */
  36. public function encode($data, $leaveEOD = false)
  37. {
  38. $t = \unpack('H*', $data);
  39. return \current($t)
  40. . ($leaveEOD ? '' : '>');
  41. }
  42. }