Package tlslite :: Package utils :: Module codec :: Class Parser
[hide private]
[frames] | no frames]

type Parser

source code


Parser for TLV and LV byte-based encodings.

Parser that can handle arbitrary byte-based encodings usually employed in Type-Length-Value or Length-Value binary encoding protocols like ASN.1 or TLS

Note: if the raw bytes don't match expected values (like trying to read a 4-byte integer from a 2-byte buffer), most methods will raise a SyntaxError exception.

TODO: don't use an exception used by language parser to indicate errors in application code.

Instance Methods [hide private]
 
__init__(self, bytes)
Bind raw bytes with parser.
source code
 
atLengthCheck(self)
Check if there is data in structure left for parsing.
source code
int
get(self, length)
Read a single big-endian integer value encoded in 'length' bytes.
source code
bytearray
getFixBytes(self, lengthBytes)
Read a string of bytes encoded in 'lengthBytes' bytes.
source code
list of int
getFixList(self, length, lengthList)
Read a list of static length with same-sized ints.
source code
 
getRemainingLength(self)
Return amount of data remaining in struct being parsed.
source code
bytearray
getVarBytes(self, lengthLength)
Read a variable length string with a fixed length.
source code
list of int
getVarList(self, length, lengthLength)
Read a variable length list of same-sized integers.
source code
list of tuple of int
getVarTupleList(self, elemLength, elemNum, lengthLength)
Read a variable length list of same sized tuples.
source code
 
setLengthCheck(self, length)
Set length of struct and start a length check for parsing.
source code
 
startLengthCheck(self, lengthLength)
Read length of struct and start a length check for parsing.
source code
 
stopLengthCheck(self)
Stop struct parsing, verify that no under- or overflow occurred.
source code
Instance Variables [hide private]
bytearray bytes
data to be interpreted (buffer)
int index
current position in the buffer
int indexCheck
position at which the structure begins in buffer
int lengthCheck
size of struct being parsed
Method Details [hide private]

__init__(self, bytes)
(Constructor)

source code 

Bind raw bytes with parser.

Parameters:
  • bytes (bytearray) - bytes to be parsed/interpreted
Overrides: object.__init__

atLengthCheck(self)

source code 

Check if there is data in structure left for parsing.

Returns True if the whole structure was parsed, False if there is some data left.

Will raise an exception if overflow occured (amount of data read was greater than expected size)

get(self, length)

source code 

Read a single big-endian integer value encoded in 'length' bytes.

Parameters:
  • length (int) - number of bytes in which the value is encoded in
Returns: int

getFixBytes(self, lengthBytes)

source code 

Read a string of bytes encoded in 'lengthBytes' bytes.

Parameters:
  • lengthBytes (int) - number of bytes to return
Returns: bytearray

getFixList(self, length, lengthList)

source code 

Read a list of static length with same-sized ints.

Parameters:
  • length (int) - size in bytes of a single element in list
  • lengthList (int) - number of elements in list
Returns: list of int

getVarBytes(self, lengthLength)

source code 

Read a variable length string with a fixed length.

Parameters:
  • lengthLength (int) - number of bytes in which the length of the string is encoded in
Returns: bytearray

getVarList(self, length, lengthLength)

source code 

Read a variable length list of same-sized integers.

Parameters:
  • length (int) - size in bytes of a single element
  • lengthLength (int) - size of the encoded length of the list
Returns: list of int

getVarTupleList(self, elemLength, elemNum, lengthLength)

source code 

Read a variable length list of same sized tuples.

Parameters:
  • elemLength (int) - length in bytes of single tuple element
  • elemNum (int) - number of elements in tuple
  • lengthLength (int) - length in bytes of the list length variable
Returns: list of tuple of int

setLengthCheck(self, length)

source code 

Set length of struct and start a length check for parsing.

Parameters:
  • length (int) - expected size of parsed struct in bytes

startLengthCheck(self, lengthLength)

source code 

Read length of struct and start a length check for parsing.

Parameters:
  • lengthLength (int) - number of bytes in which the length is encoded

stopLengthCheck(self)

source code 

Stop struct parsing, verify that no under- or overflow occurred.

In case the expected length was mismatched with actual length of processed data, raises an exception.