Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 1 | /* |
Dieter Baron | ba98df0 | 2009-03-30 00:03:53 +0200 | [diff] [blame] | 2 | zip_source_crc.c -- pass-through source that calculates CRC32 and size |
Thomas Klausner | 00ff6bb | 2016-01-08 08:21:22 +0100 | [diff] [blame] | 3 | Copyright (C) 2009-2016 Dieter Baron and Thomas Klausner |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +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 | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 37 | #include <limits.h> |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 38 | |
| 39 | #include "zipint.h" |
| 40 | |
Thomas Klausner | ea60f35 | 2013-03-19 11:08:32 +0100 | [diff] [blame] | 41 | struct crc_context { |
Thomas Klausner | dacdcce | 2015-12-29 20:18:48 +0100 | [diff] [blame] | 42 | int validate; /* whether to check CRC on EOF and return error on mismatch */ |
| 43 | int crc_complete; /* whether CRC was computed for complete file */ |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 44 | zip_error_t error; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 45 | zip_uint64_t size; |
Thomas Klausner | dacdcce | 2015-12-29 20:18:48 +0100 | [diff] [blame] | 46 | zip_uint64_t position; /* current reading position */ |
| 47 | zip_uint64_t crc_position; /* how far we've computed the CRC */ |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 48 | zip_uint32_t crc; |
| 49 | }; |
| 50 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 51 | static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 52 | |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 53 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 54 | zip_source_t * |
| 55 | zip_source_crc(zip_t *za, zip_source_t *src, int validate) |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 56 | { |
Thomas Klausner | ea60f35 | 2013-03-19 11:08:32 +0100 | [diff] [blame] | 57 | struct crc_context *ctx; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 58 | |
Dieter Baron | 2c4f9b7 | 2009-03-31 23:55:42 +0200 | [diff] [blame] | 59 | if (src == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 60 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | |
Thomas Klausner | ea60f35 | 2013-03-19 11:08:32 +0100 | [diff] [blame] | 64 | if ((ctx=(struct crc_context *)malloc(sizeof(*ctx))) == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 65 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 69 | zip_error_init(&ctx->error); |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 70 | ctx->validate = validate; |
| 71 | ctx->crc_complete = 0; |
| 72 | ctx->crc_position = 0; |
| 73 | ctx->crc = (zip_uint32_t)crc32(0, NULL, 0); |
Dieter Baron | 362713b | 2013-04-27 12:23:38 +0200 | [diff] [blame] | 74 | ctx->size = 0; |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 75 | |
Dieter Baron | ba98df0 | 2009-03-30 00:03:53 +0200 | [diff] [blame] | 76 | return zip_source_layered(za, src, crc_read, ctx); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 79 | |
| 80 | static zip_int64_t |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 81 | crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 82 | { |
Thomas Klausner | ea60f35 | 2013-03-19 11:08:32 +0100 | [diff] [blame] | 83 | struct crc_context *ctx; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 84 | zip_int64_t n; |
| 85 | |
Thomas Klausner | ea60f35 | 2013-03-19 11:08:32 +0100 | [diff] [blame] | 86 | ctx = (struct crc_context *)_ctx; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 87 | |
| 88 | switch (cmd) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 89 | case ZIP_SOURCE_OPEN: |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 90 | ctx->position = 0; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | |
| 93 | case ZIP_SOURCE_READ: |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 94 | if ((n = zip_source_read(src, data, len)) < 0) { |
Thomas Klausner | d97f544 | 2014-09-23 17:02:03 +0200 | [diff] [blame] | 95 | _zip_error_set_from_source(&ctx->error, src); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | if (n == 0) { |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 100 | if (ctx->crc_position == ctx->position) { |
| 101 | ctx->crc_complete = 1; |
| 102 | ctx->size = ctx->position; |
| 103 | |
| 104 | if (ctx->validate) { |
| 105 | struct zip_stat st; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 106 | |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 107 | if (zip_source_stat(src, &st) < 0) { |
| 108 | _zip_error_set_from_source(&ctx->error, src); |
| 109 | return -1; |
| 110 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 111 | |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 112 | if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) { |
| 113 | zip_error_set(&ctx->error, ZIP_ER_CRC, 0); |
| 114 | return -1; |
| 115 | } |
| 116 | if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) { |
| 117 | zip_error_set(&ctx->error, ZIP_ER_INCONS, 0); |
| 118 | return -1; |
| 119 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 123 | else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 124 | zip_uint64_t i, nn; |
Dieter Baron | 301472c | 2009-03-12 16:09:09 +0100 | [diff] [blame] | 125 | |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 126 | for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) { |
Thomas Klausner | 03ca1c1 | 2014-09-24 01:02:15 +0200 | [diff] [blame] | 127 | nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n-i); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 128 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 129 | ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data+i, (uInt)nn); |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 130 | ctx->crc_position += nn; |
Dieter Baron | ba98df0 | 2009-03-30 00:03:53 +0200 | [diff] [blame] | 131 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 132 | } |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 133 | ctx->position += (zip_uint64_t)n; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 134 | return n; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 135 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 136 | case ZIP_SOURCE_CLOSE: |
| 137 | return 0; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 138 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 139 | case ZIP_SOURCE_STAT: |
| 140 | { |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 141 | zip_stat_t *st; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 142 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 143 | st = (zip_stat_t *)data; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 144 | |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 145 | if (ctx->crc_complete) { |
Thomas Klausner | b52bda0 | 2013-11-28 18:01:40 +0100 | [diff] [blame] | 146 | /* TODO: Set comp_size, comp_method, encryption_method? |
Dieter Baron | 35f2b68 | 2009-03-12 18:36:34 +0100 | [diff] [blame] | 147 | After all, this only works for uncompressed data. */ |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 148 | st->size = ctx->size; |
| 149 | st->crc = ctx->crc; |
Dieter Baron | 3efab99 | 2012-05-04 09:29:29 +0200 | [diff] [blame] | 150 | st->comp_size = ctx->size; |
| 151 | st->comp_method = ZIP_CM_STORE; |
| 152 | st->encryption_method = ZIP_EM_NONE; |
| 153 | st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 154 | } |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | case ZIP_SOURCE_ERROR: |
| 159 | return zip_error_to_data(&ctx->error, data, len); |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 160 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 161 | case ZIP_SOURCE_FREE: |
| 162 | free(ctx); |
| 163 | return 0; |
| 164 | |
| 165 | case ZIP_SOURCE_SUPPORTS: |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 166 | { |
| 167 | zip_int64_t mask = zip_source_supports(src); |
| 168 | |
| 169 | if (mask < 0) { |
| 170 | _zip_error_set_from_source(&ctx->error, src); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | return mask & ~zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1); |
| 175 | } |
| 176 | |
| 177 | case ZIP_SOURCE_SEEK: |
| 178 | { |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 179 | zip_int64_t new_position; |
Thomas Klausner | ea69ac1 | 2016-01-05 14:49:33 +0100 | [diff] [blame] | 180 | zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error); |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 181 | |
Thomas Klausner | ea69ac1 | 2016-01-05 14:49:33 +0100 | [diff] [blame] | 182 | if (args == NULL) { |
| 183 | return -1; |
| 184 | } |
Dieter Baron | df3089b | 2015-07-12 11:14:22 +0200 | [diff] [blame] | 185 | if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) { |
| 186 | _zip_error_set_from_source(&ctx->error, src); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | ctx->position = (zip_uint64_t)new_position; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | case ZIP_SOURCE_TELL: |
| 196 | return (zip_int64_t)ctx->position; |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 197 | |
| 198 | default: |
| 199 | zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); |
| 200 | return -1; |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 201 | } |
Dieter Baron | 3291dac | 2009-03-12 15:56:36 +0100 | [diff] [blame] | 202 | } |