
    e              	      \   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d Z
i dddddd	d
dddddddddddddddddddddd d!d"d#d$d%i d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOZdP Z G dQ dRe      ZdS Z G dT dU ej                   dVdWdXg            Z G dY dZe      Z G d[ d\      Z G d] d^      Z G d_ d`e      Z G da dbej.                        Z G dc dd      Z G de df      Zdg Z G dh di      Zy)j    )annotationsNc                F    t         j                  | j                  d      z   S )N	utf_16_be)codecsBOM_UTF16_BEencode)ss    //usr/lib/python3/dist-packages/PIL/PdfParser.pyencode_textr      s    +!666          u   ˘   u   ˇ   u   ˆ   u   ˙   u   ˝   u   ˛   u   ˚   u   ˜   u   •   u   †   u   ‡   u   …   u   —   u   –   u   ƒ   u   ⁄   u   ‹   u   ›   u   −   u   ‰   u   „   u   “   u   ”   u   ‘   u   ’   u   ‚   u   ™   u   ﬁ   u   ﬂ   u   Ł   u   Œ   u   Š   u   Ÿu   Žu   ıu   łu   œu   šu   žu   €)                     c                    | d t        t        j                         t        j                  k(  r+| t        t        j                        d  j                  d      S dj	                  d | D              S )Nr    c              3  Z   K   | ]#  }t         j                  |t        |             % y wN)PDFDocEncodinggetchr).0bytes     r
   	<genexpr>zdecode_text.<locals>.<genexpr>D   s!     It~))$D	:Is   )+)lenr   r   decodejoin)bs    r
   decode_textrE   @   s\    	#3v""#$(;(;;V(()+,33K@@wwIqIIIr   c                      e Zd ZdZy)PdfFormatErrorz\An error that probably indicates a syntactic or semantic error in the
    PDF file structureN)__name__
__module____qualname____doc__ r   r
   rG   rG   G   s     	r   rG   c                    | st        |      y r:   )rG   )	conditionerror_messages     r
   check_format_conditionrP   N   s    ]++ r   c                  *    e Zd Zd Zd Zd Zd Zd Zy)IndirectReferencec                :    | j                    d| j                   dS )N z R	object_id
generationselfs    r
   __str__zIndirectReference.__str__V   s    ..!4??"3266r   c                @    | j                         j                  d      S Nus-ascii)rZ   r   rX   s    r
   	__bytes__zIndirectReference.__bytes__Y   s    ||~$$Z00r   c                    |j                   | j                   u xr4 |j                  | j                  k(  xr |j                  | j                  k(  S r:   )	__class__rV   rW   rY   others     r
   __eq__zIndirectReference.__eq__\   sC    OOt~~- 44>>14  DOO3	
r   c                    | |k(   S r:   rL   ra   s     r
   __ne__zIndirectReference.__ne__c   s    EM""r   c                D    t        | j                  | j                  f      S r:   )hashrV   rW   rX   s    r
   __hash__zIndirectReference.__hash__f   s    T^^T__566r   N)rH   rI   rJ   rZ   r^   rc   re   rh   rL   r   r
   rR   rR   S   s    71
#7r   rR   IndirectReferenceTuplerV   rW   c                      e Zd Zd Zy)IndirectObjectDefc                :    | j                    d| j                   dS )NrT   z objrU   rX   s    r
   rZ   zIndirectObjectDef.__str__k   s    ..!4??"3488r   N)rH   rI   rJ   rZ   rL   r   r
   rk   rk   j   s    9r   rk   c                  <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
	XrefTablec                @    i | _         i | _        ddi| _        d| _        y )Nr   i   F)existing_entriesnew_entriesdeleted_entriesreading_finishedrX   s    r
   __init__zXrefTable.__init__p   s&     " !5z %r   c                    | j                   r|| j                  |<   n|| j                  |<   || j                  v r| j                  |= y y r:   )rs   rq   rp   rr   rY   keyvalues      r
   __setitem__zXrefTable.__setitem__v   sL      $)DS!).D!!#&$&&&$$S) 'r   c                ^    	 | j                   |   S # t        $ r | j                  |   cY S w xY wr:   )rq   KeyErrorrp   rY   rw   s     r
   __getitem__zXrefTable.__getitem__~   s8    	.##C(( 	.((--	.s    ,,c                \   || j                   v r2| j                   |   d   dz   }| j                   |= || j                  |<   y || j                  v r%| j                  |   d   dz   }|| j                  |<   y || j                  v r| j                  |   }y dt        |      z   dz   }t	        |      )N   z
object ID z+ cannot be deleted because it doesn't exist)rq   rr   rp   str
IndexError)rY   rw   rW   msgs       r
   __delitem__zXrefTable.__delitem__   s    $"""))#.q1A5J  %(2D  %D)))..s3A6:J(2D  %D(((--c2J s3x'*WW  S/!r   c                >    || j                   v xs || j                  v S r:   )rp   rq   r|   s     r
   __contains__zXrefTable.__contains__   s#    d+++Fsd6F6F/FFr   c                    t        t        | j                  j                               t        | j                  j                               z  t        | j
                  j                               z        S r:   )rA   setrp   keysrq   rr   rX   s    r
   __len__zXrefTable.__len__   s^    %%**,-$""'')*+$&&++-./
 	
r   c                    t        | j                  j                               t        | j                  j                               z
  t        | j                  j                               z  S r:   )r   rp   r   rr   rq   rX   s    r
   r   zXrefTable.keys   sS    %%**,-D4H4H4M4M4O0PP  %%'() 	)r   c                   t        t        | j                  j                               t        | j                  j                               z        }t        t        | j                  j                                     }|j                         }|j                  d       |rd }t        |      D ]  \  }}||dz   |k(  r|}|d | }||d  } n |}d }|j                  d|d   t        |      fz         |D ]  }	|	| j                  v r"|j                  d| j                  |	   z         3|j                  d      }
t        |	|
k(  d|	 d|
        	 |d   }|j                  d|| j                  |	   fz          |r|S # t        $ r d}Y 7w xY w)	Ns   xref
r   s   %d %d
r   s   %010d %05d n 
z*expected the next deleted object ID to be z, instead found s   %010d %05d f 
)sortedr   rq   r   rr   tellwrite	enumeraterA   poprP   r   )rY   fr   deleted_keys	startxrefprevindexrw   contiguous_keysrV   this_deleted_object_idnext_in_linked_lists               r
   r   zXrefTable.write   s   c$**//12S9M9M9R9R9T5UUVc$"6"6";";"=>?FFH			D'o 	
s<4!8s?D&*6ElO<D	 #'GGJ/!"4c/6J!KKL, 	 0 00GG.1A1A)1LLM-9-=-=a-@**!%;;DYK P))?(@B
0.:1o+ GG*.0D0DY0OPQ @  & 0./+0s   E>>FFN)rH   rI   rJ   rt   ry   r}   r   r   r   r   r   rL   r   r
   rn   rn   o   s+    &*." G
)
%r   rn   c            	          e Zd Zd Zd Zd Zd Zd Zed        Z	 e
 edd            d	D  ch c]  }t        |       c}}} z
  Zd
 Zyc c}}} w )PdfNamec                    t        |t              r|j                  | _        y t        |t              r|| _        y |j	                  d      | _        y r\   )
isinstancer   namebytesr   )rY   r   s     r
   rt   zPdfName.__init__   s8    dG$		DIe$DIJ/DIr   c                8    | j                   j                  d      S r\   )r   rB   rX   s    r
   name_as_strzPdfName.name_as_str   s    yy
++r   c                z    t        |t              xr |j                  | j                  k(  xs || j                  k(  S r:   )r   r   r   ra   s     r
   rc   zPdfName.__eq__   s4    ug&B5::+B dii	 r   c                ,    t        | j                        S r:   )rg   r   rX   s    r
   rh   zPdfName.__hash__   s    DIIr   c                4    dt        | j                         dS )NzPdfName())reprr   rX   s    r
   __repr__zPdfName.__repr__   s    $tyy/*!,,r   c                8     | t         j                  |            S r:   )	PdfParserinterpret_name)clsdatas     r
   from_pdf_streamzPdfName.from_pdf_stream   s    9++D122r   !      z#%/()<>[]{}c                    t        d      }| j                  D ]6  }|| j                  v r|j                  |       #|j	                  d|z         8 t        |      S )N   /s   #%02X)	bytearrayr   allowed_charsappendextendr   )rY   resultrD   s      r
   r^   zPdfName.__bytes__   sT    4 	,AD&&&a hl+		,
 V}r   N)rH   rI   rJ   rt   r   rc   rh   r   classmethodr   r   rangeordr   r^   )r>   cr   s   000r
   r   r      sc    0, 
- 3 3 b#'=*I*Ia3q6*IIM +Js   A
r   c                      e Zd Zd Zy)PdfArrayc                >    ddj                  d | D              z   dz   S )Ns   [     c              3  2   K   | ]  }t        |        y wr:   )pdf_repr)r>   xs     r
   r@   z%PdfArray.__bytes__.<locals>.<genexpr>   s      ;! ;s   s    ])rC   rX   s    r
   r^   zPdfArray.__bytes__   s!    tyy ;d ;;;eCCr   N)rH   rI   rJ   r^   rL   r   r
   r   r      s    Dr   r   c                      e Zd Zd Zd Zd Zy)PdfDictc                z    |dk(  r"t         j                  j                  | ||       y || |j                  d      <   y )Nr   r]   )collectionsUserDict__setattr__r   rv   s      r
   r   zPdfDict.__setattr__   s4    &=  ,,T3>+0DJ'(r   c                `   	 | |j                  d         }t        |t              rt        |      }|j                  d      r|j                  d      r|dd  }d}t        |      dkD  r5|d   }t        |dd       d	z  }t        |      d
kD  r|t        |dd
       z  }dd t        |      dz
   }t        j                  |d t        |      dz    |      }|dv r:d	z  }|dk(  r|dz  }t        j                  t        j                  |      |z         }|S # t        $ r}t        |      |d }~ww xY w)Nr]   DatezD:   Z         <         z%Y%m%d%H%M%S)+-r   )r   r{   AttributeErrorr   r   rE   endswith
startswithrA   inttimestrptimegmtimecalendartimegm)rY   rw   rx   erelationshipoffsetformats          r
   __getattr__zPdfDict.__getattr__   s:   	-J/0E eU#&E<<%ab	L5zB$RyU2b\*R/u:?c%2,//F#$4c%j1n5FMM%(9#f+/":FCEz)"3&bLFHOOE$:V$CD-  	- %1,	-s   D 	D-D((D-c                H   t        d      }| j                         D ]i  \  }}|	t        |      }|j                  d       |j                  t	        t        |                   |j                  d       |j                  |       k |j                  d       t	        |      S )N   <<   
r   s   
>>)r   itemsr   r   r   r   )rY   outrw   rx   s       r
   r^   zPdfDict.__bytes__  s    **, 	JC}UOEJJuJJuWS\*+JJtJJu	 	

7Szr   N)rH   rI   rJ   r   r   r^   rL   r   r
   r   r      s    16r   r   c                      e Zd Zd Zd Zy)	PdfBinaryc                    || _         y r:   )r   )rY   r   s     r
   rt   zPdfBinary.__init__(  s	    	r   c                L    ddj                  d | j                  D              z  S )Ns   <%s>r   c              3  &   K   | ]	  }d |z    yw)s   %02XNrL   r>   rD   s     r
   r@   z&PdfBinary.__bytes__.<locals>.<genexpr>,  s     !A!'A+!As   )rC   r   rX   s    r
   r^   zPdfBinary.__bytes__+  s     !Atyy!AAAAr   N)rH   rI   rJ   rt   r^   rL   r   r
   r   r   '  s    Br   r   c                      e Zd Zd Zd Zy)	PdfStreamc                     || _         || _        y r:   )
dictionarybuf)rY   r   r   s      r
   rt   zPdfStream.__init__0  s    $r   c                   	 | j                   j                  }|dk(  rA	 | j                   j                  }t        j                  | j                  t        |            S dt        | j                   j                         d}t        |      # t        $ r | j                  cY S w xY w# t        $ r | j                   j
                  }Y w xY w)Ns   FlateDecode)bufsizezstream filter z unknown/unsupported)r   Filterr   r   DLLengthzlib
decompressr   r   NotImplementedError)rY   filterexpected_lengthr   s       r
   rB   zPdfStream.decode4  s    	__++F ^#9"&//"4"4 ??488S5IJJ"4(>(>#?"@@TUC%c**  	88O	
 " 9"&//"8"89s"   B B' B$#B$'C	C	N)rH   rI   rJ   rt   rB   rL   r   r
   r   r   /  s    +r   r   c                   | du ry| du ry| yt        | t        t        t        t        f      rt        |       S t        | t        t        f      rt        |       j                  d      S t        | t        j                        r+dt        j                  d|       j                  d      z   d	z   S t        | t              rt        t        |             S t        | t              rt        t        |             S t        | t              rt        t!        |             S t        | t
              r>| j#                  d
d      } | j#                  dd      } | j#                  d	d      } d| z   d	z   S t        |       S )NTs   trueFs   falses   nullr]   s   (D:z%Y%m%d%H%M%SZ   )   \s   \\   (   \(s   \))r   r   r   r   r   r   r   floatr   r   r   struct_timestrftimedictlistr   r   replace)r   s    r
   r   r   D  s-   Dy	
e	
	A(I>	?Qx	AU|	$1v}}Z((	At''	(oq9@@LLtSS	At	WQZ  	At	Xa[!!	As	A''	Au	IIeW%IIdF#IIdF#ax$Qxr   c                     e Zd ZdZdSdZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd ZdTdZd Zd Zd Zed        Zd ZdTdZdZdZdZdZedz   Zedz   ZdZdZeez   ez   Z e jB                  edz   ez   dz   ez   dz   ez   d z   ez   d!z   ez   d"z   e jD                        Z# e jB                  edz   ez   d#z   ez   dz   ez   d z   ez   d!z   ez   e jD                        Z$d$ Z%d% Z& e jB                  e      Z' e jB                  ed&z   ez   d'z         Z( e jB                  ed(z         Z) e jB                  ed)z   ez         Z*e+d*        Z, e jB                  d+      Z-e+dUd,       Z. e jB                  ed-z   ez   d'z         Z/ e jB                  ed.z   ez   d'z         Z0 e jB                  ed/z   ez   d'z         Z1 e jB                  ed0z   ez   d'z         Z2 e jB                  ed1z   ez   d'z         Z3 e jB                  ed2z         Z4 e jB                  ed3z         Z5 e jB                  ed4z   ez   d5z         Z6 e jB                  ed6z         Z7 e jB                  ed7z   ez   d7z   ez   d8z   ez   d'z         Z8 e jB                  ed7z   ez   d7z   ez   d9z   ez   d'z         Z9 e jB                  ed:z   ez   d'z         Z: e jB                  d;ez   d<z   ez   d=z         Z; e jB                  ed>z         Z< e jB                  ed?z   ez   d'z         Z=e+dVd@       Z> e jB                  dA      Z?i dBdCdDdEdFdGdHdIdJdKd;d;d'd'dLdL e@dB      dC e@dD      dE e@dF      dG e@dH      dI e@dJ      dK e@d;      d; e@d'      d' e@dL      dLZAe+dM        ZB e jB                  edNz   ez         ZC e jB                  ed z   ez   d z   ez   ez         ZD e jB                  dO      ZEdP ZFdWdQZGdTdRZHy)Xr   z|Based on
    https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
    Supports PDF up to 1.4
    Nc                   |r|rd}t        |      || _        || _        || _        || _        d| _        d| _        ||t        ||      x| _        }d| _        |>| j                  |      x| _        }d| _        |st        |d      r|j                  | _        i | _        |r| j                          nsdx| _        | _        t               | _        d | _        t               | _        d | _        i | _        g | _        g | _        d | _        d | _        i | _        t5               | _        d| j6                  _        |r| j;                          y y )Nz4specify buf or f or filename, but not both buf and fFTr   r   )RuntimeErrorfilenamer   r   start_offsetshould_close_bufshould_close_fileopenget_buf_from_filehasattrr   cached_objectsread_pdf_infofile_size_totalfile_size_thisr   rootroot_refinfoinfo_refpage_tree_rootpages
orig_pages	pages_reflast_xref_section_offsettrailer_dictrn   
xref_tablers   seek_end)rY   r
  r   r   r  moder   s          r
   rt   zPdfParser.__init__g  s>   1HCs## ( %!&AIh--DFQ%)D"=!33A66DHs$(D!6 2 !  9::D 4#6	DI DM	DI DM"$DDJ DO!DN,0D) "D'kDO+/(MMO r   c                    | S r:   rL   rX   s    r
   	__enter__zPdfParser.__enter__  s    r   c                $    | j                          y)NF)close)rY   exc_type	exc_value	tracebacks       r
   __exit__zPdfParser.__exit__  s    

r   c                D    | j                          | j                          y r:   )	close_bufr   rX   s    r
   start_writingzPdfParser.start_writing  s    r   c                t    	 | j                   j                          d | _         y # t        $ r
 Y d | _         y w xY wr:   )r   r%  r   rX   s    r
   r+  zPdfParser.close_buf  s9    	HHNN   		s   $ 	77c                    | j                   r| j                          | j                  /| j                  r"| j                  j	                          d | _        y y y r:   )r  r+  r   r  r%  rX   s    r
   r%  zPdfParser.close  sC      NN66$"8"8FFLLNDF #9r   c                X    | j                   j                  dt        j                         y )Nr   )r   seekosSEEK_ENDrX   s    r
   r   zPdfParser.seek_end  s    Ar{{#r   c                :    | j                   j                  d       y )Ns	   %PDF-1.4
)r   r   rX   s    r
   write_headerzPdfParser.write_header  s    ]#r   c                ^    | j                   j                  d| dj                                y )Nz% 
)r   r   r   )rY   r	   s     r
   write_commentzPdfParser.write_comment  s#    r!BZ&&()r   c                   | j                          | j                  | j                  j                               | _        | j                  d      | _        | j                          | j                  | j                  t        d      | j
                         | j                  | j
                  t        d      t        | j                        | j                         | j                  S )Nr      Catalog)TypePages   Pages)r:  CountKids)del_rootnext_object_idr   r   r  r  rewrite_pages	write_objr   rA   r  rX   s    r
   write_catalogzPdfParser.write_catalog  s    ++DFFKKM:,,Q/t}}7:+>dnnUNN"djj/	 	 	
 }}r   c                   g }t        | j                        D ]  \  }}| j                  |   }| j                  |j                  = |j                  |t        d                || j                  vrXi }|j                         D ]  \  }}|||j                         <    | j                  |d<    | j                  di |}t        | j                        D ]  \  }	}
|
|k(  s|| j                  |	<     |D ]X  }|s| j                  |   }|j                  | j                  v r| j                  |j                  = |j                  dd       }|rSZ g | _        y )Ns   ParentParentr:   )r   r  r  r  rV   r   r   r  r   r   r  
write_pager<   )rY   pages_tree_nodes_to_deleteipage_ref	page_infostringified_page_inforw   rx   new_page_refjcur_page_refpages_tree_node_refpages_tree_nodes                r
   rA  zPdfParser.rewrite_pages  se   %'"$T__5 	1KAx++H5I 2 23&--i	8J.KLtzz)$&!'oo/ A
U;@%coo&78A /3nn!(+*4??I3HIL#,TZZ#8 1<8+$0DJJqM1	1& $> 	K%"&"5"56I"J&00DOOC(;(E(EF&5&9&9)T&J#	 &	K r   c                   |r| j                          || _        | j                  r!| j                  d | j                        | _        | j
                  j                  | j                        }t        | j
                        }| j                  |d}| j                  | j                  |d<   | j                  r| j                  |d<   || _	        | j                  j                  dt        t        |            z   d|z  z          y )N)   Root   Size   Prev   Infos   trailer
s   
startxref
%d
%%%%EOF)r?  r  r  rB  r  r  r   r   rA   r  r   r   )rY   new_root_ref
start_xrefnum_entriesr  s        r
   write_xref_and_trailerz PdfParser.write_xref_and_trailer  s    MMO(DM99 NN4;DM__**4662
$//*!%E((4$($A$AL!99$(MML!(2%GL)*+)J67	
r   c                    t        |t              r| j                  |   }d|vrt        d      |d<   d|vr| j                  |d<    | j
                  |g|i |S )Nr:     PagerE  )r   r   r  r   r  rB  )rY   refobjsdict_objs       r
   rF  zPdfParser.write_page  sa    c3**S/C!&w/HV8#!%HXt~~c5D5H55r   c                R   | j                   }| | j                  |j                               }n3|j                         |j                  f| j                  |j
                  <   |j                  t        t        |              |j                  dd       }|t        |      |d<   |r|j                  t        |             |D ]  }|j                  t        |              |3|j                  d       |j                  |       |j                  d       |j                  d       |S )Nstreamr   s   stream
s   
endstream
s   endobj
)r   r@  r   rW   r  rV   r   r   rk   r   rA   r   )rY   r\  r]  r^  r   r`  objs          r
   rB  zPdfParser.write_obj  s    FF;%%affh/C./ffh-GDOOCMM*	'-./h-!$VHXGGHX&' 	#CGGHSM"	#GGK GGFOGG$%	
r   c                    | j                   y | j                  | j                   j                  = | j                  | j                  d   j                  = y )Nr<  )r  r  rV   r  rX   s    r
   r?  zPdfParser.del_root  s@    == OODMM334OODIIh/99:r   c                    t        | d      r| j                         S t        | d      r| j                         S 	 t        j                  | j	                         dt        j
                        S # t        $ r Y yw xY w)N	getbuffergetvaluer   )accessr   )r  rd  re  mmapfilenoACCESS_READ
ValueError)r   s    r
   r  zPdfParser.get_buf_from_file  sd    1k";;= Q
#::<yyQt7G7GHH s   3A. .	A:9A:c                   t        | j                        | _        | j                  | j                  z
  | _        | j                          | j                  d   | _        | j                  j                  dd       | _	        t        | j                  | j                              | _        | j                  t               | _        n)t        | j                  | j                              | _        t        d| j                  v d       t        | j                  d   dk(  d       t        d| j                  v d       t        t        | j                  d   t               d	       | j                  d   | _        | j                  | j"                        | _        | j'                  | j$                        | _        | j(                  d d  | _        y )
NrR  rU     Typez/Type missing in Rootr9  z/Type in Root is not /Catalogr<  z/Pages missing in Rootz+/Pages in Root is not an indirect reference)rA   r   r  r  r  read_trailerr  r  r<   r  r   read_indirectr  r  rP   r   rR   r  r  linearize_page_treer  r  rX   s    r
   r  zPdfParser.read_pdf_info  sc   "488}"22T5F5FF))'2))--gt<D..t}}=>	== 	DI 2 24== ABDIw$))35LMIIg*,.M	
 	x49946NOtyy*,=>9	
 8,"00@--d.A.AB
 **Q-r   c                    	 t        t        | j                  j                               dz   d      }||df| j                  |j
                  <   |S # t        $ r t        dd      }Y 6w xY w)Nr   r   )rR   maxr  r   rj  rV   )rY   r   	references      r
   r@  zPdfParser.next_object_id:  sm    	0)#doo.B.B.D*E*I1MI 4:A;DOOI//0	  	0)!Q/I	0s   0A A)(A)s   [][()<>{}/%]s$   [][()<>{}/%\000\011\012\014\015\040]s   [\000\011\012\014\015\040]s#   [\000\011\012\014\015\0400-9a-fA-F]   *   +s   [\000\011\014\040]*s   [\r\n]+s   trailers   <<(.*>>)s	   startxrefs   ([0-9]+)s   %%EOF   $s	   <<(.*?>>)c                   t        | j                        dz
  }|| j                  k  r| j                  }| j                  j	                  | j                  |      }t        |d       |}|r<|}| j                  j	                  | j                  |j                         dz         }|r<|s|}|j                  d      }t        |j                  d            | _	        | j                  |      | _        t               | _        | j                  | j                         d| j                  v r| j                  | j                  d          y y )N @  ztrailer end not found   r   r   xref_section_offsetrT  )rA   r   r  re_trailer_endsearchrP   startgroupr   r  interpret_trailerr  rn   r  read_xref_tableread_prev_trailer)rY   search_start_offsetm
last_matchtrailer_datas        r
   rm  zPdfParser.read_trailerl  s   !$((me3!2!22"&"3"3&&txx1DEq"9:
J##**488QWWY^DA  Awwqz(+AGGAJ% 22<@#+1N1NOd'''""4#4#4W#=> (r   c                Z   | j                  |      }| j                  j                  | j                  ||dz          }t	        |d       |j                  d      }t	        t        |j                  d            |k(  d       | j                  |      }d|v r| j                  |d          y y )Nry  rw  zprevious trailer not foundr   r   zGxref section offset in previous trailer doesn't match what was expectedrT  )	r  re_trailer_prevr|  r   rP   r~  r   r  r  )rY   rz  trailer_offsetr  r  r  s         r
   r  zPdfParser.read_prev_trailer  s    --BU-V  ''HH^nu&<=
 	q">?wwqz
O22U	
 --l;l"""<#89 #r   s   /([!-$&'*-.0-;=?-Z\\^-z|~]+)(?=r   r   s   >>c           	        i }d}	 | j                   j                  ||      }|sV| j                  j                  ||      }t        |xr |j	                         t        |      k(  dt        ||d        z          nI| j                  |j                  d            }| j                  ||j	                               \  }}|||<   t        d|v xr t        |d   t              d       t        d|v xr t        |d   t              d       |S )Nr   z+name not found in trailer, remaining data: r   rS  z&/Size not in trailer or not an integerrR  z1/Root not in trailer or not an indirect reference)re_namematchre_dict_endrP   endrA   r   r   r~  	get_valuer   r   rR   )r   r  trailerr   r  rw   rx   s          r
   r  zPdfParser.interpret_trailer  s   !!,7AOO)),?&6!%%'S%66A<012
 $$QWWQZ0CMM,@ME6 GCL  	wD:gg.>#D4	
 	wR:gg.>@Q#R?	
 r   s   ([^#]*)(#([0-9a-fA-F]{2}))?c                ^   d}| j                   j                  |      D ]p  }|j                  d      rI||j                  d      t        j	                  |j                  d      j                  d            z   z  }]||j                  d      z  }r |r|j                  d      S t        |      S )Nr      r   r]   zutf-8)re_hashes_in_namefinditerr~  r   fromhexrB   r   )r   rawas_textr   r  s        r
   r   zPdfParser.interpret_name  s    &&//4 	#Awwqz
Y%6%6qwwqz7H7H7T%UUU
"		#
 ;;w'';r   s   null(?=s   true(?=s   false(?=s   ([-+]?[0-9]+)(?=s)   ([-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+))(?=s   \[   ]s   <(s   *)>r   s   ([-+]?[0-9]+)s   R(?=s   obj(?=s	   endobj(?=r   s	   %[^\r\n]*s   )*s   stream\r?\ns   endstream(?=c                   |dk(  ry| j                   j                  ||      }|r|j                         }| j                  j                  ||      }|rt	        t        |j                  d            dkD  d       t	        t        |j                  d            dk\  d       t	        |d u xs? |t        t        |j                  d            t        |j                  d                  k(  d       | j                  ||j                         |dz
        \  }}||d fS | j                  j                  ||      }t	        |d	       ||j                         fS t	        | d
       | j                  j                  ||      }|rt	        t        |j                  d            dkD  d       t	        t        |j                  d            dk\  d       t        t        |j                  d            t        |j                  d                  |j                         fS | j                  j                  ||      }|rf|j                         }i }| j                  j                  ||      }|sd| j                  |||dz
        \  }}||d fS | j                  |||dz
        \  }	}|	||<   ||d fS | j                  j                  ||      }|sd|j                         }| j                  j                  ||      }|r	 t        |d         }
||j                         |j                         |
z    }| j&                  j                  ||j                         |
z         }t	        |d       |j                         }t)        t+        |      |      }||fS t+        |      }||fS | j,                  j                  ||      }|r|j                         }g }| j.                  j                  ||      }|sP| j                  |||dz
        \  }	}|j1                  |	       ||d fS | j.                  j                  ||      }|sP||j                         fS | j2                  j                  ||      }|rd |j                         fS | j4                  j                  ||      }|rd|j                         fS | j6                  j                  ||      }|rd|j                         fS | j8                  j                  ||      }|r9t;        | j=                  |j                  d                  |j                         fS | j>                  j                  ||      }|r*t        |j                  d            |j                         fS | j@                  j                  ||      }|r*tC        |j                  d            |j                         fS | jD                  j                  ||      }|rtG        d |j                  d      D              }tI        |      dz  dk(  r|j1                  tK        d             tF        jM                  |jO                  d            |j                         fS | jP                  j                  ||      }|r | jS                  ||j                               S dtU        |||dz          z   }t%        |      # t        t        t         f$ r&}d|j#                  dd       z  }t%        |      |d }~ww xY w)Nr   )NNr   z<indirect object definition: object ID must be greater than 0r   z;indirect object definition: generation must be non-negativez2indirect object definition different than expected)max_nestingz(indirect object definition end not foundz$indirect object definition not foundz;indirect object reference: object ID must be greater than 0z:indirect object reference: generation must be non-negatives   Lengthz)bad or missing Length in stream dict (%r)zstream end not foundTFc              3  *   K   | ]  }|d v s|  yw)s   0123456789abcdefABCDEFNrL   r   s     r
   r@   z&PdfParser.get_value.<locals>.<genexpr>b  s      #.G)G#s   	   0r]   zunrecognized object:     )+
re_commentr  r  re_indirect_def_startrP   r   r~  rR   r  re_indirect_def_endre_indirect_referencere_dict_startr  re_stream_start	TypeErrorr{   rj  r<   rG   re_stream_endr   r   re_array_startre_array_endr   re_nullre_truere_falser  r   r   re_intre_realr  re_string_hexr   rA   r   r  rB   re_string_litget_literal_stringr   )r   r   r   expect_indirectr  r  objectr   rw   rx   
stream_lenr   r   stream_data
hex_strings                  r
   r  zPdfParser.get_value  sX   !NN  v.UUWF%%++D&9"AGGAJ!#N #AGGAJ1$M #4' G"$S_c!''!*oFGD	 !]]4kTUo]VNFF~t|#''--dF;A"1&PQ1557?"!G	
 %%++D&9"AGGAJ!#M #AGGAJ1$L %S_c!''!*oFOO##D&1UUWFF%%dF3A!mmD&kTUomVV>!4<' #dFVW Xv#s>!4<'OO))$7  UUWF##))$7A5!$VI%6!7J #1557QUUWz-AB%%++D!%%'J2FG&q*@A"76?K@ 6>! !6>!$$T62UUWFF  &&tV4A #dFVW Xve$>!4<'$$**48  1557?"KKdF+= KKdF+= LLtV,!%%'>!KKdF+3--aggaj9:AEEGCCJJT6*qwwqz?AEEG++KKdF+$aeeg--##D&1" #771:# J :"a'!!#d),$$Z%6%6z%BCQUUWLL##D&1))$88%T&6B;-G(HHS!!w "8Z8 5E

!4I C )-14	5s    [ [?![::[?sF   (\\[nrtbf()\\])|(\\[0-9]{1,3})|(\\(\r\n|\r|\n))|(\r\n|\r|\n)|(\()|(\))   nr      r      t   	   b      f   r   c                ,   d}t               }| j                  j                  ||      D ]\  }|j                  |||j	                                 |j                  d      r1|j                  | j                  |j                  d      d             n|j                  d      r.|j                  t        |j                  d      dd  d             n|j                  d      rn|j                  d      r|j                  d       nq|j                  d      r|j                  d	       |dz  }nI|j                  d
      r8|dk(  rt        |      |j                         fc S |j                  d       |dz  }|j                         }_ d}t        |      )Nr   r   r      r     r      r      r   zunfinished literal string)r   re_lit_str_tokenr  r   r}  r~  escaped_charsr   r   r   r  rG   )r   r   r   nesting_depthr   r  r   s          r
   r  zPdfParser.get_literal_string  sD   %%..tV< 	AMM$v	23wwqzc//
1>?c!''!*QR.!45e$d#" A% =!%%'11d#"UUWF%	& *S!!r   s   xrefs+   ([0-9]{10}) ([0-9]{5}) ([fn])( \r| \n|\r\n)c                &   d}| j                   j                  | j                  || j                  z         }t	        |d       |j                         }	 | j                  j                  | j                  |      }|st	        |d       	 |S d}|j                         }t        |j                  d            }t        |j                  d            }t        |||z         D ]  }| j                  j                  | j                  |      }t	        |d       |j                         }|j                  d      d	k(  }|r\t        |j                  d            }	t        |j                  d            |	f}
|| j                  vs|
| j                  |<    @)
NFzxref section start not foundTzxref subsection start not foundr   r   zxref entry not foundr  r  )re_xref_section_startr  r   r  rP   r  re_xref_subsection_startr   r~  r   re_xref_entryr  )rY   rz  subsection_foundr  r   first_objectnum_objectsrH  is_freerW   	new_entrys              r
   r  zPdfParser.read_xref_table  sd    &&,,HH)D,=,==
 	q"@A--33DHHfEA&$&G    $UUWFqwwqz?Laggaj/K<)CD 	7&&,,TXXv>&q*@A''!*,!$QWWQZJ!$QWWQZ* =I/-6*	7 r   c                    | j                   |d      \  }}t        ||d   k(  d|d    d|d    d| d|        | j                  | j                  || j                  z   t        | |      d   }|| j                  |<   |S )Nr   r   zexpected to find generation z for object ID z) in xref table, instead found generation z at offset )r  r  )r  rP   r  r   r  rR   r  )rY   r\  r  r   rW   rx   s         r
   rn  zPdfParser.read_indirect  s    !__SV4
#a& *3q6(/#a& J//9l+fXO	

 HHT&&&-s3#	  

  $)C r   c                    || j                   }t        |d   dk(  d       g }|d   D ]N  }| j                  |      }|d   dk(  r|j                  |       .|j	                  | j                  |             P |S )Nrl  r<  z%/Type of page tree node is not /Pagess   Kidsr[  )node)r  rP   rn  r   r   ro  )rY   r  r  kid
kid_objects        r
   ro  zPdfParser.linearize_page_tree  s    <&&DMX%'N	
 = 	HC++C0J'"g-S!T55:5FG	H r   )NNNr   rbr:   )F)Nr   )r   )IrH   rI   rJ   rK   rt   r#  r)  r,  r+  r%  r   r4  r7  rC  rA  rY  rF  rB  r?  staticmethodr  r  r@  	delimiterdelimiter_or_ws
whitespacewhitespace_or_hexwhitespace_optionalwhitespace_mandatorywhitespace_optional_no_nlnewline_onlynewlinerecompileDOTALLr{  r  rm  r  re_whitespace_optionalr  r  r  r   r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  rn  ro  rL   r   r
   r   r   a  sp   
$L$$*:
(6*; 	 	(8 !I>O/J?$t+%, 7L',69RRGRZZ
	
	 	 		
 	 	 	 	 		 
	 	 			N !bjj
	
	 	 		
 	 	 	 	 		 
	 			O?*: (RZZ(;<bjj
-	.
	 	G BJJ2V;<M"**069<OOPK 4 #

#BC
 
 bjj,{:_LuTUGbjj,{:_LuTUGrzz-<NQVVWHRZZ22_DuLF bjj
7	8
	 	G  RZZ 3f <=N2::1E9:LBJJf$'887BM BJJ2V;<M&BJJ
	
	 	 		
 	 	 		 'BJJ
	
	 	 		
 	 	 		 %"**m+o=E ##m3g=FJ !bjj!4!FGOBJJ..@5HM x" x"t "rzzRee 	e 	e	
 	e 	d 	d 	u 	D	5 	D	5 	D	5 	D	5 	D	5 	D	4 	D	4  	E
E!M& " "2 'BJJ':X'E'OP)rzz
	
	 	 		
 	  BJJNOM< r   r   )
__future__r   r   r   r   rg  r1  r  r   r   r   r;   rE   r	  rG   rP   
namedtuplerR   rk   rn   r   r  r   r   r   r   r   r   r   rL   r   r
   <module>r     s   "     	 	  
7*(*(* 	(* 	(	*
 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(*  	(!*" 	(#*$ 	(%*& 	('*( 	()** 	(+*, 	(-*. 	(/*0 	(1*2 	(3*4 	(5*6 	(7*8 	(9*: 	(;*< 	(=*> 	(?*@ 	(A*B 	(C*D 	(E*F 





S*ZJ	\ 	,
7K3k<5PQ7.9) 9
Y Yx$ $NDt D
-k"" -`B B+ +*:E
 E
r   