Finish porting gnupg.c using phpc

This commit is contained in:
Jakub Zelenka 2016-05-22 17:54:19 +01:00
parent 4229b54fd6
commit bc85cdf849
3 changed files with 306 additions and 309 deletions

123
gnupg.c
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$ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
@ -30,6 +29,8 @@
static int le_gnupg; static int le_gnupg;
static zend_class_entry *gnupg_class_entry;
PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg); PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg);
/* {{{ GNUPG_GETOBJ */ /* {{{ GNUPG_GETOBJ */
@ -466,17 +467,18 @@ gpgme_error_t passphrase_decrypt_cb (
/* {{{ gnupg_fetchsignatures */ /* {{{ gnupg_fetchsignatures */
int gnupg_fetchsignatures(gpgme_signature_t gpgme_signatures, zval *main_arr) int gnupg_fetchsignatures(gpgme_signature_t gpgme_signatures, zval *main_arr)
{ {
zval sig_arr; phpc_val sig_arr;
PHPC_ARRAY_INIT(main_arr); PHPC_ARRAY_INIT(main_arr);
while (gpgme_signatures) { while (gpgme_signatures) {
PHPC_ARRAY_INIT(&sig_arr); PHPC_VAL_MAKE(sig_arr);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "fingerprint", gpgme_signatures->fpr); PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sig_arr));
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "validity", gpgme_signatures->validity); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "fingerprint", gpgme_signatures->fpr);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "timestamp", gpgme_signatures->timestamp); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "validity", gpgme_signatures->validity);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "status", gpgme_signatures->status); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "timestamp", gpgme_signatures->timestamp);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "summary", gpgme_signatures->summary); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "status", gpgme_signatures->status);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(main_arr, &sig_arr); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "summary", gpgme_signatures->summary);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(main_arr, PHPC_VAL_CAST_TO_PZVAL(sig_arr));
gpgme_signatures = gpgme_signatures->next; gpgme_signatures = gpgme_signatures->next;
} }
@ -565,7 +567,7 @@ PHP_FUNCTION(gnupg_seterrormode)
*/ */
PHP_FUNCTION(gnupg_setsignmode) PHP_FUNCTION(gnupg_setsignmode)
{ {
zend_long signmode; phpc_long_t signmode;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -577,7 +579,7 @@ PHP_FUNCTION(gnupg_setsignmode)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &signmode) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &signmode) == FAILURE) {
return; return;
} }
PHPC_THIS = (gnupg_object *) PHPC_RES_FETCH(res, "ctx", le_gnupg); GNUPG_RES_FETCH();
} }
switch (signmode) { switch (signmode) {
case GPGME_SIG_MODE_NORMAL: case GPGME_SIG_MODE_NORMAL:
@ -906,10 +908,8 @@ PHP_FUNCTION(gnupg_sign)
{ {
char *value = NULL; char *value = NULL;
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_sign_result_t result; gpgme_sign_result_t result;
@ -962,7 +962,7 @@ PHP_FUNCTION(gnupg_sign)
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
} else { } else {
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
} }
gpgme_data_release(in); gpgme_data_release(in);
free(userret); free(userret);
@ -980,7 +980,6 @@ PHP_FUNCTION(gnupg_encrypt)
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret = NULL; char *userret = NULL;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_encrypt_result_t result; gpgme_encrypt_result_t result;
@ -1024,7 +1023,7 @@ PHP_FUNCTION(gnupg_encrypt)
} }
userret = gpgme_data_release_and_get_mem(out, &ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
gpgme_data_release(in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free(userret); free(userret);
if (ret_size < 1) { if (ret_size < 1) {
RETURN_FALSE; RETURN_FALSE;
@ -1042,7 +1041,6 @@ PHP_FUNCTION(gnupg_encryptsign)
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret = NULL; char *userret = NULL;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_encrypt_result_t result; gpgme_encrypt_result_t result;
gpgme_sign_result_t sign_result; gpgme_sign_result_t sign_result;
@ -1108,7 +1106,7 @@ PHP_FUNCTION(gnupg_encryptsign)
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out,&ret_size);
gpgme_data_release(in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free (userret); free (userret);
if (ret_size < 1) { if (ret_size < 1) {
RETURN_FALSE; RETURN_FALSE;
@ -1123,11 +1121,12 @@ PHP_FUNCTION(gnupg_verify)
{ {
gpgme_data_t gpgme_text, gpgme_sig; gpgme_data_t gpgme_text, gpgme_sig;
gpgme_verify_result_t gpgme_result; gpgme_verify_result_t gpgme_result;
/* text without the signature, if its a detached one, or the text incl the sig */
zval *signed_text = NULL; /* text without the signature, if its a detached one, or the text incl the sig */ zval *signed_text = NULL;
zval *signature = NULL; /* signature, if its a detached one */ /* signature, if its a detached one */
zval *plain_text = NULL; /* signed_text without the signature if its not a detached sig */ zval *signature = NULL;
/* signed_text without the signature if its not a detached sig */
zval *plain_text = NULL;
char *gpg_plain; char *gpg_plain;
phpc_str_size_t gpg_plain_len; phpc_str_size_t gpg_plain_len;
@ -1193,8 +1192,8 @@ PHP_FUNCTION(gnupg_verify)
/* get a 'plain' version of the text without a signature */ /* get a 'plain' version of the text without a signature */
gpg_plain = gpgme_data_release_and_get_mem(gpgme_text, &gpg_plain_len); gpg_plain = gpgme_data_release_and_get_mem(gpgme_text, &gpg_plain_len);
if (gpg_plain && gpg_plain_len > 0 && plain_text) { if (gpg_plain && gpg_plain_len > 0 && plain_text) {
ZVAL_DEREF (plain_text); PHPC_PZVAL_DEREF(plain_text);
ZVAL_STRINGL (plain_text, gpg_plain,gpg_plain_len); PHPC_PZVAL_CSTRL(plain_text, gpg_plain, gpg_plain_len);
} }
free(gpg_plain); free(gpg_plain);
} }
@ -1209,10 +1208,8 @@ PHP_FUNCTION(gnupg_decrypt)
{ {
char *enctxt; char *enctxt;
phpc_str_size_t enctxt_len; phpc_str_size_t enctxt_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_decrypt_result_t result; gpgme_decrypt_result_t result;
@ -1257,7 +1254,7 @@ PHP_FUNCTION(gnupg_decrypt)
} }
userret = gpgme_data_release_and_get_mem(out, &ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
gpgme_data_release(in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free(userret); free(userret);
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
@ -1273,10 +1270,8 @@ PHP_FUNCTION(gnupg_decryptverify)
char *enctxt; char *enctxt;
phpc_str_size_t enctxt_len; phpc_str_size_t enctxt_len;
zval *plaintext; zval *plaintext;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_decrypt_result_t decrypt_result; gpgme_decrypt_result_t decrypt_result;
gpgme_verify_result_t verify_result; gpgme_verify_result_t verify_result;
@ -1293,7 +1288,7 @@ PHP_FUNCTION(gnupg_decryptverify)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
ZVAL_DEREF(plaintext); PHPC_PZVAL_DEREF(plaintext);
gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_decrypt_cb, PHPC_THIS); gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_decrypt_cb, PHPC_THIS);
@ -1315,7 +1310,7 @@ PHP_FUNCTION(gnupg_decryptverify)
return; return;
} }
userret = gpgme_data_release_and_get_mem(out, &ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
ZVAL_STRINGL (plaintext,userret,ret_size); PHPC_PZVAL_CSTRL(plaintext, userret, ret_size);
free(userret); free(userret);
decrypt_result = gpgme_op_decrypt_result(PHPC_THIS->ctx); decrypt_result = gpgme_op_decrypt_result(PHPC_THIS->ctx);
if (decrypt_result->unsupported_algorithm) { if (decrypt_result->unsupported_algorithm) {
@ -1343,7 +1338,6 @@ PHP_FUNCTION(gnupg_export)
phpc_str_size_t searchkey_len; phpc_str_size_t searchkey_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t out; gpgme_data_t out;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1368,9 +1362,10 @@ PHP_FUNCTION(gnupg_export)
return; return;
} }
userret = gpgme_data_release_and_get_mem(out, &ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
RETVAL_STRINGL (userret,ret_size);
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
} else {
PHPC_CSTRL_RETVAL(userret, ret_size);
} }
free(userret); free(userret);
} }
@ -1383,7 +1378,6 @@ PHP_FUNCTION(gnupg_import)
{ {
char *importkey = NULL; char *importkey = NULL;
phpc_str_size_t importkey_len; phpc_str_size_t importkey_len;
gpgme_data_t in; gpgme_data_t in;
gpgme_import_result_t result; gpgme_import_result_t result;
@ -1436,8 +1430,7 @@ PHP_FUNCTION(gnupg_deletekey)
{ {
char *key; char *key;
phpc_str_size_t key_len; phpc_str_size_t key_len;
zend_long allow_secret = 0; phpc_long_t allow_secret = 0;
gpgme_key_t gpgme_key; gpgme_key_t gpgme_key;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1474,8 +1467,7 @@ PHP_FUNCTION(gnupg_gettrustlist)
{ {
char *pattern; char *pattern;
phpc_str_size_t pattern_len; phpc_str_size_t pattern_len;
zval sub_arr; phpc_val sub_arr;
gpgme_trust_item_t item; gpgme_trust_item_t item;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1496,16 +1488,17 @@ PHP_FUNCTION(gnupg_gettrustlist)
} }
PHPC_ARRAY_INIT(return_value); PHPC_ARRAY_INIT(return_value);
while (!(PHPC_THIS->err = gpgme_op_trustlist_next (PHPC_THIS->ctx, &item))) { while (!(PHPC_THIS->err = gpgme_op_trustlist_next (PHPC_THIS->ctx, &item))) {
PHPC_ARRAY_INIT (&sub_arr); PHPC_VAL_MAKE(sub_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sub_arr));
PHPC_ARRAY_ADD_ASSOC_LONG (&sub_arr, "level", item->level ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "level", item->level);
PHPC_ARRAY_ADD_ASSOC_LONG (&sub_arr, "type", item->type ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "type", item->type);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "keyid", item->keyid ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "keyid", item->keyid);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "ownertrust", item->owner_trust ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "ownertrust", item->owner_trust);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "validity", item->validity ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "validity", item->validity);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "name", item->name ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "name", item->name);
gpgme_trust_item_unref (item); gpgme_trust_item_unref (item);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL (return_value, &sub_arr); PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(return_value, PHPC_VAL_CAST_TO_PZVAL(sub_arr));
} }
} }
/* }}} */ /* }}} */
@ -1516,8 +1509,8 @@ PHP_FUNCTION(gnupg_listsignatures)
char *keyid; char *keyid;
phpc_str_size_t keyid_len; phpc_str_size_t keyid_len;
zval sub_arr; phpc_val sub_arr;
zval sig_arr; phpc_val sig_arr;
gpgme_key_t gpgme_key; gpgme_key_t gpgme_key;
gpgme_user_id_t gpgme_userid; gpgme_user_id_t gpgme_userid;
@ -1551,23 +1544,29 @@ PHP_FUNCTION(gnupg_listsignatures)
PHPC_ARRAY_INIT(return_value); PHPC_ARRAY_INIT(return_value);
gpgme_userid = gpgme_key->uids; gpgme_userid = gpgme_key->uids;
while (gpgme_userid) { while (gpgme_userid) {
PHPC_ARRAY_INIT (&sub_arr); PHPC_VAL_MAKE(sub_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sub_arr));
gpgme_signature = gpgme_userid->signatures; gpgme_signature = gpgme_userid->signatures;
while (gpgme_signature) { while (gpgme_signature) {
PHPC_ARRAY_INIT (&sig_arr); PHPC_VAL_MAKE(sig_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sig_arr));
PHPC_ARRAY_ADD_ASSOC_CSTR (&sig_arr, "uid", gpgme_signature->uid ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "uid", gpgme_signature->uid);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "name", gpgme_signature->name ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "name", gpgme_signature->name);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "email", gpgme_signature->email ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "email", gpgme_signature->email);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "comment", gpgme_signature->comment ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "comment", gpgme_signature->comment);
PHPC_ARRAY_ADD_ASSOC_LONG (&sig_arr, "expires", gpgme_signature->expires ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "expires", gpgme_signature->expires);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "revoked", gpgme_signature->revoked ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "revoked", gpgme_signature->revoked);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "expired", gpgme_signature->expired ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "expired", gpgme_signature->expired);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "invalid", gpgme_signature->invalid ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "invalid", gpgme_signature->invalid);
add_assoc_zval (&sub_arr, gpgme_signature->keyid, &sig_arr); PHPC_ARRAY_ADD_ASSOC_ZVAL(
PHPC_VAL_CAST_TO_PZVAL(sub_arr),
gpgme_signature->keyid,
PHPC_VAL_CAST_TO_PZVAL(sig_arr)
);
gpgme_signature = gpgme_signature->next; gpgme_signature = gpgme_signature->next;
} }
add_assoc_zval (return_value,gpgme_userid->uid, &sub_arr); PHPC_ARRAY_ADD_ASSOC_ZVAL(return_value, gpgme_userid->uid, PHPC_VAL_CAST_TO_PZVAL(sub_arr));
gpgme_userid = gpgme_userid->next; gpgme_userid = gpgme_userid->next;
} }
gpgme_key_unref(gpgme_key); gpgme_key_unref(gpgme_key);

View file

@ -46,8 +46,6 @@ PHPC_OBJ_STRUCT_BEGIN(gnupg)
HashTable *decryptkeys; HashTable *decryptkeys;
PHPC_OBJ_STRUCT_END() PHPC_OBJ_STRUCT_END()
static zend_class_entry *gnupg_class_entry;
PHP_MINIT_FUNCTION(gnupg); PHP_MINIT_FUNCTION(gnupg);
PHP_MSHUTDOWN_FUNCTION(gnupg); PHP_MSHUTDOWN_FUNCTION(gnupg);
PHP_MINFO_FUNCTION(gnupg); PHP_MINFO_FUNCTION(gnupg);

2
phpc

@ -1 +1 @@
Subproject commit 11299415b61f6c90f26410517e0c673b47efb7d8 Subproject commit d549c2c50ce74a9857337ee0e697585594f29972