Coverage Report

Created: 2025-07-23 08:18

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