-
Notifications
You must be signed in to change notification settings - Fork 8k
Wrap strings passed to libzip with zip_source_function_create() #21659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tstarling
wants to merge
1
commit into
php:master
Choose a base branch
from
tstarling:zip_source_function_create
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright (c) 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: | | ||
| | https://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: Tim Starling <tstarling@wikimedia.org> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #ifdef HAVE_CONFIG_H | ||
| # include "config.h" | ||
| #endif | ||
| #include "php.h" | ||
| #include "php_zip.h" | ||
|
|
||
| typedef struct _php_zip_string_source { | ||
| /* The current string being read from */ | ||
| zend_string *in_str; | ||
| /* The offset into in_str of the current read position */ | ||
| size_t in_offset; | ||
| /* The modification time returned in stat calls */ | ||
| time_t mtime; | ||
| /* The current string being written to */ | ||
| zend_string *out_str; | ||
| /* The offset into out_str of the current write position */ | ||
| size_t out_offset; | ||
| /* A place to copy the result to when the archive is closed, or NULL */ | ||
| zend_string **dest; | ||
| /* The error to be returned when libzip asks for the last error code */ | ||
| zip_error_t error; | ||
| } php_zip_string_source; | ||
|
|
||
| /* The source callback function, see https://libzip.org/documentation/zip_source_function.html | ||
| * This is similar to read_data() in libzip's zip_source_buffer.c */ | ||
| static zip_int64_t php_zip_string_cb(void *userdata, void *data, zip_uint64_t len, zip_source_cmd_t cmd) | ||
DanielEScherzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| php_zip_string_source *ctx = userdata; | ||
| switch (cmd) { | ||
| case ZIP_SOURCE_SUPPORTS: | ||
| return zip_source_make_command_bitmap( | ||
| ZIP_SOURCE_FREE, | ||
| #if LIBZIP_VERSION_MAJOR > 1 || LIBZIP_VERSION_MINOR >= 10 | ||
| ZIP_SOURCE_SUPPORTS_REOPEN, | ||
| #endif | ||
| ZIP_SOURCE_OPEN, | ||
| ZIP_SOURCE_READ, | ||
| ZIP_SOURCE_CLOSE, | ||
| ZIP_SOURCE_STAT, | ||
| ZIP_SOURCE_ERROR, | ||
| ZIP_SOURCE_SEEK, | ||
| ZIP_SOURCE_TELL, | ||
| ZIP_SOURCE_BEGIN_WRITE, | ||
| ZIP_SOURCE_WRITE, | ||
| ZIP_SOURCE_COMMIT_WRITE, | ||
| ZIP_SOURCE_ROLLBACK_WRITE, | ||
| ZIP_SOURCE_SEEK_WRITE, | ||
| ZIP_SOURCE_TELL_WRITE, | ||
| ZIP_SOURCE_REMOVE, | ||
| -1 | ||
| ); | ||
|
|
||
| case ZIP_SOURCE_FREE: | ||
| zend_string_release(ctx->out_str); | ||
| zend_string_release(ctx->in_str); | ||
| efree(ctx); | ||
| return 0; | ||
|
|
||
| /* Read ops */ | ||
|
|
||
| case ZIP_SOURCE_OPEN: | ||
| ctx->in_offset = 0; | ||
| return 0; | ||
|
|
||
| case ZIP_SOURCE_READ: { | ||
| size_t remaining = ZSTR_LEN(ctx->in_str) - ctx->in_offset; | ||
| len = MIN(len, remaining); | ||
| if (len) { | ||
| memcpy(data, ZSTR_VAL(ctx->in_str) + ctx->in_offset, len); | ||
| ctx->in_offset += len; | ||
| } | ||
| return len; | ||
| } | ||
|
|
||
| case ZIP_SOURCE_CLOSE: | ||
| return 0; | ||
|
|
||
| case ZIP_SOURCE_STAT: { | ||
| zip_stat_t *st; | ||
| if (len < sizeof(*st)) { | ||
| zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); | ||
| return -1; | ||
| } | ||
|
|
||
| st = (zip_stat_t *)data; | ||
| zip_stat_init(st); | ||
| st->mtime = ctx->mtime; | ||
| st->size = ZSTR_LEN(ctx->in_str); | ||
| st->comp_size = st->size; | ||
| st->comp_method = ZIP_CM_STORE; | ||
| st->encryption_method = ZIP_EM_NONE; | ||
| st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; | ||
|
|
||
| return sizeof(*st); | ||
| } | ||
|
|
||
| case ZIP_SOURCE_ERROR: | ||
| return zip_error_to_data(&ctx->error, data, len); | ||
|
|
||
| /* Seekable read ops */ | ||
|
|
||
| case ZIP_SOURCE_SEEK: { | ||
| zip_int64_t new_offset = zip_source_seek_compute_offset( | ||
| ctx->in_offset, ZSTR_LEN(ctx->in_str), data, len, &ctx->error); | ||
| if (new_offset < 0) { | ||
| return -1; | ||
| } | ||
| ctx->in_offset = (size_t)new_offset; | ||
| return 0; | ||
| } | ||
|
|
||
| case ZIP_SOURCE_TELL: | ||
| if (ctx->in_offset > ZIP_INT64_MAX) { | ||
| zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); | ||
| return -1; | ||
| } | ||
| return (zip_int64_t)ctx->in_offset; | ||
|
|
||
| /* Write ops */ | ||
|
|
||
| case ZIP_SOURCE_BEGIN_WRITE: | ||
| zend_string_release(ctx->out_str); | ||
| ctx->out_str = ZSTR_EMPTY_ALLOC(); | ||
| return 0; | ||
|
|
||
| case ZIP_SOURCE_WRITE: | ||
| if (ctx->out_offset > SIZE_MAX - len) { | ||
| zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); | ||
| return -1; | ||
| } | ||
| if (ctx->out_offset + len > ZSTR_LEN(ctx->out_str)) { | ||
| ctx->out_str = zend_string_realloc(ctx->out_str, ctx->out_offset + len, false); | ||
| } | ||
| memcpy(ZSTR_VAL(ctx->out_str) + ctx->out_offset, data, len); | ||
| ctx->out_offset += len; | ||
| return len; | ||
|
|
||
| case ZIP_SOURCE_COMMIT_WRITE: | ||
| ZSTR_VAL(ctx->out_str)[ZSTR_LEN(ctx->out_str)] = '\0'; | ||
| zend_string_release(ctx->in_str); | ||
| ctx->in_str = ctx->out_str; | ||
| ctx->out_str = ZSTR_EMPTY_ALLOC(); | ||
| if (ctx->dest) { | ||
| *(ctx->dest) = zend_string_copy(ctx->in_str); | ||
| } | ||
| return 0; | ||
|
|
||
| case ZIP_SOURCE_ROLLBACK_WRITE: | ||
| zend_string_release(ctx->out_str); | ||
| ctx->out_str = ZSTR_EMPTY_ALLOC(); | ||
| return 0; | ||
|
|
||
| case ZIP_SOURCE_SEEK_WRITE: { | ||
| zip_int64_t new_offset = zip_source_seek_compute_offset( | ||
| ctx->out_offset, ZSTR_LEN(ctx->out_str), data, len, &ctx->error); | ||
| if (new_offset < 0) { | ||
| return -1; | ||
| } | ||
| ctx->out_offset = new_offset; | ||
| return 0; | ||
| } | ||
|
|
||
| case ZIP_SOURCE_TELL_WRITE: | ||
| if (ctx->out_offset > ZIP_INT64_MAX) { | ||
| zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); | ||
| return -1; | ||
| } | ||
| return (zip_int64_t)ctx->out_offset; | ||
|
|
||
| case ZIP_SOURCE_REMOVE: | ||
| zend_string_release(ctx->in_str); | ||
| ctx->in_str = ZSTR_EMPTY_ALLOC(); | ||
| ctx->in_offset = 0; | ||
| return 0; | ||
|
|
||
| default: | ||
| zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| zip_source_t * php_zip_create_string_source(zend_string *str, zend_string **dest, zip_error_t *err) | ||
| { | ||
| php_zip_string_source *ctx = ecalloc(1, sizeof(php_zip_string_source)); | ||
| ctx->in_str = zend_string_copy(str); | ||
| ctx->out_str = ZSTR_EMPTY_ALLOC(); | ||
| ctx->dest = dest; | ||
| ctx->mtime = time(NULL); | ||
| return zip_source_function_create(php_zip_string_cb, (void*)ctx, err); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.