Search code examples
phpserializationphp-extensionmsgpack

What is the difference between msgpack_pack and msgpack_serialize and MessagePack::pack?


msgpack implemented in php by msgpack extension.

There are two functions msgpack_pack and msgpack_serialize and method MessagePack::pack. I do not know what is the difference between them. How to findout API of this extension?


Solution

  • They are the same.

    This is msgpack_serialize, it calls function php_msgpack_serialize;

    static ZEND_FUNCTION(msgpack_serialize)
    {
        zval *parameter;
        smart_str buf = {0};
    
        if (zend_parse_parameters(
                ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) == FAILURE)
        {
            return;
        }
    
        php_msgpack_serialize(&buf, parameter TSRMLS_CC);
    
        ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
    
        smart_str_free(&buf);
    }
    

    and msgpack_pack is the alias of msgpack_serialize.

    // in msgpack.c
    static zend_function_entry msgpack_functions[] = {
        ZEND_FE(msgpack_serialize, arginfo_msgpack_serialize)
        ZEND_FE(msgpack_unserialize, arginfo_msgpack_unserialize)
        ZEND_FALIAS(msgpack_pack, msgpack_serialize, arginfo_msgpack_serialize)
        ZEND_FALIAS(msgpack_unpack, msgpack_unserialize, arginfo_msgpack_unserialize)
        {NULL, NULL, NULL}
    };
    

    MessagePack::pack is the object form of msgpack_serialize. It also calls php_msgpack_serialize:

    static ZEND_METHOD(msgpack, pack)
    {
        zval *parameter;
        smart_str buf = {0};
        int php_only = MSGPACK_G(php_only);
        MSGPACK_BASE_OBJECT;
    
        if (zend_parse_parameters(
                ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) == FAILURE)
        {
            return;
        }
    
        MSGPACK_G(php_only) = base->php_only;
    
        php_msgpack_serialize(&buf, parameter TSRMLS_CC);
    
        MSGPACK_G(php_only) = php_only;
    
        ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
    
        smart_str_free(&buf);
    }