This is a simple Lua module for packing and unpacking binary data.
packlib is based on Luiz Henrique de Figueiredo's pack library . 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.
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' )
[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.
Updated Source (for 5.0 and 5.1) can be found at: [http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/]