@@ -223,53 +223,55 @@ public function currentEntry() {
223223
224224 // AES vs. traditional PKZIP cipher
225225 if (99 === $ header ['compression ' ]) {
226- if (null === $ this ->password ) {
227- throw new IllegalAccessException ('No password set ' );
228- }
229-
226+ $ stream = new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ]);
230227 $ aes = unpack ('vheader/vsize/vversion/a2vendor/cstrength/vcompression ' , $ extra );
231- switch ($ aes ['strength ' ]) {
232- case 1 : $ sl = 8 ; $ dl = 16 ; break ;
233- case 2 : $ sl = 12 ; $ dl = 24 ; break ;
234- case 3 : $ sl = 16 ; $ dl = 32 ; break ;
235- default : throw new IllegalArgumentException ('Invalid AES strength ' .$ aes ['strength ' ]);
236- }
228+ $ header ['compression ' ]= $ aes ['compression ' ];
229+ $ is = function () use ($ header , $ stream , $ aes ) {
230+ if (null === $ this ->password ) {
231+ throw new IllegalAccessException ('No password set ' );
232+ }
237233
238- // Verify password
239- $ salt = $ this ->streamRead ($ sl );
240- $ pvv = $ this ->streamRead (2 );
241- $ dk = hash_pbkdf2 ('sha1 ' , $ this ->password ->reveal (), $ salt , 1000 , 2 * $ dl + 2 , true );
242- if (0 !== substr_compare ($ dk , $ pvv , 2 * $ dl , 2 )) {
243- throw new IllegalAccessException ('The password did not match ' );
244- }
234+ switch ($ aes ['strength ' ]) {
235+ case 1 : $ sl = 8 ; $ dl = 16 ; break ;
236+ case 2 : $ sl = 12 ; $ dl = 24 ; break ;
237+ case 3 : $ sl = 16 ; $ dl = 32 ; break ;
238+ default : throw new IllegalArgumentException ('Invalid AES strength ' .$ aes ['strength ' ]);
239+ }
245240
246- $ this ->skip -= $ sl + 2 ;
247- $ header ['compression ' ]= $ aes ['compression ' ];
248- $ is = new AESInputStream (
249- new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ] - $ sl - 2 ),
250- substr ($ dk , 0 , $ dl ),
251- substr ($ dk , $ dl , $ dl )
252- );
241+ // Verify password
242+ $ salt = $ stream ->read ($ sl );
243+ $ pvv = $ stream ->read (2 );
244+ $ dk = hash_pbkdf2 ('sha1 ' , $ this ->password ->reveal (), $ salt , 1000 , 2 * $ dl + 2 , true );
245+ if (0 !== substr_compare ($ dk , $ pvv , 2 * $ dl , 2 )) {
246+ throw new IllegalAccessException ('The password did not match ' );
247+ }
248+
249+ return new AESInputStream (
250+ $ stream ,
251+ substr ($ dk , 0 , $ dl ),
252+ substr ($ dk , $ dl , $ dl )
253+ );
254+ };
253255 } else if ($ header ['flags ' ] & 1 ) {
254- if (null === $ this ->password ) {
255- throw new IllegalAccessException ('No password set ' );
256- }
256+ $ stream = new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ]);
257+ $ is = function () use ($ header , $ stream ) {
258+ if (null === $ this ->password ) {
259+ throw new IllegalAccessException ('No password set ' );
260+ }
257261
258- // Verify password
259- $ cipher = new ZipCipher ();
260- $ cipher ->initialize (iconv (\xp::ENCODING , 'cp437 ' , $ this ->password ->reveal ()));
261- $ preamble = $ cipher ->decipher ($ this ->streamRead (12 ));
262- if (ord ($ preamble [11 ]) !== (($ header ['crc ' ] >> 24 ) & 0xff )) {
263- throw new IllegalAccessException ('The password did not match ' );
264- }
265-
266- $ this ->skip -= 12 ;
267- $ is = new DecipheringInputStream (
268- new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ] - 12 ),
269- $ cipher
270- );
262+ // Verify password
263+ $ cipher = new ZipCipher ();
264+ $ cipher ->initialize (iconv (\xp::ENCODING , 'cp437 ' , $ this ->password ->reveal ()));
265+ $ preamble = $ cipher ->decipher ($ stream ->read (12 ));
266+ if (ord ($ preamble [11 ]) !== (($ header ['crc ' ] >> 24 ) & 0xff )) {
267+ throw new IllegalAccessException ('The password did not match ' );
268+ }
269+
270+ return new DecipheringInputStream ($ stream , $ cipher );
271+ };
271272 } else {
272- $ is = new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ]);
273+ $ stream = new ZipFileInputStream ($ this , $ this ->position , $ header ['compressed ' ]);
274+ $ is = function () use ($ stream ) { return $ stream ; };
273275 }
274276
275277 // Create ZipEntry object and return it
0 commit comments