/src/libzip/lib/zip_progress.c
Line | Count | Source |
1 | | /* |
2 | | zip_progress.c -- progress reporting |
3 | | Copyright (C) 2017-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 <stdlib.h> |
36 | | |
37 | | |
38 | | #define _ZIP_COMPILING_DEPRECATED |
39 | | #include "zipint.h" |
40 | | |
41 | | struct zip_progress { |
42 | | zip_t *za; |
43 | | |
44 | | zip_progress_callback callback_progress; |
45 | | void (*ud_progress_free)(void *); |
46 | | void *ud_progress; |
47 | | |
48 | | zip_cancel_callback callback_cancel; |
49 | | void (*ud_cancel_free)(void *); |
50 | | void *ud_cancel; |
51 | | |
52 | | double precision; |
53 | | |
54 | | /* state */ |
55 | | double last_update; /* last value callback function was called with */ |
56 | | |
57 | | double start; /* start of sub-progress section */ |
58 | | double end; /* end of sub-progress section */ |
59 | | }; |
60 | | |
61 | | static void _zip_progress_free_cancel_callback(zip_progress_t *progress); |
62 | | static void _zip_progress_free_progress_callback(zip_progress_t *progress); |
63 | | static zip_progress_t *_zip_progress_new(zip_t *za); |
64 | | static void _zip_progress_set_cancel_callback(zip_progress_t *progress, zip_cancel_callback callback, void (*ud_free)(void *), void *ud); |
65 | | static void _zip_progress_set_progress_callback(zip_progress_t *progress, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud); |
66 | | |
67 | 0 | void _zip_progress_end(zip_progress_t *progress) { |
68 | 0 | _zip_progress_update(progress, 1.0); |
69 | 0 | } |
70 | | |
71 | | |
72 | 0 | void _zip_progress_free(zip_progress_t *progress) { |
73 | 0 | if (progress == NULL) { |
74 | 0 | return; |
75 | 0 | } |
76 | | |
77 | 0 | _zip_progress_free_progress_callback(progress); |
78 | 0 | _zip_progress_free_cancel_callback(progress); |
79 | |
|
80 | 0 | free(progress); |
81 | 0 | } |
82 | | |
83 | | |
84 | 0 | static zip_progress_t *_zip_progress_new(zip_t *za) { |
85 | 0 | zip_progress_t *progress = (zip_progress_t *)malloc(sizeof(*progress)); |
86 | |
|
87 | 0 | if (progress == NULL) { |
88 | 0 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | 0 | progress->za = za; |
93 | |
|
94 | 0 | progress->callback_progress = NULL; |
95 | 0 | progress->ud_progress_free = NULL; |
96 | 0 | progress->ud_progress = NULL; |
97 | 0 | progress->precision = 0.0; |
98 | |
|
99 | 0 | progress->callback_cancel = NULL; |
100 | 0 | progress->ud_cancel_free = NULL; |
101 | 0 | progress->ud_cancel = NULL; |
102 | |
|
103 | 0 | return progress; |
104 | 0 | } |
105 | | |
106 | 0 | static void _zip_progress_free_progress_callback(zip_progress_t *progress) { |
107 | 0 | if (progress->ud_progress_free) { |
108 | 0 | progress->ud_progress_free(progress->ud_progress); |
109 | 0 | } |
110 | |
|
111 | 0 | progress->callback_progress = NULL; |
112 | 0 | progress->ud_progress = NULL; |
113 | 0 | progress->ud_progress_free = NULL; |
114 | 0 | } |
115 | | |
116 | 0 | static void _zip_progress_free_cancel_callback(zip_progress_t *progress) { |
117 | 0 | if (progress->ud_cancel_free) { |
118 | 0 | progress->ud_cancel_free(progress->ud_cancel); |
119 | 0 | } |
120 | |
|
121 | 0 | progress->callback_cancel = NULL; |
122 | 0 | progress->ud_cancel = NULL; |
123 | 0 | progress->ud_cancel_free = NULL; |
124 | 0 | } |
125 | | |
126 | 0 | static void _zip_progress_set_progress_callback(zip_progress_t *progress, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) { |
127 | 0 | _zip_progress_free_progress_callback(progress); |
128 | |
|
129 | 0 | progress->callback_progress = callback; |
130 | 0 | progress->ud_progress_free = ud_free; |
131 | 0 | progress->ud_progress = ud; |
132 | 0 | progress->precision = precision; |
133 | 0 | } |
134 | | |
135 | 0 | void _zip_progress_set_cancel_callback(zip_progress_t *progress, zip_cancel_callback callback, void (*ud_free)(void *), void *ud) { |
136 | 0 | _zip_progress_free_cancel_callback(progress); |
137 | |
|
138 | 0 | progress->callback_cancel = callback; |
139 | 0 | progress->ud_cancel_free = ud_free; |
140 | 0 | progress->ud_cancel = ud; |
141 | 0 | } |
142 | | |
143 | 0 | int _zip_progress_start(zip_progress_t *progress) { |
144 | 0 | if (progress == NULL) { |
145 | 0 | return 0; |
146 | 0 | } |
147 | | |
148 | 0 | if (progress->callback_progress != NULL) { |
149 | 0 | progress->last_update = 0.0; |
150 | 0 | progress->callback_progress(progress->za, 0.0, progress->ud_progress); |
151 | 0 | } |
152 | |
|
153 | 0 | if (progress->callback_cancel != NULL) { |
154 | 0 | if (progress->callback_cancel(progress->za, progress->ud_cancel)) { |
155 | 0 | return -1; |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | 0 | return 0; |
160 | 0 | } |
161 | | |
162 | | |
163 | 0 | int _zip_progress_subrange(zip_progress_t *progress, double start, double end) { |
164 | 0 | if (progress == NULL) { |
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | 0 | progress->start = start; |
169 | 0 | progress->end = end; |
170 | |
|
171 | 0 | return _zip_progress_update(progress, 0.0); |
172 | 0 | } |
173 | | |
174 | 0 | int _zip_progress_update(zip_progress_t *progress, double sub_current) { |
175 | 0 | double current; |
176 | |
|
177 | 0 | if (progress == NULL) { |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | 0 | if (progress->callback_progress != NULL) { |
182 | 0 | current = ZIP_MIN(ZIP_MAX(sub_current, 0.0), 1.0) * (progress->end - progress->start) + progress->start; |
183 | |
|
184 | 0 | if (current - progress->last_update > progress->precision || (progress->last_update < 1 && current == 1)) { |
185 | 0 | progress->callback_progress(progress->za, current, progress->ud_progress); |
186 | 0 | progress->last_update = current; |
187 | 0 | } |
188 | 0 | } |
189 | |
|
190 | 0 | if (progress->callback_cancel != NULL) { |
191 | 0 | if (progress->callback_cancel(progress->za, progress->ud_cancel)) { |
192 | 0 | return -1; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | return 0; |
197 | 0 | } |
198 | | |
199 | | |
200 | 0 | ZIP_EXTERN int zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) { |
201 | 0 | if (callback != NULL) { |
202 | 0 | if (za->progress == NULL) { |
203 | 0 | if ((za->progress = _zip_progress_new(za)) == NULL) { |
204 | 0 | return -1; |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 0 | _zip_progress_set_progress_callback(za->progress, precision, callback, ud_free, ud); |
209 | 0 | } |
210 | 0 | else { |
211 | 0 | if (za->progress != NULL) { |
212 | 0 | if (za->progress->callback_cancel == NULL) { |
213 | 0 | _zip_progress_free(za->progress); |
214 | 0 | za->progress = NULL; |
215 | 0 | } |
216 | 0 | else { |
217 | 0 | _zip_progress_free_progress_callback(za->progress); |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | | |
226 | 0 | ZIP_EXTERN int zip_register_cancel_callback_with_state(zip_t *za, zip_cancel_callback callback, void (*ud_free)(void *), void *ud) { |
227 | 0 | if (callback != NULL) { |
228 | 0 | if (za->progress == NULL) { |
229 | 0 | if ((za->progress = _zip_progress_new(za)) == NULL) { |
230 | 0 | return -1; |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | 0 | _zip_progress_set_cancel_callback(za->progress, callback, ud_free, ud); |
235 | 0 | } |
236 | 0 | else { |
237 | 0 | if (za->progress != NULL) { |
238 | 0 | if (za->progress->callback_progress == NULL) { |
239 | 0 | _zip_progress_free(za->progress); |
240 | 0 | za->progress = NULL; |
241 | 0 | } |
242 | 0 | else { |
243 | 0 | _zip_progress_free_cancel_callback(za->progress); |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | 0 | return 0; |
249 | 0 | } |
250 | | |
251 | | /* LCOV_EXCL_START */ |
252 | | struct legacy_ud { |
253 | | zip_progress_callback_t callback; |
254 | | }; |
255 | | |
256 | | |
257 | 0 | static void _zip_legacy_progress_callback(zip_t *za, double progress, void *vud) { |
258 | 0 | struct legacy_ud *ud = (struct legacy_ud *)vud; |
259 | |
|
260 | 0 | ud->callback(progress); |
261 | 0 | } |
262 | | |
263 | 0 | ZIP_EXTERN void zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback) { |
264 | 0 | struct legacy_ud *ud; |
265 | |
|
266 | 0 | if (progress_callback == NULL) { |
267 | 0 | zip_register_progress_callback_with_state(za, 0, NULL, NULL, NULL); |
268 | 0 | } |
269 | |
|
270 | 0 | if ((ud = (struct legacy_ud *)malloc(sizeof(*ud))) == NULL) { |
271 | 0 | return; |
272 | 0 | } |
273 | | |
274 | 0 | ud->callback = progress_callback; |
275 | |
|
276 | 0 | if (zip_register_progress_callback_with_state(za, 0.001, _zip_legacy_progress_callback, free, ud) < 0) { |
277 | 0 | free(ud); |
278 | 0 | } |
279 | 0 | } |
280 | | /* LCOV_EXCL_STOP */ |