Home page logo

nmap-dev logo Nmap Development mailing list archives

[RFC] NSE pack/unpack library
From: "Philip Pickering" <pgpickering () gmail com>
Date: Fri, 20 Jun 2008 00:29:54 +0200


I've started working on a NSE library for handling binary data,
comparable to the perl pack/unpack functions. It's based on the
lpack library and therefore it differs from perl's pack/unpack.

Basically, there will be two functions, bin.pack and bin.unpack:

bin.pack(template, arg1, arg2, ...)
... template is the format string (see below)
... argN are the data values, which should be packed, according to the template
--> returns a string with the packed data

bin.unpack(bindata, template)
... bindata is a string with the packed binary data
... template is, again, the format string
--> returns the position where it stopped as first value and
the unpacked data values as the following return values
(the position can be used to subsequently fetch more data
by using it as a third parameter)

The format string which lpack uses is a bit different to perl's,
some operator characters stand for completely different things,
but I modified as many as possible to match perl a bit more. Right
now they are:

'Z' ... zero-terminated string
'p' ... string preceded by length byte
'P' ... string preceded by length word
'a' ... string preceded by length size_t
'A' ... string
'f' ... float
'd' ... double
'n' ... Lua number
'c' ... char
'C' ... byte = unsigned char
's' ... short
'S' ... unsigned short
'i' ... int
'I' ... unsigned int
'l' ... long
'L' ... unsigned long
'<' ... little endian
'>' ... big endian
'=' ... native endian

(note that the last three are modifiers)

Numerical modifiers following the operators stand for
repetitions (or to tell unpack how many characters to
read if using 'A').

What's missing is the B/b (bit string starting with MSB/LSB) and
H (hex string). Operators like n, N, v and V for big/little endian
shorts and longs seem unnecessary because of '<' and '>'.

I'm unsure if certain features from the perl-version are
needed,  for example 'u' to uuencode strings
(since another task I'll work on addresses base64 they
should probably get their own mechanism, if uuencoding
is really needed).

Operators I want to add are the aforemetioned 'b'/'B' and 'H',
but also 'x' (for a null byte) and maybe 'X' to back up a byte.

Are there any other features which might be useful
or important? I'd also appreciate any other comments.


Sent through the nmap-dev mailing list
Archived at http://SecLists.Org

  By Date           By Thread  

Current thread:
[ Nmap | Sec Tools | Mailing Lists | Site News | About/Contact | Advertising | Privacy ]