Archive for June, 2007

Erlang R11B-5 in Fink

June 19, 2007

My friend jao has asked me to update the Erlang package in Fink to the latest version. It is still under validation, so until it is committed you can use this file. To install it, just type:

$ tar zxvf erlang-otp-R11B-5-fink.tar.gz
$ sudo cp erlang-otp-R11B-5-fink/*
          /sw/fink/dists/unstable/main/finkinfo/languages
$ fink index; fink rebuild erlang-otp; fink install erlang-otp

It’s better if you backup your old Erlang Fink files, just in case.

Update 2007/12/05: Erlang R12B-0 has been released and the Fink package updated.

Update 2007/07/06: committed to Fink unstable.

Update 2007/06/28: added unixodbc2 and ncurses5 dependencies.

ECL 0.9i in Fink

June 19, 2007

For anyone interested in ECL (Embeddable Common-Lisp), I have created the package for Fink. It is still under validation, so until it is committed you can use this file. To install it, just type:

$ tar zxvf ecl-0.9i-fink.tar.gz
$ sudo cp ecl-0.9i-fink/*
          /sw/fink/dists/local/main/finkinfo
$ fink index; fink install ecl

Please, note that when the official ECL Fink package is updated you will have to remove your local copy (from /sw/fink/dists/local/main/finkinfo/).

Update 2008/01/03: ECL 0.9j has been released and the Fink package updated.

Update 2007/07/06: committed to Fink unstable.

Packing and unpacking bit structures in Python

June 16, 2007

Last week, I released the first version of the BitPacket Python module which allows you to pack and unpack data like the struct and array modules, but in an object-oriented way. At work I needed an easy way to create network packets and at that time I did not know the existence of the struct and array modules, so I googled a bit and I found out the BitVector class for a memory-efficient packed representation of bit arrays, which I decided to use for my purpose.

I implemented three classes, BitField, BitStructure and BitVariableStructure (the lastest two are derived from BitField). A network packet would be represented by the BitStructure class, which at creation does not contain any field, and the idea is that any BitField subclass might be added to it.

I’ll will show you the most basic example. Suppose, you need a simple network packet like the one below:

+---------------+-------------------+
|  id (1 byte)  |  address (4 byte) |
+---------------+-------------------+

You could easily create a network packet using BitStructure, like this:

>>> bs = BitStructure('mypacket')
>>> bs.append(BitField('id', BYTE_SIZE, 0x54))
>>> bs.append(BitField('address', INTEGER_SIZE, 0x10203040))

and print its contents:

>>> print bs
>>> (mypacket =
>>>   (id = 0x54)
>>>   (address = 0x10203040))

In order to unpack an incoming packet, we could use the variable created above or a new one without default values:

>>> bs = BitStructure('mypacket')
>>> bs.append(BitField('id', BYTE_SIZE))
>>> bs.append(BitField('address', INTEGER_SIZE))

In order to unpack an incoming array of bytes, we would do the following:

>>> data = array.array('B', [0x38, 0x87, 0x34, 0x21, 0x40])
>>> bs.set_stream(data)

We can then access to the packet fields by their name:

>>> print '0x%X' % bs['id']
0x38
>>> print '0x%X' % bs['address']
0x87342140

There are a lot more possibilities to pack and unpack bit field structures by using BitStructure and BitVariableStructure. You can see all of them in the module’s online documentation.