From ea5e9b319023b0e0aeab53e400a846a9ce4d3c27 Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Mon, 22 Jan 2007 00:17:14 +0000 Subject: [PATCH] - Add Phar::getStub(), step 1 --- ext/phar/TODO | 1 + ext/phar/phar_object.c | 51 ++++++++++++++++++++++++----- ext/phar/tests/phar_stub_write.phpt | 47 ++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 9 deletions(-) create mode 100755 ext/phar/tests/phar_stub_write.phpt diff --git a/ext/phar/TODO b/ext/phar/TODO index a35e195a36..e24ce132bb 100644 --- a/ext/phar/TODO +++ b/ext/phar/TODO @@ -15,6 +15,7 @@ Version 1.0.0 X stream context for specifying compression of a file [Marcus] * stream context for specifying meta-data X Phar->setStub() for specifying a new stub to the phar [Greg] + X Phar->getStub() for retrieving the stub of the phar [Marcus] X add setUncompressed(), setCompressedGZ() and setCompressedBZ2() to PharFileInfo class [Greg] * add uncompressAllFiles(), compressAllFilesGZ() and compressAllFilesBZ2() diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index bfde05a3db..3750ed8663 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -373,7 +373,7 @@ PHP_METHOD(Phar, offsetUnset) /* }}} */ /* {{{ proto int Phar::setStub(string|stream stub [, int len]) - * set the pre-phar stub for the current writeable phar + * Set the pre-phar stub for the current writeable phar */ PHP_METHOD(Phar, setStub) { @@ -382,6 +382,7 @@ PHP_METHOD(Phar, setStub) long len = -1; php_stream *stream; PHAR_ARCHIVE_OBJECT(); + if (PHAR_G(readonly)) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Cannot set stub, phar is read-only"); @@ -413,6 +414,37 @@ PHP_METHOD(Phar, setStub) } /* }}} */ +/* {{{ proto string Phar::getStub() + * Get the pre-phar stub + */ +PHP_METHOD(Phar, getStub) +{ + char *buf; + int len; + PHAR_ARCHIVE_OBJECT(); + + len = phar_obj->arc.archive->halt_offset; + + if (!phar_obj->arc.archive->fp) { + zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, + "Unable to read stub"); + return; + } + + buf = emalloc(len+1); + php_stream_rewind(phar_obj->arc.archive->fp); + if (len != php_stream_read(phar_obj->arc.archive->fp, buf, len)) { + zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, + "Unable to read stub"); + efree(buf); + return; + } + buf[len] = '\0'; + + RETURN_STRINGL(buf, len, 0); +} +/* }}}*/ + /* {{{ proto void PharFileInfo::__construct(string entry) * Construct a Phar entry object */ @@ -817,23 +849,24 @@ zend_function_entry php_archive_methods[] = { #if !HAVE_SPL PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PRIVATE) #else - PHP_ME(Phar, __construct, arginfo_phar___construct, 0) - PHP_ME(Phar, count, NULL, 0) - PHP_ME(Phar, getVersion, NULL, 0) - PHP_ME(Phar, getSignature, NULL, 0) - PHP_ME(Phar, getModified, NULL, 0) - PHP_ME(Phar, setStub, arginfo_phar_setStub, ZEND_ACC_PUBLIC) + PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PUBLIC) + PHP_ME(Phar, count, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phar, getModified, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phar, getSignature, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phar, getStub, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phar, getVersion, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phar, offsetExists, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetGet, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetSet, arginfo_phar_offsetSet, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetUnset, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) - PHP_ME(Phar, offsetExists, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) + PHP_ME(Phar, setStub, arginfo_phar_setStub, ZEND_ACC_PUBLIC) #endif /* static member functions */ PHP_ME(Phar, apiVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Phar, canCompress, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Phar, canWrite, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) - PHP_ME(Phar, mapPhar, arginfo_phar_mapPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Phar, loadPhar, arginfo_phar_loadPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) + PHP_ME(Phar, mapPhar, arginfo_phar_mapPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) {NULL, NULL, NULL} }; diff --git a/ext/phar/tests/phar_stub_write.phpt b/ext/phar/tests/phar_stub_write.phpt new file mode 100755 index 0000000000..031ec681e2 --- /dev/null +++ b/ext/phar/tests/phar_stub_write.phpt @@ -0,0 +1,47 @@ +--TEST-- +Phar stub read +--SKIPIF-- + +--INI-- +phar.require_hash=0 +--FILE-- +'; +$file = $stub; + +$files = array(); +$files['a'] = 'a'; +$files['b'] = 'b'; +$files['c'] = 'c'; + +include 'phar_test.inc'; + +$phar = new Phar($fname); +var_dump($stub); +var_dump($phar->getStub()); +var_dump($phar->getStub() == $stub); + +$stub = ''; +$phar->setStub($stub); +var_dump($phar->getStub() == $stub); + +$phar = new Phar($fname); +var_dump($phar->getStub() == $stub); + +?> +===DONE=== +--CLEAN-- + +--EXPECT-- +string(48) "" +string(48) "" +bool(true) +bool(true) +bool(true) +===DONE=== -- 2.40.0