o
     ªZhÖ&  ã                   @   s\   d Z ddlZddlmZmZ ddlmZmZ ddl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)ÚDictÚList)Úlogger_warningc                   @   s<   e Zd ZdZededefdd„ƒZededefdd„ƒZdS )	ÚCodecz#Abstract base class for all codecs.ÚdataÚreturnc                 C   ó   dS )z†
        Encode the input data.

        Args:
            data: Data to encode.

        Returns:
            Encoded data.

        N© ©Úselfr   r   r   úL/var/www/html/lang_env/lib/python3.10/site-packages/pypdf/_codecs/_codecs.pyÚencode   ó    zCodec.encodec                 C   r
   )z†
        Decode the input data.

        Args:
            data: Data to decode.

        Returns:
            Decoded data.

        Nr   r   r   r   r   Údecode   r   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dd	„Zdd
d„Z	de
de
fdd„Zdee de
fdd„Zddd„Zde
defdd„Zde
de
fdd„Zde
deddfdd„ZdS )ÚLzwCodecz2Lempel-Ziv-Welch (LZW) adaptive compression codec.é   i  é	   é   r	   Nc                 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>7   s    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_table5   s   z#LzwCodec._initialize_encoding_tablec                 C   sP   |  j d7  _ | j | jkr$| j| jk r&|  jd7  _d| j> d | _dS dS 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 ]=}|t|gƒ }|| jv r!|}q|  | j| ¡ | jd| j> d kr>| j| j|< |  ¡  n
|  | j¡ |  ¡  t|gƒ}q|rX|  | 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    )
Ú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   F   s&   


zLzwCodec.encodeÚcodesc                 C   s°   |   ¡  d}d}tƒ }|D ]7}|| j> |B }|| j7 }|dkr0|d8 }| ||? d@ ¡ |dks|| jkr:|   ¡  q|| jkr@q|  ¡  q|dkrT| |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-   r.   r#   r+   r   )r   r1   ÚbufferZbits_in_bufferÚoutputÚcoder   r   r   r/   p   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>”   s    z7LzwCodec._initialize_decoding_table.<locals>.<listcomp>r,   r   )r*   r'   r!   r.   Údecoding_tabler#   Ú_table_indexÚ_bits_to_getr(   r   r   r   Ú_initialize_decoding_table’   s   ÿþ
z#LzwCodec._initialize_decoding_tablec                 C   s¤   |  zC| j | jk r*| jd> || j d@ B | _|  jd7  _|  j d7  _ | j | jk s	| j| j | j ? | j| jd  @ }|  j | j8  _ |W S  tyQ   | j Y S w )Nr2   r3   r    r   )Ú
_next_bitsr;   Ú
_next_dataÚ_byte_pointerÚ
_and_tableÚ
IndexErrorr#   )r   r   r7   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| _| j	}	 |  
|¡}|| jkr8	 | ¡ S || j	kr[|  ¡  |  
|¡}|| jkrP	 | ¡ S | | j| ¡ |}nB|| jk r}| j| }| |¡ || j	krz|  | j| |d ¡ |}n | j| | j| dd…  }| |¡ |  | j| |d ¡ |}q))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   TNr    )r@   r:   r;   r?   r>   r=   ÚioÚBytesIOr<   r.   rB   r#   Úwriter9   Ú_add_entry_decodeÚgetvalue)r   r   Zoutput_streamZold_coder7   Ústringr   r   r   r   Ì   sJ   



ê

ï



ÿ
çzLzwCodec.decodeÚ
old_stringÚnew_charc                 C   s‚   |t |gƒ }| j| jkrtdtƒ d S || j| j< |  jd7  _| jdkr+d| _d S | jdkr5d| _d S | jdkr?d| _d S d S )	Nz#Ignoring too large LZW table index.r    rC   é
   rD   é   rE   r   )r   r:   r'   r   r   r9   r;   )r   rL   rM   Z
new_stringr   r   r   rI   ý   s   






ÿzLzwCodec._add_entry_decode)r	   N)r   r   r   r   r.   r#   r%   r*   r)   r+   r   r   r   Úintr/   r<   rB   r   rI   r   r   r   r   r   -   s    


*
"21r   )r   rF   Úabcr   r   Útypingr   r   Zpypdf._utilsr   r   r   r   r   r   r   Ú<module>   s    