Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / translation / Loader / MoFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Loader;
13
14 use Symfony\Component\Translation\Exception\InvalidResourceException;
15
16 /**
17  * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
18  */
19 class MoFileLoader extends FileLoader
20 {
21     /**
22      * Magic used for validating the format of a MO file as well as
23      * detecting if the machine used to create that file was little endian.
24      *
25      * @var float
26      */
27     const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
28
29     /**
30      * Magic used for validating the format of a MO file as well as
31      * detecting if the machine used to create that file was big endian.
32      *
33      * @var float
34      */
35     const MO_BIG_ENDIAN_MAGIC = 0xde120495;
36
37     /**
38      * The size of the header of a MO file in bytes.
39      *
40      * @var int Number of bytes
41      */
42     const MO_HEADER_SIZE = 28;
43
44     /**
45      * Parses machine object (MO) format, independent of the machine's endian it
46      * was created on. Both 32bit and 64bit systems are supported.
47      *
48      * {@inheritdoc}
49      */
50     protected function loadResource($resource)
51     {
52         $stream = fopen($resource, 'r');
53
54         $stat = fstat($stream);
55
56         if ($stat['size'] < self::MO_HEADER_SIZE) {
57             throw new InvalidResourceException('MO stream content has an invalid format.');
58         }
59         $magic = unpack('V1', fread($stream, 4));
60         $magic = hexdec(substr(dechex(current($magic)), -8));
61
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 InvalidResourceException('MO stream content has an invalid format.');
68         }
69
70         // formatRevision
71         $this->readLong($stream, $isBigEndian);
72         $count = $this->readLong($stream, $isBigEndian);
73         $offsetId = $this->readLong($stream, $isBigEndian);
74         $offsetTranslated = $this->readLong($stream, $isBigEndian);
75         // sizeHashes
76         $this->readLong($stream, $isBigEndian);
77         // offsetHashes
78         $this->readLong($stream, $isBigEndian);
79
80         $messages = array();
81
82         for ($i = 0; $i < $count; ++$i) {
83             $pluralId = null;
84             $translated = null;
85
86             fseek($stream, $offsetId + $i * 8);
87
88             $length = $this->readLong($stream, $isBigEndian);
89             $offset = $this->readLong($stream, $isBigEndian);
90
91             if ($length < 1) {
92                 continue;
93             }
94
95             fseek($stream, $offset);
96             $singularId = fread($stream, $length);
97
98             if (strpos($singularId, "\000") !== false) {
99                 list($singularId, $pluralId) = explode("\000", $singularId);
100             }
101
102             fseek($stream, $offsetTranslated + $i * 8);
103             $length = $this->readLong($stream, $isBigEndian);
104             $offset = $this->readLong($stream, $isBigEndian);
105
106             if ($length < 1) {
107                 continue;
108             }
109
110             fseek($stream, $offset);
111             $translated = fread($stream, $length);
112
113             if (strpos($translated, "\000") !== false) {
114                 $translated = explode("\000", $translated);
115             }
116
117             $ids = array('singular' => $singularId, 'plural' => $pluralId);
118             $item = compact('ids', 'translated');
119
120             if (is_array($item['translated'])) {
121                 $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
122                 if (isset($item['ids']['plural'])) {
123                     $plurals = array();
124                     foreach ($item['translated'] as $plural => $translated) {
125                         $plurals[] = sprintf('{%d} %s', $plural, $translated);
126                     }
127                     $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
128                 }
129             } elseif (!empty($item['ids']['singular'])) {
130                 $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
131             }
132         }
133
134         fclose($stream);
135
136         return array_filter($messages);
137     }
138
139     /**
140      * Reads an unsigned long from stream respecting endianness.
141      *
142      * @param resource $stream
143      * @param bool     $isBigEndian
144      *
145      * @return int
146      */
147     private function readLong($stream, $isBigEndian)
148     {
149         $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
150         $result = current($result);
151
152         return (int) substr($result, -8);
153     }
154 }