这是我遇到的情况...我有一家供应商提供的库,该供应商将加密/解密作为我们使用的产品的一部分(不知道它是如何工作的)。我构建了一个 PHP 扩展,一切都通过 CLI 运行得非常出色。这是我为 PHP 扩展编写的 raptor.c 文件:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
//#if HAVE_LIBRAPTOR
#include "php_raptor.h"
#include "raptor.h"
#include "ext/standard/info.h"
/* If you declare any globals in php_raptor.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(raptor)
*/
/* True global resources - no need for thread safety here */
static int le_raptor;
/* {{{ raptor_functions[]
*
* Every user visible function must have an entry in raptor_functions[].
*/
const zend_function_entry raptor_functions[] = {
PHP_FE(raptor_decNK, NULL)
PHP_FE(raptor_encNK, NULL)
{NULL, NULL, NULL} /* Must be the last line in raptor_functions[] */
};
/* }}} */
/* {{{ raptor_module_entry
*/
zend_module_entry raptor_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"raptor",
raptor_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(raptor),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_RAPTOR
ZEND_GET_MODULE(raptor)
#endif
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(raptor)
{
php_info_print_table_start();
php_info_print_table_header(2, "raptor API support", "enabled");
php_info_print_table_end();
}
/* }}} */
PHP_FUNCTION(raptor_decNK) {
char * enctext;
unsigned char * dectext;
int enctextsize;
size_t dectextsize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &enctext, &enctextsize) == FAILURE) {
RETURN_NULL();
}
dectext = decNK((unsigned char *) enctext, (size_t) enctextsize, &dectextsize);
if (dectext == NULL) {
RETURN_FALSE;
} else {
RETURN_STRINGL((char *) dectext, dectextsize, 1);
}
}
PHP_FUNCTION(raptor_encNK) {
char * dectext;
unsigned char * enctext;
int dectextsize;
size_t enctextsize;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dectext, &dectextsize) == FAILURE) {
RETURN_NULL();
}
enctext = encNK((unsigned char *) dectext, (size_t) dectextsize, &enctextsize);
if (enctext == NULL) {
RETURN_FALSE;
} else {
RETURN_STRINGL((char *) enctext, enctextsize, 1);
}
}
//#endif
以及供应商的 raptor.h 文件的适用部分:
unsigned char *decNK(unsigned char * s, size_t inLen, size_t * outLen);
unsigned char *encNK(unsigned char * s, size_t inLen, size_t * outLen);
我的 test.php 文件有非常简单的代码:
<?php
$x = 1;
echo "$x\n";
$y = raptor_encNK($x);
echo "$y\n";
$x = raptor_decNK($y);
echo "$x\n";
?>
我从 CLI 得到(输出 $y 每次运行都会改变,但最终输出总是正确的)
# /usr/local/bin/php -f /usr/local/var/htdocs/test.php
1
FL//haHZgltG
1
相同的代码通过浏览器获取(再次输出 $y 变化,最终输出总是垃圾)
1
TgPw72NF9Zby
<binary crap>
所以我想当它转到 Apache 时在翻译中丢失了一些东西......或者我搞砸了扩展并且无法弄清楚......或者两者兼而有之。我只是不明白为什么它会通过 CLI 而不是通过 Apache 工作。
最佳答案
所以最后这不是 size_t 的问题,而是他们代码中整数的大小。关于它在 CLI 调用时与使用 Apache 和 Web 浏览器调用时为什么起作用仍然存在大量混淆......我可能永远不会找到。
关于通过 CLI 的 PHP 扩展有效,但不能通过 apache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7896393/