Ingen beskrivning

MoFileLoader.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Translation\Exception\InvalidResourceException;
  12. /**
  13. * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
  14. */
  15. class MoFileLoader extends FileLoader
  16. {
  17. /**
  18. * Magic used for validating the format of an MO file as well as
  19. * detecting if the machine used to create that file was little endian.
  20. */
  21. public const MO_LITTLE_ENDIAN_MAGIC = 0x950412DE;
  22. /**
  23. * Magic used for validating the format of an MO file as well as
  24. * detecting if the machine used to create that file was big endian.
  25. */
  26. public const MO_BIG_ENDIAN_MAGIC = 0xDE120495;
  27. /**
  28. * The size of the header of an MO file in bytes.
  29. */
  30. public const MO_HEADER_SIZE = 28;
  31. /**
  32. * Parses machine object (MO) format, independent of the machine's endian it
  33. * was created on. Both 32bit and 64bit systems are supported.
  34. *
  35. * {@inheritdoc}
  36. */
  37. protected function loadResource($resource)
  38. {
  39. $stream = fopen($resource, 'r');
  40. $stat = fstat($stream);
  41. if ($stat['size'] < self::MO_HEADER_SIZE) {
  42. throw new InvalidResourceException('MO stream content has an invalid format.');
  43. }
  44. $magic = unpack('V1', fread($stream, 4));
  45. $magic = hexdec(substr(dechex(current($magic)), -8));
  46. if (self::MO_LITTLE_ENDIAN_MAGIC == $magic) {
  47. $isBigEndian = false;
  48. } elseif (self::MO_BIG_ENDIAN_MAGIC == $magic) {
  49. $isBigEndian = true;
  50. } else {
  51. throw new InvalidResourceException('MO stream content has an invalid format.');
  52. }
  53. // formatRevision
  54. $this->readLong($stream, $isBigEndian);
  55. $count = $this->readLong($stream, $isBigEndian);
  56. $offsetId = $this->readLong($stream, $isBigEndian);
  57. $offsetTranslated = $this->readLong($stream, $isBigEndian);
  58. // sizeHashes
  59. $this->readLong($stream, $isBigEndian);
  60. // offsetHashes
  61. $this->readLong($stream, $isBigEndian);
  62. $messages = [];
  63. for ($i = 0; $i < $count; ++$i) {
  64. $pluralId = null;
  65. $translated = null;
  66. fseek($stream, $offsetId + $i * 8);
  67. $length = $this->readLong($stream, $isBigEndian);
  68. $offset = $this->readLong($stream, $isBigEndian);
  69. if ($length < 1) {
  70. continue;
  71. }
  72. fseek($stream, $offset);
  73. $singularId = fread($stream, $length);
  74. if (str_contains($singularId, "\000")) {
  75. [$singularId, $pluralId] = explode("\000", $singularId);
  76. }
  77. fseek($stream, $offsetTranslated + $i * 8);
  78. $length = $this->readLong($stream, $isBigEndian);
  79. $offset = $this->readLong($stream, $isBigEndian);
  80. if ($length < 1) {
  81. continue;
  82. }
  83. fseek($stream, $offset);
  84. $translated = fread($stream, $length);
  85. if (str_contains($translated, "\000")) {
  86. $translated = explode("\000", $translated);
  87. }
  88. $ids = ['singular' => $singularId, 'plural' => $pluralId];
  89. $item = compact('ids', 'translated');
  90. if (!empty($item['ids']['singular'])) {
  91. $id = $item['ids']['singular'];
  92. if (isset($item['ids']['plural'])) {
  93. $id .= '|'.$item['ids']['plural'];
  94. }
  95. $messages[$id] = stripcslashes(implode('|', (array) $item['translated']));
  96. }
  97. }
  98. fclose($stream);
  99. return array_filter($messages);
  100. }
  101. /**
  102. * Reads an unsigned long from stream respecting endianness.
  103. *
  104. * @param resource $stream
  105. */
  106. private function readLong($stream, bool $isBigEndian): int
  107. {
  108. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  109. $result = current($result);
  110. return (int) substr($result, -8);
  111. }
  112. }