Rizin
unix-like reverse engineering framework and cli tools
in-memory.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <zip.h>

Go to the source code of this file.

Functions

static int get_data (void **datap, size_t *sizep, const char *archive)
 
static int modify_archive (zip_t *za)
 
static int use_data (void *data, size_t size, const char *archive)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ get_data()

static int get_data ( void **  datap,
size_t sizep,
const char *  archive 
)
static

Definition at line 43 of file in-memory.c.

43  {
44  /* example implementation that reads data from file */
45  struct stat st;
46  FILE *fp;
47 
48  if ((fp = fopen(archive, "rb")) == NULL) {
49  if (errno != ENOENT) {
50  fprintf(stderr, "can't open %s: %s\n", archive, strerror(errno));
51  return -1;
52  }
53 
54  *datap = NULL;
55  *sizep = 0;
56 
57  return 0;
58  }
59 
60  if (fstat(fileno(fp), &st) < 0) {
61  fprintf(stderr, "can't stat %s: %s\n", archive, strerror(errno));
62  fclose(fp);
63  return -1;
64  }
65 
66  if ((*datap = malloc((size_t)st.st_size)) == NULL) {
67  fprintf(stderr, "can't allocate buffer\n");
68  fclose(fp);
69  return -1;
70  }
71 
72  if (fread(*datap, 1, (size_t)st.st_size, fp) < (size_t)st.st_size) {
73  fprintf(stderr, "can't read %s: %s\n", archive, strerror(errno));
74  free(*datap);
75  fclose(fp);
76  return -1;
77  }
78 
79  fclose(fp);
80 
81  *sizep = (size_t)st.st_size;
82  return 0;
83 }
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset fstat
Definition: sflib.h:107
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * malloc(size_t size)
Definition: malloc.c:123
string FILE
Definition: benchmark.py:21
#define ENOENT
Definition: sftypes.h:112
int size_t
Definition: sftypes.h:40
Definition: zipcmp.c:60
Definition: sftypes.h:80

References ENOENT, benchmark::FILE, free(), fstat, malloc(), and NULL.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 124 of file in-memory.c.

124  {
125  const char *archive;
126  zip_source_t *src;
127  zip_t *za;
129  void *data;
130  size_t size;
131 
132  if (argc < 2) {
133  fprintf(stderr, "usage: %s archive\n", argv[0]);
134  return 1;
135  }
136  archive = argv[1];
137 
138  /* get buffer with zip archive inside */
139  if (get_data(&data, &size, archive) < 0) {
140  return 1;
141  }
142 
144  /* create source from buffer */
145  if ((src = zip_source_buffer_create(data, size, 1, &error)) == NULL) {
146  fprintf(stderr, "can't create source: %s\n", zip_error_strerror(&error));
147  free(data);
149  return 1;
150  }
151 
152  /* open zip archive from source */
153  if ((za = zip_open_from_source(src, 0, &error)) == NULL) {
154  fprintf(stderr, "can't open zip from source: %s\n", zip_error_strerror(&error));
157  return 1;
158  }
160 
161  /* we'll want to read the data back after zip_close */
163 
164  /* modify archive */
166 
167  /* close archive */
168  if (zip_close(za) < 0) {
169  fprintf(stderr, "can't close zip archive '%s': %s\n", archive, zip_strerror(za));
170  return 1;
171  }
172 
173 
174  /* copy new archive to buffer */
175 
176  if (zip_source_is_deleted(src)) {
177  /* new archive is empty, thus no data */
178  data = NULL;
179  }
180  else {
181  zip_stat_t zst;
182 
183  if (zip_source_stat(src, &zst) < 0) {
184  fprintf(stderr, "can't stat source: %s\n", zip_error_strerror(zip_source_error(src)));
185  return 1;
186  }
187 
188  size = zst.size;
189 
190  if (zip_source_open(src) < 0) {
191  fprintf(stderr, "can't open source: %s\n", zip_error_strerror(zip_source_error(src)));
192  return 1;
193  }
194  if ((data = malloc(size)) == NULL) {
195  fprintf(stderr, "malloc failed: %s\n", strerror(errno));
197  return 1;
198  }
199  if ((zip_uint64_t)zip_source_read(src, data, size) < size) {
200  fprintf(stderr, "can't read data from source: %s\n", zip_error_strerror(zip_source_error(src)));
202  free(data);
203  return 1;
204  }
206  }
207 
208  /* we're done with src */
210 
211  /* use new data */
212  use_data(data, size, archive);
213 
214  free(data);
215 
216  return 0;
217 }
lzma_index * src
Definition: index.h:567
static int get_data(void **datap, size_t *sizep, const char *archive)
Definition: in-memory.c:43
static int modify_archive(zip_t *za)
Definition: in-memory.c:86
static int use_data(void *data, size_t size, const char *archive)
Definition: in-memory.c:93
voidpf void uLong size
Definition: ioapi.h:138
ZIP_EXTERN int zip_source_close(zip_source_t *_Nonnull)
ZIP_EXTERN int zip_source_is_deleted(zip_source_t *_Nonnull)
ZIP_EXTERN zip_t *_Nullable zip_open_from_source(zip_source_t *_Nonnull, int, zip_error_t *_Nullable)
Definition: zip_open.c:79
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
ZIP_EXTERN zip_source_t *_Nullable zip_source_buffer_create(const void *_Nullable, zip_uint64_t, int, zip_error_t *_Nullable)
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)
ZIP_EXTERN int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
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 void zip_source_free(zip_source_t *_Nullable)
ZIP_EXTERN const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
ZIP_EXTERN void zip_source_keep(zip_source_t *_Nonnull)
ZIP_EXTERN const char *_Nonnull zip_strerror(zip_t *_Nonnull)
Definition: zip_strerror.c:39
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
Definition: zip.h:284
Definition: zip.h:300
zip_uint64_t size
Definition: zip.h:304
Definition: zipint.h:278
void error(const char *msg)
Definition: untgz.c:593
uint64_t zip_uint64_t
Definition: zipconf.h:39
zip_t * za
Definition: ziptool.c:79

References argv, error(), free(), get_data(), malloc(), modify_archive(), NULL, zip_stat::size, src, use_data(), za, zip_close(), zip_error_fini(), zip_error_init(), zip_error_strerror(), zip_open_from_source(), zip_source_buffer_create(), zip_source_close(), zip_source_error(), zip_source_free(), zip_source_is_deleted(), zip_source_keep(), zip_source_open(), zip_source_read(), zip_source_stat(), and zip_strerror().

◆ modify_archive()

static int modify_archive ( zip_t za)
static

Definition at line 86 of file in-memory.c.

86  {
87  /* modify the archive */
88  return 0;
89 }

Referenced by main().

◆ use_data()

static int use_data ( void *  data,
size_t  size,
const char *  archive 
)
static

Definition at line 93 of file in-memory.c.

93  {
94  /* example implementation that writes data to file */
95  FILE *fp;
96 
97  if (data == NULL) {
98  if (remove(archive) < 0 && errno != ENOENT) {
99  fprintf(stderr, "can't remove %s: %s\n", archive, strerror(errno));
100  return -1;
101  }
102  return 0;
103  }
104 
105  if ((fp = fopen(archive, "wb")) == NULL) {
106  fprintf(stderr, "can't open %s: %s\n", archive, strerror(errno));
107  return -1;
108  }
109  if (fwrite(data, 1, size, fp) < size) {
110  fprintf(stderr, "can't write %s: %s\n", archive, strerror(errno));
111  fclose(fp);
112  return -1;
113  }
114  if (fclose(fp) < 0) {
115  fprintf(stderr, "can't write %s: %s\n", archive, strerror(errno));
116  return -1;
117  }
118 
119  return 0;
120 }

References ENOENT, benchmark::FILE, and NULL.

Referenced by main().