<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Musings of an Insane Mind &#187; pack</title>
	<atom:link href="http://cd34.com/blog/tag/pack/feed/" rel="self" type="application/rss+xml" />
	<link>http://cd34.com/blog</link>
	<description>This is my blog, there are many others like it but this one is mine.</description>
	<lastBuildDate>Tue, 29 Jun 2010 04:22:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Python, Perl and PHP interoperability with pack and unpack</title>
		<link>http://cd34.com/blog/programming/python/python-perl-and-php-interoperability-with-pack-and-unpack/</link>
		<comments>http://cd34.com/blog/programming/python/python-perl-and-php-interoperability-with-pack-and-unpack/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 04:40:40 +0000</pubDate>
		<dc:creator>cd34</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[pack]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[unpack]]></category>

		<guid isPermaLink="false">http://cd34.com/blog/?p=629</guid>
		<description><![CDATA[Perl has very powerful capabilities for dealing with structures.  PHP&#8217;s support of those structures was based on Perl&#8217;s wisdom.  Python went a different direction. Perl pack/unpack definitions PING_FORMAT =&#62; &#8216;(a4n2N2N/a*)@245&#8242;; TASK_FORMAT =&#62; &#8216;a4NIN/a*a*&#8217;; RETR_FORMAT =&#62; &#8216;a4N/a*N&#8217;; ENCPAYLOAD_FORMAT =&#62; &#8216;Na*&#8217;; PHP pack/unpack definitions define(&#8216;TASK_FORMAT&#8217;, &#8216;a4NINa*a*&#8217;); define(&#8220;ENCPAYLOAD_FORMAT&#8221;,&#8217;Na*&#8217;); For a communications package written in perl that communicates with [...]]]></description>
			<content:encoded><![CDATA[<p>Perl has very powerful capabilities for dealing with structures.  PHP&#8217;s support of those structures was based on Perl&#8217;s wisdom.  Python went a different direction.</p>
<p>Perl pack/unpack definitions</p>
<blockquote><p>PING_FORMAT =&gt; &#8216;(a4n2N2N/a*)@245&#8242;;<br />
TASK_FORMAT =&gt; &#8216;a4NIN/a*a*&#8217;;<br />
RETR_FORMAT =&gt; &#8216;a4N/a*N&#8217;;<br />
ENCPAYLOAD_FORMAT =&gt; &#8216;Na*&#8217;;</p></blockquote>
<p>PHP pack/unpack definitions</p>
<blockquote><p>define(&#8216;TASK_FORMAT&#8217;, &#8216;a4NINa*a*&#8217;);<br />
define(&#8220;ENCPAYLOAD_FORMAT&#8221;,&#8217;Na*&#8217;);</p></blockquote>
<p>For a communications package written in perl that communicates with 32 bit and 64 bit machines that may not share the same endian structure.  The problem I&#8217;ve run into now is that Python does not support the Perl method, and, I don&#8217;t know why they didn&#8217;t at least offer some compatibility.  pack and unpack give enormous power to communication systems between machines and their support of the perl methods allowed for reasonable interoperability between the two platforms.</p>
<p>Python on the other hand opted to not support some of the features, which was one issue, but, their requirement is that you cannot send variable length packets.</p>
<p>In Python, we&#8217;re able to replicate N, network endian Long by using !L:</p>
<p>&gt;&gt;&gt; import struct<br />
&gt;&gt;&gt; print struct.unpack(&#8216;!L&#8217;,'\0\0\1\0&#8242;);<br />
(256,)</p>
<p>However, there is no method to support a variable length payload behind that value.  We&#8217;re able to set a fixed length like 5s, but, this means that we&#8217;ve got to know the length of the payload being sent.</p>
<p>&gt;&gt;&gt; print struct.unpack(&#8216;!L5s&#8217;,'\0\0\1\0abcde&#8217;);<br />
(256, &#8216;abcde&#8217;)</p>
<p>If we overstate the size of the field, Python is more than happy to tell us that the payload length doesn&#8217;t match the length of the data.</p>
<p>&gt;&gt;&gt; print struct.unpack(&#8216;!L8s&#8217;,'\0\0\1\0abcde&#8217;);<br />
Traceback (most recent call last):<br />
File &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt;<br />
File &#8220;/usr/lib/python2.5/struct.py&#8221;, line 87, in unpack<br />
return o.unpack(s)<br />
struct.error: unpack requires a string argument of length 12</p>
<p>The cheeseshop/pypi seems to show no suitable alternative which brings up a quandry.  For this particular solution, I&#8217;ll write a wrapper function to do the heavy lifting on the two unpack strings I need to deal with and then I&#8217;ll debate pulling the perl unpack/pack routines out of the perl source and wrapping it into an .egg, possibly for distribution.</p>
]]></content:encoded>
			<wfw:commentRss>http://cd34.com/blog/programming/python/python-perl-and-php-interoperability-with-pack-and-unpack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
