Rizin
unix-like reverse engineering framework and cli tools
zip_name_locate.c
Go to the documentation of this file.
1 /*
2  zip_name_locate.c -- get index by name
3  Copyright (C) 1999-2022 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 <info@libzip.org>
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 
35 #include <string.h>
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39 
40 #include "zipint.h"
41 
42 
45  return _zip_name_locate(za, fname, flags, &za->error);
46 }
47 
48 
51  int (*cmp)(const char *, const char *);
53  const char *fn, *p;
55 
56  if (za == NULL)
57  return -1;
58 
59  if (fname == NULL) {
61  return -1;
62  }
63 
64  if ((flags & (ZIP_FL_ENC_UTF_8 | ZIP_FL_ENC_RAW)) == 0 && fname[0] != '\0') {
65  if ((str = _zip_string_new((const zip_uint8_t *)fname, strlen(fname), flags, error)) == NULL) {
66  return -1;
67  }
68  if ((fname = (const char *)_zip_string_get(str, NULL, 0, error)) == NULL) {
70  return -1;
71  }
72  }
73 
75  /* can't use hash table */
76  cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
77 
78  for (i = 0; i < za->nentry; i++) {
80 
81  /* newly added (partially filled) entry or error */
82  if (fn == NULL)
83  continue;
84 
85  if (flags & ZIP_FL_NODIR) {
86  p = strrchr(fn, '/');
87  if (p)
88  fn = p + 1;
89  }
90 
91  if (cmp(fname, fn) == 0) {
94  return (zip_int64_t)i;
95  }
96  }
97 
100  return -1;
101  }
102  else {
105  return ret;
106  }
107 }
lzma_index ** i
Definition: index.h:629
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
#define NULL
Definition: cris-opc.c:27
void * p
Definition: libc.cpp:67
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_FL_NODIR
Definition: zip.h:77
#define ZIP_ER_NOENT
Definition: zip.h:114
#define ZIP_EXTERN
Definition: zip.h:54
#define ZIP_FL_ENC_STRICT
Definition: zip.h:84
#define ZIP_ER_INVAL
Definition: zip.h:123
zip_uint32_t zip_flags_t
Definition: zip.h:347
#define ZIP_FL_ENC_RAW
Definition: zip.h:83
#define ZIP_FL_ENC_UTF_8
Definition: zip.h:88
#define ZIP_FL_NOCASE
Definition: zip.h:76
static const char struct stat static buf struct stat static buf static idle const char static path static fd const char static len const void static prot const char struct module static image struct kernel_sym static table unsigned char static buf static fsuid unsigned struct dirent unsigned static count const struct iovec static count static pid const void static len static flags const struct sched_param static p static pid static policy struct timespec static tp static suid unsigned fn
Definition: sflib.h:186
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
Definition: zip.h:284
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
zip_uint64_t nentry
Definition: zipint.h:292
zip_hash_t * names
Definition: zipint.h:300
void error(const char *msg)
Definition: untgz.c:593
void _zip_error_clear(zip_error_t *err)
Definition: zip_error.c:92
const char * _zip_get_name(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error)
Definition: zip_get_name.c:47
zip_int64_t _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error)
Definition: zip_hash.c:312
ZIP_EXTERN zip_int64_t zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags)
zip_int64_t _zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *error)
zip_string_t * _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:121
void _zip_string_free(zip_string_t *s)
Definition: zip_string.c:69
const zip_uint8_t * _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:80
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
zip_t * za
Definition: ziptool.c:79