Coverage Report

Created: 2026-04-13 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_file_common.c
Line
Count
Source
1
/*
2
  zip_source_file_common.c -- create data source from file
3
  Copyright (C) 1999-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
#include "zipint.h"
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#include "zip_source_file.h"
41
42
static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd);
43
44
5.53k
static void zip_source_file_stat_init(zip_source_file_stat_t *st) {
45
5.53k
    st->size = 0;
46
5.53k
    st->mtime = time(NULL);
47
5.53k
    st->exists = false;
48
5.53k
    st->regular_file = false;
49
5.53k
}
50
51
5.53k
zip_source_t *zip_source_file_common_new(const char *fname, void *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_source_file_operations_t *ops, void *ops_userdata, zip_error_t *error) {
52
5.53k
    zip_source_file_context_t *ctx;
53
5.53k
    zip_source_t *zs;
54
5.53k
    zip_source_file_stat_t sb;
55
5.53k
    zip_uint64_t length;
56
57
5.53k
    if (ops == NULL) {
58
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
59
0
        return NULL;
60
0
    }
61
62
5.53k
    if (ops->close == NULL || ops->read == NULL || ops->seek == NULL || ops->stat == NULL) {
63
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
64
0
        return NULL;
65
0
    }
66
67
5.53k
    if (ops->write != NULL && (ops->commit_write == NULL || ops->create_temp_output == NULL || ops->remove == NULL || ops->rollback_write == NULL || ops->tell == NULL)) {
68
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
69
0
        return NULL;
70
0
    }
71
72
5.53k
    if (fname != NULL) {
73
5.53k
        if (ops->open == NULL || ops->string_duplicate == NULL) {
74
0
            zip_error_set(error, ZIP_ER_INTERNAL, 0);
75
0
            return NULL;
76
0
        }
77
5.53k
    }
78
0
    else if (file == NULL) {
79
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
80
0
        return NULL;
81
0
    }
82
83
5.53k
    if (len < 0) {
84
5.53k
        if (len == -1) {
85
5.53k
            len = ZIP_LENGTH_TO_END;
86
5.53k
        }
87
        // TODO: return ZIP_ER_INVAL if len != ZIP_LENGTH_UNCHECKED?
88
5.53k
        length = 0;
89
5.53k
    }
90
0
    else {
91
0
        length = (zip_uint64_t)len;
92
0
    }
93
94
5.53k
    if (start > ZIP_INT64_MAX || start + length < start) {
95
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
96
0
        return NULL;
97
0
    }
98
99
5.53k
    if ((ctx = (zip_source_file_context_t *)malloc(sizeof(zip_source_file_context_t))) == NULL) {
100
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
101
0
        return NULL;
102
0
    }
103
104
5.53k
    ctx->ops = ops;
105
5.53k
    ctx->ops_userdata = ops_userdata;
106
5.53k
    ctx->fname = NULL;
107
5.53k
    if (fname) {
108
5.53k
        if ((ctx->fname = ops->string_duplicate(ctx, fname)) == NULL) {
109
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
110
0
            free(ctx);
111
0
            return NULL;
112
0
        }
113
5.53k
    }
114
5.53k
    ctx->f = file;
115
5.53k
    ctx->start = start;
116
5.53k
    ctx->len = length;
117
5.53k
    if (st) {
118
0
        (void)memcpy_s(&ctx->st, sizeof(ctx->st), st, sizeof(*st));
119
0
        ctx->st.name = NULL;
120
0
        ctx->st.valid &= ~ZIP_STAT_NAME;
121
0
    }
122
5.53k
    else {
123
5.53k
        zip_stat_init(&ctx->st);
124
5.53k
    }
125
126
5.53k
    if (ctx->len > 0) {
127
0
        ctx->st.size = ctx->len;
128
0
        ctx->st.valid |= ZIP_STAT_SIZE;
129
0
    }
130
131
5.53k
    zip_error_init(&ctx->stat_error);
132
133
5.53k
    ctx->tmpname = NULL;
134
5.53k
    ctx->fout = NULL;
135
136
5.53k
    zip_error_init(&ctx->error);
137
5.53k
    zip_file_attributes_init(&ctx->attributes);
138
139
5.53k
    ctx->supports = ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_SUPPORTS_REOPEN, -1);
140
141
5.53k
    zip_source_file_stat_init(&sb);
142
5.53k
    if (!ops->stat(ctx, &sb)) {
143
0
        _zip_error_copy(error, &ctx->error);
144
0
        free(ctx->fname);
145
0
        free(ctx);
146
0
        return NULL;
147
0
    }
148
149
5.53k
    if (!sb.exists) {
150
1.03k
        if (ctx->fname && ctx->start == 0 && ctx->len == 0 && ops->write != NULL) {
151
1.03k
            ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
152
            /* zip_open_from_source checks for this to detect non-existing files */
153
1.03k
            zip_error_set(&ctx->stat_error, ZIP_ER_READ, ENOENT);
154
1.03k
        }
155
0
        else {
156
0
            zip_error_set(&ctx->stat_error, ZIP_ER_READ, ENOENT);
157
0
            free(ctx->fname);
158
0
            free(ctx);
159
0
            return NULL;
160
0
        }
161
1.03k
    }
162
4.50k
    else {
163
4.50k
        if ((ctx->st.valid & ZIP_STAT_MTIME) == 0) {
164
4.50k
            ctx->st.mtime = sb.mtime;
165
4.50k
            ctx->st.valid |= ZIP_STAT_MTIME;
166
4.50k
        }
167
4.50k
        if (sb.regular_file) {
168
4.50k
            ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE;
169
170
4.50k
            if (ctx->start + ctx->len > sb.size) {
171
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
172
0
                free(ctx->fname);
173
0
                free(ctx);
174
0
                return NULL;
175
0
            }
176
177
4.50k
            if (ctx->len == 0) {
178
4.50k
                if (len != ZIP_LENGTH_UNCHECKED) {
179
4.50k
                    ctx->len = sb.size - ctx->start;
180
4.50k
                    ctx->st.size = ctx->len;
181
4.50k
                    ctx->st.valid |= ZIP_STAT_SIZE;
182
4.50k
                }
183
184
                /* when using a partial file, don't allow writing */
185
4.50k
                if (ctx->fname && start == 0 && ops->write != NULL) {
186
4.50k
                    ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
187
4.50k
                }
188
4.50k
            }
189
4.50k
        }
190
191
4.50k
        ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_GET_FILE_ATTRIBUTES);
192
4.50k
    }
193
194
5.53k
    ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_ACCEPT_EMPTY);
195
5.53k
    if (ops->create_temp_output_cloning != NULL) {
196
5.53k
        if (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE)) {
197
5.53k
            ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING);
198
5.53k
        }
199
5.53k
    }
200
201
5.53k
    if ((zs = zip_source_function_create(read_file, ctx, error)) == NULL) {
202
0
        free(ctx->fname);
203
0
        free(ctx);
204
0
        return NULL;
205
0
    }
206
207
5.53k
    return zs;
208
5.53k
}
209
210
211
341k
static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
212
341k
    zip_source_file_context_t *ctx;
213
341k
    char *buf;
214
215
341k
    ctx = (zip_source_file_context_t *)state;
216
341k
    buf = (char *)data;
217
218
341k
    switch (cmd) {
219
0
    case ZIP_SOURCE_ACCEPT_EMPTY:
220
0
        return 0;
221
222
1.03k
    case ZIP_SOURCE_BEGIN_WRITE:
223
        /* write support should not be set if fname is NULL */
224
1.03k
        if (ctx->fname == NULL) {
225
0
            zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
226
0
            return -1;
227
0
        }
228
1.03k
        return ctx->ops->create_temp_output(ctx);
229
230
0
    case ZIP_SOURCE_BEGIN_WRITE_CLONING:
231
        /* write support should not be set if fname is NULL */
232
0
        if (ctx->fname == NULL) {
233
0
            zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
234
0
            return -1;
235
0
        }
236
0
        return ctx->ops->create_temp_output_cloning(ctx, len);
237
238
4.50k
    case ZIP_SOURCE_CLOSE:
239
4.50k
        if (ctx->fname) {
240
4.50k
            ctx->ops->close(ctx);
241
4.50k
            ctx->f = NULL;
242
4.50k
        }
243
4.50k
        return 0;
244
245
1.03k
    case ZIP_SOURCE_COMMIT_WRITE: {
246
1.03k
        zip_int64_t ret = ctx->ops->commit_write(ctx);
247
1.03k
        ctx->fout = NULL;
248
1.03k
        if (ret == 0) {
249
1.03k
            free(ctx->tmpname);
250
1.03k
            ctx->tmpname = NULL;
251
1.03k
        }
252
1.03k
        return ret;
253
0
    }
254
255
4.83k
    case ZIP_SOURCE_ERROR:
256
4.83k
        return zip_error_to_data(&ctx->error, data, len);
257
258
5.53k
    case ZIP_SOURCE_FREE:
259
5.53k
        free(ctx->fname);
260
5.53k
        free(ctx->tmpname);
261
5.53k
        if (ctx->f) {
262
0
            ctx->ops->close(ctx);
263
0
        }
264
5.53k
        free(ctx);
265
5.53k
        return 0;
266
267
16.2k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES:
268
16.2k
        if (len < sizeof(ctx->attributes)) {
269
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
270
0
            return -1;
271
0
        }
272
16.2k
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
273
16.2k
        return sizeof(ctx->attributes);
274
275
4.50k
    case ZIP_SOURCE_OPEN:
276
4.50k
        if (ctx->fname) {
277
4.50k
            if (ctx->ops->open(ctx) == false) {
278
0
                return -1;
279
0
            }
280
4.50k
        }
281
282
4.50k
        if (ctx->start > 0) { /* TODO: rewind on re-open */
283
0
            if (ctx->ops->seek(ctx, ctx->f, (zip_int64_t)ctx->start, SEEK_SET) == false) {
284
                /* TODO: skip by reading */
285
0
                return -1;
286
0
            }
287
0
        }
288
4.50k
        ctx->offset = 0;
289
4.50k
        return 0;
290
291
148k
    case ZIP_SOURCE_READ: {
292
148k
        zip_int64_t i;
293
148k
        zip_uint64_t n;
294
295
148k
        if (ctx->len > 0) {
296
148k
            n = ZIP_MIN(ctx->len - ctx->offset, len);
297
148k
        }
298
0
        else {
299
0
            n = len;
300
0
        }
301
302
148k
        if ((i = ctx->ops->read(ctx, buf, n)) < 0) {
303
0
            zip_error_set(&ctx->error, ZIP_ER_READ, errno);
304
0
            return -1;
305
0
        }
306
148k
        ctx->offset += (zip_uint64_t)i;
307
308
148k
        return i;
309
148k
    }
310
311
0
    case ZIP_SOURCE_REMOVE:
312
0
        return ctx->ops->remove(ctx);
313
314
0
    case ZIP_SOURCE_ROLLBACK_WRITE:
315
0
        ctx->ops->rollback_write(ctx);
316
0
        ctx->fout = NULL;
317
0
        free(ctx->tmpname);
318
0
        ctx->tmpname = NULL;
319
0
        return 0;
320
321
72.1k
    case ZIP_SOURCE_SEEK: {
322
72.1k
        zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset, ctx->len, data, len, &ctx->error);
323
324
72.1k
        if (new_offset < 0) {
325
3.80k
            return -1;
326
3.80k
        }
327
328
        /* The actual offset inside the file must be representable as zip_int64_t. */
329
68.3k
        if (new_offset > ZIP_INT64_MAX - (zip_int64_t)ctx->start) {
330
0
            zip_error_set(&ctx->error, ZIP_ER_SEEK, EOVERFLOW);
331
0
            return -1;
332
0
        }
333
334
68.3k
        ctx->offset = (zip_uint64_t)new_offset;
335
336
68.3k
        if (ctx->ops->seek(ctx, ctx->f, (zip_int64_t)(ctx->offset + ctx->start), SEEK_SET) == false) {
337
0
            return -1;
338
0
        }
339
68.3k
        return 0;
340
68.3k
    }
341
342
2.06k
    case ZIP_SOURCE_SEEK_WRITE: {
343
2.06k
        zip_source_args_seek_t *args;
344
345
2.06k
        args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
346
2.06k
        if (args == NULL) {
347
0
            return -1;
348
0
        }
349
350
2.06k
        if (ctx->ops->seek(ctx, ctx->fout, args->offset, args->whence) == false) {
351
0
            return -1;
352
0
        }
353
2.06k
        return 0;
354
2.06k
    }
355
356
30.8k
    case ZIP_SOURCE_STAT: {
357
30.8k
        if (len < sizeof(ctx->st)) {
358
0
            return -1;
359
0
        }
360
361
30.8k
        if (zip_error_code_zip(&ctx->stat_error) != 0) {
362
1.03k
            zip_error_set(&ctx->error, zip_error_code_zip(&ctx->stat_error), zip_error_code_system(&ctx->stat_error));
363
1.03k
            return -1;
364
1.03k
        }
365
366
29.7k
        (void)memcpy_s(data, sizeof(ctx->st), &ctx->st, sizeof(ctx->st));
367
29.7k
        return sizeof(ctx->st);
368
30.8k
    }
369
370
5.53k
    case ZIP_SOURCE_SUPPORTS:
371
5.53k
        return ctx->supports;
372
373
4.60k
    case ZIP_SOURCE_TELL:
374
4.60k
        return (zip_int64_t)ctx->offset;
375
376
8.24k
    case ZIP_SOURCE_TELL_WRITE:
377
8.24k
        return ctx->ops->tell(ctx, ctx->fout);
378
379
32.0k
    case ZIP_SOURCE_WRITE:
380
32.0k
        return ctx->ops->write(ctx, data, len);
381
382
0
    default:
383
0
        zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
384
0
        return -1;
385
341k
    }
386
341k
}