Port gnupg_keylistiterator to use phpc

This commit is contained in:
Jakub Zelenka 2016-05-22 18:52:23 +01:00
parent bc85cdf849
commit 643bcd1ecf
4 changed files with 125 additions and 111 deletions

View file

@ -172,7 +172,7 @@ PHPC_OBJ_HANDLER_CREATE_EX(gnupg)
PHPC_OBJ_HANDLER_CREATE_EX_RETURN(gnupg); PHPC_OBJ_HANDLER_CREATE_EX_RETURN(gnupg);
} }
/* {{{ create gnupg_object */ /* {{{ create gnupg */
PHPC_OBJ_HANDLER_CREATE(gnupg) PHPC_OBJ_HANDLER_CREATE(gnupg)
{ {
PHPC_OBJ_HANDLER_CREATE_RETURN(gnupg); PHPC_OBJ_HANDLER_CREATE_RETURN(gnupg);
@ -302,7 +302,7 @@ PHP_MINIT_FUNCTION(gnupg)
{ {
zend_class_entry ce; zend_class_entry ce;
/* init classes */ /* init class */
INIT_CLASS_ENTRY(ce, "gnupg", gnupg_methods); INIT_CLASS_ENTRY(ce, "gnupg", gnupg_methods);
PHPC_CLASS_SET_HANDLER_CREATE(ce, gnupg); PHPC_CLASS_SET_HANDLER_CREATE(ce, gnupg);
gnupg_class_entry = PHPC_CLASS_REGISTER(ce); gnupg_class_entry = PHPC_CLASS_REGISTER(ce);

View file

@ -8,84 +8,91 @@
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
| Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> | | Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> |
| Copyright (c) 2013, Jim Jagielski <jimjag@php.net> | | Copyright (c) 2013, Jim Jagielski <jimjag@php.net> |
| Copyright (c) 2016, Jakub Zelenka <bukka@php.net> |
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
*/ */
/* $Id$ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
#include "php.h" #include <stddef.h>
#include "php.h"
#include "php_ini.h" #include "php_ini.h"
#include "ext/standard/info.h" #include "ext/standard/info.h"
#include "zend_interfaces.h" #include "zend_interfaces.h"
#include "zend_exceptions.h" #include "zend_exceptions.h"
#include "php_gnupg.h" #include "php_gnupg.h"
#include "php_gnupg_keylistiterator.h" #include "php_gnupg_keylistiterator.h"
#include "phpc/phpc.h"
static int le_gnupg_keylistiterator; static int le_gnupg_keylistiterator;
static zend_object_handlers gnupg_keylistiterator_object_handlers; static zend_class_entry *gnupg_keylistiterator_class_entry;
/* {{{ defs */ PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg_keylistiterator);
/* {{{ GNUPG_GET_ITERATOR */
#define GNUPG_GET_ITERATOR() \ #define GNUPG_GET_ITERATOR() \
zval *this = getThis(); \ zval *this = getThis(); \
gnupg_keylistiterator_object *intern = NULL; \ PHPC_THIS_DECLARE(gnupg_keylistiterator) = NULL; \
if(this){ \ do { \
intern = Z_KEYLISTITERATORO_P(getThis() TSRMLS_CC); \ if (this) { \
if(!intern){ \ PHPC_THIS_FETCH_FROM_ZVAL(gnupg_keylistiterator, this); \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized gnupg object"); \ if (!PHPC_THIS) { \
RETURN_FALSE; \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized gnupg object"); \
} \ RETURN_FALSE; \
} } \
} \
} while (0)
/* }}} */ /* }}} */
/* {{{ free_iterator_storage */ /* {{{ free gnupg_keylistiterator */
static void gnupg_keylistiterator_dtor(zend_object *z_obj TSRMLS_DC){ PHPC_OBJ_HANDLER_FREE(gnupg_keylistiterator)
gnupg_keylistiterator_object *intern; {
intern = keylistiterator_object_from_obj(z_obj); PHPC_OBJ_HANDLER_FREE_INIT(gnupg_keylistiterator);
gpgme_op_keylist_end(intern->ctx); gpgme_op_keylist_end(PHPC_THIS->ctx);
gpgme_key_release(intern->gpgkey); gpgme_key_release(PHPC_THIS->gpgkey);
gpgme_release(intern->ctx); gpgme_release(PHPC_THIS->ctx);
/* if (PHPC_THIS->pattern) {
zval_dtor(&intern->pattern); efree(PHPC_THIS->pattern);
*/ }
zend_object_std_dtor(&intern->zo);
efree(intern); PHPC_OBJ_HANDLER_FREE_DESTROY();
} }
/* }}} */
/* {{{ keylistiterator_objects_new */ /* {{{ create_ex gnupg_keylistiterator */
static zend_object* gnupg_keylistiterator_objects_new(zend_class_entry *class_type TSRMLS_DC){ PHPC_OBJ_HANDLER_CREATE_EX(gnupg_keylistiterator)
gnupg_keylistiterator_object *intern; {
gpgme_ctx_t ctx; PHPC_OBJ_HANDLER_CREATE_EX_INIT(gnupg_keylistiterator);
intern = ecalloc(1, sizeof(gnupg_keylistiterator_object) + zend_object_properties_size(class_type));
zend_object_std_init(&intern->zo, class_type);
object_properties_init(&intern->zo, class_type);
intern->zo.handlers = &gnupg_keylistiterator_object_handlers;
gpgme_check_version(NULL); gpgme_check_version(NULL);
gpgme_new(&ctx);
intern->ctx = ctx;
return &intern->zo;
}
/* }}} */
/* {{{ methodlist gnupg_keylistiterator */ 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);
}
/* {{{ method list gnupg_keylistiterator */
static zend_function_entry gnupg_keylistiterator_methods[] = { static zend_function_entry gnupg_keylistiterator_methods[] = {
PHP_ME(gnupg_keylistiterator, __construct, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, current, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, current, NULL, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, key, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, key, NULL, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, next, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, next, NULL, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, rewind, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, rewind, NULL, ZEND_ACC_PUBLIC)
PHP_ME(gnupg_keylistiterator, valid, NULL, ZEND_ACC_PUBLIC) PHP_ME(gnupg_keylistiterator, valid, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL} PHPC_FE_END
}; };
/* }}} */ /* }}} */
@ -95,84 +102,106 @@ int _gnupg_keylistiterator_init(INIT_FUNC_ARGS)
{ {
zend_class_entry ce; zend_class_entry ce;
/* init class */
INIT_CLASS_ENTRY(ce, "gnupg_keylistiterator", gnupg_keylistiterator_methods); 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);
ce.create_object = gnupg_keylistiterator_objects_new;
gnupg_keylistiterator_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
memcpy(&gnupg_keylistiterator_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
le_gnupg_keylistiterator = zend_register_list_destructors_ex(NULL, NULL, "ctx_keylistiterator", module_number); le_gnupg_keylistiterator = zend_register_list_destructors_ex(NULL, NULL, "ctx_keylistiterator", module_number);
zend_class_implements (gnupg_keylistiterator_class_entry TSRMLS_CC, 1, zend_ce_iterator);
return SUCCESS; return SUCCESS;
} }
/* }}} */ /* }}} */
/* {{{ proto __contruct(string $pattern)
PHP_METHOD(gnupg_keylistiterator,__construct){ * constructs keylistiterator with supplied pattern
zval *pattern = NULL; */
PHP_METHOD(gnupg_keylistiterator, __construct)
{
char *pattern = NULL;
phpc_str_size_t pattern_len;
int args = ZEND_NUM_ARGS(); int args = ZEND_NUM_ARGS();
GNUPG_GET_ITERATOR(); GNUPG_GET_ITERATOR();
if(args > 0){ if (args > 0) {
if (zend_parse_parameters(args TSRMLS_CC, "|z", &pattern) == FAILURE){ if (zend_parse_parameters(args TSRMLS_CC, "|s", &pattern, &pattern_len) == FAILURE) {
return; return;
} }
intern->pattern = *pattern; PHPC_THIS->pattern = estrdup(pattern);
zval_copy_ctor(&intern->pattern);
}else{
ZVAL_EMPTY_STRING(&intern->pattern);
} }
} }
PHP_METHOD(gnupg_keylistiterator,current){ /* }}} */
/* {{{ proto string current() */
PHP_METHOD(gnupg_keylistiterator,current)
{
GNUPG_GET_ITERATOR(); GNUPG_GET_ITERATOR();
RETURN_STRING(intern->gpgkey->uids[0].uid); PHPC_CSTR_RETURN(PHPC_THIS->gpgkey->uids[0].uid);
} }
/* }}} */
PHP_METHOD(gnupg_keylistiterator,key){ /* {{{ proto string key() */
GNUPG_GET_ITERATOR(); PHP_METHOD(gnupg_keylistiterator,key)
{
RETURN_STRING(intern->gpgkey->subkeys[0].fpr);
}
PHP_METHOD(gnupg_keylistiterator,next){
GNUPG_GET_ITERATOR(); GNUPG_GET_ITERATOR();
if(intern->gpgkey){ PHPC_CSTR_RETURN(PHPC_THIS->gpgkey->subkeys[0].fpr);
gpgme_key_release(intern->gpgkey); }
/* }}} */
/* {{{ proto bool next() */
PHP_METHOD(gnupg_keylistiterator,next)
{
GNUPG_GET_ITERATOR();
if (PHPC_THIS->gpgkey){
gpgme_key_release(PHPC_THIS->gpgkey);
} }
if((intern->err = gpgme_op_keylist_next(intern->ctx, &intern->gpgkey))){ if ((PHPC_THIS->err = gpgme_op_keylist_next(PHPC_THIS->ctx, &PHPC_THIS->gpgkey))) {
gpgme_key_release(intern->gpgkey); gpgme_key_release(PHPC_THIS->gpgkey);
intern->gpgkey = NULL; PHPC_THIS->gpgkey = NULL;
} }
RETURN_TRUE; RETURN_TRUE;
} }
/* }}} */
PHP_METHOD(gnupg_keylistiterator,rewind){ /* {{{ proto bool rewind() */
PHP_METHOD(gnupg_keylistiterator,rewind)
{
GNUPG_GET_ITERATOR(); GNUPG_GET_ITERATOR();
if((intern->err = gpgme_op_keylist_start(intern->ctx, Z_STRVAL(intern->pattern), 0)) != GPG_ERR_NO_ERROR){ if ((PHPC_THIS->err = gpgme_op_keylist_start(
zend_throw_exception(zend_exception_get_default(TSRMLS_C),(char *)gpg_strerror(intern->err),1 TSRMLS_CC); 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((intern->err = gpgme_op_keylist_next(intern->ctx, &intern->gpgkey))!=GPG_ERR_NO_ERROR){ if ((PHPC_THIS->err = gpgme_op_keylist_next(PHPC_THIS->ctx, &PHPC_THIS->gpgkey)) != GPG_ERR_NO_ERROR){
RETURN_FALSE; RETURN_FALSE;
} }
RETURN_TRUE; RETURN_TRUE;
} }
/* }}} */
PHP_METHOD(gnupg_keylistiterator,valid){ /* {{{ proto bool valid() */
PHP_METHOD(gnupg_keylistiterator,valid)
{
GNUPG_GET_ITERATOR(); GNUPG_GET_ITERATOR();
if(intern->gpgkey!=NULL){ if (PHPC_THIS->gpgkey != NULL) {
RETURN_TRUE; RETURN_TRUE;
}else{ } else {
RETURN_FALSE; RETURN_FALSE;
} }
} }
/* }}} */
/* /*
* Local variables: * Local variables:

View file

@ -8,11 +8,10 @@
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
| Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> | | Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> |
| Copyright (c) 2013, Jim Jagielski <jimjag@php.net> | | Copyright (c) 2013, Jim Jagielski <jimjag@php.net> |
| Copyright (c) 2016, Jakub Zelenka <bukka@php.net> |
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
*/ */
/* $Id$ */
#ifndef PHP_GNUPG_H #ifndef PHP_GNUPG_H
#define PHP_GNUPG_H #define PHP_GNUPG_H

View file

@ -8,11 +8,10 @@
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
| Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> | | Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> |
| Copyright (c) 2013, Jim Jagielski <jimjag@php.net> | | Copyright (c) 2013, Jim Jagielski <jimjag@php.net> |
| Copyright (c) 2016, Jakub Zelenka <bukka@php.net> |
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
*/ */
/* $Id$ */
#ifndef PHP_GNUPG_KEYLISTITERATOR_H #ifndef PHP_GNUPG_KEYLISTITERATOR_H
#define PHP_GNUPG_KEYLISTITERATOR_H #define PHP_GNUPG_KEYLISTITERATOR_H
@ -29,29 +28,17 @@ extern zend_module_entry gnupg_keyiterator_module_entry;
#endif #endif
#include <gpgme.h> #include <gpgme.h>
#include "phpc/phpc.h"
#define gnupg_keylistiterator_init() _gnupg_keylistiterator_init(INIT_FUNC_ARGS_PASSTHRU) #define gnupg_keylistiterator_init() _gnupg_keylistiterator_init(INIT_FUNC_ARGS_PASSTHRU)
extern int _gnupg_keylistiterator_init(INIT_FUNC_ARGS); extern int _gnupg_keylistiterator_init(INIT_FUNC_ARGS);
typedef struct _gnupg_keylistiterator_object{ PHPC_OBJ_STRUCT_BEGIN(gnupg_keylistiterator)
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t err; gpgme_error_t err;
gpgme_key_t gpgkey; gpgme_key_t gpgkey;
zval pattern; char *pattern;
zend_object zo; PHPC_OBJ_STRUCT_END()
} gnupg_keylistiterator_object;
static zend_class_entry *gnupg_keylistiterator_class_entry;
static inline gnupg_keylistiterator_object *keylistiterator_object_from_obj(zend_object *obj) /* {{{ */ {
return (gnupg_keylistiterator_object*)((char*)(obj) - XtOffsetOf(gnupg_keylistiterator_object, zo));
}
/* }}} */
static inline gnupg_keylistiterator_object *Z_KEYLISTITERATORO_P(zval *zv) /* {{{ */ {
return keylistiterator_object_from_obj(Z_OBJ_P((zv)));
}
/* }}} */
PHP_METHOD(gnupg_keylistiterator, __construct); PHP_METHOD(gnupg_keylistiterator, __construct);
PHP_METHOD(gnupg_keylistiterator, current); PHP_METHOD(gnupg_keylistiterator, current);
@ -60,7 +47,6 @@ PHP_METHOD(gnupg_keylistiterator, rewind);
PHP_METHOD(gnupg_keylistiterator, key); PHP_METHOD(gnupg_keylistiterator, key);
PHP_METHOD(gnupg_keylistiterator, valid); PHP_METHOD(gnupg_keylistiterator, valid);
#ifdef ZTS #ifdef ZTS
#define GNUPG_G(v) TSRMG(gnupg_globals_id, zend_gnupg_globals *, v) #define GNUPG_G(v) TSRMG(gnupg_globals_id, zend_gnupg_globals *, v)
#else #else