Coverage Report

Created: 2026-10-14 06:52

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
8.78k
static void zip_source_file_stat_init(zip_source_file_stat_t *st) {
45
8.78k
    st->size = 0;
46
8.78k
    st->mtime = time(NULL);
47
8.78k
    st->exists = false;
48
8.78k
    st->regular_file = false;
49
8.78k
}
50
51
8.78k
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
8.78k
    zip_source_file_context_t *ctx;
53
8.78k
    zip_source_t *zs;
54
8.78k
    zip_source_file_stat_t sb;
55
8.78k
    zip_uint64_t length;
56
57
8.78k
    if (ops == NULL) {
58
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
59
0
        return NULL;
60
0
    }
61
62
8.78k
    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
8.78k
    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
8.78k
    if (fname != NULL) {
73
8.78k
        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
8.78k
    }
78
0
    else if (file == NULL) {
79
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
80
0
        return NULL;
81
0
    }
82
83
8.78k
    if (len < 0) {
84
8.78k
        if (len == -1) {
85
8.78k
            len = ZIP_LENGTH_TO_END;
86
8.78k
        }
87
        // TODO: return ZIP_ER_INVAL if len != ZIP_LENGTH_UNCHECKED?
88
8.78k
        length = 0;
89
8.78k
    }
90
0
    else {
91
0
        length = (zip_uint64_t)len;
92
0
    }
93
94
8.78k
    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
8.78k
    if ((ctx = zip_source_file_context_new(ops, ops_userdata)) == NULL) {
100
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
101
0
        return NULL;
102
0
    }
103
8.78k
    if (fname) {
104
8.78k
        if ((ctx->fname = ops->string_duplicate(ctx, fname)) == NULL) {
105
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
106
0
            zip_source_file_context_free(ctx);
107
0
            return NULL;
108
0
        }
109
8.78k
    }
110
8.78k
    ctx->f = file;
111
8.78k
    ctx->close_file = false; /* Avoid closing the file on error returns. */
112
8.78k
    ctx->start = start;
113
8.78k
    ctx->len = length;
114
8.78k
    if (st) {
115
0
        (void)memcpy_s(&ctx->st, sizeof(ctx->st), st, sizeof(*st));
116
0
        ctx->st.name = NULL;
117
0
        ctx->st.valid &= ~ZIP_STAT_NAME;
118
0
    }
119
120
8.78k
    if (ctx->len > 0) {
121
0
        ctx->st.size = ctx->len;
122
0
        ctx->st.valid |= ZIP_STAT_SIZE;
123
0
    }
124
125
8.78k
    ctx->supports = ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_SUPPORTS_REOPEN, -1);
126
127
8.78k
    zip_source_file_stat_init(&sb);
128
8.78k
    if (!ops->stat(ctx, &sb)) {
129
0
        _zip_error_copy(error, &ctx->error);
130
0
        zip_source_file_context_free(ctx);
131
0
        return NULL;
132
0
    }
133
134
8.78k
    if (!sb.exists) {
135
0
        if (ctx->fname && ctx->start == 0 && ctx->len == 0 && ops->write != NULL) {
136
0
            ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
137
            /* zip_open_from_source checks for this to detect non-existing files */
138
0
            zip_error_set(&ctx->stat_error, ZIP_ER_READ, ENOENT);
139
0
        }
140
0
        else {
141
0
            zip_error_set(error, ZIP_ER_READ, ENOENT);
142
0
            zip_source_file_context_free(ctx);
143
0
            return NULL;
144
0
        }
145
0
    }
146
8.78k
    else {
147
8.78k
        if ((ctx->st.valid & ZIP_STAT_MTIME) == 0) {
148
8.78k
            ctx->st.mtime = sb.mtime;
149
8.78k
            ctx->st.valid |= ZIP_STAT_MTIME;
150
8.78k
        }
151
8.78k
        if (sb.regular_file) {
152
8.78k
            ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE;
153
154
8.78k
            if (ctx->start + ctx->len > sb.size) {
155
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
156
0
                zip_source_file_context_free(ctx);
157
0
                return NULL;
158
0
            }
159
160
8.78k
            if (ctx->len == 0) {
161
8.78k
                if (len != ZIP_LENGTH_UNCHECKED) {
162
8.78k
                    ctx->len = sb.size - ctx->start;
163
8.78k
                    ctx->st.size = ctx->len;
164
8.78k
                    ctx->st.valid |= ZIP_STAT_SIZE;
165
8.78k
                }
166
167
                /* when using a partial file, don't allow writing */
168
8.78k
                if (ctx->fname && start == 0 && ops->write != NULL) {
169
8.78k
                    ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
170
8.78k
                }
171
8.78k
            }
172
8.78k
        }
173
174
8.78k
        if (ctx->st.valid & ZIP_STAT_SIZE) {
175
8.78k
            ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_AT_EOF);
176
8.78k
        }
177
178
8.78k
        ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_GET_FILE_ATTRIBUTES);
179
8.78k
    }
180
181
8.78k
    ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_ACCEPT_EMPTY);
182
8.78k
    if (ops->create_temp_output_cloning != NULL) {
183
8.78k
        if (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE)) {
184
8.78k
            ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING);
185
8.78k
        }
186
8.78k
    }
187
188
8.78k
    if ((zs = zip_source_function_create(read_file, ctx, error)) == NULL) {
189
0
        zip_source_file_context_free(ctx);
190
0
        return NULL;
191
0
    }
192
193
8.78k
    ctx->close_file = true; /* On success, the file is owned by ctx. */
194
8.78k
    return zs;
195
8.78k
}
196
197
198
156k
static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
199
156k
    zip_source_file_context_t *ctx;
200
156k
    char *buf;
201
202
156k
    ctx = (zip_source_file_context_t *)state;
203
156k
    buf = (char *)data;
204
205
156k
    switch (cmd) {
206
0
    case ZIP_SOURCE_ACCEPT_EMPTY:
207
0
        return 0;
208
209
0
    case ZIP_SOURCE_AT_EOF:
210
        /* We only advertise support for ZIP_SOURCE_AT_EOF if ctx->len is valid. */
211
0
        return ctx->offset == ctx->len;
212
213
0
    case ZIP_SOURCE_BEGIN_WRITE: {
214
0
        zip_int64_t ret;
215
        /* write support should not be set if fname is NULL */
216
0
        if (ctx->fname == NULL) {
217
0
            zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
218
0
            return -1;
219
0
        }
220
0
        ret = ctx->ops->create_temp_output(ctx);
221
0
        if (ret == 0) {
222
            /* Clear past error. Otherwise the error from zip_source_begin_write_cloning() will persist and be reported on zip_source_close(). */
223
0
            zip_error_set(&ctx->error, ZIP_ER_OK, 0);
224
0
        }
225
0
        return ret;
226
0
    }
227
228
0
    case ZIP_SOURCE_BEGIN_WRITE_CLONING:
229
        /* write support should not be set if fname is NULL */
230
0
        if (ctx->fname == NULL) {
231
0
            zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
232
0
            return -1;
233
0
        }
234
0
        return ctx->ops->create_temp_output_cloning(ctx, len);
235
236
8.78k
    case ZIP_SOURCE_CLOSE:
237
8.78k
        if (ctx->fname) {
238
8.78k
            ctx->ops->close(ctx);
239
8.78k
            ctx->f = NULL;
240
8.78k
        }
241
8.78k
        return 0;
242
243
0
    case ZIP_SOURCE_COMMIT_WRITE: {
244
0
        zip_int64_t ret = ctx->ops->commit_write(ctx);
245
0
        ctx->fout = NULL;
246
0
        if (ret == 0) {
247
0
            free(ctx->tmpname);
248
0
            ctx->tmpname = NULL;
249
0
        }
250
0
        return ret;
251
0
    }
252
253
294
    case ZIP_SOURCE_ERROR:
254
294
        return zip_error_to_data(&ctx->error, data, len);
255
256
8.78k
    case ZIP_SOURCE_FREE:
257
8.78k
        zip_source_file_context_free(ctx);
258
8.78k
        return 0;
259
260
6.68k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES:
261
6.68k
        if (len < sizeof(ctx->attributes)) {
262
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
263
0
            return -1;
264
0
        }
265
6.68k
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
266
6.68k
        return sizeof(ctx->attributes);
267
268
8.78k
    case ZIP_SOURCE_OPEN:
269
8.78k
        if (ctx->fname) {
270
8.78k
            if (ctx->ops->open(ctx) == false) {
271
0
                return -1;
272
0
            }
273
8.78k
        }
274
275
8.78k
        if (ctx->start > 0) { /* TODO: rewind on re-open */
276
0
            if (ctx->ops->seek(ctx, ctx->f, (zip_int64_t)ctx->start, SEEK_SET) == false) {
277
                /* TODO: skip by reading */
278
0
                return -1;
279
0
            }
280
0
        }
281
8.78k
        ctx->offset = 0;
282
8.78k
        return 0;
283
284
33.3k
    case ZIP_SOURCE_READ: {
285
33.3k
        zip_int64_t i;
286
33.3k
        zip_uint64_t n;
287
288
33.3k
        if (ctx->len > 0) {
289
33.3k
            n = ZIP_MIN(ctx->len - ctx->offset, len);
290
33.3k
        }
291
0
        else {
292
0
            n = len;
293
0
        }
294
295
33.3k
        if ((i = ctx->ops->read(ctx, buf, n)) < 0) {
296
0
            zip_error_set(&ctx->error, ZIP_ER_READ, errno);
297
0
            return -1;
298
0
        }
299
33.3k
        ctx->offset += (zip_uint64_t)i;
300
301
33.3k
        return i;
302
33.3k
    }
303
304
0
    case ZIP_SOURCE_REMOVE:
305
0
        return ctx->ops->remove(ctx);
306
307
0
    case ZIP_SOURCE_ROLLBACK_WRITE:
308
0
        ctx->ops->rollback_write(ctx);
309
0
        ctx->fout = NULL;
310
0
        free(ctx->tmpname);
311
0
        ctx->tmpname = NULL;
312
0
        return 0;
313
314
29.8k
    case ZIP_SOURCE_SEEK: {
315
29.8k
        zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset, ctx->len, data, len, &ctx->error);
316
317
29.8k
        if (new_offset < 0) {
318
294
            return -1;
319
294
        }
320
321
        /* The actual offset inside the file must be representable as zip_int64_t. */
322
29.5k
        if (new_offset > ZIP_INT64_MAX - (zip_int64_t)ctx->start) {
323
0
            zip_error_set(&ctx->error, ZIP_ER_SEEK, EOVERFLOW);
324
0
            return -1;
325
0
        }
326
327
29.5k
        ctx->offset = (zip_uint64_t)new_offset;
328
329
29.5k
        if (ctx->ops->seek(ctx, ctx->f, (zip_int64_t)(ctx->offset + ctx->start), SEEK_SET) == false) {
330
0
            return -1;
331
0
        }
332
29.5k
        return 0;
333
29.5k
    }
334
335
0
    case ZIP_SOURCE_SEEK_WRITE: {
336
0
        zip_source_args_seek_t *args;
337
338
0
        args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
339
0
        if (args == NULL) {
340
0
            return -1;
341
0
        }
342
343
0
        if (ctx->ops->seek(ctx, ctx->fout, args->offset, args->whence) == false) {
344
0
            return -1;
345
0
        }
346
0
        return 0;
347
0
    }
348
349
42.7k
    case ZIP_SOURCE_STAT: {
350
42.7k
        if (len < sizeof(ctx->st)) {
351
0
            return -1;
352
0
        }
353
354
42.7k
        if (zip_error_code_zip(&ctx->stat_error) != 0) {
355
0
            zip_error_set(&ctx->error, zip_error_code_zip(&ctx->stat_error), zip_error_code_system(&ctx->stat_error));
356
0
            return -1;
357
0
        }
358
359
42.7k
        (void)memcpy_s(data, sizeof(ctx->st), &ctx->st, sizeof(ctx->st));
360
42.7k
        return sizeof(ctx->st);
361
42.7k
    }
362
363
8.78k
    case ZIP_SOURCE_SUPPORTS:
364
8.78k
        return ctx->supports;
365
366
8.65k
    case ZIP_SOURCE_TELL:
367
8.65k
        return (zip_int64_t)ctx->offset;
368
369
0
    case ZIP_SOURCE_TELL_WRITE:
370
0
        return ctx->ops->tell(ctx, ctx->fout);
371
372
0
    case ZIP_SOURCE_WRITE:
373
0
        return ctx->ops->write(ctx, data, len);
374
375
0
    default:
376
0
        zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
377
0
        return -1;
378
156k
    }
379
156k
}
380
381
8.78k
zip_source_file_context_t *zip_source_file_context_new(zip_source_file_operations_t *ops, void *ops_userdata) {
382
8.78k
    zip_source_file_context_t *ctx;
383
384
8.78k
    if ((ctx = (zip_source_file_context_t *)malloc(sizeof(zip_source_file_context_t))) == NULL) {
385
0
        return NULL;
386
0
    }
387
388
8.78k
    zip_error_init(&ctx->error);
389
8.78k
    ctx->supports = 0;
390
8.78k
    ctx->fname = NULL;
391
8.78k
    ctx->f = NULL;
392
8.78k
    ctx->close_file = true;
393
8.78k
    zip_stat_init(&ctx->st);
394
8.78k
    zip_file_attributes_init(&ctx->attributes);
395
8.78k
    zip_error_init(&ctx->stat_error);
396
8.78k
    ctx->start = 0;
397
8.78k
    ctx->len = 0;
398
8.78k
    ctx->offset = 0;
399
8.78k
    ctx->tmpname = NULL;
400
8.78k
    ctx->fout = NULL;
401
8.78k
    ctx->temp_output_created = false;
402
403
8.78k
    ctx->ops = ops;
404
8.78k
    ctx->ops_userdata = ops_userdata;
405
406
8.78k
    return ctx;
407
8.78k
}
408
8.78k
void zip_source_file_context_free(zip_source_file_context_t *ctx) {
409
8.78k
    if (ctx == NULL) {
410
0
        return;
411
0
    }
412
413
8.78k
    if (ctx->f && ctx->close_file) {
414
0
        ctx->ops->close(ctx);
415
0
    }
416
8.78k
    free(ctx->fname);
417
8.78k
    zip_error_fini(&ctx->error);
418
8.78k
    zip_error_fini(&ctx->stat_error);
419
8.78k
    free(ctx->tmpname);
420
8.78k
    free(ctx);
421
8.78k
}