MoFileParser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\I18n\Parser;
  16. use RuntimeException;
  17. /**
  18. * Parses file in MO format
  19. *
  20. * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
  21. * @copyright Copyright (c) 2014, Fabien Potencier https://github.com/symfony/Translation/blob/master/LICENSE
  22. */
  23. class MoFileParser
  24. {
  25. /**
  26. * Magic used for validating the format of a MO file as well as
  27. * detecting if the machine used to create that file was little endian.
  28. *
  29. * @var float
  30. */
  31. const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
  32. /**
  33. * Magic used for validating the format of a MO file as well as
  34. * detecting if the machine used to create that file was big endian.
  35. *
  36. * @var float
  37. */
  38. const MO_BIG_ENDIAN_MAGIC = 0xde120495;
  39. /**
  40. * The size of the header of a MO file in bytes.
  41. *
  42. * @var int
  43. */
  44. const MO_HEADER_SIZE = 28;
  45. /**
  46. * Parses machine object (MO) format, independent of the machine's endian it
  47. * was created on. Both 32bit and 64bit systems are supported.
  48. *
  49. * @param resource $resource The file to be parsed.
  50. * @return array List of messages extracted from the file
  51. * @throws \RuntimeException If stream content has an invalid format.
  52. */
  53. public function parse($resource)
  54. {
  55. $stream = fopen($resource, 'rb');
  56. $stat = fstat($stream);
  57. if ($stat['size'] < self::MO_HEADER_SIZE) {
  58. throw new RuntimeException('Invalid format for MO translations file');
  59. }
  60. $magic = unpack('V1', fread($stream, 4));
  61. $magic = hexdec(substr(dechex(current($magic)), -8));
  62. if ($magic === self::MO_LITTLE_ENDIAN_MAGIC) {
  63. $isBigEndian = false;
  64. } elseif ($magic === self::MO_BIG_ENDIAN_MAGIC) {
  65. $isBigEndian = true;
  66. } else {
  67. throw new RuntimeException('Invalid format for MO translations file');
  68. }
  69. // offset formatRevision
  70. fread($stream, 4);
  71. $count = $this->_readLong($stream, $isBigEndian);
  72. $offsetId = $this->_readLong($stream, $isBigEndian);
  73. $offsetTranslated = $this->_readLong($stream, $isBigEndian);
  74. // Offset to start of translations
  75. fread($stream, 8);
  76. $messages = [];
  77. for ($i = 0; $i < $count; $i++) {
  78. $pluralId = null;
  79. $context = null;
  80. $plurals = null;
  81. fseek($stream, $offsetId + $i * 8);
  82. $length = $this->_readLong($stream, $isBigEndian);
  83. $offset = $this->_readLong($stream, $isBigEndian);
  84. if ($length < 1) {
  85. continue;
  86. }
  87. fseek($stream, $offset);
  88. $singularId = fread($stream, $length);
  89. if (strpos($singularId, "\x04") !== false) {
  90. list($context, $singularId) = explode("\x04", $singularId);
  91. }
  92. if (strpos($singularId, "\000") !== false) {
  93. list($singularId, $pluralId) = explode("\000", $singularId);
  94. }
  95. fseek($stream, $offsetTranslated + $i * 8);
  96. $length = $this->_readLong($stream, $isBigEndian);
  97. $offset = $this->_readLong($stream, $isBigEndian);
  98. fseek($stream, $offset);
  99. $translated = fread($stream, $length);
  100. if ($pluralId !== null || strpos($translated, "\000") !== false) {
  101. $translated = explode("\000", $translated);
  102. $plurals = $pluralId !== null ? $translated : null;
  103. $translated = $translated[0];
  104. }
  105. $singular = $translated;
  106. if ($context !== null) {
  107. $messages[$singularId]['_context'][$context] = $singular;
  108. if ($pluralId !== null) {
  109. $messages[$pluralId]['_context'][$context] = $plurals;
  110. }
  111. continue;
  112. }
  113. $messages[$singularId]['_context'][''] = $singular;
  114. if ($pluralId !== null) {
  115. $messages[$pluralId]['_context'][''] = $plurals;
  116. }
  117. }
  118. fclose($stream);
  119. return $messages;
  120. }
  121. /**
  122. * Reads an unsigned long from stream respecting endianess.
  123. *
  124. * @param resource $stream The File being read.
  125. * @param bool $isBigEndian Whether or not the current platform is Big Endian
  126. * @return int
  127. */
  128. protected function _readLong($stream, $isBigEndian)
  129. {
  130. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  131. $result = current($result);
  132. return (int)substr((string)$result, -8);
  133. }
  134. }