pack

Name

pack -- Data packing and unpacking.

Synopsis



gint        gnet_pack                       (const gchar *format,
                                             gchar *buffer,
                                             const guint len,
                                             ...);
gint        gnet_pack_strdup                (const gchar *format,
                                             gchar **buffer,
                                             ...);
gint        gnet_calcsize                   (const gchar *format,
                                             ...);
gint        gnet_vcalcsize                  (const gchar *format,
                                             va_list args);
gint        gnet_vpack                      (const gchar *format,
                                             gchar *buffer,
                                             const guint len,
                                             va_list args);
gint        gnet_unpack                     (const gchar *format,
                                             gchar *buffer,
                                             guint len,
                                             ...);
gint        gnet_vunpack                    (const gchar *format,
                                             gchar *buffer,
                                             guint len,
                                             va_list args);

Description

Perl/python style functions for converting a list of data to an array of bytes (packing) and back again (unpacking).

Details

gnet_pack ()

gint        gnet_pack                       (const gchar *format,
                                             gchar *buffer,
                                             const guint len,
                                             ...);

The pack format string is a list of types. Each type is represented by a character. Most types can be prefixed by an integer, which represents how many times it is repeated (eg, "4i2b" is equivalent to "iiiibb".

Native size/order is the default. If the first character of FORMAT is < then little endian order and standard size 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. x is a pad byte. The pad byte is the NULL character.

b/B are signed/unsigned chars

h/H are signed/unsigned shorts

i/I are signed/unsigned ints

l/L are signed/unsigned longs

f/D are floats/doubles (always native order/size) v is a void pointer (always native size)

s is a zero-terminated string. REPEAT is repeat.

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).

r is a byte array of NEXT bytes. NEXT is the next argument and is an integer. REPEAT is repeat. (r is from "raw")

R is a byte array of REPEAT bytes. REPEAT must be specified.

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.

Mnemonics: (B)yte, s(H)ort, (I)nteger, (F)loat, (D)ouble, (V)oid pointer, (S)tring, (R)aw

pack was mostly inspired by Python's pack, with some awareness of Perl's pack. We don't do Python 0-repeat-is-alignment. Submit a patch if you really want it.

format : Pack format
buffer : Buffer to pack to
len : Length of buffer
... : Variables to pack from
Returns : bytes packed; -1 if error.


gnet_pack_strdup ()

gint        gnet_pack_strdup                (const gchar *format,
                                             gchar **buffer,
                                             ...);

Packs the arguments into an allocated buffer. Caller is responsible for deallocating the buffer.

format : Pack format (see gnet_pack)
buffer : Pointer to buffer to allocate and pack to
... : Variables to pack from
Returns : bytes packed; -1 if error.


gnet_calcsize ()

gint        gnet_calcsize                   (const gchar *format,
                                             ...);

Calculate the size of the buffer needed to pack the given format. All arguments should be passed.

format : Pack format
... : Variables
Returns : number of bytes required to pack; -1 if error.


gnet_vcalcsize ()

gint        gnet_vcalcsize                  (const gchar *format,
                                             va_list args);

Var arg interface to gnet_calcsize(). Size gnet_calcsize() for additional information.

format : Pack format
args : var args
Returns : number of bytes required to pack; -1 if error.


gnet_vpack ()

gint        gnet_vpack                      (const gchar *format,
                                             gchar *buffer,
                                             const guint len,
                                             va_list args);

Var arg interface to gnet_pack(). See gnet_pack() for format information.

format : Pack format (see gnet_pack)
buffer : Buffer to pack to
len : Length of buffer
args : var args
Returns : bytes packed; -1 if error.


gnet_unpack ()

gint        gnet_unpack                     (const gchar *format,
                                             gchar *buffer,
                                             guint len,
                                             ...);

The unpack format string is a list of types. Each type is represented by a character. Most types can be prefixed by an integer, which represents how many times it is repeated (eg, "4i2b" is equivalent to "iiiibb".

In unpack, the arguments must be pointers to the appropriate type. Strings and byte arrays are allocated dynamicly (by g_new). The caller is responsible for g_free()-ing it.

Native size/order is the default. If the first character of FORMAT is < then little endian order and standard size 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.

x is a pad byte. The byte is skipped and not stored. We do not check its value.

b/B are signed/unsigned chars

h/H are signed/unsigned shorts (h is from sHort)

i/I are signed/unsigned ints

l/L are signed/unsigned longs

f/D are floats/doubles (always native order/size) v is a void pointer (always native size)

s is a zero-terminated string. REPEAT is repeat.

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.

r is a byte array of NEXT bytes. NEXT is the next argument and is an integer. REPEAT is repeat. (r is from "raw")

R is a byte array of REPEAT bytes. REPEAT must be specified.

String/byte array memory is allocated by unpack.

Mnemonics: sHort, Integer, Float, Double, Pointer, String, Raw

unpack was mostly inspired by Python's unpack, with some awareness of Perl's unpack.

format : Unpack format
buffer : Buffer to unpack from
len : Length of buffer
... : Addresses of variables to unpack to
Returns : bytes unpacked; -1 if error.


gnet_vunpack ()

gint        gnet_vunpack                    (const gchar *format,
                                             gchar *buffer,
                                             guint len,
                                             va_list args);

Var arg interface to gnet_unpack(). See gnet_unpack() for format information.

format : Unpack format (see below)
buffer : Buffer to unpack from
len : Length of buffer
args : var args
Returns : bytes packed; -1 if error.