lua-users home
lua-l archive

LPeg for TLV (BER,DER,CER)

[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...

AltStyle によって変換されたページ (->オリジナル) /