Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88585 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46237 invoked from network); 30 Sep 2015 20:59:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Sep 2015 20:59:00 -0000 Authentication-Results: pb1.pair.com header.from=sean@siobud.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=sean@siobud.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain siobud.com designates 104.236.58.159 as permitted sender) X-PHP-List-Original-Sender: sean@siobud.com X-Host-Fingerprint: 104.236.58.159 siobud.com Received: from [104.236.58.159] ([104.236.58.159:45649] helo=siobud.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FD/D0-38941-21D4C065 for ; Wed, 30 Sep 2015 16:58:59 -0400 Received: from 3006.local (c-73-8-76-141.hsd1.il.comcast.net [73.8.76.141]) by siobud.com (Postfix) with ESMTPSA id BA482D7CED; Wed, 30 Sep 2015 20:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=siobud.com; s=mail; t=1443646736; bh=VAYJv/G1/vBhQeoxrLlLc/OqdBnjxdySdws9GI44CgU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=vxhruH5+wxtMXH9GmiqZUkBxT50I68renD3r/A+83a5Hnfd2y9hdJEe63tItbeUDU qiNzVhHpv9aIKfK1o1ItOLJNyWXHLgWxHPIIiZknCiBZVRPfeFi7RaeQ7c43lGUeYs YgFk/Zg/82VWZi9h0gNmvyH2LOYmHXDGZ+rG8hc4= Date: Wed, 30 Sep 2015 15:58:57 -0500 To: j adams Cc: internals Message-ID: <20150930205857.GA93503@3006.local> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Subject: Re: [PHP-DEV] good example of unserialization fundamentals? From: sean@siobud.com (Sean DuBois) On Wed, Sep 30, 2015 at 01:39:26PM -0700, j adams wrote: > I have completed a first draft of serialization functionality which is > intended to be an updated to amfext. It's on github[0] if anyone is > interested. I would now like to write all the unserialization routines and > would like some suggestions from experienced php devs. I've been reading > Sara Golemon's book, but am still a little unsure. > > In particular, I want my userland function, amf_decode to accept as a > parameter a string (and possibly other args). This is not so difficult but > I must call upon another function, php_amf_decode, which needs to be able > to handle recursive calls. > > Something like this: > > // userland function > static PHP_FUNCTION(amf_decode) { > char *buf; // char buffer, contains serialize data string > int buf_len, buf_cursor=0; // the length of the buffer > long flags = 0; > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &buf, > &buf_len, &flags) == FAILURE) { > return; > } > > if (!buf_len) { > RETURN_NULL(); > } > > php_amf_decode(return_value, buf, buf_len, &buf_cursor, flags > TSRMLS_CC); > } > > // proposed recursive decoding function > PHP_AMF_API void php_amf_decode(zval *return_value, char *buf, int buf_len, > int *buf_cursor, long flags TSRMLS_DC) > { > > // is it safe to use these macros in this recursive function? > RETVAL_BOOL(1); > > // or this > RETURN_LONG(42); > > > } > > > [0] https://github.com/sneakyimp/amfext Yep those are safe! RETVAL_* expands out so you don't have to repeat the variable `return_value` ZVAL_LONG(return_value, l) RETURN_* expands out so you don't have to call return OR specify the variable `return_value` ZVAL_LONG(return_value, l) return; Here they are in 5.6 http://lxr.php.net/xref/PHP_5_6/Zend/zend_API.h#618 --- A great tool to look these things up quickly is lxr, sure beats grep! http://lxr.php.net/search?q=&defs=RETURN_LONG&refs=&path=&hist=&project=PHP_5_6