MoFileLoader.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. protected function loadResource(string $resource): array
  36. {
  37. $stream = fopen($resource, 'r');
  38. $stat = fstat($stream);
  39. if ($stat['size'] < self::MO_HEADER_SIZE) {
  40. throw new InvalidResourceException('MO stream content has an invalid format.');
  41. }
  42. $magic = unpack('V1', fread($stream, 4));
  43. $magic = hexdec(substr(dechex(current($magic)), -8));
  44. if (self::MO_LITTLE_ENDIAN_MAGIC == $magic) {
  45. $isBigEndian = false;
  46. } elseif (self::MO_BIG_ENDIAN_MAGIC == $magic) {
  47. $isBigEndian = true;
  48. } else {
  49. throw new InvalidResourceException('MO stream content has an invalid format.');
  50. }
  51. // formatRevision
  52. $this->readLong($stream, $isBigEndian);
  53. $count = $this->readLong($stream, $isBigEndian);
  54. $offsetId = $this->readLong($stream, $isBigEndian);
  55. $offsetTranslated = $this->readLong($stream, $isBigEndian);
  56. // sizeHashes
  57. $this->readLong($stream, $isBigEndian);
  58. // offsetHashes
  59. $this->readLong($stream, $isBigEndian);
  60. $messages = [];
  61. for ($i = 0; $i < $count; ++$i) {
  62. $pluralId = null;
  63. $translated = null;
  64. fseek($stream, $offsetId + $i * 8);
  65. $length = $this->readLong($stream, $isBigEndian);
  66. $offset = $this->readLong($stream, $isBigEndian);
  67. if ($length < 1) {
  68. continue;
  69. }
  70. fseek($stream, $offset);
  71. $singularId = fread($stream, $length);
  72. if (str_contains($singularId, "\000")) {
  73. [$singularId, $pluralId] = explode("\000", $singularId);
  74. }
  75. fseek($stream, $offsetTranslated + $i * 8);
  76. $length = $this->readLong($stream, $isBigEndian);
  77. $offset = $this->readLong($stream, $isBigEndian);
  78. if ($length < 1) {
  79. continue;
  80. }
  81. fseek($stream, $offset);
  82. $translated = fread($stream, $length);
  83. if (str_contains($translated, "\000")) {
  84. $translated = explode("\000", $translated);
  85. }
  86. $ids = ['singular' => $singularId, 'plural' => $pluralId];
  87. $item = compact('ids', 'translated');
  88. if (!empty($item['ids']['singular'])) {
  89. $id = $item['ids']['singular'];
  90. if (isset($item['ids']['plural'])) {
  91. $id .= '|'.$item['ids']['plural'];
  92. }
  93. $messages[$id] = stripcslashes(implode('|', (array) $item['translated']));
  94. }
  95. }
  96. fclose($stream);
  97. return array_filter($messages);
  98. }
  99. /**
  100. * Reads an unsigned long from stream respecting endianness.
  101. *
  102. * @param resource $stream
  103. */
  104. private function readLong($stream, bool $isBigEndian): int
  105. {
  106. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  107. $result = current($result);
  108. return (int) substr($result, -8);
  109. }
  110. }