Replacing strictEqual-assertion with a more useful QUnit.push that includes the expec...
[lhc/web/wiklou.git] / includes / MacBinary.php
1 <?php
2 /**
3 * MacBinary signature checker and data fork extractor, for files
4 * uploaded from Internet Explorer for Mac.
5 *
6 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
7 * Portions based on Convert::BinHex by Eryq et al
8 * http://www.mediawiki.org/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 class MacBinary {
29 function __construct( $filename ) {
30 $this->open( $filename );
31 $this->loadHeader();
32 }
33
34 private $valid, $version, $filename, $dataLength, $resourceLength, $handle;
35
36 /**
37 * The file must be seekable, such as local filesystem.
38 * Remote URLs probably won't work.
39 *
40 * @param $filename String
41 */
42 function open( $filename ) {
43 $this->valid = false;
44 $this->version = 0;
45 $this->filename = '';
46 $this->dataLength = 0;
47 $this->resourceLength = 0;
48 $this->handle = fopen( $filename, 'rb' );
49 }
50
51 /**
52 * Does this appear to be a valid MacBinary archive?
53 *
54 * @return Boolean
55 */
56 function isValid() {
57 return $this->valid;
58 }
59
60 /**
61 * Get length of data fork
62 *
63 * @return Integer
64 */
65 function dataForkLength() {
66 return $this->dataLength;
67 }
68
69 /**
70 * Copy the data fork to an external file or resource.
71 *
72 * @param $destination Ressource
73 * @return Boolean
74 */
75 function extractData( $destination ) {
76 if( !$this->isValid() ) {
77 return false;
78 }
79
80 // Data fork appears immediately after header
81 fseek( $this->handle, 128 );
82 return $this->copyBytesTo( $destination, $this->dataLength );
83 }
84
85 /**
86 *
87 */
88 function close() {
89 fclose( $this->handle );
90 }
91
92 // --------------------------------------------------------------
93
94 /**
95 * Check if the given file appears to be MacBinary-encoded,
96 * as Internet Explorer on Mac OS may provide for unknown types.
97 * http://www.lazerware.com/formats/macbinary/macbinary_iii.html
98 * If ok, load header data.
99 *
100 * @return bool
101 * @access private
102 */
103 function loadHeader() {
104 $fname = 'MacBinary::loadHeader';
105
106 fseek( $this->handle, 0 );
107 $head = fread( $this->handle, 128 );
108 #$this->hexdump( $head );
109
110 if( strlen( $head ) < 128 ) {
111 wfDebug( "$fname: couldn't read full MacBinary header\n" );
112 return false;
113 }
114
115 if( $head[0] != "\x00" || $head[74] != "\x00" ) {
116 wfDebug( "$fname: header bytes 0 and 74 not null\n" );
117 return false;
118 }
119
120 $signature = substr( $head, 102, 4 );
121 $a = unpack( "ncrc", substr( $head, 124, 2 ) );
122 $storedCRC = $a['crc'];
123 $calculatedCRC = $this->calcCRC( substr( $head, 0, 124 ) );
124 if( $storedCRC == $calculatedCRC ) {
125 if( $signature == 'mBIN' ) {
126 $this->version = 3;
127 } else {
128 $this->version = 2;
129 }
130 } else {
131 $crc = sprintf( "%x != %x", $storedCRC, $calculatedCRC );
132 if( $storedCRC == 0 && $head[82] == "\x00" &&
133 substr( $head, 101, 24 ) == str_repeat( "\x00", 24 ) ) {
134 wfDebug( "$fname: no CRC, looks like MacBinary I\n" );
135 $this->version = 1;
136 } elseif( $signature == 'mBIN' && $storedCRC == 0x185 ) {
137 // Mac IE 5.0 seems to insert this value in the CRC field.
138 // 5.2.3 works correctly; don't know about other versions.
139 wfDebug( "$fname: CRC doesn't match ($crc), looks like Mac IE 5.0\n" );
140 $this->version = 3;
141 } else {
142 wfDebug( "$fname: CRC doesn't match ($crc) and not MacBinary I\n" );
143 return false;
144 }
145 }
146
147 $nameLength = ord( $head[1] );
148 if( $nameLength < 1 || $nameLength > 63 ) {
149 wfDebug( "$fname: invalid filename size $nameLength\n" );
150 return false;
151 }
152 $this->filename = substr( $head, 2, $nameLength );
153
154 $forks = unpack( "Ndata/Nresource", substr( $head, 83, 8 ) );
155 $this->dataLength = $forks['data'];
156 $this->resourceLength = $forks['resource'];
157 $maxForkLength = 0x7fffff;
158
159 if( $this->dataLength < 0 || $this->dataLength > $maxForkLength ) {
160 wfDebug( "$fname: invalid data fork length $this->dataLength\n" );
161 return false;
162 }
163
164 if( $this->resourceLength < 0 || $this->resourceLength > $maxForkLength ) {
165 wfDebug( "$fname: invalid resource fork size $this->resourceLength\n" );
166 return false;
167 }
168
169 wfDebug( "$fname: appears to be MacBinary $this->version, data length $this->dataLength\n" );
170 $this->valid = true;
171 return true;
172 }
173
174 /**
175 * Calculate a 16-bit CRC value as for MacBinary headers.
176 * Adapted from perl5 Convert::BinHex by Eryq,
177 * based on the mcvert utility (Doug Moore, April '87),
178 * with magic array thingy by Jim Van Verth.
179 * http://search.cpan.org/~eryq/Convert-BinHex-1.119/lib/Convert/BinHex.pm
180 *
181 * @param $data String
182 * @param $seed Integer
183 * @return Integer
184 * @access private
185 */
186 function calcCRC( $data, $seed = 0 ) {
187 # An array useful for CRC calculations that use 0x1021 as the "seed":
188 $MAGIC = array(
189 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
190 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
191 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
192 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
193 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
194 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
195 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
196 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
197 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
198 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
199 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
200 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
201 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
202 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
203 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
204 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
205 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
206 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
207 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
208 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
209 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
210 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
211 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
212 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
213 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
214 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
215 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
216 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
217 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
218 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
219 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
220 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
221 );
222 $len = strlen( $data );
223 $crc = $seed;
224 for( $i = 0; $i < $len; $i++ ) {
225 $crc ^= ord( $data[$i] ) << 8;
226 $crc &= 0xFFFF;
227 $crc = ($crc << 8) ^ $MAGIC[$crc >> 8];
228 $crc &= 0xFFFF;
229 }
230 return $crc;
231 }
232
233 /**
234 * @param $destination Resource
235 * @param $bytesToCopy Integer
236 * @return Boolean
237 * @access private
238 */
239 function copyBytesTo( $destination, $bytesToCopy ) {
240 $bufferSize = 65536;
241 for( $remaining = $bytesToCopy; $remaining > 0; $remaining -= $bufferSize ) {
242 $thisChunkSize = min( $remaining, $bufferSize );
243 $buffer = fread( $this->handle, $thisChunkSize );
244 fwrite( $destination, $buffer );
245 }
246 }
247
248 /**
249 * Hex dump of the header for debugging
250 * @access private
251 */
252 function hexdump( $data ) {
253 global $wgDebugLogFile;
254 if( !$wgDebugLogFile ) return;
255
256 $width = 16;
257 $at = 0;
258 for( $remaining = strlen( $data ); $remaining > 0; $remaining -= $width ) {
259 $line = sprintf( "%04x:", $at );
260 $printable = '';
261 for( $i = 0; $i < $width && $remaining - $i > 0; $i++ ) {
262 $byte = ord( $data[$at++] );
263 $line .= sprintf( " %02x", $byte );
264 $printable .= ($byte >= 32 && $byte <= 126 )
265 ? chr( $byte )
266 : '.';
267 }
268 if( $i < $width ) {
269 $line .= str_repeat( ' ', $width - $i );
270 }
271 wfDebug( "MacBinary: $line $printable\n" );
272 }
273 }
274 }