Coverage Report

Created: 2026-07-30 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_error.c
Line
Count
Source
1
/*
2
  zip_error.c -- zip_error_t helper functions
3
  Copyright (C) 1999-2024 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
#include <stdlib.h>
35
36
#include "zipint.h"
37
38
39
8.85k
ZIP_EXTERN int zip_error_code_system(const zip_error_t *error) {
40
8.85k
    return error->sys_err;
41
8.85k
}
42
43
44
17.3k
ZIP_EXTERN int zip_error_code_zip(const zip_error_t *error) {
45
17.3k
    return error->zip_err;
46
17.3k
}
47
48
49
6.30k
ZIP_EXTERN void zip_error_fini(zip_error_t *err) {
50
6.30k
    free(err->str);
51
6.30k
    err->str = NULL;
52
6.30k
}
53
54
55
18.1k
ZIP_EXTERN void zip_error_init(zip_error_t *err) {
56
18.1k
    err->zip_err = ZIP_ER_OK;
57
18.1k
    err->sys_err = 0;
58
18.1k
    err->str = NULL;
59
18.1k
}
60
61
0
ZIP_EXTERN void zip_error_init_with_code(zip_error_t *error, int ze) {
62
0
    zip_error_init(error);
63
0
    error->zip_err = ze;
64
0
    switch (zip_error_system_type(error)) {
65
0
    case ZIP_ET_SYS:
66
0
    case ZIP_ET_LIBZIP:
67
0
        error->sys_err = errno;
68
0
        break;
69
70
0
    default:
71
0
        error->sys_err = 0;
72
0
        break;
73
0
    }
74
0
}
75
76
77
1.38k
ZIP_EXTERN int zip_error_system_type(const zip_error_t *error) {
78
1.38k
    if (error->zip_err < 0 || error->zip_err >= _zip_err_str_count) {
79
0
        return ZIP_ET_NONE;
80
0
    }
81
82
1.38k
    return _zip_err_str[error->zip_err].type;
83
1.38k
}
84
85
86
8.06k
void _zip_error_clear(zip_error_t *err) {
87
8.06k
    if (err == NULL) {
88
0
        return;
89
0
    }
90
91
8.06k
    err->zip_err = ZIP_ER_OK;
92
8.06k
    err->sys_err = 0;
93
8.06k
}
94
95
96
2.59k
void _zip_error_copy(zip_error_t *dst, const zip_error_t *src) {
97
2.59k
    if (dst == NULL) {
98
0
        return;
99
0
    }
100
101
2.59k
    dst->zip_err = src->zip_err;
102
2.59k
    dst->sys_err = src->sys_err;
103
2.59k
}
104
105
106
0
void _zip_error_get(const zip_error_t *err, int *zep, int *sep) {
107
0
    if (zep) {
108
0
        *zep = err->zip_err;
109
0
    }
110
0
    if (sep) {
111
0
        if (zip_error_system_type(err) != ZIP_ET_NONE) {
112
0
            *sep = err->sys_err;
113
0
        }
114
0
        else {
115
0
            *sep = 0;
116
0
        }
117
0
    }
118
0
}
119
120
121
32.5k
void zip_error_set(zip_error_t *err, int ze, int se) {
122
32.5k
    if (err) {
123
16.5k
        err->zip_err = ze;
124
16.5k
        err->sys_err = se;
125
16.5k
    }
126
32.5k
}
127
128
129
1.93k
void zip_error_set_from_source(zip_error_t *err, zip_source_t *src) {
130
1.93k
    if (src == NULL) {
131
0
        zip_error_set(err, ZIP_ER_INVAL, 0);
132
0
        return;
133
0
    }
134
135
1.93k
    _zip_error_copy(err, zip_source_error(src));
136
1.93k
}
137
138
139
8.61k
zip_int64_t zip_error_to_data(const zip_error_t *error, void *data, zip_uint64_t length) {
140
8.61k
    int *e = (int *)data;
141
142
8.61k
    if (length < sizeof(int) * 2) {
143
0
        return -1;
144
0
    }
145
146
8.61k
    e[0] = zip_error_code_zip(error);
147
8.61k
    e[1] = zip_error_code_system(error);
148
8.61k
    return sizeof(int) * 2;
149
8.61k
}