
    j:j)                     h    S r SSKrSSKJrJr  SSKJr  SSKJr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)logger_warning)LimitReachedErrorPdfStreamErrorc                   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     K/var/www/website/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\	SS	4S
 jj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  	      max_output_lengthr   Nc                     Xl         g )N)r&   )r   r&   s     r   __init__LzwCodec.__init__5   s    !2r   c                     [        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_table8   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/   r2   r1   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_MARKERr5   r   r-   r/   r8   r9   r.   _pack_codes_into_bytes)r   r   result_codescurrent_sequencebytenext_sequences         r   r   LzwCodec.encodeI   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         )r5   	bytearrayr1   r<   r=   r.   r9   r   )r   rD   bufferbits_in_bufferoutputcodes         r   r>   LzwCodec._pack_codes_into_bytess   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$   )	r8   r2   r,   r=   r   decoding_tabler.   _table_index_bits_to_getr3   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)NrF   r+   r$   i )
_next_bitsrQ   
_next_data_byte_pointer
_and_table
IndexErrorr.   )r   r   rL   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                 0   / SQU l         SU l        SU l        SU l        SU l        SU l        [        R                  " 5       nSnU R                  5         SU l        SU l        SU l        U R                  n U R                  U5      nXPR                  :X  a   UR'                  5       $ XPR                  :X  ad  U R                  5         U R                  U5      nXPR                  :X  a   UR'                  5       $ UR                  U R                  U   =n5        UnOXPR                  :  aT  U R                  U   nUR                  U5        X@R                  :w  a"  U R                  U R                  U   US   5        UnO`U R                  U   nU(       d  [        SU SU R                   S35      eXwSS -   nUR                  U5        U R                  XvS   5        UnU[!        U5      -  nX0R"                  :  a  [%        S	U S
U R"                   35      e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$   z	LZW code z- out of range with empty base at table index .Nr+   z#Limit reached while decompressing: z > )rX   rP   rQ   rW   rV   rU   ioBytesIOrR   r=   rZ   r.   writerO   _add_entry_decoder   lenr&   r   getvalue)r   r   output_streamoutput_lengthold_coderL   decodedbases           r   r   LzwCodec.decode   s   
 2

'')**))$/D&B %%''? ...//1--d3??*6 %%''5 ##t/B/B4/H$HGI)))--d3##G,666**4+>+>x+H'RS*U **84(#D6)VW[WhWhViijk  bq/##G,&&tQZ8S\)M555'9-DLbLbKcd A r   
old_stringnew_charc                 ^   U[        U/5      -   nU R                  U R                  :  a  [        S[        S9  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.)sourcer+   r]   
   r^      r_   r%   )r   rP   r2   r   r   rO   rQ   )r   rm   rn   
new_strings       r   rd   LzwCodec._add_entry_decode  s    %
"33
t222@R1;D--.Q # "D$& "D$& "D 'r   )rX   rQ   rW   rU   rV   rP   r1   rO   r-   r2   r&   r/   )ihx)r   N)r   r   r   r   r   r=   r.   r0   r8   intr(   r5   r9   r   r   listr>   rR   rZ   r   rd   r   r   r   r   r!   r!   -   s    <J3# 3t 3<@(95 (9U (9T DI  %  D#e # #l9(5 9(U 9(v#E #S #T #r   r!   )r   ra   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r	   r!   r   r   r   <module>rz      s2    
 # ' :C <o#u o#r   