Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_window.c
Line
Count
Source
1
/*
2
  zip_source_window.c -- return part of lower source
3
  Copyright (C) 2012-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 <stdlib.h>
37
#include <string.h>
38
39
struct window {
40
    zip_uint64_t start; /* where in file we start reading */
41
    zip_uint64_t end;   /* where in file we stop reading */
42
    bool end_valid;     /* whether end is set, otherwise read until EOF */
43
44
    /* if not NULL, read file data for this file */
45
    zip_t *source_archive;
46
    zip_uint64_t source_index;
47
48
    zip_uint64_t offset; /* offset in src for next read */
49
50
    zip_stat_t stat;
51
    zip_uint64_t stat_invalid;
52
    zip_file_attributes_t attributes;
53
    zip_dostime_t dostime;
54
    bool dostime_valid;
55
    zip_error_t error;
56
    zip_int64_t supports;
57
    bool needs_seek;
58
};
59
60
static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
61
62
63
0
ZIP_EXTERN zip_source_t *zip_source_window_create(zip_source_t *src, zip_uint64_t start, zip_int64_t len, zip_error_t *error) {
64
0
    return _zip_source_window_new(src, start, len, NULL, 0, NULL, NULL, NULL, 0, false, error);
65
0
}
66
67
68
11.4k
zip_source_t *_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length, zip_stat_t *st, zip_uint64_t st_invalid, zip_file_attributes_t *attributes, zip_dostime_t *dostime, zip_t *source_archive, zip_uint64_t source_index, bool take_ownership, zip_error_t *error) {
69
11.4k
    zip_source_t *window_source;
70
11.4k
    struct window *ctx;
71
72
11.4k
    if (src == NULL || length < -1 || (source_archive == NULL && source_index != 0)) {
73
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
74
0
        return NULL;
75
0
    }
76
77
11.4k
    if (length >= 0) {
78
11.4k
        if (start + (zip_uint64_t)length < start) {
79
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
80
0
            return NULL;
81
0
        }
82
11.4k
    }
83
84
11.4k
    if ((ctx = (struct window *)malloc(sizeof(*ctx))) == NULL) {
85
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
86
0
        return NULL;
87
0
    }
88
89
11.4k
    ctx->start = start;
90
11.4k
    if (length == -1) {
91
0
        ctx->end_valid = false;
92
0
    }
93
11.4k
    else {
94
11.4k
        ctx->end = start + (zip_uint64_t)length;
95
11.4k
        ctx->end_valid = true;
96
11.4k
    }
97
11.4k
    zip_stat_init(&ctx->stat);
98
11.4k
    ctx->stat_invalid = st_invalid;
99
11.4k
    if (attributes != NULL) {
100
11.3k
        (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes));
101
11.3k
    }
102
31
    else {
103
31
        zip_file_attributes_init(&ctx->attributes);
104
31
    }
105
11.4k
    if (dostime != NULL) {
106
11.3k
        ctx->dostime = *dostime;
107
11.3k
        ctx->dostime_valid = true;
108
11.3k
    }
109
31
    else {
110
31
        ctx->dostime_valid = false;
111
31
    }
112
11.4k
    ctx->source_archive = source_archive;
113
11.4k
    ctx->source_index = source_index;
114
11.4k
    zip_error_init(&ctx->error);
115
11.4k
    ctx->supports = (zip_source_supports(src) & (ZIP_SOURCE_SUPPORTS_SEEKABLE | ZIP_SOURCE_SUPPORTS_REOPEN)) | (zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_GET_DOS_TIME, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_FREE, -1));
116
11.4k
    if (ctx->end_valid) {
117
11.4k
        ctx->supports |= ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_AT_EOF);
118
11.4k
    }
119
11.4k
    ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false;
120
121
11.4k
    if (st) {
122
11.4k
        if (_zip_stat_merge(&ctx->stat, st, error) < 0) {
123
0
            free(ctx);
124
0
            return NULL;
125
0
        }
126
11.4k
    }
127
128
11.4k
    window_source = zip_source_layered_create(src, window_read, ctx, error);
129
11.4k
    if (window_source == NULL) {
130
0
        free(ctx);
131
0
        return NULL;
132
0
    }
133
11.4k
    if (!take_ownership) {
134
11.2k
        zip_source_keep(src);
135
11.2k
    }
136
11.4k
    return window_source;
137
11.4k
}
138
139
140
11.3k
int _zip_source_set_source_archive(zip_source_t *src, zip_t *za) {
141
11.3k
    src->source_archive = za;
142
11.3k
    return _zip_register_source(za, src);
143
11.3k
}
144
145
146
/* called by zip_discard to avoid operating on file from closed archive */
147
0
void _zip_source_invalidate(zip_source_t *src) {
148
0
    src->source_closed = 1;
149
150
0
    if (zip_error_code_zip(&src->error) == ZIP_ER_OK) {
151
0
        zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0);
152
0
    }
153
0
}
154
155
156
93.0k
static zip_int64_t window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
157
93.0k
    struct window *ctx;
158
93.0k
    zip_int64_t ret;
159
93.0k
    zip_uint64_t n, i;
160
161
93.0k
    ctx = (struct window *)_ctx;
162
163
93.0k
    switch (cmd) {
164
1.67k
    case ZIP_SOURCE_AT_EOF:
165
1.67k
        if (ctx->end_valid) {
166
1.67k
            return ctx->offset == ctx->end;
167
1.67k
        }
168
0
        else {
169
0
            return zip_source_pass_to_lower_layer(src, data, len, cmd);
170
0
        }
171
172
9.82k
    case ZIP_SOURCE_CLOSE:
173
9.82k
        return 0;
174
175
3.36k
    case ZIP_SOURCE_ERROR:
176
3.36k
        return zip_error_to_data(&ctx->error, data, len);
177
178
11.4k
    case ZIP_SOURCE_FREE:
179
11.4k
        free(ctx);
180
11.4k
        return 0;
181
182
10.9k
    case ZIP_SOURCE_OPEN:
183
10.9k
        if (ctx->source_archive) {
184
10.8k
            zip_uint64_t offset;
185
186
10.8k
            if ((offset = _zip_file_get_offset(ctx->source_archive, ctx->source_index, &ctx->error)) == 0) {
187
1.15k
                return -1;
188
1.15k
            }
189
9.69k
            if (ctx->end_valid) {
190
9.69k
                if (ctx->end + offset < ctx->end) {
191
                    /* zip archive data claims end of data past zip64 limits */
192
0
                    zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_CDIR_ENTRY_INVALID, ctx->source_index));
193
0
                    return -1;
194
0
                }
195
9.69k
                ctx->end += offset;
196
9.69k
            }
197
9.69k
            ctx->start += offset;
198
9.69k
            ctx->source_archive = NULL;
199
9.69k
        }
200
201
9.82k
        if (!ctx->needs_seek) {
202
0
            DEFINE_BYTE_ARRAY(b, BUFSIZE);
203
204
0
            if (!byte_array_init(b, BUFSIZE)) {
205
0
                zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
206
0
                return -1;
207
0
            }
208
209
0
            for (n = 0; n < ctx->start; n += (zip_uint64_t)ret) {
210
0
                i = (ctx->start - n > BUFSIZE ? BUFSIZE : ctx->start - n);
211
0
                if ((ret = zip_source_read(src, b, i)) < 0) {
212
0
                    zip_error_set_from_source(&ctx->error, src);
213
0
                    byte_array_fini(b);
214
0
                    return -1;
215
0
                }
216
0
                if (ret == 0) {
217
0
                    zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
218
0
                    byte_array_fini(b);
219
0
                    return -1;
220
0
                }
221
0
            }
222
223
0
            byte_array_fini(b);
224
0
        }
225
226
9.82k
        ctx->offset = ctx->start;
227
9.82k
        return 0;
228
229
21.2k
    case ZIP_SOURCE_READ:
230
21.2k
        if (ctx->end_valid && len > ctx->end - ctx->offset) {
231
12.6k
            len = ctx->end - ctx->offset;
232
12.6k
        }
233
234
21.2k
        if (len == 0) {
235
6.00k
            return 0;
236
6.00k
        }
237
238
15.2k
        if (ctx->needs_seek) {
239
15.2k
            if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) {
240
255
                zip_error_set_from_source(&ctx->error, src);
241
255
                return -1;
242
255
            }
243
15.2k
        }
244
245
15.0k
        if ((ret = zip_source_read(src, data, len)) < 0) {
246
0
            zip_error_set_from_source(&ctx->error, src);
247
0
            return -1;
248
0
        }
249
250
15.0k
        if (ret == 0) {
251
1.95k
            if (ctx->end_valid && ctx->offset < ctx->end) {
252
1.95k
                zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
253
1.95k
                return -1;
254
1.95k
            }
255
1.95k
        }
256
257
13.0k
        ctx->offset += (zip_uint64_t)ret;
258
13.0k
        return ret;
259
260
0
    case ZIP_SOURCE_SEEK: {
261
0
        zip_uint64_t length;
262
0
        zip_int64_t new_offset;
263
0
        zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
264
265
        /*
266
          If we don't know the length and seek from the end, we seek in the lower source to get the new offset.
267
        */
268
0
        if (!ctx->end_valid && args->whence == SEEK_END) {
269
0
            zip_int64_t lower_offset;
270
271
0
            if (zip_source_seek(src, args->offset, args->whence) < 0) {
272
0
                zip_error_set_from_source(&ctx->error, src);
273
0
                return -1;
274
0
            }
275
0
            if ((lower_offset = zip_source_tell(src)) < 0) {
276
0
                zip_error_set_from_source(&ctx->error, src);
277
0
                return -1;
278
0
            }
279
0
            if ((zip_uint64_t)lower_offset < ctx->start) {
280
0
                zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
281
0
                return -1;
282
0
            }
283
0
            ctx->offset = (zip_uint64_t)lower_offset;
284
0
            return 0;
285
0
        }
286
287
        /* 
288
          If we don't know the length, we disable the end check in zip_source_seek_compute_offset by passing in the largest possible value and seek in the lower source to validate the new offset.
289
        */
290
0
        length = ctx->end_valid ? (ctx->end - ctx->start) : ZIP_INT64_MAX;
291
0
        new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, length, data, len, &ctx->error);
292
293
0
        if (new_offset < 0) {
294
0
            return -1;
295
0
        }
296
297
0
        if (new_offset + ctx->start < ctx->start) {
298
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
299
0
            return -1;
300
0
        }
301
0
        new_offset += ctx->start;
302
303
0
        if (!ctx->end_valid) {
304
0
            if (zip_source_seek(src, new_offset, SEEK_SET) < 0) {
305
0
                zip_error_set_from_source(&ctx->error, src);
306
0
                return -1;
307
0
            }
308
0
        }
309
0
        ctx->offset = (zip_uint64_t)new_offset;
310
0
        return 0;
311
0
    }
312
313
15.3k
    case ZIP_SOURCE_STAT: {
314
15.3k
        zip_stat_t *st;
315
316
15.3k
        st = (zip_stat_t *)data;
317
318
15.3k
        if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) {
319
0
            return -1;
320
0
        }
321
322
15.3k
        if (!(ctx->stat.valid & ZIP_STAT_SIZE)) {
323
0
            if (ctx->end_valid) {
324
0
                st->valid |= ZIP_STAT_SIZE;
325
0
                st->size = ctx->end - ctx->start;
326
0
            }
327
0
            else if (st->valid & ZIP_STAT_SIZE) {
328
0
                st->size -= ctx->start;
329
0
            }
330
0
        }
331
332
15.3k
        st->valid &= ~ctx->stat_invalid;
333
334
15.3k
        return 0;
335
15.3k
    }
336
337
7.81k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES:
338
7.81k
        if (len < sizeof(ctx->attributes)) {
339
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
340
0
            return -1;
341
0
        }
342
343
7.81k
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
344
7.81k
        return sizeof(ctx->attributes);
345
346
0
    case ZIP_SOURCE_GET_DOS_TIME:
347
0
        if (len < sizeof(ctx->dostime)) {
348
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
349
0
            return -1;
350
0
        }
351
0
        if (ctx->dostime_valid) {
352
0
            (void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime));
353
0
            return sizeof(ctx->dostime);
354
0
        }
355
0
        else {
356
0
            return 0;
357
0
        }
358
359
11.4k
    case ZIP_SOURCE_SUPPORTS:
360
11.4k
        return ctx->supports;
361
362
0
    case ZIP_SOURCE_TELL:
363
0
        return (zip_int64_t)(ctx->offset - ctx->start);
364
365
0
    default:
366
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
367
93.0k
    }
368
93.0k
}
369
370
371
11.3k
void _zip_deregister_source(zip_t *za, zip_source_t *src) {
372
11.3k
    zip_uint64_t i;
373
374
11.3k
    for (i = 0; i < za->nopen_source; i++) {
375
11.3k
        if (za->open_source[i] == src) {
376
11.3k
            za->open_source[i] = za->open_source[za->nopen_source - 1];
377
11.3k
            za->nopen_source--;
378
11.3k
            break;
379
11.3k
        }
380
11.3k
    }
381
11.3k
}
382
383
384
11.3k
int _zip_register_source(zip_t *za, zip_source_t *src) {
385
11.3k
    if (za->nopen_source + 1 >= za->nopen_source_alloc) {
386
4.62k
        if (!ZIP_REALLOC(za->open_source, za->nopen_source_alloc, 10, &za->error)) {
387
0
            return -1;
388
0
        }
389
4.62k
    }
390
391
11.3k
    za->open_source[za->nopen_source++] = src;
392
393
11.3k
    return 0;
394
11.3k
}