/src/libzip/lib/zip_set_name.c
Line | Count | Source |
1 | | /* |
2 | | zip_set_name.c -- rename helper function |
3 | | Copyright (C) 1999-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 | | |
41 | 1.03k | int _zip_set_name(zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags) { |
42 | 1.03k | zip_entry_t *e; |
43 | 1.03k | zip_string_t *str; |
44 | 1.03k | bool same_as_orig; |
45 | 1.03k | zip_int64_t i; |
46 | 1.03k | const zip_uint8_t *old_name, *new_name; |
47 | 1.03k | zip_string_t *old_str; |
48 | | |
49 | 1.03k | if (idx >= za->nentry) { |
50 | 0 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
51 | 0 | return -1; |
52 | 0 | } |
53 | | |
54 | 1.03k | if (ZIP_IS_RDONLY(za)) { |
55 | 0 | zip_error_set(&za->error, ZIP_ER_RDONLY, 0); |
56 | 0 | return -1; |
57 | 0 | } |
58 | | |
59 | 1.03k | if (name && name[0] != '\0') { |
60 | | /* TODO: check for string too long */ |
61 | 1.03k | if ((str = _zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL) { |
62 | 0 | return -1; |
63 | 0 | } |
64 | 1.03k | if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(str, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED) { |
65 | 0 | str->encoding = ZIP_ENCODING_UTF8_KNOWN; |
66 | 0 | } |
67 | 1.03k | } |
68 | 0 | else { |
69 | 0 | str = NULL; |
70 | 0 | } |
71 | | |
72 | | /* TODO: encoding flags needed for CP437? */ |
73 | 1.03k | if ((i = _zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) { |
74 | 0 | _zip_string_free(str); |
75 | 0 | zip_error_set(&za->error, ZIP_ER_EXISTS, 0); |
76 | 0 | return -1; |
77 | 0 | } |
78 | | |
79 | | /* no effective name change */ |
80 | 1.03k | if (i >= 0 && (zip_uint64_t)i == idx) { |
81 | 0 | _zip_string_free(str); |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | 1.03k | e = za->entry + idx; |
86 | | |
87 | 1.03k | if (e->orig) { |
88 | 0 | same_as_orig = _zip_string_equal(e->orig->filename, str); |
89 | 0 | } |
90 | 1.03k | else { |
91 | 1.03k | same_as_orig = false; |
92 | 1.03k | } |
93 | | |
94 | 1.03k | if (!same_as_orig && e->changes == NULL) { |
95 | 1.03k | if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { |
96 | 0 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
97 | 0 | _zip_string_free(str); |
98 | 0 | return -1; |
99 | 0 | } |
100 | 1.03k | } |
101 | | |
102 | 1.03k | if ((new_name = _zip_string_get(same_as_orig ? e->orig->filename : str, NULL, 0, &za->error)) == NULL) { |
103 | 0 | _zip_string_free(str); |
104 | 0 | return -1; |
105 | 0 | } |
106 | | |
107 | 1.03k | if (e->changes) { |
108 | 1.03k | old_str = e->changes->filename; |
109 | 1.03k | } |
110 | 0 | else if (e->orig) { |
111 | 0 | old_str = e->orig->filename; |
112 | 0 | } |
113 | 0 | else { |
114 | 0 | old_str = NULL; |
115 | 0 | } |
116 | | |
117 | 1.03k | if (old_str) { |
118 | 0 | if ((old_name = _zip_string_get(old_str, NULL, 0, &za->error)) == NULL) { |
119 | 0 | _zip_string_free(str); |
120 | 0 | return -1; |
121 | 0 | } |
122 | 0 | } |
123 | 1.03k | else { |
124 | 1.03k | old_name = NULL; |
125 | 1.03k | } |
126 | | |
127 | 1.03k | if (_zip_hash_add(za->names, new_name, idx, 0, &za->error) == false) { |
128 | 0 | _zip_string_free(str); |
129 | 0 | return -1; |
130 | 0 | } |
131 | 1.03k | if (old_name) { |
132 | 0 | _zip_hash_delete(za->names, old_name, NULL); |
133 | 0 | } |
134 | | |
135 | 1.03k | if (same_as_orig) { |
136 | 0 | if (e->changes) { |
137 | 0 | if (e->changes->changed & ZIP_DIRENT_FILENAME) { |
138 | 0 | _zip_string_free(e->changes->filename); |
139 | 0 | e->changes->changed &= ~ZIP_DIRENT_FILENAME; |
140 | 0 | if (e->changes->changed == 0) { |
141 | 0 | _zip_dirent_free(e->changes); |
142 | 0 | e->changes = NULL; |
143 | 0 | } |
144 | 0 | else { |
145 | | /* TODO: what if not cloned? can that happen? */ |
146 | 0 | e->changes->filename = e->orig->filename; |
147 | 0 | } |
148 | 0 | } |
149 | 0 | } |
150 | 0 | _zip_string_free(str); |
151 | 0 | } |
152 | 1.03k | else { |
153 | 1.03k | if (e->changes->changed & ZIP_DIRENT_FILENAME) { |
154 | 0 | _zip_string_free(e->changes->filename); |
155 | 0 | } |
156 | 1.03k | e->changes->changed |= ZIP_DIRENT_FILENAME; |
157 | 1.03k | e->changes->filename = str; |
158 | 1.03k | } |
159 | | |
160 | 1.03k | return 0; |
161 | 1.03k | } |