Coverage Report

Created: 2026-08-11 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_crc.c
Line
Count
Source
1
/*
2
  zip_source_crc.c -- pass-through source that calculates CRC32 and size
3
  Copyright (C) 2009-2025 Dieter Baron and Thomas Klausner
4
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
  The authors can be contacted at <info@libzip.org>
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
34
35
#include <limits.h>
36
#include <stdlib.h>
37
#include <zlib.h>
38
39
#include "zipint.h"
40
41
struct crc_context {
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 */
44
    zip_error_t error;
45
    zip_uint64_t size;
46
    zip_uint64_t position;     /* current reading position */
47
    zip_uint64_t crc_position; /* how far we've computed the CRC */
48
    zip_uint32_t crc;
49
};
50
51
static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
52
static zip_int64_t validate_crc(struct crc_context *ctx, zip_source_t *src);
53
54
9.73k
zip_source_t *zip_source_crc_create(zip_source_t *src, int validate, zip_error_t *error) {
55
9.73k
    struct crc_context *ctx;
56
9.73k
    zip_source_t *new_src;
57
58
9.73k
    if (src == NULL) {
59
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
60
0
        return NULL;
61
0
    }
62
63
9.73k
    if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
64
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
65
0
        return NULL;
66
0
    }
67
68
9.73k
    zip_error_init(&ctx->error);
69
9.73k
    ctx->validate = validate;
70
9.73k
    ctx->crc_complete = 0;
71
9.73k
    ctx->crc_position = 0;
72
9.73k
    ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
73
9.73k
    ctx->size = 0;
74
75
9.73k
    new_src = zip_source_layered_create(src, crc_read, ctx, error);
76
9.73k
    if (new_src == NULL) {
77
0
        free(ctx);
78
0
        return NULL;
79
0
    }
80
9.73k
    return new_src;
81
9.73k
}
82
83
84
58.1k
static zip_int64_t crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
85
58.1k
    struct crc_context *ctx;
86
58.1k
    zip_int64_t n;
87
88
58.1k
    ctx = (struct crc_context *)_ctx;
89
90
58.1k
    switch (cmd) {
91
8.88k
    case ZIP_SOURCE_OPEN:
92
8.88k
        ctx->position = 0;
93
8.88k
        return 0;
94
95
11.2k
    case ZIP_SOURCE_READ:
96
11.2k
        if ((n = zip_source_read(src, data, len)) < 0) {
97
2.15k
            zip_error_set_from_source(&ctx->error, src);
98
2.15k
            return -1;
99
2.15k
        }
100
101
9.10k
        if (n == 0) {
102
2.08k
            return validate_crc(ctx, src);
103
2.08k
        }
104
7.01k
        else {
105
            /*
106
             We can only compute the CRC data in the correct order. So we update the CRC for data read this time that starts at ctx->crc_position.
107
108
             If we already computed the CRC for the whole file, or if there is a gap between ctx->crc_position and the start of new data, or if we already computed the CRC for all new data read, we don't update the CRC.
109
110
             ctx->position is the position of the first byte in data.
111
112
             ctx->crc_position is the position of the first byte in data that has not been included  in the CRC yet.
113
114
             Therefore, we want to compute the CRC for data[ctx->crc_position - ctx->position .. n-1].
115
116
            */
117
7.01k
            if (!ctx->crc_complete && ctx->position <= ctx->crc_position && ctx->crc_position < ctx->position + (zip_uint64_t)n) {
118
7.01k
                zip_uint64_t i, nn;
119
120
14.0k
                for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) {
121
7.01k
                    nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i);
122
123
7.01k
                    ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn);
124
7.01k
                    ctx->crc_position += nn;
125
7.01k
                }
126
7.01k
            }
127
7.01k
        }
128
7.01k
        ctx->position += (zip_uint64_t)n;
129
7.01k
        return n;
130
131
8.88k
    case ZIP_SOURCE_CLOSE: {
132
8.88k
        zip_int64_t ret = zip_source_at_eof(src);
133
8.88k
        if (ret < 0) {
134
1.88k
            zip_error_set_from_source(&ctx->error, src);
135
1.88k
            return -1;
136
1.88k
        }
137
7.00k
        else if (ret == 1) {
138
5.69k
            return validate_crc(ctx, src);
139
5.69k
        }
140
1.30k
        else {
141
1.30k
            return 0;
142
1.30k
        }
143
8.88k
    }
144
145
0
    case ZIP_SOURCE_STAT: {
146
0
        zip_stat_t *st;
147
148
0
        st = (zip_stat_t *)data;
149
150
0
        if (ctx->crc_complete) {
151
0
            if ((st->valid & ZIP_STAT_SIZE) && st->size != ctx->size) {
152
0
                zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0);
153
0
                return -1;
154
0
            }
155
            /* TODO: Set comp_size, comp_method, encryption_method?
156
                    After all, this only works for uncompressed data. */
157
0
            st->size = ctx->size;
158
0
            st->crc = ctx->crc;
159
0
            st->comp_size = ctx->size;
160
0
            st->comp_method = ZIP_CM_STORE;
161
0
            st->encryption_method = ZIP_EM_NONE;
162
0
            st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
163
0
        }
164
0
        return 0;
165
0
    }
166
167
9.65k
    case ZIP_SOURCE_ERROR:
168
9.65k
        return zip_error_to_data(&ctx->error, data, len);
169
170
9.73k
    case ZIP_SOURCE_FREE:
171
9.73k
        free(ctx);
172
9.73k
        return 0;
173
174
9.73k
    case ZIP_SOURCE_SUPPORTS: {
175
9.73k
        zip_int64_t mask = zip_source_supports(src);
176
177
9.73k
        if (mask < 0) {
178
0
            zip_error_set_from_source(&ctx->error, src);
179
0
            return -1;
180
0
        }
181
182
9.73k
        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, ZIP_SOURCE_GET_FILE_ATTRIBUTES, -1);
183
9.73k
        mask |= zip_source_make_command_bitmap(ZIP_SOURCE_FREE, -1);
184
9.73k
        return mask;
185
9.73k
    }
186
187
0
    case ZIP_SOURCE_SEEK: {
188
0
        zip_int64_t new_position;
189
0
        zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
190
191
0
        if (args == NULL) {
192
0
            return -1;
193
0
        }
194
0
        if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) {
195
0
            zip_error_set_from_source(&ctx->error, src);
196
0
            return -1;
197
0
        }
198
199
0
        ctx->position = (zip_uint64_t)new_position;
200
201
0
        return 0;
202
0
    }
203
204
0
    case ZIP_SOURCE_TELL:
205
0
        return (zip_int64_t)ctx->position;
206
207
0
    default:
208
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
209
58.1k
    }
210
58.1k
}
211
212
7.78k
zip_int64_t validate_crc(struct crc_context *ctx, zip_source_t *src) {
213
7.78k
    struct zip_stat st;
214
215
7.78k
    ctx->size = ctx->position;
216
217
7.78k
    if (ctx->validate) {
218
7.78k
        if (zip_source_stat(src, &st) < 0) {
219
0
            zip_error_set_from_source(&ctx->error, src);
220
0
            return -1;
221
0
        }
222
7.78k
    }
223
224
7.78k
    if (ctx->crc_position == ctx->position) {
225
7.78k
        ctx->crc_complete = 1;
226
227
7.78k
        if (ctx->validate) {
228
7.78k
            if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
229
5.55k
                zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
230
5.55k
                return -1;
231
5.55k
            }
232
7.78k
        }
233
7.78k
    }
234
235
2.23k
    if (ctx->validate) {
236
2.23k
        if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
237
            /* We don't have the index here, but the caller should know which file they are reading from. */
238
64
            zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_INVALID_FILE_LENGTH, MAX_DETAIL_INDEX));
239
64
            return -1;
240
64
        }
241
2.23k
    }
242
243
2.16k
    return 0;
244
2.23k
}