a
    ˜ëjØ)  ã                   @   s\   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 G dd„ deƒZ
G dd	„ d	e
ƒZdS )
z™
This module is for codecs only.

While the codec implementation can contain details of the PDF specification,
the module should not do any PDF parsing.
é    N)ÚABCÚabstractmethod)Úlogger_warning)ÚLimitReachedErrorÚPdfStreamErrorc                   @   s8   e Zd ZdZeeedœdd„ƒZeeedœdd„ƒZdS )ÚCodecz#Abstract base class for all codecs.©ÚdataÚreturnc                 C   s   dS )z†
        Encode the input data.

        Args:
            data: Data to encode.

        Returns:
            Encoded data.

        N© ©Úselfr	   r   r   úT/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/_codecs/_codecs.pyÚencode   s    zCodec.encodec                 C   s   dS )z†
        Decode the input data.

        Args:
            data: Data to decode.

        Returns:
            Decoded data.

        Nr   r   r   r   r   Údecode   s    zCodec.decodeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úbytesr   r   r   r   r   r   r      s
   r   c                   @   s²   e Zd ZdZdZdZdZdZdeddœd	d
„Z	ddœdd„Z
ddœdd„Zeedœdd„Zee edœdd„Zddœdd„Zeedœdd„Zeedœdd„Zeeddœdd„ZdS ) ÚLzwCodecz2Lempel-Ziv-Welch (LZW) adaptive compression codec.é   i  é	   é   éÀhxN)Úmax_output_lengthr
   c                 C   s
   || _ d S )N)r   )r   r   r   r   r   Ú__init__5   s    zLzwCodec.__init__)r
   c                 C   s<   dd„ t dƒD ƒ| _| jd | _| j| _d| j> d | _dS )z>Initialize the encoding table and state to initial conditions.c                 S   s   i | ]}t |gƒ|“qS r   ©r   ©Ú.0Úir   r   r   Ú
<dictcomp>:   ó    z7LzwCodec._initialize_encoding_table.<locals>.<dictcomp>r   é   N)ÚrangeÚencoding_tableÚ
EOD_MARKERÚ	next_codeÚINITIAL_BITS_PER_CODEÚbits_per_codeÚmax_code_value©r   r   r   r   Ú_initialize_encoding_table8   s    z#LzwCodec._initialize_encoding_tablec                 C   sH   |  j d7  _ | j | jkrD| j| jk rD|  jd7  _d| j> d | _dS )z5Update bits_per_code and max_code_value if necessary.r#   N)r'   r*   r)   ÚMAX_BITS_PER_CODEr+   r   r   r   Ú_increase_next_code?   s    
ÿ
þzLzwCodec._increase_next_coder   c                 C   sÆ   g }|  | j¡ |  ¡  d}|D ]z}|t|gƒ }|| jv rB|}q |  | j| ¡ | jd| j> d kr|| j| j|< |  ¡  n|  | j¡ |  ¡  t|gƒ}q |r°|  | j| ¡ |  | j¡ |  	|¡S )zˆ
        Encode data using the LZW compression algorithm.

        Taken from PDF 1.7 specs, "7.4.4.2 Details of LZW Encoding".
        r"   r#   )
ÚappendÚCLEAR_TABLE_MARKERr,   r   r%   r'   r-   r.   r&   Ú_pack_codes_into_bytes)r   r	   Zresult_codesZcurrent_sequenceÚbyteZnext_sequencer   r   r   r   I   s&    

zLzwCodec.encode)Úcodesr
   c                 C   s¬   |   ¡  d}d}tƒ }|D ]j}|| j> |B }|| j7 }|dkrZ|d8 }| ||? d@ ¡ q6|| jkrn|   ¡  q|| jkr|qq|  ¡  q|dkr¤| |d| > d@ ¡ t|ƒS )z¼
        Convert the list of result codes into a continuous byte stream, with codes packed as per the code bit-width.
        The bit-width starts at 9 bits and expands as needed.
        r   é   éÿ   )r,   Ú	bytearrayr)   r/   r0   r&   r.   r   )r   r3   ÚbufferZbits_in_bufferÚoutputÚcoder   r   r   r1   s   s$    




zLzwCodec._pack_codes_into_bytesc                 C   sR   d| j > d | _dd„ t| jƒD ƒdg| j| j d   | _| jd | _d| _d S )Nr#   c                 S   s   g | ]}t |gƒ‘qS r   r   r   r   r   r   Ú
<listcomp>—   r"   z7LzwCodec._initialize_decoding_table.<locals>.<listcomp>r"   r   )r-   r*   r$   r0   Údecoding_tabler&   Ú_table_indexÚ_bits_to_getr+   r   r   r   Ú_initialize_decoding_table•   s    ÿþz#LzwCodec._initialize_decoding_tablec                 C   s¤   |  z‚| j | jk rF| jd> || j B | _|  jd7  _|  j d7  _ q| j| j | j ? | j| jd  @ }|  j | j8  _ | jd@ | _W n tyž   | j Y S 0 |S )Nr4   r#   r   iÿÿ )Ú
_next_bitsr=   Ú
_next_dataÚ_byte_pointerÚ
_and_tableÚ
IndexErrorr&   )r   r	   r9   r   r   r   Ú_next_code_decode�   s     ÿþzLzwCodec._next_code_decodec                 C   s€  g d¢| _ d| _d| _d| _d| _d| _t ¡ }d}|  ¡  d| _d| _d| _| j	}|  
|¡}|| jkrl�qx|| j	kr°|  ¡  |  
|¡}|| jkr–�qx| | j|  }¡ |}nš|| jk rô| j| }| |¡ || j	krî|  | j| |d ¡ |}nV| j| }|�std|› d| j› d�ƒ‚||dd…  }| |¡ |  ||d ¡ |}|t|ƒ7 }|| jkrTtd	|› d
| j› �ƒ‚qT| ¡ S )zÎ
        The following code was converted to Python from the following code:
        https://github.com/empira/PDFsharp/blob/master/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Filters/LzwDecode.cs
        )éÿ  éÿ  éÿ  iÿ  r   r   z	LZW code z- out of range with empty base at table index Ú.Nr#   z#Limit reached while decompressing: z > )rB   r<   r=   rA   r@   r?   ÚioÚBytesIOr>   r0   rD   r&   Úwriter;   Ú_add_entry_decoder   Úlenr   r   Úgetvalue)r   r	   Zoutput_streamZoutput_lengthZold_coder9   ÚdecodedÚbaser   r   r   r   Ò   sX    










ÿ

ÿzLzwCodec.decode)Ú
old_stringÚnew_charr
   c                 C   s|   |t |gƒ }| j| jkr*tdtd� d S || j| j< |  jd7  _| jdkrVd| _n"| jdkrhd| _n| jdkrxd	| _d S )
Nz#Ignoring too large LZW table index.)Úsourcer#   rE   é
   rF   é   rG   r   )r   r<   r*   r   r   r;   r=   )r   rQ   rR   Z
new_stringr   r   r   rL     s    


zLzwCodec._add_entry_decode)r   )r   r   r   r   r0   r&   r(   r-   Úintr   r,   r.   r   r   Úlistr1   r>   rD   r   rL   r   r   r   r   r   -   s   
*"5;r   )r   rI   Úabcr   r   Zpypdf._utilsr   Zpypdf.errorsr   r   r   r   r   r   r   r   Ú<module>   s   