lua-users home
lua-l archive

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


Does anybody have any suggestions on how to use LPeg 0.6 for TLV
parsing, especially BER?

... here's what I have so far..
local TAG =
	-- MULTI-BYTE TAG  'XXX1 1111', '1XXX XXXX'* '0XXX XXXX'
	lpeg.S('\31\63\95\127\159\191\223\255') * lpeg.R('\128\255')^0 * lpeg.R('\0\127')
	-- SINGLE-BYTE TAG
	+ 1

local LENGTH_VALUE =
	-- Short-form
	#lpeg.R('\0\127') * GetShortLengthValue
	-- Indefinite form (terminates when a Tag=0,Length=0 item found)
	+ #lpeg.P('\128') * GetIndefiniteLengthValue
	-- Long-form (lower 7 bits determine # of bytes in length value)
	--    EX:   82 01 00  == 256 byte length
	+ #lpeg.R('\129\255') * GetLongLengthValue


Basically I think I need some way to use the input and have LPeg
operate over a sub-string of the data...  I'd prefer that I didn't have
to take a 'real' sub-string due to the costs, but I'm not sure how I'd
do it otherwise...

Note that I also have Lua struct (the one that has pack,unpack,size)
and bitlib for some value parsing.....

Another minor restriction is that adding more stuff in 'C' is out of
the question due to restrictions...