Lua Pack

lua-users home
wiki


[!] VersionNotice: The below code pertains to an older Lua version, Lua 4. It does not run as is under Lua 5.

Description

This is a simple Lua module for packing and unpacking binary data. packlib is based on Luiz Henrique de Figueiredo's pack library [1]. I just changed the template format to fit my own needs and converted it into a Lua module (see LuaBinaryModules).

It exports two functions: bpack and bunpack.

bpack is called as follows: bpack(F,x1,x2,...), where F is a string describing how the value x1, x2, ... are to be interpreted and formatted. Each letter in F consumes one of the values. The letter codes understood by bpack are:

bpack returns a (binary) string containing the values packed as described in F.

bunpack is called as follows: bunpack(s,F,[init]), where s is a (binary) string containing data packed by pack, F is a string describing what is to be read from s, and the optional init marks where in s to read the values.

bunpack returns one value per letter in F (the letters codes are the same as for bpack); bpack also returns the position of the next unread position in s, which can be used as the init position in a subsequent call to bunpack. If the position returned by bunpack is beyond the end of s, then bunpack should not be called again, of course, or an error will be raised.

Examples

The following code reads the header of a tga file:

header = read( file, 18 )

id_length, colormap_type, image_type,
colormap_index, colormap_length,
colormap_size, 
x_origin, y_origin, width, height,
pixel_size, attributes, num = bunpack( header, 'CCCSSCSSSSCC' )

Downloads

Binaries

[Win32 DLL] | [GNU/Linux-i386 .so]

Note: the win32 binaries have been built using the [MinGW] gcc compiler.

Note: The win32 binaries have been updated to use the lua-4.0.dll instead of statically linking it.

Sources

[packlib.zip]

[packlib.tar.gz]

Updated Source (for 5.0 and 5.1) can be found at: [http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/]


RecentChanges · preferences
edit · history
Last edited October 31, 2007 8:43 pm GMT (diff)