| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class MoFileLoader extends FileLoader |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public const MO_LITTLE_ENDIAN_MAGIC = 0x950412DE; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public const MO_BIG_ENDIAN_MAGIC = 0xDE120495; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public const MO_HEADER_SIZE = 28; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected function loadResource(string $resource): array |
| 43 |
{ |
| 44 |
$stream = fopen($resource, 'r'); |
| 45 |
|
| 46 |
$stat = fstat($stream); |
| 47 |
|
| 48 |
if ($stat['size'] < self::MO_HEADER_SIZE) { |
| 49 |
throw new InvalidResourceException('MO stream content has an invalid format.'); |
| 50 |
} |
| 51 |
$magic = unpack('V1', fread($stream, 4)); |
| 52 |
$magic = hexdec(substr(dechex(current($magic)), -8)); |
| 53 |
|
| 54 |
if (self::MO_LITTLE_ENDIAN_MAGIC == $magic) { |
| 55 |
$isBigEndian = false; |
| 56 |
} elseif (self::MO_BIG_ENDIAN_MAGIC == $magic) { |
| 57 |
$isBigEndian = true; |
| 58 |
} else { |
| 59 |
throw new InvalidResourceException('MO stream content has an invalid format.'); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
$this->readLong($stream, $isBigEndian); |
| 64 |
$count = $this->readLong($stream, $isBigEndian); |
| 65 |
$offsetId = $this->readLong($stream, $isBigEndian); |
| 66 |
$offsetTranslated = $this->readLong($stream, $isBigEndian); |
| 67 |
|
| 68 |
$this->readLong($stream, $isBigEndian); |
| 69 |
|
| 70 |
$this->readLong($stream, $isBigEndian); |
| 71 |
|
| 72 |
$messages = []; |
| 73 |
|
| 74 |
for ($i = 0; $i < $count; ++$i) { |
| 75 |
$pluralId = null; |
| 76 |
$translated = null; |
| 77 |
|
| 78 |
fseek($stream, $offsetId + $i * 8); |
| 79 |
|
| 80 |
$length = $this->readLong($stream, $isBigEndian); |
| 81 |
$offset = $this->readLong($stream, $isBigEndian); |
| 82 |
|
| 83 |
if ($length < 1) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
fseek($stream, $offset); |
| 88 |
$singularId = fread($stream, $length); |
| 89 |
|
| 90 |
if (str_contains($singularId, "\000")) { |
| 91 |
[$singularId, $pluralId] = explode("\000", $singularId); |
| 92 |
} |
| 93 |
|
| 94 |
fseek($stream, $offsetTranslated + $i * 8); |
| 95 |
$length = $this->readLong($stream, $isBigEndian); |
| 96 |
$offset = $this->readLong($stream, $isBigEndian); |
| 97 |
|
| 98 |
if ($length < 1) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
fseek($stream, $offset); |
| 103 |
$translated = fread($stream, $length); |
| 104 |
|
| 105 |
if (str_contains($translated, "\000")) { |
| 106 |
$translated = explode("\000", $translated); |
| 107 |
} |
| 108 |
|
| 109 |
$ids = ['singular' => $singularId, 'plural' => $pluralId]; |
| 110 |
$item = compact('ids', 'translated'); |
| 111 |
|
| 112 |
if (!empty($item['ids']['singular'])) { |
| 113 |
$id = $item['ids']['singular']; |
| 114 |
if (isset($item['ids']['plural'])) { |
| 115 |
$id .= '|'.$item['ids']['plural']; |
| 116 |
} |
| 117 |
$messages[$id] = stripcslashes(implode('|', (array) $item['translated'])); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
fclose($stream); |
| 122 |
|
| 123 |
return array_filter($messages); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private function readLong($stream, bool $isBigEndian): int |
| 132 |
{ |
| 133 |
$result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4)); |
| 134 |
$result = current($result); |
| 135 |
|
| 136 |
return (int) substr($result, -8); |
| 137 |
} |
| 138 |
} |