lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Todd Coram once stated:
> On Mon, Jul 15, 2013, at 09:58 PM, Luiz Henrique de Figueiredo wrote:
> > > 64-bit IEEE floating point:
> > > <<Sign:1,Exponent:11,Mantissa:52>>
> > > 
> > > IP datagram:
> > > <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16, 
> > >       ID:16, Flgs:3, FragOff:13,
> > >       TTL:8, Proto:8, HdrChkSum:16,
> > >       SrcIP:32, DestIP:32, RestDgram/binary>>
> > 
> > Given these strings it easy to parse them into a Lua table containing the
> > names and starting and end points of the bitfields described there. That
> > table can then be used to set index and newindex metamethods for get and
> > put the bitfields using Lua syntax, or perhaps pack and unpack functions:
> > 
> > template = "<<Sign:1,Exponent:11,Mantissa:52>>"
> > name = "64-bit IEEE floating point"
> > b = bitfelds.parse(name,template)
> > s = io.read(file,b.size)
> > t = b:unpack(s)
> > print(t.Sign,t.Exponent,t.Mantissa)
> > t.Exponent=0
> > s = b:pack(t)
> > 
> 
> Erlang's bit syntax is a joy to work with and I miss it every time I use
> another programming language to decode protocols.
> In particular, I like the inline binding to variables as opposed to the
> regex/scanf like approach taken by other languages.
> 
> An efficient analogue (to Erlang's bit syntax) in Lua would be
> tremendously useful.
> 
> Lua tables would work great for something like this.  :)

  A good test for this would be DNS packets.  The header portion isn't that
bad:

	id:16,
	query:1,	-- boolean, 0=query, 1=response
	opcode:4,	-- type of query
	aa:1,		-- boolean, authoritative answer
	tc:1,		-- boolean, truncation
	rd:1,		-- boolean, recursion desired
	ra:1,		-- boolean, recursion available
	z:1,		-- not used, must be 0
	ad:1,		-- boolean, authentic bit
	cd:1,		-- boolean, checking disabled
	rcode:4,	-- return code
	qcount:16,	-- query count
	ancount:16,	-- answer count
	nscount:16,	-- nameserver count
	adcount:16	-- additional count

But the record types are variable length:

	name:??,	-- key for record, example: www.conman.org
	type:16,	-- type of record
	class:16,	-- class of record
	ttl:16,		-- Time-To-Live
	rdlength:16,	-- length of rdata
	rdata:??	-- rest of data

And the name portion isn't easy to skip---you *have* to parse it to find the
end (check RFC-1035 for the gory details).  

  -spc