Coverage Report

Created: 2025-11-16 06:38

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-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
#include <stdlib.h>
35
36
#include "zipint.h"
37
38
39
ZIP_EXTERN int
40
85.8k
zip_error_code_system(const zip_error_t *error) {
41
85.8k
    return error->sys_err;
42
85.8k
}
43
44
45
ZIP_EXTERN int
46
219k
zip_error_code_zip(const zip_error_t *error) {
47
219k
    return error->zip_err;
48
219k
}
49
50
51
ZIP_EXTERN void
52
146k
zip_error_fini(zip_error_t *err) {
53
146k
    free(err->str);
54
146k
    err->str = NULL;
55
146k
}
56
57
58
ZIP_EXTERN void
59
359k
zip_error_init(zip_error_t *err) {
60
359k
    err->zip_err = ZIP_ER_OK;
61
359k
    err->sys_err = 0;
62
359k
    err->str = NULL;
63
359k
}
64
65
ZIP_EXTERN void
66
4.32k
zip_error_init_with_code(zip_error_t *error, int ze) {
67
4.32k
    zip_error_init(error);
68
4.32k
    error->zip_err = ze;
69
4.32k
    switch (zip_error_system_type(error)) {
70
126
        case ZIP_ET_SYS:
71
1.69k
        case ZIP_ET_LIBZIP:
72
1.69k
            error->sys_err = errno;
73
1.69k
            break;
74
            
75
2.63k
        default:
76
2.63k
            error->sys_err = 0;
77
2.63k
            break;
78
4.32k
    }
79
4.32k
}
80
81
82
ZIP_EXTERN int
83
6.91k
zip_error_system_type(const zip_error_t *error) {
84
6.91k
    if (error->zip_err < 0 || error->zip_err >= _zip_err_str_count)
85
0
        return ZIP_ET_NONE;
86
87
6.91k
    return _zip_err_str[error->zip_err].type;
88
6.91k
}
89
90
91
void
92
150k
_zip_error_clear(zip_error_t *err) {
93
150k
    if (err == NULL)
94
0
        return;
95
96
150k
    err->zip_err = ZIP_ER_OK;
97
150k
    err->sys_err = 0;
98
150k
}
99
100
101
void
102
104k
_zip_error_copy(zip_error_t *dst, const zip_error_t *src) {
103
104k
    if (dst == NULL) {
104
0
        return;
105
0
    }
106
107
104k
    dst->zip_err = src->zip_err;
108
104k
    dst->sys_err = src->sys_err;
109
104k
}
110
111
112
void
113
0
_zip_error_get(const zip_error_t *err, int *zep, int *sep) {
114
0
    if (zep)
115
0
        *zep = err->zip_err;
116
0
    if (sep) {
117
0
        if (zip_error_system_type(err) != ZIP_ET_NONE)
118
0
            *sep = err->sys_err;
119
0
        else
120
0
            *sep = 0;
121
0
    }
122
0
}
123
124
125
void
126
399k
zip_error_set(zip_error_t *err, int ze, int se) {
127
399k
    if (err) {
128
208k
        err->zip_err = ze;
129
208k
        err->sys_err = se;
130
208k
    }
131
399k
}
132
133
134
void
135
93.7k
zip_error_set_from_source(zip_error_t *err, zip_source_t *src) {
136
93.7k
    if (src == NULL) {
137
0
        zip_error_set(err, ZIP_ER_INVAL, 0);
138
0
        return;
139
0
    }
140
141
93.7k
    _zip_error_copy(err, zip_source_error(src));
142
93.7k
}
143
144
145
zip_int64_t
146
81.1k
zip_error_to_data(const zip_error_t *error, void *data, zip_uint64_t length) {
147
81.1k
    int *e = (int *)data;
148
149
81.1k
    if (length < sizeof(int) * 2) {
150
0
        return -1;
151
0
    }
152
153
81.1k
    e[0] = zip_error_code_zip(error);
154
81.1k
    e[1] = zip_error_code_system(error);
155
81.1k
    return sizeof(int) * 2;
156
81.1k
}