Back to index

php5  5.3.10
corrupt_zipmaker.php.inc
Go to the documentation of this file.
00001 <?php
00002 // this corrupt zip maker uses portions of Vincent Lascaux's File_Archive to create
00003 // specifically corrupted zip archives for unit-testing zip support in the phar extension
00004 // and was modified by Greg Beaver
00036 class corrupt_zipmaker
00037 {
00042        var $offset = 0;
00043 
00048        var $comment = "";
00049 
00054        var $central = "";
00055 
00060        var $start = "";
00061 
00065        function setComment($comment) { $this->comment = $comment; }
00066 
00071        function getMTime($time)
00072        {
00073               $mtime = ($time !== null ? getdate($time) : getdate());
00074               $mtime = preg_replace(
00075                             "/(..){1}(..){1}(..){1}(..){1}/",
00076                             "\\x\\4\\x\\3\\x\\2\\x\\1",
00077                             dechex(($mtime['year']-1980<<25)|
00078                                    ($mtime['mon'    ]<<21)|
00079                                    ($mtime['mday'   ]<<16)|
00080                                    ($mtime['hours'  ]<<11)|
00081                                    ($mtime['minutes']<<5)|
00082                                    ($mtime['seconds']>>1)));
00083               eval('$mtime = "'.$mtime.'";');
00084               return $mtime;
00085        }
00086 
00087        private function getFileEntry($compmethod, $mtime, $crc32, $complength, $uncomplength, $filename, $data, $corrupt, $fakecomp)
00088        {
00089               switch ($corrupt) {
00090                      case null :
00091                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00092                                           $mtime .
00093                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 0x00) .
00094                                           $filename .
00095                                           $data;
00096                             break;
00097                      case 'compress' :
00098                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $fakecomp) .
00099                                           $mtime .
00100                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 0x00) .
00101                                           $filename .
00102                                           $data;
00103                             break;
00104                      case 'encrypt' :
00105                             $file = "PK\x03\x04\x14\x00\x01\x00" . pack('v', $compmethod) .
00106                                           $mtime .
00107                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 0x00) .
00108                                           $filename .
00109                                           $data;
00110                             break;
00111                      case 'crc32' :
00112                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00113                                           $mtime .
00114                                           pack("VVVvv", $crc32 + 1, $complength, $uncomplength, strlen($filename), 0x00) .
00115                                           $filename .
00116                                           $data;
00117                             break;
00118                      case 'complength' :
00119                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00120                                           $mtime .
00121                                           pack("VVVvv", $crc32, $complength + 1, $uncomplength, strlen($filename), 0x00) .
00122                                           $filename .
00123                                           $data;
00124                             break;
00125                      case 'uncomplength' :
00126                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00127                                           $mtime .
00128                                           pack("VVVvv", $crc32, $complength, $uncomplength - 1, strlen($filename), 0x00) .
00129                                           $filename .
00130                                           $data;
00131                             break;
00132                      case 'filename_len' :
00133                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00134                                           $mtime .
00135                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename) - 1, 0x00) .
00136                                           $filename .
00137                                           $data;
00138                             break;
00139                      case 'extra_len' :
00140                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00141                                           $mtime .
00142                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 1) .
00143                                           $filename .
00144                                           $data;
00145                             break;
00146                      case 'filename' :
00147                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00148                                           $mtime .
00149                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 0x00) .
00150                                           substr($filename, 1) .
00151                                           $data;
00152                             break;
00153                      case 'data' :
00154                             $file = "PK\x03\x04\x14\x00\x00\x00" . pack('v', $compmethod) .
00155                                           $mtime .
00156                                           pack("VVVvv", $crc32, $complength, $uncomplength, strlen($filename), 0x00) .
00157                                           $filename .
00158                                           substr($data, 1);
00159                             break;
00160               }
00161               return $file;
00162        }
00163 
00164        private function getCentralEntry($compmethod, $mtime, $crc32, $complength, $uncomplength, $filename, $comment, $corrupt, &$offset, $fakecomp)
00165        {
00166               settype($comment, 'string');
00167               switch ($corrupt) {
00168                      case null :
00169                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00170                                           $mtime .
00171                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00172                                                  0x0000, $this->offset).
00173                                           $filename . $comment;
00174                             $offset = strlen($central);
00175                             break;
00176                      case 'encrypt' :
00177                             $central = "PK\x01\x02\x00\x00\x14\x00\x01\x00" . pack('v', $compmethod) .
00178                                           $mtime .
00179                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00180                                                  0x0000, $this->offset).
00181                                           $filename . $comment;
00182                             $offset = strlen($central);
00183                             break;
00184                      case 'compress' :
00185                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $fakecomp) .
00186                                           $mtime .
00187                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00188                                                  0x0000, $this->offset).
00189                                           $filename . $comment;
00190                             $offset = strlen($central);
00191                             break;
00192                      case 'crc32' :
00193                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00194                                           $mtime .
00195                                           pack("VVVvvvvvVV", $crc32 + 1, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00196                                                  0x0000, $this->offset).
00197                                           $filename . $comment;
00198                             $offset = strlen($central);
00199                             break;
00200                      case 'complength' :
00201                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00202                                           $mtime .
00203                                           pack("VVVvvvvvVV", $crc32, $complength - 1, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00204                                                  0x0000, $this->offset).
00205                                           $filename . $comment;
00206                             $offset = strlen($central);
00207                             break;
00208                      case 'uncomplength' :
00209                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00210                                           $mtime .
00211                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength - 1, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00212                                                  0x0000, $this->offset).
00213                                           $filename . $comment;
00214                             $offset = strlen($central);
00215                             break;
00216                      case 'filename_len' :
00217                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00218                                           $mtime .
00219                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename) - 1, 0x00,strlen($comment),0x00,0x00,
00220                                                  0x0000, $this->offset).
00221                                           $filename . $comment;
00222                             $offset = strlen($central);
00223                             break;
00224                      case 'offset' :
00225                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00226                                           $mtime .
00227                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment),0x00,0x00,
00228                                                  0x0000, $this->offset - 1).
00229                                           $filename . $comment;
00230                             $offset = strlen($central) - 1;
00231                             break;
00232                      case 'comment' :
00233                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00234                                           $mtime .
00235                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), 0x00,strlen($comment) + 1,0x00,0x00,
00236                                                  0x0000, $this->offset).
00237                                           $filename . $comment;
00238                             $offset = strlen($central);
00239                             break;
00240                      case 'extralen1' :
00241                             $extra = 'nu' . 0xffff; // way huge size
00242                             $central = "PK\x01\x02\x00\x00\x14\x00\x00\x00" . pack('v', $compmethod) .
00243                                           $mtime .
00244                                           pack("VVVvvvvvVV", $crc32, $complength, $uncomplength, strlen($filename), strlen($extra),strlen($comment),0x00,0x00,
00245                                                  0x0000, $this->offset).
00246                                           $filename . $extra . $comment;
00247                             $offset = strlen($central);
00248                             break;
00249               }
00250               return $central;
00251        }
00252 
00253        function addFile($filename, $mtime, $data, $comment = null, $compress = null, $filecorrupt = null, $centralcorrupt = null, $fakecomp = 1)
00254        {
00255               $mtime = $this->getMTime($mtime ? $mtime : null);
00256 
00257               $uncomplength = strlen($data);
00258               $crc32 = crc32($data) & 0xFFFFFFFF;
00259               $compmethod = 0;
00260               switch ($compress) {
00261                      case 'gz' :
00262                             $data = gzcompress($data);
00263                             $compmethod = 8;
00264                             break;
00265                      case 'bz2' :
00266                             $data = bzcompress($data);
00267                             $compmethod = 12;
00268                             break;
00269               }
00270               $complength = strlen($data);
00271 
00272               $this->start .= ($file = $this->getFileEntry($compmethod, $mtime, $crc32, $complength, $uncomplength, $filename, $data, $filecorrupt, $fakecomp));
00273 
00274               $offset = 0;
00275               $this->central .= $this->getCentralEntry($compmethod, $mtime, $crc32, $complength, $uncomplength, $filename, $comment, $centralcorrupt, $offset, $fakecomp);
00276 
00277               $this->offset += $offset;
00278               $this->count++;
00279        }
00280 
00281        function writeZip($zipfile, $corrupt = null)
00282        {
00283               $write = $this->start . $this->central;
00284               switch ($corrupt) {
00285                      case null :
00286                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00287                                    pack("vvVVv", $this->count, $this->count,
00288                                    $this->offset, strlen($this->start),
00289                                    strlen($this->comment)) . $this->comment;
00290                             break;
00291                      case 'disknumber' :
00292                             $write .= "PK\x05\x06\x01\x00\x01\x00" .
00293                                    pack("vvVVv", $this->count, $this->count,
00294                                    $this->offset, strlen($this->start),
00295                                    strlen($this->comment)) . $this->comment;
00296                             break;
00297                      case 'count1' :
00298                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00299                                    pack("vvVVv", $this->count + 1, $this->count,
00300                                    $this->offset, strlen($this->start),
00301                                    strlen($this->comment)) . $this->comment;
00302                             break;
00303                      case 'count2' :
00304                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00305                                    pack("vvVVv", $this->count, $this->count + 1,
00306                                    $this->offset, strlen($this->start),
00307                                    strlen($this->comment)) . $this->comment;
00308                             break;
00309                      case 'cdir_offset' :
00310                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00311                                    pack("vvVVv", $this->count, $this->count,
00312                                    $this->offset, strlen($this->start) - 3,
00313                                    strlen($this->comment)) . $this->comment;
00314                             break;
00315                      case 'cdir_len' :
00316                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00317                                    pack("vvVVv", $this->count, $this->count,
00318                                    $this->offset - 5, strlen($this->start),
00319                                    strlen($this->comment)) . $this->comment;
00320                             break;
00321                      case 'comment' :
00322                             $write .= "PK\x05\x06\x00\x00\x00\x00" .
00323                                    pack("vvVVv", $this->count, $this->count,
00324                                    strlen($this->start), $this->offset + 1,
00325                                    strlen($this->comment) + 1) . $this->comment;
00326                             break;
00327                      case 'none' :
00328               }
00329               file_put_contents($zipfile, $write);
00330        }
00331 }
00332 ?>