Coverage Report

Created: 2025-10-13 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_name_locate.c
Line
Count
Source
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
43
ZIP_EXTERN zip_int64_t
44
0
zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags) {
45
0
    return _zip_name_locate(za, fname, flags, &za->error);
46
0
}
47
48
49
zip_int64_t
50
1.93k
_zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *error) {
51
1.93k
    int (*cmp)(const char *, const char *);
52
1.93k
    size_t fname_length;
53
1.93k
    zip_string_t *str = NULL;
54
1.93k
    const char *fn, *p;
55
1.93k
    zip_uint64_t i;
56
57
1.93k
    if (za == NULL) {
58
0
        return -1;
59
0
    }
60
61
1.93k
    if (fname == NULL) {
62
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
63
0
        return -1;
64
0
    }
65
66
1.93k
    fname_length = strlen(fname);
67
68
1.93k
    if (fname_length > ZIP_UINT16_MAX) {
69
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
70
0
        return -1;
71
0
    }
72
73
1.93k
    if ((flags & (ZIP_FL_ENC_UTF_8 | ZIP_FL_ENC_RAW)) == 0 && fname[0] != '\0') {
74
1.93k
        if ((str = _zip_string_new((const zip_uint8_t *)fname, (zip_uint16_t)strlen(fname), flags, error)) == NULL) {
75
0
            return -1;
76
0
        }
77
1.93k
        if ((fname = (const char *)_zip_string_get(str, NULL, 0, error)) == NULL) {
78
0
            _zip_string_free(str);
79
0
            return -1;
80
0
        }
81
1.93k
    }
82
83
1.93k
    if (flags & (ZIP_FL_NOCASE | ZIP_FL_NODIR | ZIP_FL_ENC_RAW | ZIP_FL_ENC_STRICT)) {
84
        /* can't use hash table */
85
0
        cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
86
87
0
        for (i = 0; i < za->nentry; i++) {
88
0
            fn = _zip_get_name(za, i, flags, error);
89
90
            /* newly added (partially filled) entry or error */
91
0
            if (fn == NULL)
92
0
                continue;
93
94
0
            if (flags & ZIP_FL_NODIR) {
95
0
                p = strrchr(fn, '/');
96
0
                if (p)
97
0
                    fn = p + 1;
98
0
            }
99
100
0
            if (cmp(fname, fn) == 0) {
101
0
                _zip_error_clear(error);
102
0
                _zip_string_free(str);
103
0
                return (zip_int64_t)i;
104
0
            }
105
0
        }
106
107
0
        zip_error_set(error, ZIP_ER_NOENT, 0);
108
0
        _zip_string_free(str);
109
0
        return -1;
110
0
    }
111
1.93k
    else {
112
1.93k
        zip_int64_t ret = _zip_hash_lookup(za->names, (const zip_uint8_t *)fname, flags, error);
113
1.93k
        _zip_string_free(str);
114
1.93k
        return ret;
115
1.93k
    }
116
1.93k
}