/src/libzip/lib/zip_stat_index.c
Line | Count | Source |
1 | | /* |
2 | | zip_stat_index.c -- get information about file by index |
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 | | |
35 | | #include "zipint.h" |
36 | | |
37 | | |
38 | 55.6k | ZIP_EXTERN int zip_stat_index(zip_t *za, zip_uint64_t index, zip_flags_t flags, zip_stat_t *st) { |
39 | 55.6k | const char *name; |
40 | 55.6k | zip_dirent_t *de; |
41 | 55.6k | zip_entry_t *entry; |
42 | | |
43 | 55.6k | if ((de = _zip_get_dirent(za, index, flags, NULL)) == NULL) { |
44 | 0 | return -1; |
45 | 0 | } |
46 | | |
47 | 55.6k | if ((name = zip_get_name(za, index, flags)) == NULL) { |
48 | 0 | return -1; |
49 | 0 | } |
50 | | |
51 | 55.6k | entry = za->entry + index; |
52 | | |
53 | 55.6k | if ((flags & ZIP_FL_UNCHANGED) == 0 && ZIP_ENTRY_DATA_CHANGED(za->entry + index)) { |
54 | 0 | if (zip_source_stat(entry->source, st) < 0) { |
55 | 0 | zip_error_set(&za->error, ZIP_ER_CHANGED, 0); |
56 | 0 | return -1; |
57 | 0 | } |
58 | | |
59 | 0 | if (de->comp_method == ZIP_CM_DEFAULT) { |
60 | 0 | if (!(st->valid & ZIP_STAT_COMP_METHOD) || st->comp_method == ZIP_CM_STORE) { |
61 | 0 | st->valid &= ~(ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD); |
62 | 0 | } |
63 | 0 | } |
64 | 0 | else { |
65 | 0 | if ((st->valid & ZIP_STAT_COMP_METHOD) && st->comp_method != de->comp_method) { |
66 | 0 | st->valid &= ~ZIP_STAT_COMP_SIZE; |
67 | 0 | } |
68 | 0 | st->valid |= ZIP_STAT_COMP_METHOD; |
69 | 0 | st->comp_method = de->comp_method; |
70 | 0 | } |
71 | |
|
72 | 0 | if (((st->valid & (ZIP_STAT_COMP_METHOD | ZIP_STAT_SIZE)) == (ZIP_STAT_COMP_METHOD | ZIP_STAT_SIZE)) && st->comp_method == ZIP_CM_STORE) { |
73 | 0 | st->valid |= ZIP_STAT_COMP_SIZE; |
74 | 0 | st->comp_size = st->size; |
75 | 0 | } |
76 | |
|
77 | 0 | if (entry->changes != NULL && entry->changes->changed & ZIP_DIRENT_LAST_MOD) { |
78 | 0 | st->mtime = zip_dirent_get_last_mod_mtime(de); |
79 | 0 | st->valid |= ZIP_STAT_MTIME; |
80 | 0 | } |
81 | 0 | } |
82 | 55.6k | else { |
83 | 55.6k | zip_stat_init(st); |
84 | | |
85 | 55.6k | st->crc = de->crc; |
86 | 55.6k | st->size = de->uncomp_size; |
87 | 55.6k | st->mtime = zip_dirent_get_last_mod_mtime(de); |
88 | 55.6k | st->comp_size = de->comp_size; |
89 | 55.6k | st->comp_method = (zip_uint16_t)de->comp_method; |
90 | 55.6k | st->encryption_method = de->encryption_method; |
91 | 55.6k | st->valid = (de->crc_valid ? ZIP_STAT_CRC : 0) | ZIP_STAT_SIZE | ZIP_STAT_MTIME | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
92 | 55.6k | if (entry->changes != NULL && entry->changes->changed & ZIP_DIRENT_COMP_METHOD) { |
93 | 0 | st->valid &= ~ZIP_STAT_COMP_SIZE; |
94 | 0 | } |
95 | 55.6k | } |
96 | | |
97 | 55.6k | if ((za->ch_flags & ZIP_AFL_WANT_TORRENTZIP) && (flags & ZIP_FL_UNCHANGED) == 0) { |
98 | 0 | if (za->torrent_mtime == 0) { |
99 | 0 | zip_dostime_t dostime = {0xbc00, 0x2198}; |
100 | 0 | za->torrent_mtime = _zip_d2u_time(&dostime); |
101 | 0 | } |
102 | 0 | st->comp_method = ZIP_CM_DEFLATE; |
103 | 0 | st->mtime = za->torrent_mtime; |
104 | 0 | st->valid |= ZIP_STAT_MTIME | ZIP_STAT_COMP_METHOD; |
105 | 0 | st->valid &= ~ZIP_STAT_COMP_SIZE; |
106 | 0 | } |
107 | | |
108 | 55.6k | st->index = index; |
109 | 55.6k | st->name = name; |
110 | 55.6k | st->valid |= ZIP_STAT_INDEX | ZIP_STAT_NAME; |
111 | | |
112 | 55.6k | return 0; |
113 | 55.6k | } |