forked from aphixsoftware/php-gnupg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnupg_keylistiterator.c
216 lines (178 loc) · 5.61 KB
/
gnupg_keylistiterator.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
/*
+--------------------------------------------------------------------+
| PECL :: gnupg |
+--------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
| Copyright (c) 2006, Thilo Raufeisen <[email protected]> |
| Copyright (c) 2013, Jim Jagielski <[email protected]> |
| Copyright (c) 2016, Jakub Zelenka <[email protected]> |
+--------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "php_gnupg.h"
#include "php_gnupg_keylistiterator.h"
#include "phpc/phpc.h"
static int le_gnupg_keylistiterator;
static zend_class_entry *gnupg_keylistiterator_class_entry;
PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg_keylistiterator);
/* {{{ GNUPG_GET_ITERATOR */
#define GNUPG_GET_ITERATOR() \
zval *this = getThis(); \
PHPC_THIS_DECLARE(gnupg_keylistiterator) = NULL; \
do { \
if (this) { \
PHPC_THIS_FETCH_FROM_ZVAL(gnupg_keylistiterator, this); \
if (!PHPC_THIS) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized gnupg object"); \
RETURN_FALSE; \
} \
} \
} while (0)
/* }}} */
/* {{{ free gnupg_keylistiterator */
PHPC_OBJ_HANDLER_FREE(gnupg_keylistiterator)
{
PHPC_OBJ_HANDLER_FREE_INIT(gnupg_keylistiterator);
gpgme_op_keylist_end(PHPC_THIS->ctx);
gpgme_key_release(PHPC_THIS->gpgkey);
gpgme_release(PHPC_THIS->ctx);
if (PHPC_THIS->pattern) {
efree(PHPC_THIS->pattern);
}
PHPC_OBJ_HANDLER_FREE_DESTROY();
}
/* {{{ create_ex gnupg_keylistiterator */
PHPC_OBJ_HANDLER_CREATE_EX(gnupg_keylistiterator)
{
PHPC_OBJ_HANDLER_CREATE_EX_INIT(gnupg_keylistiterator);
gpgme_check_version(NULL);
gpgme_new(&PHPC_THIS->ctx);
PHPC_THIS->err = 0;
PHPC_THIS->gpgkey = NULL;
PHPC_THIS->pattern = NULL;
PHPC_OBJ_HANDLER_CREATE_EX_RETURN(gnupg_keylistiterator);
}
/* {{{ create gnupg_keylistiterator */
PHPC_OBJ_HANDLER_CREATE(gnupg_keylistiterator)
{
PHPC_OBJ_HANDLER_CREATE_RETURN(gnupg_keylistiterator);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_void, 0, 0, 0)
ZEND_END_ARG_INFO()
/* {{{ method list gnupg_keylistiterator */
static zend_function_entry gnupg_keylistiterator_methods[] = {
PHP_ME(gnupg_keylistiterator, __construct, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, current, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, key, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, next, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, rewind, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, valid, arginfo_void, ZEND_ACC_PUBLIC)
PHPC_FE_END
};
/* }}} */
/* {{{ _gnupg_keylistiterator_init
*/
int _gnupg_keylistiterator_init(INIT_FUNC_ARGS)
{
zend_class_entry ce;
/* init class */
INIT_CLASS_ENTRY(ce, "gnupg_keylistiterator", gnupg_keylistiterator_methods);
PHPC_CLASS_SET_HANDLER_CREATE(ce, gnupg_keylistiterator);
gnupg_keylistiterator_class_entry = PHPC_CLASS_REGISTER(ce);
PHPC_OBJ_INIT_HANDLERS(gnupg_keylistiterator);
PHPC_OBJ_SET_HANDLER_OFFSET(gnupg_keylistiterator);
PHPC_OBJ_SET_HANDLER_FREE(gnupg_keylistiterator);
zend_class_implements(gnupg_keylistiterator_class_entry TSRMLS_CC, 1, zend_ce_iterator);
le_gnupg_keylistiterator = zend_register_list_destructors_ex(NULL, NULL, "ctx_keylistiterator", module_number);
return SUCCESS;
}
/* }}} */
/* {{{ proto __contruct(string $pattern)
* constructs keylistiterator with supplied pattern
*/
PHP_METHOD(gnupg_keylistiterator, __construct)
{
char *pattern = NULL;
phpc_str_size_t pattern_len;
int args = ZEND_NUM_ARGS();
GNUPG_GET_ITERATOR();
if (args > 0) {
if (zend_parse_parameters(args TSRMLS_CC, "|s", &pattern, &pattern_len) == FAILURE) {
return;
}
PHPC_THIS->pattern = estrdup(pattern);
}
}
/* }}} */
/* {{{ proto string current() */
PHP_METHOD(gnupg_keylistiterator,current)
{
GNUPG_GET_ITERATOR();
PHPC_CSTR_RETURN(PHPC_THIS->gpgkey->uids[0].uid);
}
/* }}} */
/* {{{ proto string key() */
PHP_METHOD(gnupg_keylistiterator,key)
{
GNUPG_GET_ITERATOR();
PHPC_CSTR_RETURN(PHPC_THIS->gpgkey->subkeys[0].fpr);
}
/* }}} */
/* {{{ proto bool next() */
PHP_METHOD(gnupg_keylistiterator,next)
{
GNUPG_GET_ITERATOR();
if (PHPC_THIS->gpgkey){
gpgme_key_release(PHPC_THIS->gpgkey);
}
if ((PHPC_THIS->err = gpgme_op_keylist_next(PHPC_THIS->ctx, &PHPC_THIS->gpgkey))) {
gpgme_key_release(PHPC_THIS->gpgkey);
PHPC_THIS->gpgkey = NULL;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool rewind() */
PHP_METHOD(gnupg_keylistiterator,rewind)
{
GNUPG_GET_ITERATOR();
if ((PHPC_THIS->err = gpgme_op_keylist_start(
PHPC_THIS->ctx, PHPC_THIS->pattern ? PHPC_THIS->pattern : "", 0)) != GPG_ERR_NO_ERROR){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), (char *)gpg_strerror(PHPC_THIS->err), 1 TSRMLS_CC);
}
if ((PHPC_THIS->err = gpgme_op_keylist_next(PHPC_THIS->ctx, &PHPC_THIS->gpgkey)) != GPG_ERR_NO_ERROR){
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool valid() */
PHP_METHOD(gnupg_keylistiterator,valid)
{
GNUPG_GET_ITERATOR();
if (PHPC_THIS->gpgkey != NULL) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/