Rizin
unix-like reverse engineering framework and cli tools
fread.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.

Macros

#define USAGE   "usage: %s [-v] archive\n"
 

Enumerations

enum  when { WHEN_NEVER , WHEN_OPEN , WHEN_READ , WHEN_CLOSE }
 

Functions

static int do_read (zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex)
 
int main (int argc, char *argv[])
 

Variables

const char * when_name [] = {"no", "zip_fopen", "zip_fread", "zip_fclose"}
 
int verbose
 
const char * progname
 

Macro Definition Documentation

◆ USAGE

#define USAGE   "usage: %s [-v] archive\n"

Definition at line 54 of file fread.c.

Enumeration Type Documentation

◆ when

enum when
Enumerator
WHEN_NEVER 
WHEN_OPEN 
WHEN_READ 
WHEN_CLOSE 

Definition at line 45 of file fread.c.

@ WHEN_NEVER
Definition: fread.c:45
@ WHEN_READ
Definition: fread.c:45
@ WHEN_CLOSE
Definition: fread.c:45
@ WHEN_OPEN
Definition: fread.c:45

Function Documentation

◆ do_read()

static int do_read ( zip_t z,
const char *  name,
zip_flags_t  flags,
enum when  when_ex,
int  ze_ex,
int  se_ex 
)
static

Definition at line 168 of file fread.c.

168  {
169  zip_file_t *zf;
170  enum when when_got;
171  zip_error_t error_got, error_ex;
172  zip_error_t *zf_error;
173  int err;
174  char b[8192];
175  zip_int64_t n;
176 
177  when_got = WHEN_NEVER;
178  zip_error_init(&error_got);
179  zip_error_init(&error_ex);
180  zip_error_set(&error_ex, ze_ex, se_ex);
181 
182  if ((zf = zip_fopen(z, name, flags)) == NULL) {
183  when_got = WHEN_OPEN;
184  zf_error = zip_get_error(z);
185  zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
186  }
187  else {
188  while ((n = zip_fread(zf, b, sizeof(b))) > 0)
189  ;
190  if (n < 0) {
191  when_got = WHEN_READ;
192  zf_error = zip_file_get_error(zf);
193  zip_error_set(&error_got, zip_error_code_zip(zf_error), zip_error_code_system(zf_error));
194  }
195  err = zip_fclose(zf);
196  if (when_got == WHEN_NEVER && err != 0) {
197  when_got = WHEN_CLOSE;
198  zip_error_init_with_code(&error_got, err);
199  }
200  }
201 
202  if (when_got != when_ex || zip_error_code_zip(&error_got) != zip_error_code_zip(&error_ex) || zip_error_code_system(&error_got) != zip_error_code_system(&error_ex)) {
203  printf("%s: %s: got %s error (%s), expected %s error (%s)\n", progname, name, when_name[when_got], zip_error_strerror(&error_got), when_name[when_ex], zip_error_strerror(&error_ex));
204  zip_error_fini(&error_got);
205  zip_error_fini(&error_ex);
206  return 1;
207  }
208  else if (verbose)
209  printf("%s: %s: passed\n", progname, name);
210 
211  return 0;
212 }
static bool err
Definition: armass.c:435
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
int verbose
Definition: fread.c:51
const char * when_name[]
Definition: fread.c:47
when
Definition: fread.c:45
const char * progname
Definition: fread.c:53
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
ZIP_EXTERN void zip_error_init_with_code(zip_error_t *_Nonnull, int)
Definition: zip_error.c:66
ZIP_EXTERN int zip_fclose(zip_file_t *_Nonnull)
Definition: zip_fclose.c:41
ZIP_EXTERN zip_file_t *_Nullable zip_fopen(zip_t *_Nonnull, const char *_Nonnull, zip_flags_t)
Definition: zip_fopen.c:39
ZIP_EXTERN int zip_error_code_system(const zip_error_t *_Nonnull)
Definition: zip_error.c:40
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
ZIP_EXTERN zip_error_t *_Nonnull zip_get_error(zip_t *_Nonnull)
Definition: zip_error_get.c:46
ZIP_EXTERN zip_int64_t zip_fread(zip_file_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_fread.c:39
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN int zip_error_code_zip(const zip_error_t *_Nonnull)
Definition: zip_error.c:46
ZIP_EXTERN zip_error_t *_Nonnull zip_file_get_error(zip_file_t *_Nonnull)
Definition: zip_error_get.c:52
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 flags
Definition: sfsocketcall.h:123
#define b(i)
Definition: sha256.c:42
Definition: z80asm.h:102
Definition: zip.h:284
int64_t zip_int64_t
Definition: zipconf.h:38

References b, err, flags, n, NULL, printf(), progname, verbose, WHEN_CLOSE, when_name, WHEN_NEVER, WHEN_OPEN, WHEN_READ, zip_error_code_system(), zip_error_code_zip(), zip_error_fini(), zip_error_init(), zip_error_init_with_code(), zip_error_set(), zip_error_strerror(), zip_fclose(), zip_file_get_error(), zip_fopen(), zip_fread(), and zip_get_error().

Referenced by main().

◆ main()

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

Definition at line 57 of file fread.c.

57  {
58  int fail, ze;
59  int c;
60  zip_t *z;
61  zip_source_t *zs;
62  char *archive;
64 
65  verbose = 0;
66  fail = 0;
67 
68  progname = argv[0];
69 
70  while ((c = getopt(argc, argv, "v")) != -1) {
71  switch (c) {
72  case 'v':
73  verbose = 1;
74  break;
75 
76  default:
77  fprintf(stderr, USAGE, progname);
78  return 1;
79  }
80  }
81 
82 
83  if (argc - optind != 1) {
84  fprintf(stderr, USAGE, progname);
85  return 1;
86  }
87 
88  archive = argv[optind];
89 
90  if ((z = zip_open(archive, 0, &ze)) == NULL) {
93  fprintf(stderr, "%s: can't open zip archive '%s': %s\n", progname, archive, zip_error_strerror(&error));
95  return 1;
96  }
97 
98  fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0);
99  fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0);
100  fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
101  fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
102  fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3);
103 #ifndef __clang_analyzer__ /* This test intentionally violates nullability. */
104  fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0);
105 #endif
106  fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
107  fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
108  fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
109  fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED, WHEN_READ, ZIP_ER_CRC, 0);
110  fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
111 
112  fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_NOPASSWD, 0);
113  zip_set_default_password(z, "crypt");
114  fail += do_read(z, "cryptok", 0, WHEN_NEVER, 0, 0);
115  zip_set_default_password(z, "wrong");
116  fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_WRONGPASSWD, 0);
118 
119  zs = zip_source_buffer(z, "asdf", 4, 0);
120  if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
121  fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
122  fail++;
123  }
124  else {
125  if (zip_replace(z, (zip_uint64_t)idx, zs) < 0) {
126  fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
127  fail++;
128  }
129  else {
130  fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
131  fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
132  }
133  }
134  if ((idx = zip_name_locate(z, "storedok", 0)) < 0) {
135  fprintf(stderr, "%s: can't locate 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
136  fail++;
137  }
138  else {
139  if (zip_delete(z, (zip_uint64_t)idx) < 0) {
140  fprintf(stderr, "%s: can't replace 'storedok' in zip archive '%s': %s\n", progname, archive, zip_strerror(z));
141  fail++;
142  }
143  else {
144  fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
145  fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
146  }
147  }
148  zs = zip_source_buffer(z, "asdf", 4, 0);
149  if (zip_file_add(z, "new_file", zs, 0) < 0) {
150  fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", progname, archive, zip_strerror(z));
151  fail++;
152  }
153  else {
154  fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
155  }
156 
157  zip_unchange_all(z);
158  if (zip_close(z) == -1) {
159  fprintf(stderr, "%s: can't close zip archive '%s': %s\n", progname, archive, zip_strerror(z));
160  return 1;
161  }
162 
163  exit(fail ? 1 : 0);
164 }
int getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.h:20
int optind
Definition: getopt.h:6
#define USAGE
Definition: fread.c:54
static int do_read(zip_t *z, const char *name, zip_flags_t flags, enum when when_ex, int ze_ex, int se_ex)
Definition: fread.c:168
ZIP_EXTERN zip_int64_t zip_name_locate(zip_t *_Nonnull, const char *_Nonnull, zip_flags_t)
#define ZIP_ER_ZLIB
Definition: zip.h:118
#define ZIP_ER_CHANGED
Definition: zip.h:120
#define ZIP_ER_NOPASSWD
Definition: zip.h:131
#define ZIP_FL_COMPRESSED
Definition: zip.h:78
#define ZIP_ER_NOENT
Definition: zip.h:114
ZIP_EXTERN int zip_set_default_password(zip_t *_Nonnull, const char *_Nullable)
#define ZIP_ER_WRONGPASSWD
Definition: zip.h:132
#define ZIP_ER_CRC
Definition: zip.h:112
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
ZIP_EXTERN int zip_delete(zip_t *_Nonnull, zip_uint64_t)
Definition: zip_delete.c:39
ZIP_EXTERN int zip_unchange_all(zip_t *_Nonnull)
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
ZIP_EXTERN int zip_replace(zip_t *_Nonnull, zip_uint64_t, zip_source_t *_Nonnull)
Definition: zip_replace.c:40
#define ZIP_FL_UNCHANGED
Definition: zip.h:79
ZIP_EXTERN zip_int64_t zip_file_add(zip_t *_Nonnull, const char *_Nonnull, zip_source_t *_Nonnull, zip_flags_t)
Definition: zip_file_add.c:45
ZIP_EXTERN zip_source_t *_Nullable zip_source_buffer(zip_t *_Nonnull, const void *_Nullable, zip_uint64_t, int)
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
int idx
Definition: setup.py:197
#define c(i)
Definition: sha256.c:43
Definition: zipcmp.c:60
Definition: zipint.h:278
#define fail(test)
Definition: tests.h:29
void error(const char *msg)
Definition: untgz.c:593
uint64_t zip_uint64_t
Definition: zipconf.h:39

References argv, c, do_read(), error(), test-lz4-list::exit, fail, getopt(), setup::idx, NULL, optind, progname, USAGE, verbose, WHEN_NEVER, WHEN_OPEN, WHEN_READ, zip_close(), zip_delete(), ZIP_ER_CHANGED, ZIP_ER_CRC, ZIP_ER_INVAL, ZIP_ER_NOENT, ZIP_ER_NOPASSWD, ZIP_ER_WRONGPASSWD, ZIP_ER_ZLIB, zip_error_fini(), zip_error_init_with_code(), zip_error_strerror(), zip_file_add(), ZIP_FL_COMPRESSED, ZIP_FL_UNCHANGED, zip_name_locate(), zip_open(), zip_replace(), zip_set_default_password(), zip_source_buffer(), zip_strerror(), and zip_unchange_all().

Variable Documentation

◆ progname

const char* progname

Definition at line 53 of file fread.c.

Referenced by do_read(), and main().

◆ verbose

int verbose

Definition at line 51 of file fread.c.

Referenced by do_read(), and main().

◆ when_name

const char* when_name[] = {"no", "zip_fopen", "zip_fread", "zip_fclose"}

Definition at line 47 of file fread.c.

Referenced by do_read().