ocebuild.parsers.types#

Type parsing helper functions.

Module Contents#

Functions#

encode_data(→ bytes)

Encodes a base64 or hexadecimal string to a binary representation.

decode_data(→ str)

Decodes a binary representation to a base64 string.

Attributes#

RE_VALID_BASE64

Regular expression to match a valid base64 string.

RE_VALID_HEX

Regular expression to match a valid hexadecimal string.

ocebuild.parsers.types.RE_VALID_BASE64 = '^([a-zA-Z0-9+/]{4})*([a-zA-Z0-9+/]{3}=|[a-zA-Z0-9+/]{2}==)?$'[source]#

Regular expression to match a valid base64 string.

ocebuild.parsers.types.RE_VALID_HEX = '^([a-fA-F0-9]{2})*$'[source]#

Regular expression to match a valid hexadecimal string.

ocebuild.parsers.types.encode_data(value: str) bytes[source]#

Encodes a base64 or hexadecimal string to a binary representation.

Parameters:

value – The base64 or hexadecimal string to encode.

Raises:

ValueError – If the string is not a valid base64 or hexadecimal string.

Returns:

A binary representation of the string.

Examples

>>> encode_data('<01 00 00 00>')
b'\x01\x00\x00\x00'
>>> encode_data('01000000')
b'\x01\x00\x00\x00'
>>> encode_data('AQAAAA==')
b'\x01\x00\x00\x00'
ocebuild.parsers.types.decode_data(value: bytes, enc: str = 'base64') str[source]#

Decodes a binary representation to a base64 string.

Parameters:
  • value – The binary representation to decode.

  • enc – The encoding format to return. Valid values are ‘base64’ and ‘hex’.

Raises:

ValueError – If the format is not a valid format.

Returns:

A base64 or hex string representation of the binary data.