Coverage Report

Created: 2025-07-11 06:24

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