
    є j;)                     r    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e          Z	 G 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                   R    e Zd ZdZededefd            Zededefd            ZdS )Codecz#Abstract base class for all codecs.datareturnc                     dS )z
        Encode the input data.

        Args:
            data: Data to encode.

        Returns:
            Encoded data.

        N selfr	   s     iC:\Users\Terasoftware\OneDrive\Desktop\faahhh\fyndo\fyndo\venv\Lib\site-packages\pypdf/_codecs/_codecs.pyencodezCodec.encode             c                     dS )z
        Decode the input data.

        Args:
            data: Data to decode.

        Returns:
            Decoded data.

        Nr   r   s     r   decodezCodec.decode   r   r   N)__name__
__module____qualname____doc__r   bytesr   r   r   r   r   r   r      st        --
5 
U 
 
 
 ^
 
5 
U 
 
 
 ^
 
 
r   r   c                       e Zd ZdZdZdZdZdZddedd	fd
Z	ddZ
ddZdedefdZdee         defdZddZdedefdZdedefdZdededd	fdZd	S )LzwCodecz2Lempel-Ziv-Welch (LZW) adaptive compression codec.   i  	      hxmax_output_lengthr
   Nc                     || _         d S )N)r    )r   r    s     r   __init__zLzwCodec.__init__5   s    !2r   c                     d t          d          D             | _        | j        dz   | _        | j        | _        d| j        z  dz
  | _        dS )z>Initialize the encoding table and state to initial conditions.c                 0    i | ]}t          |g          |S r   r   .0is     r   
<dictcomp>z7LzwCodec._initialize_encoding_table.<locals>.<dictcomp>:   s"    0S0S0S1sQ0S0S0Sr   r      N)rangeencoding_table
EOD_MARKER	next_codeINITIAL_BITS_PER_CODEbits_per_codemax_code_valuer   s    r   _initialize_encoding_tablez#LzwCodec._initialize_encoding_table8   sR    0S0Sc

0S0S0S1,!7 D$66!;r   c                     | xj         dz  c_         | j         | j        k    r4| j        | j        k     r&| xj        dz  c_        d| j        z  dz
  | _        dS dS dS )z5Update bits_per_code and max_code_value if necessary.r*   N)r.   r1   r0   MAX_BITS_PER_CODEr2   s    r   _increase_next_codezLzwCodec._increase_next_code?   s    !NT00	@"T%;;	@ !##$(:#:a"?D	@ 	@ 	@ 	@r   r	   c                    g }|                     | j                   |                                  d}|D ]}|t          |g          z   }|| j        v r|}!|                     | j        |                    | j        d| j        z  dz
  k    r$| j        | j        |<   |                                  n.|                     | j                   |                                  t          |g          }|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_MARKERr3   r   r,   r.   r5   r6   r-   _pack_codes_into_bytes)r   r	   result_codescurrent_sequencebytenext_sequences         r   r   zLzwCodec.encodeI   se    #% 	D3444''))) 	1 	1D,udV}}<M 33 1#0   ##D$78H$IJJJ >a4+A&AQ%FF 69=D'6,,.... !''(?@@@33555 $)$==    	G 34D EFFFDO,,,**<888r   codesc                    |                                   d}d}t                      }|D ]}|| j        z  |z  }|| j        z  }|dk    r&|dz  }|                    ||z	  dz             |dk    &|| j        k    r|                                   e|| j        k    rq|                                  |dk    r|                    |d|z
  z  dz             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         )r3   	bytearrayr0   r8   r9   r-   r6   r   )r   r?   bufferbits_in_bufferoutputcodes         r   r:   zLzwCodec._pack_codes_into_bytess   s(   
 	''))) 	+ 	+D 22d:Fd00N
 !A% A!#v74?@@@ !A% A t.. +//1111( +((**** A 	CMM6a.&89TABBBV}}r   c                     d| j         z  dz
  | _        d t          | j                  D             dg| j        | j        z
  dz   z  z   | _        | j        dz   | _        d| _        d S )Nr*   c                 .    g | ]}t          |g          S r   r%   r&   s     r   
<listcomp>z7LzwCodec._initialize_decoding_table.<locals>.<listcomp>   s     RRRauaSzzRRRr   r   r   )r5   r1   r+   r9   decoding_tabler-   _table_index_bits_to_getr2   s    r   _initialize_decoding_tablez#LzwCodec._initialize_decoding_table   s     D$::a?RR59P3Q3QRRRV
 4#::Q>V@ @ !Oa/r   c                    |  	 | j         | j        k     rM| j        dz  || j                 z  | _        | xj        dz  c_        | xj         dz  c_         | j         | j        k     M| j        | j         | j        z
  z	  | j        | j        dz
           z  }| xj         | j        z  c_         | j        dz  | _        |S # t
          $ r
 | j        cY S w xY w)NrA   r*   r   i )
_next_bitsrM   
_next_data_byte_pointer
_and_table
IndexErrorr-   )r   r	   rG   s      r   _next_code_decodezLzwCodec._next_code_decode   s    	#/D$55 %#'?a#7+,# ""a'""1$ /D$55 % DOd6G$GH 1A 567D OOt00OO #o7DOK 	# 	# 	#?"""	#s   B/B4 4CCc                    g d| _         d| _        d| _        d| _        d| _        d| _        t          j                    }d}|                                  d| _        d| _        d| _        | j	        }	 | 
                    |          }|| j        k    rnb|| j	        k    r[|                                  | 
                    |          }|| j        k    rn!|                    | j        |         x}           |}n|| j        k     rW| j        |         }|                    |           || j	        k    r'|                     | j        |         |d                    |}na| j        |         | j        |         dd         z   }|                    |           |                     | j        |         |d                    |}|t          |          z  }|| j        k    rt#          d| d| j                   |                                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*   z#Limit reached while decompressing: z > )rS   rL   rM   rR   rQ   rP   ioBytesIOrN   r9   rU   r-   writerK   _add_entry_decodelenr    r   getvalue)r   r	   output_streamoutput_lengthold_coderG   decodeds          r   r   zLzwCodec.decode   sF   
 211
'')))*	))$//Dt& t..  //111--d334?* ##t/B4/H$HGIII))  -d3##G,,,t66 V**4+>x+H'RS*UUU '1D4G4QRTSTRT4UU  ##G,,,&&t':8'DgajQQQS\\)Mt55 'd-ddDLbdd  ;	B %%'''r   
old_stringnew_charc                 0   |t          |g          z   }| j        | j        k    rt          dt                     d S || j        | j        <   | xj        dz  c_        | j        dk    r	d| _        d S | j        dk    r	d| _        d S | j        dk    r	d| _        d S d S )	Nz#Ignoring too large LZW table index.r*   rW   
   rX      rY   r   )r   rL   r1   r   r   rK   rM   )r   rd   re   
new_strings       r   r]   zLzwCodec._add_entry_decode  s    %
"3"33
t22 	@(KKKF1;D-.Q # 	# "D$& 	# "D$& 	# "D	# 	#r   )r   )r
   N)r   r   r   r   r9   r-   r/   r5   intr"   r3   r6   r   r   listr:   rN   rU   r   r]   r   r   r   r   r   -   sQ       <<J3 3# 3t 3 3 3 3< < < <@ @ @ @(95 (9U (9 (9 (9 (9T DI  %        D   #e # # # # #l6(5 6(U 6( 6( 6( 6(p#E #S #T # # # # # #r   r   )r   rZ   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r   r   r   r   <module>ro      s     
			 # # # # # # # # ' ' ' ' ' ' * * * * * *    C   <l# l# l# l# l#u l# l# l# l# l#r   