o
    li%                     @   sP   d Z ddlZddlmZ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)DictListc                   @   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
   T/var/www/html/pca-backend/venv/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>5   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_table3   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   result_codescurrent_sequencebytenext_sequencer
   r
   r   r   D   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   r3   bufferbits_in_bufferoutputcoder
   r
   r   r.   n   s&   




zLzwCodec._pack_codes_into_bytesc                 C   s<   dd t | jD dgd| j   | _| jd | _d| _d S )Nc                 S   s   g | ]}t |gqS r
   r   r   r
   r
   r   
<listcomp>   s    z7LzwCodec._initialize_decoding_table.<locals>.<listcomp>r+   i   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 )Nr4   r5   r   r   )
_next_bitsr>   
_next_data_byte_pointer
_and_table
IndexErrorr"   )r   r   r:   r
   r
   r   _next_code_decode   s"   
zLzwCodec._next_code_decodec                 C   s8  g d| _ d| _d| _d| _d| _d| _t }|   d| _d| _d| _| j	}	 | 
|}|| jkr4nb|| j	krS|   | 
|}|| jkrHnN|| j|  |}nB|| jk ru| j| }|| || j	krr| | j| |d  |}n | j| | j| dd  }|| | | j| |d  |}q)| }|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   TNr   )rC   r=   r>   rB   rA   r@   ioBytesIOr?   r-   rE   r"   writer<   _add_entry_decodegetvalue)r   r   output_streamold_coder:   stringr9   r
   r
   r   r      sJ   










zLzwCodec.decode
old_stringnew_charc                 C   sh   |t |g }|| j| j< |  jd7  _| jdkrd| _d S | jdkr(d| _d S | jdkr2d| _d S d S )Nr   rF   
   rG      rH   r   )r   r<   r=   r>   )r   rQ   rR   
new_stringr
   r
   r   rL      s   





zLzwCodec._add_entry_decode)r   N)r   r   r   r   r-   r"   r$   r)   r(   r*   r   r   r   intr.   r?   rE   r   rL   r
   r
   r
   r   r   +   s    


*
"22r   )
r   rI   abcr   r   typingr   r   r   r   r
   r
   r
   r   <module>   s    