2 hole.c -- convert huge files with mostly NULs to/from source_hole
3 Copyright (C) 2014-2016 Dieter Baron and Thomas Klausner
5 This file is part of libzip, a library to manipulate ZIP archives.
6 The authors can be contacted at <libzip@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
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
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
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.
52 zip_source_t *source_hole_create(const char *, int flags, zip_error_t *);
58 copy_source(zip_source_t *from, zip_source_t *to)
60 zip_uint8_t buf[8192];
63 if (zip_source_open(from) < 0) {
64 fprintf(stderr, "%s: can't open source for reading: %s\n", prg, zip_error_strerror(zip_source_error(from)));
68 if (zip_source_begin_write(to) < 0) {
69 fprintf(stderr, "%s: can't open source for writing: %s\n", prg, zip_error_strerror(zip_source_error(to)));
70 zip_source_close(from);
74 while ((n = zip_source_read(from, buf, sizeof(buf))) > 0) {
75 if (zip_source_write(to, buf, (zip_uint64_t)n) != n) {
76 fprintf(stderr, "%s: can't write to source: %s\n", prg, zip_error_strerror(zip_source_error(to)));
77 zip_source_close(from);
78 zip_source_rollback_write(to);
84 fprintf(stderr, "%s: can't read from source: %s\n", prg, zip_error_strerror(zip_source_error(from)));
85 zip_source_close(from);
86 zip_source_rollback_write(to);
90 zip_source_close(from);
92 if (zip_source_commit_write(to) < 0) {
93 fprintf(stderr, "%s: can't commit source: %s\n", prg, zip_error_strerror(zip_source_error(to)));
94 zip_source_rollback_write(to);
102 static zip_source_t *
103 open_compressed(const char *fname, int flags)
108 zip_error_init(&error);
110 if ((src = source_hole_create(fname, flags, &error)) == NULL) {
111 fprintf(stderr, "%s: can't open compressed file %s: %s\n", prg, fname, zip_error_strerror(&error));
112 zip_error_fini(&error);
120 static zip_source_t *
121 open_file(const char *fname)
126 zip_error_init(&error);
128 if ((src = zip_source_file_create(fname, 0, 0, &error)) == NULL) {
129 fprintf(stderr, "%s: can't open file %s: %s\n", prg, fname, zip_error_strerror(&error));
130 zip_error_fini(&error);
141 fprintf(stderr, "usage: %s [-du] in out\n", prg);
142 fprintf(stderr, "\nOptions:\n -d decompress in\n -u update in\n");
148 main(int argc, char **argv)
158 while ((c=getopt(argc, argv, "du")) != -1) {
176 if (optind+2 != argc) {
181 from = open_compressed(argv[optind], 0);
184 from = open_file(argv[optind]);
188 to = open_compressed(argv[optind+1], ZIP_CREATE);
191 to = open_file(argv[optind+1]);
194 err = copy_source(from, to);
196 zip_source_free(from);
199 exit(err < 0 ? 1 : 0);