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