
    @wg                     R   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZ dd	lmZmZmZmZm Z m!Z!m"Z"m#Z# dd
l$m%Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= e
rddl>m?Z?  G d de      Z@y)    N)BytesIOUnsupportedOperation)Path)TracebackType)TYPE_CHECKINGAnyCallableDictIterableListOptionalTupleTypeUnioncast   )PdfDocCommonconvert_to_int)
EncryptionPasswordType)StrByteType
StreamTypelogger_warningread_non_whitespaceread_previous_lineread_until_whitespaceskip_over_commentskip_over_whitespace)TrailerKeys)EmptyFileErrorFileNotDecryptedErrorPdfReadErrorPdfStreamErrorWrongPasswordError)ArrayObjectContentStreamDecodedStreamObjectDictionaryObjectEncodedStreamObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectStreamObjectTextStringObjectis_null_or_noneread_object)XmpInformation)
PageObjectc                   (   e Zd ZdZ	 	 d?deeef   dededee	f   ddfdZ
deeef   ddfdZdeeee	f      ddfd	Zd@d
Zdeee      dee   dee   ddfdZdAdZedefd       Zedee   fd       Zedee   fd       Z	 	 dBdedee   f   dedee   f   deeef   fdZedefd       Zedee   fd       Z dede!e"e#f   dee!   fdZ$de#dee!e%ef   fdZ&dee!e#f   dee%   fdZ'de(de)e!e!f   fdZ*de!de!dee%   fdZ+de!de!d ee%   dee%   fd!Z,d"e#d e%de%fd#Z-de(ddfd$Z.de(ddfd%Z/de(ddfd&Z0de(de!fd'Z1de(ddfd(Z2de(d)ee!   d*e!ddfd+Z3d,eddfd-Z4de(dee!   fd.Z5de(d)e!dee!   fd/Z6de(dee7e8e9f   fd0Z:e;de(d)e!de!fd1       Z<de(ddfd2Z=d3e>e!   d4e?e!gee!e)e!d5f   f   f   d6e?e!ee!e)e!d5f   f   gef   ddfd7Z@d8e>e!   dee)e!e!f      fd9ZAdeee	f   deBfd:ZCedefd;       ZDd<edee   fd=ZEd<edee   fd>ZFy)C	PdfReadera  
    Initialize a PdfReader object.

    This operation can take some time, as the PDF stream's cross-reference
    tables are read into memory.

    Args:
        stream: A File object or an object that supports the standard read
            and seek methods similar to a File object. Could also be a
            string representing a path to a PDF file.
        strict: Determines whether user should be warned of all
            problems and also causes some correctable problems to be fatal.
            Defaults to ``False``.
        password: Decrypt PDF file at initialization. If the
            password is None, the file will not be decrypted.
            Defaults to ``None``.

    Nstreamstrictpasswordreturnc                 4   || _         d | _        i | _        d| _        i | _        i | _        i | _        t               | _        d | _	        d | _
        | j                  |       d| _        d | _        | j                  r| j                  |       y |t!        d      y )Nr   FzNot an encrypted file)r8   flattened_pagesresolved_objects
xref_indexxrefxref_free_entryxref_objStmr(   trailer_page_id2num_validated_root_initialize_stream_override_encryption_encryptionis_encrypted_handle_encryptionr"   )selfr7   r8   r9   s       D/var/www/horilla/myenv/lib/python3.12/site-packages/pypdf/_reader.py__init__zPdfReader.__init__o   s     ;?LN/1	:<79') 7;;?'$)!15##H-!677 "    c                 R   t        |d      rd|j                  vrt        dt               d| _        t        |t        t        f      r5t        |d      5 }t        |j                               }d d d        d| _        d| _        | j                  |       || _        y # 1 sw Y   0xY w)NmodebzQPdfReader stream/file object is not in binary mode. It may not be read correctly.FrbTr   )hasattrrO   r   __name___stream_opened
isinstancestrr   openr   read
_startxrefr7   )rJ   r7   fhs      rK   rE   zPdfReader._initialize_stream   s    66"s&++'=0
 $fsDk*fd# ,r +,"&D 		&, ,s   BB&c                    d| _         | j                  j                  t        j                        }|r|d   j                         j                  nd}t        t        | j                  t        j                     j                               }t        j                  ||      | _        ||nd}| j                  j                  |      t        j                  k(  r|t!        d      d| _         y )NTr   rM   zWrong passwordF)rF   rB   getTKID
get_objectoriginal_bytesr   r(   ENCRYPTr   rX   rG   verifyr   NOT_DECRYPTEDr$   )rJ   r9   id_entry	id1_entryencrypt_entrypwds         rK   rI   zPdfReader._handle_encryption   s    $(! <<##BEE*?GHQK**,;;S	-t||BJJ/G/R/R/TU%??=)D #.hC##C(L,F,FF$ %%566$)!rM   c                     | S N rJ   s    rK   	__enter__zPdfReader.__enter__   s    rM   exc_typeexc_valexc_tbc                 $    | j                          y ri   )close)rJ   rm   rn   ro   s       rK   __exit__zPdfReader.__exit__   s     	

rM   c                     | j                   r| j                  j                          g | _        i | _        t               | _        i | _        i | _        i | _	        y)z8Close the stream if opened in __init__ and clear memory.N)
rT   r7   rq   r<   r=   r(   rB   r?   r@   rA   rk   s    rK   rq   zPdfReader.close   sL    KK! "')	!rM   c                    | j                   r| j                   S | j                  j                  t        j                        }t        |      rt        dt               nt        t        t        t        |      j                               j                  d      dk(  r2t        t        t        t        |      j                               | _         nt        dt               | j                   t        dt               t        t        | j                  j                  dd            }t        |      D ]b  }	 | j                  |d	z         }t        |t              s)|j                  d      dk(  s>|| _         t        d
|j                   t                n | j                   t#        d      | j                   S # t        $ r d}Y }w xY w)z7Provide access to "/Root". Standardized with PdfWriter.z"Cannot find "/Root" key in trailer/Typez/CatalogzInvalid Root object in trailerNz$Searching object with "/Catalog" key/Sizer   r   zRoot found at zCannot find Root object in pdf)rD   rB   r\   r]   ROOTr1   r   rS   r   r(   r.   r_   intrange	ExceptionrU   indirect_referencer"   )rJ   rootnbios        rK   root_objectzPdfReader.root_object   s|    '''||(4 ?J!4	4#8#C#C#EFJJ7S $( $y$"7"B"B"D$D  ;XF'A8Lc4<<++GQ78B2Y A.A a!12quuW~7S+,D("^A4H4H3K#LhW ##+"#CDD### ! As   3F77GGc                     | j                   j                  t        j                  d      }t	        |      ry|j                         }t        |t              st        d      |S )z
        Provide access to "/Info". Standardized with PdfWriter.

        Returns:
            /Info Dictionary; None if the entry does not exist

        NzETrailer not found or does not point to document information directory)	rB   r\   r]   INFOr1   r_   rU   r(   r"   )rJ   infos     rK   _infozPdfReader._info   sW     ||.4 ??$Dd$45"[  KrM   c                     | j                   j                  t        j                  d      }t	        |      rdS t        t        |j                               S )z
        Provide access to "/ID". Standardized with PdfWriter.

        Returns:
            /ID array; None if the entry does not exist

        N)rB   r\   r]   r^   r1   r   r%   r_   )rJ   ids     rK   _IDzPdfReader._ID   s=     \\beeT*&r*tR["--/0RRrM   includeexcludec                 8   | j                   j                  d       | j                   j                         }d|i}|(|j                         D ci c]  \  }}||v s|| }}}|(|j                         D ci c]  \  }}||vs|| }}}|S c c}}w c c}}w )z
        Integration into Jupyter Notebooks.

        This method returns a dictionary that maps a mime-type to its
        representation.

        See https://ipython.readthedocs.io/en/stable/config/integrating.html
        r   zapplication/pdf)r7   seekrX   items)rJ   r   r   pdf_datadatakvs          rK   _repr_mimebundle_zPdfReader._repr_mimebundle_	  s     	;;##%x
 %)ZZ\BTQQ'\AqDBDB%)ZZ\FTQQg5EAqDFDF C Gs   BB9BBc                     | j                   j                         }| j                   j                  dd       | j                   j                  d      j	                  dd      }| j                   j                  |d       |S )z
        The first 8 bytes of the file.

        This is typically something like ``'%PDF-1.6'`` and can be used to
        detect if the file is actually a PDF file and which version it is.
        r      zutf-8backslashreplace)r7   tellr   rX   decode)rJ   locpdf_file_versions      rK   
pdf_headerzPdfReader.pdf_header&  sg     kk A;;++A.55g?QRa rM   c                     	 d| _         t        t        | j                  j                        d| _         S # d| _         w xY w)z(XMP (Extensible Metadata Platform) data.TF)rF   r   r3   r   xmp_metadatark   s    rK   r   zPdfReader.xmp_metadata6  s6    	.(,D%(8(8(E(EF(-D%D%s   *4 	=r{   c                    | j                   Bt        | j                        D ci c]  \  }}|j                  j                  | c}}| _         t        |      ryt        |t        t        f      sJ d       t        |t              r|}n|j                  }| j                   J d       | j                   j                  |d      }|S c c}}w )z
        Retrieve the page number from an indirect reference.

        Args:
            indirect_reference: The indirect reference to locate.

        Returns:
            Page number or None.

        Nmypyzhint for mypy)
rC   	enumeratepagesr{   idnumr1   rU   rx   r*   r\   )rJ   r{   r~   xr   rets         rK   _get_page_number_by_indirectz&PdfReader._get_page_number_by_indirect?  s     $:CDJJ:O!26!Q$$**A-!D -.,sN.CDLfLD(#.&E&,,E  ,=o=,##E40
!s   !Cc                 
   | j                   |j                     \  }}t        |d|       j                         }t	        t
        |d         dk(  sJ t        |j                               }t        |d         D ]  }t        |       |j                  dd       t        j                  |      }t        |       |j                  dd       t        j                  |      }t        |       |j                  dd       ||j                  k7  r| j                  r||k7  rt        d      |j                  t        |d   |z         d       t        |       |j                  dd       	 t!        ||       }	|	c S  | j                  rt        d      t+               S # t"        $ ra}
t%        d	| d
|j                   d|j&                   d|
 t(               | j                  rt        d|
       t+               }	Y d }
~
|	c S d }
~
ww xY w)Nr   ru   /ObjStm/Nr   zObject is in wrong index.z/FirstzInvalid stream (index z) within object  z: zCannot read object stream: z%This is a fatal error in strict mode.)rA   r   r*   r_   r   rV   r   get_datary   r   r   r-   read_from_streamr8   r"   rx   r2   r#   r   
generationrS   r,   )rJ   r{   stmnumidxobj_stmstream_datar~   objnumoffsetobjexcs              rK   _get_object_from_streamz!PdfReader._get_object_from_stream\  s   
 &&'9'?'?@'5fa'F'Q'Q'SC)*i777g..01wt}% &	A,R#!22;?F,R#!22;?F,R#+111{{sax"#>??S!2V!;<a@  ,R##!+t4" JM&	P ;;7  |- " # ,QC/?)//02D2O2O1PPRe 	 ;;&5cU;  !lJ!#s   &F	H!AG==Hc                    t        |t              rt        |d|       }| j                  |j                  |j
                        }||S |j                  dk(  r+|j
                  | j                  v r| j                  |      }n|j                  | j                  v rJ|j
                  | j                  |j                     v r$| j                  j                  |j                  i       j                  |j
                  d      r
t               S | j                  |j                     |j
                     }| j                  j                  |d       	 | j                  | j                        \  }}||j
                  k7  s||j                  k7  rt        d      	 ||j
                  k7  rD| j8                  r8| j:                  rst        d|j
                   d|j                   d| d| d	      ||j
                  k7  r8| j:                  r,t        d|j
                   d|j                   d| d| d	      | j:                  r||j                  k(  sJ t=        | j                  |       }| j>                  s'| j@                  | j@                  jC                         stE        d      tG        tH        |      }| j@                  jK                  ||j
                  |j                        }nt#        | j                  d      r$t%        | j                  j'                               }nm| j                  j)                         }| j                  j                  dd       | j                  j+                  d      }| j                  j                  |d       t-        j.                  d|j
                   d|j                   dj1                         |      }|t3        d|j
                   d|j                   dt4               |j                  | j                  vri | j                  |j                  <   |j7                  d      dz   | j                  |j                     |j
                  <   | j                  j                  |jM                  d      dz          tO        | j                         | j                  j                  dd       t=        | j                  |       }| j>                  s| j@                  | j@                  jC                         stE        d      tG        tH        |      }| j@                  jK                  ||j
                  |j                        }nBt3        d|j
                   d|j                   dt4               | j:                  rt        d      | jQ                  |j                  |j
                  |       |S # t         $ r t#        | j                  d      r$t%        | j                  j'                               }nm| j                  j)                         }| j                  j                  dd       | j                  j+                  d      }| j                  j                  |d       t-        j.                  d|j
                   d|j                   dj1                         |      }|t3        d	|j
                   d
|j                   dt4               |j7                  d      dz   | j                  |j                     |j
                  <   | j                  j                  |j7                  d      dz          | j                  | j                        \  }}nd}d}Y w xY w)Nr   Fz&Not matching, we parse the file for it	getbufferr   z\sz\s+z\s+objz
Object ID ,z ref repairedr   zExpected object ID (r   z) does not match actual (z); xref table not zero-indexed.z).zFile has not been decryptedzObject z foundz not defined.zCould not find object.))rU   rx   r*   cache_get_indirect_objectr   r   rA   r   r?   r@   r\   r,   r7   r   read_object_headerr"   rz   rR   bytesr   r   rX   researchencoder   rS   startr>   r8   r2   rF   rG   is_decryptedr!   r   r.   decrypt_objectendr   cache_indirect_object)	rJ   r{   retvalr   r   r   bufpms	            rK   r_   zPdfReader.get_object  s    (#.!/0BAt!L//))+=+C+C
 M))Q."((D,<,<<112DEF))TYY6"((DII6H6S6S,TT##''(:(E(ErJNN"((% "|#II0;;<=O=U=UVEKKUA&$$($;$;DKK$H!z/555!%7%B%BB&'OPP C8 *000T__;;&./A/G/G.HJ\JgJgIh i227* F77  ,222t{{"*+=+C+C*DA)445 6wa
|2/ 
 {{!%7%B%BBBB d3F ,,1A1A1M''446/0MNNi0))88.446H6S6S t{{K0DKK1134KK$$&  A&kk&&r*  A&		(../s3E3P3P2QQWX__aA }0667q9K9V9V8WW]^ &00		A?ADII0;;<GGAJN 		,7789K9Q9QR   qA.$T[[1  Q'$T[[$7 00T5E5E5Q++88:34QRR!)V4F!--<< 2 8 8:L:W:WF 0667q9K9V9V8WWde ;;&'?@@""))+=+C+CV	
 M  $4;;4 5 5 78C((*AKK$$Q*++**2.CKK$$Q*II,22337I7T7T6UU[\cce ="$%7%=%=$>a@R@]@]?^^kl  a II0;;<*00 KK$$QWWQZ!^4(,(?(?(L%E:E!#J1$s   AW   F%])(])c                    d}t        |       |t        |      z  }|j                  dd       t        |      }|t        |      z  }|j                  dd       t        |      }|t        |      z  }|j                  dd       |j	                  d      }t        |       |j                  dd       |r"| j                  rt        d| d| t               t        |      t        |      fS )NFr   r      z.Superfluous whitespace found in object header r   )
r   r   r   r   rX   r   r8   r   rS   rx   )rJ   r7   extrar   r   _objs         rK   r   zPdfReader.read_object_header  s    
 &!%f--B%f-%f--B*62
%f--B {{1~F#BT[[@qU 5z3z?**rM   r   r   c                 n    	 | j                   j                  ||f      S # t        $ r t        d      w xY w)Nz Maximum recursion depth reached.)r=   r\   RecursionErrorr"   )rJ   r   r   s      rK   r   z#PdfReader.cache_get_indirect_object8  sA    	C((,,j%-@AA 	CABB	Cs    4r   c                     ||f| j                   v r/d| d| }| j                  rt        |      t        |t               || j                   ||f<   |t        |||       |_        |S )NzOverwriting cache for r   )r=   r8   r"   r   rS   r*   r{   )rJ   r   r   r   msgs        rK   r   zPdfReader.cache_indirect_object@  su     $"7"77*:,aw?C{{"3''3)58z512?%3E:t%LC"
rM   indirectc                     |j                   | k7  rt        d      |j                  |j                  f| j                  vrt        d      || j                  |j                  |j                  f<   ||_        |S )Nz,Cannot update PdfReader with external objectzCannot find referenced object)pdf
ValueErrorr   r   r=   r{   )rJ   r   r   s      rK   _replace_objectzPdfReader._replace_objectM  so    <<4KLL08M8MM<==GJx22HNNCD!)
rM   c                    | j                  |       | j                  |       | j                  |      }|| _        | j	                  ||      }|dk7  r-| j
                  r|rt        d      t        d| dt               | j                  |||       | j                  r| j
                  s|j                         }| j                  j                         D ]  \  }}|dk(  rt        |j                               }|D ]q  }|j!                  ||   d       	 | j#                  |      \  }	}
|	|| j                  z
  k(  s@| j                  |   |   | j                  |   |	<   | j                  |   |= s  |j!                  |d       | j
                  s|j                         }| j                  j                         D ]R  \  }}|dk(  rt)        |j                               }|D ])  }|j!                  ||   d       	 | j#                  |       + T |j!                  |d       y	y	# t$        $ r | j'                  |       Y  lw xY w# t$        $ r# t        d| d| d||    dt               ||= Y w xY w)
z
        Read and process the PDF stream, extracting necessary data.

        Args:
            stream: The PDF file stream.

        r   zBroken xref tablezincorrect startxref pointer()  zIgnoring wrong pointing object r   z	 (offset N)_basic_validation_find_eof_marker_find_startxref_posrY   _get_xref_issuesr8   r"   r   rS   _read_xref_tables_and_trailersr>   r   r?   r   sortedkeysr   r   r   _rebuild_xref_tablelist)rJ   r7   	startxrefxref_issue_nrr   gen
xref_entryxref_kr   pid_pgenidss               rK   rX   zPdfReader.readW  sq    	v&f%,,V4	# --fi@A{{}"#6779-JHU 	++FI}M ??4;;++-C#'99??#4 /Z%<OO% ! 
/BKK
22%)%<%<V%D
U b4??22.2iinR.@		#s+ IIcN2.
//& KKQ {{++-C#'99??#4 +Z%<:??,- 	+BKK
22+//7	+	+ KKQ!  & 008, & +&=bT3%yQ[\^Q_P``ab$ 'rN+s$   H"9I"I I)I0/I0c                 |   |j                  dt        j                         	 |j                  d      }|dk(  rt        d      |dk7  r=| j                  rt        d|j                  d       d	      t        d
| t               |j                  dt        j                         y# t        $ r t        d      w xY w)z)Ensure the stream is valid and not empty.r      zcannot read headerrM   zCannot read an empty files   %PDF-zPDF starts with 'utf8z', but '%PDF-' expectedzinvalid pdf header: N)r   osSEEK_SETrX   UnicodeDecodeErrorr   r    r8   r"   r   r   rS   SEEK_END)rJ   r7   header_bytes      rK   r   zPdfReader._basic_validation  s    Ar{{#	= ++a.K # !<==H${{"'(:(:6(B'C D+ + 
 !5k]CXNAr{{# " 	=&';<<	=s   B& &B;c                 D   d}dd}dd dk7  rdk7  r)|r't        fddD              rt        d	t               yd
}dv rt        dt               |j                         |k  r'| j                  rt        d      t        dt               t        |      dd dk7  ryy)a  
        Jump to the %%EOF marker.

        According to the specs, the %%EOF marker should be at the very end of
        the file. Hence for standard-compliant PDF documents this function will
        read only the last part (DEFAULT_BUFFER_SIZE).
        r   rM   TNr   s   %%EOFc              3   \   K   | ]#  }j                         j                  |       % y wri   )stripendswith).0trlines     rK   	<genexpr>z-PdfReader._find_eof_marker.<locals>.<genexpr>  s'      24DJJL))"-s   ),)s   %%EOs   %%Es   %%   %zEOF marker seems truncatedF	   startxrefzpCAUTION: startxref found while searching for %%EOF. The file might be truncated and some data might not be read.zEOF marker not found)anyr   rS   r   r8   r"   r   )rJ   r7   HEADER_SIZEfirstr   s       @rK   r   zPdfReader._find_eof_marker  s     2Ah("s{u 8V 
 ##?Jt#S
 {{}{*;;&'=>>"#98D%f-D+ 2Ah("rM   c                     t        |      }	 t        |      }t        |      }|dd dk7  rt        d      |S # t        $ rL |j	                  d      st        d      t        |dd j                               }t        dt               Y |S w xY w)z
        Find startxref entry - the location of the xref table.

        Args:
            stream:

        Returns:
            The bytes offset

        N	   r   zstartxref not foundz startxref on same line as offset)r   rx   r"   r   
startswithr   r   rS   )rJ   r7   r   r   s       rK   r   zPdfReader._find_startxref_pos  s     "&)	:D	I &f-DBQx<'"#899  	I??<0"#899DHNN,-I=xH
 	Is   8 ABBc                 T   |j                  d      }|dk7  rt        d      t        |       |j                  dd       d}	 t	        t
        t        ||             }|r(|dk7  r#|| _        | j                  rt        dt               d	}t        |       |j                  dd       t	        t
        t        ||             }t        |t
              s2t        d
t               | j                  |       |j                          y t        |       |j                  dd       d}||k  r!|j                  d      }|d   dv r+|j                  dd       |j                  d      }|d   dv r+|d   dv r|j                  dd       	 |d d j                  d      \  }}	|dd }
t        |      t        |	      }}|| j0                  vri | j0                  |<   i | j2                  |<   || j0                  |   v rnC
dk(  r|| j0                  |   |<   	 |
dk(  | j2                  |   |<   	 |
dk(  | j2                  d   |<   |dz  }|dz  }||k  r!t        |       |j                  dd       |j                  d      }|dk7  r|j                  dd       ny N# t        $ r t        |d      rt!        |j#                               }nD|j%                         }|j                  dd       |j                  d      }|j                  |       t'        j(                  | dj+                         |      }|t        d| dt               d}d}n>t        d| dt               t        |j-                  d            }|j/                         }Y w xY w# t        $ r Y vw xY w# t        $ r Y pw xY w)Nr   s   refzxref table read errorr   r   Tr   zFXref table not zero-indexed. ID numbers for objects will be corrected.Fz,Invalid/Truncated xref table. Rebuilding it.      
is   0123456789t             r   z\s+(\d+)\s+objzentry z( in Xref table invalid; object not foundr   z' in Xref table invalid but object found   n   f   s   traileri)rX   r"   r   r   r   rx   r2   r>   r8   r   rS   rU   r   splitrz   rR   r   r   r   r   r   r   groupr   r?   r@   )rJ   r7   ref
first_timenumsizecntr   offset_bgeneration_bentry_type_br   r   r   r   ftrailer_tags                    rK   _read_standard_xref_tablez#PdfReader._read_standard_xref_table  s   kk!n&=677F#B
sK56CcQh"%;;"`  J'KKA[67DdC(B ((0'KKAC*{{2 1g,KK+!;;r?D 1g, 8~-KKA&+-1#2Y__T-B*Hl#'2;L),XL8IJF6 TYY.,.DIIj)79D((4$))J//
 #t+5;		*-c2@LPT@T,,Z8=;G4;O,,U3C8 qqU *V  'KKA ++a.Kj(B"Y j ! +v{3#F$4$4$67"KKMAq)$kk"oA		SE):";"B"B"DcJAy&$SE)QR$ &+
!#&$SE)PQ$ &)_
!"1+P %  % s7   2J N
 !N C3NN
	NN	N'&N'r   r   c                    i | _         i | _        i | _        t               | _        ||j                  |d       |j                  d      }|dv r|j                  d      }|dk(  r| j                  |      }ns|r	 | j                  |       y|j                         r<	 | j                  |      }| j%                  |       d|v rt'        t(        |d         }ny| j+                  ||      }|yy# t        $ r d}Y w xY w# t        $ r[}t        j                  | j                  v r"t        d|j                   t                Y d}~yt#        d|j                         d}~ww xY w)	z?Read the cross-reference tables and trailers in the PDF stream.Nr   r   r     xz!Previous trailer cannot be read: zTrailer cannot be read: /Prev)r?   r@   rA   r(   rB   r   rX   
_read_xrefr   rz   isdigit_read_pdf15_xref_streamr]   rw   r   argsrS   r"   _process_xref_streamr   rx   _read_xref_other_error)rJ   r7   r   r   r   
xrefstreames          rK   r   z(PdfReader._read_xref_tables_and_trailers^  s\    	!')#KK	1%AAG|KKNDy OOF3	&,,V4 	P!%!=!=f!EJ ))*5j( $S*W*= >I 77	J	? # ! &$%M&
 ! Pww$,,.&?xH( *-EaffX+NOOPs0   9C. C? .C<;C<?	E#9EEE#r   c                 .   t         j                  t         j                  t         j                  t         j                  t         j
                  f}|D ]=  }||v s|| j                  vs|j                  |      | j                  t        |      <   ? d|v r| j                  j                         }| j                  j                  t        t        |d         dz   d       | j                  | j                         | j                  j                  |d       yy)z#Process and handle the xref stream./XRefStmr   r   N)r]   rw   ra   r   r^   SIZErB   raw_getr+   r7   r   r   r   rx   r  )rJ   r   trailer_keyskeyr   s        rK   r  zPdfReader._process_xref_stream  s    ww

BGGRUUBGGC 	HCj S%<0:0B0B30GZ_-	H #  "AKKT#z*'=>BAF((5KKQ"	 $rM   c                    | j                  |       |j                  d      dk(  ry |j                  dd       t        |       |j                  dd       t	        t
        t        t        f   t        ||             }|j                         D ]#  \  }}|| j                  vs|| j                  |<   % d|v rZ|j                         }|j                  t	        t        |d         dz   d       	 | j                  |       |j                  |d       d|v r|d   }|S y # t        $ r t        d|d    dt                Y @w xY w)	Nr   rM   r   r#  r   zXRef object at z, can not be read, some object may be missingr  )r  rX   r   r   r   r
   rV   r   r2   r   rB   r   rx   r  rz   r   rS   )rJ   r7   new_trailerr'  valuer   r   s          rK   r  zPdfReader._read_xref  s9   &&v.;;q>S BF#B4S>;vt+DE%++- 	*JC$,,&$)S!	* $AKKS+j"9:Q>B,,V4 KK1k!#G,I  %k*&=%>>jks   .D  E ?E c                 6   |dk(  r(| j                   rt        d      t        dt               y |j	                  dd       |j                  d      }|j                  d      }|dk7  r
|d	|z
  z  }|S |j	                  |d       t        d
      D ]*  }|j                  d      j                         s#||z  }|c S  d| j                  v r/| j                   s#t        dt               	 | j                  |       y t        d      # t        $ r t        d      w xY w)Nr   z6/Prev=0 in the trailer (try opening with strict=False)zA/Prev=0 in the trailer - assuming there is no previous xref tableir   r     xrefr   
      z/Rootz"Invalid parent xref., rebuild xrefzCannot rebuild xrefz/Could not find xref table at specified location)r8   r"   r   rS   r   rX   findry   r  rB   r   rz   )rJ   r7   r   tmpxref_loclooks         rK   r  z PdfReader._read_xref_other_error  s"    >{{"L  S  	Ckk"o88G$r>h&IIq!"I 	!D{{1~%%'T!	  		! dll"4;;?J:((0 LMM  :"#899:s   &D Dc                    	 |j                  dd        j                  |      \  }}t        t        t	        |             }t        t
        |d         dk(  sJ  j                  |||       t        |j                               	|j                  dd|j                  d      g      }t        t        t        t        f   |j                  d            t              d	k\  sJ  j                  rt              d	kD  rt        d
       dt        dt         t        t"        t        df   f   f	fd}dt        dt         t        t"        t        df   f   dt$        f fd} j'                  |||       |S )z-Read the cross-reference stream for PDF 1.5+.r   r   ru   z/XRefz/Indexr   rv   z/Wr   zToo many entry sizes: r~   r:   .c                 h    |    dkD  r#j                  |          }t        ||          S | dk(  ryy)Nr   r   )rX   r   )r~   dentry_sizesr   s     rK   	get_entryz4PdfReader._read_pdf15_xref_stream.<locals>.get_entry  sE     1~!$$[^4%aQ88 AvrM   r  r   c                 `    | j                   j                  |g       v xs | j                  v S ri   )r?   r\   rA   )r  r   rJ   s     rK   used_beforez6PdfReader._read_pdf15_xref_stream.<locals>.used_before  s-    $))--
B77R3$BRBR;RRrM   )r   r   r   r&   r2   rV   r   r   r   r\   r
   r   lenr8   r"   rx   r   r   bool_read_xref_subsections)
rJ   r7   r   r   r   	idx_pairsr7  r9  r6  r   s
   `       @@rK   r  z!PdfReader._read_pdf15_xref_stream  s_    	B 33F;z-VT)BC
CG,-888"":ujAj1134 NN8a1H-IJ	4S>:>>$+?@;1$$$;;3{+a/!7}EFF	 	sE#s(O';!< 		SS 	SeCsCx4H.I 	Sd 	S
 	##Iy+FrM   c                 F   | j                  |dz
  d       | j                  d      }|dk(  r| j                  d      }|dvry| j                  d      }|dk7  rJd}|dv r| j                  d      }|dk(  ry	|dv r|| j                  d	      z  }|j                         d
k7  ryy)z
        Return an int which indicates an issue. 0 means there is no issue.

        Args:
            stream:
            startxref:

        Returns:
            0 means no issue, other values represent specific issues.

        r   r      js   
 	   r,  rM   s   0123456789 	   s   objr   )r   rX   lower)r7   r   r   s      rK   r   zPdfReader._get_xref_issues   s     	IM1%{{1~4<;;q>Dz!{{1~7?D**{{1~3; ** FKKN"Dzz|v%rM   c                    i | _         |j                  dd       |j                  d      }t        j                  d|      D ]t  }t        |j                  d            }t        |j                  d            }|| j                   vri | j                   |<   |j                  d      | j                   |   |<   v t        dt               | j                   D ][  }| j                   |   D ]E  }|j                  | j                   |   |   d       	 | j                  |      }t        t        t        ||             }	|	j                  dd      d	k7  rht        |	j!                               }
d}	 t#        |
      }|j%                         snft        |      }t'        |
       |
j                  dd       t#        |
      }|j%                         sn"t        |      }||f| j(                  |<   |dz  }||	j                  d
      k7  r,t        d| d| d| d|	j                  d
       d	t               H ^ |j                  dd       t        j                  d|      D ]z  }|j                  |j                  d      d       t        t,        t.        t.        f   t        ||             }t1        |j3                               D ]  \  }}|| j4                  |<    | y # t*        $ r Y w xY w)Nr   r   s(   [\r\n \t][ \t]*(\d+)[ \t]+(\d+)[ \t]+objr   rA  zparsing for Object Streamsru    r   r   zfound z objects within Object(r   z
) whereas z	 expecteds$   [\r\n \t][ \t]*trailer[\r\n \t]*(<<))r?   r   rX   r   finditerrx   r  r   r   rS   r   r   r/   r2   r\   r   r   r   r  r   rA   rz   r
   r   r   r   rB   )rJ   r7   f_r   r   r   gr~   _r   strmcpts_i_or)  r'  r*  s                     rK   r   zPdfReader._rebuild_xref_table   s   	Aq[[_I2N 	6A
OEQWWQZJ*(*		*%+,771:DIIj!%(	6 	3X> 	AYYq\ DIIaLOQ///7A\;vt+DEAuuWb)Y6 "1::<0DC1$7 yy{! V,T2		"a(1$7 yy{! V012w((,q  aeeDk)&$SE)@1QC H(()d}I?$1	B 	AqErJ 	*AKK
A&tCH~{64/HIK";#4#4#67 *
U$)S!*		*	 ! s   A KCK	K&%K&r=  r7  .r9  c                    | j                  |      D ]  \  }}t        |||z         D ]  } |d      }|dk(  r |d      } |d      }	!|dk(  rJ |d      }
 |d      }|| j                  vri | j                  |<    |||      r]|
| j                  |   |<   p|dk(  r. |d      } |d      }d} |||      r||f| j                  |<   | j                  st        d|         y)z-Read and process the subsections of the xref.r   r   rA  zUnknown xref type: N)_pairsry   r?   rA   r8   r"   )rJ   r=  r7  r9  r   r  r  	xref_typenext_free_objectnext_generationbyte_offsetr   
objstr_num	obstr_idxs                 rK   r<  z PdfReader._read_xref_subsectionsV  s     ;;y1 	JKE4UEDL1 J%aL	>'0|$&/lO!^"+A,K!*1J!202		*-&sJ75@		*-c2!^!*1J )!I!"J&sJ71;Y0G((-[[&)<YK'HII1J	JrM   arrayc              #      K   d}|dz   t        |      k  r'||   ||dz      f |dz  }|dz   t        |      k  r&yyw)z Iterate over pairs in the array.r   r   rA  N)r:  )rJ   rV  r~   s      rK   rO  zPdfReader._pairsy  sM     !ec%j (E!a%L((FA !ec%j s   9>>c                 f    | j                   st        d      | j                   j                  |      S )a  
        When using an encrypted / secured PDF file with the PDF Standard
        encryption handler, this function will allow the file to be decrypted.
        It checks the given password against the document's user password and
        owner password, and then stores the resulting decryption key if either
        password is correct.

        It does not matter which password was matched. Both passwords provide
        the correct decryption key that will allow the document to be used with
        this library.

        Args:
            password: The password to match.

        Returns:
            An indicator if the document was decrypted and whether it was the
            owner password or the user password.

        zNot encrypted file)rG   r"   rb   )rJ   r9   s     rK   decryptzPdfReader.decrypt  s0    ( 344&&x00rM   c                 :    t         j                  | j                  v S )z
        Read-only boolean property showing whether this PDF file is encrypted.

        Note that this property, if true, will remain true even after the
        :meth:`decrypt()<pypdf.PdfReader.decrypt>` method is called.
        )r]   ra   rB   rk   s    rK   rH   zPdfReader.is_encrypted  s     zzT\\))rM   namec                    | j                   }d|vst        |d   t              syt        t        |t	        d               }d|vryt               }t        |      |t	        d      <   |t	        d         |t	        d      <   | j                  dt        | j                  D cg c]  \  }}|dk(  s| c}}      dz   |       t               }|j                  |j                         ||t	        d      <   t        t        |d         D ]L  }|j                         }	d|	v rt        d	|	j                   d
t               |j                  |	t	        d      <   N |S c c}}w )z
        Add a top level form that groups all form fields below it.

        Args:
            name: text string of the "/T" Attribute of the created object

        Returns:
            The created object. ``None`` means no object was created.

        	/AcroFormN/Fields/Tz/Kidsr   r   z/ParentzTop Level Form Field z have a non-expected parent)r   rU   r(   r   r+   r0   r   maxr=   r%   appendr{   r_   r   rS   )
rJ   r[  catalogacroforminterimrG  r~   arrr   r   s
             rK   add_form_topnamezPdfReader.add_form_topname  s^    ""g%ZK "2.
 ('*[2I*JKH$"$$4T$:
4 !'/
90E'F
7#$""!6!6Av1!q&ABQF	

 m

7--.*-I&'k77#34 	DA,,.CC+C,B,B+CC^_ *1)C)CC
9%&	D  Bs   !E/Ec           
      4   | j                   }d|vst        |d   t              syt        t        |t	        d               }d|vryt        t        t        t
        |t	        d               d   j                               }t        |      |t	        d      <   |S )z
        Rename top level form field that all form fields below it.

        Args:
            name: text string of the "/T" field of the created object

        Returns:
            The modified object. ``None`` means no object was modified.

        r]  Nr^  r   r_  )r   rU   r(   r   r+   r%   r_   r0   )rJ   r[  rb  rc  rd  s        rK   rename_form_topnamezPdfReader.rename_form_topname  s     ""g%ZK "2.
 ('*[2I*JKH$hz)'<=>qALLN
 %5T$:
4 !rM   )FN)r:   r6   )r:   N)NN)GrS   
__module____qualname____doc__r   r   r   r;  rV   r   rL   rE   r   rI   rl   r   BaseExceptionr   rr   rq   propertyr(   r   r   r%   r   r   r
   r   r   r   r3   r   rx   r,   r*   r   r.   r   r_   r   r   r   r   r   r   rX   r   r   r   r  r   r  r  r  r&   r)   r'   r  staticmethodr   r   r   r	   r<  rO  r   rY  rH   rf  rh  rj   rM   rK   r6   r6   [   s   , ,0	8k4'(8 8 c5()	8
 
8>{D/@)A d  *8E#u*4E+F *4 *(4./ -( '	
 
	 $- $ $@ x 01  & 	SXk* 	S 	S /3.2tXc]*+ tXc]*+ 
c3h	:  C     .h~6 . ."'c:~(M"N	#:6"06	sIs"	#6pF"'^(;"<F	)	FP+ +c3h +8CC&)C	)	C&)080C	)	 Y 9 @ : @ $ @ D$
 $t $& .z  .d  .D*  4t
 tt tl'K 'K-5c]'KKN'K	'KR
#/? 
#D 
#  8'N 'N-0'N	#'NR& &	}13FF	G&P     >4** 4* 4*l!J9!J SE5eCHo)=#>>?!J sE#uS#X*>$?@$FG	!J
 
!JFDI (5c?*C 1c5j 1 1l 12 *d * *)S )X6F-G )V 9I0J rM   r6   )Ar   r   ior   r   pathlibr   typesr   typingr   r   r	   r
   r   r   r   r   r   r   r   _doc_commonr   r   rG   r   r   _utilsr   r   r   r   r   r   r   r   	constantsr   r]   errorsr    r!   r"   r#   r$   genericr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   xmpr3   _pager4   r6   rj   rM   rK   <module>rz     s}   < 
 	 ,      6 1	 	 	 )       !M MrM   