o
    j;)                     @   sX   d Z ddlZddlmZmZ ddl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)logger_warning)LimitReachedErrorc                   @   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
   [/var/www/html/fyndo/pharma/fyndo/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edd	fd
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  	      hxmax_output_lengthr   Nc                 C   s
   || _ d S )N)r   )r   r   r
   r
   r   __init__5   s   
zLzwCodec.__init__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>:   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_table8   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   I   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/   r0   r%   r-   r   )r   r6   buffer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>   s    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   |  zG| j | jk r(| jd> || j B | _|  jd7  _|  j d7  _ | j | jk s	| j| j | j ? | j| jd  @ }|  j | j8  _ | jd@ | _|W S  tyU   | j Y S w )Nr7   r"   r   i )
_next_bitsrA   
_next_data_byte_pointer
_and_table
IndexErrorr%   )r   r   r=   r
   r
   r   _next_code_decode   s$   
zLzwCodec._next_code_decodec                 C   sp  g d| _ d| _d| _d| _d| _d| _t }d}|   d| _d| _d| _| j	}	 | 
|}|| jkr:	 | S || j	kr_|   | 
|}|| jkrR	 | S || j|  } |}nB|| jk r| j| }|| || j	kr~| | j| |d  |}n | j| | j| dd  }|| | | j| |d  |}|t|7 }|| jkrtd| d| j 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"   z#Limit reached while decompressing: z > )rF   r@   rA   rE   rD   rC   ioBytesIOrB   r0   rH   r%   writer?   _add_entry_decodelenr   r   getvalue)r   r   output_streamoutput_lengthold_coder=   decodedr
   r
   r   r      sV   











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"   rI   
   rJ      rK   r   )r   r@   r)   r   r   r?   rA   )r   rV   rW   
new_stringr
   r
   r   rO     s   






zLzwCodec._add_entry_decode)r   )r   N)r   r   r   r   r0   r%   r'   r,   intr   r+   r-   r   r   listr1   rB   rH   r   rO   r
   r
   r
   r   r   -   s    


*
"68r   )r   rL   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r   r
   r
   r
   r   <module>   s    