MCPcopy Create free account
hub / github.com/secdev/scapy / huffman_decode

Method huffman_decode

scapy/contrib/http2.py:1045–1100  ·  view source on GitHub ↗

huffman_decode decodes the bitstring provided as parameters. :param int i: the bitstring to decode :param int ibl: the bitlength of i :return: str: the string decoded from the bitstring :raises: AssertionError, InvalidEncodingException

(cls, i, ibl)

Source from the content-addressed store, hash-verified

1043
1044 @classmethod
1045 def huffman_decode(cls, i, ibl):
1046 # type: (int, int) -> str
1047 """ huffman_decode decodes the bitstring provided as parameters.
1048
1049 :param int i: the bitstring to decode
1050 :param int ibl: the bitlength of i
1051 :return: str: the string decoded from the bitstring
1052 :raises: AssertionError, InvalidEncodingException
1053 """
1054 assert i >= 0
1055 assert ibl >= 0
1056
1057 if isinstance(cls.static_huffman_tree, type(None)):
1058 cls.huffman_compute_decode_tree()
1059 assert not isinstance(cls.static_huffman_tree, type(None))
1060
1061 s = []
1062 j = 0
1063 interrupted = False
1064 cur = cls.static_huffman_tree
1065 cur_sym = 0
1066 cur_sym_bl = 0
1067 while j < ibl:
1068 b = (i >> (ibl - j - 1)) & 1
1069 cur_sym = (cur_sym << 1) + b
1070 cur_sym_bl += 1
1071 elmt = cur[b]
1072
1073 if isinstance(elmt, HuffmanNode):
1074 interrupted = True
1075 cur = elmt
1076 if isinstance(cur, type(None)):
1077 raise AssertionError()
1078 elif isinstance(elmt, EOS):
1079 raise InvalidEncodingException('Huffman decoder met the full EOS symbol') # noqa: E501
1080 elif isinstance(elmt, bytes):
1081 interrupted = False
1082 s.append(elmt)
1083 cur = cls.static_huffman_tree
1084 cur_sym = 0
1085 cur_sym_bl = 0
1086 else:
1087 raise InvalidEncodingException('Should never happen, so incidentally it will') # noqa: E501
1088 j += 1
1089
1090 if interrupted:
1091 # Interrupted values true if the bitstring ends in the middle of a
1092 # symbol; this symbol must be, according to RFC7541 par5.2 the MSB
1093 # of the EOS symbol
1094 if cur_sym_bl > 7:
1095 raise InvalidEncodingException('Huffman decoder is detecting padding longer than 7 bits') # noqa: E501
1096 eos_symbol = cls.static_huffman_code[-1]
1097 eos_msb = eos_symbol[0] >> (eos_symbol[1] - cur_sym_bl)
1098 if eos_msb != cur_sym:
1099 raise InvalidEncodingException('Huffman decoder is detecting unexpected padding format') # noqa: E501
1100 return b''.join(s)
1101
1102 @classmethod

Callers 1

_parseMethod · 0.80

Calls 4

joinMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected