php trim注入,php过滤函数trim实现原理

static inline int php_charmask(unsigned char *input, int len, char *mask TSRMLS_DC)

{

unsigned char *end;

unsigned char c;

int result = SUCCESS;

memset(mask, 0, 256);

for (end = input+len; input < end; input++) {

c=*input;

if ((input+3 < end) && input[1] == '.' && input[2] == '.'

&& input[3] >= c) {

memset(mask+c, 1, input[3] - c + 1);

input+=3;

} else if ((input+1 < end) && input[0] == '.' && input[1] == '.') {

/* Error, try to be as helpful as possible:

(a range ending/starting with '.' won't be captured here) */

if (end-len >= input) { /* there was no 'left' char */

php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, no character to the left of '..'");

result = FAILURE;

continue;

}

if (input+2 >= end) { /* there is no 'right' char */

php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, no character to the right of '..'");

result = FAILURE;

continue;

}

if (input[-1] > input[2]) { /* wrong order */

php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, '..'-range needs to be incrementing");

result = FAILURE;

continue;

}

/* FIXME: better error (a..b..c is the only left possibility?) */

php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range");

result = FAILURE;

continue;

} else {

mask[c]=1;

}

}

return result;

}

/* }}} */

/* {{{ php_trim()

* mode 1 : trim left

* mode 2 : trim right

* mode 3 : trim left and right

* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')

*/

PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC)

{

register int i;

int trimmed = 0;

char mask[256];

if (what) {

php_charmask((unsigned char*)what, what_len, mask TSRMLS_CC);

} else {

php_charmask((unsigned char*)" \n\r\t\v\0", 6, mask TSRMLS_CC);

}

if (mode & 1) {

for (i = 0; i < len; i++) {

if (mask[(unsigned char)c[i]]) {

trimmed++;

} else {

break;

}

}

len -= trimmed;

c += trimmed;

}

if (mode & 2) {

for (i = len - 1; i >= 0; i--) {

if (mask[(unsigned char)c[i]]) {

len--;

} else {

break;

}

}

}

if (return_value) {

RETVAL_STRINGL(c, len, 1);

} else {

return estrndup(c, len);

}

return "";

}

/* }}} */

/* {{{ php_do_trim

* Base for trim(), rtrim() and ltrim() functions.

*/

static void php_do_trim(INTERNAL_FUNCTION_PARAMETERS, int mode)

{

char *str;

char *what = NULL;

int str_len, what_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &what, &what_len) == FAILURE) {

return;

}

php_trim(str, str_len, what, what_len, return_value, mode TSRMLS_CC);

}

/* }}} */

/* {{{ proto string trim(string str [, string character_mask])

Strips whitespace from the beginning and end of a string */

PHP_FUNCTION(trim)

{

php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);

}

/* }}} */

/* {{{ proto string rtrim(string str [, string character_mask])

Removes trailing whitespace */

PHP_FUNCTION(rtrim)

{

php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);

}

/* }}} */

/* {{{ proto string ltrim(string str [, string character_mask])

Strips whitespace from the beginning of a string */

PHP_FUNCTION(ltrim)

{

php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);

}

/* }}} */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值