Coverage Report

Created: 2025-11-16 07:22

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