"Fossies" - the Fresh Open Source Software Archive

Member "libzip-1.5.2/regress/hole.c" (12 Mar 2019, 4920 Bytes) of package /linux/misc/libzip-1.5.2.tar.xz:


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 "hole.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.1_vs_1.5.2.

    1 /*
    2  hole.c -- convert huge files with mostly NULs to/from source_hole
    3  Copyright (C) 2014-2018 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 #include "config.h"
   35 
   36 #include <errno.h>
   37 #include <stdio.h>
   38 #include <stdlib.h>
   39 #include <string.h>
   40 #ifdef HAVE_UNISTD_H
   41 #include <unistd.h>
   42 #endif
   43 
   44 #ifndef HAVE_GETOPT
   45 #include "getopt.h"
   46 #endif
   47 
   48 #include "zip.h"
   49 
   50 /* public API */
   51 
   52 zip_source_t *source_hole_create(const char *, int flags, zip_error_t *);
   53 
   54 const char *progname;
   55 
   56 
   57 static int
   58 copy_source(zip_source_t *from, zip_source_t *to) {
   59     zip_uint8_t buf[8192];
   60     zip_int64_t n;
   61 
   62     if (zip_source_open(from) < 0) {
   63     fprintf(stderr, "%s: can't open source for reading: %s\n", progname, zip_error_strerror(zip_source_error(from)));
   64     return -1;
   65     }
   66 
   67     if (zip_source_begin_write(to) < 0) {
   68     fprintf(stderr, "%s: can't open source for writing: %s\n", progname, zip_error_strerror(zip_source_error(to)));
   69     zip_source_close(from);
   70     return -1;
   71     }
   72 
   73     while ((n = zip_source_read(from, buf, sizeof(buf))) > 0) {
   74     if (zip_source_write(to, buf, (zip_uint64_t)n) != n) {
   75         fprintf(stderr, "%s: can't write to source: %s\n", progname, zip_error_strerror(zip_source_error(to)));
   76         zip_source_close(from);
   77         zip_source_rollback_write(to);
   78         return -1;
   79     }
   80     }
   81 
   82     if (n < 0) {
   83     fprintf(stderr, "%s: can't read from source: %s\n", progname, zip_error_strerror(zip_source_error(from)));
   84     zip_source_close(from);
   85     zip_source_rollback_write(to);
   86     return -1;
   87     }
   88 
   89     zip_source_close(from);
   90 
   91     if (zip_source_commit_write(to) < 0) {
   92     fprintf(stderr, "%s: can't commit source: %s\n", progname, zip_error_strerror(zip_source_error(to)));
   93     zip_source_rollback_write(to);
   94     return -1;
   95     }
   96 
   97     return 0;
   98 }
   99 
  100 
  101 static zip_source_t *
  102 open_compressed(const char *fname, int flags) {
  103     zip_error_t error;
  104     zip_source_t *src;
  105 
  106     zip_error_init(&error);
  107 
  108     if ((src = source_hole_create(fname, flags, &error)) == NULL) {
  109     fprintf(stderr, "%s: can't open compressed file %s: %s\n", progname, fname, zip_error_strerror(&error));
  110     zip_error_fini(&error);
  111     exit(1);
  112     }
  113 
  114     return src;
  115 }
  116 
  117 
  118 static zip_source_t *
  119 open_file(const char *fname) {
  120     zip_error_t error;
  121     zip_source_t *src;
  122 
  123     zip_error_init(&error);
  124 
  125     if ((src = zip_source_file_create(fname, 0, 0, &error)) == NULL) {
  126     fprintf(stderr, "%s: can't open file %s: %s\n", progname, fname, zip_error_strerror(&error));
  127     zip_error_fini(&error);
  128     exit(1);
  129     }
  130 
  131     return src;
  132 }
  133 
  134 
  135 static void
  136 usage(void) {
  137     fprintf(stderr, "usage: %s [-du] in out\n", progname);
  138     fprintf(stderr, "\nOptions:\n  -d  decompress in\n  -u  update in\n");
  139     exit(1);
  140 }
  141 
  142 
  143 int
  144 main(int argc, char **argv) {
  145     zip_source_t *from;
  146     zip_source_t *to;
  147     int c, err;
  148     int compress = 1;
  149     int decompress = 0;
  150 
  151     progname = argv[0];
  152 
  153     while ((c = getopt(argc, argv, "du")) != -1) {
  154     switch (c) {
  155     case 'd':
  156         compress = 0;
  157         decompress = 1;
  158         break;
  159 
  160     case 'u':
  161         compress = 1;
  162         decompress = 1;
  163         break;
  164 
  165     default:
  166         usage();
  167         break;
  168     }
  169     }
  170 
  171     if (optind + 2 != argc) {
  172     usage();
  173     }
  174 
  175     if (decompress) {
  176     from = open_compressed(argv[optind], 0);
  177     }
  178     else {
  179     from = open_file(argv[optind]);
  180     }
  181 
  182     if (compress) {
  183     to = open_compressed(argv[optind + 1], ZIP_CREATE);
  184     }
  185     else {
  186     to = open_file(argv[optind + 1]);
  187     }
  188 
  189     err = copy_source(from, to);
  190 
  191     zip_source_free(from);
  192     zip_source_free(to);
  193 
  194     exit(err < 0 ? 1 : 0);
  195 }