Rizin
unix-like reverse engineering framework and cli tools
hole.c File Reference
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
#include "zip.h"

Go to the source code of this file.

Functions

zip_source_tsource_hole_create (const char *, int flags, zip_error_t *)
 
static int copy_source (zip_source_t *from, zip_source_t *to)
 
static zip_source_topen_compressed (const char *fname, int flags)
 
static zip_source_topen_file (const char *fname)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

const char * progname
 

Function Documentation

◆ copy_source()

static int copy_source ( zip_source_t from,
zip_source_t to 
)
static

Definition at line 53 of file hole.c.

53  {
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 }
const char * progname
Definition: hole.c:49
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 zip_error_t *_Nonnull zip_source_error(zip_source_t *_Nonnull)
ZIP_EXTERN int zip_source_open(zip_source_t *_Nonnull)
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 const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
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
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

References from, n, progname, to, zip_error_strerror(), zip_source_begin_write(), zip_source_close(), zip_source_commit_write(), zip_source_error(), zip_source_open(), zip_source_read(), zip_source_rollback_write(), and zip_source_write().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 139 of file hole.c.

139  {
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 }
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
static int copy_source(zip_source_t *from, zip_source_t *to)
Definition: hole.c:53
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
static void usage(void)
Definition: hole.c:131
#define ZIP_CREATE
Definition: zip.h:67
ZIP_EXTERN void zip_source_free(zip_source_t *_Nullable)
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define c(i)
Definition: sha256.c:43

References argv, c, compress(), copy_source(), decompress(), err, test-lz4-list::exit, from, getopt(), open_compressed(), open_file(), optind, progname, to, usage(), ZIP_CREATE, and zip_source_free().

◆ open_compressed()

static zip_source_t* open_compressed ( const char *  fname,
int  flags 
)
static

Definition at line 97 of file hole.c.

97  {
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 }
lzma_index * src
Definition: index.h:567
#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
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
Definition: zip.h:284
void error(const char *msg)
Definition: untgz.c:593

References error(), test-lz4-list::exit, flags, create_tags_rz::fname, NULL, progname, source_hole_create(), src, zip_error_fini(), zip_error_init(), and zip_error_strerror().

Referenced by main().

◆ open_file()

static zip_source_t* open_file ( const char *  fname)
static

Definition at line 114 of file hole.c.

114  {
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 }
ZIP_EXTERN zip_source_t *_Nullable zip_source_file_create(const char *_Nonnull, zip_uint64_t, zip_int64_t, zip_error_t *_Nullable)

References error(), test-lz4-list::exit, create_tags_rz::fname, NULL, progname, src, zip_error_fini(), zip_error_init(), zip_error_strerror(), and zip_source_file_create().

Referenced by main().

◆ source_hole_create()

zip_source_t * source_hole_create ( const char *  fname,
int  flags,
zip_error_t error 
)

Definition at line 94 of file source_hole.c.

94  {
95  hole_t *ud = hole_new(fname, flags, error);
96 
97  if (ud == NULL) {
98  return NULL;
99  }
101 }
ZIP_EXTERN zip_source_t *_Nullable zip_source_function_create(zip_source_callback _Nonnull, void *_Nullable, zip_error_t *_Nullable)
static zip_int64_t source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command)
Definition: source_hole.c:494
static hole_t * hole_new(const char *fname, int flags, zip_error_t *error)
Definition: source_hole.c:467

References error(), flags, create_tags_rz::fname, hole_new(), NULL, source_hole_cb(), and zip_source_function_create().

Referenced by open_compressed().

◆ usage()

static void usage ( void  )
static

Definition at line 131 of file hole.c.

131  {
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 }

References test-lz4-list::exit, and progname.

Referenced by main().

Variable Documentation

◆ progname

const char* progname

Definition at line 49 of file hole.c.

Referenced by copy_source(), main(), open_compressed(), open_file(), and usage().