<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Pack</title> <meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <link rel="start" href="index.html" title="GNet Network Library Reference Manual"> <link rel="up" href="libgnet-reference.html" title="GNet Library Reference"> <link rel="prev" href="gnet-base64.html" title="Base64"> <link rel="next" href="gnet-md5.html" title="MD5"> <meta name="generator" content="GTK-Doc V1.8 (XML mode)"> <link rel="stylesheet" href="style.css" type="text/css"> <link rel="chapter" href="gnet-overview.html" title="GNet Overview"> <link rel="chapter" href="gnet-developers.html" title="GNet for developers"> <link rel="chapter" href="gnet-examples.html" title="GNet Examples"> <link rel="chapter" href="libgnet-reference.html" title="GNet Library Reference"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"> <tr valign="middle"> <td><a accesskey="p" href="gnet-base64.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td> <td><a accesskey="u" href="libgnet-reference.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td> <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td> <th width="100%" align="center">GNet Network Library Reference Manual</th> <td><a accesskey="n" href="gnet-md5.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td> </tr> <tr><td colspan="5" class="shortcuts"><nobr><a href="#id387992" class="shortcut">Top</a>  |  <a href="#id388589" class="shortcut">Description</a></nobr></td></tr> </table> <div class="refentry" lang="en"> <a name="gnet-pack"></a><div class="titlepage"></div> <div class="refnamediv"><table width="100%"><tr> <td valign="top"> <h2> <a name="id387992"></a><span class="refentrytitle">Pack</span> </h2> <p>Pack — Byte packing and unpacking functions</p> </td> <td valign="top" align="right"></td> </tr></table></div> <div class="refsynopsisdiv"> <h2>Synopsis</h2> <pre class="synopsis"> #include <gnet.h> gint <a href="gnet-pack.html" title="Pack">gnet_pack</a> (const gchar *format, gchar *buffer, const gint length, ...); gint <a href="gnet-pack.html#gnet-pack-strdup">gnet_pack_strdup</a> (const gchar *format, gchar **bufferp, ...); gint <a href="gnet-pack.html#gnet-vpack">gnet_vpack</a> (const gchar *format, gchar *buffer, const gint length, va_list args); gint <a href="gnet-pack.html#gnet-calcsize">gnet_calcsize</a> (const gchar *format, ...); gint <a href="gnet-pack.html#gnet-vcalcsize">gnet_vcalcsize</a> (const gchar *format, va_list args); gint <a href="gnet-pack.html#gnet-unpack">gnet_unpack</a> (const gchar *format, const gchar *buffer, gint length, ...); gint <a href="gnet-pack.html#gnet-vunpack">gnet_vunpack</a> (const gchar *format, const gchar *buffer, gint length, va_list args); </pre> </div> <div class="refsect1" lang="en"> <a name="id388589"></a><h2>Description</h2> <p> The pack module provides functions for converting data into bytes ("packing") and converting bytes to data ("unpacking"). For example, two integers and a string may be packed into a buffer to be transfered over the network. These functions are similar to the ones in Python and Perl. </p> </div> <div class="refsect1" lang="en"> <a name="id388606"></a><h2>Details</h2> <div class="refsect2" lang="en"> <a name="id388616"></a><h3> <a name="gnet-pack"></a>gnet_pack ()</h3> <a class="indexterm" name="id388628"></a><pre class="programlisting">gint gnet_pack (const gchar *format, gchar *buffer, const gint length, ...);</pre> <p> Writes <em class="parameter"><code>Varargs</code></em> to <em class="parameter"><code>buffer</code></em>. <em class="parameter"><code>format</code></em> is a string that describes the <em class="parameter"><code>Varargs</code></em> and how they are to be packed. This string is a list of characters, each describing the type of an argument in <em class="parameter"><code>Varargs</code></em>. </p> <p> An example: </p> <p> </p> <div class="informalexample"><pre class="programlisting"> char buf[5]; int myint = 42; int mybyte = 23; gnet_pack("!ib", buf, sizeof(buf), myint, mybyte); /* Now buf is { 42, 0, 0, 0, 23 }; */ </pre></div> <p> </p> <p> As a shortcut, most types can be prefixed by an integer to specify how many times the type is repeated. For example, "4i2b" is equivalent to "iiiibb". This repeat argument is refered below to as REPEAT. </p> <p> Native byte order and sizes are used by default. If the first character of <em class="parameter"><code>format</code></em> is < then little endian order and standard sizes are used. If the first character is > or !, then big endian (or network) order and standard size are used. Standard sizes are 1 byte for chars, 2 bytes for shorts, and 4 bytes for ints and longs. </p> <p> The types: </p> <p> x is a NULL character. It can be used for padding. It does not correspond to an argument in <em class="parameter"><code>Varargs</code></em>. </p> <p> b/B is a signed/unsigned char. </p> <p> h/H is a signed/unsigned short. </p> <p> i/I is a signed/unsigned int. </p> <p> l/L is a signed/unsigned long. </p> <p> f/D is a float/double (always native order/size). </p> <p> v is a void pointer (always native size). </p> <p> s is a zero-terminated string. </p> <p> S is a zero-padded string of maximum length REPEAT. We write up-to a NULL character or REPEAT characters, whichever comes first. We then write NULL characters up to a total of REPEAT characters. Special case: if REPEAT is not specified, we write the string as a non-NULL-terminated string (note that it can't be unpacked easily then). </p> <p> r is a byte array of NEXT bytes. NEXT is the next argument passed to <a href="gnet-pack.html" title="Pack"><code class="function">gnet_pack()</code></a> and is a gint. </p> <p> R is a byte array of REPEAT bytes. REPEAT must be specified. </p> <p> p is a Pascal string. The string passed is a NULL-termiated string of less than 256 character. The string writen is a non-NULL-terminated string with a byte before the string storing the string length. REPEAT is repeat. </p> <p> Mnemonics: (B)yte, s(H)ort, (I)nteger, (F)loat, (D)ouble, (V)oid pointer, (S)tring, (R)aw </p> <p> Pack was inspired by Python's and Perl's pack. It is more like Python's than Perl's. Note that in GNet, a repeat of 0 does not align the data (as in Python).</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> pack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to pack to </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>...</code></em> :</span></td> <td> variables to pack from </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> number of bytes packed; -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id388899"></a><h3> <a name="gnet-pack-strdup"></a>gnet_pack_strdup ()</h3> <a class="indexterm" name="id388911"></a><pre class="programlisting">gint gnet_pack_strdup (const gchar *format, gchar **bufferp, ...);</pre> <p> Writes <em class="parameter"><code>Varargs</code></em> into a buffer pointed to by <em class="parameter"><code>bufferp</code></em>. The buffer is allocated by the function and is caller owned. See <a href="gnet-pack.html" title="Pack"><code class="function">gnet_pack()</code></a> for more information.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> pack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>bufferp</code></em> :</span></td> <td> pointer to a buffer (buffer is caller owned) </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>...</code></em> :</span></td> <td> variables to pack from </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> bytes packed; -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id389029"></a><h3> <a name="gnet-vpack"></a>gnet_vpack ()</h3> <a class="indexterm" name="id389041"></a><pre class="programlisting">gint gnet_vpack (const gchar *format, gchar *buffer, const gint length, va_list args);</pre> <p> Var arg interface to <a href="gnet-pack.html" title="Pack"><code class="function">gnet_pack()</code></a>. See <a href="gnet-pack.html" title="Pack"><code class="function">gnet_pack()</code></a> for more information.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> pack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to pack to </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>args</code></em> :</span></td> <td> var args </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> bytes packed; -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id389190"></a><h3> <a name="gnet-calcsize"></a>gnet_calcsize ()</h3> <a class="indexterm" name="id389202"></a><pre class="programlisting">gint gnet_calcsize (const gchar *format, ...);</pre> <p> Calculates the size of the buffer needed to pack <em class="parameter"><code>Varargs</code></em> by the given format. See <a href="gnet-pack.html" title="Pack"><code class="function">gnet_pack()</code></a> for more information.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> pack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>...</code></em> :</span></td> <td> variables </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> number of bytes required to pack; -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id389293"></a><h3> <a name="gnet-vcalcsize"></a>gnet_vcalcsize ()</h3> <a class="indexterm" name="id389305"></a><pre class="programlisting">gint gnet_vcalcsize (const gchar *format, va_list args);</pre> <p> Var arg interface to <a href="gnet-pack.html#gnet-calcsize"><code class="function">gnet_calcsize()</code></a>. See <a href="gnet-pack.html#gnet-calcsize"><code class="function">gnet_calcsize()</code></a> for additional information.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> pack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>args</code></em> :</span></td> <td> var args </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> number of bytes required to pack; -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id389407"></a><h3> <a name="gnet-unpack"></a>gnet_unpack ()</h3> <a class="indexterm" name="id389419"></a><pre class="programlisting">gint gnet_unpack (const gchar *format, const gchar *buffer, gint length, ...);</pre> <p> Reads the data in <em class="parameter"><code>buffer</code></em> into <em class="parameter"><code>Varargs</code></em>. <em class="parameter"><code>format</code></em> is a string that describes the <em class="parameter"><code>Varargs</code></em> and how they are to be packed. This string is a list of characters, each describing the type of an argument in <em class="parameter"><code>Varargs</code></em>. </p> <p> An example: </p> <p> </p> <div class="informalexample"><pre class="programlisting"> char buf[5] = { 42, 0, 0, 0, 23 }; int myint; int mybyte; gnet_unpack("!ib", buf, sizeof(buf), &myint, &mybyte); /* Now myint is 42 and mybyte is 23 */ </pre></div> <p> </p> <p> In unpack, the arguments must be pointers to the appropriate type. Strings and byte arrays are allocated dynamicly (by <code class="function">g_new()</code>). The caller is responsible for <code class="function">g_free()</code>-ing it. </p> <p> As a shortcut, most types can be prefixed by an integer to specify how many times the type is repeated. For example, "4i2b" is equivalent to "iiiibb". This repeat argument is refered below to as REPEAT. </p> <p> The types: </p> <p> x is a pad byte. The byte is skipped and not stored. We do not check its value. </p> <p> b/B is a signed/unsigned char. </p> <p> h/H is a signed/unsigned short. </p> <p> i/I is a signed/unsigned int. </p> <p> l/L is a signed/unsigned long. </p> <p> f/D is a float/double (always native order/size). v is a void pointer (always native size). </p> <p> s is a zero-terminated string. </p> <p> S is a zero-padded string of length REPEAT. We read REPEAT characters or until a NULL character. Any remaining characters are filled in with 0's. REPEAT must be specified. </p> <p> r is a byte array of NEXT bytes. NEXT is the next argument and is a gint. REPEAT is repeat. </p> <p> R is a byte array of REPEAT bytes. REPEAT must be specified. </p> <p> p is a Pascal string. The first byte read is the length of the string and the string follows. The unpacked string will be a normal, NULL-terminated string. REPEAT is repeat.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> unpack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to unpack from </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>...</code></em> :</span></td> <td> addresses of variables to unpack to </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> number of bytes unpacked; -1 on error. The bytes are unpacked to the variables pointed to by the <em class="parameter"><code>Varargs</code></em>. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id389673"></a><h3> <a name="gnet-vunpack"></a>gnet_vunpack ()</h3> <a class="indexterm" name="id389685"></a><pre class="programlisting">gint gnet_vunpack (const gchar *format, const gchar *buffer, gint length, va_list args);</pre> <p> Var arg interface to <a href="gnet-pack.html#gnet-unpack"><code class="function">gnet_unpack()</code></a>. See <a href="gnet-pack.html#gnet-unpack"><code class="function">gnet_unpack()</code></a> for more information.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>format</code></em> :</span></td> <td> unpack data format </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to unpack from </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>args</code></em> :</span></td> <td> var args </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> number of bytes packed; -1 on error. </td> </tr> </tbody> </table></div> </div> </div> </div> </body> </html>