
    ۡi;)                     d    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
y)
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                   D    e Zd ZdZededefd       Zededefd       Zy)Codecz#Abstract base class for all codecs.datareturnc                      y)z
        Encode the input data.

        Args:
            data: Data to encode.

        Returns:
            Encoded data.

        N selfr	   s     _/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pypdf/_codecs/_codecs.pyencodezCodec.encode           c                      y)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      sG    -
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y)LzwCodecz2Lempel-Ziv-Welch (LZW) adaptive compression codec.   i  	      max_output_lengthr
   Nc                     || _         y )N)r   )r   r   s     r   __init__zLzwCodec.__init__5   s
    !2r   c                     t        d      D ci c]  }t        |g      | c}| _        | j                  dz   | _        | j
                  | _        d| j                  z  dz
  | _        yc c}w )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_tablez#LzwCodec._initialize_encoding_table8   s_    HMc
0S1sQ0S1,!77 D$6$66!; 1Ts   A'c                     | xj                   dz  c_         | j                   | j                  kD  rG| j                  | j                  k  r-| xj                  dz  c_        d| j                  z  dz
  | _        yyy)z5Update bits_per_code and max_code_value if necessary.r#   N)r'   r*   r)   MAX_BITS_PER_CODE)r   s    r   _increase_next_codezLzwCodec._increase_next_code?   si    !NNT000""T%;%;;!##$(:(:#:a"?D < 1r   r	   c                    g }|j                  | j                         | j                          d}|D ]  }|t        |g      z   }|| j                  v r|}#|j                  | j                  |          | j
                  d| j                  z  dz
  k  r*| j
                  | j                  |<   | j                          n+|j                  | j                         | j                          t        |g      } |r|j                  | j                  |          |j                  | 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'   r/   r0   r&   _pack_codes_into_bytes)r   r	   result_codescurrent_sequencebytenext_sequences         r   r   zLzwCodec.encodeI   s0    #% 	D334'') 	1D,udV}<M 3 33#0  ##D$7$78H$IJ >>a4+A+A&AQ%FF9=D''6,,. !''(?(?@335 $)$= )	1.  3 34D EFDOO,**<88r   codesc                    | j                          d}d}t               }|D ]  }|| j                  z  |z  }|| j                  z  }|dk\  r"|dz  }|j                  ||z	  dz         |dk\  r"|| j                  k(  r| j                          k|| j
                  k(  r{| j                           |dkD  r|j                  |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)   r2   r3   r&   r0   r   )r   r9   bufferbits_in_bufferoutputcodes         r   r4   zLzwCodec._pack_codes_into_bytess   s    
 	'') 	+D 2 22d:Fd000N
 !A%!#v74?@ !A% t...//1(((*!	+& AMM6a.&89TABV}r   c                    d| j                   z  dz
  | _        t        | j                        D cg c]  }t	        |g       c}dg| j                  | j                  z
  dz   z  z   | _        | j                  dz   | _        d| _        y c c}w )Nr#   r   r   )	r/   r*   r$   r3   r   decoding_tabler&   _table_index_bits_to_getr+   s     r   _initialize_decoding_tablez#LzwCodec._initialize_decoding_table   s     D$:$::a?389P9P3QRauaSzRV
  4#:#::Q>V@ @ !OOa/	 Ss   Bc                 0   |  	 | j                   | j                  k  rh| j                  dz  || j                     z  | _        | xj                  dz  c_        | xj                   dz  c_         | j                   | j                  k  rh| 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)Nr;   r#   r   i )
_next_bitsrE   
_next_data_byte_pointer
_and_table
IndexErrorr&   )r   r	   rA   s      r   _next_code_decodez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                    g d| _         d| _        d| _        d| _        d| _        d| _        t        j                         }d}| j                          d| _        d| _        d| _        | j                  }	 | j                  |      }|| j                  k(  r	 |j%                         S || j                  k(  rd| j                          | j                  |      }|| j                  k(  r	 |j%                         S |j                  | j                  |   x}       |}n|| j                  k  rT| j                  |   }|j                  |       || j                  k7  r"| j                  | j                  |   |d          |}nW| j                  |   | j                  |   dd z   }|j                  |       | j                  | j                  |   |d          |}|t        |      z  }|| j                   kD  rt#        d| d| j                          )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 > )rK   rD   rE   rJ   rI   rH   ioBytesIOrF   r3   rM   r&   writerC   _add_entry_decodelenr   r   getvalue)r   r	   output_streamoutput_lengthold_coderA   decodeds          r   r   zLzwCodec.decode   s   
 2

'')**))$/Dt&< %%''9 t...//1--d34??*0 %%''/ ##t/B/B4/H$HGI)))--d3##G,t666**4+>+>x+H'RS*U ''1D4G4G4QRTST4UU  ##G,&&t':':8'DgajQS\)Mt555'9-DLbLbKcd ; r   
old_stringnew_charc                 \   |t        |g      z   }| j                  | j                  kD  rt        dt               y || j
                  | j                  <   | xj                  dz  c_        | j                  dk(  rd| _        y | j                  dk(  rd| _        y | j                  dk(  rd| _        y y )	Nz#Ignoring too large LZW table index.r#   rO   
   rP      rQ   r   )r   rD   r*   r   r   rC   rE   )r   r\   r]   
new_strings       r   rU   zLzwCodec._add_entry_decode  s    %
"33
t222@(K1;D--.Q # "D$& "D$& "D 'r   )ihx)r
   N)r   r   r   r   r3   r&   r(   r/   intr!   r-   r0   r   r   listr4   rF   rM   r   rU   r   r   r   r   r   -   s    <J3# 3t 3<@(95 (9U (9T DI  %  D#e # #l6(5 6(U 6(p#E #S #T #r   r   )r   rR   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r   r   r   r   <module>rg      s2    
 # ' *C <l#u l#r   