summaryrefslogtreecommitdiff
path: root/php
diff options
context:
space:
mode:
authorHideyuki TAKEI <takehide22@gmail.com>2010-04-05 00:10:28 +0900
committerHideyuki TAKEI <takehide22@gmail.com>2010-04-05 00:10:28 +0900
commit99a2d2859269ddf803a802c043babcd977260faf (patch)
tree5d0a89a77fab9f3c81dbcf49ef5696ef6d3b1f26 /php
parent254ee80c16b3b0ce12b461d189aa1e6302debea0 (diff)
downloadmsgpack-python-99a2d2859269ddf803a802c043babcd977260faf.tar.gz
import MessagePack for PHP
Diffstat (limited to 'php')
-rw-r--r--php/CREDITS1
-rw-r--r--php/EXPERIMENTAL0
-rw-r--r--php/README0
-rw-r--r--php/bench/bench.php51
-rw-r--r--php/config.m414
-rw-r--r--php/config.w3213
-rw-r--r--php/msgpack.c656
-rw-r--r--php/msgpack.php21
-rw-r--r--php/msgpack/pack_define.h26
-rw-r--r--php/msgpack/pack_template.h741
-rw-r--r--php/msgpack/unpack_define.h129
-rw-r--r--php/msgpack/unpack_template.h361
-rw-r--r--php/php_msgpack.h86
-rwxr-xr-xphp/test_normal.php19
-rwxr-xr-xphp/test_streaming.php34
-rw-r--r--php/tests/test_pack.phpt169
16 files changed, 2321 insertions, 0 deletions
diff --git a/php/CREDITS b/php/CREDITS
new file mode 100644
index 0000000..3aae276
--- /dev/null
+++ b/php/CREDITS
@@ -0,0 +1 @@
+msgpack
diff --git a/php/EXPERIMENTAL b/php/EXPERIMENTAL
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/php/EXPERIMENTAL
diff --git a/php/README b/php/README
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/php/README
diff --git a/php/bench/bench.php b/php/bench/bench.php
new file mode 100644
index 0000000..95a7ca4
--- /dev/null
+++ b/php/bench/bench.php
@@ -0,0 +1,51 @@
+<?php
+
+//ini_set('memory_limit' ,'128M');
+
+$ary = array();
+for($i=0; $i<pow(2, 10); $i++){
+ $ary = array_merge($ary, range(0, 1024));
+}
+
+echo count($ary);
+
+function getSize($ary)
+{
+ if (ini_get('mbstring.func_overload') & 2 && function_exists('mb_strlen')) {
+ $size = mb_strlen($ary, 'ASCII');
+ } else {
+ $size = strlen($ary);
+ }
+
+ return $size;
+}
+
+echo "fin" . $size . "\n";
+
+echo "----\n";
+echo "MessagePack\n";
+$a = microtime(true);
+$packed = msgpack_pack($ary);
+$b = microtime(true);
+echo ($b-$a) . "sec, " . getSize($packed) . "bytes\n";
+
+$a = microtime(true);
+$pack = msgpack_unpack($packed);
+$b = microtime(true);
+echo ($b-$a) . "sec\n";
+
+
+echo "----\n";
+echo "JSON\n";
+$a = microtime(true);
+$jsoned = json_encode($ary);
+$b = microtime(true);
+echo ($b-$a) . "sec, " . getSize($jsoned) . "bytes\n";
+
+$a = microtime(true);
+$json = json_decode($jsoned);
+$b = microtime(true);
+echo ($b-$a) . "sec\n";
+
+
+?>
diff --git a/php/config.m4 b/php/config.m4
new file mode 100644
index 0000000..751b471
--- /dev/null
+++ b/php/config.m4
@@ -0,0 +1,14 @@
+dnl $Id$
+dnl config.m4 for extension msgpack
+
+PHP_ARG_ENABLE(msgpack, whether to enable MessagePack support,
+Make sure that the comment is aligned:
+[ --enable-msgpack Enable MessagePack support])
+
+if test "$PHP_MSGPACK" != "no"; then
+ dnl AC_DEFINE([HAVE_MSGPACK],1 ,[whether to enable MessagePack support])
+ dnl AC_HEADER_STDC
+
+ PHP_NEW_EXTENSION(msgpack, msgpack.c, $ext_shared)
+ dnl PHP_SUBST(MSGPACK_SHARED_LIBADD)
+fi
diff --git a/php/config.w32 b/php/config.w32
new file mode 100644
index 0000000..50e9bec
--- /dev/null
+++ b/php/config.w32
@@ -0,0 +1,13 @@
+// $Id$
+// vim:ft=javascript
+
+// If your extension references something external, use ARG_WITH
+// ARG_WITH("msgpack", "for msgpack support", "no");
+
+// Otherwise, use ARG_ENABLE
+// ARG_ENABLE("msgpack", "enable msgpack support", "no");
+
+if (PHP_MSGPACK != "no") {
+ EXTENSION("msgpack", "msgpack.c");
+}
+
diff --git a/php/msgpack.c b/php/msgpack.c
new file mode 100644
index 0000000..b38eb18
--- /dev/null
+++ b/php/msgpack.c
@@ -0,0 +1,656 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2007 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Author: Hideyuki TAKEI |
+ +----------------------------------------------------------------------+
+*/
+
+/* $Id: header 226204 2007-01-01 19:32:10Z iliaa $ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "php.h"
+#include "php_ini.h"
+#include "ext/standard/info.h"
+#include "ext/standard/php_smart_str.h"
+#include "php_msgpack.h"
+
+#define PHP_EXT_VERSION "0.01"
+
+#ifndef TRUE
+# define TRUE 1
+# define FALSE 0
+#endif
+
+
+/* pack */
+#include "msgpack/pack_define.h"
+
+#define msgpack_pack_inline_func(name) \
+ static inline void msgpack_pack ## name
+
+#define msgpack_pack_inline_func_cint(name) \
+ static inline void msgpack_pack ## name
+
+#define msgpack_pack_user smart_str*
+
+#define msgpack_pack_append_buffer(user, buf, len) \
+ smart_str_appendl(user, (const void*)buf, len)
+
+#include "msgpack/pack_template.h"
+
+
+/* unpack */
+#include "msgpack/unpack_define.h"
+
+typedef struct {
+ int finished;
+ char* source;
+} unpack_user;
+
+#define msgpack_unpack_struct(name) \
+ struct template ## name
+
+#define msgpack_unpack_func(ret, name) \
+ ret template ## name
+
+#define msgpack_unpack_callback(name) \
+ template_callback ## name
+
+#define msgpack_unpack_object zval*
+
+#define msgpack_unpack_user unpack_user
+
+struct template_context;
+typedef struct template_context msgpack_unpack_t;
+
+static void template_init(msgpack_unpack_t* u);
+static msgpack_unpack_object template_data(msgpack_unpack_t* u);
+static int template_execute(msgpack_unpack_t* u,
+ const char* data, size_t len, size_t* off);
+
+ZEND_BEGIN_MODULE_GLOBALS(msgpack)
+ msgpack_unpack_t *global_mp;
+ZEND_END_MODULE_GLOBALS(msgpack)
+
+#ifdef ZTS
+#define MSGPACK_G(v) TSRMG(msgpack_globals_id, zend_msgpack_globals *, v)
+#else
+#define MSGPACK_G(v) (msgpack_globals.v)
+#endif
+
+static inline msgpack_unpack_object template_callback_root(unpack_user* u)
+{
+ msgpack_unpack_object data;
+ ALLOC_INIT_ZVAL(data);
+ ZVAL_NULL(data);
+ return data;
+}
+
+static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, d); return 0; }
+
+static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, d); return 0; }
+
+static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, d); return 0; }
+
+static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, d); return 0; }
+
+static inline int template_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, (long)d); return 0; }
+
+static inline int template_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, (long)d); return 0; }
+
+static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, (long)d); return 0; }
+
+static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_LONG(*o, d); return 0; }
+
+static inline int template_callback_float(unpack_user* u, float d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_DOUBLE(*o, d); return 0; }
+
+static inline int template_callback_double(unpack_user* u, double d, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_DOUBLE(*o, d); return 0; }
+
+static inline int template_callback_nil(unpack_user* u, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_NULL(*o); return 0; }
+
+static inline int template_callback_true(unpack_user* u, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_BOOL(*o, 1); return 0; }
+
+static inline int template_callback_false(unpack_user* u, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); ZVAL_BOOL(*o, 0); return 0;}
+
+static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); array_init(*o); return 0; }
+
+static inline int template_callback_array_item(unpack_user* u, msgpack_unpack_object* c, msgpack_unpack_object o)
+{ add_next_index_zval(*c, o); return 0; }
+
+static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
+{ ALLOC_INIT_ZVAL(*o); array_init(*o); return 0; }
+
+static inline int template_callback_map_item(unpack_user* u, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v)
+{
+ switch(k->type) {
+ case IS_LONG:
+ add_index_zval(*c, Z_LVAL(*k), v);
+ break;
+ case IS_STRING:
+ add_assoc_zval_ex(*c, Z_STRVAL(*k), Z_STRLEN(*k)+1, v);
+ break;
+ default:
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_decode) illegal offset type, skip this decoding");
+ break;
+ }
+ return 0;
+}
+
+
+static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
+{
+ ALLOC_INIT_ZVAL(*o);
+ if (l == 0) {
+ ZVAL_STRINGL(*o, "", 0, 1);
+ } else {
+ ZVAL_STRINGL(*o, p, l, 1);
+ }
+ return 0;
+}
+
+#include "msgpack/unpack_template.h"
+
+static PHP_GINIT_FUNCTION(msgpack);
+
+ZEND_DECLARE_MODULE_GLOBALS(msgpack)
+
+/* True global resources - no need for thread safety here */
+static int le_msgpack;
+
+/* {{{ msgpack_functions[]
+ *
+ * Every user visible function must have an entry in msgpack_functions[].
+ */
+zend_function_entry msgpack_functions[] = {
+ PHP_FE(msgpack_pack, NULL)
+ PHP_FE(msgpack_unpack, NULL)
+ PHP_FE(msgpack_unpack_limit, NULL)
+ PHP_ME(msgpack, initialize, NULL, 0)
+ PHP_ME(msgpack, execute, NULL, 0)
+ PHP_ME(msgpack, execute_limit, NULL, 0)
+ PHP_ME(msgpack, finished, NULL, 0)
+ PHP_ME(msgpack, data, NULL, 0)
+ {NULL, NULL, NULL} /* Must be the last line in msgpack_functions[] */
+};
+/* }}} */
+
+/* {{{ msgpack_module_entry
+ */
+zend_module_entry msgpack_module_entry = {
+#if ZEND_MODULE_API_NO >= 20010901
+ STANDARD_MODULE_HEADER,
+#endif
+ "msgpack",
+ msgpack_functions,
+ PHP_MINIT(msgpack),
+ PHP_MSHUTDOWN(msgpack),
+ PHP_RINIT(msgpack), /* Replace with NULL if there's nothing to do at request start */
+ PHP_RSHUTDOWN(msgpack), /* Replace with NULL if there's nothing to do at request end */
+ PHP_MINFO(msgpack),
+#if ZEND_MODULE_API_NO >= 20010901
+ "0.1", /* Replace with version number for your extension */
+#endif
+ PHP_MODULE_GLOBALS(msgpack),
+ PHP_GINIT(msgpack),
+ NULL,
+ NULL,
+ STANDARD_MODULE_PROPERTIES_EX
+};
+/* }}} */
+
+#ifdef COMPILE_DL_MSGPACK
+ZEND_GET_MODULE(msgpack)
+#endif
+
+/* {{{ PHP_GINIT_FUNCTION */
+static PHP_GINIT_FUNCTION(msgpack)
+{
+ msgpack_globals->global_mp = NULL;
+}
+/* }}} */
+
+/* {{{ PHP_MINIT_FUNCTION
+ */
+PHP_MINIT_FUNCTION(msgpack)
+{
+ zend_class_entry ce;
+ INIT_CLASS_ENTRY(ce, "MessagePack", msgpack_functions);
+ msgpack_ce = zend_register_internal_class(&ce TSRMLS_CC);
+
+ return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PHP_MSHUTDOWN_FUNCTION
+ */
+PHP_MSHUTDOWN_FUNCTION(msgpack)
+{
+ /* uncomment this line if you have INI entries
+ UNREGISTER_INI_ENTRIES();
+ */
+ if (MSGPACK_G(global_mp)) {
+ efree(MSGPACK_G(global_mp));
+ MSGPACK_G(global_mp) = NULL;
+ }
+
+ return SUCCESS;
+}
+/* }}} */
+
+/* Remove if there's nothing to do at request start */
+/* {{{ PHP_RINIT_FUNCTION
+ */
+PHP_RINIT_FUNCTION(msgpack)
+{
+ return SUCCESS;
+}
+/* }}} */
+
+/* Remove if there's nothing to do at request end */
+/* {{{ PHP_RSHUTDOWN_FUNCTION
+ */
+PHP_RSHUTDOWN_FUNCTION(msgpack)
+{
+ return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PHP_MINFO_FUNCTION
+ */
+PHP_MINFO_FUNCTION(msgpack)
+{
+ php_info_print_table_start();
+ php_info_print_table_header(2, "msgpack support", "enabled");
+ php_info_print_table_row(2, "php extension version", PHP_EXT_VERSION);
+ php_info_print_table_row(2, "author", "Hideyuki TAKEI");
+ php_info_print_table_row(2, "homepage", "http://msgpack.sourceforge.net");
+ php_info_print_table_row(2, "open sourced by", "KLab inc.");
+ php_info_print_table_end();
+}
+/* }}} */
+
+PHP_MSGPACK_API int msgpack_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
+{
+ int i;
+ HashTable *myht = HASH_OF(*val);
+
+ i = myht ? zend_hash_num_elements(myht) : 0;
+ if (i > 0) {
+ char *key;
+ ulong index, idx;
+ uint key_len;
+ HashPosition pos;
+
+ zend_hash_internal_pointer_reset_ex(myht, &pos);
+ idx = 0;
+ for (;; zend_hash_move_forward_ex(myht, &pos)) {
+ i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
+ if (i == HASH_KEY_NON_EXISTANT)
+ break;
+ if (i == HASH_KEY_IS_STRING) {
+ return 1;
+ } else {
+ if (index != idx) {
+ return 1;
+ }
+ }
+ idx++;
+ }
+ }
+ return 0;
+}
+/* }}} */
+
+PHP_MSGPACK_API void msgpack_pack_array_hash(smart_str *pk, zval **val TSRMLS_DC) /* {{{ */
+{
+ int i, r;
+ HashTable *myht;
+
+ if(Z_TYPE_PP(val) == IS_ARRAY){
+ myht = HASH_OF(*val);
+ r = msgpack_determine_array_type(val TSRMLS_CC);
+ }
+ else{
+ myht = Z_OBJPROP_PP(val);
+ r = 1;
+ }
+
+ i = myht ? zend_hash_num_elements(myht) : 0;
+
+ if(r == 0){
+ msgpack_pack_array(pk, i);
+ }
+ else{
+ msgpack_pack_map(pk, i);
+ }
+
+ if(i>0){
+ char *key;
+ zval **data;
+ ulong index;
+ uint key_len;
+ HashPosition pos;
+ HashTable *tmp_ht;
+ int need_comma = 0;
+
+ zend_hash_internal_pointer_reset_ex(myht, &pos);
+ for(;; zend_hash_move_forward_ex(myht, &pos)){
+ i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
+ if(i==HASH_KEY_NON_EXISTANT)
+ break;
+ if(zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS){
+ tmp_ht = HASH_OF(*data);
+ if (tmp_ht)
+ tmp_ht->nApplyCount++;
+
+ if(r==0)
+ php_msgpack_pack(pk, *data TSRMLS_CC);
+ else if(r==1){
+ if(i==HASH_KEY_IS_STRING){
+ if(key[0]=='\0' && Z_TYPE_PP(val)==IS_OBJECT){
+ // Skip protected and private members.
+ if(tmp_ht)
+ tmp_ht->nApplyCount--;
+ continue;
+ }
+ msgpack_pack_raw(pk, key_len-1);
+ msgpack_pack_raw_body(pk, key, key_len-1);
+ php_msgpack_pack(pk, *data TSRMLS_CC);
+ }
+ else{
+ msgpack_pack_long(pk, index);
+ php_msgpack_pack(pk, *data TSRMLS_CC);
+ }
+ }
+
+ if(tmp_ht){
+ tmp_ht->nApplyCount--;
+ }
+ }
+ }
+
+ }
+}
+/* }}} */
+
+PHP_MSGPACK_API void php_msgpack_pack(smart_str *pk, zval *val TSRMLS_DC) /* {{{ */
+{
+ switch(Z_TYPE_P(val)){
+ case IS_NULL:
+ msgpack_pack_nil(pk);
+ break;
+ case IS_BOOL:
+ if (Z_BVAL_P(val))
+ msgpack_pack_true(pk);
+ else
+ msgpack_pack_false(pk);
+ break;
+ case IS_LONG:
+ msgpack_pack_long(pk, Z_LVAL_P(val));
+ break;
+ case IS_DOUBLE:
+ {
+ double dbl = Z_DVAL_P(val);
+ if (zend_isinf(dbl) || zend_isnan(dbl)) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_pack) double %.9g does not conform to the MSGPACK spec, encoded as 0", dbl);
+ ZVAL_LONG(val, 0);
+ }
+ msgpack_pack_double(pk, Z_DVAL_P(val));
+ }
+ break;
+ case IS_STRING:
+ msgpack_pack_raw(pk, Z_STRLEN_P(val));
+ msgpack_pack_raw_body(pk, Z_STRVAL_P(val), Z_STRLEN_P(val));
+ break;
+ case IS_ARRAY:
+ case IS_OBJECT:
+ msgpack_pack_array_hash(pk, &val TSRMLS_CC);
+ break;
+ defalut:
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_pack) type is unsupported, encoded as null");
+ msgpack_pack_nil(pk);
+ break;
+ }
+
+ return;
+}
+/* }}} */
+
+PHP_MSGPACK_API void php_msgpack_unpack_limit(zval *return_value, const char *buf, int len, zend_bool assoc TSRMLS_DC) /* {{{ */
+{
+ if (len<=0) {
+ RETURN_NUL();
+ }
+
+ msgpack_unpack_t mp;
+ template_init(&mp);
+ unpack_user u = {0, ""};
+
+ size_t from = 0;
+ char* dptr = (char*)buf;
+ long dlen = len;
+ int ret;
+
+ (&mp)->user.source = (char*)buf;
+ ret = template_execute(&mp, dptr, (size_t)dlen, &from);
+ (&mp)->user.source = "";
+
+ if(ret < 0) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpack) parse error");
+ } else if(ret == 0) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpack) insufficient bytes");
+ } else {
+ if(from < dlen) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpack) extra bytes");
+ }
+
+ *return_value = *template_data(&mp);
+ FREE_ZVAL(template_data(&mp));
+ }
+}
+/* }}} */
+
+
+PHP_FUNCTION(msgpack_pack)
+{
+ zval *parameter;
+ smart_str buf = {0};
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) == FAILURE) {
+ return;
+ }
+
+ php_msgpack_pack(&buf, parameter TSRMLS_CC);
+
+ ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
+
+ smart_str_free(&buf);
+}
+
+PHP_FUNCTION(msgpack_unpack)
+{
+ char *parameter;
+ int parameter_len;
+ zend_bool assoc = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b",
+ &parameter, &parameter_len, &assoc) == FAILURE) {
+ return;
+ }
+
+ if (!parameter_len) {
+ RETURN_NULL();
+ }
+
+ php_msgpack_unpack_limit(return_value, parameter, parameter_len, assoc TSRMLS_CC);
+}
+
+PHP_FUNCTION(msgpack_unpack_limit)
+{
+ char *parameter;
+ int parameter_len;
+ int limit;
+ zend_bool assoc = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|b",
+ &parameter, &parameter_len, &limit, &assoc) == FAILURE) {
+ return;
+ }
+
+ if (!parameter_len) {
+ RETURN_NULL();
+ }
+ else if (parameter_len < limit) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpack) limit greater than data_len");
+ limit = parameter_len;
+ }
+
+ php_msgpack_unpack_limit(return_value, parameter, limit, assoc TSRMLS_CC);
+}
+
+
+PHP_MSGPACK_API void php_msgpack_unpacker_execute_limit(zval *return_value, const char *buf, int off, int len, zend_bool assoc TSRMLS_DC) /* {{{ */
+{
+ if (len<=0) {
+ RETURN_NUL();
+ }
+
+ size_t from = off;
+ char* dptr = (char*)buf;
+ long dlen = len;
+ int ret;
+
+ if(from >= dlen) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpacker_execute_limit) offset is bigger than data buffer size");
+ }
+
+ MSGPACK_G(global_mp)->user.source = (char*)buf;
+ ret = template_execute(MSGPACK_G(global_mp), dptr, (size_t)dlen, &from);
+ MSGPACK_G(global_mp)->user.source = "";
+
+ if(ret < 0) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpacker_execute_limit) parse error");
+ } else if(ret > 0) {
+ MSGPACK_G(global_mp)->user.finished = 1;
+ RETVAL_LONG(from);
+ } else {
+ MSGPACK_G(global_mp)->user.finished = 0;
+ RETVAL_LONG(from);
+ }
+}
+/* }}} */
+
+PHP_MSGPACK_API void php_msgpack_unpacker_reset(TSRMLS_D) /* {{{ */
+{
+ if(MSGPACK_G(global_mp)) {
+ efree(MSGPACK_G(global_mp));
+ MSGPACK_G(global_mp) = NULL;
+ }
+ MSGPACK_G(global_mp) = safe_emalloc(sizeof(msgpack_unpack_t), 1, 0);
+
+ template_init(MSGPACK_G(global_mp));
+ unpack_user u = {0, ""};
+ MSGPACK_G(global_mp)->user = u;
+ return;
+}
+/* }}} */
+
+PHP_METHOD(msgpack, initialize)
+{
+ php_msgpack_unpacker_reset(TSRMLS_C);
+ return;
+}
+
+PHP_METHOD(msgpack, execute)
+{
+ char *data;
+ int off;
+ int data_len;
+ zend_bool assoc = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|b",
+ &data, &data_len, &off, &assoc) == FAILURE) {
+ return;
+ }
+
+ if (!data_len) {
+ RETURN_NULL();
+ }
+
+ php_msgpack_unpacker_execute_limit(return_value, data, off, data_len, assoc TSRMLS_CC);
+}
+
+PHP_METHOD(msgpack, execute_limit)
+{
+ char *data;
+ int off;
+ int data_len;
+ int limit;
+ zend_bool assoc = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b",
+ &data, &data_len, &off, &limit, &assoc) == FAILURE) {
+ return;
+ }
+
+ if (!data_len) {
+ RETURN_NULL();
+ }
+ else if (data_len < limit) {
+ zend_error(E_WARNING, "[msgpack] (php_msgpack_unpack) limit greater than (data+off)_len");
+ limit = data_len;
+ }
+
+ php_msgpack_unpacker_execute_limit(return_value, data, off, limit, assoc TSRMLS_CC);
+}
+
+PHP_METHOD(msgpack, finished)
+{
+ if(MSGPACK_G(global_mp)->user.finished == 1) {
+ RETURN_TRUE;
+ }
+ RETURN_FALSE;
+}
+
+PHP_METHOD(msgpack, data)
+{
+ *return_value = *template_data(MSGPACK_G(global_mp));
+ return;
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
diff --git a/php/msgpack.php b/php/msgpack.php
new file mode 100644
index 0000000..c2498dd
--- /dev/null
+++ b/php/msgpack.php
@@ -0,0 +1,21 @@
+<?php
+$br = (php_sapi_name() == "cli")? "":"<br>";
+
+if(!extension_loaded('msgpack')) {
+ dl('msgpack.' . PHP_SHLIB_SUFFIX);
+}
+$module = 'msgpack';
+$functions = get_extension_funcs($module);
+echo "Functions available in the test extension:$br\n";
+foreach($functions as $func) {
+ echo $func."$br\n";
+}
+echo "$br\n";
+$function = 'confirm_' . $module . '_compiled';
+if (extension_loaded($module)) {
+ $str = $function($module);
+} else {
+ $str = "Module $module is not compiled into PHP";
+}
+echo "$str\n";
+?>
diff --git a/php/msgpack/pack_define.h b/php/msgpack/pack_define.h
new file mode 100644
index 0000000..33408e5
--- /dev/null
+++ b/php/msgpack/pack_define.h
@@ -0,0 +1,26 @@
+/*
+ * MessagePack unpacking routine template
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MSGPACK_PACK_DEFINE_H__
+#define MSGPACK_PACK_DEFINE_H__
+
+#include <stddef.h>
+#include <stdint.h>
+#include <limits.h>
+
+#endif /* msgpack/pack_define.h */
+
diff --git a/php/msgpack/pack_template.h b/php/msgpack/pack_template.h
new file mode 100644
index 0000000..aa620f5
--- /dev/null
+++ b/php/msgpack/pack_template.h
@@ -0,0 +1,741 @@
+/*
+ * MessagePack packing routine template
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN__
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define __BIG_ENDIAN__
+#endif
+#endif
+
+
+#ifdef __LITTLE_ENDIAN__
+
+#define STORE8_BE8(d) \
+ ((uint8_t*)&d)[0]
+
+
+#define STORE16_BE8(d) \
+ ((uint8_t*)&d)[0]
+
+#define STORE16_BE16(d) \
+ ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+
+#define STORE32_BE8(d) \
+ ((uint8_t*)&d)[0]
+
+#define STORE32_BE16(d) \
+ ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+#define STORE32_BE32(d) \
+ ((uint8_t*)&d)[3], ((uint8_t*)&d)[2], ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+
+#define STORE64_BE8(d) \
+ ((uint8_t*)&d)[0]
+
+#define STORE64_BE16(d) \
+ ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+#define STORE64_BE32(d) \
+ ((uint8_t*)&d)[3], ((uint8_t*)&d)[2], ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+#define STORE64_BE64(d) \
+ ((uint8_t*)&d)[7], ((uint8_t*)&d)[6], ((uint8_t*)&d)[5], ((uint8_t*)&d)[4], \
+ ((uint8_t*)&d)[3], ((uint8_t*)&d)[2], ((uint8_t*)&d)[1], ((uint8_t*)&d)[0]
+
+
+#elif __BIG_ENDIAN__
+
+#define STORE8_BE8(d) \
+ ((uint8_t*)&d)[0]
+
+
+#define STORE16_BE8(d) \
+ ((uint8_t*)&d)[1]
+
+#define STORE16_BE16(d) \
+ ((uint8_t*)&d)[0], ((uint8_t*)&d)[1]
+
+
+#define STORE32_BE8(d) \
+ ((uint8_t*)&d)[3]
+
+#define STORE32_BE16(d) \
+ ((uint8_t*)&d)[2], ((uint8_t*)&d)[3]
+
+#define STORE32_BE32(d) \
+ ((uint8_t*)&d)[0], ((uint8_t*)&d)[1], ((uint8_t*)&d)[2], ((uint8_t*)&d)[3]
+
+
+#define STORE64_BE8(d) \
+ ((uint8_t*)&d)[7]
+
+#define STORE64_BE16(d) \
+ ((uint8_t*)&d)[6], ((uint8_t*)&d)[7]
+
+#define STORE64_BE32(d) \
+ ((uint8_t*)&d)[4], ((uint8_t*)&d)[5], ((uint8_t*)&d)[6], ((uint8_t*)&d)[7]
+
+#define STORE64_BE64(d) \
+ ((uint8_t*)&d)[0], ((uint8_t*)&d)[1], ((uint8_t*)&d)[2], ((uint8_t*)&d)[3], \
+ ((uint8_t*)&d)[4], ((uint8_t*)&d)[5], ((uint8_t*)&d)[6], ((uint8_t*)&d)[7]
+
+#endif
+
+#ifndef msgpack_pack_inline_func
+#error msgpack_pack_inline_func template is not defined
+#endif
+
+#ifndef msgpack_pack_user
+#error msgpack_pack_user type is not defined
+#endif
+
+#ifndef msgpack_pack_append_buffer
+#error msgpack_pack_append_buffer callback is not defined
+#endif
+
+
+/*
+ * Integer
+ */
+
+#define msgpack_pack_real_uint8(x, d) \
+do { \
+ if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE8_BE8(d), 1); \
+ } else { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE8_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+} while(0)
+
+#define msgpack_pack_real_uint16(x, d) \
+do { \
+ if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE16_BE8(d), 1); \
+ } else if(d < (1<<8)) { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE16_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } else { \
+ /* unsigned 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE16_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } \
+} while(0)
+
+#define msgpack_pack_real_uint32(x, d) \
+do { \
+ if(d < (1<<8)) { \
+ if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE32_BE8(d), 1); \
+ } else { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE32_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+ } else { \
+ if(d < (1<<16)) { \
+ /* unsigned 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE32_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else { \
+ /* unsigned 32 */ \
+ const unsigned char buf[5] = {0xce, STORE32_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } \
+ } \
+} while(0)
+
+#define msgpack_pack_real_uint64(x, d) \
+do { \
+ if(d < (1ULL<<8)) { \
+ if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE64_BE8(d), 1); \
+ } else { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE64_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+ } else { \
+ if(d < (1ULL<<16)) { \
+ /* signed 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE64_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else if(d < (1ULL<<32)) { \
+ /* signed 32 */ \
+ const unsigned char buf[5] = {0xce, STORE64_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } else { \
+ /* signed 64 */ \
+ const unsigned char buf[9] = {0xcf, STORE64_BE64(d)}; \
+ msgpack_pack_append_buffer(x, buf, 9); \
+ } \
+ } \
+} while(0)
+
+#define msgpack_pack_real_int8(x, d) \
+do { \
+ if(d < -(1<<5)) { \
+ /* signed 8 */ \
+ const unsigned char buf[2] = {0xd0, STORE8_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } else { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE8_BE8(d), 1); \
+ } \
+} while(0)
+
+#define msgpack_pack_real_int16(x, d) \
+do { \
+ if(d < -(1<<5)) { \
+ if(d < -(1<<7)) { \
+ /* signed 16 */ \
+ const unsigned char buf[3] = {0xd1, STORE16_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else { \
+ /* signed 8 */ \
+ const unsigned char buf[2] = {0xd0, STORE16_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+ } else if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE16_BE8(d), 1); \
+ } else { \
+ if(d < (1<<8)) { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE16_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } else { \
+ /* unsigned 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE16_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } \
+ } \
+} while(0)
+
+#define msgpack_pack_real_int32(x, d) \
+do { \
+ if(d < -(1<<5)) { \
+ if(d < -(1<<15)) { \
+ /* signed 32 */ \
+ const unsigned char buf[5] = {0xd2, STORE32_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } else if(d < -(1<<7)) { \
+ /* signed 16 */ \
+ const unsigned char buf[3] = {0xd1, STORE32_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else { \
+ /* signed 8 */ \
+ const unsigned char buf[2] = {0xd0, STORE32_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+ } else if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE32_BE8(d), 1); \
+ } else { \
+ if(d < (1<<8)) { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE32_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } else if(d < (1<<16)) { \
+ /* unsigned 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE32_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else { \
+ /* unsigned 32 */ \
+ const unsigned char buf[5] = {0xce, STORE32_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } \
+ } \
+} while(0)
+
+#define msgpack_pack_real_int64(x, d) \
+do { \
+ if(d < -(1LL<<5)) { \
+ if(d < -(1LL<<15)) { \
+ if(d < -(1LL<<31)) { \
+ /* signed 64 */ \
+ const unsigned char buf[9] = {0xd3, STORE64_BE64(d)}; \
+ msgpack_pack_append_buffer(x, buf, 9); \
+ } else { \
+ /* signed 32 */ \
+ const unsigned char buf[5] = {0xd2, STORE64_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } \
+ } else { \
+ if(d < -(1<<7)) { \
+ /* signed 16 */ \
+ const unsigned char buf[3] = {0xd1, STORE64_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } else { \
+ /* signed 8 */ \
+ const unsigned char buf[2] = {0xd0, STORE64_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } \
+ } \
+ } else if(d < (1<<7)) { \
+ /* fixnum */ \
+ msgpack_pack_append_buffer(x, &STORE64_BE8(d), 1); \
+ } else { \
+ if(d < (1LL<<16)) { \
+ if(d < (1<<8)) { \
+ /* unsigned 8 */ \
+ const unsigned char buf[2] = {0xcc, STORE64_BE8(d)}; \
+ msgpack_pack_append_buffer(x, buf, 2); \
+ } else { \
+ /* unsigned 16 */ \
+ const unsigned char buf[3] = {0xcd, STORE64_BE16(d)}; \
+ msgpack_pack_append_buffer(x, buf, 3); \
+ } \
+ } else { \
+ if(d < (1LL<<32)) { \
+ /* unsigned 32 */ \
+ const unsigned char buf[5] = {0xce, STORE64_BE32(d)}; \
+ msgpack_pack_append_buffer(x, buf, 5); \
+ } else { \
+ /* unsigned 64 */ \
+ const unsigned char buf[9] = {0xcf, STORE64_BE64(d)}; \
+ msgpack_pack_append_buffer(x, buf, 9); \
+ } \
+ } \
+ } \
+} while(0)
+
+
+#ifdef msgpack_pack_inline_func_fastint
+
+msgpack_pack_inline_func_fastint(_uint8)(msgpack_pack_user x, uint8_t d)
+{
+ const unsigned char buf[2] = {0xcc, STORE8_BE8(d)};
+ msgpack_pack_append_buffer(x, buf, 2);
+}
+
+msgpack_pack_inline_func_fastint(_uint16)(msgpack_pack_user x, uint16_t d)
+{
+ const unsigned char buf[3] = {0xcd, STORE16_BE16(d)};
+ msgpack_pack_append_buffer(x, buf, 3);
+}
+
+msgpack_pack_inline_func_fastint(_uint32)(msgpack_pack_user x, uint32_t d)
+{
+ const unsigned char buf[5] = {0xce, STORE32_BE32(d)};
+ msgpack_pack_append_buffer(x, buf, 5);
+}
+
+msgpack_pack_inline_func_fastint(_uint64)(msgpack_pack_user x, uint64_t d)
+{
+ const unsigned char buf[9] = {0xcf, STORE64_BE64(d)};
+ msgpack_pack_append_buffer(x, buf, 9);
+}
+
+msgpack_pack_inline_func_fastint(_int8)(msgpack_pack_user x, int8_t d)
+{
+ const unsigned char buf[2] = {0xd0, STORE8_BE8(d)};
+ msgpack_pack_append_buffer(x, buf, 2);
+}
+
+msgpack_pack_inline_func_fastint(_int16)(msgpack_pack_user x, int16_t d)
+{
+ const unsigned char buf[3] = {0xd1, STORE16_BE16(d)};
+ msgpack_pack_append_buffer(x, buf, 3);
+}
+
+msgpack_pack_inline_func_fastint(_int32)(msgpack_pack_user x, int32_t d)
+{
+ const unsigned char buf[5] = {0xd2, STORE32_BE32(d)};
+ msgpack_pack_append_buffer(x, buf, 5);
+}
+
+msgpack_pack_inline_func_fastint(_int64)(msgpack_pack_user x, int64_t d)
+{
+ const unsigned char buf[9] = {0xd3, STORE64_BE64(d)};
+ msgpack_pack_append_buffer(x, buf, 9);
+}
+
+#undef msgpack_pack_inline_func_fastint
+#endif
+
+
+msgpack_pack_inline_func(_uint8)(msgpack_pack_user x, uint8_t d)
+{
+ msgpack_pack_real_uint8(x, d);
+}
+
+msgpack_pack_inline_func(_uint16)(msgpack_pack_user x, uint16_t d)
+{
+ msgpack_pack_real_uint16(x, d);
+}
+
+msgpack_pack_inline_func(_uint32)(msgpack_pack_user x, uint32_t d)
+{
+ msgpack_pack_real_uint32(x, d);
+}
+
+msgpack_pack_inline_func(_uint64)(msgpack_pack_user x, uint64_t d)
+{
+ msgpack_pack_real_uint64(x, d);
+}
+
+msgpack_pack_inline_func(_int8)(msgpack_pack_user x, int8_t d)
+{
+ msgpack_pack_real_int8(x, d);
+}
+
+msgpack_pack_inline_func(_int16)(msgpack_pack_user x, int16_t d)
+{
+ msgpack_pack_real_int16(x, d);
+}
+
+msgpack_pack_inline_func(_int32)(msgpack_pack_user x, int32_t d)
+{
+ msgpack_pack_real_int32(x, d);
+}
+
+msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d)
+{
+ msgpack_pack_real_int64(x, d);
+}
+
+
+#ifdef msgpack_pack_inline_func_cint
+
+msgpack_pack_inline_func_cint(_short)(msgpack_pack_user x, short d)
+{
+#if defined(SIZEOF_SHORT) || defined(SHRT_MAX)
+#if SIZEOF_SHORT == 2 || SHRT_MAX == 0x7fff
+ msgpack_pack_real_int16(x, d);
+#elif SIZEOF_SHORT == 4 || SHRT_MAX == 0x7fffffff
+ msgpack_pack_real_int32(x, d);
+#else
+ msgpack_pack_real_int64(x, d);
+#endif
+#else
+if(sizeof(short) == 2) {
+ msgpack_pack_real_int16(x, d);
+} else if(sizeof(short) == 4) {
+ msgpack_pack_real_int32(x, d);
+} else {
+ msgpack_pack_real_int64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_int)(msgpack_pack_user x, int d)
+{
+#if defined(SIZEOF_INT) || defined(INT_MAX)
+#if SIZEOF_INT == 2 || INT_MAX == 0x7fff
+ msgpack_pack_real_int16(x, d);
+#elif SIZEOF_INT == 4 || INT_MAX == 0x7fffffff
+ msgpack_pack_real_int32(x, d);
+#else
+ msgpack_pack_real_int64(x, d);
+#endif
+#else
+if(sizeof(int) == 2) {
+ msgpack_pack_real_int16(x, d);
+} else if(sizeof(int) == 4) {
+ msgpack_pack_real_int32(x, d);
+} else {
+ msgpack_pack_real_int64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_long)(msgpack_pack_user x, long d)
+{
+#if defined(SIZEOF_LONG) || defined(LONG_MAX)
+#if SIZEOF_LONG == 2 || LONG_MAX == 0x7fffL
+ msgpack_pack_real_int16(x, d);
+#elif SIZEOF_LONG == 4 || LONG_MAX == 0x7fffffffL
+ msgpack_pack_real_int32(x, d);
+#else
+ msgpack_pack_real_int64(x, d);
+#endif
+#else
+if(sizeof(long) == 2) {
+ msgpack_pack_real_int16(x, d);
+} else if(sizeof(long) == 4) {
+ msgpack_pack_real_int32(x, d);
+} else {
+ msgpack_pack_real_int64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_long_long)(msgpack_pack_user x, long long d)
+{
+#if defined(SIZEOF_LONG_LONG) || defined(LLONG_MAX)
+#if SIZEOF_LONG_LONG == 2 || LLONG_MAX == 0x7fffL
+ msgpack_pack_real_int16(x, d);
+#elif SIZEOF_LONG_LONG == 4 || LLONG_MAX == 0x7fffffffL
+ msgpack_pack_real_int32(x, d);
+#else
+ msgpack_pack_real_int64(x, d);
+#endif
+#else
+if(sizeof(long long) == 2) {
+ msgpack_pack_real_int16(x, d);
+} else if(sizeof(long long) == 4) {
+ msgpack_pack_real_int32(x, d);
+} else {
+ msgpack_pack_real_int64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_unsigned_short)(msgpack_pack_user x, unsigned short d)
+{
+#if defined(SIZEOF_SHORT) || defined(USHRT_MAX)
+#if SIZEOF_SHORT == 2 || USHRT_MAX == 0xffffU
+ msgpack_pack_real_uint16(x, d);
+#elif SIZEOF_SHORT == 4 || USHRT_MAX == 0xffffffffU
+ msgpack_pack_real_uint32(x, d);
+#else
+ msgpack_pack_real_uint64(x, d);
+#endif
+#else
+if(sizeof(unsigned short) == 2) {
+ msgpack_pack_real_uint16(x, d);
+} else if(sizeof(unsigned short) == 4) {
+ msgpack_pack_real_uint32(x, d);
+} else {
+ msgpack_pack_real_uint64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_unsigned_int)(msgpack_pack_user x, unsigned int d)
+{
+#if defined(SIZEOF_INT) || defined(UINT_MAX)
+#if SIZEOF_INT == 2 || UINT_MAX == 0xffffU
+ msgpack_pack_real_uint16(x, d);
+#elif SIZEOF_INT == 4 || UINT_MAX == 0xffffffffU
+ msgpack_pack_real_uint32(x, d);
+#else
+ msgpack_pack_real_uint64(x, d);
+#endif
+#else
+if(sizeof(unsigned int) == 2) {
+ msgpack_pack_real_uint16(x, d);
+} else if(sizeof(unsigned int) == 4) {
+ msgpack_pack_real_uint32(x, d);
+} else {
+ msgpack_pack_real_uint64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_unsigned_long)(msgpack_pack_user x, unsigned long d)
+{
+#if defined(SIZEOF_LONG) || defined(ULONG_MAX)
+#if SIZEOF_LONG == 2 || ULONG_MAX == 0xffffUL
+ msgpack_pack_real_uint16(x, d);
+#elif SIZEOF_LONG == 4 || ULONG_MAX == 0xffffffffUL
+ msgpack_pack_real_uint32(x, d);
+#else
+ msgpack_pack_real_uint64(x, d);
+#endif
+#else
+if(sizeof(unsigned int) == 2) {
+ msgpack_pack_real_uint16(x, d);
+} else if(sizeof(unsigned int) == 4) {
+ msgpack_pack_real_uint32(x, d);
+} else {
+ msgpack_pack_real_uint64(x, d);
+}
+#endif
+}
+
+msgpack_pack_inline_func_cint(_unsigned_long_long)(msgpack_pack_user x, unsigned long long d)
+{
+#if defined(SIZEOF_LONG_LONG) || defined(ULLONG_MAX)
+#if SIZEOF_LONG_LONG == 2 || ULLONG_MAX == 0xffffUL
+ msgpack_pack_real_uint16(x, d);
+#elif SIZEOF_LONG_LONG == 4 || ULLONG_MAX == 0xffffffffUL
+ msgpack_pack_real_uint32(x, d);
+#else
+ msgpack_pack_real_uint64(x, d);
+#endif
+#else
+if(sizeof(unsigned long long) == 2) {
+ msgpack_pack_real_uint16(x, d);
+} else if(sizeof(unsigned long long) == 4) {
+ msgpack_pack_real_uint32(x, d);
+} else {
+ msgpack_pack_real_uint64(x, d);
+}
+#endif
+}
+
+#undef msgpack_pack_inline_func_cint
+#endif
+
+
+
+/*
+ * Float
+ */
+
+msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d)
+{
+ union { char buf[4]; uint32_t num; } f;
+ *((float*)&f.buf) = d; // FIXME
+ const unsigned char buf[5] = {0xca, STORE32_BE32(f.num)};
+ msgpack_pack_append_buffer(x, buf, 5);
+}
+
+msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d)
+{
+ union { char buf[8]; uint64_t num; } f;
+ *((double*)&f.buf) = d; // FIXME
+ const unsigned char buf[9] = {0xcb, STORE64_BE64(f.num)};
+ msgpack_pack_append_buffer(x, buf, 9);
+}
+
+
+/*
+ * Nil
+ */
+
+msgpack_pack_inline_func(_nil)(msgpack_pack_user x)
+{
+ static const unsigned char d = 0xc0;
+ msgpack_pack_append_buffer(x, &d, 1);
+}
+
+
+/*
+ * Boolean
+ */
+
+msgpack_pack_inline_func(_true)(msgpack_pack_user x)
+{
+ static const unsigned char d = 0xc3;
+ msgpack_pack_append_buffer(x, &d, 1);
+}
+
+msgpack_pack_inline_func(_false)(msgpack_pack_user x)
+{
+ static const unsigned char d = 0xc2;
+ msgpack_pack_append_buffer(x, &d, 1);
+}
+
+
+/*
+ * Array
+ */
+
+msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n)
+{
+ if(n < 16) {
+ unsigned char d = 0x90 | n;
+ msgpack_pack_append_buffer(x, &d, 1);
+ } else if(n < 65536) {
+ uint16_t d = (uint16_t)n;
+ unsigned char buf[3] = {0xdc, STORE16_BE16(d)};
+ msgpack_pack_append_buffer(x, buf, 3);
+ } else {
+ uint32_t d = (uint32_t)n;
+ unsigned char buf[5] = {0xdd, STORE32_BE32(d)};
+ msgpack_pack_append_buffer(x, buf, 5);
+ }
+}
+
+
+/*
+ * Map
+ */
+
+msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n)
+{
+ if(n < 16) {
+ unsigned char d = 0x80 | n;
+ msgpack_pack_append_buffer(x, &STORE8_BE8(d), 1);
+ } else if(n < 65536) {
+ uint16_t d = (uint16_t)n;
+ unsigned char buf[3] = {0xde, STORE16_BE16(d)};
+ msgpack_pack_append_buffer(x, buf, 3);
+ } else {
+ uint32_t d = (uint32_t)n;
+ unsigned char buf[5] = {0xdf, STORE32_BE32(d)};
+ msgpack_pack_append_buffer(x, buf, 5);
+ }
+}
+
+
+/*
+ * Raw
+ */
+
+msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l)
+{
+ if(l < 32) {
+ unsigned char d = 0xa0 | l;
+ msgpack_pack_append_buffer(x, &STORE8_BE8(d), 1);
+ } else if(l < 65536) {
+ uint16_t d = (uint16_t)l;
+ unsigned char buf[3] = {0xda, STORE16_BE16(d)};
+ msgpack_pack_append_buffer(x, buf, 3);
+ } else {
+ uint32_t d = (uint32_t)l;
+ unsigned char buf[5] = {0xdb, STORE32_BE32(d)};
+ msgpack_pack_append_buffer(x, buf, 5);
+ }
+}
+
+msgpack_pack_inline_func(_raw_body)(msgpack_pack_user x, const void* b, size_t l)
+{
+ msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
+}
+
+#undef msgpack_pack_inline_func
+#undef msgpack_pack_user
+#undef msgpack_pack_append_buffer
+
+#undef STORE8_BE8
+
+#undef STORE16_BE8
+#undef STORE16_BE16
+
+#undef STORE32_BE8
+#undef STORE32_BE16
+#undef STORE32_BE32
+
+#undef STORE64_BE8
+#undef STORE64_BE16
+#undef STORE64_BE32
+#undef STORE64_BE64
+
+#undef msgpack_pack_real_uint8
+#undef msgpack_pack_real_uint16
+#undef msgpack_pack_real_uint32
+#undef msgpack_pack_real_uint64
+#undef msgpack_pack_real_int8
+#undef msgpack_pack_real_int16
+#undef msgpack_pack_real_int32
+#undef msgpack_pack_real_int64
+
diff --git a/php/msgpack/unpack_define.h b/php/msgpack/unpack_define.h
new file mode 100644
index 0000000..63668c2
--- /dev/null
+++ b/php/msgpack/unpack_define.h
@@ -0,0 +1,129 @@
+/*
+ * MessagePack unpacking routine template
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef MSGPACK_UNPACK_DEFINE_H__
+#define MSGPACK_UNPACK_DEFINE_H__
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#ifndef __WIN32__
+#include <arpa/inet.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#ifndef MSGPACK_MAX_STACK_SIZE
+#define MSGPACK_MAX_STACK_SIZE 16
+#endif
+
+
+#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN__
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define __BIG_ENDIAN__
+#endif
+#endif
+
+#define msgpack_betoh16(x) ntohs(x)
+#define msgpack_betoh32(x) ntohl(x)
+
+#ifdef __LITTLE_ENDIAN__
+#if defined(__bswap_64)
+# define msgpack_betoh64(x) __bswap_64(x)
+#elif defined(__DARWIN_OSSwapInt64)
+# define msgpack_betoh64(x) __DARWIN_OSSwapInt64(x)
+#else
+static inline uint64_t msgpack_betoh64(uint64_t x) {
+ return ((x << 56) & 0xff00000000000000ULL ) |
+ ((x << 40) & 0x00ff000000000000ULL ) |
+ ((x << 24) & 0x0000ff0000000000ULL ) |
+ ((x << 8) & 0x000000ff00000000ULL ) |
+ ((x >> 8) & 0x00000000ff000000ULL ) |
+ ((x >> 24) & 0x0000000000ff0000ULL ) |
+ ((x >> 40) & 0x000000000000ff00ULL ) |
+ ((x >> 56) & 0x00000000000000ffULL ) ;
+}
+#endif
+#else
+#define msgpack_betoh64(x) (x)
+#endif
+
+
+typedef enum {
+ CS_HEADER = 0x00, // nil
+
+ //CS_ = 0x01,
+ //CS_ = 0x02, // false
+ //CS_ = 0x03, // true
+
+ //CS_ = 0x04,
+ //CS_ = 0x05,
+ //CS_ = 0x06,
+ //CS_ = 0x07,
+
+ //CS_ = 0x08,
+ //CS_ = 0x09,
+ CS_FLOAT = 0x0a,
+ CS_DOUBLE = 0x0b,
+ CS_UINT_8 = 0x0c,
+ CS_UINT_16 = 0x0d,
+ CS_UINT_32 = 0x0e,
+ CS_UINT_64 = 0x0f,
+ CS_INT_8 = 0x10,
+ CS_INT_16 = 0x11,
+ CS_INT_32 = 0x12,
+ CS_INT_64 = 0x13,
+
+ //CS_ = 0x14,
+ //CS_ = 0x15,
+ //CS_BIG_INT_16 = 0x16,
+ //CS_BIG_INT_32 = 0x17,
+ //CS_BIG_FLOAT_16 = 0x18,
+ //CS_BIG_FLOAT_32 = 0x19,
+ CS_RAW_16 = 0x1a,
+ CS_RAW_32 = 0x1b,
+ CS_ARRAY_16 = 0x1c,
+ CS_ARRAY_32 = 0x1d,
+ CS_MAP_16 = 0x1e,
+ CS_MAP_32 = 0x1f,
+
+ //ACS_BIG_INT_VALUE,
+ //ACS_BIG_FLOAT_VALUE,
+ ACS_RAW_VALUE,
+} msgpack_unpack_state;
+
+
+typedef enum {
+ CT_ARRAY_ITEM,
+ CT_MAP_KEY,
+ CT_MAP_VALUE,
+} msgpack_container_type;
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* msgpack/unpack_define.h */
+
diff --git a/php/msgpack/unpack_template.h b/php/msgpack/unpack_template.h
new file mode 100644
index 0000000..d67fd1e
--- /dev/null
+++ b/php/msgpack/unpack_template.h
@@ -0,0 +1,361 @@
+/*
+ * MessagePack unpacking routine template
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef msgpack_unpack_func
+#error msgpack_unpack_func template is not defined
+#endif
+
+#ifndef msgpack_unpack_callback
+#error msgpack_unpack_callback template is not defined
+#endif
+
+#ifndef msgpack_unpack_struct
+#error msgpack_unpack_struct template is not defined
+#endif
+
+#ifndef msgpack_unpack_struct_decl
+#define msgpack_unpack_struct_decl(name) msgpack_unpack_struct(name)
+#endif
+
+#ifndef msgpack_unpack_object
+#error msgpack_unpack_object type is not defined
+#endif
+
+#ifndef msgpack_unpack_user
+#error msgpack_unpack_user type is not defined
+#endif
+
+
+msgpack_unpack_struct_decl(_stack) {
+ msgpack_unpack_object obj;
+ size_t count;
+ unsigned int ct;
+ msgpack_unpack_object map_key;
+};
+
+msgpack_unpack_struct_decl(_context) {
+ msgpack_unpack_user user;
+ unsigned int cs;
+ unsigned int trail;
+ unsigned int top;
+ msgpack_unpack_struct(_stack) stack[MSGPACK_MAX_STACK_SIZE];
+};
+
+
+msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx)
+{
+ ctx->cs = CS_HEADER;
+ ctx->trail = 0;
+ ctx->top = 0;
+ ctx->stack[0].obj = msgpack_unpack_callback(_root)(&ctx->user);
+}
+
+msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context)* ctx)
+{
+ return (ctx)->stack[0].obj;
+}
+
+
+msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
+{
+ assert(len >= *off);
+
+ const unsigned char* p = (unsigned char*)data + *off;
+ const unsigned char* const pe = (unsigned char*)data + len;
+ const void* n = NULL;
+
+ unsigned int trail = ctx->trail;
+ unsigned int cs = ctx->cs;
+ unsigned int top = ctx->top;
+ msgpack_unpack_struct(_stack)* stack = ctx->stack;
+ msgpack_unpack_user* user = &ctx->user;
+
+ msgpack_unpack_object obj;
+ msgpack_unpack_struct(_stack)* c = NULL;
+
+ int ret;
+
+#define push_simple_value(func) \
+ if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \
+ goto _push
+#define push_fixed_value(func, arg) \
+ if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \
+ goto _push
+#define push_variable_value(func, base, pos, len) \
+ if(msgpack_unpack_callback(func)(user, \
+ (const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
+ goto _push
+
+#define again_fixed_trail(_cs, trail_len) \
+ trail = trail_len; \
+ cs = _cs; \
+ goto _fixed_trail_again
+#define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
+ trail = trail_len; \
+ if(trail == 0) { goto ifzero; } \
+ cs = _cs; \
+ goto _fixed_trail_again
+
+#define start_container(func, count_, ct_) \
+ if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
+ if((count_) == 0) { obj = stack[top].obj; goto _push; } \
+ if(top >= MSGPACK_MAX_STACK_SIZE) { goto _failed; } \
+ stack[top].ct = ct_; \
+ stack[top].count = count_; \
+ /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
+ /*printf("stack push %d\n", top);*/ \
+ ++top; \
+ goto _header_again
+
+#define NEXT_CS(p) \
+ ((unsigned int)*p & 0x1f)
+
+#define PTR_CAST_8(ptr) (*(uint8_t*)ptr)
+#define PTR_CAST_16(ptr) msgpack_betoh16(*(uint16_t*)ptr)
+#define PTR_CAST_32(ptr) msgpack_betoh32(*(uint32_t*)ptr)
+#define PTR_CAST_64(ptr) msgpack_betoh64(*(uint64_t*)ptr)
+
+ if(p == pe) { goto _out; }
+ do {
+ switch(cs) {
+ case CS_HEADER:
+ switch(*p) {
+ case 0x00 ... 0x7f: // Positive Fixnum
+ push_fixed_value(_uint8, *(uint8_t*)p);
+ case 0xe0 ... 0xff: // Negative Fixnum
+ push_fixed_value(_int8, *(int8_t*)p);
+ case 0xc0 ... 0xdf: // Variable
+ switch(*p) {
+ case 0xc0: // nil
+ push_simple_value(_nil);
+ //case 0xc1: // string
+ // again_terminal_trail(NEXT_CS(p), p+1);
+ case 0xc2: // false
+ push_simple_value(_false);
+ case 0xc3: // true
+ push_simple_value(_true);
+ //case 0xc4:
+ //case 0xc5:
+ //case 0xc6:
+ //case 0xc7:
+ //case 0xc8:
+ //case 0xc9:
+ case 0xca: // float
+ case 0xcb: // double
+ case 0xcc: // unsigned int 8
+ case 0xcd: // unsigned int 16
+ case 0xce: // unsigned int 32
+ case 0xcf: // unsigned int 64
+ case 0xd0: // signed int 8
+ case 0xd1: // signed int 16
+ case 0xd2: // signed int 32
+ case 0xd3: // signed int 64
+ again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
+ //case 0xd4:
+ //case 0xd5:
+ //case 0xd6: // big integer 16
+ //case 0xd7: // big integer 32
+ //case 0xd8: // big float 16
+ //case 0xd9: // big float 32
+ case 0xda: // raw 16
+ case 0xdb: // raw 32
+ case 0xdc: // array 16
+ case 0xdd: // array 32
+ case 0xde: // map 16
+ case 0xdf: // map 32
+ again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01));
+ default:
+ goto _failed;
+ }
+ case 0xa0 ... 0xbf: // FixRaw
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
+ case 0x90 ... 0x9f: // FixArray
+ start_container(_array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
+ case 0x80 ... 0x8f: // FixMap
+ start_container(_map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
+
+ default:
+ goto _failed;
+ }
+ // end CS_HEADER
+
+
+ _fixed_trail_again:
+ ++p;
+
+ default:
+ if((size_t)(pe - p) < trail) { goto _out; }
+ n = p; p += trail - 1;
+ switch(cs) {
+ //case CS_
+ //case CS_
+ case CS_FLOAT: {
+ union { uint32_t num; char buf[4]; } f;
+ f.num = PTR_CAST_32(n); // FIXME
+ push_fixed_value(_float, *((float*)f.buf)); }
+ case CS_DOUBLE: {
+ union { uint64_t num; char buf[8]; } f;
+ f.num = PTR_CAST_64(n); // FIXME
+ push_fixed_value(_double, *((double*)f.buf)); }
+ case CS_UINT_8:
+ push_fixed_value(_uint8, (uint8_t)PTR_CAST_8(n));
+ case CS_UINT_16:
+ push_fixed_value(_uint16, (uint16_t)PTR_CAST_16(n));
+ case CS_UINT_32:
+ push_fixed_value(_uint32, (uint32_t)PTR_CAST_32(n));
+ case CS_UINT_64:
+ push_fixed_value(_uint64, (uint64_t)PTR_CAST_64(n));
+
+ case CS_INT_8:
+ push_fixed_value(_int8, (int8_t)PTR_CAST_8(n));
+ case CS_INT_16:
+ push_fixed_value(_int16, (int16_t)PTR_CAST_16(n));
+ case CS_INT_32:
+ push_fixed_value(_int32, (int32_t)PTR_CAST_32(n));
+ case CS_INT_64:
+ push_fixed_value(_int64, (int64_t)PTR_CAST_64(n));
+
+ //case CS_
+ //case CS_
+ //case CS_BIG_INT_16:
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint16_t)PTR_CAST_16(n), _big_int_zero);
+ //case CS_BIG_INT_32:
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint32_t)PTR_CAST_32(n), _big_int_zero);
+ //case ACS_BIG_INT_VALUE:
+ //_big_int_zero:
+ // // FIXME
+ // push_variable_value(_big_int, data, n, trail);
+
+ //case CS_BIG_FLOAT_16:
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero);
+ //case CS_BIG_FLOAT_32:
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint32_t)PTR_CAST_32(n), _big_float_zero);
+ //case ACS_BIG_FLOAT_VALUE:
+ //_big_float_zero:
+ // // FIXME
+ // push_variable_value(_big_float, data, n, trail);
+
+ case CS_RAW_16:
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero);
+ case CS_RAW_32:
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero);
+ case ACS_RAW_VALUE:
+ _raw_zero:
+ push_variable_value(_raw, data, n, trail);
+
+ case CS_ARRAY_16:
+ start_container(_array, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM);
+ case CS_ARRAY_32:
+ /* FIXME security guard */
+ start_container(_array, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM);
+
+ case CS_MAP_16:
+ start_container(_map, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY);
+ case CS_MAP_32:
+ /* FIXME security guard */
+ start_container(_map, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY);
+
+ default:
+ goto _failed;
+ }
+ }
+
+_push:
+ if(top == 0) { goto _finish; }
+ c = &stack[top-1];
+ switch(c->ct) {
+ case CT_ARRAY_ITEM:
+ if(msgpack_unpack_callback(_array_item)(user, &c->obj, obj) < 0) { goto _failed; }
+ if(--c->count == 0) {
+ obj = c->obj;
+ --top;
+ /*printf("stack pop %d\n", top);*/
+ goto _push;
+ }
+ goto _header_again;
+ case CT_MAP_KEY:
+ c->map_key = obj;
+ c->ct = CT_MAP_VALUE;
+ goto _header_again;
+ case CT_MAP_VALUE:
+ if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
+ if(--c->count == 0) {
+ obj = c->obj;
+ --top;
+ /*printf("stack pop %d\n", top);*/
+ goto _push;
+ }
+ c->ct = CT_MAP_KEY;
+ goto _header_again;
+
+ default:
+ goto _failed;
+ }
+
+_header_again:
+ cs = CS_HEADER;
+ ++p;
+ } while(p != pe);
+ goto _out;
+
+
+_finish:
+ stack[0].obj = obj;
+ ++p;
+ ret = 1;
+ /*printf("-- finish --\n"); */
+ goto _end;
+
+_failed:
+ /*printf("** FAILED **\n"); */
+ ret = -1;
+ goto _end;
+
+_out:
+ ret = 0;
+ goto _end;
+
+_end:
+ ctx->cs = cs;
+ ctx->trail = trail;
+ ctx->top = top;
+ *off = p - (const unsigned char*)data;
+
+ return ret;
+}
+
+
+#undef msgpack_unpack_func
+#undef msgpack_unpack_callback
+#undef msgpack_unpack_struct
+#undef msgpack_unpack_object
+#undef msgpack_unpack_user
+
+#undef push_simple_value
+#undef push_fixed_value
+#undef push_variable_value
+#undef again_fixed_trail
+#undef again_fixed_trail_if_zero
+#undef start_container
+
+#undef NEXT_CS
+#undef PTR_CAST_8
+#undef PTR_CAST_16
+#undef PTR_CAST_32
+#undef PTR_CAST_64
+
diff --git a/php/php_msgpack.h b/php/php_msgpack.h
new file mode 100644
index 0000000..a1f434c
--- /dev/null
+++ b/php/php_msgpack.h
@@ -0,0 +1,86 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 5 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2007 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Author: |
+ +----------------------------------------------------------------------+
+*/
+
+/* $Id: header 226204 2007-01-01 19:32:10Z iliaa $ */
+
+#ifndef PHP_MSGPACK_H
+#define PHP_MSGPACK_H
+
+extern zend_module_entry msgpack_module_entry;
+#define phpext_msgpack_ptr &msgpack_module_entry
+
+#ifdef PHP_WIN32
+#define PHP_MSGPACK_API __declspec(dllexport)
+#else
+#define PHP_MSGPACK_API
+#endif
+
+#ifdef ZTS
+#include "TSRM.h"
+#endif
+
+PHP_MINIT_FUNCTION(msgpack);
+PHP_MSHUTDOWN_FUNCTION(msgpack);
+PHP_RINIT_FUNCTION(msgpack);
+PHP_RSHUTDOWN_FUNCTION(msgpack);
+PHP_MINFO_FUNCTION(msgpack);
+
+PHP_FUNCTION(msgpack_pack);
+PHP_FUNCTION(msgpack_unpack);
+PHP_FUNCTION(msgpack_unpack_limit);
+
+PHP_METHOD(msgpack, initialize);
+PHP_METHOD(msgpack, execute);
+PHP_METHOD(msgpack, execute_limit);
+PHP_METHOD(msgpack, finished);
+PHP_METHOD(msgpack, data);
+
+static zend_class_entry *msgpack_ce;
+
+/*
+ Declare any global variables you may need between the BEGIN
+ and END macros here:
+
+ZEND_BEGIN_MODULE_GLOBALS(msgpack)
+ long global_value;
+ char *global_string;
+ZEND_END_MODULE_GLOBALS(msgpack)
+*/
+
+/* In every utility function you add that needs to use variables
+ in php_msgpack_globals, call TSRMLS_FETCH(); after declaring other
+ variables used by that function, or better yet, pass in TSRMLS_CC
+ after the last function argument and declare your utility function
+ with TSRMLS_DC after the last declared argument. Always refer to
+ the globals in your function as MSGPACK_G(variable). You are
+ encouraged to rename these macros something shorter, see
+ examples in any other php module directory.
+*/
+
+
+#endif /* PHP_MSGPACK_H */
+
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
diff --git a/php/test_normal.php b/php/test_normal.php
new file mode 100755
index 0000000..ee348c2
--- /dev/null
+++ b/php/test_normal.php
@@ -0,0 +1,19 @@
+<?php
+ //$data = array(array(null=>1), array("takei"=>"hide"), 3);
+ //$data = array("more"=>10, "test", null);
+ //$data = array();
+ $data = array(0=>1,1=>2,2=>3);
+ var_dump($data);
+
+ // serialize
+ $msg = msgpack_pack($data);
+
+ // hexadecimal
+ $str = unpack('H*', $msg);
+ var_dump("0x".$str[1]);
+
+ // deserialize
+ $ret = msgpack_unpack($msg);
+ var_dump($ret);
+?>
+
diff --git a/php/test_streaming.php b/php/test_streaming.php
new file mode 100755
index 0000000..8f89f4c
--- /dev/null
+++ b/php/test_streaming.php
@@ -0,0 +1,34 @@
+<?php
+
+ // serialized data
+ $msgs = array(pack("C*", 0x93, 0x01, 0x02, 0x03, 0x92), pack("C*", 0x03, 0x09, 0x04));
+
+ // streaming deserialize
+ $unpacker = new MessagePack();
+ $unpacker->initialize();
+ $buffer = "";
+ $nread = 0;
+
+ foreach($msgs as $msg){
+ $buffer = $buffer . $msg;
+
+ while(true){
+ $nread = $unpacker->execute($buffer, $nread);
+
+ if($unpacker->finished()){
+ $msg = $unpacker->data();
+ var_dump($msg);
+
+ $unpacker->initialize();
+ $buffer = substr($buffer, $nread);
+ $nread = 0;
+
+ if(!empty($buffer)){
+ continue;
+ }
+ }
+ break;
+ }
+ }
+?>
+
diff --git a/php/tests/test_pack.phpt b/php/tests/test_pack.phpt
new file mode 100644
index 0000000..595e4d0
--- /dev/null
+++ b/php/tests/test_pack.phpt
@@ -0,0 +1,169 @@
+--TEST--
+Test msgpack_pack() function : basic functionality
+--SKIPIF--
+<?php if (!extension_loaded("msgpack")) print "skip"; ?>
+--FILE--
+<?php
+echo "*** Testing msgpack_pack() : basic functionality ***\n";
+
+
+function create_map($num)
+{
+ $data = array();
+ for($i=0; $i<=$num; $i++)
+ $data[$i+1] = $i;
+ return $data;
+}
+
+$inputs = array (
+ // null
+/*1*/ null,
+ NULL,
+
+ // boolean
+/*3*/ false,
+ FALSE,
+ true,
+ TRUE,
+
+ // zero
+/*7*/ 0,
+
+ // positive fixnum
+/*8*/ 1,
+ (1<<6),
+ (1<<7)-1,
+
+ // positive int 8
+/*11*/ (1<<7),
+ (1<<8)-1,
+
+ // positive int 16
+/*13*/ (1<<8),
+ (1<<16)-1,
+
+ // positive int 32
+/*15*/ (1<<16),
+ (1<<32)-1,
+
+ // positive int 64
+/*17*/ (1<<32),
+ (1<<64)-1,
+
+ // negative fixnum
+/*19*/ -1,
+ -((1<<5)-1),
+ -(1<<5),
+
+ // negative int 8
+/*22*/ -((1<<5)+1),
+ -(1<<7),
+
+ // negative int 16
+/*24*/ -((1<<7)+1),
+ -(1<<15),
+
+ //negative int 32
+/*26*/ -((1<<15)+1),
+ -(1<<31),
+
+ // negative int 64
+/*28*/ -((1<<31)+1),
+ -(1<<63),
+
+ // double
+/*30*/ 1.0,
+ 0.1,
+ -0.1,
+ -1.0,
+
+ // fixraw
+/*34*/ "",
+ str_repeat(" ", (1<<5)-1),
+
+ // raw 16
+/*36*/ str_repeat(" ", (1<<5)),
+ //str_repeat(" ", (1<<16)-1),
+
+ // raw 32
+/*38*/ //str_repeat(" ", (1<<16)),
+ //str_repeat(" ", (1<<32)-1), // memory error
+
+ // fixarraw
+/*39*/ array(),
+ range(0, (1<<4)-1),
+
+ // array 16
+/*41*/ range(0, (1<<4)),
+ //range(0, (1<<16)-1),
+
+ // array 32
+/*43*/ //range(0, (1<<16)),
+ //range(0, (1<<32)-1), // memory error
+
+ // fixmap
+ //array(),
+/*44*/ //create_map((1<<4)-1),
+
+ // map 16
+/*45*/ //create_map((1<<4)),
+ //create_map((1<<16)-1),
+
+ // map 32
+/*47*/ //create_map((1<<16))
+ //create_map((1<<32)-1) // memory error
+);
+
+$count = 1;
+foreach($inputs as $input) {
+ echo "-- Iteration $count --\n";
+ $str = unpack('H*', msgpack_pack($input));
+ var_dump("0x".$str[1]);
+ $count ++;
+}
+
+?>
+===DONE===
+--EXPECT--
+*** Testing msgpack_pack() : basic functionality ***
+-- Iteration 1 --
+string(4) "0xc0"
+-- Iteration 2 --
+string(4) "0xc0"
+-- Iteration 3 --
+string(4) "0xc2"
+-- Iteration 4 --
+string(4) "0xc2"
+-- Iteration 5 --
+string(4) "0xc3"
+-- Iteration 6 --
+string(4) "0xc3"
+-- Iteration 7 --
+string(4) "0x00"
+-- Iteration 8 --
+string(4) "0x7f"
+-- Iteration 9 --
+string(6) "0xcc80"
+-- Iteration 10 --
+string(8) "0xcd0100"
+-- Iteration 11 --
+string(4) "0xff"
+-- Iteration 12 --
+string(6) "0xd0df"
+-- Iteration 13 --
+string(8) "0xd1ff7f"
+-- Iteration 14 --
+string(8) "0x810101"
+-- Iteration 15 --
+string(20) "0xcb3ff0000000000000"
+-- Iteration 16 --
+string(4) "0x90"
+-- Iteration 17 --
+string(34) "0x9f000102030405060708090a0b0c0d0e"
+-- Iteration 18 --
+string(40) "0xdc0010000102030405060708090a0b0c0d0e0f"
+-- Iteration 19 --
+string(64) "0x8f0100020103020403050406050706080709080a090b0a0c0b0d0c0e0d0f0e"
+-- Iteration 20 --
+string(72) "0xde00100100020103020403050406050706080709080a090b0a0c0b0d0c0e0d0f0e100f"
+===DONE===