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