
    Ng%                     j    d Z ddlZddlmZmZ ddlmZ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)DictListc                   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     Q/var/www/html/ai-engine/env/lib/python3.11/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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  	      r
   Nc                     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>5   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_table3   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*   r-   r,   MAX_BITS_PER_CODEr.   s    r   _increase_next_codezLzwCodec._increase_next_code:   sp    !NT000"T%;;;!##$(:#:a"?D	 10;;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_MARKERr/   r   r(   r*   r1   r2   r)   _pack_codes_into_bytes)r   r	   result_codescurrent_sequencebytenext_sequences         r   r   zLzwCodec.encodeD   sa    #% 	D3444''))) 	1 	1D,udV}}<M 333#0   ##D$78H$IJJJ >a4+A&AQ%FFF9=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         )r/   	bytearrayr,   r4   r5   r)   r2   r   )r   r;   bufferbits_in_bufferoutputcodes         r   r6   zLzwCodec._pack_codes_into_bytesn   s   
 	''))) 	+ 	+D 22d:Fd00N
 !A%%!#v74?@@@ !A%% t...//1111((((**** AMM6a.&89TABBBV}}r   c                     d t          | j                  D             dgd| j        z
  z  z   | _        | j        dz   | _        d| _        d S )Nc                 .    g | ]}t          |g          S r   r!   r"   s     r   
<listcomp>z7LzwCodec._initialize_decoding_table.<locals>.<listcomp>   s     RRRauaSzzRRRr   r   i   r&   r   )r'   r5   decoding_tabler)   _table_index_bits_to_getr.   s    r   _initialize_decoding_tablez#LzwCodec._initialize_decoding_table   sa    RR59P3Q3QRRRV
D++V- - !Oa/r   c                 ~   |  	 | j         | j        k     rP| j        dz  || j                 dz  z  | _        | xj        dz  c_        | xj         dz  c_         | j         | j        k     P| j        | j         | j        z
  z	  | j        | j        dz
           z  }| xj         | j        z  c_         |S # t
          $ r
 | j        cY S w xY w)Nr=   r>   r&   r   )
_next_bitsrI   
_next_data_byte_pointer
_and_table
IndexErrorr)   )r   r	   rC   s      r   _next_code_decodezLzwCodec._next_code_decode   s    	#/D$555#'?a#7+,t3# ""a'""1$ /D$555 DOd6G$GH 1A 567D OOt00OOK 	# 	# 	#?"""	#s   B#B( (B<;B<c                    g d| _         d| _        d| _        d| _        d| _        d| _        t          j                    }|                                  d| _        d| _        d| _        | j	        }	 | 
                    |          }|| j        k    rn(|| j	        k    rX|                                  | 
                    |          }|| j        k    rn|                    | j        |                    |}n|| j        k     rW| j        |         }|                    |           || j	        k    r'|                     | j        |         |d                    |}na| j        |         | j        |         dd         z   }|                    |           |                     | j        |         |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&   )rO   rH   rI   rN   rM   rL   ioBytesIOrJ   r5   rQ   r)   writerG   _add_entry_decodegetvalue)r   r	   output_streamold_coderC   stringrB   s          r   r   zLzwCodec.decode   s   
 211
'')))*	 ))$//Dt&&t...//111--d334?**##D$7$=>>>))),T2##F+++t666**4+>x+H&QR)TTT '1D4G4QRTSTRT4UU  ##F+++&&t':8'DfQiPPP3	 6 ''))r   
old_stringnew_charc                     |t          |g          z   }|| 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 )Nr&   rS   
   rT      rU   r   )r   rG   rH   rI   )r   r^   r_   
new_strings       r   rY   zLzwCodec._add_entry_decode   s    %
"3"33
1;D-.Q ## "D$&& "D$&& "D '&r   )r
   N)r   r   r   r   r5   r)   r+   r1   r/   r2   r   r   r   intr6   rJ   rQ   r   rY   r   r   r   r   r   +   s*       <<J< < < <@ @ @ @(95 (9U (9 (9 (9 (9T DI  %        D   #e # # # # #d05 0U 0 0 0 0d#E #S #T # # # # # #r   r   )
r   rV   abcr   r   typingr   r   r   r   r   r   r   <module>rg      s     
			 # # # # # # # #            C   <[# [# [# [# [#u [# [# [# [# [#r   