o
    / hO	  ã                   @  s"   d Z ddlmZ G dd„ dƒZdS )zˆ
An implementation of a bitwise prefix tree specially built for decoding
Huffman-coded content where we already know the Huffman table.
é    )Úannotationsc                   @  s$   e Zd ZdZddd„Zddd„ZdS )ÚHuffmanEncoderzj
    Encodes a string according to the Huffman encoding table defined in the
    HPACK specification.
    Úhuffman_code_listú	list[int]Úhuffman_code_list_lengthsÚreturnÚNonec                 C  s   || _ || _d S )N)r   r   )Úselfr   r   © r
   úF/var/www/html/govbot/env/lib/python3.10/site-packages/hpack/huffman.pyÚ__init__   s   
zHuffmanEncoder.__init__Úbytes_to_encodeúbytes | NoneÚbytesc                 C  sò   |sdS d}d}|D ] }| j | }| j| d|d  d @ }||K }||O }||7 }q
d|d  d }||K }|d|> d O }t|ƒdd…  d¡}t|ƒd dkrVd| n|}|| d }	|	d }
t|ƒ|
krt|
t|ƒ }d| | }t |¡S )	zm
        Given a string of bytes, encodes them according to the HPACK Huffman
        specification.
        ó    r   é   é   é   NÚLÚ0)r   r   ÚhexÚrstripÚlenr   Úfromhex)r	   r   Ú	final_numÚfinal_int_lenÚbyteÚbin_int_lenÚbin_intÚbits_to_be_paddedÚsÚtotal_bytesÚexpected_digitsÚmissing_digitsr
   r
   r   Úencode   s.   
ÿ

zHuffmanEncoder.encodeN)r   r   r   r   r   r   )r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r
   r
   r
   r   r      s    
r   N)r(   Ú
__future__r   r   r
   r
   r
   r   Ú<module>   s    