From 664d102aa65af165bf25163b220424e67241369c Mon Sep 17 00:00:00 2001 From: SVN Migration Date: Mon, 24 Jul 2006 16:58:59 +0000 Subject: [PATCH] This commit was manufactured by cvs2svn to create branch 'PHP_5_2'. --- ext/zip/CREDITS | 2 + ext/zip/TODO | 4 + ext/zip/config.m4 | 68 + ext/zip/config.w32 | 37 + ext/zip/examples/comment.php | 6 + ext/zip/examples/create.php | 23 + ext/zip/examples/dir.php | 21 + ext/zip/examples/extract.php | 28 + ext/zip/examples/extractAll.php | 24 + ext/zip/examples/fopen.php | 35 + ext/zip/examples/get_set_comments.php | 38 + ext/zip/examples/im.php | 11 + ext/zip/examples/odt.php | 20 + ext/zip/examples/oldapi.php | 17 + ext/zip/examples/test.odt | Bin 0 -> 6162 bytes ext/zip/examples/test.zip | Bin 0 -> 541 bytes ext/zip/examples/test1.zip | Bin 0 -> 681 bytes ext/zip/examples/test_im.zip | Bin 0 -> 1086 bytes ext/zip/examples/test_with_comment.zip | Bin 0 -> 560 bytes ext/zip/examples/too.php | 2 + ext/zip/lib/mkstemp.c | 136 ++ ext/zip/lib/unistd.h | 3 + ext/zip/lib/zip.h | 205 ++ ext/zip/lib/zip.h.orig | 170 ++ ext/zip/lib/zip_add.c | 51 + ext/zip/lib/zip_delete.c | 61 + ext/zip/lib/zip_dirent.c | 532 +++++ ext/zip/lib/zip_entry_free.c | 55 + ext/zip/lib/zip_entry_new.c | 81 + ext/zip/lib/zip_err_str.c | 72 + ext/zip/lib/zip_error.c | 95 + ext/zip/lib/zip_error_get.c | 47 + ext/zip/lib/zip_error_get_sys_type.c | 50 + ext/zip/lib/zip_error_strerror.c | 93 + ext/zip/lib/zip_error_to_str.c | 73 + ext/zip/lib/zip_fclose.c | 74 + ext/zip/lib/zip_file_error_get.c | 47 + ext/zip/lib/zip_file_get_offset.c | 80 + ext/zip/lib/zip_file_strerror.c | 47 + ext/zip/lib/zip_fopen.c | 52 + ext/zip/lib/zip_fopen_index.c | 219 ++ ext/zip/lib/zip_fread.c | 125 ++ ext/zip/lib/zip_free.c | 83 + ext/zip/lib/zip_get_archive_comment.c | 56 + ext/zip/lib/zip_get_file_comment.c | 61 + ext/zip/lib/zip_get_name.c | 74 + ext/zip/lib/zip_get_num_files.c | 50 + ext/zip/lib/zip_memdup.c | 58 + ext/zip/lib/zip_name_locate.c | 91 + ext/zip/lib/zip_new.c | 71 + ext/zip/lib/zip_open.c | 464 ++++ ext/zip/lib/zip_rename.c | 52 + ext/zip/lib/zip_replace.c | 79 + ext/zip/lib/zip_set_archive_comment.c | 68 + ext/zip/lib/zip_set_file_comment.c | 69 + ext/zip/lib/zip_set_name.c | 77 + ext/zip/lib/zip_source_buffer.c | 162 ++ ext/zip/lib/zip_source_file.c | 71 + ext/zip/lib/zip_source_filep.c | 180 ++ ext/zip/lib/zip_source_free.c | 54 + ext/zip/lib/zip_source_function.c | 62 + ext/zip/lib/zip_source_zip.c | 189 ++ ext/zip/lib/zip_stat.c | 52 + ext/zip/lib/zip_stat_index.c | 93 + ext/zip/lib/zip_strerror.c | 47 + ext/zip/lib/zip_unchange.c | 84 + ext/zip/lib/zip_unchange_all.c | 56 + ext/zip/lib/zip_unchange_archive.c | 52 + ext/zip/lib/zip_unchange_data.c | 53 + ext/zip/lib/zip_win32.h | 29 + ext/zip/lib/zipint.h | 225 ++ ext/zip/package.xml | 362 +++ ext/zip/php_zip.c | 1934 +++++++++++++++++ ext/zip/php_zip.h | 101 + ext/zip/tests/001.phpt | 12 + ext/zip/tests/binarynull.zip | Bin 0 -> 656 bytes ext/zip/tests/bug7214.phpt | 23 + ext/zip/tests/bug7658.odt | Bin 0 -> 8873 bytes ext/zip/tests/bug7658.phpt | 60 + ext/zip/tests/bug7658.xml | 2 + ext/zip/tests/bug8009.phpt | 27 + ext/zip/tests/bug8009.zip | Bin 0 -> 112 bytes ext/zip/tests/oo_addfile.phpt | 37 + ext/zip/tests/oo_close.phpt | 25 + ext/zip/tests/oo_delete.phpt | 76 + ext/zip/tests/oo_ext_zip.phpt | 27 + ext/zip/tests/oo_namelocate.phpt | 45 + ext/zip/tests/oo_open.phpt | 46 + ext/zip/tests/oo_rename.phpt | 58 + ext/zip/tests/test.zip | Bin 0 -> 526 bytes ext/zip/tests/test_procedural.zip | Bin 0 -> 541 bytes ext/zip/tests/utils.php | 8 + ext/zip/tests/zip_close.phpt | 17 + ext/zip/tests/zip_entry_compressedsize.phpt | 23 + .../tests/zip_entry_compressionmethod.phpt | 24 + ext/zip/tests/zip_entry_filesize.phpt | 23 + ext/zip/tests/zip_entry_name.phpt | 23 + ext/zip/tests/zip_entry_open.phpt | 17 + ext/zip/tests/zip_entry_read.phpt | 18 + ext/zip/tests/zip_open.phpt | 16 + ext/zip/tests/zip_read.phpt | 21 + ext/zip/zip_stream.c | 239 ++ 102 files changed, 8730 insertions(+) create mode 100644 ext/zip/CREDITS create mode 100644 ext/zip/TODO create mode 100644 ext/zip/config.m4 create mode 100644 ext/zip/config.w32 create mode 100644 ext/zip/examples/comment.php create mode 100644 ext/zip/examples/create.php create mode 100644 ext/zip/examples/dir.php create mode 100644 ext/zip/examples/extract.php create mode 100644 ext/zip/examples/extractAll.php create mode 100644 ext/zip/examples/fopen.php create mode 100644 ext/zip/examples/get_set_comments.php create mode 100644 ext/zip/examples/im.php create mode 100644 ext/zip/examples/odt.php create mode 100644 ext/zip/examples/oldapi.php create mode 100644 ext/zip/examples/test.odt create mode 100644 ext/zip/examples/test.zip create mode 100644 ext/zip/examples/test1.zip create mode 100644 ext/zip/examples/test_im.zip create mode 100644 ext/zip/examples/test_with_comment.zip create mode 100644 ext/zip/examples/too.php create mode 100644 ext/zip/lib/mkstemp.c create mode 100644 ext/zip/lib/unistd.h create mode 100644 ext/zip/lib/zip.h create mode 100644 ext/zip/lib/zip.h.orig create mode 100644 ext/zip/lib/zip_add.c create mode 100644 ext/zip/lib/zip_delete.c create mode 100644 ext/zip/lib/zip_dirent.c create mode 100644 ext/zip/lib/zip_entry_free.c create mode 100644 ext/zip/lib/zip_entry_new.c create mode 100644 ext/zip/lib/zip_err_str.c create mode 100644 ext/zip/lib/zip_error.c create mode 100644 ext/zip/lib/zip_error_get.c create mode 100644 ext/zip/lib/zip_error_get_sys_type.c create mode 100644 ext/zip/lib/zip_error_strerror.c create mode 100644 ext/zip/lib/zip_error_to_str.c create mode 100644 ext/zip/lib/zip_fclose.c create mode 100644 ext/zip/lib/zip_file_error_get.c create mode 100644 ext/zip/lib/zip_file_get_offset.c create mode 100644 ext/zip/lib/zip_file_strerror.c create mode 100644 ext/zip/lib/zip_fopen.c create mode 100644 ext/zip/lib/zip_fopen_index.c create mode 100644 ext/zip/lib/zip_fread.c create mode 100644 ext/zip/lib/zip_free.c create mode 100644 ext/zip/lib/zip_get_archive_comment.c create mode 100644 ext/zip/lib/zip_get_file_comment.c create mode 100644 ext/zip/lib/zip_get_name.c create mode 100644 ext/zip/lib/zip_get_num_files.c create mode 100644 ext/zip/lib/zip_memdup.c create mode 100644 ext/zip/lib/zip_name_locate.c create mode 100644 ext/zip/lib/zip_new.c create mode 100644 ext/zip/lib/zip_open.c create mode 100644 ext/zip/lib/zip_rename.c create mode 100644 ext/zip/lib/zip_replace.c create mode 100644 ext/zip/lib/zip_set_archive_comment.c create mode 100644 ext/zip/lib/zip_set_file_comment.c create mode 100644 ext/zip/lib/zip_set_name.c create mode 100644 ext/zip/lib/zip_source_buffer.c create mode 100644 ext/zip/lib/zip_source_file.c create mode 100644 ext/zip/lib/zip_source_filep.c create mode 100644 ext/zip/lib/zip_source_free.c create mode 100644 ext/zip/lib/zip_source_function.c create mode 100644 ext/zip/lib/zip_source_zip.c create mode 100644 ext/zip/lib/zip_stat.c create mode 100644 ext/zip/lib/zip_stat_index.c create mode 100644 ext/zip/lib/zip_strerror.c create mode 100644 ext/zip/lib/zip_unchange.c create mode 100644 ext/zip/lib/zip_unchange_all.c create mode 100644 ext/zip/lib/zip_unchange_archive.c create mode 100644 ext/zip/lib/zip_unchange_data.c create mode 100644 ext/zip/lib/zip_win32.h create mode 100644 ext/zip/lib/zipint.h create mode 100644 ext/zip/package.xml create mode 100644 ext/zip/php_zip.c create mode 100644 ext/zip/php_zip.h create mode 100644 ext/zip/tests/001.phpt create mode 100644 ext/zip/tests/binarynull.zip create mode 100644 ext/zip/tests/bug7214.phpt create mode 100644 ext/zip/tests/bug7658.odt create mode 100644 ext/zip/tests/bug7658.phpt create mode 100644 ext/zip/tests/bug7658.xml create mode 100644 ext/zip/tests/bug8009.phpt create mode 100644 ext/zip/tests/bug8009.zip create mode 100644 ext/zip/tests/oo_addfile.phpt create mode 100644 ext/zip/tests/oo_close.phpt create mode 100644 ext/zip/tests/oo_delete.phpt create mode 100644 ext/zip/tests/oo_ext_zip.phpt create mode 100644 ext/zip/tests/oo_namelocate.phpt create mode 100644 ext/zip/tests/oo_open.phpt create mode 100644 ext/zip/tests/oo_rename.phpt create mode 100644 ext/zip/tests/test.zip create mode 100644 ext/zip/tests/test_procedural.zip create mode 100644 ext/zip/tests/utils.php create mode 100644 ext/zip/tests/zip_close.phpt create mode 100644 ext/zip/tests/zip_entry_compressedsize.phpt create mode 100644 ext/zip/tests/zip_entry_compressionmethod.phpt create mode 100644 ext/zip/tests/zip_entry_filesize.phpt create mode 100644 ext/zip/tests/zip_entry_name.phpt create mode 100644 ext/zip/tests/zip_entry_open.phpt create mode 100644 ext/zip/tests/zip_entry_read.phpt create mode 100644 ext/zip/tests/zip_open.phpt create mode 100644 ext/zip/tests/zip_read.phpt create mode 100644 ext/zip/zip_stream.c diff --git a/ext/zip/CREDITS b/ext/zip/CREDITS new file mode 100644 index 0000000000..6c7e42d417 --- /dev/null +++ b/ext/zip/CREDITS @@ -0,0 +1,2 @@ +Zip +Pierre-Alain Joye diff --git a/ext/zip/TODO b/ext/zip/TODO new file mode 100644 index 0000000000..c1baba97ed --- /dev/null +++ b/ext/zip/TODO @@ -0,0 +1,4 @@ +- add pattern support to extract or add files +- stream to add or modify entries +- crypt support for zip (read and write) +- Iterators diff --git a/ext/zip/config.m4 b/ext/zip/config.m4 new file mode 100644 index 0000000000..5208cff295 --- /dev/null +++ b/ext/zip/config.m4 @@ -0,0 +1,68 @@ +dnl +dnl $Id$ +dnl + +PHP_ARG_ENABLE(zip, for zip archive read/writesupport, +[ --enable-zip Include Zip read/write support.]) + +if test -z "$PHP_ZLIB_DIR"; then +PHP_ARG_WITH(zlib-dir, for the location of libz, +[ --with-zlib-dir[=DIR] zip: Set the path to libz install prefix.], no, no) +fi + +if test "$PHP_ZIP" != "no"; then + + if test "$PHP_ZLIB_DIR" != "no" && test "$PHP_ZLIB_DIR" != "yes"; then + if test -f "$PHP_ZLIB_DIR/include/zlib/zlib.h"; then + PHP_ZLIB_DIR="$PHP_ZLIB_DIR" + PHP_ZLIB_INCDIR="$PHP_ZLIB_DIR/include/zlib" + elif test -f "$PHP_ZLIB_DIR/include/zlib.h"; then + PHP_ZLIB_DIR="$PHP_ZLIB_DIR" + PHP_ZLIB_INCDIR="$PHP_ZLIB_DIR/include" + else + AC_MSG_ERROR([Can't find zlib headers under "$PHP_ZLIB_DIR"]) + fi + else + for i in /usr/local /usr; do + if test -f "$i/include/zlib/zlib.h"; then + PHP_ZLIB_DIR="$i" + PHP_ZLIB_INCDIR="$i/include/zlib" + elif test -f "$i/include/zlib.h"; then + PHP_ZLIB_DIR="$i" + PHP_ZLIB_INCDIR="$i/include" + fi + done + fi + + dnl # zlib + AC_MSG_CHECKING([for the location of zlib]) + if test "$PHP_ZLIB_DIR" = "no"; then + AC_MSG_ERROR([zip support requires ZLIB. Use --with-zlib-dir= to specify prefix where ZLIB include and library are located]) + else + AC_MSG_RESULT([$PHP_ZLIB_DIR]) + PHP_ADD_LIBRARY_WITH_PATH(z, $PHP_ZLIB_DIR/lib, ZIP_SHARED_LIBADD) + PHP_ADD_INCLUDE($PHP_ZLIB_INCDIR) + fi + + PHP_ZIP_SOURCES="$PHP_ZIP_SOURCES lib/zip_add.c lib/zip_error.c lib/zip_fclose.c \ + lib/zip_fread.c lib/zip_open.c lib/zip_source_filep.c \ + lib/zip_strerror.c lib/zip_close.c lib/zip_error_get.c \ + lib/zip_file_error_get.c lib/zip_free.c lib/zip_rename.c \ + lib/zip_source_free.c lib/zip_unchange_all.c lib/zip_delete.c \ + lib/zip_error_get_sys_type.c lib/zip_file_get_offset.c \ + lib/zip_get_name.c lib/zip_replace.c lib/zip_source_function.c \ + lib/zip_unchange.c lib/zip_dirent.c lib/zip_error_strerror.c \ + lib/zip_file_strerror.c lib/zip_get_num_files.c \ + lib/zip_set_name.c lib/zip_source_zip.c lib/zip_unchange_data.c \ + lib/zip_entry_free.c lib/zip_error_to_str.c lib/zip_fopen.c \ + lib/zip_name_locate.c lib/zip_source_buffer.c lib/zip_stat.c \ + lib/zip_entry_new.c lib/zip_err_str.c lib/zip_fopen_index.c \ + lib/zip_new.c lib/zip_source_file.c lib/zip_stat_index.c lib/zip_get_archive_comment.c \ + lib/zip_get_file_comment.c lib/zip_set_archive_comment.c lib/zip_set_file_comment.c \ + lib/zip_unchange_archive.c lib/zip_memdup.c" + + AC_DEFINE(HAVE_ZIP,1,[ ]) + PHP_NEW_EXTENSION(zip, php_zip.c zip_stream.c $PHP_ZIP_SOURCES, $ext_shared) + PHP_ADD_BUILD_DIR($ext_builddir/lib, 1) + PHP_SUBST(ZIP_SHARED_LIBADD) +fi diff --git a/ext/zip/config.w32 b/ext/zip/config.w32 new file mode 100644 index 0000000000..1a99217b59 --- /dev/null +++ b/ext/zip/config.w32 @@ -0,0 +1,37 @@ +// $Id$ +// vim:ft=javascript + +ARG_ENABLE("zip", "ZIP support", "no"); + +if (PHP_ZIP != "no") { + + if (CHECK_HEADER_ADD_INCLUDE("zlib.h", "CFLAGS_ZIP", "..\\zlib;" + php_usual_include_suspects + ";" + PHP_ZIP)) { + if (PHP_ZLIB_SHARED) { + CHECK_LIB("zlib.lib", "zip", PHP_ZIP); + } + EXTENSION('zip', 'php_zip.c zip_stream.c'); + ADD_SOURCES(configure_module_dirname + "/lib", "zip_add.c zip_error.c zip_fclose.c \ + zip_fread.c zip_open.c zip_source_filep.c \ + zip_strerror.c zip_close.c zip_error_get.c \ + zip_file_error_get.c zip_free.c zip_rename.c \ + zip_source_free.c zip_unchange_all.c zip_delete.c \ + zip_error_get_sys_type.c zip_file_get_offset.c \ + zip_get_name.c zip_replace.c zip_source_function.c \ + zip_unchange.c zip_dirent.c zip_error_strerror.c \ + zip_file_strerror.c zip_get_num_files.c \ + zip_set_name.c zip_source_zip.c zip_unchange_data.c \ + zip_entry_free.c zip_error_to_str.c zip_fopen.c \ + zip_name_locate.c zip_source_buffer.c zip_stat.c \ + zip_entry_new.c zip_err_str.c zip_fopen_index.c \ + zip_new.c zip_source_file.c zip_stat_index.c \ + zip_get_archive_comment.c zip_get_file_comment.c \ + zip_set_archive_comment.c zip_set_file_comment.c \ + zip_unchange_archive.c zip_memdup.c", "zip"); + + AC_DEFINE('HAVE_ZLIB', 1); + AC_DEFINE('HAVE_ZIP', 1); + } else { + WARNING("zip not enabled; libraries and headers not found"); + } +} + diff --git a/ext/zip/examples/comment.php b/ext/zip/examples/comment.php new file mode 100644 index 0000000000..90b37d3795 --- /dev/null +++ b/ext/zip/examples/comment.php @@ -0,0 +1,6 @@ +open('test_with_comment.zip'); +// Add "Foo Comment" as comment for the foo entry +$z->setCommentName('foo', 'Too Comment ' . time()); +$z->close(); diff --git a/ext/zip/examples/create.php b/ext/zip/examples/create.php new file mode 100644 index 0000000000..a41c8e6d0b --- /dev/null +++ b/ext/zip/examples/create.php @@ -0,0 +1,23 @@ +open($filename, ZIPARCHIVE::CREATE)) { + exit("cannot open <$filename>\n"); +} else { + echo "file <$filename> OK\n"; +} + +$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n"); +$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n"); +$zip->addFile($thisdir . "/too.php","/testfromfile.php"); +echo "numfiles: " . $zip->numFiles . "\n"; +echo "status:" . $zip->status . "\n"; +$zip->close(); +unset($zip); diff --git a/ext/zip/examples/dir.php b/ext/zip/examples/dir.php new file mode 100644 index 0000000000..c362a72ba3 --- /dev/null +++ b/ext/zip/examples/dir.php @@ -0,0 +1,21 @@ +open('test_with_comment.zip'); +print_r($za); +var_dump($za); +echo "numFiles: " . $za->numFiles . "\n"; +echo "status: " . $za->status . "\n"; +echo "statusSys: " . $za->statusSys . "\n"; +echo "filename: " . $za->filename . "\n"; +echo "comment: " . $za->comment . "\n"; + +for ($i=0; $i<$za->numFiles;$i++) { + echo "index: $i\n"; + print_r($za->statIndex($i)); +} +echo "numFile:" . $za->numFiles . "\n"; diff --git a/ext/zip/examples/extract.php b/ext/zip/examples/extract.php new file mode 100644 index 0000000000..5276b0dca5 --- /dev/null +++ b/ext/zip/examples/extract.php @@ -0,0 +1,28 @@ +filename . "\n"; +$zip->open("test.zip"); +/* +$zip->addFile("./modules/"); +$zip->addFile("./testempty"); +*/ +echo $zip->status . "\n"; +echo $zip->statusSys . "\n"; + +echo $zip->numFiles . "\n"; +echo $zip->filename . "\n"; +var_dump($zip); +$files = array('test', 'testdir/test2'); +if (!$zip->extractTo("./testext/path/to", $files)) { + echo "error!\n"; + echo $zip->status . "\n"; + echo $zip->statusSys . "\n"; + +} + +$zip->close(); diff --git a/ext/zip/examples/extractAll.php b/ext/zip/examples/extractAll.php new file mode 100644 index 0000000000..d318a453df --- /dev/null +++ b/ext/zip/examples/extractAll.php @@ -0,0 +1,24 @@ +filename . "\n"; +$zip->open("test.zip"); +/* +$zip->addFile("./modules/"); +$zip->addFile("./testempty"); +*/ +echo $zip->status . "\n"; +echo $zip->statusSys . "\n"; + +echo $zip->numFiles . "\n"; +echo $zip->filename . "\n"; +var_dump($zip); +$files = array('test', 'testdir/test2'); +if (!$zip->extractTo("./testext/path/to")) { + echo "error!\n"; + echo $zip->status . "\n"; + echo $zip->statusSys . "\n"; + +} + +$zip->close(); diff --git a/ext/zip/examples/fopen.php b/ext/zip/examples/fopen.php new file mode 100644 index 0000000000..32357f3c08 --- /dev/null +++ b/ext/zip/examples/fopen.php @@ -0,0 +1,35 @@ +open(dirname(__FILE__) . '/test.zip'); +$fp = $z->getStream('test'); + +var_dump($fp); +if(!$fp) exit("\n"); +while (!feof($fp)) { + $contents .= fread($fp, 2); +} + +fclose($fp); +file_put_contents('t',$contents); +echo "done.\n"; + + diff --git a/ext/zip/examples/get_set_comments.php b/ext/zip/examples/get_set_comments.php new file mode 100644 index 0000000000..5bd302e6da --- /dev/null +++ b/ext/zip/examples/get_set_comments.php @@ -0,0 +1,38 @@ +open('t.zip'); + +print_r($z); + +for ($i=0; $i<$z->numFiles; $i++) { + echo "index: $i\n"; + print_r($z->getCommentIndex($i)); + echo "\n\n"; +} +echo "foobar/ " . $z->getCommentName('foobar/') . "\n"; + +echo "Archive comment: " . $z->getArchiveComment() . "\n"; + + +$z->setCommentIndex(1, 'new comment idx 1'); +$z->setCommentName('foobar/', 'new comment foobar/'); + +$z->setArchiveComment( 'new archive comment'); + +for ($i=0; $i<$z->numFiles; $i++) { + echo "index: $i\n"; + print_r($z->getCommentIndex($i)); + echo "\n\n"; +} + +echo $z->getCommentName('foobar/') . "\n"; + +// Get the original comment +echo $z->getCommentName('foobar/', ZIPARCHIVE::FL_UNCHANGED) . "\n"; + +echo "Archive comment: " . $z->getArchiveComment() . "\n"; +echo "Archive comment (original): " . $z->getArchiveComment(ZIPARCHIVE::FL_UNCHANGED) . "\n"; + diff --git a/ext/zip/examples/im.php b/ext/zip/examples/im.php new file mode 100644 index 0000000000..3721434054 --- /dev/null +++ b/ext/zip/examples/im.php @@ -0,0 +1,11 @@ +open(dirname(__FILE__) . '/test_im.zip'); +$im_string = $z->getFromName("pear_item.gif"); +$im = imagecreatefromstring($im_string); +imagepng($im, 'b.png'); + diff --git a/ext/zip/examples/odt.php b/ext/zip/examples/odt.php new file mode 100644 index 0000000000..c829f12343 --- /dev/null +++ b/ext/zip/examples/odt.php @@ -0,0 +1,20 @@ +open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml'); +$odt_meta = array(); +while ($reader->read()) { + if ($reader->nodeType == XMLREADER::ELEMENT) { + $elm = $reader->name; + } else { + if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') { + break; + } + if (!trim($reader->value)) { + continue; + } + $odt_meta[$elm] = $reader->value; + } +} +print_r($odt_meta); diff --git a/ext/zip/examples/oldapi.php b/ext/zip/examples/oldapi.php new file mode 100644 index 0000000000..2f17f43ebd --- /dev/null +++ b/ext/zip/examples/oldapi.php @@ -0,0 +1,17 @@ +HVZjl3y_q6qnSGzT4RfkRViWGv+eY&YE=hJ`;R(i@!g2TdYU8S1?W z_$*t~=pHCxwjUVHnAc%xd;UJl#!m>!;>fPpLAhbQ=x+#!Lk z{mCNz*QD(=r*4^Cc#l9)Q(L4u;Dbq`Qq;tpqg_sIek+x0MNr+tGOY<2WkWp@?xo?= zySjU@ZOJ)hZxaujRhkIAI5UcKku5a;^UvrO?LxQn-$D8vES0Sbm!@CFI!v&};~QLg zJ|L6r+pkGhoZ>Arhcum^Swm#Yv7rQ!9gZ= zk{xu?(}sXO2D@7Ik)Rmv8YhB8?Onv+{lMi;v4wBLdfVE0&HJfuCttS-6w9&S%W!bk zZ`A`hm3y@`RCu+lIO?ulNS9vpW^MUed=c3iY=fqhb#vzB?tS~IPNdE6F)UlIO!7IW z=KZ8!r-4X=InjVWl{ac5ZxGh~cJ5umE&+kU*pGoGlc;9$AE;y4)cR)N zl+2v!5sxvjAbM_|Yl9E(*FNoc^21p5Wz84Tr`<-2+->fKNM#ya3UYWBf&ENwPBBl7 zT0K&A9^sPPLJ<&)hPW{AsmYe#V74%3-(C8ut22-p02rg6o~`};-!?9gzHup3A3T=A z*tgC0&E#Gc-p7ka;1i{)5t)>Rpa59;{H)j|k|pLU@cSK1qfH4&Q;2nBb?jr~*uWda^4HmCaWck9afHLO zl zd+c2N>GrW_z19-7IratIAkO6E!qRrnC=o+eMs!-iQOp{CrP6l{fK@m92W;FvXJM9T zs&&uy0WF7aFXtW9^iA-+l6-a4@XdVPNr{XkORHlfmtI?&O5Ov^8&}<^m&|M%13R^#>t!9A@U#WaOXo=kL= zgeIN@h8whz0^!vjrDxCy(+)M^lJ%3?3a1Mti-sC=+RUyU2u=+1DO#wS;R(LRX%^hH zn#9kIYz}(*Y9_erEMmk9clI!1a5uaDxQqoaeFk8g5EZ_5ddyX;`_Rwn@pY$zYkd zg*d2x?fH98)v1&lVPC^cpkYBGT&k}v<_m&i1p5p}v(`ZjgXa8{9b3%d8r-vP1#8g3@ zWo+u~*iyz#hw);oNl6?JSq$IzC*p_?$Bm!5)<3E?J+)ZDF+Qk*s--ulNdoVN3H7H3 zr2cFs)W5df&?qdHVQUPP0!BHVZTFL#EUx*)`YH{S#oM!%TF)JR`K?!UE{jV6(}>XJ z4DGM{Sv`**OmZufpFiZ?w6}GM(@A(mz+<1bcbX%!K@Jn!qLc(na30jwEe)ApH(7b9 zXDkQ&bkvWEPBDsJRJ<1yW9ch0QdN4Wxk>F>fA8R#$$+F@=B1Fa*`=oheqgD0=xo#! zhiQtW07BZ1U+%LT3JEKSzaWGv6Q?>QYGH&HM{7?^!Gpu06HKeJoOzPjQ(qj>9IZ^1 zOK>Us=<%2AhZ;C9XnTAsb1`z6g@a>w`anrDsWhsNGIF8XA?Wkh6#?b?{tGLRT-khdrxmC!O zv>q!~p4I%_Divo}C9bQAfm1a^6THo|Ai2d~{N~!$GH0msL!#(|GGfY4t}Iel_OL?& zSQueC%(VXoi4_3w7TylijJ$%8U#LFBsLbvH02t3^285-e7(rU|DE$v~0ZXRPb095o zm;zKBge9U$7#z`6OWej<53DAB6baNMxwzoaSj2W(1g7UtC=i{MNI`53X^GRwM8swS zg&?8`SPFuQM(oU_5IfO`O+{Nh-gq3*LrdI^O7%oQAYNWx3SLSIB(f_6rmn6I*&G%B zp$dckiSwk9@tY_N8iK`R8Mzt-0#krNwy_Kg|5u|J*qtVkNTi>N7<1T~;-)%9C=?Fa zdKU-%@X9~jhuroRLnu;nQ?x6Vh^6O;Br?*RF3o(?ms?X*P*;E{s{dhvBhtJ<{~UDk zB$M2+XevZS4f=;R4Tr~o)zlP~H6g!=)Wo0>R2&tLMe6)WT0vASg$i;Zkr{3yV^J6^ z8H6HYKo@bInvkDp1|J4XL6dQwj06~IMgpNp1coC)h$EZ0oudpih33pK2uVQ^Jn>i% zje>H;(tWT)ix-aS_EXI^e9H%Dx*-fbU<`_iMbh1<0){GqVT#r;HH4BXLJ|IBV*kW# z8u_0La414ib<0M^;FbmttdAFoj6wdI^slwsGHTOv+c1V`Jc{T_qt60~C4z0NHU<9d z+7gPz;HdPo07m3I!O@rRYab^jBK`EeOQT39QY!s%i+^=`BL9Nl7~QLtn%2J3<& zVlkjiAmjLGi5n7KNFbOvF**F5-k-vA6Hh0YLy`DReupjK6C^E{GjPi8b25%2A)^xj10^~9|se`um`ER{q}Pfo8AAOzS4sxz}k&Qa3-Q~cnXBN-Bj=-x{h;wrHqZl z4mqDY_hEj{lY_Zk`B+WhWp0C1-{&(|gMx5nIgx0C7k4UDRTpq&Vk+ z*>5~>Nww9nWTP@;a%LCHo{ASi$kxlNsPm0qu~=+l{@CcVx3i`T2m*R7Uj=1xuc zH&^+$dNp6K>hPMM?;41#82BDvW@GeyCARg@2C%>Hk`iWpzHi(qU}L@38mV=W(4Dpa z;d~vbc3owCrEKXaVPl|beYNDlhIi{g;(P^v(NchQgkpx$3ZXr6Nh6c!%VY()oO3^| z5s?!jMZcY}?_O*L%H}Zv03Yc$mVaMvF2-#IOJ$ty&HGB3{YgrX8n1ty)6=DtbK1RC z-pmI;S^CB-0tQMQswhZ$8w*IU?~h)*2oC4f5zdAgSaqG_GHp{ntw6hRl<1om%cR&$ z1KD#Spr}BOSL-DoCMQ5jx1;g9;#&zMtO-9{bbNlrz3-8B86Hj|5=H;+SnuQ=35J*} z-)i;u0P;C;|INDWqKNK#ypa*JvEW{9`j}$sIvu_~qfw#3vaFvX7EwTQuSQjumFr7g z7UCJVd*Gux-v40P@LM&on@fPS@f2lfTB&EqyT2hL1SD((!WC zr+Wk7R0ve{n+(902vb@CEj=CAS{)|xY zmDtY>Uf7m4j-7shvq+m$bSk9 zWwU|dJ09%T?6l(L6^&rK$|n2#{L>G;>GHlck>7?mZkn659$P+^pOA5BK{_v2OjjVg z`x@WgY!1!>9!Ez>=|3Q&O!!BkCnocx;-+Wxt+h3!_>^=JHLm))M0rVBVb3gX`=G;p z3Gfp>X$=&My7TWuaxySk5u@oV!qUJ}@JWS(CV$^D`3zuwJXix)N=G zRlSXMFki_hrc)u7`%exj7NZ546e^maXp=)BoDbp(AY$ZdjI)k3IkxeUp?G<;Xy*Z? ziP^6#JmO@-wIjYkX+8rQxBY^8LggJKoLE~DD|^c(RCKF(TQa_zE%v>=^OvFQi8zKpJv^yN3vx#fy*^$4IAXKzC%Lg0THls$q$bi z;FY6~HJ?ML3K}<%H8AOzCZF6cD2eQ@Aez?;5SQ@tg*v_@we%kU9_BTgzxxZcG5J* z?8`tzn5@YCIac%GPv+rze-FS?Ha5dd%BP}tV0u3NM&$nsGkfV_##Gn(IM~ok4?;i@ zaW3@ajFEa5#KB1QP~OnR@4znKiE=9}-wmL3e&@b0D3zLGT=dZ~bLrZLfv)2~ivO7di>YfI9FtHzv-0FeV>K0vn57%!B678 zaF+Mi)YLVLnBQ|$4!oaP#HQ1C+ft!~b@dZ120Stwr_}FsCj@w-lT`bqA)&umsj`SCRtCREh~*fOrX9{^S(w~lfA>fj$SWc8 zZhU1a?=Ih=u>oCA1st4UrBJmNpSVY62q1n6Rq!ON+T*C3M(wgcoSTA z>s_cgdiq(X8aJ(t77VLO<>6WT)D1M)U_x*;mS?Q!5mMqi<1?R<@k^CYnNK>dS(OU- zlqGc>VnlZ)=S3Wx9^Dn`uD38|VipAad!d)^`yI4h_Whk}2L!;V=>8&8dhC66K50-^7D;opGLI3S#6fHOT{tqKI7RCSo literal 0 HcmV?d00001 diff --git a/ext/zip/examples/test.zip b/ext/zip/examples/test.zip new file mode 100644 index 0000000000000000000000000000000000000000..6b986948031d904753e54b3739462aa99b8932c9 GIT binary patch literal 541 zcmWIWW@h1H0D-bb7GoXxC9hb4Y!GH<5M@Zq&kqgZWMGz8`sCyX#Gw@|3||;iK;m3n z0dTDqK&>w~FJfTvcTA5VPFROl?CKq1^`TmW~2ZB literal 0 HcmV?d00001 diff --git a/ext/zip/examples/test1.zip b/ext/zip/examples/test1.zip new file mode 100644 index 0000000000000000000000000000000000000000..a4deb13c6b6dc6b488cf532001c7d4bf93caab42 GIT binary patch literal 681 zcmWIWW@Zs#U|`^2c(zH$WOcEog#nOf48)Q^T#{N`l9rj1T98qoS5i@8XlQJ1XkcJz zX&h`8!g2D1P8h=?m)1#F9tpKRnzZQ5o|4uhQ>I)IX+4s(At5TwFU!%bG0xMqQ%;;A z0L2=$rtl|*K;un-SPIn|BXoO$h_h!RhDGmdbWEO3kybqcv|te<1A_p}qN4m7(ZTeCyw!xjLI|;;*l} zRgn8;XZ{?Yl?#Jwk{!ElghU-n7HXEvMwe7dCH$e zuYIcwnvYnvwjDUd*Ra2J7f)`jj?CAN+*M+nM#p5CPG354_(Xfux7Gua<`T@a{o)VU z`7dS<@MdIUV89)ZK(8YKoUw{-40=2v%t!|^k&Ho$HDq(JMFYZ|3ovt#A|=3^l?|kV P83@k;>0`jiV_*OPT7=v< literal 0 HcmV?d00001 diff --git a/ext/zip/examples/test_im.zip b/ext/zip/examples/test_im.zip new file mode 100644 index 0000000000000000000000000000000000000000..6385ee925249efaa79da331069aff58cfc7dd110 GIT binary patch literal 1086 zcmWIWW@Zs#U|`^2c(zH$WOcEog#nOf48)Q^T#{N`l9rj1T98qoS5i@8XlQJ1XkcJz zX&h`8!g2D1P8h=?m)1#F9tpKRnzZQ5o|4uhQ>I)IX+4s(At5TwFU!%bG0xMqQ%;;A z0L2=$rtl|*K;un-SPIn|BXoO$h_h!RhDGmdbWEO3kybqcv|te<1A_p}qN4m7(ZTeCyw!xjLI|;;*l} zRgn8;XZ{?Yl?#Jwk{!ElghU-n7HXEvMwe7dCH$e zuYIcwnvYnvwjDUd*Ra2J7f)`jj?CAN+*M+nM#p5CPG354_(Xfux7Gua<`T@a{o)VU z`7dS%1uqxSe+&#!yJJi?bxrc(1^O6_8AKThQWJ~fGfPr)_0lubLPIzin03yvxU4xL z;u2cH!tjFm1+%-Sn}ubfAcHW&5wIy*Qy4TRGw4oZ(4WC zoWtI7C~fY!w(GCY-hY4n*{25|e!MZ8#cV&H*JH7G>L#mz6>?vGevDeH^7GI4ntk5C z|Ni{<|L@;_fByXa{T~c;Kvsb4Vql%LK)p|;(j@g&;Vq|ytI`caVk0-Q2MSLO6`qk5 zc+y90D#OyrMFt|0ClwrfEj%W$8VPwS?2CK2&f{g>{$|b;mma=z&l!@h%t#T*w@_MG z(IX))Qdw2wq$e6zmB5}OBHG;2qtwvURm~Ny#8(#7#Uvu566MyJ!Rn+WVK3gv$;eWy zDZ|sj%*-Y-$xw`aFR!$wB7=2+HzN}R1MW-#4k0*!GmD@bgP!Q&CNQJ}nMlT|C<;_5%nafI E0BuKP>i_@% literal 0 HcmV?d00001 diff --git a/ext/zip/examples/test_with_comment.zip b/ext/zip/examples/test_with_comment.zip new file mode 100644 index 0000000000000000000000000000000000000000..cc65375fb5720b6bd74a06b1ecef083eff3dca75 GIT binary patch literal 560 zcmWIWW@h1H0D-bb7GoXxC9hb4Y!GH<5M@Zq&kqgZWMGz8`sCyX#Gw@|3||;iK;m3n z0dTDqK&>w~FJfTvcTA5VPFROl?8}}8KN=^6cUS) QGcwCk6_WFFb5rw505e=|{{R30 literal 0 HcmV?d00001 diff --git a/ext/zip/examples/too.php b/ext/zip/examples/too.php new file mode 100644 index 0000000000..6d2100811e --- /dev/null +++ b/ext/zip/examples/too.php @@ -0,0 +1,2 @@ + +#include + +#include +#include +#include +#include +#include +#include +#include + + +int +_zip_mkstemp(char *path) +{ + int fd; + char *start, *trv; + struct stat sbuf; + pid_t pid; + + /* To guarantee multiple calls generate unique names even if + the file is not created. 676 different possibilities with 7 + or more X's, 26 with 6 or less. */ + static char xtra[2] = "aa"; + int xcnt = 0; + + pid = getpid(); + + /* Move to end of path and count trailing X's. */ + for (trv = path; *trv; ++trv) + if (*trv == 'X') + xcnt++; + else + xcnt = 0; + + /* Use at least one from xtra. Use 2 if more than 6 X's. */ + if (*(trv - 1) == 'X') + *--trv = xtra[0]; + if (xcnt > 6 && *(trv - 1) == 'X') + *--trv = xtra[1]; + + /* Set remaining X's to pid digits with 0's to the left. */ + while (*--trv == 'X') { + *trv = (pid % 10) + '0'; + pid /= 10; + } + + /* update xtra for next call. */ + if (xtra[0] != 'z') + xtra[0]++; + else { + xtra[0] = 'a'; + if (xtra[1] != 'z') + xtra[1]++; + else + xtra[1] = 'a'; + } + + /* + * check the target directory; if you have six X's and it + * doesn't exist this runs for a *very* long time. + */ + for (start = trv + 1;; --trv) { + if (trv <= path) + break; + if (*trv == '/') { + *trv = '\0'; + if (stat(path, &sbuf)) + return (0); + if (!S_ISDIR(sbuf.st_mode)) { + errno = ENOTDIR; + return (0); + } + *trv = '/'; + break; + } + } + + for (;;) { + if ((fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0) + return (1); + if (errno != EEXIST) + return (0); + + /* tricky little algorithm for backward compatibility */ + for (trv = start;;) { + if (!*trv) + return (0); + if (*trv == 'z') + *trv++ = 'a'; + else { + if (isdigit((unsigned char)*trv)) + *trv = 'a'; + else + ++*trv; + break; + } + } + } + /*NOTREACHED*/ +} diff --git a/ext/zip/lib/unistd.h b/ext/zip/lib/unistd.h new file mode 100644 index 0000000000..2ef435c012 --- /dev/null +++ b/ext/zip/lib/unistd.h @@ -0,0 +1,3 @@ +#ifndef _MSC_VER +#include +#endif diff --git a/ext/zip/lib/zip.h b/ext/zip/lib/zip.h new file mode 100644 index 0000000000..93c791f14c --- /dev/null +++ b/ext/zip/lib/zip.h @@ -0,0 +1,205 @@ +#ifndef _HAD_ZIP_H +#define _HAD_ZIP_H + +/* + $NiH: zip.h,v 1.57 2006/04/24 14:04:19 dillo Exp $ + + zip.h -- exported declarations. + Copyright (C) 1999-2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#include "zip_win32.h" + +/* flags for zip_open */ + +#define ZIP_CREATE 1 +#define ZIP_EXCL 2 +#define ZIP_CHECKCONS 4 + + +/* flags for zip_name_locate, zip_fopen, zip_stat, ... */ + +#define ZIP_FL_NOCASE 1 /* ignore case on name lookup */ +#define ZIP_FL_NODIR 2 /* ignore directory component */ +#define ZIP_FL_COMPRESSED 4 /* read compressed data */ +#define ZIP_FL_UNCHANGED 8 /* use original data, ignoring changes */ + +/* libzip error codes */ + +#define ZIP_ER_OK 0 /* N No error */ +#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */ +#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */ +#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */ +#define ZIP_ER_SEEK 4 /* S Seek error */ +#define ZIP_ER_READ 5 /* S Read error */ +#define ZIP_ER_WRITE 6 /* S Write error */ +#define ZIP_ER_CRC 7 /* N CRC error */ +#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */ +#define ZIP_ER_NOENT 9 /* N No such file */ +#define ZIP_ER_EXISTS 10 /* N File already exists */ +#define ZIP_ER_OPEN 11 /* S Can't open file */ +#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */ +#define ZIP_ER_ZLIB 13 /* Z Zlib error */ +#define ZIP_ER_MEMORY 14 /* N Malloc failure */ +#define ZIP_ER_CHANGED 15 /* N Entry has been changed */ +#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */ +#define ZIP_ER_EOF 17 /* N Premature EOF */ +#define ZIP_ER_INVAL 18 /* N Invalid argument */ +#define ZIP_ER_NOZIP 19 /* N Not a zip archive */ +#define ZIP_ER_INTERNAL 20 /* N Internal error */ +#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */ +#define ZIP_ER_REMOVE 22 /* S Can't remove file */ +#define ZIP_ER_DELETED 23 /* N Entry has been deleted */ + + +/* type of system error value */ + +#define ZIP_ET_NONE 0 /* sys_err unused */ +#define ZIP_ET_SYS 1 /* sys_err is errno */ +#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */ + +/* compression methods */ + +#define ZIP_CM_DEFAULT -1 /* better of deflate or store */ +#define ZIP_CM_STORE 0 /* stored (uncompressed) */ +#define ZIP_CM_SHRINK 1 /* shrunk */ +#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */ +#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */ +#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */ +#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */ +#define ZIP_CM_IMPLODE 6 /* imploded */ +/* 7 - Reserved for Tokenizing compression algorithm */ +#define ZIP_CM_DEFLATE 8 /* deflated */ +#define ZIP_CM_DEFLATE64 9 /* deflate64 */ +#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */ +/* 11 - Reserved by PKWARE */ +#define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */ + +/* encryption methods */ + +#define ZIP_EM_NONE 0 /* not encrypted */ +#define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */ +#if 0 /* Strong Encryption Header not parsed yet */ +#define ZIP_EM_DES 0x6601 /* strong encryption: DES */ +#define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */ +#define ZIP_EM_3DES_168 0x6603 +#define ZIP_EM_3DES_112 0x6609 +#define ZIP_EM_AES_128 0x660e +#define ZIP_EM_AES_192 0x660f +#define ZIP_EM_AES_256 0x6610 +#define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */ +#define ZIP_EM_RC4 0x6801 +#endif +#define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */ + + + +enum zip_source_cmd { + ZIP_SOURCE_OPEN, /* prepare for reading */ + ZIP_SOURCE_READ, /* read data */ + ZIP_SOURCE_CLOSE, /* reading is done */ + ZIP_SOURCE_STAT, /* get meta information */ + ZIP_SOURCE_ERROR, /* get error information */ + ZIP_SOURCE_FREE /* cleanup and free resources */ +}; + +typedef ssize_t (*zip_source_callback)(void *state, void *data, + size_t len, enum zip_source_cmd cmd); + +struct zip_stat { + const char *name; /* name of the file */ + int index; /* index within archive */ + unsigned int crc; /* crc of file data */ + time_t mtime; /* modification time */ + off_t size; /* size of file (uncompressed) */ + off_t comp_size; /* size of file (compressed) */ + unsigned short comp_method; /* compression method used */ + unsigned short encryption_method; /* encryption method used */ +}; + +struct zip; +struct zip_file; +struct zip_source; + + + +int zip_add(struct zip *, const char *, struct zip_source *); +int zip_close(struct zip *); +int zip_delete(struct zip *, int); +void zip_error_get(struct zip *, int *, int *); +int zip_error_get_sys_type(int); +int zip_error_to_str(char *, size_t, int, int); +int zip_fclose(struct zip_file *); +void zip_file_error_get(struct zip_file *, int *, int *); +const char *zip_file_strerror(struct zip_file *); +struct zip_file *zip_fopen(struct zip *, const char *, int); +struct zip_file *zip_fopen_index(struct zip *, int, int); +ssize_t zip_fread(struct zip_file *, void *, size_t); +const char *zip_get_archive_comment(struct zip *, int *, int); +const char *zip_get_file_comment(struct zip *, int, int *, int); +const char *zip_get_name(struct zip *, int, int); +int zip_get_num_files(struct zip *); +int zip_name_locate(struct zip *, const char *, int); +struct zip *zip_open(const char *, int, int *); +int zip_rename(struct zip *, int, const char *); +int zip_replace(struct zip *, int, struct zip_source *); +int zip_set_archive_comment(struct zip *, const char *, int); +int zip_set_file_comment(struct zip *, int, const char *, int); +struct zip_source *zip_source_buffer(struct zip *, const void *, off_t, int); +struct zip_source *zip_source_file(struct zip *, const char *, off_t, off_t); +struct zip_source *zip_source_filep(struct zip *, FILE *, off_t, off_t); +void zip_source_free(struct zip_source *); +struct zip_source *zip_source_function(struct zip *, + zip_source_callback, void *); +struct zip_source *zip_source_zip(struct zip *, struct zip *, int, int, + off_t, off_t); +int zip_stat(struct zip *, const char *, int, struct zip_stat *); +int zip_stat_index(struct zip *, int, int, struct zip_stat *); +const char *zip_strerror(struct zip *); +int zip_unchange(struct zip *, int); +int zip_unchange_all(struct zip *); +int zip_unchange_archive(struct zip *); + +#ifdef __cplusplus +} +#endif + +#endif /* _HAD_ZIP_H */ diff --git a/ext/zip/lib/zip.h.orig b/ext/zip/lib/zip.h.orig new file mode 100644 index 0000000000..bf20ab4689 --- /dev/null +++ b/ext/zip/lib/zip.h.orig @@ -0,0 +1,170 @@ +#ifndef _HAD_ZIP_H +#define _HAD_ZIP_H + +/* + $NiH: zip.h,v 1.49 2005/06/09 19:57:09 dillo Exp $ + + zip.h -- exported declarations. + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include + +/* flags for zip_open */ + +#define ZIP_CREATE 1 +#define ZIP_EXCL 2 +#define ZIP_CHECKCONS 4 + + +/* flags for zip_name_locate, zip_fopen, zip_stat, ... */ + +#define ZIP_FL_NOCASE 1 /* ignore case on name lookup */ +#define ZIP_FL_NODIR 2 /* ignore directory component */ +#define ZIP_FL_COMPRESSED 4 /* read compressed data */ +#define ZIP_FL_UNCHANGED 8 /* use original data, ignoring changes */ + +/* libzip error codes */ + +#define ZIP_ER_OK 0 /* N No error */ +#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */ +#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */ +#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */ +#define ZIP_ER_SEEK 4 /* S Seek error */ +#define ZIP_ER_READ 5 /* S Read error */ +#define ZIP_ER_WRITE 6 /* S Write error */ +#define ZIP_ER_CRC 7 /* N CRC error */ +#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */ +#define ZIP_ER_NOENT 9 /* N No such file */ +#define ZIP_ER_EXISTS 10 /* N File already exists */ +#define ZIP_ER_OPEN 11 /* S Can't open file */ +#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */ +#define ZIP_ER_ZLIB 13 /* Z Zlib error */ +#define ZIP_ER_MEMORY 14 /* N Malloc failure */ +#define ZIP_ER_CHANGED 15 /* N Entry has been changed */ +#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */ +#define ZIP_ER_EOF 17 /* N Premature EOF */ +#define ZIP_ER_INVAL 18 /* N Invalid argument */ +#define ZIP_ER_NOZIP 19 /* N Not a zip archive */ +#define ZIP_ER_INTERNAL 20 /* N Internal error */ +#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */ +#define ZIP_ER_REMOVE 22 /* S Can't remove file */ +#define ZIP_ER_DELETED 23 /* N Entry has been deleted */ + + +/* type of system error value */ + +#define ZIP_ET_NONE 0 /* sys_err unused */ +#define ZIP_ET_SYS 1 /* sys_err is errno */ +#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */ + +/* compression methods */ + +#define ZIP_CM_DEFAULT -1 /* better of deflate or store */ +#define ZIP_CM_STORE 0 /* stored (uncompressed) */ +#define ZIP_CM_SHRINK 1 /* shrunk */ +#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */ +#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */ +#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */ +#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */ +#define ZIP_CM_IMPLODE 6 /* imploded */ +/* 7 - Reserved for Tokenizing compression algorithm */ +#define ZIP_CM_DEFLATE 8 /* deflated */ +#define ZIP_CM_DEFLATE64 9 /* deflate64 */ +#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */ + + + +enum zip_source_cmd { + ZIP_SOURCE_OPEN, /* prepare for reading */ + ZIP_SOURCE_READ, /* read data */ + ZIP_SOURCE_CLOSE, /* reading is done */ + ZIP_SOURCE_STAT, /* get meta information */ + ZIP_SOURCE_ERROR, /* get error information */ + ZIP_SOURCE_FREE /* cleanup and free resources */ +}; + +typedef ssize_t (*zip_source_callback)(void *state, void *data, + size_t len, enum zip_source_cmd cmd); + +struct zip_stat { + const char *name; /* name of the file */ + int index; /* index within archive */ + unsigned int crc; /* crc of file data */ + time_t mtime; /* modification time */ + off_t size; /* size of file (uncompressed) */ + off_t comp_size; /* size of file (compressed) */ + unsigned short comp_method; /* compression method used */ +}; + +struct zip; +struct zip_file; +struct zip_source; + + + +int zip_add(struct zip *, const char *, struct zip_source *); +int zip_close(struct zip *); +int zip_delete(struct zip *, int); +void zip_error_get(struct zip *, int *, int *); +int zip_error_get_sys_type(int); +int zip_error_to_str(char *, size_t, int, int); +int zip_fclose(struct zip_file *); +void zip_file_error_get(struct zip_file *, int *, int *); +const char *zip_file_strerror(struct zip_file *); +struct zip_file *zip_fopen(struct zip *, const char *, int); +struct zip_file *zip_fopen_index(struct zip *, int, int); +ssize_t zip_fread(struct zip_file *, void *, size_t); +const char *zip_get_name(struct zip *, int, int); +int zip_get_num_files(struct zip *); +int zip_name_locate(struct zip *, const char *, int); +struct zip *zip_open(const char *, int, int *); +int zip_rename(struct zip *, int, const char *); +int zip_replace(struct zip *, int, struct zip_source *); +struct zip_source *zip_source_buffer(struct zip *, const void *, off_t, int); +struct zip_source *zip_source_file(struct zip *, const char *, off_t, off_t); +struct zip_source *zip_source_filep(struct zip *, FILE *, off_t, off_t); +void zip_source_free(struct zip_source *); +struct zip_source *zip_source_function(struct zip *, + zip_source_callback, void *); +struct zip_source *zip_source_zip(struct zip *, struct zip *, int, int, + off_t, off_t); +int zip_stat(struct zip *, const char *, int, struct zip_stat *); +int zip_stat_index(struct zip *, int, int, struct zip_stat *); +const char *zip_strerror(struct zip *); +int zip_unchange(struct zip *, int); +int zip_unchange_all(struct zip *); + +#endif /* _HAD_ZIP_H */ diff --git a/ext/zip/lib/zip_add.c b/ext/zip/lib/zip_add.c new file mode 100644 index 0000000000..535d6e4689 --- /dev/null +++ b/ext/zip/lib/zip_add.c @@ -0,0 +1,51 @@ +/* + $NiH: zip_add.c,v 1.14 2004/11/18 15:04:04 wiz Exp $ + + zip_add.c -- add file via callback function + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_add(struct zip *za, const char *name, struct zip_source *source) +{ + if (name == NULL || source == NULL) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + return _zip_replace(za, -1, name, source); +} diff --git a/ext/zip/lib/zip_delete.c b/ext/zip/lib/zip_delete.c new file mode 100644 index 0000000000..7244e612f7 --- /dev/null +++ b/ext/zip/lib/zip_delete.c @@ -0,0 +1,61 @@ +/* + $NiH: zip_delete.c,v 1.17 2005/06/09 19:57:09 dillo Exp $ + + zip_delete.c -- delete file from zip archive + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_delete(struct zip *za, int idx) +{ + if (idx < 0 || idx >= za->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + /* allow duplicate file names, because the file will + * be removed directly afterwards */ + if (_zip_unchange(za, idx, 1) != 0) + return -1; + + za->entry[idx].state = ZIP_ST_DELETED; + + return 0; +} + + diff --git a/ext/zip/lib/zip_dirent.c b/ext/zip/lib/zip_dirent.c new file mode 100644 index 0000000000..f0c988bc70 --- /dev/null +++ b/ext/zip/lib/zip_dirent.c @@ -0,0 +1,532 @@ +/* + $NiH: zip_dirent.c,v 1.9 2006/04/23 14:51:45 wiz Exp $ + + zip_dirent.c -- read directory entry (local or central), clean dirent + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include +#ifndef _MSC_VER +#include +#endif +#include +#include + +#include "zip.h" +#include "zipint.h" + +static time_t _zip_d2u_time(int, int); +static char *_zip_readfpstr(FILE *, unsigned int, int, struct zip_error *); +static char *_zip_readstr(unsigned char **, int, int, struct zip_error *); +static void _zip_u2d_time(time_t, unsigned short *, unsigned short *); +static void _zip_write2(unsigned short, FILE *); +static void _zip_write4(unsigned int, FILE *); + + + +void +_zip_cdir_free(struct zip_cdir *cd) +{ + int i; + + if (!cd) + return; + + for (i=0; inentry; i++) + _zip_dirent_finalize(cd->entry+i); + free(cd->comment); + free(cd->entry); + free(cd); +} + + + +struct zip_cdir * +_zip_cdir_new(int nentry, struct zip_error *error) +{ + struct zip_cdir *cd; + + if ((cd=(struct zip_cdir *)malloc(sizeof(*cd))) == NULL) { + _zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry)) + == NULL) { + _zip_error_set(error, ZIP_ER_MEMORY, 0); + free(cd); + return NULL; + } + + /* entries must be initialized by caller */ + + cd->nentry = nentry; + cd->size = cd->offset = 0; + cd->comment = NULL; + cd->comment_len = 0; + + return cd; +} + + + +int +_zip_cdir_write(struct zip_cdir *cd, FILE *fp, struct zip_error *error) +{ + int i; + + cd->offset = ftell(fp); + + for (i=0; inentry; i++) { + if (_zip_dirent_write(cd->entry+i, fp, 0, error) != 0) + return -1; + } + + cd->size = ftell(fp) - cd->offset; + + /* clearerr(fp); */ + fwrite(EOCD_MAGIC, 1, 4, fp); + _zip_write4(0, fp); + _zip_write2((unsigned short)cd->nentry, fp); + _zip_write2((unsigned short)cd->nentry, fp); + _zip_write4(cd->size, fp); + _zip_write4(cd->offset, fp); + _zip_write2(cd->comment_len, fp); + fwrite(cd->comment, 1, cd->comment_len, fp); + + if (ferror(fp)) { + _zip_error_set(error, ZIP_ER_WRITE, errno); + return -1; + } + + return 0; +} + + + +void +_zip_dirent_finalize(struct zip_dirent *zde) +{ + free(zde->filename); + zde->filename = NULL; + free(zde->extrafield); + zde->extrafield = NULL; + free(zde->comment); + zde->comment = NULL; +} + + + +void +_zip_dirent_init(struct zip_dirent *de) +{ + de->version_madeby = 0; + de->version_needed = 20; /* 2.0 */ + de->bitflags = 0; + de->comp_method = 0; + de->last_mod = 0; + de->crc = 0; + de->comp_size = 0; + de->uncomp_size = 0; + de->filename = NULL; + de->filename_len = 0; + de->extrafield = NULL; + de->extrafield_len = 0; + de->comment = NULL; + de->comment_len = 0; + de->disk_number = 0; + de->int_attrib = 0; + de->ext_attrib = 0; + de->offset = 0; +} + + + +/* _zip_dirent_read(zde, fp, bufp, left, localp, error): + Fills the zip directory entry zde. + + If bufp is non-NULL, data is taken from there and bufp is advanced + by the amount of data used; no more than left bytes are used. + Otherwise data is read from fp as needed. + + If localp != 0, it reads a local header instead of a central + directory entry. + + Returns 0 if successful. On error, error is filled in and -1 is + returned. +*/ + +int +_zip_dirent_read(struct zip_dirent *zde, FILE *fp, + unsigned char **bufp, unsigned int left, int localp, + struct zip_error *error) +{ + unsigned char buf[CDENTRYSIZE]; + unsigned char *cur; + unsigned short dostime, dosdate; + unsigned int size; + + if (localp) + size = LENTRYSIZE; + else + size = CDENTRYSIZE; + + if (bufp) { + /* use data from buffer */ + cur = *bufp; + if (left < size) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return -1; + } + } + else { + /* read entry from disk */ + if ((fread(buf, 1, size, fp)version_madeby = _zip_read2(&cur); + else + zde->version_madeby = 0; + zde->version_needed = _zip_read2(&cur); + zde->bitflags = _zip_read2(&cur); + zde->comp_method = _zip_read2(&cur); + + /* convert to time_t */ + dostime = _zip_read2(&cur); + dosdate = _zip_read2(&cur); + zde->last_mod = _zip_d2u_time(dostime, dosdate); + + zde->crc = _zip_read4(&cur); + zde->comp_size = _zip_read4(&cur); + zde->uncomp_size = _zip_read4(&cur); + + zde->filename_len = _zip_read2(&cur); + zde->extrafield_len = _zip_read2(&cur); + + if (localp) { + zde->comment_len = 0; + zde->disk_number = 0; + zde->int_attrib = 0; + zde->ext_attrib = 0; + zde->offset = 0; + } else { + zde->comment_len = _zip_read2(&cur); + zde->disk_number = _zip_read2(&cur); + zde->int_attrib = _zip_read2(&cur); + zde->ext_attrib = _zip_read4(&cur); + zde->offset = _zip_read4(&cur); + } + + zde->filename = NULL; + zde->extrafield = NULL; + zde->comment = NULL; + + if (bufp) { + if (left < CDENTRYSIZE + (zde->filename_len+zde->extrafield_len + +zde->comment_len)) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return -1; + } + + if (zde->filename_len) { + zde->filename = _zip_readstr(&cur, zde->filename_len, 1, error); + if (!zde->filename) + return -1; + } + + if (zde->extrafield_len) { + zde->extrafield = _zip_readstr(&cur, zde->extrafield_len, 0, + error); + if (!zde->extrafield) + return -1; + } + + if (zde->comment_len) { + zde->comment = _zip_readstr(&cur, zde->comment_len, 0, error); + if (!zde->comment) + return -1; + } + } + else { + if (zde->filename_len) { + zde->filename = _zip_readfpstr(fp, zde->filename_len, 1, error); + if (!zde->filename) + return -1; + } + + if (zde->extrafield_len) { + zde->extrafield = _zip_readfpstr(fp, zde->extrafield_len, 0, + error); + if (!zde->extrafield) + return -1; + } + + if (zde->comment_len) { + zde->comment = _zip_readfpstr(fp, zde->comment_len, 0, error); + if (!zde->comment) + return -1; + } + } + + if (bufp) + *bufp = cur; + + return 0; +} + + + +/* _zip_dirent_write(zde, fp, localp, error): + Writes zip directory entry zde to file fp. + + If localp != 0, it writes a local header instead of a central + directory entry. + + Returns 0 if successful. On error, error is filled in and -1 is + returned. +*/ + +int +_zip_dirent_write(struct zip_dirent *zde, FILE *fp, int localp, + struct zip_error *error) +{ + unsigned short dostime, dosdate; + + fwrite(localp ? LOCAL_MAGIC : CENTRAL_MAGIC, 1, 4, fp); + + if (!localp) + _zip_write2(zde->version_madeby, fp); + _zip_write2(zde->version_needed, fp); + _zip_write2(zde->bitflags, fp); + _zip_write2(zde->comp_method, fp); + + _zip_u2d_time(zde->last_mod, &dostime, &dosdate); + _zip_write2(dostime, fp); + _zip_write2(dosdate, fp); + + _zip_write4(zde->crc, fp); + _zip_write4(zde->comp_size, fp); + _zip_write4(zde->uncomp_size, fp); + + _zip_write2(zde->filename_len, fp); + _zip_write2(zde->extrafield_len, fp); + + if (!localp) { + _zip_write2(zde->comment_len, fp); + _zip_write2(zde->disk_number, fp); + _zip_write2(zde->int_attrib, fp); + _zip_write4(zde->ext_attrib, fp); + _zip_write4(zde->offset, fp); + } + + if (zde->filename_len) + fwrite(zde->filename, 1, zde->filename_len, fp); + + if (zde->extrafield_len) + fwrite(zde->extrafield, 1, zde->extrafield_len, fp); + + if (!localp) { + if (zde->comment_len) + fwrite(zde->comment, 1, zde->comment_len, fp); + } + + if (ferror(fp)) { + _zip_error_set(error, ZIP_ER_WRITE, errno); + return -1; + } + + return 0; +} + + + +static time_t +_zip_d2u_time(int dtime, int ddate) +{ + struct tm *tm; + time_t now; + + now = time(NULL); + tm = localtime(&now); + + tm->tm_year = ((ddate>>9)&127) + 1980 - 1900; + tm->tm_mon = ((ddate>>5)&15) - 1; + tm->tm_mday = ddate&31; + + tm->tm_hour = (dtime>>11)&31; + tm->tm_min = (dtime>>5)&63; + tm->tm_sec = (dtime<<1)&62; + + return mktime(tm); +} + + + +unsigned short +_zip_read2(unsigned char **a) +{ + unsigned short ret; + + ret = (*a)[0]+((*a)[1]<<8); + *a += 2; + + return ret; +} + + + +unsigned int +_zip_read4(unsigned char **a) +{ + unsigned int ret; + + ret = ((((((*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0]; + *a += 4; + + return ret; +} + + + +static char * +_zip_readfpstr(FILE *fp, unsigned int len, int nulp, struct zip_error *error) +{ + char *r, *o; + + r = (char *)malloc(nulp ? len+1 : len); + if (!r) { + _zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + if (fread(r, 1, len, fp)>8)&0xff, fp); + + return; +} + + + +static void +_zip_write4(unsigned int i, FILE *fp) +{ + putc(i&0xff, fp); + putc((i>>8)&0xff, fp); + putc((i>>16)&0xff, fp); + putc((i>>24)&0xff, fp); + + return; +} + + + +static void +_zip_u2d_time(time_t time, unsigned short *dtime, unsigned short *ddate) +{ + struct tm *tm; + + tm = localtime(&time); + *ddate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + + tm->tm_mday; + *dtime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + + ((tm->tm_sec)>>1); + + return; +} diff --git a/ext/zip/lib/zip_entry_free.c b/ext/zip/lib/zip_entry_free.c new file mode 100644 index 0000000000..48443cefd5 --- /dev/null +++ b/ext/zip/lib/zip_entry_free.c @@ -0,0 +1,55 @@ +/* + $NiH: zip_entry_free.c,v 1.2 2006/04/09 19:05:47 wiz Exp $ + + zip_entry_free.c -- free struct zip_entry + Copyright (C) 1999, 2003, 2004, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +void +_zip_entry_free(struct zip_entry *ze) +{ + free(ze->ch_filename); + ze->ch_filename = NULL; + free(ze->ch_comment); + ze->ch_comment = NULL; + ze->ch_comment_len = -1; + + _zip_unchange_data(ze); +} diff --git a/ext/zip/lib/zip_entry_new.c b/ext/zip/lib/zip_entry_new.c new file mode 100644 index 0000000000..390b72ad70 --- /dev/null +++ b/ext/zip/lib/zip_entry_new.c @@ -0,0 +1,81 @@ +/* + $NiH: zip_entry_new.c,v 1.2 2006/04/09 19:05:47 wiz Exp $ + + zip_entry_new.c -- create and init struct zip_entry + Copyright (C) 1999, 2003, 2004, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +struct zip_entry * +_zip_entry_new(struct zip *za) +{ + struct zip_entry *ze; + if (!za) { + ze = (struct zip_entry *)malloc(sizeof(struct zip_entry)); + if (!ze) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + } + else { + if (za->nentry >= za->nentry_alloc-1) { + za->nentry_alloc += 16; + za->entry = (struct zip_entry *)realloc(za->entry, + sizeof(struct zip_entry) + * za->nentry_alloc); + if (!za->entry) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + } + ze = za->entry+za->nentry; + } + + ze->state = ZIP_ST_UNCHANGED; + + ze->ch_filename = NULL; + ze->ch_comment = NULL; + ze->ch_comment_len = -1; + ze->source = NULL; + + if (za) + za->nentry++; + + return ze; +} diff --git a/ext/zip/lib/zip_err_str.c b/ext/zip/lib/zip_err_str.c new file mode 100644 index 0000000000..c74538d64a --- /dev/null +++ b/ext/zip/lib/zip_err_str.c @@ -0,0 +1,72 @@ +/* + This file was generated automatically by ./make_zip_err_str.sh + from ./zip.h; make changes there. + + NiH: make_zip_err_str.sh,v 1.8 2004/11/17 21:55:09 wiz Exp + NiH: zip.h,v 1.57 2006/04/24 14:04:19 dillo Exp + */ + +#include "zip.h" +#include "zipint.h" + + + +const char * const _zip_err_str[] = { + "No error", + "Multi-disk zip archives not supported", + "Renaming temporary file failed", + "Closing zip archive failed", + "Seek error", + "Read error", + "Write error", + "CRC error", + "Containing zip archive was closed", + "No such file", + "File already exists", + "Can't open file", + "Failure to create temporary file", + "Zlib error", + "Malloc failure", + "Entry has been changed", + "Compression method not supported", + "Premature EOF", + "Invalid argument", + "Not a zip archive", + "Internal error", + "Zip archive inconsistent", + "Can't remove file", + "Entry has been deleted", +}; + +const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]); + +#define N ZIP_ET_NONE +#define S ZIP_ET_SYS +#define Z ZIP_ET_ZLIB + +const int _zip_err_type[] = { + N, + N, + S, + S, + S, + S, + S, + N, + N, + N, + N, + S, + S, + Z, + N, + N, + N, + N, + N, + N, + N, + N, + S, + N, +}; diff --git a/ext/zip/lib/zip_error.c b/ext/zip/lib/zip_error.c new file mode 100644 index 0000000000..33a8f3374a --- /dev/null +++ b/ext/zip/lib/zip_error.c @@ -0,0 +1,95 @@ +/* + $NiH: zip_error.c,v 1.7 2005/06/09 19:57:09 dillo Exp $ + + zip_error.c -- struct zip_error helper functions + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +void +_zip_error_copy(struct zip_error *dst, struct zip_error *src) +{ + dst->zip_err = src->zip_err; + dst->sys_err = src->sys_err; +} + + + +void +_zip_error_fini(struct zip_error *err) +{ + free(err->str); + err->str = NULL; +} + + + +void +_zip_error_get(struct zip_error *err, int *zep, int *sep) +{ + if (zep) + *zep = err->zip_err; + if (sep) { + if (zip_error_get_sys_type(err->zip_err) != ZIP_ET_NONE) + *sep = err->sys_err; + else + *sep = 0; + } +} + + + +void +_zip_error_init(struct zip_error *err) +{ + err->zip_err = 0; + err->sys_err = 0; + err->str = NULL; +} + + + +void +_zip_error_set(struct zip_error *err, int ze, int se) +{ + if (err) { + err->zip_err = ze; + err->sys_err = se; + } +} diff --git a/ext/zip/lib/zip_error_get.c b/ext/zip/lib/zip_error_get.c new file mode 100644 index 0000000000..712575a864 --- /dev/null +++ b/ext/zip/lib/zip_error_get.c @@ -0,0 +1,47 @@ +/* + $NiH: zip_error_get.c,v 1.1 2004/11/18 15:06:20 wiz Exp $ + + zip_error_get.c -- get zip error + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +void +zip_error_get(struct zip *za, int *zep, int *sep) +{ + _zip_error_get(&za->error, zep, sep); +} diff --git a/ext/zip/lib/zip_error_get_sys_type.c b/ext/zip/lib/zip_error_get_sys_type.c new file mode 100644 index 0000000000..613ec94328 --- /dev/null +++ b/ext/zip/lib/zip_error_get_sys_type.c @@ -0,0 +1,50 @@ +/* + $NiH: zip_error_get_sys_type.c,v 1.1 2004/12/22 15:49:18 wiz Exp $ + + zip_error_get_sys_type.c -- return type of system error code + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_error_get_sys_type(int ze) +{ + if (ze < 0 || ze >= _zip_nerr_str) + return 0; + + return _zip_err_type[ze]; +} diff --git a/ext/zip/lib/zip_error_strerror.c b/ext/zip/lib/zip_error_strerror.c new file mode 100644 index 0000000000..f14f7190e6 --- /dev/null +++ b/ext/zip/lib/zip_error_strerror.c @@ -0,0 +1,93 @@ +/* + $NiH: zip_error_strerror.c,v 1.4 2006/02/21 09:41:00 dillo Exp $ + + zip_error_sterror.c -- get string representation of struct zip_error + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include + +#include "zip.h" +#include "zipint.h" + + + +const char * +_zip_error_strerror(struct zip_error *err) +{ + const char *zs, *ss; + char buf[128], *s; + + _zip_error_fini(err); + + if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) { + sprintf(buf, "Unknown error %d", err->zip_err); + zs = NULL; + ss = buf; + } + else { + zs = _zip_err_str[err->zip_err]; + + switch (_zip_err_type[err->zip_err]) { + case ZIP_ET_SYS: + ss = strerror(err->sys_err); + break; + + case ZIP_ET_ZLIB: + ss = zError(err->sys_err); + break; + + default: + ss = NULL; + } + } + + if (ss == NULL) + return zs; + else { + if ((s=(char *)malloc(strlen(ss) + + (zs ? strlen(zs)+2 : 0) + 1)) == NULL) + return _zip_err_str[ZIP_ER_MEMORY]; + + sprintf(s, "%s%s%s", + (zs ? zs : ""), + (zs ? ": " : ""), + ss); + err->str = s; + + return ss; + } +} diff --git a/ext/zip/lib/zip_error_to_str.c b/ext/zip/lib/zip_error_to_str.c new file mode 100644 index 0000000000..e566192255 --- /dev/null +++ b/ext/zip/lib/zip_error_to_str.c @@ -0,0 +1,73 @@ +/* + $NiH: zip_error_to_str.c,v 1.1 2004/11/18 15:06:20 wiz Exp $ + + zip_error_to_str.c -- get string representation of zip error code + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include + +#include "zip.h" +#include "zipint.h" + + + +int +zip_error_to_str(char *buf, size_t len, int ze, int se) +{ + const char *zs, *ss; + + if (ze < 0 || ze >= _zip_nerr_str) + return snprintf(buf, len, "Unknown error %d", ze); + + zs = _zip_err_str[ze]; + + switch (_zip_err_type[ze]) { + case ZIP_ET_SYS: + ss = strerror(se); + break; + + case ZIP_ET_ZLIB: + ss = zError(se); + break; + + default: + ss = NULL; + } + + return snprintf(buf, len, "%s%s%s", + zs, (ss ? ": " : ""), (ss ? ss : "")); +} diff --git a/ext/zip/lib/zip_fclose.c b/ext/zip/lib/zip_fclose.c new file mode 100644 index 0000000000..cf4f35c71c --- /dev/null +++ b/ext/zip/lib/zip_fclose.c @@ -0,0 +1,74 @@ +/* + $NiH: zip_fclose.c,v 1.14 2005/06/09 19:57:09 dillo Exp $ + + zip_fclose.c -- close file in zip archive + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +int +zip_fclose(struct zip_file *zf) +{ + int i, ret; + + if (zf->zstr) + inflateEnd(zf->zstr); + free(zf->buffer); + free(zf->zstr); + + for (i=0; iza->nfile; i++) { + if (zf->za->file[i] == zf) { + zf->za->file[i] = zf->za->file[zf->za->nfile-1]; + zf->za->nfile--; + break; + } + } + + ret = 0; + if (zf->error.zip_err) + ret = zf->error.zip_err; + else if ((zf->flags & ZIP_ZF_CRC) && (zf->flags & ZIP_ZF_EOF)) { + /* if EOF, compare CRC */ + if (zf->crc_orig != zf->crc) + ret = ZIP_ER_CRC; + } + + free(zf); + return ret; +} diff --git a/ext/zip/lib/zip_file_error_get.c b/ext/zip/lib/zip_file_error_get.c new file mode 100644 index 0000000000..2ab3a73a4c --- /dev/null +++ b/ext/zip/lib/zip_file_error_get.c @@ -0,0 +1,47 @@ +/* + $NiH: zip_file_error_get.c,v 1.1 2004/11/18 15:06:21 wiz Exp $ + + zip_file_error_get.c -- get zip file error + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +void +zip_file_error_get(struct zip_file *zf, int *zep, int *sep) +{ + _zip_error_get(&zf->error, zep, sep); +} diff --git a/ext/zip/lib/zip_file_get_offset.c b/ext/zip/lib/zip_file_get_offset.c new file mode 100644 index 0000000000..8bcc64934d --- /dev/null +++ b/ext/zip/lib/zip_file_get_offset.c @@ -0,0 +1,80 @@ +/* + $NiH: zip_file_get_offset.c,v 1.4 2006/04/23 14:51:45 wiz Exp $ + + zip_file_get_offset.c -- get offset of file data in archive. + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include +#ifndef _MSC_VER +#include +#endif +#include +#include + +#include "zip.h" +#include "zipint.h" + + + +/* _zip_file_get_offset(za, ze): + Returns the offset of the file data for entry ze. + + On error, fills in za->error and returns 0. +*/ + +unsigned int +_zip_file_get_offset(struct zip *za, int idx) +{ + struct zip_dirent de; + unsigned int offset; + + offset = za->cdir->entry[idx].offset; + + if (fseek(za->zp, offset, SEEK_SET) != 0) { + _zip_error_set(&za->error, ZIP_ER_SEEK, errno); + return 0; + } + + if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) + return 0; + + offset += LENTRYSIZE + de.filename_len + de.extrafield_len; + + _zip_dirent_finalize(&de); + + return offset; +} diff --git a/ext/zip/lib/zip_file_strerror.c b/ext/zip/lib/zip_file_strerror.c new file mode 100644 index 0000000000..aaff189996 --- /dev/null +++ b/ext/zip/lib/zip_file_strerror.c @@ -0,0 +1,47 @@ +/* + $NiH: zip_file_strerror.c,v 1.1 2003/10/05 16:05:22 dillo Exp $ + + zip_file_sterror.c -- get string representation of zip file error + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +const char * +zip_file_strerror(struct zip_file *zf) +{ + return _zip_error_strerror(&zf->error); +} diff --git a/ext/zip/lib/zip_fopen.c b/ext/zip/lib/zip_fopen.c new file mode 100644 index 0000000000..850a8d835a --- /dev/null +++ b/ext/zip/lib/zip_fopen.c @@ -0,0 +1,52 @@ +/* + $NiH: zip_fopen.c,v 1.12 2005/06/09 19:57:09 dillo Exp $ + + zip_fopen.c -- open file in zip archive for reading + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +struct zip_file * +zip_fopen(struct zip *za, const char *fname, int flags) +{ + int idx; + + if ((idx=zip_name_locate(za, fname, flags)) < 0) + return NULL; + + return zip_fopen_index(za, idx, flags); +} diff --git a/ext/zip/lib/zip_fopen_index.c b/ext/zip/lib/zip_fopen_index.c new file mode 100644 index 0000000000..5da9230b2e --- /dev/null +++ b/ext/zip/lib/zip_fopen_index.c @@ -0,0 +1,219 @@ +/* + $NiH: zip_fopen_index.c,v 1.24 2005/05/20 21:54:53 wiz Exp $ + + zip_fopen_index.c -- open file in zip archive for reading by index + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include + +#include "zip.h" +#include "zipint.h" + +static struct zip_file *_zip_file_new(struct zip *za); + + + +struct zip_file * +zip_fopen_index(struct zip *za, int fileno, int flags) +{ + int len, ret; + int zfflags; + struct zip_file *zf; + + if ((fileno < 0) || (fileno >= za->nentry)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((flags & ZIP_FL_UNCHANGED) == 0 + && ZIP_ENTRY_DATA_CHANGED(za->entry+fileno)) { + _zip_error_set(&za->error, ZIP_ER_CHANGED, 0); + return NULL; + } + + if (fileno >= za->cdir->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + zfflags = 0; + switch (za->cdir->entry[fileno].comp_method) { + case ZIP_CM_STORE: + zfflags |= ZIP_ZF_CRC; + break; + + case ZIP_CM_DEFLATE: + if ((flags & ZIP_FL_COMPRESSED) == 0) + zfflags |= ZIP_ZF_CRC | ZIP_ZF_DECOMP; + break; + default: + if ((flags & ZIP_FL_COMPRESSED) == 0) { + _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); + return NULL; + } + break; + } + + zf = _zip_file_new(za); + + zf->flags = zfflags; + /* zf->name = za->cdir->entry[fileno].filename; */ + zf->method = za->cdir->entry[fileno].comp_method; + zf->bytes_left = za->cdir->entry[fileno].uncomp_size; + zf->cbytes_left = za->cdir->entry[fileno].comp_size; + zf->crc_orig = za->cdir->entry[fileno].crc; + + if ((zf->fpos=_zip_file_get_offset(za, fileno)) == 0) { + zip_fclose(zf); + return NULL; + } + + if ((zf->flags & ZIP_ZF_DECOMP) == 0) + zf->bytes_left = zf->cbytes_left; + else { + if ((zf->buffer=(char *)malloc(BUFSIZE)) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + zip_fclose(zf); + return NULL; + } + + len = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf); + if (len <= 0) { + _zip_error_copy(&za->error, &zf->error); + zip_fclose(zf); + return NULL; + } + + if ((zf->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + zip_fclose(zf); + return NULL; + } + zf->zstr->zalloc = Z_NULL; + zf->zstr->zfree = Z_NULL; + zf->zstr->opaque = NULL; + zf->zstr->next_in = (Bytef *)zf->buffer; + zf->zstr->avail_in = len; + + /* negative value to tell zlib that there is no header */ + if ((ret=inflateInit2(zf->zstr, -MAX_WBITS)) != Z_OK) { + _zip_error_set(&za->error, ZIP_ER_ZLIB, ret); + zip_fclose(zf); + return NULL; + } + } + + return zf; +} + + + +int +_zip_file_fillbuf(void *buf, size_t buflen, struct zip_file *zf) +{ + int i, j; + + if (zf->error.zip_err != ZIP_ER_OK) + return -1; + + if ((zf->flags & ZIP_ZF_EOF) || zf->cbytes_left <= 0 || buflen <= 0) + return 0; + + if (fseek(zf->za->zp, zf->fpos, SEEK_SET) < 0) { + _zip_error_set(&zf->error, ZIP_ER_SEEK, errno); + return -1; + } + if (buflen < zf->cbytes_left) + i = buflen; + else + i = zf->cbytes_left; + + j = fread(buf, 1, i, zf->za->zp); + if (j == 0) { + _zip_error_set(&zf->error, ZIP_ER_EOF, 0); + j = -1; + } + else if (j < 0) + _zip_error_set(&zf->error, ZIP_ER_READ, errno); + else { + zf->fpos += j; + zf->cbytes_left -= j; + } + + return j; +} + + + +static struct zip_file * +_zip_file_new(struct zip *za) +{ + struct zip_file *zf, **file; + int n; + + if ((zf=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + + if (za->nfile >= za->nfile_alloc-1) { + n = za->nfile_alloc + 10; + file = (struct zip_file **)realloc(za->file, + n*sizeof(struct zip_file *)); + if (file == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + free(zf); + return NULL; + } + za->nfile_alloc = n; + za->file = file; + } + + za->file[za->nfile++] = zf; + + zf->za = za; + _zip_error_init(&zf->error); + zf->flags = 0; + zf->crc = crc32(0L, Z_NULL, 0); + zf->crc_orig = 0; + zf->method = -1; + zf->bytes_left = zf->cbytes_left = 0; + zf->fpos = 0; + zf->buffer = NULL; + zf->zstr = NULL; + + return zf; +} diff --git a/ext/zip/lib/zip_fread.c b/ext/zip/lib/zip_fread.c new file mode 100644 index 0000000000..aeb64c90fa --- /dev/null +++ b/ext/zip/lib/zip_fread.c @@ -0,0 +1,125 @@ +/* + $NiH: zip_fread.c,v 1.21 2006/04/23 14:49:50 wiz Exp $ + + zip_fread.c -- read from file + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +ssize_t +zip_fread(struct zip_file *zf, void *outbuf, size_t toread) +{ + int ret; + size_t out_before, len; + int i; + + if (!zf) + return -1; + + if (zf->error.zip_err != 0) + return -1; + + if ((zf->flags & ZIP_ZF_EOF) || (toread == 0)) + return 0; + + if (zf->bytes_left == 0) { + zf->flags |= ZIP_ZF_EOF; + if (zf->flags & ZIP_ZF_CRC) { + if (zf->crc != zf->crc_orig) { + _zip_error_set(&zf->error, ZIP_ER_CRC, 0); + return -1; + } + } + return 0; + } + + if ((zf->flags & ZIP_ZF_DECOMP) == 0) { + ret = _zip_file_fillbuf(outbuf, toread, zf); + if (ret > 0) { + if (zf->flags & ZIP_ZF_CRC) + zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret); + zf->bytes_left -= ret; + } + return ret; + } + + zf->zstr->next_out = (Bytef *)outbuf; + zf->zstr->avail_out = toread; + out_before = zf->zstr->total_out; + + /* endless loop until something has been accomplished */ + for (;;) { + ret = inflate(zf->zstr, Z_SYNC_FLUSH); + + switch (ret) { + case Z_OK: + case Z_STREAM_END: + /* all ok */ + /* Z_STREAM_END probably won't happen, since we didn't + have a header */ + len = zf->zstr->total_out - out_before; + if (len >= zf->bytes_left || len >= toread) { + if (zf->flags & ZIP_ZF_CRC) + zf->crc = crc32(zf->crc, (Bytef *)outbuf, len); + zf->bytes_left -= len; + return len; + } + break; + + case Z_BUF_ERROR: + if (zf->zstr->avail_in == 0) { + i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf); + if (i == 0) { + _zip_error_set(&zf->error, ZIP_ER_INCONS, 0); + return -1; + } + else if (i < 0) + return -1; + zf->zstr->next_in = (Bytef *)zf->buffer; + zf->zstr->avail_in = i; + continue; + } + /* fallthrough */ + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_STREAM_ERROR: + case Z_MEM_ERROR: + _zip_error_set(&zf->error, ZIP_ER_ZLIB, ret); + return -1; + } + } +} diff --git a/ext/zip/lib/zip_free.c b/ext/zip/lib/zip_free.c new file mode 100644 index 0000000000..cbead2a6b7 --- /dev/null +++ b/ext/zip/lib/zip_free.c @@ -0,0 +1,83 @@ +/* + $NiH: zip_free.c,v 1.17 2005/06/09 19:57:10 dillo Exp $ + + zip_free.c -- free struct zip + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include "zip.h" +#include "zipint.h" + + + +/* _zip_free: + frees the space allocated to a zipfile struct, and closes the + corresponding file. */ + +void +_zip_free(struct zip *za) +{ + int i; + + if (za == NULL) + return; + + if (za->zn) + free(za->zn); + + if (za->zp) + fclose(za->zp); + + _zip_cdir_free(za->cdir); + + if (za->entry) { + for (i=0; inentry; i++) { + _zip_entry_free(za->entry+i); + } + free(za->entry); + } + + for (i=0; infile; i++) { + if (za->file[i]->error.zip_err == ZIP_ER_OK) { + _zip_error_set(&za->file[i]->error, ZIP_ER_ZIPCLOSED, 0); + za->file[i]->za = NULL; + } + } + + free(za->file); + + free(za); + + return; +} diff --git a/ext/zip/lib/zip_get_archive_comment.c b/ext/zip/lib/zip_get_archive_comment.c new file mode 100644 index 0000000000..6e68f2f4d9 --- /dev/null +++ b/ext/zip/lib/zip_get_archive_comment.c @@ -0,0 +1,56 @@ +/* + $NiH: zip_get_archive_comment.c,v 1.4 2006/04/23 16:11:33 wiz Exp $ + + zip_get_archive_comment.c -- get archive comment + Copyright (C) 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +const char * +zip_get_archive_comment(struct zip *za, int *lenp, int flags) +{ + if ((flags & ZIP_FL_UNCHANGED) + || (za->ch_comment_len == -1)) { + if (lenp != NULL) + *lenp = za->cdir->comment_len; + return za->cdir->comment; + } + + if (lenp != NULL) + *lenp = za->ch_comment_len; + return za->ch_comment; +} diff --git a/ext/zip/lib/zip_get_file_comment.c b/ext/zip/lib/zip_get_file_comment.c new file mode 100644 index 0000000000..79a5c236bc --- /dev/null +++ b/ext/zip/lib/zip_get_file_comment.c @@ -0,0 +1,61 @@ +/* + $NiH: zip_get_file_comment.c,v 1.2 2006/04/23 13:06:28 wiz Exp $ + + zip_get_file_comment.c -- get file comment + Copyright (C) 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +const char * +zip_get_file_comment(struct zip *za, int idx, int *lenp, int flags) +{ + if (idx < 0 || idx >= za->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((flags & ZIP_FL_UNCHANGED) + || (za->entry[idx].ch_comment_len == -1)) { + if (lenp != NULL) + *lenp = za->cdir->entry[idx].comment_len; + return za->cdir->entry[idx].comment; + } + + if (lenp != NULL) + *lenp = za->entry[idx].ch_comment_len; + return za->entry[idx].ch_comment; +} diff --git a/ext/zip/lib/zip_get_name.c b/ext/zip/lib/zip_get_name.c new file mode 100644 index 0000000000..c45dd0333f --- /dev/null +++ b/ext/zip/lib/zip_get_name.c @@ -0,0 +1,74 @@ +/* + $NiH: zip_get_name.c,v 1.13 2005/01/20 21:00:54 dillo Exp $ + + zip_get_name.c -- get filename for a file in zip file + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +const char * +zip_get_name(struct zip *za, int idx, int flags) +{ + return _zip_get_name(za, idx, flags, &za->error); +} + + + +const char * +_zip_get_name(struct zip *za, int idx, int flags, struct zip_error *error) +{ + if (idx < 0 || idx >= za->nentry) { + _zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((flags & ZIP_FL_UNCHANGED) == 0) { + if (za->entry[idx].state == ZIP_ST_DELETED) { + _zip_error_set(error, ZIP_ER_DELETED, 0); + return NULL; + } + if (za->entry[idx].ch_filename) + return za->entry[idx].ch_filename; + } + + if (za->cdir == NULL || idx >= za->cdir->nentry) { + _zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } + + return za->cdir->entry[idx].filename; +} diff --git a/ext/zip/lib/zip_get_num_files.c b/ext/zip/lib/zip_get_num_files.c new file mode 100644 index 0000000000..0cc81edb06 --- /dev/null +++ b/ext/zip/lib/zip_get_num_files.c @@ -0,0 +1,50 @@ +/* + $NiH: zip_get_num_files.c,v 1.2 2003/12/27 22:53:15 wiz Exp $ + + zip_get_num_files.c -- get number of files in archive + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_get_num_files(struct zip *za) +{ + if (za == NULL) + return -1; + + return za->nentry; +} diff --git a/ext/zip/lib/zip_memdup.c b/ext/zip/lib/zip_memdup.c new file mode 100644 index 0000000000..c4ecf4a69f --- /dev/null +++ b/ext/zip/lib/zip_memdup.c @@ -0,0 +1,58 @@ +/* + $NiH: zip_memdup.c,v 1.2 2006/04/24 10:34:39 dillo Exp $ + + zip_memdup.c -- internal zip function, "strdup" with len + Copyright (C) 1999, 2003, 2004, 2005, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include + +#include "zip.h" +#include "zipint.h" + + + +void * +_zip_memdup(const void *mem, size_t len, struct zip_error *error) +{ + void *ret; + + ret = malloc(len); + if (!ret) { + _zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + memcpy(ret, mem, len); + + return ret; +} diff --git a/ext/zip/lib/zip_name_locate.c b/ext/zip/lib/zip_name_locate.c new file mode 100644 index 0000000000..a2fce2d034 --- /dev/null +++ b/ext/zip/lib/zip_name_locate.c @@ -0,0 +1,91 @@ +/* + $NiH: zip_name_locate.c,v 1.19 2005/06/09 19:57:10 dillo Exp $ + + zip_name_locate.c -- get index by name + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +int +zip_name_locate(struct zip *za, const char *fname, int flags) +{ + return _zip_name_locate(za, fname, flags, &za->error); +} + + + +int +_zip_name_locate(struct zip *za, const char *fname, int flags, + struct zip_error *error) +{ + int (*cmp)(const char *, const char *); + const char *fn, *p; + int i, n; + + if (fname == NULL) { + _zip_error_set(error, ZIP_ER_INVAL, 0); + return -1; + } + + cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp; + + n = (flags & ZIP_FL_UNCHANGED) ? za->cdir->nentry : za->nentry; + for (i=0; icdir->entry[i].filename; + else + fn = _zip_get_name(za, i, flags, error); + + /* newly added (partially filled) entry */ + if (fn == NULL) + continue; + + if (flags & ZIP_FL_NODIR) { + p = strrchr(fn, '/'); + if (p) + fn = p+1; + } + + if (cmp(fname, fn) == 0) + return i; + } + + _zip_error_set(error, ZIP_ER_NOENT, 0); + return -1; +} diff --git a/ext/zip/lib/zip_new.c b/ext/zip/lib/zip_new.c new file mode 100644 index 0000000000..660183a15c --- /dev/null +++ b/ext/zip/lib/zip_new.c @@ -0,0 +1,71 @@ +/* + $NiH: zip_new.c,v 1.12 2006/04/23 00:40:47 wiz Exp $ + + zip_new.c -- create and init struct zip + Copyright (C) 1999, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include "zip.h" +#include "zipint.h" + + + +/* _zip_new: + creates a new zipfile struct, and sets the contents to zero; returns + the new struct. */ + +struct zip * +_zip_new(struct zip_error *error) +{ + struct zip *za; + + za = (struct zip *)malloc(sizeof(struct zip)); + if (!za) { + _zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; + } + + za->zn = NULL; + za->zp = NULL; + _zip_error_init(&za->error); + za->cdir = NULL; + za->ch_comment = NULL; + za->ch_comment_len = -1; + za->nentry = za->nentry_alloc = 0; + za->entry = NULL; + za->nfile = za->nfile_alloc = 0; + za->file = NULL; + + return za; +} diff --git a/ext/zip/lib/zip_open.c b/ext/zip/lib/zip_open.c new file mode 100644 index 0000000000..65ba8a5b9f --- /dev/null +++ b/ext/zip/lib/zip_open.c @@ -0,0 +1,464 @@ +/* + $NiH: zip_open.c,v 1.38 2006/05/04 00:01:26 dillo Exp $ + + zip_open.c -- open zip archive + Copyright (C) 1999-2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include +#include +#ifndef _MSC_VER +#include +#endif +#include +#include "zip.h" +#include "zipint.h" + +static void set_error(int *, struct zip_error *, int); +static int _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *); +static int _zip_headercomp(struct zip_dirent *, int, + struct zip_dirent *, int); +static unsigned char *_zip_memmem(const unsigned char *, int, + const unsigned char *, int); +static struct zip_cdir *_zip_readcdir(FILE *, unsigned char *, unsigned char *, + int, int, struct zip_error *); + + + +struct zip * +zip_open(const char *fn, int flags, int *zep) +{ + FILE *fp; + unsigned char *buf, *match; + int a, i, buflen, best; + struct zip *za; + struct zip_cdir *cdir, *cdirnew; + long len; + struct stat st; + struct zip_error error, err2; + + if (fn == NULL) { + set_error(zep, NULL, ZIP_ER_INVAL); + return NULL; + } + + if (stat(fn, &st) != 0) { + if (flags & ZIP_CREATE) { + if ((za=_zip_new(&error)) == NULL) { + set_error(zep, &error, 0); + return NULL; + } + + za->zn = strdup(fn); + if (!za->zn) { + _zip_free(za); + set_error(zep, NULL, ZIP_ER_MEMORY); + return NULL; + } + return za; + } + else { + set_error(zep, NULL, ZIP_ER_OPEN); + return NULL; + } + } + else if ((flags & ZIP_EXCL)) { + set_error(zep, NULL, ZIP_ER_EXISTS); + return NULL; + } + /* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL, + just like open() */ + + if ((fp=fopen(fn, "rb")) == NULL) { + set_error(zep, NULL, ZIP_ER_OPEN); + return NULL; + } + + clearerr(fp); + fseek(fp, 0, SEEK_END); + len = ftell(fp); + i = fseek(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END); + if (i == -1 && errno != EFBIG) { + /* seek before start of file on my machine */ + set_error(zep, NULL, ZIP_ER_SEEK); + fclose(fp); + return NULL; + } + + /* 64k is too much for stack */ + if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) { + set_error(zep, NULL, ZIP_ER_MEMORY); + fclose(fp); + return NULL; + } + + clearerr(fp); + buflen = fread(buf, 1, CDBUFSIZE, fp); + + if (ferror(fp)) { + set_error(zep, NULL, ZIP_ER_READ); + free(buf); + fclose(fp); + return NULL; + } + + best = -2; + cdir = NULL; + match = buf; + while ((match=_zip_memmem(match, buflen-(match-buf)-18, + (const unsigned char *)EOCD_MAGIC, 4))!=NULL) { + /* found match -- check, if good */ + /* to avoid finding the same match all over again */ + match++; + if ((cdirnew=_zip_readcdir(fp, buf, match-1, buflen, flags, + &err2)) == NULL) { + if (best == -2) { + set_error(zep, &err2, 0); + best = -1; + } + continue; + } + + if (cdir) { + if (best <= 0) + best = _zip_checkcons(fp, cdir, &err2); + a = _zip_checkcons(fp, cdirnew, &err2); + if (best < a) { + _zip_cdir_free(cdir); + cdir = cdirnew; + best = a; + } + else + _zip_cdir_free(cdirnew); + } + else { + cdir = cdirnew; + if (flags & ZIP_CHECKCONS) + best = _zip_checkcons(fp, cdir, &err2); + else + best = 0; + } + cdirnew = NULL; + } + + free(buf); + + if (best < 0) { + /* no consistent eocd found */ + if (best == -2) { + /* no eocd found at all */ + set_error(zep, NULL, ZIP_ER_NOZIP); + } + _zip_cdir_free(cdir); + fclose(fp); + return NULL; + } + + if ((za=_zip_new(&error)) == NULL) { + set_error(zep, &error, 0); + _zip_cdir_free(cdir); + fclose(fp); + return NULL; + } + + za->zp = fp; + za->cdir = cdir; + + if ((za->zn=strdup(fn)) == NULL) { + set_error(zep, NULL, ZIP_ER_MEMORY); + _zip_free(za); + return NULL; + } + + if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry)) + * cdir->nentry)) == NULL) { + set_error(zep, NULL, ZIP_ER_MEMORY); + _zip_free(za); + return NULL; + } + for (i=0; inentry; i++) + _zip_entry_new(za); + + return za; +} + + + +static void +set_error(int *zep, struct zip_error *err, int ze) +{ + int se; + + if (err) { + _zip_error_get(err, &ze, &se); + if (zip_error_get_sys_type(ze) == ZIP_ET_SYS) + errno = se; + } + + if (zep) + *zep = ze; +} + + + +/* _zip_readcdir: + tries to find a valid end-of-central-directory at the beginning of + buf, and then the corresponding central directory entries. + Returns a struct zip_cdir which contains the central directory + entries, or NULL if unsuccessful. */ + +static struct zip_cdir * +_zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen, + int flags, struct zip_error *error) +{ + struct zip_cdir *cd; + unsigned char *cdp, **bufp; + int i, comlen, nentry; + + comlen = buf + buflen - eocd - EOCDLEN; + if (comlen < 0) { + /* not enough bytes left for comment */ + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return NULL; + } + + /* check for end-of-central-dir magic */ + if (memcmp(eocd, EOCD_MAGIC, 4) != 0) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return NULL; + } + + if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) { + _zip_error_set(error, ZIP_ER_MULTIDISK, 0); + return NULL; + } + + cdp = eocd + 8; + /* number of cdir-entries on this disk */ + i = _zip_read2(&cdp); + /* number of cdir-entries */ + nentry = _zip_read2(&cdp); + + if ((cd=_zip_cdir_new(nentry, error)) == NULL) + return NULL; + + cd->size = _zip_read4(&cdp); + cd->offset = _zip_read4(&cdp); + cd->comment = NULL; + cd->comment_len = _zip_read2(&cdp); + + if ((comlen < cd->comment_len) || (cd->nentry != i)) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + free(cd); + return NULL; + } + if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) { + _zip_error_set(error, ZIP_ER_INCONS, 0); + free(cd); + return NULL; + } + + if (cd->comment_len) + if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN, + cd->comment_len, error)) + == NULL) { + free(cd); + return NULL; + } + + cdp = eocd; + if (cd->size < (unsigned int)(eocd-buf)) { + /* if buffer already read in, use it */ + cdp = eocd - cd->size; + bufp = &cdp; + } + else { + /* go to start of cdir and read it entry by entry */ + bufp = NULL; + clearerr(fp); + fseek(fp, -(cd->size+cd->comment_len+EOCDLEN), SEEK_END); + if (ferror(fp) || ((unsigned int)ftell(fp) != cd->offset)) { + /* seek error or offset of cdir wrong */ + if (ferror(fp)) + _zip_error_set(error, ZIP_ER_SEEK, errno); + else + _zip_error_set(error, ZIP_ER_NOZIP, 0); + free(cd); + return NULL; + } + } + + for (i=0; inentry; i++) { + if ((_zip_dirent_read(cd->entry+i, fp, bufp, eocd-cdp, 0, + error)) < 0) { + cd->nentry = i; + _zip_cdir_free(cd); + return NULL; + } + } + + return cd; +} + + + +/* _zip_checkcons: + Checks the consistency of the central directory by comparing central + directory entries with local headers and checking for plausible + file and header offsets. Returns -1 if not plausible, else the + difference between the lowest and the highest fileposition reached */ + +static int +_zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error) +{ + int i; + unsigned int min, max, j; + struct zip_dirent temp; + + if (cd->nentry) { + max = cd->entry[0].offset; + min = cd->entry[0].offset; + } + else + min = max = 0; + + for (i=0; inentry; i++) { + if (cd->entry[i].offset < min) + min = cd->entry[i].offset; + if (min > cd->offset) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return -1; + } + + j = cd->entry[i].offset + cd->entry[i].comp_size + + cd->entry[i].filename_len + LENTRYSIZE; + if (j > max) + max = j; + if (max > cd->offset) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + return -1; + } + + if (fseek(fp, cd->entry[i].offset, SEEK_SET) != 0) { + _zip_error_set(error, ZIP_ER_SEEK, 0); + return -1; + } + + if (_zip_dirent_read(&temp, fp, NULL, 0, 1, error) == -1) + return -1; + + if (_zip_headercomp(cd->entry+i, 0, &temp, 1) != 0) { + _zip_error_set(error, ZIP_ER_NOZIP, 0); + _zip_dirent_finalize(&temp); + return -1; + } + _zip_dirent_finalize(&temp); + } + + return max - min; +} + + + +/* _zip_headercomp: + compares two headers h1 and h2; if they are local headers, set + local1p or local2p respectively to 1, else 0. Return 0 if they + are identical, -1 if not. */ + +static int +_zip_headercomp(struct zip_dirent *h1, int local1p, struct zip_dirent *h2, + int local2p) +{ + if ((h1->version_needed != h2->version_needed) +#if 0 + /* some zip-files have different values in local + and global headers for the bitflags */ + || (h1->bitflags != h2->bitflags) +#endif + || (h1->comp_method != h2->comp_method) + || (h1->last_mod != h2->last_mod) + || (h1->crc != h2->crc) + || (h1->comp_size != h2->comp_size) + || (h1->uncomp_size != h2->uncomp_size) + || (h1->filename_len != h2->filename_len) + || !h1->filename || !h2->filename + || strcmp(h1->filename, h2->filename)) + return -1; + + if ((local1p == local2p) + && ((h1->extrafield_len != h2->extrafield_len) + || (h1->extrafield_len && h2->extrafield + && memcmp(h1->extrafield, h2->extrafield, + h1->extrafield_len)))) + return -1; + + /* if either is local, nothing more to check */ + if (local1p || local2p) + return 0; + + if ((h1->version_madeby != h2->version_madeby) + || (h1->disk_number != h2->disk_number) + || (h1->int_attrib != h2->int_attrib) + || (h1->ext_attrib != h2->ext_attrib) + || (h1->offset != h2->offset) + || (h1->comment_len != h2->comment_len) + || (h1->comment_len && h2->comment + && memcmp(h1->comment, h2->comment, h1->comment_len))) + return -1; + + return 0; +} + + + +static unsigned char * +_zip_memmem(const unsigned char *big, int biglen, const unsigned char *little, + int littlelen) +{ + const unsigned char *p; + + if ((biglen < littlelen) || (littlelen == 0)) + return NULL; + p = big-1; + while ((p=(const unsigned char *) + memchr(p+1, little[0], (size_t)(big-(p+1)+biglen-littlelen+1))) + != NULL) { + if (memcmp(p+1, little+1, littlelen-1)==0) + return (unsigned char *)p; + } + + return NULL; +} diff --git a/ext/zip/lib/zip_rename.c b/ext/zip/lib/zip_rename.c new file mode 100644 index 0000000000..f35291893f --- /dev/null +++ b/ext/zip/lib/zip_rename.c @@ -0,0 +1,52 @@ +/* + $NiH: zip_rename.c,v 1.15 2004/11/30 22:19:38 wiz Exp $ + + zip_rename.c -- rename file in zip archive + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_rename(struct zip *za, int idx, const char *name) +{ + if (idx >= za->nentry || idx < 0) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + return _zip_set_name(za, idx, name); +} diff --git a/ext/zip/lib/zip_replace.c b/ext/zip/lib/zip_replace.c new file mode 100644 index 0000000000..011bbc2af0 --- /dev/null +++ b/ext/zip/lib/zip_replace.c @@ -0,0 +1,79 @@ +/* + $NiH: zip_replace.c,v 1.20 2006/04/09 14:52:02 wiz Exp $ + + zip_replace.c -- replace file via callback function + Copyright (C) 1999, 2003, 2004, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_replace(struct zip *za, int idx, struct zip_source *source) +{ + if (idx < 0 || idx >= za->nentry || source == NULL) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if (_zip_replace(za, idx, NULL, source) == -1) + return -1; + + return 0; +} + + + + +int +_zip_replace(struct zip *za, int idx, const char *name, + struct zip_source *source) +{ + if (idx == -1) { + if (_zip_entry_new(za) == NULL) + return -1; + idx = za->nentry - 1; + } + + _zip_unchange_data(za->entry+idx); + + if (name && _zip_set_name(za, idx, name) != 0) + return -1; + za->entry[idx].state = ((za->cdir == NULL || idx >= za->cdir->nentry) + ? ZIP_ST_ADDED : ZIP_ST_REPLACED); + za->entry[idx].source = source; + + return idx; +} diff --git a/ext/zip/lib/zip_set_archive_comment.c b/ext/zip/lib/zip_set_archive_comment.c new file mode 100644 index 0000000000..51a8416da4 --- /dev/null +++ b/ext/zip/lib/zip_set_archive_comment.c @@ -0,0 +1,68 @@ +/* + $NiH: zip_set_archive_comment.c,v 1.3 2006/04/24 10:34:39 dillo Exp $ + + zip_set_archive_comment.c -- set archive comment + Copyright (C) 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +int +zip_set_archive_comment(struct zip *za, const char *comment, int len) +{ + char *tmpcom; + + if (len < 0 || len > MAXCOMLEN + || (len > 0 && comment == NULL)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if (len > 0) { + if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL) + return -1; + } + else + tmpcom = NULL; + + free(za->ch_comment); + za->ch_comment = tmpcom; + za->ch_comment_len = len; + + return 0; +} diff --git a/ext/zip/lib/zip_set_file_comment.c b/ext/zip/lib/zip_set_file_comment.c new file mode 100644 index 0000000000..cbf71db5c2 --- /dev/null +++ b/ext/zip/lib/zip_set_file_comment.c @@ -0,0 +1,69 @@ +/* + $NiH: zip_set_file_comment.c,v 1.4 2006/04/24 10:34:39 dillo Exp $ + + zip_set_file_comment.c -- set comment for file in archive + Copyright (C) 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +int +zip_set_file_comment(struct zip *za, int idx, const char *comment, int len) +{ + char *tmpcom; + + if (idx < 0 || idx >= za->nentry + || len < 0 || len > MAXCOMLEN + || (len > 0 && comment == NULL)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if (len > 0) { + if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL) + return -1; + } + else + tmpcom = NULL; + + free(za->entry[idx].ch_comment); + za->entry[idx].ch_comment = tmpcom; + za->entry[idx].ch_comment_len = len; + + return 0; +} diff --git a/ext/zip/lib/zip_set_name.c b/ext/zip/lib/zip_set_name.c new file mode 100644 index 0000000000..46dca56466 --- /dev/null +++ b/ext/zip/lib/zip_set_name.c @@ -0,0 +1,77 @@ +/* + $NiH: zip_set_name.c,v 1.16 2004/11/30 23:02:47 wiz Exp $ + + zip_set_name.c -- rename helper function + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include "zip.h" +#include "zipint.h" + + + +int +_zip_set_name(struct zip *za, int idx, const char *name) +{ + char *s; + int i; + + if (idx < 0 || idx >= za->nentry || name == NULL) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if ((i=_zip_name_locate(za, name, 0, NULL)) != -1 && i != idx) { + _zip_error_set(&za->error, ZIP_ER_EXISTS, 0); + return -1; + } + + /* no effective name change */ + if (i == idx) + return 0; + + if ((s=strdup(name)) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return -1; + } + + if (za->entry[idx].state == ZIP_ST_UNCHANGED) + za->entry[idx].state = ZIP_ST_RENAMED; + + free(za->entry[idx].ch_filename); + za->entry[idx].ch_filename = s; + + return 0; +} diff --git a/ext/zip/lib/zip_source_buffer.c b/ext/zip/lib/zip_source_buffer.c new file mode 100644 index 0000000000..ada9ae85fb --- /dev/null +++ b/ext/zip/lib/zip_source_buffer.c @@ -0,0 +1,162 @@ +/* + $NiH: zip_source_buffer.c,v 1.8 2006/04/23 14:50:49 wiz Exp $ + + zip_source_buffer.c -- create zip data source from buffer + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include + +#include "zip.h" +#include "zipint.h" + +struct read_data { + const char *buf, *data, *end; + time_t mtime; + int freep; +}; + +static ssize_t read_data(void *state, void *data, size_t len, + enum zip_source_cmd cmd); + + + +struct zip_source * +zip_source_buffer(struct zip *za, const void *data, off_t len, int freep) +{ + struct read_data *f; + struct zip_source *zs; + + if (za == NULL) + return NULL; + + if (len < 0 || (data == NULL && len > 0)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + + f->data = (const char *)data; + f->end = ((const char *)data)+len; + f->freep = freep; + f->mtime = time(NULL); + + if ((zs=zip_source_function(za, read_data, f)) == NULL) { + free(f); + return NULL; + } + + return zs; +} + +static ssize_t +read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd) +{ + struct read_data *z; + char *buf; + size_t n; + + z = (struct read_data *)state; + buf = (char *)data; + + switch (cmd) { + case ZIP_SOURCE_OPEN: + z->buf = z->data; + return 0; + + case ZIP_SOURCE_READ: + n = z->end - z->buf; + if (n > len) + n = len; + if (n < 0) + n = 0; + + if (n) { + memcpy(buf, z->buf, n); + z->buf += n; + } + + return n; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_STAT: + { + struct zip_stat *st; + + if (len < sizeof(*st)) + return -1; + + st = (struct zip_stat *)data; + + st->mtime = z->mtime; + st->crc = 0; + st->size = z->end - z->data; + st->comp_size = -1; + st->comp_method = ZIP_CM_STORE; + + return sizeof(*st); + } + + case ZIP_SOURCE_ERROR: + { + int *e; + + if (len < sizeof(int)*2) + return -1; + + e = (int *)data; + e[0] = e[1] = 0; + } + return sizeof(int)*2; + + case ZIP_SOURCE_FREE: + if (z->freep) { + free((void *)z->data); + z->data = NULL; + } + free(z); + return 0; + + default: + ; + } + + return -1; +} diff --git a/ext/zip/lib/zip_source_file.c b/ext/zip/lib/zip_source_file.c new file mode 100644 index 0000000000..f06b32fec6 --- /dev/null +++ b/ext/zip/lib/zip_source_file.c @@ -0,0 +1,71 @@ +/* + $NiH: zip_source_file.c,v 1.2 2004/11/18 16:28:13 wiz Exp $ + + zip_source_file.c -- create data source from file + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include + +#include "zip.h" +#include "zipint.h" + + + +struct zip_source * +zip_source_file(struct zip *za, const char *fname, off_t start, off_t len) +{ + struct zip_source *zs; + FILE *fp; + + if (za == NULL) + return NULL; + + if (fname == NULL || start < 0 || len < -1) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((fp=fopen(fname, "rb")) == NULL) { + _zip_error_set(&za->error, ZIP_ER_OPEN, errno); + return NULL; + } + + if ((zs=zip_source_filep(za, fp, start, len)) == NULL) { + fclose(fp); + return NULL; + } + + return zs; +} diff --git a/ext/zip/lib/zip_source_filep.c b/ext/zip/lib/zip_source_filep.c new file mode 100644 index 0000000000..9c7383cf29 --- /dev/null +++ b/ext/zip/lib/zip_source_filep.c @@ -0,0 +1,180 @@ +/* + $NiH: zip_source_filep.c,v 1.6 2005/06/09 19:57:10 dillo Exp $ + + zip_source_filep.c -- create data source from FILE * + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include +#include +#include +#include + +#include "zip.h" +#include "zipint.h" + +struct read_file { + FILE *f; /* file to copy from */ + off_t off; /* start offset of */ + off_t len; /* lengt of data to copy */ + off_t remain; /* bytes remaining to be copied */ + int e[2]; /* error codes */ +}; + +static ssize_t read_file(void *state, void *data, size_t len, + enum zip_source_cmd cmd); + + + +struct zip_source * +zip_source_filep(struct zip *za, FILE *file, off_t start, off_t len) +{ + struct read_file *f; + struct zip_source *zs; + + if (za == NULL) + return NULL; + + if (file == NULL || start < 0 || len < -1) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + + f->f = file; + f->off = start; + f->len = (len ? len : -1); + + if ((zs=zip_source_function(za, read_file, f)) == NULL) { + free(f); + return NULL; + } + + return zs; +} + + + +static ssize_t +read_file(void *state, void *data, size_t len, enum zip_source_cmd cmd) +{ + struct read_file *z; + char *buf; + int i, n; + + z = (struct read_file *)state; + buf = (char *)data; + + switch (cmd) { + case ZIP_SOURCE_OPEN: + if (fseeko(z->f, z->off, SEEK_SET) < 0) { + z->e[0] = ZIP_ER_SEEK; + z->e[1] = errno; + return -1; + } + z->remain = z->len; + return 0; + + case ZIP_SOURCE_READ: + if (z->remain != -1) + n = len > z->remain ? z->remain : len; + else + n = len; + + if ((i=fread(buf, 1, n, z->f)) < 0) { + z->e[0] = ZIP_ER_READ; + z->e[1] = errno; + return -1; + } + + if (z->remain != -1) + z->remain -= i; + + return i; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_STAT: + { + struct zip_stat *st; + struct stat fst; + + if (len < sizeof(*st)) + return -1; + + st = (struct zip_stat *)data; + + if (fstat(fileno(z->f), &fst) != 0) { + z->e[0] = ZIP_ER_READ; /* best match */ + z->e[1] = errno; + return -1; + } + + st->mtime = fst.st_mtime; + st->crc = 0; + if (z->len != -1) + st->size = z->len; + else if ((fst.st_mode&S_IFMT) == S_IFREG) + st->size = fst.st_size; + else + st->size = -1; + st->comp_size = -1; + st->comp_method = ZIP_CM_STORE; + + return sizeof(*st); + } + + case ZIP_SOURCE_ERROR: + if (len < sizeof(int)*2) + return -1; + + memcpy(data, z->e, sizeof(int)*2); + return sizeof(int)*2; + + case ZIP_SOURCE_FREE: + fclose(z->f); + free(z); + return 0; + + default: + ; + } + + return -1; +} diff --git a/ext/zip/lib/zip_source_free.c b/ext/zip/lib/zip_source_free.c new file mode 100644 index 0000000000..33e36eaf43 --- /dev/null +++ b/ext/zip/lib/zip_source_free.c @@ -0,0 +1,54 @@ +/* + $NiH: zip_source_free.c,v 1.2 2004/12/22 16:32:00 dillo Exp $ + + zip_source_free.c -- free zip data source + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +void +zip_source_free(struct zip_source *source) +{ + if (source == NULL) + return; + + (void)source->f(source->ud, NULL, 0, ZIP_SOURCE_FREE); + + free(source); +} diff --git a/ext/zip/lib/zip_source_function.c b/ext/zip/lib/zip_source_function.c new file mode 100644 index 0000000000..4f01a43c51 --- /dev/null +++ b/ext/zip/lib/zip_source_function.c @@ -0,0 +1,62 @@ +/* + $NiH: zip_source_function.c,v 1.4 2006/02/21 09:41:00 dillo Exp $ + + zip_source_function.c -- create zip data source from callback function + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zip.h" +#include "zipint.h" + + + +struct zip_source * +zip_source_function(struct zip *za, zip_source_callback zcb, void *ud) +{ + struct zip_source *zs; + + if (za == NULL) + return NULL; + + if ((zs=(struct zip_source *)malloc(sizeof(*zs))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + + zs->f = zcb; + zs->ud = ud; + + return zs; +} diff --git a/ext/zip/lib/zip_source_zip.c b/ext/zip/lib/zip_source_zip.c new file mode 100644 index 0000000000..a31fd2787e --- /dev/null +++ b/ext/zip/lib/zip_source_zip.c @@ -0,0 +1,189 @@ +/* + $NiH: zip_source_zip.c,v 1.7 2006/02/21 09:41:00 dillo Exp $ + + zip_source_zip.c -- create data source from zip file + Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include + +#include "zip.h" +#include "zipint.h" + +struct read_zip { + struct zip_file *zf; + struct zip_stat st; + off_t off, len; +}; + +static ssize_t read_zip(void *st, void *data, size_t len, + enum zip_source_cmd cmd); + + + +struct zip_source * +zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags, + off_t start, off_t len) +{ + struct zip_error error; + struct zip_source *zs; + struct read_zip *p; + + if (za == NULL) + return NULL; + + if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return NULL; + } + + if ((flags & ZIP_FL_UNCHANGED) == 0 + && ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) { + _zip_error_set(&za->error, ZIP_ER_CHANGED, 0); + return NULL; + } + + if (len == 0) + len = -1; + + if (start == 0 && len == -1) + flags |= ZIP_FL_COMPRESSED; + else + flags &= ~ZIP_FL_COMPRESSED; + + if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) { + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); + return NULL; + } + + _zip_error_copy(&error, &srcza->error); + + if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0 + || (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) { + free(p); + _zip_error_copy(&za->error, &srcza->error); + _zip_error_copy(&srcza->error, &error); + + return NULL; + } + p->off = start; + p->len = len; + + if ((flags & ZIP_FL_COMPRESSED) == 0) { + p->st.size = p->st.comp_size = len; + p->st.comp_method = ZIP_CM_STORE; + p->st.crc = 0; + } + + if ((zs=zip_source_function(za, read_zip, p)) == NULL) { + free(p); + return NULL; + } + + return zs; +} + + + +static ssize_t +read_zip(void *state, void *data, size_t len, enum zip_source_cmd cmd) +{ + struct read_zip *z; + char b[8192], *buf; + int i, n; + + z = (struct read_zip *)state; + buf = (char *)data; + + switch (cmd) { + case ZIP_SOURCE_OPEN: + for (n=0; noff; n+= i) { + i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n); + if ((i=zip_fread(z->zf, b, i)) < 0) { + zip_fclose(z->zf); + z->zf = NULL; + return -1; + } + } + return 0; + + case ZIP_SOURCE_READ: + if (z->len != -1) + n = len > z->len ? z->len : len; + else + n = len; + + + if ((i=zip_fread(z->zf, buf, n)) < 0) + return -1; + + if (z->len != -1) + z->len -= i; + + return i; + + case ZIP_SOURCE_CLOSE: + return 0; + + case ZIP_SOURCE_STAT: + if (len < sizeof(z->st)) + return -1; + len = sizeof(z->st); + + memcpy(data, &z->st, len); + return len; + + case ZIP_SOURCE_ERROR: + { + int *e; + + if (len < sizeof(int)*2) + return -1; + + e = (int *)data; + zip_file_error_get(z->zf, e, e+1); + } + return sizeof(int)*2; + + case ZIP_SOURCE_FREE: + zip_fclose(z->zf); + free(z); + return 0; + + default: + ; + } + + return -1; +} diff --git a/ext/zip/lib/zip_stat.c b/ext/zip/lib/zip_stat.c new file mode 100644 index 0000000000..bea153d157 --- /dev/null +++ b/ext/zip/lib/zip_stat.c @@ -0,0 +1,52 @@ +/* + $NiH: zip_stat.c,v 1.3 2004/04/16 09:40:30 dillo Exp $ + + zip_stat.c -- get information about file by name + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st) +{ + int idx; + + if ((idx=zip_name_locate(za, fname, flags)) < 0) + return -1; + + return zip_stat_index(za, idx, flags, st); +} diff --git a/ext/zip/lib/zip_stat_index.c b/ext/zip/lib/zip_stat_index.c new file mode 100644 index 0000000000..837d639077 --- /dev/null +++ b/ext/zip/lib/zip_stat_index.c @@ -0,0 +1,93 @@ +/* + $NiH: zip_stat_index.c,v 1.10 2006/04/24 14:04:19 dillo Exp $ + + zip_stat_index.c -- get information about file by index + Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +int +zip_stat_index(struct zip *za, int index, int flags, struct zip_stat *st) +{ + const char *name; + + if (index < 0 || index >= za->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if ((name=zip_get_name(za, index, flags)) == NULL) + return -1; + + + if ((flags & ZIP_FL_UNCHANGED) == 0 + && ZIP_ENTRY_DATA_CHANGED(za->entry+index)) { + if (za->entry[index].source->f(za->entry[index].source->ud, + st, sizeof(*st), ZIP_SOURCE_STAT) < 0) { + _zip_error_set(&za->error, ZIP_ER_CHANGED, 0); + return -1; + } + } + else { + if (za->cdir == NULL || index >= za->cdir->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + st->index = index; + st->crc = za->cdir->entry[index].crc; + st->size = za->cdir->entry[index].uncomp_size; + st->mtime = za->cdir->entry[index].last_mod; + st->comp_size = za->cdir->entry[index].comp_size; + st->comp_method = za->cdir->entry[index].comp_method; + if (za->cdir->entry[index].bitflags & ZIP_GPBF_ENCRYPTED) { + if (za->cdir->entry[index].bitflags & ZIP_GPBF_STRONG_ENCRYPTION) { + /* XXX */ + st->encryption_method = ZIP_EM_UNKNOWN; + } + else + st->encryption_method = ZIP_EM_TRAD_PKWARE; + } + else + st->encryption_method = ZIP_EM_NONE; + /* st->bitflags = za->cdir->entry[index].bitflags; */ + } + + st->name = name; + + return 0; +} diff --git a/ext/zip/lib/zip_strerror.c b/ext/zip/lib/zip_strerror.c new file mode 100644 index 0000000000..83e2985068 --- /dev/null +++ b/ext/zip/lib/zip_strerror.c @@ -0,0 +1,47 @@ +/* + $NiH: zip_strerror.c,v 1.1 2003/10/05 16:05:22 dillo Exp $ + + zip_sterror.c -- get string representation of zip error + Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include "zip.h" +#include "zipint.h" + + + +const char * +zip_strerror(struct zip *za) +{ + return _zip_error_strerror(&za->error); +} diff --git a/ext/zip/lib/zip_unchange.c b/ext/zip/lib/zip_unchange.c new file mode 100644 index 0000000000..34e7f2d367 --- /dev/null +++ b/ext/zip/lib/zip_unchange.c @@ -0,0 +1,84 @@ +/* + $NiH: zip_unchange.c,v 1.19 2006/04/23 13:21:18 wiz Exp $ + + zip_unchange.c -- undo changes to file in zip archive + Copyright (C) 1999, 2004, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include "zip.h" +#include "zipint.h" + + + +int +zip_unchange(struct zip *za, int idx) +{ + return _zip_unchange(za, idx, 0); +} + + + +int +_zip_unchange(struct zip *za, int idx, int allow_duplicates) +{ + int i; + + if (idx < 0 || idx >= za->nentry) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } + + if (za->entry[idx].ch_filename) { + if (!allow_duplicates) { + i = _zip_name_locate(za, + _zip_get_name(za, idx, ZIP_FL_UNCHANGED, NULL), + 0, NULL); + if (i != -1 && i != idx) { + _zip_error_set(&za->error, ZIP_ER_EXISTS, 0); + return -1; + } + } + + free(za->entry[idx].ch_filename); + za->entry[idx].ch_filename = NULL; + } + + free(za->entry[idx].ch_comment); + za->entry[idx].ch_comment = NULL; + za->entry[idx].ch_comment_len = -1; + + _zip_unchange_data(za->entry+idx); + + return 0; +} diff --git a/ext/zip/lib/zip_unchange_all.c b/ext/zip/lib/zip_unchange_all.c new file mode 100644 index 0000000000..f1e27da045 --- /dev/null +++ b/ext/zip/lib/zip_unchange_all.c @@ -0,0 +1,56 @@ +/* + $NiH: zip_unchange_all.c,v 1.10 2006/04/23 13:14:46 wiz Exp $ + + zip_unchange.c -- undo changes to all files in zip archive + Copyright (C) 1999, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include "zip.h" +#include "zipint.h" + + + +int +zip_unchange_all(struct zip *za) +{ + int ret, i; + + ret = 0; + for (i=0; inentry; i++) + ret |= _zip_unchange(za, i, 1); + + ret |= zip_unchange_archive(za); + + return ret; +} diff --git a/ext/zip/lib/zip_unchange_archive.c b/ext/zip/lib/zip_unchange_archive.c new file mode 100644 index 0000000000..7418aaf6ac --- /dev/null +++ b/ext/zip/lib/zip_unchange_archive.c @@ -0,0 +1,52 @@ +/* + $NiH: zip_unchange_archive.c,v 1.1 2006/04/23 13:14:46 wiz Exp $ + + zip_unchange_archive.c -- undo global changes to ZIP archive + Copyright (C) 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include +#include "zip.h" +#include "zipint.h" + + + +int +zip_unchange_archive(struct zip *za) +{ + free(za->ch_comment); + za->ch_comment = NULL; + za->ch_comment_len = -1; + + return 0; +} diff --git a/ext/zip/lib/zip_unchange_data.c b/ext/zip/lib/zip_unchange_data.c new file mode 100644 index 0000000000..2526769261 --- /dev/null +++ b/ext/zip/lib/zip_unchange_data.c @@ -0,0 +1,53 @@ +/* + $NiH: zip_unchange_data.c,v 1.15 2004/12/22 16:32:00 dillo Exp $ + + zip_unchange_data.c -- undo helper function + Copyright (C) 1999, 2004 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + + +#include + +#include "zipint.h" + +void +_zip_unchange_data(struct zip_entry *ze) +{ + if (ze->source) { + (void)ze->source->f(ze->source->ud, NULL, 0, ZIP_SOURCE_FREE); + free(ze->source); + ze->source = NULL; + } + + ze->state = ze->ch_filename ? ZIP_ST_RENAMED : ZIP_ST_UNCHANGED; +} + diff --git a/ext/zip/lib/zip_win32.h b/ext/zip/lib/zip_win32.h new file mode 100644 index 0000000000..a33347ba0c --- /dev/null +++ b/ext/zip/lib/zip_win32.h @@ -0,0 +1,29 @@ + +#ifdef _MSC_VER + +#define _POSIX_ +#include +#include +#include + +#ifndef ssize_t +# define ssize_t SSIZE_T +#endif +#ifndef mode_t +# define mode_t int +#endif +#ifndef strcasecmp +# define strcasecmp stricmp +#endif +#ifndef snprintf +# define snprintf _snprintf +#endif +#ifndef mkstemp +# define mkstemp(t) _creat(_mktemp(t), _S_IREAD|_S_IWRITE) +#endif +/* +#ifndef fseeko +# define fseeko fseek +#endif +*/ +#endif diff --git a/ext/zip/lib/zipint.h b/ext/zip/lib/zipint.h new file mode 100644 index 0000000000..9efaf06a33 --- /dev/null +++ b/ext/zip/lib/zipint.h @@ -0,0 +1,225 @@ +#ifndef _HAD_ZIPINT_H +#define _HAD_ZIPINT_H + +/* + $NiH: zipint.h,v 1.48 2006/04/24 14:04:19 dillo Exp $ + + zipint.h -- internal declarations. + Copyright (C) 1999, 2003, 2004, 2005, 2006 Dieter Baron and Thomas Klausner + + This file is part of libzip, a library to manipulate ZIP archives. + The authors can be contacted at + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. The names of the authors may not be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include + +#include "zip.h" +#ifndef HAVE_FSEEKO +#define fseeko(s, o, w) (fseek((s), (long int)(o), (w))) +#endif + +#define CENTRAL_MAGIC "PK\1\2" +#define LOCAL_MAGIC "PK\3\4" +#define EOCD_MAGIC "PK\5\6" +#define DATADES_MAGIC "PK\7\8" +#define CDENTRYSIZE 46u +#define LENTRYSIZE 30 +#define MAXCOMLEN 65536 +#define EOCDLEN 22 +#define CDBUFSIZE (MAXCOMLEN+EOCDLEN) +#define BUFSIZE 8192 + + + +/* state of change of a file in zip archive */ + +enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED, + ZIP_ST_ADDED, ZIP_ST_RENAMED }; + +/* constants for struct zip_file's member flags */ + +#define ZIP_ZF_EOF 1 /* EOF reached */ +#define ZIP_ZF_DECOMP 2 /* decompress data */ +#define ZIP_ZF_CRC 4 /* compute and compare CRC */ + +/* directory entry: general purpose bit flags */ + +#define ZIP_GPBF_ENCRYPTED 0x0001 /* is encrypted */ +#define ZIP_GPBF_STRONG_ENCRYPTION 0x0040 /* uses strong encryption */ +#define ZIP_GPBF_USE_DATA_DESCRIPTOR 0x0008 /* uses crc and size from data header */ + +/* error information */ + +struct zip_error { + int zip_err; /* libzip error code (ZIP_ER_*) */ + int sys_err; /* copy of errno (E*) or zlib error code */ + char *str; /* string representation or NULL */ +}; + +/* zip archive, part of API */ + +struct zip { + char *zn; /* file name */ + FILE *zp; /* file */ + struct zip_error error; /* error information */ + + struct zip_cdir *cdir; /* central directory */ + char *ch_comment; /* changed archive comment */ + int ch_comment_len; /* length of changed zip archive + * comment, -1 if unchanged */ + int nentry; /* number of entries */ + int nentry_alloc; /* number of entries allocated */ + struct zip_entry *entry; /* entries */ + int nfile; /* number of opened files within archive */ + int nfile_alloc; /* number of files allocated */ + struct zip_file **file; /* opened files within archive */ +}; + +/* file in zip archive, part of API */ + +struct zip_file { + struct zip *za; /* zip archive containing this file */ + struct zip_error error; /* error information */ + int flags; /* -1: eof, >0: error */ + + int method; /* compression method */ + long fpos; /* position within zip file (fread/fwrite) */ + unsigned long bytes_left; /* number of bytes left to read */ + unsigned long cbytes_left; /* number of bytes of compressed data left */ + + unsigned long crc; /* CRC so far */ + unsigned long crc_orig; /* CRC recorded in archive */ + + char *buffer; + z_stream *zstr; +}; + +/* zip archive directory entry (central or local) */ + +struct zip_dirent { + unsigned short version_madeby; /* (c) version of creator */ + unsigned short version_needed; /* (cl) version needed to extract */ + unsigned short bitflags; /* (cl) general purpose bit flag */ + unsigned short comp_method; /* (cl) compression method used */ + time_t last_mod; /* (cl) time of last modification */ + unsigned int crc; /* (cl) CRC-32 of uncompressed data */ + unsigned int comp_size; /* (cl) size of commpressed data */ + unsigned int uncomp_size; /* (cl) size of uncommpressed data */ + char *filename; /* (cl) file name (NUL-terminated) */ + unsigned short filename_len; /* (cl) length of filename (w/o NUL) */ + char *extrafield; /* (cl) extra field */ + unsigned short extrafield_len; /* (cl) length of extra field */ + char *comment; /* (c) file comment */ + unsigned short comment_len; /* (c) length of file comment */ + unsigned short disk_number; /* (c) disk number start */ + unsigned short int_attrib; /* (c) internal file attributes */ + unsigned int ext_attrib; /* (c) external file attributes */ + unsigned int offset; /* (c) offset of local header */ +}; + +/* zip archive central directory */ + +struct zip_cdir { + struct zip_dirent *entry; /* directory entries */ + int nentry; /* number of entries */ + + unsigned int size; /* size of central direcotry */ + unsigned int offset; /* offset of central directory in file */ + char *comment; /* zip archive comment */ + unsigned short comment_len; /* length of zip archive comment */ +}; + + + +struct zip_source { + zip_source_callback f; + void *ud; +}; + +/* entry in zip archive directory */ + +struct zip_entry { + enum zip_state state; + struct zip_source *source; + char *ch_filename; + char *ch_comment; + int ch_comment_len; +}; + + + +extern const char * const _zip_err_str[]; +extern const int _zip_nerr_str; +extern const int _zip_err_type[]; + + + +#define ZIP_ENTRY_DATA_CHANGED(x) \ + ((x)->state == ZIP_ST_REPLACED \ + || (x)->state == ZIP_ST_ADDED) + + + +void _zip_cdir_free(struct zip_cdir *); +struct zip_cdir *_zip_cdir_new(int, struct zip_error *); +int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *); + +void _zip_dirent_finalize(struct zip_dirent *); +void _zip_dirent_init(struct zip_dirent *); +int _zip_dirent_read(struct zip_dirent *, FILE *, + unsigned char **, unsigned int, int, struct zip_error *); +int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *); + +void _zip_entry_free(struct zip_entry *); +void _zip_entry_init(struct zip *, int); +struct zip_entry *_zip_entry_new(struct zip *); + +void _zip_error_copy(struct zip_error *, struct zip_error *); +void _zip_error_fini(struct zip_error *); +void _zip_error_get(struct zip_error *, int *, int *); +void _zip_error_init(struct zip_error *); +void _zip_error_set(struct zip_error *, int, int); +const char *_zip_error_strerror(struct zip_error *); + +int _zip_file_fillbuf(void *, size_t, struct zip_file *); +unsigned int _zip_file_get_offset(struct zip *, int); + +void _zip_free(struct zip *); +const char *_zip_get_name(struct zip *, int, int, struct zip_error *); +int _zip_local_header_read(struct zip *, int); +void *_zip_memdup(const void *, size_t, struct zip_error *); +int _zip_name_locate(struct zip *, const char *, int, struct zip_error *); +struct zip *_zip_new(struct zip_error *); +unsigned short _zip_read2(unsigned char **); +unsigned int _zip_read4(unsigned char **); +int _zip_replace(struct zip *, int, const char *, struct zip_source *); +int _zip_set_name(struct zip *, int, const char *); +int _zip_unchange(struct zip *, int, int); +void _zip_unchange_data(struct zip_entry *); + +#endif /* zipint.h */ diff --git a/ext/zip/package.xml b/ext/zip/package.xml new file mode 100644 index 0000000000..c579af926f --- /dev/null +++ b/ext/zip/package.xml @@ -0,0 +1,362 @@ + + + zip + pecl.php.net + A zip management extension + Zip is an extension to create, modify and read zip files. + + Pierre-Alain Joye + pajoye + pajoye@php.net + yes + + 2006-07-14 + + 1.6.0 + 1.6.0 + + + beta + alpha + + PHP License + - Rename Class Zip to ZipArchive, required to be bundled +in php 5.2.0 (Zip being a ZipCode in US), making the role of +this class more obvious *Gah* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 4.3.0 + + + 1.4.0 + + + + zip + + + + 2006-07-14 + + 1.5.0 + 1.5.0 + + + alpha + alpha + + PHP License + - bring consistency to the method names: + - rename zip::delete() to deleteIndex() + - add zip::deleteName() + - rename zip::statPath to zip::statName() + - rename zip::rename to zip::renameIndex() + - add zip::renameName(); +- add zip::locateName(), returns the index of an entry, allows case insensitive + or directory free lookup (ZIP::FL_NOCASE, ZIP::FL_NODIR) +- add zip::unchangeName(), ::unchangeIndex(), unchangeAll() and unchangeArchive() + revert changes to an entry, to all entries or the archive +- add zip::getNameIndex(), get the name of the entry at the given position +- Bug #7658, Modify zip archives causes corruption if the data descriptor is used + (bit 3 of the general flags) +- fix a bug when in the delete and rename methods when the index is lower than 1 +- zip::addFile() must return true on success +- zip::open() returns now the error code on error and true on success + + + + 2006-06-28 + + 1.4.1 + 1.4.0 + + + alpha + alpha + + PHP License + - Add missing files in the package release + + + + 2006-06-28 + + 1.4.0 + 1.4.0 + + + alpha + alpha + + PHP License + - Add write mode to the archive comment (zip::comment property) +- Add zip::setCommentName and ::setCommentIndex, add or remove entrie comment +- Add zip::getCommentName and ::getCommentIndex, get an entrie comment +- Add zip::setArchiveComment +- Full sync with zip-0.7.1 +- #8009, modify archives on windows cannot be closed + + + 2006-04-26 + + 1.3.1 + 1.3.0 + + + alpha + alpha + + PHP License + - #7500, Fix build with php 5.1.0, 5.1.1 and 5.1.2 + + + + 2006-04-26 + + 1.3.0 + 1.3.0 + + + alpha + alpha + + - Fix possible leak with __set/__get in classes extending the Zip +intern object (tony2001[at]php.net) +- Added getFromName(), return the contents of an entry using its name +- Added getFromIndex(), return the contents of an entry using its index + + + 2006-03-30 + + 1.2.3 + 1.2.1 + + + alpha + alpha + + PHP License + - #7214, use binary safe string as return value, 0x00 was considered as +the end of the content, affected only the procedural API + + + 2006-03-22 + + + 1.2.2 + 1.2.1 + + + alpha + alpha + + PHP License + - fix a bug in internal zip_close, new archives were not written +- ::open returns now false on error +- update the "create.php" example, status and returned codes are now checked +- fix a leak when the ::open method is called twice or more with the same +object + + + 2006-03-19 + + + 1.2.1 + 1.2.1 + + + alpha + alpha + + PHP License + - fixed a possible build problem (mkstemp missing) +- fix a bug where extending the Zip class does allow +to write to a property, see php bugs #36743 (array) + + + 2006-03-12 + + + 1.2.0 + 1.2.0 + + + alpha + alpha + + PHP License + - enable stream by default, + * use uri like zip:///path/to/my.zip#entryname.dat + (the URI format may change in future version) +- add support for print_r/var_dump (Zip object) +- add tests for the OO (still need more) +- add more example, like using xmlreader and zip + stream to parse OpenDocument meta info + + + + 2006-03-05 + + + 1.1.2 + 1.1.0 + + + alpha + alpha + + PHP License + - do not create empty file when a zip entry is only +a directory (empty or not) + + + + 2006-03-02 + + + 1.1.1 + 1.1.0 + + + alpha + alpha + + PHP License + - Fix PHP 5.1 detection at compiletie +- fix possible build errors with gcc other than 4.0.x +- add better zlib detection +- "--with-zlib-dir=[DIR]" added + + + + 2006-03-01 + + + 1.1.0 + 1.1.0 + + + alpha + alpha + + PHP License + - replace the old zip extensions + * 100% compatible with the old API + * added zip creation support (write mode) + * added OO interface + * Stream support, getStream method + returns a file handler +- bundled libzip and drop zzlib usage + (see http://www.nih.at/libzip/) + + + + diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c new file mode 100644 index 0000000000..b736004fb4 --- /dev/null +++ b/ext/zip/php_zip.c @@ -0,0 +1,1934 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2006 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Piere-Alain Joye 0) { + len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname); + } else { + len = spprintf(&file_dirname_fullpath, 0, "%s", dest); + } + + php_basename(file, file_len, NULL, 0, &file_basename, &file_basename_len TSRMLS_CC); + + SAFEMODE_CHECKFILE(file_dirname_fullpath); + + /* let see if the path already exists */ + if (php_stream_stat_path(file_dirname_fullpath, &ssb) < 0) { + ret = php_stream_mkdir(file_dirname_fullpath, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL); + if (!ret) { + efree(file_dirname_fullpath); + efree(file_basename); + return 0; + } + } + + /* it is a standalone directory, job done */ + if (file[file_len - 1] == '/') { + efree(file_dirname_fullpath); + efree(file_basename); + return 1; + } + + len = spprintf(&fullpath, 0, "%s/%s/%s", dest, file_dirname, file_basename); + if (!len) { + efree(file_dirname_fullpath); + efree(file_basename); + return 0; + } + + /* check again the full path, not sure if it + * is required, does a file can have a different + * safemode status as its parent folder? + */ + SAFEMODE_CHECKFILE(fullpath); + + zf = zip_fopen(za, file, 0); + if (zf == NULL) { + efree(fullpath); + efree(file_dirname_fullpath); + efree(file_basename); + return 0; + } + + stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); + n = 0; + if (stream) { + while ((n=zip_fread(zf, b, sizeof(b))) > 0) php_stream_write(stream, b, n); + php_stream_close(stream); + } + n = zip_fclose(zf); + + efree(fullpath); + efree(file_basename); + efree(file_dirname_fullpath); + + if (n<0) { + return 0; + } else { + return 1; + } +} +/* }}} */ + +/* {{{ REGISTER_ZIP_CLASS_CONST_LONG */ +#define REGISTER_ZIP_CLASS_CONST_LONG(const_name, value) \ + zend_declare_class_constant_long(zip_class_entry, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC); +/* }}} */ + +/* {{{ ZIP_FROM_OBJECT */ +#define ZIP_FROM_OBJECT(intern, object) \ + { \ + ze_zip_object *obj = (ze_zip_object*) zend_object_store_get_object(object TSRMLS_CC); \ + intern = obj->za; \ + if (!intern) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized Zip object"); \ + RETURN_FALSE; \ + } \ + } +/* }}} */ + +/* {{{ RETURN_SB(sb) */ +#define RETURN_SB(sb) \ + { \ + array_init(return_value); \ + add_assoc_string(return_value, "name", estrdup((sb)->name), 0); \ + add_assoc_long(return_value, "index", (long) (sb)->index); \ + add_assoc_long(return_value, "crc", (long) (sb)->crc); \ + add_assoc_long(return_value, "size", (long) (sb)->size); \ + add_assoc_long(return_value, "mtime", (long) (sb)->mtime); \ + add_assoc_long(return_value, "comp_size", (long) (sb)->comp_size); \ + add_assoc_long(return_value, "comp_method", (long) (sb)->comp_method); \ + } +/* }}} */ + +static int php_zip_status(struct zip *za TSRMLS_DC) /* {{{ */ +{ + int zep, syp; + + zip_error_get(za, &zep, &syp); + return zep; +} +/* }}} */ + +static int php_zip_status_sys(struct zip *za TSRMLS_DC) /* {{{ */ +{ + int zep, syp; + + zip_error_get(za, &zep, &syp); + return syp; +} +/* }}} */ + +static int php_zip_get_num_files(struct zip *za TSRMLS_DC) /* {{{ */ +{ + return zip_get_num_files(za); +} +/* }}} */ + +static char * php_zipobj_get_filename(ze_zip_object *obj TSRMLS_DC) /* {{{ */ +{ + if (!obj) { + return NULL; + } + + if (obj->filename) { + return obj->filename; + } + return NULL; +} +/* }}} */ + +static char * php_zipobj_get_zip_comment(struct zip *za, int *len TSRMLS_DC) /* {{{ */ +{ + if (za) { + return (char *)zip_get_archive_comment(za, len, 0); + } + return NULL; +} +/* }}} */ + +#endif + +/* {{{ zend_function_entry */ +static zend_function_entry zip_functions[] = { + PHP_FE(zip_open, NULL) + PHP_FE(zip_close, NULL) + PHP_FE(zip_read, NULL) + PHP_FE(zip_entry_open, NULL) + PHP_FE(zip_entry_close, NULL) + PHP_FE(zip_entry_read, NULL) + PHP_FE(zip_entry_filesize, NULL) + PHP_FE(zip_entry_name, NULL) + PHP_FE(zip_entry_compressedsize, NULL) + PHP_FE(zip_entry_compressionmethod, NULL) + + {NULL, NULL, NULL} +}; +/* }}} */ + +/* {{{ ZE2 OO definitions */ +#ifdef ZEND_ENGINE_2_1 +zend_class_entry *zip_class_entry; +static zend_object_handlers zip_object_handlers; + +static HashTable zip_prop_handlers; + +typedef int (*zip_read_int_t)(struct zip *za TSRMLS_DC); +typedef char *(*zip_read_const_char_t)(struct zip *za, int *len TSRMLS_DC); +typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj TSRMLS_DC); + +typedef struct _zip_prop_handler { + zip_read_int_t read_int_func; + zip_read_const_char_t read_const_char_func; + zip_read_const_char_from_ze_t read_const_char_from_obj_func; + + int type; +} zip_prop_handler; +#endif +/* }}} */ + +#ifdef ZEND_ENGINE_2_1 +static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, zip_read_int_t read_int_func, zip_read_const_char_t read_char_func, zip_read_const_char_from_ze_t read_char_from_obj_func, int rettype TSRMLS_DC) /* {{{ */ +{ + zip_prop_handler hnd; + + hnd.read_const_char_func = read_char_func; + hnd.read_int_func = read_int_func; + hnd.read_const_char_from_obj_func = read_char_from_obj_func; + hnd.type = rettype; + zend_hash_add(prop_handler, name, strlen(name)+1, &hnd, sizeof(zip_prop_handler), NULL); +} +/* }}} */ + +static int php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, zval **retval, int newzval TSRMLS_DC) /* {{{ */ +{ + const char *retchar = NULL; + int retint = 0; + int len = 0; + + if (obj && obj->za != NULL) { + if (hnd->read_const_char_func) { + retchar = hnd->read_const_char_func(obj->za, &len TSRMLS_CC); + } else { + if (hnd->read_int_func) { + retint = hnd->read_int_func(obj->za TSRMLS_CC); + if (retint == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal zip error returned"); + return FAILURE; + } + } else { + if (hnd->read_const_char_from_obj_func) { + retchar = hnd->read_const_char_from_obj_func(obj TSRMLS_CC); + } + } + } + } + + if (newzval) { + ALLOC_ZVAL(*retval); + } + + switch (hnd->type) { + case IS_STRING: + if (retchar) { + ZVAL_STRINGL(*retval, (char *) retchar, len, 1); + } else { + ZVAL_EMPTY_STRING(*retval); + } + break; + case IS_BOOL: + ZVAL_BOOL(*retval, (long)retint); + break; + case IS_LONG: + ZVAL_LONG(*retval, (long)retint); + break; + default: + ZVAL_NULL(*retval); + } + + return SUCCESS; +} +/* }}} */ + +zval **php_zip_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC) /* {{{ */ +{ + ze_zip_object *obj; + zval tmp_member; + zval **retval = NULL; + + zip_prop_handler *hnd; + zend_object_handlers *std_hnd; + int ret; + + if (member->type != IS_STRING) { + tmp_member = *member; + zval_copy_ctor(&tmp_member); + convert_to_string(&tmp_member); + member = &tmp_member; + } + + ret = FAILURE; + obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC); + + if (obj->prop_handler != NULL) { + ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd); + } + + + if (ret == FAILURE) { + std_hnd = zend_get_std_object_handlers(); + retval = std_hnd->get_property_ptr_ptr(object, member TSRMLS_CC); + } + + if (member == &tmp_member) { + zval_dtor(member); + } + return retval; +} +/* }}} */ + +zval* php_zip_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */ +{ + ze_zip_object *obj; + zval tmp_member; + zval *retval; + zip_prop_handler *hnd; + zend_object_handlers *std_hnd; + int ret; + + if (member->type != IS_STRING) { + tmp_member = *member; + zval_copy_ctor(&tmp_member); + convert_to_string(&tmp_member); + member = &tmp_member; + } + + ret = FAILURE; + obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC); + + if (obj->prop_handler != NULL) { + ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd); + } + + if (ret == SUCCESS) { + ret = php_zip_property_reader(obj, hnd, &retval, 1 TSRMLS_CC); + if (ret == SUCCESS) { + /* ensure we're creating a temporary variable */ + retval->refcount = 0; + } else { + retval = EG(uninitialized_zval_ptr); + } + } else { + std_hnd = zend_get_std_object_handlers(); + retval = std_hnd->read_property(object, member, type TSRMLS_CC); + } + + if (member == &tmp_member) { + zval_dtor(member); + } + return retval; +} +/* }}} */ + +static HashTable *php_zip_get_properties(zval *object TSRMLS_DC)/* {{{ */ +{ + ze_zip_object *obj; + zip_prop_handler *hnd; + HashTable *props; + zval *val; + int ret; + char *key; + uint key_len; + HashPosition pos; + ulong num_key; + + obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC); + props = obj->zo.properties; + + if (obj->prop_handler == NULL) { + return NULL; + } + zend_hash_internal_pointer_reset_ex(obj->prop_handler, &pos); + + while (zend_hash_get_current_data_ex(obj->prop_handler, (void**)&hnd, &pos) == SUCCESS) { + zend_hash_get_current_key_ex(obj->prop_handler, &key, &key_len, &num_key, 0, &pos); + MAKE_STD_ZVAL(val); + ret = php_zip_property_reader(obj, hnd, &val, 0 TSRMLS_CC); + if (ret != SUCCESS) { + val = EG(uninitialized_zval_ptr); + } + zend_hash_update(props, key, key_len, (void *)&val, sizeof(zval *), NULL); + zend_hash_move_forward_ex(obj->prop_handler, &pos); + } + return obj->zo.properties; +} +/* }}} */ + +static void php_zip_object_free_storage(void *object TSRMLS_DC) /* {{{ */ +{ + ze_zip_object * intern = (ze_zip_object *) object; + int i; + + if (!intern) { + return; + } + if (intern->za) { + zip_close(intern->za); + intern->za = NULL; + } + + if (intern->buffers_cnt>0) { + for (i=0; ibuffers_cnt; i++) { + efree(intern->buffers[i]); + } + efree(intern->buffers); + } + + intern->za = NULL; + +#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5) + zend_object_std_dtor(&intern->zo TSRMLS_CC); +#else + if (intern->zo.guards) { + zend_hash_destroy(intern->zo.guards); + FREE_HASHTABLE(intern->zo.guards); + } + + if (intern->zo.properties) { + zend_hash_destroy(intern->zo.properties); + FREE_HASHTABLE(intern->zo.properties); + } +#endif + + if (intern->filename) { + efree(intern->filename); + } + efree(intern); +} +/* }}} */ + +PHP_ZIP_API zend_object_value php_zip_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */ +{ + ze_zip_object *intern; + zval *tmp; + zend_object_value retval; + + intern = emalloc(sizeof(ze_zip_object)); + memset(&intern->zo, 0, sizeof(zend_object)); + intern->zo.ce = class_type; + + intern->za = NULL; + intern->buffers = NULL; + intern->filename = NULL; + intern->buffers_cnt = 0; + intern->prop_handler = &zip_prop_handlers; + + ALLOC_HASHTABLE(intern->zo.properties); + zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0); + zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, + (void *) &tmp, sizeof(zval *)); + + retval.handle = zend_objects_store_put(intern, + NULL, + (zend_objects_free_object_storage_t) php_zip_object_free_storage, + NULL TSRMLS_CC); + + retval.handlers = (zend_object_handlers *) & zip_object_handlers; + + return retval; +} +/* }}} */ +#endif + +/* {{{ Resource dtors */ + +/* {{{ php_zip_free_dir */ +static void php_zip_free_dir(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + zip_rsrc * zip_int = (zip_rsrc *) rsrc->ptr; + + if (zip_int) { + if (zip_int->za) { + zip_close(zip_int->za); + } + + efree(rsrc->ptr); + + rsrc->ptr = NULL; + } +} +/* }}} */ + +/* {{{ php_zip_free_entry */ +static void php_zip_free_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + zip_read_rsrc *zr_rsrc = (zip_read_rsrc *) rsrc->ptr; + + efree(zr_rsrc); + rsrc->ptr = NULL; +} +/* }}} */ + +/* }}}*/ + +/* {{{ function prototypes */ +PHP_MINIT_FUNCTION(zip); +PHP_MSHUTDOWN_FUNCTION(zip); +PHP_MINFO_FUNCTION(zip); +/* }}} */ + +/* {{{ zip_module_entry + */ +zend_module_entry zip_module_entry = { + STANDARD_MODULE_HEADER, + "zip", + zip_functions, + PHP_MINIT(zip), + PHP_MSHUTDOWN(zip), + NULL, + NULL, + PHP_MINFO(zip), + "1.4.0", + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_ZIP +ZEND_GET_MODULE(zip) +#endif + +/* {{{ proto resource zip_open(string filename [,flags]) +Create new zip using source uri for output */ +PHP_FUNCTION(zip_open) +{ + char *filename; + int filename_len; + zip_rsrc *rsrc_int; + long mode = 0; + int err = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &mode) == FAILURE) { + return; + } + if (SAFEMODE_CHECKFILE(filename)) { + RETURN_FALSE; + } + + rsrc_int = (zip_rsrc *)emalloc(sizeof(zip_rsrc)); + + rsrc_int->za = zip_open(filename, mode, &err); + if (rsrc_int->za == NULL) { + efree(rsrc_int); + RETURN_LONG((long)err); + } + + rsrc_int->index_current = 0; + rsrc_int->num_files = zip_get_num_files(rsrc_int->za); + + ZEND_REGISTER_RESOURCE(return_value, rsrc_int, le_zip_dir); +} +/* }}} */ + +/* {{{ proto void zip_close(resource zip) + Close a Zip archive */ +PHP_FUNCTION(zip_close) +{ + zval * zip; + zip_rsrc *z_rsrc = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir); + + /* really close the zip will break BC :-D */ + zend_list_delete(Z_LVAL_P(zip)); +} +/* }}} */ + +/* {{{ proto resource zip_read(resource zip [, int flags]) + Returns the next file in the archive */ +PHP_FUNCTION(zip_read) +{ + zval *zip_dp; + zip_read_rsrc *zr_rsrc; + int ret; + long flags = 0; + zip_rsrc *rsrc_int; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zip_dp, flags) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(rsrc_int, zip_rsrc *, &zip_dp, -1, le_zip_dir_name, le_zip_dir); + + if (rsrc_int && rsrc_int->za) { + if (rsrc_int->index_current >= rsrc_int->num_files) { + RETURN_FALSE; + } + + zr_rsrc = emalloc(sizeof(zip_read_rsrc)); + + ret = zip_stat_index(rsrc_int->za, rsrc_int->index_current, flags, &zr_rsrc->sb); + + if (ret != 0) { + efree(zr_rsrc); + RETURN_LONG((long)ret); + } + + zr_rsrc->zf = zip_fopen_index(rsrc_int->za, rsrc_int->index_current, 0); + if (zr_rsrc->zf) { + rsrc_int->index_current++; + ZEND_REGISTER_RESOURCE(return_value, zr_rsrc, le_zip_entry); + } else { + RETURN_FALSE; + } + + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool zip_entry_open(resource zip_dp, resource zip_entry [, string mode]) + Open a Zip File, pointed by the resource entry */ +/* Dummy function to follow the old API */ +PHP_FUNCTION(zip_entry_open) +{ + zval * zip; + zval * zip_entry; + char *mode; + int mode_len; + zip_read_rsrc * zr_rsrc; + zip_rsrc *z_rsrc; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|s", &zip, &zip_entry, &mode, &mode_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry); + ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir); + + if (zr_rsrc->zf != NULL) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto void zip_entry_close(resource zip_ent) + Close a zip entry */ +/* another dummy function to fit in the old api*/ +PHP_FUNCTION(zip_entry_close) +{ + zval * zip_entry; + zip_read_rsrc * zr_rsrc; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry); + /* we got a zip_entry resource, be happy */ + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto mixed zip_entry_read(resource zip_entry [, int len [, int mode]]) + Read from an open directory entry */ +PHP_FUNCTION(zip_entry_read) +{ + zval * zip_entry; + long len = 0, mode = 0; + zip_read_rsrc * zr_rsrc; + char *buffer; + int n = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zip_entry, &len, &mode) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry); + + if (len <= 0) { + len = 1024; + } + + if (zr_rsrc->zf) { + buffer = safe_emalloc(len, 1, 1); + n = zip_fread(zr_rsrc->zf, buffer, len); + if (n > 0) { + buffer[n] = 0; + RETURN_STRINGL(buffer, n, 0); + } else { + RETURN_EMPTY_STRING() + } + } else { + RETURN_FALSE; + } +} +/* }}} */ + +static void php_zip_entry_get_info(INTERNAL_FUNCTION_PARAMETERS, int opt) /* {{{ */ +{ + zval * zip_entry; + zip_read_rsrc * zr_rsrc; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry); + + if (!zr_rsrc->zf) { + RETURN_FALSE; + } + + switch (opt) { + case 0: + RETURN_STRING((char *)zr_rsrc->sb.name, 1); + break; + case 1: + RETURN_LONG((long) (zr_rsrc->sb.comp_size)); + break; + case 2: + RETURN_LONG((long) (zr_rsrc->sb.size)); + break; + case 3: + switch (zr_rsrc->sb.comp_method) { + case 0: + RETURN_STRING("stored", 1); + break; + case 1: + RETURN_STRING("shrunk", 1); + break; + case 2: + case 3: + case 4: + case 5: + RETURN_STRING("reduced", 1); + break; + case 6: + RETURN_STRING("imploded", 1); + break; + case 7: + RETURN_STRING("tokenized", 1); + break; + case 8: + RETURN_STRING("deflated", 1); + break; + case 9: + RETURN_STRING("deflatedX", 1); + break; + case 10: + RETURN_STRING("implodedX", 1); + break; + default: + RETURN_FALSE; + } + RETURN_LONG((long) (zr_rsrc->sb.comp_method)); + break; + } + +} +/* }}} */ + +/* {{{ proto string zip_entry_name(resource zip_entry) + Return the name given a ZZip entry */ +PHP_FUNCTION(zip_entry_name) +{ + php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + +/* {{{ proto int zip_entry_compressedsize(resource zip_entry) + Return the compressed size of a ZZip entry */ +PHP_FUNCTION(zip_entry_compressedsize) +{ + php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} +/* }}} */ + +/* {{{ proto int zip_entry_filesize(resource zip_entry) + Return the actual filesize of a ZZip entry */ +PHP_FUNCTION(zip_entry_filesize) +{ + php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2); +} +/* }}} */ + +/* {{{ proto string zip_entry_compressionmethod(resource zip_entry) + Return a string containing the compression method used on a particular entry */ +PHP_FUNCTION(zip_entry_compressionmethod) +{ + php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3); +} +/* }}} */ + +#ifdef ZEND_ENGINE_2_1 +/* {{{ proto mixed open(string source [, int flags]) +Create new zip using source uri for output, return TRUE on success or the error code */ +ZIPARCHIVE_METHOD(open) +{ + struct zip *intern; + char *filename; + int filename_len; + int err = 0; + long flags = 0; + + zval *this = getThis(); + ze_zip_object *ze_obj = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (this) { + /* We do not use ZIP_FROM_OBJECT, zip init function here */ + ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC); + } + + if (filename_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source"); + RETURN_FALSE; + } + if (ze_obj->za) { + /* we already have an opened zip, free it */ + zip_close(ze_obj->za); + } + if (ze_obj->filename) { + efree(ze_obj->filename); + } + + intern = zip_open(filename, flags, &err); + if (!intern || err) { + RETURN_LONG((long)err); + } + ze_obj->filename = estrndup(filename, filename_len); + ze_obj->filename_len = filename_len; + ze_obj->za = intern; + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto resource close() +close the zip archive */ +ZIPARCHIVE_METHOD(close) +{ + struct zip *intern; + zval *this = getThis(); + ze_zip_object *ze_obj; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC); + + if (zip_close(intern)) { + RETURN_FALSE; + } + + efree(ze_obj->filename); + ze_obj->filename = NULL; + ze_obj->filename_len = 0; + ze_obj->za = NULL; + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool addFile(string filepath[, string entryname[, int start [, int length]]]) +Add a file in a Zip archive using its path and the name to use. */ +ZIPARCHIVE_METHOD(addFile) +{ + struct zip *intern; + zval *this = getThis(); + char *filename; + int filename_len; + char *entry_name = NULL; + int entry_name_len = 0; + struct zip_source *zs; + long offset_start = 0, offset_len = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sll", + &filename, &filename_len, &entry_name, &entry_name_len, &offset_start, &offset_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (filename_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as filename"); + RETURN_FALSE; + } + + if (entry_name_len == 0) { + entry_name = filename; + entry_name_len = filename_len; + } + + if (SAFEMODE_CHECKFILE(filename)) { + RETURN_FALSE; + } + + zs = zip_source_file(intern, filename, 0, 0); + if (!zs) { + RETURN_FALSE; + } + if (zip_add(intern, entry_name, zs) < 0) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ proto resource addFromString(string name, string content) +Add a file using content and the entry name */ +ZIPARCHIVE_METHOD(addFromString) +{ + struct zip *intern; + zval *this = getThis(); + char *buffer, *name; + int buffer_len, name_len; + ze_zip_object *ze_obj; + struct zip_source *zs; + int pos = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &name, &name_len, &buffer, &buffer_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC); + if (ze_obj->buffers_cnt) { + ze_obj->buffers = (char **)erealloc(ze_obj->buffers, sizeof(char *) * (ze_obj->buffers_cnt+1)); + pos = ze_obj->buffers_cnt++; + } else { + ze_obj->buffers = (char **)emalloc(sizeof(char *)); + ze_obj->buffers_cnt++; + pos = 0; + } + ze_obj->buffers[pos] = (char *)emalloc(buffer_len); + memcpy(ze_obj->buffers[pos], buffer, buffer_len); + + zs = zip_source_buffer(intern, ze_obj->buffers[pos], buffer_len, 0); + + if (zs == NULL) { + RETURN_FALSE; + } + + if (zip_add(intern, name, zs) == -1) { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto resource statName(string filename[, int flags]) +Returns the information about a the zip entry filename */ +ZIPARCHIVE_METHOD(statName) +{ + struct zip *intern; + zval *this = getThis(); + char *name; + int name_len; + long flags = 0; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", + &name, &name_len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + PHP_ZIP_STAT_PATH(intern, name, name_len, flags, sb); + + RETURN_SB(&sb); +} +/* }}} */ + +/* {{{ proto resource statIndex(int index[, int flags]) +Returns the zip entry informations using its index */ +ZIPARCHIVE_METHOD(statIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index, flags = 0; + + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", + &index, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (zip_stat_index(intern, index, flags, &sb) != 0) { + RETURN_FALSE; + } + RETURN_SB(&sb); +} +/* }}} */ + +/* {{{ proto resource locateName(string filename[, int flags]) +Returns the index of the entry named filename in the archive */ +ZIPARCHIVE_METHOD(locateName) +{ + struct zip *intern; + zval *this = getThis(); + char *name; + int name_len; + long flags = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", + &name, &name_len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + if (name_len<1) { + RETURN_FALSE; + } + + RETURN_LONG((long)zip_name_locate(intern, (const char *)name, flags)) +} +/* }}} */ + +/* {{{ proto resource getNameIndex(int index [, int flags]) +Returns the name of the file at position index */ +ZIPARCHIVE_METHOD(getNameIndex) +{ + struct zip *intern; + zval *this = getThis(); + const char *name; + long flags = 0, index = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", + &index, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + name = zip_get_name(intern, (int) index, flags); + + if (name) { + RETVAL_STRING((char *)name, 1); + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto resource setArchiveComment(string name, string comment) +Set or remove (NULL/'') the comment of the archive */ +ZIPARCHIVE_METHOD(setArchiveComment) +{ + struct zip *intern; + zval *this = getThis(); + long comment_len = 0; + char * comment; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &comment, &comment_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + if (zip_set_archive_comment(intern, (const char *)comment, (int)comment_len)) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ proto resource getArchiveComment() +Returns the comment of an entry using its index */ +ZIPARCHIVE_METHOD(getArchiveComment) +{ + struct zip *intern; + zval *this = getThis(); + long flags = 0; + const char * comment; + int comment_len = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + comment = zip_get_archive_comment(intern, &comment_len, (int)flags); + RETURN_STRINGL((char *)comment, (long)comment_len, 1); +} +/* }}} */ + +/* {{{ proto resource setCommentName(string name, string comment) +Set or remove (NULL/'') the comment of an entry using its Name */ +ZIPARCHIVE_METHOD(setCommentName) +{ + struct zip *intern; + zval *this = getThis(); + long comment_len = 0, name_len = 0; + char * comment, *name; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &name, &name_len, &comment, &comment_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + + PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb); + PHP_ZIP_SET_FILE_COMMENT(intern, sb.index, comment, comment_len); +} +/* }}} */ + +/* {{{ proto resource setCommentIndex(int index, string comment) +Set or remove (NULL/'') the comment of an entry using its index */ +ZIPARCHIVE_METHOD(setCommentIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index, comment_len = 0; + char * comment; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", + &index, &comment, &comment_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + PHP_ZIP_STAT_INDEX(intern, index, 0, sb); + PHP_ZIP_SET_FILE_COMMENT(intern, index, comment, comment_len); +} +/* }}} */ + +/* {{{ proto resource getCommentName(string name) +Returns the comment of an entry using its name */ +ZIPARCHIVE_METHOD(getCommentName) +{ + struct zip *intern; + zval *this = getThis(); + long name_len = 0, flags = 0; + int comment_len = 0; + const char * comment; + char *name; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", + &name, &name_len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb); + comment = zip_get_file_comment(intern, sb.index, &comment_len, (int)flags); + RETURN_STRINGL((char *)comment, (long)comment_len, 1); +} +/* }}} */ + +/* {{{ proto resource getCommentIndex(int index) +Returns the comment of an entry using its index */ +ZIPARCHIVE_METHOD(getCommentIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index, flags = 0; + const char * comment; + int comment_len = 0; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", + &index, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + PHP_ZIP_STAT_INDEX(intern, index, 0, sb); + comment = zip_get_file_comment(intern, index, &comment_len, (int)flags); + RETURN_STRINGL((char *)comment, (long)comment_len, 1); +} +/* }}} */ + +/* {{{ proto resource deleteIndex(int index) +Delete a file using its index */ +ZIPARCHIVE_METHOD(deleteIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (index < 0) { + RETURN_FALSE; + } + + if (zip_delete(intern, index) < 0) { + RETURN_FALSE; + } + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto resource deleteName(string name) +Delete a file using its index */ +ZIPARCHIVE_METHOD(deleteName) +{ + struct zip *intern; + zval *this = getThis(); + long name_len = 0; + char *name; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + if (name_len < 1) { + RETURN_FALSE; + } + + PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb); + if (zip_delete(intern, sb.index)) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto resource renameIndex(int index, string new_name) +Rename an entry selected by its index to new_name */ +ZIPARCHIVE_METHOD(renameIndex) +{ + struct zip *intern; + zval *this = getThis(); + + char *new_name; + int new_name_len; + long index; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &index, &new_name, &new_name_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (index < 0) { + RETURN_FALSE; + } + + if (new_name_len < 1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as new entry name"); + RETURN_FALSE; + } + if (zip_rename(intern, index, (const char *)new_name) != 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto resource renameName(string name, string new_name) +Rename an entry selected by its name to new_name */ +ZIPARCHIVE_METHOD(renameName) +{ + struct zip *intern; + zval *this = getThis(); + struct zip_stat sb; + char *name, *new_name; + int name_len, new_name_len; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &new_name, &new_name_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (new_name_len < 1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as new entry name"); + RETURN_FALSE; + } + + PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb); + + if (zip_rename(intern, sb.index, (const char *)new_name)) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto resource unchangeIndex(int index) +Changes to the file at position index are reverted */ +ZIPARCHIVE_METHOD(unchangeIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (index < 0) { + RETURN_FALSE; + } + + if (zip_unchange(intern, index) != 0) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ proto resource unchangeName(string name) +Changes to the file named 'name' are reverted */ +ZIPARCHIVE_METHOD(unchangeName) +{ + struct zip *intern; + zval *this = getThis(); + struct zip_stat sb; + char *name; + int name_len; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (name_len < 1) { + RETURN_FALSE; + } + + PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb); + + if (zip_unchange(intern, sb.index) != 0) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ proto resource unchangeAll() +All changes to files and global information in archive are reverted */ +ZIPARCHIVE_METHOD(unchangeAll) +{ + struct zip *intern; + zval *this = getThis(); + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zip_unchange_all(intern) != 0) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ proto resource unchangeAll() +Revert all global changes to the archive archive. For now, this only reverts archive comment changes. */ +ZIPARCHIVE_METHOD(unchangeArchive) +{ + struct zip *intern; + zval *this = getThis(); + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zip_unchange_archive(intern) != 0) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } +} +/* }}} */ + +/* {{{ array resource extractTo(string pathto[, mixed files]) +Extract one or more file from a zip archive */ +/* TODO: + * - allow index or array of indeces + * - replace path + * - patterns + */ +ZIPARCHIVE_METHOD(extractTo) +{ + struct zip *intern; + + zval *this = getThis(); + zval *zval_files = NULL; + zval **zval_file = NULL; + php_stream_statbuf ssb; + char *pathto; + char *file; + int pathto_len; + int ret, i; + + int nelems; + + if (!this) { + RETURN_FALSE; + } + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &pathto, &pathto_len, &zval_files) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (pathto_len<1) { + RETURN_FALSE; + } + + if (php_stream_stat_path(pathto, &ssb) < 0) { + ret = php_stream_mkdir(pathto, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL); + if (!ret) { + efree(pathto); + RETURN_FALSE; + } + } + + ZIP_FROM_OBJECT(intern, this); + if (zval_files) { + switch (Z_TYPE_P(zval_files)) { + case IS_STRING: + file = Z_STRVAL_P(zval_files); + if (!php_zip_extract_file(intern, pathto, file TSRMLS_CC)) { + RETURN_FALSE; + } + break; + case IS_ARRAY: + nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files)); + if (nelems == 0 ) { + RETURN_FALSE; + } + for (i = 0; i < nelems; i++) { + if (zend_hash_index_find(Z_ARRVAL_P(zval_files), i, (void **) &zval_file) == SUCCESS) { + switch (Z_TYPE_PP(zval_file)) { + case IS_LONG: + break; + case IS_STRING: + file = Z_STRVAL_PP(zval_file); + if (!php_zip_extract_file(intern, pathto, file TSRMLS_CC)) { + RETURN_FALSE; + } + break; + } + } + } + break; + case IS_LONG: + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument, expect string or array of strings"); + break; + } + } else { + /* Extract all files */ + int filecount = zip_get_num_files(intern); + + if (filecount == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal archive"); + RETURN_FALSE; + } + + for (i = 0; i < filecount; i++) { + file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED); + if (!php_zip_extract_file(intern, pathto, file TSRMLS_CC)) { + RETURN_FALSE; + } + } + } + + RETURN_TRUE; +} +/* }}} */ + +static void php_zip_get_from(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */ +{ + struct zip *intern; + zval *this = getThis(); + + struct zip_stat sb; + struct zip_file *zf; + + char *filename; + int filename_len; + long index; + long flags = 0; + long len = 0; + + char *buffer; + int n = 0; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (type == 1) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &filename, &filename_len, &len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + PHP_ZIP_STAT_PATH(intern, filename, filename_len, flags, sb); + } else { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &index, &len, &flags) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + PHP_ZIP_STAT_INDEX(intern, index, 0, sb); + } + + if (sb.size < 1) { + RETURN_EMPTY_STRING(); + } + + if (len < 1) { + len = sb.size; + } + + zf = zip_fopen(intern, filename, flags); + if (zf == NULL) { + RETURN_FALSE; + } + + buffer = safe_emalloc(len + 1, 1, 1); + n = zip_fread(zf, buffer, len); + if (n < 1) { + RETURN_EMPTY_STRING(); + } + + zip_fclose(zf); + buffer[n] = 0; + RETURN_STRINGL(buffer, n, 0); +} +/* }}} */ + +/* {{{ proto resource getFromName(string entryname[, int len [, int flags]]) +get the contents of an entry using its name */ +ZIPARCHIVE_METHOD(getFromName) +{ + php_zip_get_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} +/* }}} */ + +/* {{{ proto resource getFromIndex(string entryname[, int len [, int flags]]) +get the contents of an entry using its index */ +ZIPARCHIVE_METHOD(getFromIndex) +{ + php_zip_get_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + +/* {{{ proto resource getStream(string entryname) +get a stream for an entry using its name */ +ZIPARCHIVE_METHOD(getStream) +{ + struct zip *intern; + zval *this = getThis(); + struct zip_stat sb; + char *filename; + int filename_len; + char *mode = "rb"; + php_stream *stream; + ze_zip_object *obj; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { + WRONG_PARAM_COUNT; + return; + } + + if (zip_stat(intern, filename, 0, &sb)) { + RETURN_FALSE; + } + + obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC); + + stream = php_stream_zip_open(obj->filename, filename, mode STREAMS_CC TSRMLS_CC); + if (stream) { + php_stream_to_zval(stream, return_value); + } +} +/* }}} */ + +/* {{{ ze_zip_object_class_functions */ +static zend_function_entry zip_class_functions[] = { + ZIPARCHIVE_ME(open, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(close, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(addFromString, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(addFile, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(renameIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(renameName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(setArchiveComment, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getArchiveComment, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(setCommentIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(setCommentName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getCommentIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getCommentName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(deleteIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(deleteName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(statName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(statIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(locateName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getNameIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(unchangeArchive, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(unchangeAll, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(unchangeIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(unchangeName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(extractTo, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getFromName, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getFromIndex, NULL, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getStream, NULL, ZEND_ACC_PUBLIC) + {NULL, NULL, NULL} +}; +/* }}} */ +#endif + +/* {{{ PHP_MINIT_FUNCTION */ +PHP_MINIT_FUNCTION(zip) +{ +#ifdef ZEND_ENGINE_2_1 + zend_class_entry ce; + + memcpy(&zip_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + zip_object_handlers.clone_obj = NULL; + zip_object_handlers.get_property_ptr_ptr = php_zip_get_property_ptr_ptr; + zip_object_handlers.write_property = NULL; + + zip_object_handlers.get_properties = php_zip_get_properties; + zip_object_handlers.read_property = php_zip_read_property; + + INIT_CLASS_ENTRY(ce, "ZipArchive", zip_class_functions); + ce.create_object = php_zip_object_new; + zip_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + + zend_hash_init(&zip_prop_handlers, 0, NULL, NULL, 1); + php_zip_register_prop_handler(&zip_prop_handlers, "status", php_zip_status, NULL, NULL, IS_LONG TSRMLS_CC); + php_zip_register_prop_handler(&zip_prop_handlers, "statusSys", php_zip_status_sys, NULL, NULL, IS_LONG TSRMLS_CC); + php_zip_register_prop_handler(&zip_prop_handlers, "numFiles", php_zip_get_num_files, NULL, NULL, IS_LONG TSRMLS_CC); + php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, NULL, php_zipobj_get_filename, IS_STRING TSRMLS_CC); + php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, NULL, IS_STRING TSRMLS_CC); + + REGISTER_ZIP_CLASS_CONST_LONG("CREATE", ZIP_CREATE); + REGISTER_ZIP_CLASS_CONST_LONG("EXCL", ZIP_EXCL); + REGISTER_ZIP_CLASS_CONST_LONG("CHECKCONS", ZIP_CHECKCONS); + REGISTER_ZIP_CLASS_CONST_LONG("FL_NOCASE", ZIP_FL_NOCASE); + REGISTER_ZIP_CLASS_CONST_LONG("FL_NODIR", ZIP_FL_NODIR); + REGISTER_ZIP_CLASS_CONST_LONG("FL_COMPRESSED", ZIP_FL_COMPRESSED); + REGISTER_ZIP_CLASS_CONST_LONG("FL_UNCHANGED", ZIP_FL_UNCHANGED); + REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFAULT", ZIP_CM_DEFAULT); + REGISTER_ZIP_CLASS_CONST_LONG("CM_STORE", ZIP_CM_STORE); + REGISTER_ZIP_CLASS_CONST_LONG("CM_SHRINK", ZIP_CM_SHRINK); + REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_1", ZIP_CM_REDUCE_1); + REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_2", ZIP_CM_REDUCE_2); + REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_3", ZIP_CM_REDUCE_3); + REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_4", ZIP_CM_REDUCE_4); + REGISTER_ZIP_CLASS_CONST_LONG("CM_IMPLODE", ZIP_CM_IMPLODE); + REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFLATE", ZIP_CM_DEFLATE); + REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFLATE64", ZIP_CM_DEFLATE64); + REGISTER_ZIP_CLASS_CONST_LONG("CM_PKWARE_IMPLODE", ZIP_CM_PKWARE_IMPLODE); + + /* Error code */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_OK", ZIP_ER_OK); /* N No error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_MULTIDISK", ZIP_ER_MULTIDISK); /* N Multi-disk zip archives not supported */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_RENAME", ZIP_ER_RENAME); /* S Renaming temporary file failed */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_CLOSE", ZIP_ER_CLOSE); /* S Closing zip archive failed */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_SEEK", ZIP_ER_SEEK); /* S Seek error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_READ", ZIP_ER_READ); /* S Read error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_WRITE", ZIP_ER_WRITE); /* S Write error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_CRC", ZIP_ER_CRC); /* N CRC error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_ZIPCLOSED", ZIP_ER_ZIPCLOSED); /* N Containing zip archive was closed */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_NOENT", ZIP_ER_NOENT); /* N No such file */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_EXISTS", ZIP_ER_EXISTS); /* N File already exists */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_OPEN", ZIP_ER_OPEN); /* S Can't open file */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_TMPOPEN", ZIP_ER_TMPOPEN); /* S Failure to create temporary file */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_ZLIB", ZIP_ER_ZLIB); /* Z Zlib error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_MEMORY", ZIP_ER_MEMORY); /* N Malloc failure */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_CHANGED", ZIP_ER_CHANGED); /* N Entry has been changed */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_COMPNOTSUPP", ZIP_ER_COMPNOTSUPP);/* N Compression method not supported */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_EOF", ZIP_ER_EOF); /* N Premature EOF */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_INVAL", ZIP_ER_INVAL); /* N Invalid argument */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_NOZIP", ZIP_ER_NOZIP); /* N Not a zip archive */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_INTERNAL", ZIP_ER_INTERNAL); /* N Internal error */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_INCONS", ZIP_ER_INCONS); /* N Zip archive inconsistent */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_REMOVE", ZIP_ER_REMOVE); /* S Can't remove file */ + REGISTER_ZIP_CLASS_CONST_LONG("ER_DELETED", ZIP_ER_DELETED); /* N Entry has been deleted */ + + php_register_url_stream_wrapper("zip", &php_stream_zip_wrapper TSRMLS_CC); +#endif + + le_zip_dir = zend_register_list_destructors_ex(php_zip_free_dir, NULL, le_zip_dir_name, module_number); + le_zip_entry = zend_register_list_destructors_ex(php_zip_free_entry, NULL, le_zip_entry_name, module_number); + + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION + */ +PHP_MSHUTDOWN_FUNCTION(zip) +{ +#ifdef ZEND_ENGINE_2_1 + zend_hash_destroy(&zip_prop_handlers); + php_unregister_url_stream_wrapper("zip" TSRMLS_CC); +#endif + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(zip) +{ + php_info_print_table_start(); + { + php_info_print_table_row(2, "Zip", "enabled"); + php_info_print_table_row(2, "$Id$", "enabled"); + php_info_print_table_row(2, "Zip version", "1.4.0"); + php_info_print_table_row(2, "Libzip version", "0.7.1"); + } + php_info_print_table_end(); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/zip/php_zip.h b/ext/zip/php_zip.h new file mode 100644 index 0000000000..2b194f7583 --- /dev/null +++ b/ext/zip/php_zip.h @@ -0,0 +1,101 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2006 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Pierre-Alain Joye | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_ZIP_H +#define PHP_ZIP_H + +extern zend_module_entry zip_module_entry; +#define phpext_zip_ptr &zip_module_entry + +#ifdef PHP_WIN32 +#define PHP_ZIP_API __declspec(dllexport) +#else +#define PHP_ZIP_API +#endif + +#ifdef ZTS +#include "TSRM.h" +#endif + +#include "lib/zip.h" + +#ifndef ZEND_ENGINE_2_1 +# if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 0) +# define ZEND_ENGINE_2_1 +# endif +#endif + +typedef struct _ze_zip_rsrc { + struct zip *za; + int index_current; + int num_files; +} zip_rsrc; + +typedef zip_rsrc * zip_rsrc_ptr; + +typedef struct _ze_zip_read_rsrc { + struct zip_file *zf; + struct zip_stat sb; +} zip_read_rsrc; + +#ifdef ZEND_ENGINE_2_1 +#define ZIPARCHIVE_ME(name, arg_info, flags) ZEND_FENTRY(name, c_ziparchive_ ##name, arg_info, flags) +#define ZIPARCHIVE_METHOD(name) ZEND_NAMED_FUNCTION(c_ziparchive_##name) + +/* Extends zend object */ +typedef struct _ze_zip_object { + zend_object zo; + struct zip *za; + int buffers_cnt; + char **buffers; + HashTable *prop_handler; + char *filename; + int filename_len; +} ze_zip_object; + +zend_class_entry *zip_class_entry_ce; + +php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); +php_stream *php_stream_zip_open(char *filename, char *path, char *mode STREAMS_DC TSRMLS_DC); + +extern php_stream_wrapper php_stream_zip_wrapper; +#endif + +PHP_FUNCTION(zip_open); +PHP_FUNCTION(zip_read); +PHP_FUNCTION(zip_close); +PHP_FUNCTION(zip_entry_read); +PHP_FUNCTION(zip_entry_filesize); +PHP_FUNCTION(zip_entry_name); +PHP_FUNCTION(zip_entry_compressedsize); +PHP_FUNCTION(zip_entry_compressionmethod); +PHP_FUNCTION(zip_entry_open); +PHP_FUNCTION(zip_entry_close); + +#endif /* PHP_ZIP_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/zip/tests/001.phpt b/ext/zip/tests/001.phpt new file mode 100644 index 0000000000..37dccc055e --- /dev/null +++ b/ext/zip/tests/001.phpt @@ -0,0 +1,12 @@ +--TEST-- +Check for zip presence +--SKIPIF-- + +--POST-- +--GET-- +--FILE-- + +--EXPECT-- +zip extension is available diff --git a/ext/zip/tests/binarynull.zip b/ext/zip/tests/binarynull.zip new file mode 100644 index 0000000000000000000000000000000000000000..9da004efed082ebb572404a893dc32ee6f140ccd GIT binary patch literal 656 zcmWIWW@Zs#U|`^2sA;M*nY?a~`7cHWhV4uY44e$23?-?>C3;x}sp+91oD9r7FST97 zfjG2+h2aJB3+CVTkN-2XG3uN9o3pvOv9Y-+r2P2bfAE~;iK~W3>`!xYYv)&1w&)_?qT z^|=51{WX8T{XKp@UZLL5$iVmzLs&g0)8C^<4jnpki0_lQa +--FILE-- + +--EXPECT-- +Ok \ No newline at end of file diff --git a/ext/zip/tests/bug7658.odt b/ext/zip/tests/bug7658.odt new file mode 100644 index 0000000000000000000000000000000000000000..527e09fefcefc76d452a3679f4028ac4459f4fbb GIT binary patch literal 8873 zcma)C2RzjO|G!Ae9+8a52SLXfBhccx5xYLeed_{`Fg$Hfg=$@o(OwEBoqxt3nJZN2z#U*)(wWh2x4Gf7`;D~;qgBcGrpvR zL^!}5v8a7DXkj5V28zL=ZJ{WkZ@8m)lLRMzg75``BL{I%J3E*w46g=>60*ahPz38< zZmz@tJxx-A(*PpE2R{AV9VUG1LA4I9NGJyO!x;kppfi8#ggeq5>y94)?DtOm4afQy zYwjqdBMOH8t^0pN{>@KyR3YDg8ZPirC5%lV z@0A#Jm$O%}h?EE`)%-%d(6UHyhVt378xJ~O-{=6 zy}Bv}_JJ<)w3dy?!-JNeN9HO_E{vWq)$f~?;L3RtRr~yywbSR^x!RIxZVmZ@#6Dno zPF`EhsL@-SILxX$uAR}cPlqRIF~_w)!P&@%+taAlq=WsKjcj{ihJe+u$O~WD^)u$P zI~?~q9rDbE=}@eJj*gCgY(O?04NI#F4*GC98r9oihQK_Ksf|e5YX0s?*FGLAdnHa> z#u1llMk7(SdF0OEqRx1dz}#Eo4J`w0dy*vAAl`dCJX4%Q%FN>p z02;ab4SEY3;To-3Gyn+M4RSt>G z6%R{&R~WB+Y3t^Bt(?stKHntr;6WnSLW#|~jxcpaKCy^Ya6=?iGPuZYC-0u`U|j#S zD$-`mUZuq(0o8yVoB0@YOJ5+&ZYqr>Ch%+fjV|@FS6d4wyR1Z)Tr>MVV9UNTCnY2% z$+ix(cY1uGXR^XFC>yD%r6o_j{yey7ytU9IakL>doIgU2i_494P(SiC50m!GR=2H- zLF!UTfG~2M99!T=w&;-aE>GtXN94FrLd~jZK`T`?)&e8w)T(4;oG;zFctPR)P~Qv% zH6gte?>q$3#V4%NLwdAsMkVhzL~q`F&>%afY({^?dMdN%Cz;)FC4EDsfx8C1zVg& zwl#LW)xp{#E@`_vFU7e!knE|s%lfY>(pGz(SkLkF0LT}tlHGXK`cPj~n4vUxfbB`b zOv)FGJUdh9r_`$;d!zhSdxwokGZk;{$n%NbTpiJQw;e#;Zv&{!j!~v1-;6#NTz_)Q zCy=v_AyD2*;xTyyL%QKKh5SZZ&utVDgO3K&W8QAcsyA0#O`(hd8UD!9f>wLnt@emU z2S49aGHn*?J2&sx$huTq%+A=&YmsRd2FgktN3T+z;~;DQ7+b{8Fc*3uMTR!vTA0FF zI8l&Jiv9VX!(1=)1#k8mk}F4WPBl1jsfI*$jd72?1bcTCB(I6IzQ;yI3d0fr>U6@ z)ZN6@SD!PnV_6~fEM~d<>n@RIV$a@;O+^VQXh!-5Ygn;D>^yYBi<`!fl!*wbz58C? zH`#<@fRZu)h7Dq~DOwMb&fPtq?5oh4rAXdYkfHnf$S|^4|8^-P^kveWw3^bSp#tAE zkpaD0Zpr!wk7likkpl3gIP0vK+eu>@s+47sAr+`&ygKU4=;=*>Nmy>~od>|Xb`5-( zLrC=_vsE*kds-*Pnn#;0I$kn__r6BVEhWScQa%JxNl%QuK|q+7S*kZIOsjS-LEG9I z^_6{W1a;}PUQ$EugE;SkH}s`lTlGI_*vVfXJ=GZYb!iBvM!U+MnkHuA!AyZ>#Ws`= z-L!@$K<~^(+MAxBbGdDJNY>n6{wht+0}tU~)LqHqrVKjw!Er|K3xd_`CbVB0JqDO#kg#U?wnZeQ5 zhi(UBo@ezGbQ_P_&F3aJM{OB9trl;N*$VCw{rgx%hJRP1F_&FoXu`>-)7%<4Bu3*q zmPrF~MHT}y7wMkNgO0Nqr@U=^AWL1WY%$EI&ymfylsU%6uTgwX(m9f0Ug^-sH^9T~)y;>rtO|<0VfSSiDvSwq8T2Sn_BF z7meWO7TPCM<}oblq66`fj9N&o$_NI5nw~T7Moljwn$N}yAnq%{8<#pQq3>iP(}%vs z0?~Yl{DU0~s#ee8e6>;Npyd({X>|iL!`CZ*C)WMr_N-bfz))4Vw_7~ z=(E+)ISzJ>!tBUp6)|6Lp(4G~lrzb1qS~h^X%>COldqi1vdss+lhrWF@e-BRYGr$& z$uDi9|BN9Ei>ooOF|QmughR*H=nsP!3NjtvUOd|?I90@?$-Kg&InJ>_1!Y!qBbRix zSiX99P<>8U`hjp{q2U}3$yQo9{d}i|^-^mYB#uOT@~FZ)GcrM{Bs!t)_PrblhL_en zA+c>~@!edZ*B-^ZmZv^B;>=3&LI7a^=wu@?27FY?kav3MRN;7Dm0dVPG_lyH;VWiJ zJozDuRYpYuWY9w<08DjN<G_$##N@!Hi-u^vjydXnWncdclyJhKu?t#Z z$qQy}$3w_2=GCfIIUJIj47W}e%VWv^tdtRVvF~kum0v^Ey+XD;6=?^r&O9Y=mE45P z!dW-pC=Eq#{a~fZm4cDE1c##2Db^xNOHZcyCOLtV;7OfJg~w7^=J^FaO;(uenyO(+;j}brjoLcH9K{dMlg3}tS8)q&*p{ICNN>(^ zQw(rO#;&)}K!2L8E04vZnUCUR&jl}M84_vPO=AVks<0qV2k*ngRt^-@9IQ-@Hq}w1 zYD|x9GGXboysr?rg}HTgNQpyuFdeRsSvNf#tHldS+Xtu&1_y$>aG` zb~y6v$C?iTRP4yRoQje}Y68F-OnkhtUuH~^eNz8>X0Fdt9thfnYv*0m*j{;SEnLJI zN~6hg+Oby|A>m2edee^sB{gG}d(PQ~nj(8h-%R-9PIojJ8^vX3CpH{;?!KwC0Eh^w zpH);6rs6S8i?A=emiQ^ggQA;^ZZNKy3v}FdL|Mf5>QPCB1?c=i@=yx*9dV#$&96r0VD zl6^SxPjv&ib!EcRUiw)b8a>4Gl4;>Ga5~0H@gu@Zh`BzcOSvgz=4AF1gY9HPN_C|% zE#ryAU9asaAIaUJezc5JeVU>z%M(p!q5m8d`$Da?%B!SSf*noDFRdKyc_Sq)m2^k1 zisA}XnukhiJhxUY0aCtl1pM-?YI9whO6v9k+s@(E=XRxQ9f)Iwk|IHk<&j^L#%z-n zPK*W!;}lQj$=lxwmg&|Dq`8+is}CO=8O{w)Jd7P!1vV#r+(G z^=tHipb1sZ`gOqK@#1lon^7B?{lUvrhwsvW2A|EN5|W>^4NDAGHYlH^;vBy)IN;K_ zAs13PrV+tr60NOd5pH!hMOv{Nbb3_L+g!2L)~MsiGjH<=iWRr|tG%{ca#DS^$=)aK zl6Dj(CW3)ogX62C5Tu|CvEN(iZQA_1>)qvb!|%OE9X?x9j^@Obc;KcZMC+ICIc2KN zk_#&IR+*&<9+jiMm^CeUK_=MzB{Z6(_0}*W-wx2Zq`X{f(9G$@rdD0Q(Ca6+PP`w; z#pe(B9FNbs__}udcOUs@0W%-()-d(dvUG@6BiLYRqt^J#D%bm?k7UO@4VbrOXS{lo zY?j(K!$O&BoKMuGoQZio{ASttOXs<0kJeLiIjM4cFj}q$%cljN@{@io53rJTc(iWQ z(eW;I@zmy`&&K;4UryF`o!m#={x;IDddC}b7RpcyH|tF(bMyA@`RNL$L}fP7ZkoFTjM`Joru!;fA2a5@D-xg7 z40AoHjQASCYsSMRgCP|!)MtD!(cH0E+IZcZYu0Non1=M& z4XFkUitozDLLb#nW~ZIHoT=w>jHE9ytjoOW#)GcfBf)a(G?W1Oo?6zutrXneAp#p< zjBMa%#9vXZxeu}d6Ai&LmWKfVLf&UOU>GRjT`D7w|KRY1q0o4)LzWXP2;$^`A?%R$ zaD<~Qr;(v5P=Zrlj!XvW-~hLSN&TQdfOwt+%7KR>&{E%;WI3@Ygw#G!B87mu!O&6| zJE`9p6RF>&rS=VdtMPJ$BV1%ToiG@8DIp;o4kw5c5k#UKg}{=Ml0y5foZn5^+x?Vt z$D&;KrR?p5@bn0QlR*oC1;Ii;WC z4a|KW3wN~#N=QnAWP}bV{AK`-fMeiLSD+mVM&OC$l%Xg%lEVP*h{O->cg#VVZ^(TF ze(r(xcs585-#alNNE8T?Gz5dCM8HzQlHbvP%E{QDDJz9Srgc9-q&2Ru2j@OO&`>i-nB|IVw)=^27A2#aV# zL@pkH{gn6y#G+v+pgqh1j)2*7?2jQ~mCACeBOH(%U{0a$Gxa~kg#VkE$bS=q{1p57 zR1qjbI2r@D`{p4AYU>IE+99zBj4Y=x=eNpzj_OAx{%PIs3(}UbS`OB_E^11K+;w?ovx zAW*z_r9`k{$7SzX&rtzl+TdFn)_Vip)!Q0eeA7%_;bmy^r9%vwG2v$0qYadv6B#2Hzi**p@9I^FG^fnw{DtFvXxCfMD~1hf7^LyTI? zz2Th?E@wUa7G7K)+1_ibX+o|+M&B@AgUg_GpH)iuN!F)Nn6-APiVIM+Z?)PcBSxo7 zWh^f@#$BQ(nccIvwAFUCPH!{G$ZA(R;z>Z>oBpN5x-Hw0p*r~)4_9|t;dLwb%?Aq~ zNRzOkqcR_`-V-0PMIJv>f(O6nGftS#J(LwhDC z<<2~m_p#ms*5R%#xQNYneMG+LDl70>FWzA<+SR$-zr8+rP4w7KIm_CK`w1tcN3(fG z7z!({EF;F&FG?Bn_J1Bzr?|4W{@Cx%;v`*icotUIck$Z$nbEHwqvm1VX^Yb7dvk+> zg4di|iau?0$5l}0Ko1SA)b~E#E*7o2GE9B{?bFW(T4fW6^G20wUfy*3Z#(c0(?hetF$|sPDA^m5)f(t41YKb#?W# zT?<@fExTa4VBUuQs$$=Yq|&8NjGarf%VU|6PrAN}PS0J7SE;$1Q7&8%Uq89lB zZ1MeqSzh^0by!og0AHtmY4s+ovkGw1=J|Bmw5-ln49hZl>xo4|_hrDf8;W#(5C+PY z9vUCTSLt7#4giJpd(|>3zVO^q*gCTUK2i}sQ^&rT*;2&oQa`QPZoy2y)%V`Hq|)-z zr8INH51kHAAJmReQtEYmoix4pc5Xd`X0y2REbcFg|Oyhf?v(;$zU38MuRkcI$whN1+_q4T;0g25J9oxMrBpdl zsH$Y+E>Ugq+Cq39`6L1LX;+Lc_sEe`-rsS)iL{HrVlnvyx{ngQ;!WfeS9WlJ~c7$ZOm- z`g+=oW)DS^TC1COGHXl%?O$@5&Ggh%_8(J z!?Z_4eT{s^DT{tt5?D1*e z-VSV{O;e+_uuhQVi=0(Qjo*g=eeGu685YZ8?=Ub&8gahHy;-n;GVj(dq1s-z2WwI>K$KFJ%X+Yro{Kn;x8X#pxIRLqSD6vKY zFV0nR$he3<JT>16G+t_Y&{Z zV}$fW(_j@aY{NAjkbb@53&)*_!jFgE@2)WIgfRJ(${h=AGG3#3eY}6htk8>F?dcw0 zWPT2OFjewwCb6u&h!NxDscf?WsTMwM&-tcNHC~IDL`8=8_ri?xSLzI^V`bd+AwkcKoCBEsPDLF$I$Nol zsNH0L?^Ry{N;C?wU8c+;H}J3kq9 zGlH>beUU7ylD3Ql7-#t^N4yb@@pR`tAGf$v8f2jmM0 zoA`*5iJr`3`<`-RJZ-;K0Y3>AUu zI;ui$Pz2lohTe}Nmc&RS8#!pMuWysZjr4ftJeG*~m}QvlCd{p*=HE?4cjT~I^!C=| zh0Dd+IT>UurnAdc(pxF=!=-13uCMjZMauMeNBS{lL{{>*p;i;*BPLP?>f$?EZr%L~ zajA$E^*Tp&%e9uZruMc-q=?wl6h|zOL~n|@<;*ShFP=AUxwd31b905L6*q(`Z}5nm zN#F6Xh#bd2%j0T$s!6O<__Q!as)39UM(e3t`L`8?r2Sm7Iqv!)aK-?`$l3CzdUS}q zNPjz5qM^CarAwbI6=_Q^=pY9avxiZZae=g+F$<0_-prYc)h|Y^-0k3M9XAhd$eo?)6-C@DrGQ71mMdkf`O!FD+7Aw&)x17U?FE0u<+mmh`J!FPpCdh2&h@8W zf(yntdYKyJ##=NZhfEc2mn{~S_;`A-Q_1a5-N`$G(Q&}PBh&c# z`z`&5Qy)bACITQtZ+|6ieDPoe`1gSHH|xK@$bLkX2_flUDF|OYsOKL6>i^{&;Q;t6 z4daUkmHrA(|2Z(fP=Etnq{9#8K`pd<%aY zdHq_+Ki1OkoCJZ#|CJJs{m$wiTrLM0i(k0+H2DA2z~4Iv{KXH{4{N_4&iESj`?2mI^7m(&u<8CvBluPJ*DY92ljI0Nj2i#9g+Ei?J4N{Q Ee~$&2H2?qr literal 0 HcmV?d00001 diff --git a/ext/zip/tests/bug7658.phpt b/ext/zip/tests/bug7658.phpt new file mode 100644 index 0000000000..73096f2da4 --- /dev/null +++ b/ext/zip/tests/bug7658.phpt @@ -0,0 +1,60 @@ +--TEST-- +bug #7658, modify archive with general bit flag 3 set +--SKIPIF-- + +--FILE-- +open($file)) { + echo 'failed'; +} + +dump_entries_name($zip); + +$zip->deleteName('content.xml'); +$zip->addFile($dirname . "bug7658.xml","content.xml"); +$zip->close(); +echo "\n"; +$zip->open($file); +dump_entries_name($zip); +@unlink($file); +?> +--EXPECT-- +0 mimetype +1 Configurations2/statusbar/ +2 Configurations2/accelerator/current.xml +3 Configurations2/floater/ +4 Configurations2/popupmenu/ +5 Configurations2/progressbar/ +6 Configurations2/menubar/ +7 Configurations2/toolbar/ +8 Configurations2/images/Bitmaps/ +9 content.xml +10 styles.xml +11 meta.xml +12 Thumbnails/thumbnail.png +13 settings.xml +14 META-INF/manifest.xml + +0 mimetype +1 Configurations2/statusbar/ +2 Configurations2/accelerator/current.xml +3 Configurations2/floater/ +4 Configurations2/popupmenu/ +5 Configurations2/progressbar/ +6 Configurations2/menubar/ +7 Configurations2/toolbar/ +8 Configurations2/images/Bitmaps/ +9 styles.xml +10 meta.xml +11 Thumbnails/thumbnail.png +12 settings.xml +13 META-INF/manifest.xml +14 content.xml diff --git a/ext/zip/tests/bug7658.xml b/ext/zip/tests/bug7658.xml new file mode 100644 index 0000000000..98076f1984 --- /dev/null +++ b/ext/zip/tests/bug7658.xml @@ -0,0 +1,2 @@ + +Other text Silvio Berlusconi, le ricordo che ha in prestito i seguenti libriTitoloInventarioCodice Da Vinci112345678Lo Zen e il tiro con l'arco1020304Lo Zen e l'arte della manutenzione della motocicletta1020305101 Storie Zen1020306Antani di Blinda come fosse di Cappotto4112345peraltro, sottolineiamo la perentorietà della restituzione anche dei vieppiù interessanti testi:TitoloAutoreInventarioAngeli e DemoniDan Brown12131415La versione di BarneyMordecai Richler2010322Manuale PHPVarii32413543La prematurata supercazzola negli anni a venireUgo Tognazzi31213243La sbiriguda in vicesindacoUgo Tognazzi1324354654Gentili saluti, la sua biblioteca diff --git a/ext/zip/tests/bug8009.phpt b/ext/zip/tests/bug8009.phpt new file mode 100644 index 0000000000..90ab765bfa --- /dev/null +++ b/ext/zip/tests/bug8009.phpt @@ -0,0 +1,27 @@ +--TEST-- +bug #8009, cannot add again same entry to an archive +--SKIPIF-- + +--FILE-- +open($filename)) { + exit("cannot open $filename\n"); +} +$zip->addFromString("2.txt", "=)"); +$zip->close(); +unlink($filename); +echo "status: " . $zip->status . "\n"; +echo "\n"; + +--EXPECT-- +status: 0 diff --git a/ext/zip/tests/bug8009.zip b/ext/zip/tests/bug8009.zip new file mode 100644 index 0000000000000000000000000000000000000000..45bedcbe8891cf667083f24d4ae4879d62765b88 GIT binary patch literal 112 zcmWIWW@Zs#U|`^2sNlYBqI;-0lLg3Q0%BGmHq +--FILE-- +open($file)) { + exit('failed'); +} +if (!$zip->addFile($dirname . 'utils.php', 'test.php')) { + echo "failed\n"; +} +if ($zip->status == ZIPARCHIVE::ER_OK) { + dump_entries_name($zip); + $zip->close(); +} else { + echo "failed\n"; +} +@unlink($file); +?> +--EXPECTF-- +0 bar +1 foobar/ +2 foobar/baz +3 entry1.txt +4 test.php diff --git a/ext/zip/tests/oo_close.phpt b/ext/zip/tests/oo_close.phpt new file mode 100644 index 0000000000..ea67dcddfc --- /dev/null +++ b/ext/zip/tests/oo_close.phpt @@ -0,0 +1,25 @@ +--TEST-- +zip::close() function +--SKIPIF-- + +--FILE-- +open($dirname . 'test.zip')) { + exit('failed'); +} + +if ($zip->status == ZIPARCHIVE::ER_OK) { + $zip->close(); + echo "ok\n"; +} else { + echo "failed\n"; +} +?> +--EXPECTF-- +ok diff --git a/ext/zip/tests/oo_delete.phpt b/ext/zip/tests/oo_delete.phpt new file mode 100644 index 0000000000..176cda44dc --- /dev/null +++ b/ext/zip/tests/oo_delete.phpt @@ -0,0 +1,76 @@ +--TEST-- +Delete entries +--SKIPIF-- + +--FILE-- +open($file, ZIPARCHIVE::CREATE)) { + exit('failed'); +} +$zip->addFromString('entry1.txt', 'entry #1'); +$zip->addFromString('entry2.txt', 'entry #2'); +$zip->addFromString('dir/entry2.txt', 'entry #2'); + +if ($zip->status == ZIPARCHIVE::ER_OK) { + $zip->close(); + echo "ok\n"; +} else { + var_dump($zip); + echo "failed\n"; +} + +if (!$zip->open($file, ZIPARCHIVE::CREATE)) { + exit('failed'); +} + +if ($zip->deleteIndex(0)) { + echo "ok\n"; +} + +if ($zip->deleteName('entry2.txt')) { + echo "ok\n"; +} else { + echo "failed 3\n"; +} + +if ($zip->deleteName('dir/entry2.txt')) { + echo "ok\n"; +} else { + echo "failed 3\n"; +} + +if (!$zip->deleteIndex(123)) { + echo "ok\n"; +} else { + print_r($zip); + echo "failed\n"; +} + + +$sb = $zip->statIndex(0); +var_dump($sb); +$sb = $zip->statIndex(1); +var_dump($sb); +$sb = $zip->statIndex(2); +var_dump($sb); +@unlink($file); +?> +--EXPECTF-- +ok +ok +ok +ok +ok +bool(false) +bool(false) +bool(false) diff --git a/ext/zip/tests/oo_ext_zip.phpt b/ext/zip/tests/oo_ext_zip.phpt new file mode 100644 index 0000000000..739a671e0b --- /dev/null +++ b/ext/zip/tests/oo_ext_zip.phpt @@ -0,0 +1,27 @@ +--TEST-- +Extending Zip class and array property +--SKIPIF-- + +--FILE-- +testarray[] = 1; + var_dump($this->testarray); + } +} + +$z = new myZip; +?> +--EXPECTF-- +array(1) { + [0]=> + int(1) +} diff --git a/ext/zip/tests/oo_namelocate.phpt b/ext/zip/tests/oo_namelocate.phpt new file mode 100644 index 0000000000..ccfa3a9d8d --- /dev/null +++ b/ext/zip/tests/oo_namelocate.phpt @@ -0,0 +1,45 @@ +--TEST-- +Locate entries by name +--SKIPIF-- + +--FILE-- +open($file, ZIPARCHIVE::CREATE)) { + exit('failed'); +} + +$zip->addFromString('entry1.txt', 'entry #1'); +$zip->addFromString('entry2.txt', 'entry #2'); +$zip->addFromString('dir/entry2d.txt', 'entry #2'); + +if (!$zip->status == ZIPARCHIVE::ER_OK) { + echo "failed to write zip\n"; +} +$zip->close(); + +if (!$zip->open($file)) { + exit('failed'); +} + +echo $zip->locateName('entry1.txt') . "\n"; +echo $zip->locateName('eNtry2.txt') . "\n"; +echo $zip->locateName('eNtry2.txt', ZIPARCHIVE::FL_NOCASE) . "\n"; +echo $zip->locateName('enTRy2d.txt', ZIPARCHIVE::FL_NOCASE|ZIPARCHIVE::FL_NODIR) . "\n"; +$zip->close(); + +?> +--EXPECTF-- +0 +-1 +1 +2 diff --git a/ext/zip/tests/oo_open.phpt b/ext/zip/tests/oo_open.phpt new file mode 100644 index 0000000000..0760db34c4 --- /dev/null +++ b/ext/zip/tests/oo_open.phpt @@ -0,0 +1,46 @@ +--TEST-- +zip::open() function +--SKIPIF-- + +--FILE-- +open($dirname . 'nofile'); +if ($r !== TRUE) { + echo "ER_OPEN: ok\n"; +} else { + echo "ER_OPEN: FAILED\n"; +} + +$r = $zip->open($dirname . 'nofile', ZIPARCHIVE::CREATE); +if (!$r) { + echo "create: failed\n"; +} else { + echo "create: ok\n"; +} +@unlink($dirname . 'nofile'); + +$zip = new ZipArchive; +$zip->open(''); + +if (!$zip->open($dirname . 'test.zip')) { + exit("failed 1\n"); +} + +if ($zip->status == ZIPARCHIVE::ER_OK) { + echo "OK\n"; +} else { + echo "failed\n"; +} +?> +--EXPECTF-- +ER_OPEN: ok +create: ok + +Warning: ZipArchive::open(): Empty string as source in %s on line %d +OK diff --git a/ext/zip/tests/oo_rename.phpt b/ext/zip/tests/oo_rename.phpt new file mode 100644 index 0000000000..37339a3bf5 --- /dev/null +++ b/ext/zip/tests/oo_rename.phpt @@ -0,0 +1,58 @@ +--TEST-- +Rename entries +--SKIPIF-- + +--FILE-- +open($file, ZIPARCHIVE::CREATE)) { + exit('failed'); +} + +$zip->addFromString('entry1.txt', 'entry #1'); +$zip->addFromString('entry2.txt', 'entry #2'); +$zip->addFromString('dir/entry2.txt', 'entry #2'); + +if (!$zip->status == ZIPARCHIVE::ER_OK) { + var_dump($zip); + echo "failed\n"; +} + +$zip->close(); + +if (!$zip->open($file)) { + exit('failed'); +} + +dump_entries_name($zip); +echo "\n"; + +if (!$zip->renameIndex(0, 'ren_entry1.txt')) { + echo "failed index 0\n"; +} + +if (!$zip->renameName('dir/entry2.txt', 'dir3/ren_entry2.txt')) { + echo "failed name dir/entry2.txt\n"; +} +dump_entries_name($zip); +$zip->close(); + +@unlink($file); +?> +--EXPECTF-- +0 entry1.txt +1 entry2.txt +2 dir/entry2.txt + +0 ren_entry1.txt +1 entry2.txt +2 dir3/ren_entry2.txt diff --git a/ext/zip/tests/test.zip b/ext/zip/tests/test.zip new file mode 100644 index 0000000000000000000000000000000000000000..35bd5eecdf696886bd91e941c98df25c931c93b4 GIT binary patch literal 526 zcmWIWW@h1H0D+1|7UP$j7qPGa*&xi!Aj*)GSQHw<$-u0l^vTH&h(jw_7``y3fW)~1 z;5z$&IzcAD06S1;T7EuIkv>?r4^U?ll5UVr5ujoY29VAOyQ8uaK%O)Zb0KslC04;T zBU|l#%G2kRX9&Zjm9AW_ot_E1eteiWM@x?ZVd%nz&rEKo@O%Mzivw&Z14C+FNl~St zUP(oX_gQVtRUrXpEI=KMOybPAJkJZ%$-uw}#9JCcEO1b;LV_Z+f&oQ0W&l8RGcX7+ z{B^Vf>V^jg&;(F$AWXm&5XknIAWQ%U3z{Je3=l)Gc^u?)2sjC(5Wx`O&B_K6X92>$ KK>9a`!vFyFl4z~~ literal 0 HcmV?d00001 diff --git a/ext/zip/tests/test_procedural.zip b/ext/zip/tests/test_procedural.zip new file mode 100644 index 0000000000000000000000000000000000000000..6b986948031d904753e54b3739462aa99b8932c9 GIT binary patch literal 541 zcmWIWW@h1H0D-bb7GoXxC9hb4Y!GH<5M@Zq&kqgZWMGz8`sCyX#Gw@|3||;iK;m3n z0dTDqK&>w~FJfTvcTA5VPFROl?CKq1^`TmW~2ZB literal 0 HcmV?d00001 diff --git a/ext/zip/tests/utils.php b/ext/zip/tests/utils.php new file mode 100644 index 0000000000..41a2791266 --- /dev/null +++ b/ext/zip/tests/utils.php @@ -0,0 +1,8 @@ +numFiles; $i++) { + $sb = $z->statIndex($i); + echo $i . ' ' . $sb['name'] . "\n"; + } +} diff --git a/ext/zip/tests/zip_close.phpt b/ext/zip/tests/zip_close.phpt new file mode 100644 index 0000000000..7f9d09ae36 --- /dev/null +++ b/ext/zip/tests/zip_close.phpt @@ -0,0 +1,17 @@ +--TEST-- +zip_close() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +OK diff --git a/ext/zip/tests/zip_entry_compressedsize.phpt b/ext/zip/tests/zip_entry_compressedsize.phpt new file mode 100644 index 0000000000..fefa6e5977 --- /dev/null +++ b/ext/zip/tests/zip_entry_compressedsize.phpt @@ -0,0 +1,23 @@ +--TEST-- +zip_entry_compressedsize() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +5 +4 +0 +24 diff --git a/ext/zip/tests/zip_entry_compressionmethod.phpt b/ext/zip/tests/zip_entry_compressionmethod.phpt new file mode 100644 index 0000000000..cabdbb797f --- /dev/null +++ b/ext/zip/tests/zip_entry_compressionmethod.phpt @@ -0,0 +1,24 @@ +--TEST-- +zip_entry_compressionmethod() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +stored +stored +stored +deflated + diff --git a/ext/zip/tests/zip_entry_filesize.phpt b/ext/zip/tests/zip_entry_filesize.phpt new file mode 100644 index 0000000000..b8d8820b64 --- /dev/null +++ b/ext/zip/tests/zip_entry_filesize.phpt @@ -0,0 +1,23 @@ +--TEST-- +zip_entry_filesize() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +5 +4 +0 +27 diff --git a/ext/zip/tests/zip_entry_name.phpt b/ext/zip/tests/zip_entry_name.phpt new file mode 100644 index 0000000000..1916e25dac --- /dev/null +++ b/ext/zip/tests/zip_entry_name.phpt @@ -0,0 +1,23 @@ +--TEST-- +zip_entry_name() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +foo +bar +foobar/ +foobar/baz diff --git a/ext/zip/tests/zip_entry_open.phpt b/ext/zip/tests/zip_entry_open.phpt new file mode 100644 index 0000000000..b79e39108f --- /dev/null +++ b/ext/zip/tests/zip_entry_open.phpt @@ -0,0 +1,17 @@ +--TEST-- +zip_entry_open() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +OK diff --git a/ext/zip/tests/zip_entry_read.phpt b/ext/zip/tests/zip_entry_read.phpt new file mode 100644 index 0000000000..94dde59cbd --- /dev/null +++ b/ext/zip/tests/zip_entry_read.phpt @@ -0,0 +1,18 @@ +--TEST-- +zip_entry_read() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +foo diff --git a/ext/zip/tests/zip_open.phpt b/ext/zip/tests/zip_open.phpt new file mode 100644 index 0000000000..91474bc6fd --- /dev/null +++ b/ext/zip/tests/zip_open.phpt @@ -0,0 +1,16 @@ +--TEST-- +zip_open() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +OK diff --git a/ext/zip/tests/zip_read.phpt b/ext/zip/tests/zip_read.phpt new file mode 100644 index 0000000000..5cadb2d1ef --- /dev/null +++ b/ext/zip/tests/zip_read.phpt @@ -0,0 +1,21 @@ +--TEST-- +zip_read() function +--SKIPIF-- + +--FILE-- + +--EXPECT-- +4 entries diff --git a/ext/zip/zip_stream.c b/ext/zip/zip_stream.c new file mode 100644 index 0000000000..021330b3d9 --- /dev/null +++ b/ext/zip/zip_stream.c @@ -0,0 +1,239 @@ +/* $Id$ */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include "php.h" +#if HAVE_ZIP +#ifdef ZEND_ENGINE_2 + +#include "lib/zip.h" + +#include "php_streams.h" +#include "ext/standard/file.h" +#include "fopen_wrappers.h" + +#include "ext/standard/url.h" + +struct php_zip_stream_data_t { + struct zip *za; + struct zip_file *zf; + size_t cursor; + php_stream *stream; +}; + +#define STREAM_DATA_FROM_STREAM() \ + struct php_zip_stream_data_t *self = (struct php_zip_stream_data_t *) stream->abstract; + + +/* {{{ php_zip_ops_read */ +static size_t php_zip_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) +{ + int n = 0; + STREAM_DATA_FROM_STREAM(); + + if (self->za && self->zf) { + n = (size_t)zip_fread(self->zf, buf, (int)count); + + if (n == 0) { + stream->eof = 1; + } else { + self->cursor += n; + } + } + return n<1 ? 0 : n; +} +/* }}} */ + +/* {{{ php_zip_ops_write */ +static size_t php_zip_ops_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) +{ + STREAM_DATA_FROM_STREAM(); + + if (!stream) { + return 0; + } + + return count; +} +/* }}} */ + +/* {{{ php_zip_ops_close */ +static int php_zip_ops_close(php_stream *stream, int close_handle TSRMLS_DC) +{ + STREAM_DATA_FROM_STREAM(); + if (close_handle && self->za) { + zip_close(self->za); + self->za = NULL; + } + efree(self); + stream->abstract = NULL; + return EOF; +} +/* }}} */ + +/* {{{ php_zip_ops_flush */ +static int php_zip_ops_flush(php_stream *stream TSRMLS_DC) +{ + + STREAM_DATA_FROM_STREAM(); + + if (!stream) { + return 0; + } + + return 0; +} +/* }}} */ + +php_stream_ops php_stream_zipio_ops = { + php_zip_ops_write, php_zip_ops_read, + php_zip_ops_close, php_zip_ops_flush, + "zip", + NULL, /* seek */ + NULL, /* cast */ + NULL, /* stat */ + NULL /* set_option */ +}; + +/* {{{ php_stream_zip_open */ +php_stream *php_stream_zip_open(char *filename, char *path, char *mode STREAMS_DC TSRMLS_DC) +{ + struct zip_file *zf = NULL; + int err = 0; + + php_stream *stream = NULL; + struct php_zip_stream_data_t *self; + struct zip *stream_za; + + if (strncmp(mode,"r", strlen("r")) != 0) { + return NULL; + } + + if (filename) { + /* duplicate to make the stream za independent (esp. for MSHUTDOWN) */ + stream_za = zip_open(filename, ZIP_CREATE, &err); + if (!stream_za) { + return NULL; + } + + zf = zip_fopen(stream_za, path, 0); + if (zf) { + self = emalloc(sizeof(*self)); + + self->za = stream_za; + self->zf = zf; + self->stream = NULL; + self->cursor = 0; + stream = php_stream_alloc(&php_stream_zipio_ops, self, NULL, mode); + } else { + zip_close(stream_za); + } + } + + if (!stream) { + return NULL; + } else { + return stream; + } + +} +/* }}} */ + +/* {{{ php_stream_zip_opener */ +php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper, + char *path, + char *mode, + int options, + char **opened_path, + php_stream_context *context STREAMS_DC TSRMLS_DC) +{ + int path_len; + + char *file_basename; + size_t file_basename_len; + char file_dirname[MAXPATHLEN+1]; + + struct zip *za; + struct zip_file *zf = NULL; + char *fragment; + int fragment_len; + int err; + + php_stream *stream = NULL; + struct php_zip_stream_data_t *self; + + fragment = strchr(path, '#'); + if (!fragment) { + return NULL; + } + + if (strncasecmp("zip://", path, 6) == 0) { + path += 6; + } + + fragment_len = strlen(fragment); + + if (fragment_len < 1) { + return NULL; + } + path_len = strlen(path); + + memcpy(file_dirname, path, path_len - fragment_len); + file_dirname[path_len - fragment_len] = '\0'; + + php_basename(path, path_len - fragment_len, NULL, 0, &file_basename, &file_basename_len TSRMLS_CC); + fragment++; + if (mode[0] != 'r') { + return NULL; + } + + za = zip_open(file_dirname, ZIP_CREATE, &err); + if (za) { + zf = zip_fopen(za, fragment, 0); + if (zf) { + self = emalloc(sizeof(*self)); + + self->za = za; + self->zf = zf; + self->stream = NULL; + self->cursor = 0; + stream = php_stream_alloc(&php_stream_zipio_ops, self, NULL, mode); + + if (opened_path) { + *opened_path = estrdup(path); + } + } else { + zip_close(za); + } + } + + efree(file_basename); + + if (!stream) { + return NULL; + } else { + return stream; + } +} +/* }}} */ + +static php_stream_wrapper_ops zip_stream_wops = { + php_stream_zip_opener, + NULL, /* close */ + NULL, /* fstat */ + NULL, /* stat */ + NULL, /* opendir */ + "zip wrapper", + NULL, /* unlink */ + NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ +}; + +php_stream_wrapper php_stream_zip_wrapper = { + &zip_stream_wops, + NULL, + 0 /* is_url */ +}; +#endif /* ZEND_ENGINE_2 */ +#endif /* HAVE_ZIP */ -- 2.40.0