|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Copyright (c) The PHP Group | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to version 3.01 of the PHP license, | |
| 6 | + | that is bundled with this package in the file LICENSE, and is | |
| 7 | + | available through the world-wide-web at the following url: | |
| 8 | + | https://www.php.net/license/3_01.txt | |
| 9 | + | If you did not receive a copy of the PHP license and are unable to | |
| 10 | + | obtain it through the world-wide-web, please send a note to | |
| 11 | + | license@php.net so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | Author: Tim Starling <tstarling@wikimedia.org> | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#ifdef HAVE_CONFIG_H |
| 18 | +# include "config.h" |
| 19 | +#endif |
| 20 | +#include "php.h" |
| 21 | +#include "php_zip.h" |
| 22 | + |
| 23 | +typedef struct _php_zip_string_source { |
| 24 | + /* The current string being read from */ |
| 25 | + zend_string *in_str; |
| 26 | + /* The offset into in_str of the current read position */ |
| 27 | + size_t in_offset; |
| 28 | + /* The modification time returned in stat calls */ |
| 29 | + time_t mtime; |
| 30 | + /* The current string being written to */ |
| 31 | + zend_string *out_str; |
| 32 | + /* The offset into out_str of the current write position */ |
| 33 | + size_t out_offset; |
| 34 | + /* A place to copy the result to when the archive is closed, or NULL */ |
| 35 | + zend_string **dest; |
| 36 | + /* The error to be returned when libzip asks for the last error code */ |
| 37 | + zip_error_t error; |
| 38 | +} php_zip_string_source; |
| 39 | + |
| 40 | +/* The source callback function, see https://libzip.org/documentation/zip_source_function.html |
| 41 | + * This is similar to read_data() in libzip's zip_source_buffer.c */ |
| 42 | +static zip_int64_t php_zip_string_cb(void *userdata, void *data, zip_uint64_t len, zip_source_cmd_t cmd) |
| 43 | +{ |
| 44 | + php_zip_string_source *ctx = userdata; |
| 45 | + switch (cmd) { |
| 46 | + case ZIP_SOURCE_SUPPORTS: |
| 47 | + return zip_source_make_command_bitmap( |
| 48 | + ZIP_SOURCE_FREE, |
| 49 | +#if LIBZIP_VERSION_MAJOR > 1 || LIBZIP_VERSION_MINOR >= 10 |
| 50 | + ZIP_SOURCE_SUPPORTS_REOPEN, |
| 51 | +#endif |
| 52 | + ZIP_SOURCE_OPEN, |
| 53 | + ZIP_SOURCE_READ, |
| 54 | + ZIP_SOURCE_CLOSE, |
| 55 | + ZIP_SOURCE_STAT, |
| 56 | + ZIP_SOURCE_ERROR, |
| 57 | + ZIP_SOURCE_SEEK, |
| 58 | + ZIP_SOURCE_TELL, |
| 59 | + ZIP_SOURCE_BEGIN_WRITE, |
| 60 | + ZIP_SOURCE_WRITE, |
| 61 | + ZIP_SOURCE_COMMIT_WRITE, |
| 62 | + ZIP_SOURCE_ROLLBACK_WRITE, |
| 63 | + ZIP_SOURCE_SEEK_WRITE, |
| 64 | + ZIP_SOURCE_TELL_WRITE, |
| 65 | + ZIP_SOURCE_REMOVE, |
| 66 | + -1 |
| 67 | + ); |
| 68 | + |
| 69 | + case ZIP_SOURCE_FREE: |
| 70 | + zend_string_release(ctx->out_str); |
| 71 | + zend_string_release(ctx->in_str); |
| 72 | + efree(ctx); |
| 73 | + return 0; |
| 74 | + |
| 75 | + /* Read ops */ |
| 76 | + |
| 77 | + case ZIP_SOURCE_OPEN: |
| 78 | + ctx->in_offset = 0; |
| 79 | + return 0; |
| 80 | + |
| 81 | + case ZIP_SOURCE_READ: { |
| 82 | + size_t remaining = ZSTR_LEN(ctx->in_str) - ctx->in_offset; |
| 83 | + len = MIN(len, remaining); |
| 84 | + if (len) { |
| 85 | + memcpy(data, ZSTR_VAL(ctx->in_str) + ctx->in_offset, len); |
| 86 | + ctx->in_offset += len; |
| 87 | + } |
| 88 | + return len; |
| 89 | + } |
| 90 | + |
| 91 | + case ZIP_SOURCE_CLOSE: |
| 92 | + return 0; |
| 93 | + |
| 94 | + case ZIP_SOURCE_STAT: { |
| 95 | + zip_stat_t *st; |
| 96 | + if (len < sizeof(*st)) { |
| 97 | + zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + st = (zip_stat_t *)data; |
| 102 | + zip_stat_init(st); |
| 103 | + st->mtime = ctx->mtime; |
| 104 | + st->size = ZSTR_LEN(ctx->in_str); |
| 105 | + st->comp_size = st->size; |
| 106 | + st->comp_method = ZIP_CM_STORE; |
| 107 | + st->encryption_method = ZIP_EM_NONE; |
| 108 | + st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
| 109 | + |
| 110 | + return sizeof(*st); |
| 111 | + } |
| 112 | + |
| 113 | + case ZIP_SOURCE_ERROR: |
| 114 | + return zip_error_to_data(&ctx->error, data, len); |
| 115 | + |
| 116 | + /* Seekable read ops */ |
| 117 | + |
| 118 | + case ZIP_SOURCE_SEEK: { |
| 119 | + zip_int64_t new_offset = zip_source_seek_compute_offset( |
| 120 | + ctx->in_offset, ZSTR_LEN(ctx->in_str), data, len, &ctx->error); |
| 121 | + if (new_offset < 0) { |
| 122 | + return -1; |
| 123 | + } |
| 124 | + ctx->in_offset = (size_t)new_offset; |
| 125 | + return 0; |
| 126 | + } |
| 127 | + |
| 128 | + case ZIP_SOURCE_TELL: |
| 129 | + if (ctx->in_offset > ZIP_INT64_MAX) { |
| 130 | + zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
| 131 | + return -1; |
| 132 | + } |
| 133 | + return (zip_int64_t)ctx->in_offset; |
| 134 | + |
| 135 | + /* Write ops */ |
| 136 | + |
| 137 | + case ZIP_SOURCE_BEGIN_WRITE: |
| 138 | + zend_string_release(ctx->out_str); |
| 139 | + ctx->out_str = ZSTR_EMPTY_ALLOC(); |
| 140 | + return 0; |
| 141 | + |
| 142 | + case ZIP_SOURCE_WRITE: |
| 143 | + if (ctx->out_offset > SIZE_MAX - len) { |
| 144 | + zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); |
| 145 | + return -1; |
| 146 | + } |
| 147 | + if (ctx->out_offset + len > ZSTR_LEN(ctx->out_str)) { |
| 148 | + ctx->out_str = zend_string_realloc(ctx->out_str, ctx->out_offset + len, false); |
| 149 | + } |
| 150 | + memcpy(ZSTR_VAL(ctx->out_str) + ctx->out_offset, data, len); |
| 151 | + ctx->out_offset += len; |
| 152 | + return len; |
| 153 | + |
| 154 | + case ZIP_SOURCE_COMMIT_WRITE: |
| 155 | + ZSTR_VAL(ctx->out_str)[ZSTR_LEN(ctx->out_str)] = '\0'; |
| 156 | + zend_string_release(ctx->in_str); |
| 157 | + ctx->in_str = ctx->out_str; |
| 158 | + ctx->out_str = ZSTR_EMPTY_ALLOC(); |
| 159 | + if (ctx->dest) { |
| 160 | + *(ctx->dest) = zend_string_copy(ctx->in_str); |
| 161 | + } |
| 162 | + return 0; |
| 163 | + |
| 164 | + case ZIP_SOURCE_ROLLBACK_WRITE: |
| 165 | + zend_string_release(ctx->out_str); |
| 166 | + ctx->out_str = ZSTR_EMPTY_ALLOC(); |
| 167 | + return 0; |
| 168 | + |
| 169 | + case ZIP_SOURCE_SEEK_WRITE: { |
| 170 | + zip_int64_t new_offset = zip_source_seek_compute_offset( |
| 171 | + ctx->out_offset, ZSTR_LEN(ctx->out_str), data, len, &ctx->error); |
| 172 | + if (new_offset < 0) { |
| 173 | + return -1; |
| 174 | + } |
| 175 | + ctx->out_offset = new_offset; |
| 176 | + return 0; |
| 177 | + } |
| 178 | + |
| 179 | + case ZIP_SOURCE_TELL_WRITE: |
| 180 | + if (ctx->out_offset > ZIP_INT64_MAX) { |
| 181 | + zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
| 182 | + return -1; |
| 183 | + } |
| 184 | + return (zip_int64_t)ctx->out_offset; |
| 185 | + |
| 186 | + case ZIP_SOURCE_REMOVE: |
| 187 | + zend_string_release(ctx->in_str); |
| 188 | + ctx->in_str = ZSTR_EMPTY_ALLOC(); |
| 189 | + ctx->in_offset = 0; |
| 190 | + return 0; |
| 191 | + |
| 192 | + default: |
| 193 | + zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); |
| 194 | + return -1; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +zip_source_t * php_zip_create_string_source(zend_string *str, zend_string **dest, zip_error_t *err) |
| 199 | +{ |
| 200 | + php_zip_string_source *ctx = ecalloc(1, sizeof(php_zip_string_source)); |
| 201 | + ctx->in_str = zend_string_copy(str); |
| 202 | + ctx->out_str = ZSTR_EMPTY_ALLOC(); |
| 203 | + ctx->dest = dest; |
| 204 | + ctx->mtime = time(NULL); |
| 205 | + return zip_source_function_create(php_zip_string_cb, (void*)ctx, err); |
| 206 | +} |
0 commit comments