MoFileLoader.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 a MO file as well as
  19. * detecting if the machine used to create that file was little endian.
  20. */
  21. const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
  22. /**
  23. * Magic used for validating the format of a MO file as well as
  24. * detecting if the machine used to create that file was big endian.
  25. */
  26. const MO_BIG_ENDIAN_MAGIC = 0xde120495;
  27. /**
  28. * The size of the header of a MO file in bytes.
  29. */
  30. 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 = array();
  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 (false !== strpos($singularId, "\000")) {
  75. list($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 (false !== strpos($translated, "\000")) {
  86. $translated = explode("\000", $translated);
  87. }
  88. $ids = array('singular' => $singularId, 'plural' => $pluralId);
  89. $item = compact('ids', 'translated');
  90. if (is_array($item['translated'])) {
  91. $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
  92. if (isset($item['ids']['plural'])) {
  93. $plurals = array();
  94. foreach ($item['translated'] as $plural => $translated) {
  95. $plurals[] = sprintf('{%d} %s', $plural, $translated);
  96. }
  97. $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
  98. }
  99. } elseif (!empty($item['ids']['singular'])) {
  100. $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
  101. }
  102. }
  103. fclose($stream);
  104. return array_filter($messages);
  105. }
  106. /**
  107. * Reads an unsigned long from stream respecting endianness.
  108. *
  109. * @param resource $stream
  110. */
  111. private function readLong($stream, bool $isBigEndian): int
  112. {
  113. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  114. $result = current($result);
  115. return (int) substr($result, -8);
  116. }
  117. }