forked from cataphract/php-rar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rar_error.c
261 lines (236 loc) · 7.53 KB
/
rar_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
| |
| **** WARNING **** |
| |
| This module makes use of unRAR - free utility for RAR archives. |
| Its license states that you MUST NOT use its code to develop |
| a RAR (WinRAR) compatible archiver. |
| Please, read unRAR license for full information. |
| unRAR & RAR copyrights are owned by Eugene Roshal |
+----------------------------------------------------------------------+
| Author: Antony Dovgal <[email protected]> |
| Author: Gustavo Lopes <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <php.h>
#include <zend_exceptions.h>
#include "php_rar.h"
/* {{{ Globals with external linkage */
zend_class_entry *rarexception_ce_ptr;
/* }}} */
/* Functions with external linkage {{{ */
/* Functions with external linkage {{{ */
int _rar_handle_error(int errcode TSRMLS_DC) /* {{{ */
{
return _rar_handle_error_ex("", errcode TSRMLS_CC);
}
/* }}} */
int _rar_handle_error_ex(const char *preamble, int errcode TSRMLS_DC) /* {{{ */
{
const char *err = _rar_error_to_string(errcode);
if (err == NULL) {
return SUCCESS;
}
if (_rar_using_exceptions(TSRMLS_C)) {
zend_throw_exception_ex(rarexception_ce_ptr, errcode TSRMLS_CC,
"unRAR internal error: %s%s", preamble, err);
}
else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s%s", preamble, err);
}
return FAILURE;
}
/* }}} */
/* Errors not related to the unRAR library */
void _rar_handle_ext_error(const char *format TSRMLS_DC, ...) /* {{{ */
{
va_list arg;
char *message;
#if defined(ZTS) && PHP_MAJOR_VERSION < 7
va_start(arg, TSRMLS_C);
#else
va_start(arg, format);
#endif
vspprintf(&message, 0, format, arg);
va_end(arg);
if (_rar_using_exceptions(TSRMLS_C))
zend_throw_exception(rarexception_ce_ptr, message, -1L TSRMLS_CC);
else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
efree(message);
}
/* }}} */
int _rar_using_exceptions(TSRMLS_D)
{
zval *pval;
pval = zend_read_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 1 TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
assert(Z_TYPE_P(pval) == IS_BOOL);
return Z_BVAL_P(pval);
#else
assert(Z_TYPE_P(pval) == IS_TRUE || Z_TYPE_P(pval) == IS_FALSE);
return Z_TYPE_P(pval) == IS_TRUE;
#endif
}
/* returns a string or NULL if not an error */
const char * _rar_error_to_string(int errcode) /* {{{ */
{
const char *ret;
switch (errcode) {
case 0:
/* no error */
case 1:
/* no error (comment completely read) */
case ERAR_END_ARCHIVE:
/* no error */
ret = NULL;
break;
case ERAR_NO_MEMORY:
ret = "ERAR_NO_MEMORY (not enough memory)";
break;
case ERAR_BAD_DATA:
ret = "ERAR_BAD_DATA";
break;
case ERAR_BAD_ARCHIVE:
ret = "ERAR_BAD_ARCHIVE";
break;
case ERAR_UNKNOWN_FORMAT:
ret = "ERAR_UNKNOWN_FORMAT";
break;
case ERAR_EOPEN:
ret = "ERAR_EOPEN (file open error)";
break;
case ERAR_ECREATE:
ret = "ERAR_ECREATE";
break;
case ERAR_ECLOSE:
ret = "ERAR_ECLOSE (error closing file)";
break;
case ERAR_EREAD:
ret = "ERAR_EREAD";
break;
case ERAR_EWRITE:
ret = "ERAR_EWRITE";
break;
case ERAR_SMALL_BUF:
ret = "ERAR_SMALL_BUF";
break;
case ERAR_UNKNOWN:
ret = "ERAR_UNKNOWN (unknown RAR error)";
break;
case ERAR_MISSING_PASSWORD:
ret = "ERAR_MISSING_PASSWORD (password needed but not specified)";
break;
default:
ret = "unknown RAR error (should not happen)";
break;
}
return ret;
}
/* }}} */
/* }}} */
/* {{{ proto bool RarException::setUsingExceptions(using_exceptions)
Set whether exceptions are to be used */
PHP_METHOD(rarexception, setUsingExceptions)
{
zend_bool argval;
int result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &argval) == FAILURE ) {
return;
}
result = zend_update_static_property_bool(rarexception_ce_ptr,
"usingExceptions", sizeof("usingExceptions") -1,
(long) argval TSRMLS_CC);
if (result == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Could not set error handling mode. "
"This is a bug, please report it.");
return;
}
}
/* }}} */
/* {{{ proto bool RarException::isUsingExceptions()
Return whether exceptions are being used */
PHP_METHOD(rarexception, isUsingExceptions)
{
#if PHP_MAJOR_VERSION < 7
zval **pval;
#else
zval *pval;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE ) {
return;
}
/* or zend_read_static_property, which calls zend_std_get... after chg scope */
#if PHP_VERSION_ID < 50399
pval = zend_std_get_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 0 TSRMLS_CC);
#elif PHP_MAJOR_VERSION < 7
pval = zend_std_get_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 0, NULL TSRMLS_CC);
#else
zend_string *prop_name =
zend_string_init("usingExceptions", sizeof("usingExceptions") - 1, 0);
pval = zend_std_get_static_property(rarexception_ce_ptr, prop_name,
(zend_bool) 0);
zend_string_release(prop_name);
#endif
/* property always exists */
assert(pval != NULL);
#if PHP_MAJOR_VERSION < 7
assert(Z_TYPE_PP(pval) == IS_BOOL);
RETURN_ZVAL(*pval, 0, 0);
#else
assert(Z_TYPE_P(pval) == IS_TRUE || Z_TYPE_P(pval) == IS_FALSE);
RETURN_ZVAL(pval, 0, 0);
#endif
}
/* }}} */
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_rarexception_sue, 0, 0, 1)
ZEND_ARG_INFO(0, using_exceptions)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_rarexception_void, 0)
ZEND_END_ARG_INFO()
/* }}} */
static zend_function_entry php_rarexception_class_functions[] = {
PHP_ME(rarexception, setUsingExceptions, arginfo_rarexception_sue, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(rarexception, isUsingExceptions, arginfo_rarexception_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};
void minit_rarerror(TSRMLS_D) /* {{{ */
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "RarException", php_rarexception_class_functions);
#if PHP_MAJOR_VERSION < 7
rarexception_ce_ptr = zend_register_internal_class_ex(&ce,
zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
#else
rarexception_ce_ptr = zend_register_internal_class_ex(&ce,
zend_exception_get_default(TSRMLS_C));
#endif
rarexception_ce_ptr->ce_flags |= ZEND_ACC_FINAL;
zend_declare_property_bool(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, 0L /* FALSE */,
ZEND_ACC_PRIVATE | ZEND_ACC_STATIC TSRMLS_CC);
}
/* }}} */
#ifdef __cplusplus
}
#endif