
    HAi'                     h    S r SSKrSSKJrJr  SSKJrJr  SSKJ	r	   " S S\5      r
 " S S	\
5      rg)
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                   P    \ rS rSrSr\S\S\4S j5       r\S\S\4S j5       rSr	g)	Codec   z#Abstract base class for all codecs.datareturnc                     g)zV
Encode the input data.

Args:
    data: Data to encode.

Returns:
    Encoded data.

N selfr   s     Q/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pypdf/_codecs/_codecs.pyencodeCodec.encode           c                     g)zV
Decode the input data.

Args:
    data: Data to decode.

Returns:
    Decoded data.

Nr   r   s     r   decodeCodec.decode   r   r   r   N)
__name__
__module____qualname____firstlineno____doc__r   bytesr   r   __static_attributes__r   r   r   r	   r	      sG    -
5 
U 
 
 
5 
U 
 
r   r	   c                       \ rS rSrSrSrSrSrSrSS	 jr	SS
 jr
S\S\4S jrS\\   S\4S jrSS jrS\S\4S jrS\S\4S jrS\S\SS4S jrSrg)LzwCodec-   z2Lempel-Ziv-Welch (LZW) adaptive compression codec.   i  	      r   Nc                     [        S5       Vs0 s H  n[        U/5      U_M     snU l        U R                  S-   U l        U R
                  U l        SU R                  -  S-
  U l        gs  snf )z>Initialize the encoding table and state to initial conditions.r#      N)ranger   encoding_table
EOD_MARKER	next_codeINITIAL_BITS_PER_CODEbits_per_codemax_code_valuer   is     r   _initialize_encoding_table#LzwCodec._initialize_encoding_table5   sc    HMc
0S
1sQ
0S1,!77 D$6$66!; 1Ts   A*c                     U =R                   S-  sl         U R                   U R                  :  aH  U R                  U R                  :  a-  U =R                  S-  sl        SU R                  -  S-
  U l        ggg)z5Update bits_per_code and max_code_value if necessary.r'   N)r+   r.   r-   MAX_BITS_PER_CODE)r   s    r   _increase_next_codeLzwCodec._increase_next_code<   si    !NNT000""T%;%;;!##$(:(:#:a"?D < 1r   r   c                    / nUR                  U R                  5        U R                  5         SnU H  nU[        U/5      -   nXPR                  ;   a  UnM%  UR                  U R                  U   5        U R
                  SU R                  -  S-
  ::  a*  U R
                  U R                  U'   U R                  5         O+UR                  U R                  5        U R                  5         [        U/5      nM     U(       a  UR                  U R                  U   5        UR                  U R                  5        U R                  U5      $ )zp
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_MARKERr1   r   r)   r+   r4   r5   r*   _pack_codes_into_bytes)r   r   result_codescurrent_sequencebytenext_sequences         r   r   LzwCodec.encodeF   s)    #% 	D334'')D,udV}<M 3 33#0  ##D$7$78H$IJ >>a4+A+A&AQ%FF9=D''6,,. !''(?(?@335 $)$= ) .  3 34D EFDOO,**<88r   codesc                    U R                  5         SnSn[        5       nU H  nX R                  -  U-  nX0R                  -  nUS:  a#  US-  nUR                  X#-	  S-  5        US:  a  M#  XPR                  :X  a  U R                  5         Ml  XPR
                  :X  a  M}  U R                  5         M     US:  a  UR                  USU-
  -  S-  5        [        U5      $ )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         )r1   	bytearrayr-   r8   r9   r*   r5   r   )r   r@   bufferbits_in_bufferoutputcodes         r   r:   LzwCodec._pack_codes_into_bytesp   s    
 	'')D 2 22d:F000N
 !A%!#v74?@ !A% ...//1(((*! & AMM6a.&89TABV}r   c                    SU R                   -  S-
  U l        [        U R                  5       Vs/ s H  n[	        U/5      PM     snS/U R                  U R                  -
  S-   -  -   U l        U R                  S-   U l        SU l        g s  snf )Nr'   r   r$   )	r4   r.   r(   r9   r   decoding_tabler*   _table_index_bits_to_getr/   s     r   _initialize_decoding_table#LzwCodec._initialize_decoding_table   s     D$:$::a?389P9P3QR3QauaSz3QRV
  4#:#::Q>V@ @ !OOa/	 Ss   B
c                 6   U    U R                   U R                  :  ai  U R                  S-  XR                     -  U l        U =R                  S-  sl        U =R                   S-  sl         U R                   U R                  :  a  Mi  U R                  U R                   U R                  -
  -	  U R                  U R                  S-
     -  nU =R                   U R                  -  sl         U R                  S-  U l        U$ ! [
         a    U R                  s $ f = f)NrB   r'   r$   i )
_next_bitsrM   
_next_data_byte_pointer
_and_table
IndexErrorr*   )r   r   rH   s      r   _next_code_decodeLzwCodec._next_code_decode   s    	#//D$5$55#'??a#7++,# ""a'"1$ //D$5$55 DOOd6G6G$GH 1 1A 567D OOt000O #oo7DOK 	#??"	#s   BC? A7C? ?DDc                    / SQU l         SU l        SU l        SU l        SU l        SU l        [        R                  " 5       nU R                  5         SU l        SU l        SU l        U R                  n U R                  U5      nX@R                  :X  a   UR                  5       $ X@R                  :X  ab  U R                  5         U R                  U5      nX@R                  :X  a   UR                  5       $ UR                  U R                  U   5        UnOX@R                  :  aT  U R                  U   nUR                  U5        X0R                  :w  a"  U R                  U R                  U   US   5        UnOWU R                  U   U R                  U   SS -   nUR                  U5        U R                  U R                  U   US   5        UnGM_  )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$   Nr'   )rT   rL   rM   rS   rR   rQ   ioBytesIOrN   r9   rV   r*   writerK   _add_entry_decodegetvalue)r   r   output_streamold_coderH   strings         r   r   LzwCodec.decode   s   
 2

'')**))$/D&0 %%''- ...//1--d3??*$ %%''# ##D$7$7$=>))),,T2##F+666**4+>+>x+H&QR)T ''1D4G4G4QRTST4UU  ##F+&&t':':8'DfQiP3 r   
old_stringnew_charc                 b   U[        U/5      -   nU R                  U R                  :  a  [        S[        5        g X0R
                  U R                  '   U =R                  S-  sl        U R                  S:X  a  SU l        g U R                  S:X  a  SU l        g U R                  S:X  a  SU l        g g )	Nz#Ignoring too large LZW table index.r'   rY   
   rZ      r[   r%   )r   rL   r.   r   r   rK   rM   )r   re   rf   
new_strings       r   r_   LzwCodec._add_entry_decode  s    %
"33
t222@(K1;D--.Q # "D$& "D$& "D 'r   )rT   rM   rS   rQ   rR   rL   r-   rK   r)   r.   r+   )r   N)r   r   r   r   r   r9   r*   r,   r4   r1   r5   r   r   r   intr:   rN   rV   r   r_   r   r   r   r   r!   r!   -   s    <J<@(95 (9U (9T DI  %  D#e # #l/(5 /(U /(b#E #S #T #r   r!   )r   r\   abcr   r   typingr   r   pypdf._utilsr   r	   r!   r   r   r   <module>rp      s2    
 #  'C <b#u b#r   