Coverage Report

Created: 2025-12-03 06:06

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-2024 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
ZIP_EXTERN zip_source_t *
64
0
zip_source_window_create(zip_source_t *src, zip_uint64_t start, zip_int64_t len, zip_error_t *error) {
65
0
    return _zip_source_window_new(src, start, len, NULL, 0, NULL, NULL, NULL, 0, false, error);
66
0
}
67
68
69
zip_source_t *
70
47.3k
_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) {
71
47.3k
    zip_source_t* window_source;
72
47.3k
    struct window *ctx;
73
74
47.3k
    if (src == NULL || length < -1 || (source_archive == NULL && source_index != 0)) {
75
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
76
0
        return NULL;
77
0
    }
78
    
79
47.3k
    if (length >= 0) {
80
47.3k
        if (start + (zip_uint64_t)length < start) {
81
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
82
0
            return NULL;
83
0
        }
84
47.3k
    }
85
86
47.3k
    if ((ctx = (struct window *)malloc(sizeof(*ctx))) == NULL) {
87
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
88
0
        return NULL;
89
0
    }
90
91
47.3k
    ctx->start = start;
92
47.3k
    if (length == -1) {
93
0
        ctx->end_valid = false;
94
0
    }
95
47.3k
    else {
96
47.3k
        ctx->end = start + (zip_uint64_t)length;
97
47.3k
        ctx->end_valid = true;
98
47.3k
    }
99
47.3k
    zip_stat_init(&ctx->stat);
100
47.3k
    ctx->stat_invalid = st_invalid;
101
47.3k
    if (attributes != NULL) {
102
47.2k
        (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes));
103
47.2k
    }
104
120
    else {
105
120
        zip_file_attributes_init(&ctx->attributes);
106
120
    }
107
47.3k
    if (dostime != NULL) {
108
47.2k
        ctx->dostime = *dostime;
109
47.2k
        ctx->dostime_valid = true;
110
47.2k
    }
111
120
    else {
112
120
        ctx->dostime_valid = false;
113
120
    }
114
47.3k
    ctx->source_archive = source_archive;
115
47.3k
    ctx->source_index = source_index;
116
47.3k
    zip_error_init(&ctx->error);
117
47.3k
    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));
118
47.3k
    ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false;
119
120
47.3k
    if (st) {
121
47.3k
        if (_zip_stat_merge(&ctx->stat, st, error) < 0) {
122
0
            free(ctx);
123
0
            return NULL;
124
0
        }
125
47.3k
    }
126
    
127
47.3k
    window_source = zip_source_layered_create(src, window_read, ctx, error);
128
47.3k
    if (window_source == NULL) {
129
0
        free(ctx);
130
0
        return NULL;
131
0
    }
132
47.3k
    if (!take_ownership) {
133
41.2k
        zip_source_keep(src);
134
41.2k
    }
135
47.3k
    return window_source;
136
47.3k
}
137
138
139
int
140
47.2k
_zip_source_set_source_archive(zip_source_t *src, zip_t *za) {
141
47.2k
    src->source_archive = za;
142
47.2k
    return _zip_register_source(za, src);
143
47.2k
}
144
145
146
/* called by zip_discard to avoid operating on file from closed archive */
147
void
148
0
_zip_source_invalidate(zip_source_t *src) {
149
0
    src->source_closed = 1;
150
151
0
    if (zip_error_code_zip(&src->error) == ZIP_ER_OK) {
152
0
        zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0);
153
0
    }
154
0
}
155
156
157
static zip_int64_t
158
338k
window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
159
338k
    struct window *ctx;
160
338k
    zip_int64_t ret;
161
338k
    zip_uint64_t n, i;
162
163
338k
    ctx = (struct window *)_ctx;
164
165
338k
    switch (cmd) {
166
35.3k
    case ZIP_SOURCE_CLOSE:
167
35.3k
        return 0;
168
169
17.2k
    case ZIP_SOURCE_ERROR:
170
17.2k
        return zip_error_to_data(&ctx->error, data, len);
171
172
47.3k
    case ZIP_SOURCE_FREE:
173
47.3k
        free(ctx);
174
47.3k
        return 0;
175
176
41.0k
    case ZIP_SOURCE_OPEN:
177
41.0k
        if (ctx->source_archive) {
178
35.8k
            zip_uint64_t offset;
179
180
35.8k
            if ((offset = _zip_file_get_offset(ctx->source_archive, ctx->source_index, &ctx->error)) == 0) {
181
5.66k
                return -1;
182
5.66k
            }
183
30.1k
            if (ctx->end + offset < ctx->end) {
184
                /* zip archive data claims end of data past zip64 limits */
185
0
                zip_error_set(&ctx->error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_CDIR_ENTRY_INVALID, ctx->source_index));
186
0
                return -1;
187
0
            }
188
30.1k
            ctx->start += offset;
189
30.1k
            ctx->end += offset;
190
30.1k
            ctx->source_archive = NULL;
191
30.1k
        }
192
193
35.3k
        if (!ctx->needs_seek) {
194
0
            DEFINE_BYTE_ARRAY(b, BUFSIZE);
195
196
0
            if (!byte_array_init(b, BUFSIZE)) {
197
0
                zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
198
0
                return -1;
199
0
            }
200
201
0
            for (n = 0; n < ctx->start; n += (zip_uint64_t)ret) {
202
0
                i = (ctx->start - n > BUFSIZE ? BUFSIZE : ctx->start - n);
203
0
                if ((ret = zip_source_read(src, b, i)) < 0) {
204
0
                    zip_error_set_from_source(&ctx->error, src);
205
0
                    byte_array_fini(b);
206
0
                    return -1;
207
0
                }
208
0
                if (ret == 0) {
209
0
                    zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
210
0
                    byte_array_fini(b);
211
0
                    return -1;
212
0
                }
213
0
            }
214
215
0
            byte_array_fini(b);
216
0
        }
217
218
35.3k
        ctx->offset = ctx->start;
219
35.3k
        return 0;
220
221
72.9k
    case ZIP_SOURCE_READ:
222
72.9k
        if (ctx->end_valid && len > ctx->end - ctx->offset) {
223
38.3k
            len = ctx->end - ctx->offset;
224
38.3k
        }
225
226
72.9k
        if (len == 0) {
227
20.1k
            return 0;
228
20.1k
        }
229
230
52.7k
        if (ctx->needs_seek) {
231
52.7k
            if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) {
232
2.86k
                zip_error_set_from_source(&ctx->error, src);
233
2.86k
                return -1;
234
2.86k
            }
235
52.7k
        }
236
237
49.9k
        if ((ret = zip_source_read(src, data, len)) < 0) {
238
0
            zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
239
0
            return -1;
240
0
        }
241
242
49.9k
        ctx->offset += (zip_uint64_t)ret;
243
244
49.9k
        if (ret == 0) {
245
8.72k
            if (ctx->end_valid && ctx->offset < ctx->end) {
246
8.72k
                zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
247
8.72k
                return -1;
248
8.72k
            }
249
8.72k
        }
250
41.2k
        return ret;
251
252
0
    case ZIP_SOURCE_SEEK: {
253
0
        zip_int64_t new_offset;
254
        
255
0
        if (!ctx->end_valid) {
256
0
            zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
257
            
258
0
            if (args == NULL) {
259
0
                return -1;
260
0
            }
261
0
            if (args->whence == SEEK_END) {
262
0
                if (zip_source_seek(src, args->offset, args->whence) < 0) {
263
0
                    zip_error_set_from_source(&ctx->error, src);
264
0
                    return -1;
265
0
                }
266
0
                new_offset = zip_source_tell(src);
267
0
                if (new_offset < 0) {
268
0
                    zip_error_set_from_source(&ctx->error, src);
269
0
                    return -1;
270
0
                }
271
0
                if ((zip_uint64_t)new_offset < ctx->start) {
272
0
                    zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
273
0
                    (void)zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET);
274
0
                    return -1;
275
0
                }
276
0
                ctx->offset = (zip_uint64_t)new_offset;
277
0
                return 0;
278
0
            }
279
0
        }
280
281
0
        new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, ctx->end - ctx->start, data, len, &ctx->error);
282
        
283
0
        if (new_offset < 0) {
284
0
            return -1;
285
0
        }
286
        
287
0
        ctx->offset = (zip_uint64_t)new_offset + ctx->start;
288
0
        return 0;
289
0
    }
290
291
44.1k
    case ZIP_SOURCE_STAT: {
292
44.1k
        zip_stat_t *st;
293
294
44.1k
        st = (zip_stat_t *)data;
295
296
44.1k
        if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) {
297
0
            return -1;
298
0
        }
299
300
44.1k
        if (!(ctx->stat.valid & ZIP_STAT_SIZE)) {
301
0
            if (ctx->end_valid) {
302
0
                st->valid |= ZIP_STAT_SIZE;
303
0
                st->size = ctx->end - ctx->start;
304
0
            }
305
0
            else if (st->valid & ZIP_STAT_SIZE) {
306
0
                st->size -= ctx->start;
307
0
            }
308
0
        }
309
310
44.1k
        st->valid &= ~ctx->stat_invalid;
311
312
44.1k
        return 0;
313
44.1k
    }
314
315
29.2k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES:
316
29.2k
        if (len < sizeof(ctx->attributes)) {
317
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
318
0
            return -1;
319
0
        }
320
321
29.2k
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
322
29.2k
        return sizeof(ctx->attributes);
323
324
3.99k
    case ZIP_SOURCE_GET_DOS_TIME:
325
3.99k
        if (len < sizeof(ctx->dostime)) {
326
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
327
0
            return -1;
328
0
        }
329
3.99k
        if (ctx->dostime_valid) {
330
3.99k
            (void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime));
331
3.99k
            return sizeof(ctx->dostime);
332
3.99k
        }
333
0
        else {
334
0
            return 0;
335
0
        }
336
337
47.3k
    case ZIP_SOURCE_SUPPORTS:
338
47.3k
        return ctx->supports;
339
340
0
    case ZIP_SOURCE_TELL:
341
0
        return (zip_int64_t)(ctx->offset - ctx->start);
342
343
0
    default:
344
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
345
338k
    }
346
338k
}
347
348
349
void
350
47.2k
_zip_deregister_source(zip_t *za, zip_source_t *src) {
351
47.2k
    zip_uint64_t i;
352
353
47.2k
    for (i = 0; i < za->nopen_source; i++) {
354
47.2k
        if (za->open_source[i] == src) {
355
47.2k
            za->open_source[i] = za->open_source[za->nopen_source - 1];
356
47.2k
            za->nopen_source--;
357
47.2k
            break;
358
47.2k
        }
359
47.2k
    }
360
47.2k
}
361
362
363
int
364
47.2k
_zip_register_source(zip_t *za, zip_source_t *src) {
365
47.2k
    if (za->nopen_source + 1 >= za->nopen_source_alloc) {
366
2.89k
        if (!ZIP_REALLOC(za->open_source, za->nopen_source_alloc, 10, &za->error)) {
367
0
            return -1;
368
0
        }
369
2.89k
    }
370
371
47.2k
    za->open_source[za->nopen_source++] = src;
372
373
47.2k
    return 0;
374
47.2k
}