Rizin
unix-like reverse engineering framework and cli tools
hole.c
Go to the documentation of this file.
1 /*
2  hole.c -- convert huge files with mostly NULs to/from source_hole
3  Copyright (C) 2014-2021 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 <stdio.h>
37 #include <stdlib.h>
38 
39 #ifndef HAVE_GETOPT
40 #include "getopt.h"
41 #endif
42 
43 #include "zip.h"
44 
45 /* public API */
46 
47 zip_source_t *source_hole_create(const char *, int flags, zip_error_t *);
48 
49 const char *progname;
50 
51 
52 static int
54  zip_uint8_t buf[8192];
55  zip_int64_t n;
56 
57  if (zip_source_open(from) < 0) {
58  fprintf(stderr, "%s: can't open source for reading: %s\n", progname, zip_error_strerror(zip_source_error(from)));
59  return -1;
60  }
61 
62  if (zip_source_begin_write(to) < 0) {
63  fprintf(stderr, "%s: can't open source for writing: %s\n", progname, zip_error_strerror(zip_source_error(to)));
65  return -1;
66  }
67 
68  while ((n = zip_source_read(from, buf, sizeof(buf))) > 0) {
69  if (zip_source_write(to, buf, (zip_uint64_t)n) != n) {
70  fprintf(stderr, "%s: can't write to source: %s\n", progname, zip_error_strerror(zip_source_error(to)));
73  return -1;
74  }
75  }
76 
77  if (n < 0) {
78  fprintf(stderr, "%s: can't read from source: %s\n", progname, zip_error_strerror(zip_source_error(from)));
81  return -1;
82  }
83 
85 
86  if (zip_source_commit_write(to) < 0) {
87  fprintf(stderr, "%s: can't commit source: %s\n", progname, zip_error_strerror(zip_source_error(to)));
89  return -1;
90  }
91 
92  return 0;
93 }
94 
95 
96 static zip_source_t *
97 open_compressed(const char *fname, int flags) {
100 
102 
103  if ((src = source_hole_create(fname, flags, &error)) == NULL) {
104  fprintf(stderr, "%s: can't open compressed file %s: %s\n", progname, fname, zip_error_strerror(&error));
106  exit(1);
107  }
108 
109  return src;
110 }
111 
112 
113 static zip_source_t *
114 open_file(const char *fname) {
116  zip_source_t *src;
117 
119 
120  if ((src = zip_source_file_create(fname, 0, 0, &error)) == NULL) {
121  fprintf(stderr, "%s: can't open file %s: %s\n", progname, fname, zip_error_strerror(&error));
123  exit(1);
124  }
125 
126  return src;
127 }
128 
129 
130 static void
131 usage(void) {
132  fprintf(stderr, "usage: %s [-du] in out\n", progname);
133  fprintf(stderr, "\nOptions:\n -d decompress in\n -u update in\n");
134  exit(1);
135 }
136 
137 
138 int
139 main(int argc, char **argv) {
141  zip_source_t *to;
142  int c, err;
143  int compress = 1;
144  int decompress = 0;
145 
146  progname = argv[0];
147 
148  while ((c = getopt(argc, argv, "du")) != -1) {
149  switch (c) {
150  case 'd':
151  compress = 0;
152  decompress = 1;
153  break;
154 
155  case 'u':
156  compress = 1;
157  decompress = 1;
158  break;
159 
160  default:
161  usage();
162  break;
163  }
164  }
165 
166  if (optind + 2 != argc) {
167  usage();
168  }
169 
170  if (decompress) {
172  }
173  else {
175  }
176 
177  if (compress) {
179  }
180  else {
181  to = open_file(argv[optind + 1]);
182  }
183 
184  err = copy_source(from, to);
185 
188 
189  exit(err < 0 ? 1 : 0);
190 }
lzma_index * src
Definition: index.h:567
static bool err
Definition: armass.c:435
static bool decompress(RzBuffer *source_buf, ut64 source_offset, ut64 source_size, ut8 *dst_buf, ut64 decompressed_size)
Definition: bin_nso.c:108
int getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.h:20
int optind
Definition: getopt.h:6
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
#define NULL
Definition: cris-opc.c:27
zip_source_t * source_hole_create(const char *, int flags, zip_error_t *)
Definition: source_hole.c:94
static int copy_source(zip_source_t *from, zip_source_t *to)
Definition: hole.c:53
int main(int argc, char **argv)
Definition: hole.c:139
static zip_source_t * open_file(const char *fname)
Definition: hole.c:114
static zip_source_t * open_compressed(const char *fname, int flags)
Definition: hole.c:97
const char * progname
Definition: hole.c:49
static void usage(void)
Definition: hole.c:131
voidpf void * buf
Definition: ioapi.h:138
ZIP_EXTERN int zip_source_close(zip_source_t *_Nonnull)
ZIP_EXTERN void zip_source_rollback_write(zip_source_t *_Nonnull)
ZIP_EXTERN int zip_source_commit_write(zip_source_t *_Nonnull)
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
ZIP_EXTERN zip_error_t *_Nonnull zip_source_error(zip_source_t *_Nonnull)
#define ZIP_CREATE
Definition: zip.h:67
ZIP_EXTERN int zip_source_open(zip_source_t *_Nonnull)
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN int zip_source_begin_write(zip_source_t *_Nonnull)
ZIP_EXTERN zip_int64_t zip_source_write(zip_source_t *_Nonnull, const void *_Nullable, zip_uint64_t)
ZIP_EXTERN void zip_source_free(zip_source_t *_Nullable)
ZIP_EXTERN const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
ZIP_EXTERN zip_source_t *_Nullable zip_source_file_create(const char *_Nonnull, zip_uint64_t, zip_int64_t, zip_error_t *_Nullable)
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
int n
Definition: mipsasm.c:19
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define c(i)
Definition: sha256.c:43
Definition: zip.h:284
void error(const char *msg)
Definition: untgz.c:593
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
int64_t zip_int64_t
Definition: zipconf.h:38