Coverage Report

Created: 2026-04-01 07:49

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
53
54
0
zip_source_t *zip_source_crc_create(zip_source_t *src, int validate, zip_error_t *error) {
55
0
    struct crc_context *ctx;
56
0
    zip_source_t *new_src;
57
58
0
    if (src == NULL) {
59
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
60
0
        return NULL;
61
0
    }
62
63
0
    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
0
    zip_error_init(&ctx->error);
69
0
    ctx->validate = validate;
70
0
    ctx->crc_complete = 0;
71
0
    ctx->crc_position = 0;
72
0
    ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
73
0
    ctx->size = 0;
74
75
0
    new_src = zip_source_layered_create(src, crc_read, ctx, error);
76
0
    if (new_src == NULL) {
77
0
        free(ctx);
78
0
        return NULL;
79
0
    }
80
0
    return new_src;
81
0
}
82
83
84
0
static zip_int64_t crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
85
0
    struct crc_context *ctx;
86
0
    zip_int64_t n;
87
88
0
    ctx = (struct crc_context *)_ctx;
89
90
0
    switch (cmd) {
91
0
    case ZIP_SOURCE_OPEN:
92
0
        ctx->position = 0;
93
0
        return 0;
94
95
0
    case ZIP_SOURCE_READ:
96
0
        if ((n = zip_source_read(src, data, len)) < 0) {
97
0
            zip_error_set_from_source(&ctx->error, src);
98
0
            return -1;
99
0
        }
100
101
0
        if (n == 0) {
102
0
            if (ctx->crc_position == ctx->position) {
103
0
                ctx->crc_complete = 1;
104
0
                ctx->size = ctx->position;
105
106
0
                if (ctx->validate) {
107
0
                    struct zip_stat st;
108
109
0
                    if (zip_source_stat(src, &st) < 0) {
110
0
                        zip_error_set_from_source(&ctx->error, src);
111
0
                        return -1;
112
0
                    }
113
114
0
                    if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
115
0
                        zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
116
0
                        return -1;
117
0
                    }
118
0
                    if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
119
                        /* We don't have the index here, but the caller should know which file they are reading from. */
120
0
                        zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_INVALID_FILE_LENGTH, MAX_DETAIL_INDEX));
121
0
                        return -1;
122
0
                    }
123
0
                }
124
0
            }
125
0
        }
126
0
        else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) {
127
0
            zip_uint64_t i, nn;
128
129
0
            for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) {
130
0
                nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i);
131
132
0
                ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn);
133
0
                ctx->crc_position += nn;
134
0
            }
135
0
        }
136
0
        ctx->position += (zip_uint64_t)n;
137
0
        return n;
138
139
0
    case ZIP_SOURCE_CLOSE:
140
0
        return 0;
141
142
0
    case ZIP_SOURCE_STAT: {
143
0
        zip_stat_t *st;
144
145
0
        st = (zip_stat_t *)data;
146
147
0
        if (ctx->crc_complete) {
148
0
            if ((st->valid & ZIP_STAT_SIZE) && st->size != ctx->size) {
149
0
                zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0);
150
0
                return -1;
151
0
            }
152
            /* TODO: Set comp_size, comp_method, encryption_method?
153
                    After all, this only works for uncompressed data. */
154
0
            st->size = ctx->size;
155
0
            st->crc = ctx->crc;
156
0
            st->comp_size = ctx->size;
157
0
            st->comp_method = ZIP_CM_STORE;
158
0
            st->encryption_method = ZIP_EM_NONE;
159
0
            st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
160
0
        }
161
0
        return 0;
162
0
    }
163
164
0
    case ZIP_SOURCE_ERROR:
165
0
        return zip_error_to_data(&ctx->error, data, len);
166
167
0
    case ZIP_SOURCE_FREE:
168
0
        free(ctx);
169
0
        return 0;
170
171
0
    case ZIP_SOURCE_SUPPORTS: {
172
0
        zip_int64_t mask = zip_source_supports(src);
173
174
0
        if (mask < 0) {
175
0
            zip_error_set_from_source(&ctx->error, src);
176
0
            return -1;
177
0
        }
178
179
0
        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);
180
0
        mask |= zip_source_make_command_bitmap(ZIP_SOURCE_FREE, -1);
181
0
        return mask;
182
0
    }
183
184
0
    case ZIP_SOURCE_SEEK: {
185
0
        zip_int64_t new_position;
186
0
        zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
187
188
0
        if (args == NULL) {
189
0
            return -1;
190
0
        }
191
0
        if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) {
192
0
            zip_error_set_from_source(&ctx->error, src);
193
0
            return -1;
194
0
        }
195
196
0
        ctx->position = (zip_uint64_t)new_position;
197
198
0
        return 0;
199
0
    }
200
201
0
    case ZIP_SOURCE_TELL:
202
0
        return (zip_int64_t)ctx->position;
203
204
0
    default:
205
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
206
0
    }
207
0
}