Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | zip_source_window.c -- return part of lower source |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 3 | Copyright (C) 2012-2014 Dieter Baron and Thomas Klausner |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
| 6 | The authors can be contacted at <libzip@nih.at> |
| 7 | |
| 8 | Redistribution and use in source and binary forms, with or without |
| 9 | modification, are permitted provided that the following conditions |
| 10 | are met: |
| 11 | 1. Redistributions of source code must retain the above copyright |
| 12 | notice, this list of conditions and the following disclaimer. |
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in |
| 15 | the documentation and/or other materials provided with the |
| 16 | distribution. |
| 17 | 3. The names of the authors may not be used to endorse or promote |
| 18 | products derived from this software without specific prior |
| 19 | written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
| 22 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 25 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 29 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "zipint.h" |
| 39 | |
| 40 | struct window { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 41 | zip_uint64_t start; |
| 42 | zip_uint64_t end; |
| 43 | zip_uint64_t offset; |
| 44 | zip_stat_t stat; |
| 45 | zip_error_t error; |
| 46 | zip_int64_t supports; |
Dieter Baron | 8631d4b | 2014-10-10 16:28:54 +0200 | [diff] [blame] | 47 | bool needs_seek; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 50 | static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 51 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 52 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 53 | zip_source_t * |
| 54 | zip_source_window(zip_t *za, zip_source_t *src, zip_uint64_t start, zip_uint64_t len) |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 55 | { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 56 | return _zip_source_window_new(src, start, len, NULL, &za->error); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | zip_source_t * |
| 61 | _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t length, zip_stat_t *st, zip_error_t *error) |
| 62 | { |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 63 | struct window *ctx; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 64 | |
| 65 | if (src == NULL || start + length < start) { |
| 66 | zip_error_set(error, ZIP_ER_INVAL, 0); |
| 67 | return NULL; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 68 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 69 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 70 | if ((ctx=(struct window *)malloc(sizeof(*ctx))) == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 71 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
| 72 | return NULL; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 73 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 74 | |
| 75 | ctx->start = start; |
| 76 | ctx->end = start + length; |
| 77 | zip_stat_init(&ctx->stat); |
| 78 | zip_error_init(&ctx->error); |
Dieter Baron | 8631d4b | 2014-10-10 16:28:54 +0200 | [diff] [blame] | 79 | ctx->supports = (zip_source_supports(src) & ZIP_SOURCE_SUPPORTS_SEEKABLE) | (zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, -1)); |
| 80 | ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 81 | |
| 82 | if (st) { |
| 83 | if (_zip_stat_merge(&ctx->stat, st, error) < 0) { |
| 84 | free(ctx); |
| 85 | return NULL; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return zip_source_layered_create(src, window_read, ctx, error); |
| 90 | } |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 91 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 92 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 93 | int |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 94 | _zip_source_set_source_archive(zip_source_t *src, zip_t *za) |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 95 | { |
| 96 | src->source_archive = za; |
| 97 | return _zip_register_source(za, src); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /* called by zip_discard to avoid operating on file from closed archive */ |
| 102 | void |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 103 | _zip_source_invalidate(zip_source_t *src) |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 104 | { |
| 105 | src->source_closed = 1; |
| 106 | |
| 107 | if (zip_error_code_zip(&src->error) == ZIP_ER_OK) { |
| 108 | zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0); |
| 109 | } |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 112 | |
| 113 | static zip_int64_t |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 114 | window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 115 | { |
| 116 | struct window *ctx; |
Dieter Baron | abc6fd7 | 2012-07-22 15:49:45 +0200 | [diff] [blame] | 117 | zip_int64_t ret; |
| 118 | zip_uint64_t n, i; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 119 | char b[8192]; |
| 120 | |
| 121 | ctx = (struct window *)_ctx; |
| 122 | |
| 123 | switch (cmd) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 124 | case ZIP_SOURCE_CLOSE: |
| 125 | return 0; |
| 126 | |
| 127 | case ZIP_SOURCE_ERROR: |
| 128 | return zip_error_to_data(&ctx->error, data, len); |
| 129 | |
| 130 | case ZIP_SOURCE_FREE: |
| 131 | free(ctx); |
| 132 | return 0; |
| 133 | |
| 134 | case ZIP_SOURCE_OPEN: |
| 135 | if (!ctx->needs_seek) { |
| 136 | for (n=0; n<ctx->start; n+=(zip_uint64_t)ret) { |
| 137 | i = (ctx->start-n > sizeof(b) ? sizeof(b) : ctx->start-n); |
| 138 | if ((ret=zip_source_read(src, b, i)) < 0) { |
Thomas Klausner | d97f544 | 2014-09-23 17:02:03 +0200 | [diff] [blame] | 139 | _zip_error_set_from_source(&ctx->error, src); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 140 | return -1; |
| 141 | } |
| 142 | if (ret==0) { |
| 143 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
| 144 | return -1; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | ctx->offset = ctx->start; |
| 150 | return 0; |
| 151 | |
| 152 | case ZIP_SOURCE_READ: |
| 153 | if (len > ctx->end - ctx->offset) |
| 154 | len = ctx->end - ctx->offset; |
| 155 | |
| 156 | if (len == 0) |
| 157 | return 0; |
| 158 | |
| 159 | if (ctx->needs_seek) { |
| 160 | if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) { |
Thomas Klausner | d97f544 | 2014-09-23 17:02:03 +0200 | [diff] [blame] | 161 | _zip_error_set_from_source(&ctx->error, src); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if ((ret=zip_source_read(src, data, len)) < 0) { |
| 167 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | ctx->offset += (zip_uint64_t)ret; |
| 172 | |
| 173 | if (ret == 0) { |
| 174 | if (ctx->offset < ctx->end) { |
| 175 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
| 176 | return -1; |
| 177 | } |
| 178 | } |
| 179 | return ret; |
| 180 | |
| 181 | case ZIP_SOURCE_SEEK: |
| 182 | { |
Dieter Baron | c548a18 | 2014-10-10 18:27:50 +0200 | [diff] [blame] | 183 | zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, ctx->end - ctx->start, data, len, &ctx->error); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 184 | |
Dieter Baron | c548a18 | 2014-10-10 18:27:50 +0200 | [diff] [blame] | 185 | if (new_offset < 0) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 186 | return -1; |
| 187 | } |
| 188 | |
Dieter Baron | c548a18 | 2014-10-10 18:27:50 +0200 | [diff] [blame] | 189 | ctx->offset = (zip_uint64_t)new_offset + ctx->start; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 190 | return 0; |
| 191 | } |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 192 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 193 | case ZIP_SOURCE_STAT: |
| 194 | { |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 195 | zip_stat_t *st; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 196 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 197 | st = (zip_stat_t *)data; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 198 | |
| 199 | if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) { |
| 200 | return -1; |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | case ZIP_SOURCE_SUPPORTS: |
| 206 | return ctx->supports; |
| 207 | |
| 208 | case ZIP_SOURCE_TELL: |
| 209 | return (zip_int64_t)(ctx->offset - ctx->start); |
| 210 | |
| 211 | default: |
| 212 | zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); |
| 213 | return -1; |
| 214 | } |
| 215 | } |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 216 | |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 217 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 218 | void |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 219 | _zip_deregister_source(zip_t *za, zip_source_t *src) |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 220 | { |
| 221 | unsigned int i; |
| 222 | |
| 223 | for (i=0; i<za->nopen_source; i++) { |
| 224 | if (za->open_source[i] == src) { |
| 225 | za->open_source[i] = za->open_source[za->nopen_source-1]; |
| 226 | za->nopen_source--; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 231 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 232 | |
| 233 | int |
| 234 | _zip_register_source(zip_t *za, zip_source_t *src) |
| 235 | { |
| 236 | zip_source_t **open_source; |
| 237 | |
| 238 | if (za->nopen_source+1 >= za->nopen_source_alloc) { |
| 239 | unsigned int n; |
| 240 | n = za->nopen_source_alloc + 10; |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 241 | open_source = (zip_source_t **)realloc(za->open_source, n*sizeof(zip_source_t *)); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 242 | if (open_source == NULL) { |
| 243 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
| 244 | return -1; |
| 245 | } |
| 246 | za->nopen_source_alloc = n; |
| 247 | za->open_source = open_source; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 250 | za->open_source[za->nopen_source++] = src; |
| 251 | |
| 252 | return 0; |
Dieter Baron | ba561a6 | 2012-02-13 22:02:46 +0100 | [diff] [blame] | 253 | } |