"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/libzip/zip_progress.c" (10 Oct 2018, 4732 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "zip_progress.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  zip_progress.c -- progress reporting
    3  Copyright (C) 2017 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 <libzip@nih.at>
    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     zip_progress_callback callback;
   44     void (*ud_free)(void *);
   45 
   46     void *ud;
   47 
   48     double precision;
   49 
   50     /* state */
   51     double last_update; /* last value callback function was called with */
   52 
   53     double start; /* start of sub-progress section */
   54     double end;   /* end of sub-progress section */
   55 };
   56 
   57 
   58 void
   59 _zip_progress_end(zip_progress_t *progress) {
   60     _zip_progress_update(progress, 1.0);
   61 }
   62 
   63 
   64 void
   65 _zip_progress_free(zip_progress_t *progress) {
   66     if (progress == NULL) {
   67     return;
   68     }
   69 
   70     if (progress->ud_free) {
   71     progress->ud_free(progress->ud);
   72     }
   73 
   74     free(progress);
   75 }
   76 
   77 
   78 zip_progress_t *
   79 _zip_progress_new(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
   80     zip_progress_t *progress = (zip_progress_t *)malloc(sizeof(*progress));
   81 
   82     if (progress == NULL) {
   83     zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
   84     return NULL;
   85     }
   86 
   87     progress->za = za;
   88     progress->callback = callback;
   89     progress->ud_free = ud_free;
   90     progress->ud = ud;
   91     progress->precision = precision;
   92 
   93     return progress;
   94 }
   95 
   96 
   97 void
   98 _zip_progress_start(zip_progress_t *progress) {
   99     if (progress == NULL) {
  100     return;
  101     }
  102 
  103     progress->last_update = 0.0;
  104     progress->callback(progress->za, 0.0, progress->ud);
  105 }
  106 
  107 
  108 void
  109 _zip_progress_subrange(zip_progress_t *progress, double start, double end) {
  110     if (progress == NULL) {
  111     return;
  112     }
  113 
  114     progress->start = start;
  115     progress->end = end;
  116 
  117     _zip_progress_update(progress, 0.0);
  118 }
  119 
  120 void
  121 _zip_progress_update(zip_progress_t *progress, double sub_current) {
  122     double current;
  123 
  124     if (progress == NULL) {
  125     return;
  126     }
  127 
  128     current = ZIP_MIN(ZIP_MAX(sub_current, 0.0), 1.0) * (progress->end - progress->start) + progress->start;
  129 
  130     if (current - progress->last_update > progress->precision) {
  131     progress->callback(progress->za, current, progress->ud);
  132     progress->last_update = current;
  133     }
  134 }
  135 
  136 
  137 ZIP_EXTERN int
  138 zip_register_progress_callback_with_state(zip_t *za, double precision, zip_progress_callback callback, void (*ud_free)(void *), void *ud) {
  139     zip_progress_t *progress = NULL;
  140 
  141     if (callback != NULL) {
  142     if ((progress = _zip_progress_new(za, precision, callback, ud_free, ud)) == NULL) {
  143         return -1;
  144     }
  145     }
  146 
  147     _zip_progress_free(za->progress);
  148     za->progress = progress;
  149 
  150     return 0;
  151 }
  152 
  153 
  154 struct legacy_ud {
  155     zip_progress_callback_t callback;
  156 };
  157 
  158 
  159 static void
  160 _zip_legacy_progress_callback(zip_t *za, double progress, void *vud) {
  161     struct legacy_ud *ud = (struct legacy_ud *)vud;
  162 
  163     ud->callback(progress);
  164 }
  165 
  166 ZIP_EXTERN void
  167 zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback) {
  168     struct legacy_ud *ud;
  169 
  170     if (progress_callback == NULL) {
  171     zip_register_progress_callback_with_state(za, 0, NULL, NULL, NULL);
  172     }
  173 
  174     if ((ud = (struct legacy_ud *)malloc(sizeof(*ud))) == NULL) {
  175     return;
  176     }
  177 
  178     ud->callback = progress_callback;
  179 
  180     if (zip_register_progress_callback_with_state(za, 0.001, _zip_legacy_progress_callback, free, ud) < 0) {
  181     free(ud);
  182     }
  183 }