
    i>)                     d    S r SSKrSSKJrJr  SSK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)logger_warning)LimitReachedErrorc                   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     O/home/james-whalen/.local/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.   r1   r0   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_MARKERr4   r   r,   r.   r7   r8   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         )r4   	bytearrayr0   r;   r<   r-   r8   r   )r   rC   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#   )	r7   r1   r+   r<   r   decoding_tabler-   _table_index_bits_to_getr2   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)NrE   r*   r#   i )
_next_bitsrP   
_next_data_byte_pointer
_and_table
IndexErrorr-   )r   r
   rK   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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OWU R                  U   U R                  U   SS -   nUR                  U5        U R                  U R                  U   US   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#   Nr*   z#Limit reached while decompressing: z > )rW   rO   rP   rV   rU   rT   ioBytesIOrQ   r<   rY   r-   writerN   _add_entry_decodelenr%   r   getvalue)r   r
   output_streamoutput_lengthold_coderK   decodeds          r   r   LzwCodec.decode   s   
 2

'')**))$/D&< %%''9 ...//1--d3??*0 %%''/ ##t/B/B4/H$HGI)))--d3##G,666**4+>+>x+H'RS*U ''1D4G4G4QRTST4UU  ##G,&&t':':8'DgajQS\)M555'9-DLbLbKcd ; 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*   r\   
   r]      r^   r$   )r   rO   r1   r   r   rN   rP   )r   rj   rk   
new_strings       r   rb   LzwCodec._add_entry_decode  s    %
"33
t222@(K1;D--.Q # "D$& "D$& "D 'r   )rW   rP   rV   rT   rU   rO   r0   rN   r,   r1   r%   r.   )i ʚ;)r   N)r   r   r   r   r   r<   r-   r/   r7   intr'   r4   r8   r   r   listr=   rQ   rY   r   rb   r   r   r   r   r    r    -   s    <J3# 3$ 3<@(95 (9U (9T DI  %  D#e # #l6(5 6(U 6(p#E #S #T #r   r    )r   r_   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r    r   r   r   <module>rv      s2    
 # ' *C <l#u l#r   