blob: 2e0a49d2632945e0f1a77fc8f706819b26b05136 [file] [log] [blame]
Dieter Baron5149dfb2003-10-05 16:05:25 +00001/*
Thomas Klausnerea8ba492014-09-23 16:54:47 +02002 zip_error.c -- zip_error_t helper functions
Thomas Klausnerdf77dc62016-12-31 13:40:32 +01003 Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner
Dieter Baron5149dfb2003-10-05 16:05:25 +00004
5 This file is part of libzip, a library to manipulate ZIP archives.
Dieter Baronb86c4332007-11-07 14:35:13 +01006 The authors can be contacted at <libzip@nih.at>
Dieter Baron5149dfb2003-10-05 16:05:25 +00007
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
Dieter Baron5149dfb2003-10-05 16:05:25 +000034#include <stdlib.h>
35
Dieter Baron5149dfb2003-10-05 16:05:25 +000036#include "zipint.h"
37
Dieter Baron5149dfb2003-10-05 16:05:25 +000038
Thomas Klausnerea8ba492014-09-23 16:54:47 +020039ZIP_EXTERN int
40zip_error_code_system(const zip_error_t *error) {
41 return error->sys_err;
42}
43
44
45ZIP_EXTERN int
46zip_error_code_zip(const zip_error_t *error) {
47 return error->zip_err;
48}
49
50
51ZIP_EXTERN void
52zip_error_fini(zip_error_t *err)
53{
54 free(err->str);
55 err->str = NULL;
56}
57
58
59ZIP_EXTERN void
60zip_error_init(zip_error_t *err)
61{
62 err->zip_err = ZIP_ER_OK;
63 err->sys_err = 0;
64 err->str = NULL;
65}
66
Thomas Klausnerda1c2452014-12-02 16:01:26 +010067ZIP_EXTERN void
68zip_error_init_with_code(zip_error_t *error, int ze)
69{
70 zip_error_init(error);
71 error->zip_err = ze;
72 switch (zip_error_system_type(error)) {
73 case ZIP_ET_SYS:
74 error->sys_err = errno;
75 break;
76
77 default:
78 error->sys_err = 0;
79 break;
80 }
81}
82
Thomas Klausnerea8ba492014-09-23 16:54:47 +020083
84ZIP_EXTERN int
85zip_error_system_type(const zip_error_t *error) {
86 if (error->zip_err < 0 || error->zip_err >= _zip_nerr_str)
87 return ZIP_ET_NONE;
88
89 return _zip_err_type[error->zip_err];
90}
91
92
Dieter Baron5149dfb2003-10-05 16:05:25 +000093void
Thomas Klausnerea8ba492014-09-23 16:54:47 +020094_zip_error_clear(zip_error_t *err)
Dieter Baron81babc52006-10-04 15:21:09 +000095{
Thomas Klausneracfd96a2012-02-20 01:34:46 +010096 if (err == NULL)
97 return;
98
Dieter Baron81babc52006-10-04 15:21:09 +000099 err->zip_err = ZIP_ER_OK;
100 err->sys_err = 0;
101}
102
Dieter Baron81babc52006-10-04 15:21:09 +0000103
104void
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200105_zip_error_copy(zip_error_t *dst, const zip_error_t *src)
Dieter Baronadaf98c2003-10-06 16:37:42 +0000106{
Dieter Baron814ca952018-01-02 14:36:25 +0100107 if (dst == NULL) {
108 return;
109 }
110
Dieter Baronadaf98c2003-10-06 16:37:42 +0000111 dst->zip_err = src->zip_err;
112 dst->sys_err = src->sys_err;
113}
114
Dieter Baronadaf98c2003-10-06 16:37:42 +0000115
116void
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200117_zip_error_get(const zip_error_t *err, int *zep, int *sep)
Dieter Barone3f91ef2003-10-06 02:50:14 +0000118{
119 if (zep)
120 *zep = err->zip_err;
Dieter Barona87f0e82005-01-11 18:30:30 +0000121 if (sep) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200122 if (zip_error_system_type(err) != ZIP_ET_NONE)
Dieter Barona87f0e82005-01-11 18:30:30 +0000123 *sep = err->sys_err;
124 else
125 *sep = 0;
126 }
Dieter Barone3f91ef2003-10-06 02:50:14 +0000127}
Dieter Baron5149dfb2003-10-05 16:05:25 +0000128
Dieter Barone3f91ef2003-10-06 02:50:14 +0000129
130void
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200131zip_error_set(zip_error_t *err, int ze, int se)
Dieter Barone3f91ef2003-10-06 02:50:14 +0000132{
Dieter Baronb2ed74d2004-04-14 14:01:31 +0000133 if (err) {
134 err->zip_err = ze;
135 err->sys_err = se;
136 }
Dieter Barone3f91ef2003-10-06 02:50:14 +0000137}
Dieter Baron3291dac2009-03-12 15:56:36 +0100138
Dieter Baron3291dac2009-03-12 15:56:36 +0100139
140void
Dieter Baron1d9dfeb2014-09-28 23:02:54 +0200141_zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Dieter Baron3291dac2009-03-12 15:56:36 +0100142{
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200143 _zip_error_copy(err, zip_source_error(src));
144}
145
146
147zip_int64_t
148zip_error_to_data(const zip_error_t *error, void *data, zip_uint64_t length)
149{
150 int *e = (int *)data;
Dieter Baron3291dac2009-03-12 15:56:36 +0100151
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200152 if (length < sizeof(int)*2) {
153 return -1;
154 }
155
156 e[0] = zip_error_code_zip(error);
157 e[1] = zip_error_code_system(error);
158 return sizeof(int)*2;
Dieter Baron3291dac2009-03-12 15:56:36 +0100159}