如何在PHP中查看内置函数的源代码/内容?

时间:2014-09-02 20:20:42

标签: php

PHP有一种简单的方法可以找出内置函数的源代码/内容吗?

比如说我想知道base64_decode()对给定的编码base64字符串实际做了什么来将其转换为纯文本。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以浏览PHP here

的源代码

在您的情况下base64_decode已实施here (PHP 5.6.0)

注意:此代码在C中,因为这就是PHP的编写内容。事实上,PHP的所有内置函数和扩展都是用C语言编写的。

PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
{
    const unsigned char *current = str;
    int ch, i = 0, j = 0, k;
    /* this sucks for threaded environments */
    unsigned char *result;

    result = (unsigned char *)safe_emalloc(length, 1, 1);

    /* run through the whole string, converting as we go */
    while ((ch = *current++) != '\0' && length-- > 0) {
        if (ch == base64_pad) {
            if (*current != '=' && ((i % 4) == 1 || (strict && length > 0))) {
                if ((i % 4) != 1) {
                    while (isspace(*(++current))) {
                        continue;
                    }
                    if (*current == '\0') {
                        continue;
                    }
                }
                efree(result);
                return NULL;
            }
            continue;
        }

        ch = base64_reverse_table[ch];
        if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
            continue;
        } else if (ch == -2) {
            efree(result);
            return NULL;
        }

        switch(i % 4) {
        case 0:
            result[j] = ch << 2;
            break;
        case 1:
            result[j++] |= ch >> 4;
            result[j] = (ch & 0x0f) << 4;
            break;
        case 2:
            result[j++] |= ch >>2;
            result[j] = (ch & 0x03) << 6;
            break;
        case 3:
            result[j++] |= ch;
            break;
        }
        i++;
    }

    k = j;
    /* mop things up if we ended on a boundary */
    if (ch == base64_pad) {
        switch(i % 4) {
        case 1:
            efree(result);
            return NULL;
        case 2:
            k++;
        case 3:
            result[k] = 0;
        }
    }
    if(ret_length) {
        *ret_length = j;
    }
    result[j] = '\0';
    return result;
}