Coverage Report

Created: 2025-07-12 06:27

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