
    mj)                     h    d Z ddlZddlmZmZ ddlmZ ddlmZ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)LimitReachedErrorPdfStreamErrorc                   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     H/root/.hermes/venv/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(   r0   r1   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*   r3   r4   r'   r1   r   )r   r:   bufferbits_in_bufferoutputcodes         r   r5   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   )	r0   r+   r%   r4   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_bitsrF   
_next_data_byte_pointer
_and_table
IndexErrorr'   )r   r
   rB   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          |}n]| j                  |   }|st        d| d| j                   d      ||dd z   }|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   z	LZW code z- out of range with empty base at table index .Nr$   z#Limit reached while decompressing: z > )rL   rE   rF   rK   rJ   rI   ioBytesIOrG   r4   rN   r'   writerD   _add_entry_decoder   lenr    r   getvalue)r   r
   output_streamoutput_lengthold_coderB   decodedbases           r   r   zLzwCodec.decode   s.   
 2

'')**))$/Dt&B %%''? t...//1--d34??*6 %%''5 ##t/B/B4/H$HGI)))--d3##G,t666**4+>+>x+H'RS*U **84(#D6)VW[WhWhViijk  bq/##G,&&tWQZ8S\)Mt555'9-DLbLbKcd A 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.)sourcer$   rP   
   rQ      rR   r   )r   rE   r+   r   r   rD   rF   )r   r_   r`   
new_strings       r   rW   zLzwCodec._add_entry_decode  s    %
"33
t222@R1;D--.Q # "D$& "D$& "D 'r   )ihx)r   N)r   r   r   r   r4   r'   r)   r0   intr"   r.   r1   r   r   listr5   rG   rN   r   rW   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   rT   abcr   r   pypdf._utilsr   pypdf.errorsr   r   r	   r   r   r   r   <module>rk      s2    
 # ' :C <o#u o#r   