Rizin
unix-like reverse engineering framework and cli tools
zip_progress.c
Go to the documentation of this file.
1 /*
2  zip_progress.c -- progress reporting
3  Copyright (C) 2017-2020 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 {
43 
45  void (*ud_progress_free)(void *);
46  void *ud_progress;
47 
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 
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 void
69  _zip_progress_update(progress, 1.0);
70 }
71 
72 
73 void
75  if (progress == NULL) {
76  return;
77  }
78 
81 
82  free(progress);
83 }
84 
85 
86 static zip_progress_t *
88  zip_progress_t *progress = (zip_progress_t *)malloc(sizeof(*progress));
89 
90  if (progress == NULL) {
92  return NULL;
93  }
94 
95  progress->za = za;
96 
97  progress->callback_progress = NULL;
98  progress->ud_progress_free = NULL;
99  progress->ud_progress = NULL;
100  progress->precision = 0.0;
101 
102  progress->callback_cancel = NULL;
103  progress->ud_cancel_free = NULL;
104  progress->ud_cancel = NULL;
105 
106  return progress;
107 }
108 
109 static void
111  if (progress->ud_progress_free) {
112  progress->ud_progress_free(progress->ud_progress);
113  }
114 
115  progress->callback_progress = NULL;
116  progress->ud_progress = NULL;
117  progress->ud_progress_free = NULL;
118 }
119 
120 static void
122  if (progress->ud_cancel_free) {
123  progress->ud_cancel_free(progress->ud_cancel);
124  }
125 
126  progress->callback_cancel = NULL;
127  progress->ud_cancel = NULL;
128  progress->ud_cancel_free = NULL;
129 }
130 
131 static void
132 _zip_progress_set_progress_callback(zip_progress_t *progress, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
134 
135  progress->callback_progress = callback;
136  progress->ud_progress_free = ud_free;
137  progress->ud_progress = ud;
138  progress->precision = precision;
139 }
140 
141 void
142 _zip_progress_set_cancel_callback(zip_progress_t *progress, zip_cancel_callback callback, void (*ud_free)(void *), void *ud) {
144 
145  progress->callback_cancel = callback;
146  progress->ud_cancel_free = ud_free;
147  progress->ud_cancel = ud;
148 }
149 
150 int
152  if (progress == NULL) {
153  return 0;
154  }
155 
156  if (progress->callback_progress != NULL) {
157  progress->last_update = 0.0;
158  progress->callback_progress(progress->za, 0.0, progress->ud_progress);
159  }
160 
161  if (progress->callback_cancel != NULL) {
162  if (progress->callback_cancel(progress->za, progress->ud_cancel)) {
163  return -1;
164  }
165  }
166 
167  return 0;
168 }
169 
170 
171 int
172 _zip_progress_subrange(zip_progress_t *progress, double start, double end) {
173  if (progress == NULL) {
174  return 0;
175  }
176 
177  progress->start = start;
178  progress->end = end;
179 
180  return _zip_progress_update(progress, 0.0);
181 }
182 
183 int
184 _zip_progress_update(zip_progress_t *progress, double sub_current) {
185  double current;
186 
187  if (progress == NULL) {
188  return 0;
189  }
190 
191  if (progress->callback_progress != NULL) {
192  current = ZIP_MIN(ZIP_MAX(sub_current, 0.0), 1.0) * (progress->end - progress->start) + progress->start;
193 
194  if (current - progress->last_update > progress->precision) {
195  progress->callback_progress(progress->za, current, progress->ud_progress);
196  progress->last_update = current;
197  }
198  }
199 
200  if (progress->callback_cancel != NULL) {
201  if (progress->callback_cancel(progress->za, progress->ud_cancel)) {
202  return -1;
203  }
204  }
205 
206  return 0;
207 }
208 
209 
210 ZIP_EXTERN int
211 zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
212  if (callback != NULL) {
213  if (za->progress == NULL) {
214  if ((za->progress = _zip_progress_new(za)) == NULL) {
215  return -1;
216  }
217  }
218 
219  _zip_progress_set_progress_callback(za->progress, precision, callback, ud_free, ud);
220  }
221  else {
222  if (za->progress != NULL) {
223  if (za->progress->callback_cancel == NULL) {
225  za->progress = NULL;
226  }
227  else {
229  }
230  }
231  }
232 
233  return 0;
234 }
235 
236 
237 ZIP_EXTERN int
238 zip_register_cancel_callback_with_state(zip_t *za, zip_cancel_callback callback, void (*ud_free)(void *), void *ud) {
239  if (callback != NULL) {
240  if (za->progress == NULL) {
241  if ((za->progress = _zip_progress_new(za)) == NULL) {
242  return -1;
243  }
244  }
245 
246  _zip_progress_set_cancel_callback(za->progress, callback, ud_free, ud);
247  }
248  else {
249  if (za->progress != NULL) {
250  if (za->progress->callback_progress == NULL) {
252  za->progress = NULL;
253  }
254  else {
256  }
257  }
258  }
259 
260  return 0;
261 }
262 
263 
264 struct legacy_ud {
266 };
267 
268 
269 static void
270 _zip_legacy_progress_callback(zip_t *za, double progress, void *vud) {
271  struct legacy_ud *ud = (struct legacy_ud *)vud;
272 
273  ud->callback(progress);
274 }
275 
276 ZIP_EXTERN void
278  struct legacy_ud *ud;
279 
280  if (progress_callback == NULL) {
282  }
283 
284  if ((ud = (struct legacy_ud *)malloc(sizeof(*ud))) == NULL) {
285  return;
286  }
287 
289 
291  free(ud);
292  }
293 }
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
void(* zip_progress_callback)(zip_t *_Nonnull, double, void *_Nullable)
Definition: zip.h:350
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_EXTERN
Definition: zip.h:54
void(* zip_progress_callback_t)(double)
Definition: zip.h:354
int(* zip_cancel_callback)(zip_t *_Nonnull, void *_Nullable)
Definition: zip.h:351
void * malloc(size_t size)
Definition: malloc.c:123
zip_progress_callback_t callback
Definition: zip_progress.c:265
void(* ud_cancel_free)(void *)
Definition: zip_progress.c:49
double last_update
Definition: zip_progress.c:55
void(* ud_progress_free)(void *)
Definition: zip_progress.c:45
void * ud_progress
Definition: zip_progress.c:46
zip_progress_callback callback_progress
Definition: zip_progress.c:44
double start
Definition: zip_progress.c:57
double precision
Definition: zip_progress.c:52
zip_cancel_callback callback_cancel
Definition: zip_progress.c:48
void * ud_cancel
Definition: zip_progress.c:50
zip_t * za
Definition: zip_progress.c:42
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
zip_progress_t * progress
Definition: zipint.h:302
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static void _zip_progress_set_cancel_callback(zip_progress_t *progress, zip_cancel_callback callback, void(*ud_free)(void *), void *ud)
Definition: zip_progress.c:142
int _zip_progress_subrange(zip_progress_t *progress, double start, double end)
Definition: zip_progress.c:172
static zip_progress_t * _zip_progress_new(zip_t *za)
Definition: zip_progress.c:87
static void _zip_progress_set_progress_callback(zip_progress_t *progress, double precision, zip_progress_callback callback, void(*ud_free)(void *), void *ud)
Definition: zip_progress.c:132
void _zip_progress_end(zip_progress_t *progress)
Definition: zip_progress.c:68
ZIP_EXTERN void zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback)
Definition: zip_progress.c:277
int _zip_progress_start(zip_progress_t *progress)
Definition: zip_progress.c:151
ZIP_EXTERN int zip_register_cancel_callback_with_state(zip_t *za, zip_cancel_callback callback, void(*ud_free)(void *), void *ud)
Definition: zip_progress.c:238
ZIP_EXTERN int zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progress_callback callback, void(*ud_free)(void *), void *ud)
Definition: zip_progress.c:211
static void _zip_progress_free_progress_callback(zip_progress_t *progress)
Definition: zip_progress.c:110
static void _zip_progress_free_cancel_callback(zip_progress_t *progress)
Definition: zip_progress.c:121
static void _zip_legacy_progress_callback(zip_t *za, double progress, void *vud)
Definition: zip_progress.c:270
void _zip_progress_free(zip_progress_t *progress)
Definition: zip_progress.c:74
int _zip_progress_update(zip_progress_t *progress, double sub_current)
Definition: zip_progress.c:184
#define ZIP_MAX(a, b)
Definition: zipint.h:472
#define ZIP_MIN(a, b)
Definition: zipint.h:473
zip_t * za
Definition: ziptool.c:79
static void progress_callback(zip_t *archive, double percentage, void *ud)
Definition: ziptool.c:398