Rizin
unix-like reverse engineering framework and cli tools
fseek.c
Go to the documentation of this file.
1 /*
2  fseek.c -- test tool for seeking in zip archives
3  Copyright (C) 2016-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 <stdlib.h>
35 
36 #include "zip.h"
37 
38 const char *progname;
39 #define USAGE "usage: %s archive index offset\n"
40 
41 int
42 main(int argc, char *argv[]) {
43  int ze;
44  zip_t *z;
45  zip_file_t *zf;
46  char *archive;
48  zip_uint64_t index;
49  char b[1024];
50 
51  progname = argv[0];
52 
53  if (argc != 4) {
54  fprintf(stderr, USAGE, progname);
55  return 1;
56  }
57 
58  archive = argv[1];
59  index = strtoull(argv[2], NULL, 10);
60  offset = (zip_int64_t)strtoull(argv[3], NULL, 10);
61 
62  if ((z = zip_open(archive, 0, &ze)) == NULL) {
65  fprintf(stderr, "%s: can't open zip archive '%s': %s\n", progname, archive, zip_error_strerror(&error));
67  return 1;
68  }
69 
70  if ((zf = zip_fopen_index(z, index, 0)) == NULL) {
71  fprintf(stderr, "%s: can't open file in archive '%s': %s\n", progname, archive, zip_error_strerror(zip_file_get_error(zf)));
72  zip_close(z);
73  return 1;
74  }
75 
76  if (zip_fseek(zf, offset, SEEK_SET) < 0) {
77  fprintf(stderr, "%s: zip_fseek failed: %s\n", progname, zip_error_strerror(zip_file_get_error(zf)));
78  zip_fclose(zf);
79  zip_close(z);
80  return 1;
81  }
82 
83  while ((n = zip_fread(zf, b, sizeof(b))) > 0) {
84  printf("%.*s", (int)n, b);
85  }
86  if (n < 0) {
87  fprintf(stderr, "%s: zip_fread failed: %s\n", progname, zip_error_strerror(zip_file_get_error(zf)));
88  zip_fclose(zf);
89  zip_close(z);
90  return 1;
91  }
92 
93  if (zip_fclose(zf) == -1) {
94  fprintf(stderr, "%s: can't close zip archive entry %" PRIu64 " in '%s': %s\n", progname, index, archive, zip_strerror(z));
95  return 1;
96  }
97 
98  if (zip_close(z) == -1) {
99  fprintf(stderr, "%s: can't close zip archive '%s': %s\n", progname, archive, zip_strerror(z));
100  return 1;
101  }
102 
103  return 0;
104 }
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
int main(int argc, char *argv[])
Definition: fseek.c:42
#define USAGE
Definition: fseek.c:39
const char * progname
Definition: fseek.c:38
voidpf uLong offset
Definition: ioapi.h:144
#define PRIu64
Definition: macros.h:18
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_int8_t zip_fseek(zip_file_t *_Nonnull, zip_int64_t, int)
Definition: zip_fseek.c:38
ZIP_EXTERN zip_int64_t zip_fread(zip_file_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_fread.c:39
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN zip_error_t *_Nonnull zip_file_get_error(zip_file_t *_Nonnull)
Definition: zip_error_get.c:52
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
ZIP_EXTERN const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
ZIP_EXTERN zip_file_t *_Nullable zip_fopen_index(zip_t *_Nonnull, zip_uint64_t, zip_flags_t)
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 n
Definition: mipsasm.c:19
#define b(i)
Definition: sha256.c:42
Definition: zipcmp.c:60
Definition: zip.h:284
Definition: zipint.h:278
void error(const char *msg)
Definition: untgz.c:593
#define SEEK_SET
Definition: zip.c:88
uint64_t zip_uint64_t
Definition: zipconf.h:39
int64_t zip_int64_t
Definition: zipconf.h:38