
    }f~              	         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 dl
mZmZmZmZm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/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPZdQ Z G dR dSe          ZdT Z G dU dVe          Z G dW dXe          Z G dY dZe          Z G d[ d\          Z G d] d^          Z G d_ d`ee                   Zerej        eeef         ef         Znej        Z G da dbe          Z  G dc dd          Z! G de df          Z"dg Z# G dh di          Z$dS )j    )annotationsN)TYPE_CHECKINGAnyList
NamedTupleUnionc                F    t           j        |                     d          z   S )N	utf_16_be)codecsBOM_UTF16_BEencode)ss    F/var/www/rtfcompare/venv/lib/python3.11/site-packages/PIL/PdfParser.pyencode_textr      s    +!6!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    r4| t          t          j                  d                              d          S d                    d | D                       S )Nr
    c              3  f   K   | ],}t                               |t          |                    V  -d S N)PDFDocEncodinggetchr).0bytes     r   	<genexpr>zdecode_text.<locals>.<genexpr>E   s8      IIt~))$D		::IIIIIIr   )lenr   r   decodejoin)bs    r   decode_textrJ   A   sq    	#3v"##	#$(;;;V())++,33K@@@wwIIqIIIIIIr   c                      e Zd ZdZdS )PdfFormatErrorz\An error that probably indicates a syntactic or semantic error in the
    PDF file structureN)__name__
__module____qualname____doc__ r   r   rL   rL   H   s          	Dr   rL   c                (    | st          |          d S r?   )rL   )	conditionerror_messages     r   check_format_conditionrU   O   s"     ,]+++, ,r   c                  $    e Zd ZU ded<   ded<   dS )IndirectReferenceTupleint	object_id
generationN)rM   rN   rO   __annotations__rQ   r   r   rW   rW   T   s"         NNNOOOOOr   rW   c                  ,    e Zd Zd Zd Zd Zd Zd ZdS )IndirectReferencec                &    | j          d| j         dS )N z RrY   rZ   selfs    r   __str__zIndirectReference.__str__Z   s    .664?6666r   c                P    |                                                      d          S Nus-ascii)rc   r   ra   s    r   	__bytes__zIndirectReference.__bytes__]   s    ||~~$$Z000r   c                ^    |j         | j         u o|j        | j        k    o|j        | j        k    S r?   )	__class__rY   rZ   rb   others     r   __eq__zIndirectReference.__eq__`   s6    Ot~- 44>14 DO3	
r   c                    | |k     S r?   rQ   rj   s     r   __ne__zIndirectReference.__ne__g   s    EM""r   c                8    t          | j        | j        f          S r?   )hashrY   rZ   ra   s    r   __hash__zIndirectReference.__hash__j   s    T^T_5666r   N)rM   rN   rO   rc   rg   rl   rn   rq   rQ   r   r   r]   r]   Y   s_        7 7 71 1 1
 
 
# # #7 7 7 7 7r   r]   c                      e Zd Zd ZdS )IndirectObjectDefc                &    | j          d| j         dS )Nr_   z objr`   ra   s    r   rc   zIndirectObjectDef.__str__o   s    .884?8888r   N)rM   rN   rO   rc   rQ   r   r   rs   rs   n   s#        9 9 9 9 9r   rs   c                  >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
	XrefTablec                B    i | _         i | _        ddi| _        d| _        d S )Nr   i   F)existing_entriesnew_entriesdeleted_entriesreading_finishedra   s    r   __init__zXrefTable.__init__t   s,     " !5z %r   c                d    | j         r|| j        |<   n
|| j        |<   || j        v r
| j        |= d S d S r?   )r{   ry   rx   rz   rb   keyvalues      r   __setitem__zXrefTable.__setitem__z   sQ      	/$)DS!!).D!#&$&&&$S))) '&r   c                X    	 | j         |         S # t          $ r | j        |         cY S w xY wr?   )ry   KeyErrorrx   rb   r   s     r   __getitem__zXrefTable.__getitem__   sD    	.#C(( 	. 	. 	.(----	.s    ))c                6   || j         v r*| j         |         d         dz   }| j         |= || j        |<   d S || j        v r"| j        |         d         dz   }|| j        |<   d S || j        v r| j        |         }d S dt          |          z   dz   }t	          |          )N   z
object ID z+ cannot be deleted because it doesn't exist)ry   rz   rx   str
IndexError)rb   r   rZ   msgs       r   __delitem__zXrefTable.__delitem__   s    $""")#.q1A5J %(2D %%%D))).s3A6:J(2D %%%D(((-c2JJJ s3xx'*WW  S//!r   c                &    || j         v p|| j        v S r?   )rx   ry   r   s     r   __contains__zXrefTable.__contains__   s    d++Fsd6F/FFr   c                   t          t          | j                                                  t          | j                                                  z  t          | j                                                  z            S r?   )rF   setrx   keysry   rz   ra   s    r   __len__zXrefTable.__len__   sl    %**,,--$"''))**+$&++--../
 
 	
r   c                    t          | j                                                  t          | j                                                  z
  t          | j                                                  z  S r?   )r   rx   r   rz   ry   ra   s    r   r   zXrefTable.keys   s\    %**,,--D4H4M4M4O4O0P0PP %%''(() 	)r   c                   t          t          | j                                                  t          | j                                                  z            }t          t          | j                                                            }|                                }|                    d           |rd }t          |          D ](\  }}|	|dz   |k    r|}|d |         }||d          } n|}d }|                    d|d         t          |          fz             |D ]}	|	| j        v r$|                    d| j        |	         z             /|	                    d          }
t          |	|
k    d|	 d|
            	 |d         }n# t          $ r d}Y nw xY w|                    d|| j        |	         fz             ||S )	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   ry   r   rz   tellwrite	enumeraterF   poprU   r   )rb   fr   deleted_keys	startxrefprevindexr   contiguous_keysrY   this_deleted_object_idnext_in_linked_lists               r   r   zXrefTable.write   s'   c$*//1122S9M9R9R9T9T5U5UUVVc$"6";";"="=>>??FFHH			 	D'oo 	 	
s<4!8s??DD&*6E6lO<DE"&GGJ/!"4c/6J6J!KKLLL,  	 000GG.1A)1LLMMMM-9-=-=a-@-@**!%;;BY B B)?B B  
0.:1o++% 0 0 0./+++0GG*.0DY0OPQ   9  	@ s   FFFN)rM   rN   rO   r|   r   r   r   r   r   r   r   rQ   r   r   rv   rv   s   s        & & &* * *. . ." " " G G G
 
 
) ) )
% % % % %r   rv   c                      e Zd Zd Zd Zd Zd Zd Zed             Z	 e
 edd                    d	 d
D             z
  Zd ZdS )PdfNamec                    t          |t                    r|j        | _        d S t          |t                    r	|| _        d S |                    d          | _        d S re   )
isinstancer   namebytesr   )rb   r   s     r   r|   zPdfName.__init__   sU    dG$$ 	0	DIIIe$$ 	0DIIIJ//DIIIr   c                6    | j                             d          S re   )r   rG   ra   s    r   name_as_strzPdfName.name_as_str   s    y
+++r   c                b    t          |t                    o|j        | j        k    p
|| j        k    S r?   )r   r   r   rj   s     r   rl   zPdfName.__eq__   s1    ug&&B5:+B di	 r   c                *    t          | j                  S r?   )rp   r   ra   s    r   rq   zPdfName.__hash__   s    DIr   c                2    dt          | j                   dS )NzPdfName())reprr   ra   s    r   __repr__zPdfName.__repr__   s    ,$ty//,,,,r   c                H     | t                               |                    S r?   )	PdfParserinterpret_name)clsdatas     r   from_pdf_streamzPdfName.from_pdf_stream   s     s9++D11222r   !      c                ,    h | ]}t          |          S rQ   )ord)rC   cs     r   	<setcomp>zPdfName.<setcomp>   s    *I*I*Ia3q66*I*I*Ir   z#%/()<>[]{}c                    t          d          }| j        D ]9}|| j        v r|                    |           !|                    d|z             :t          |          S )N   /s   #%02X)	bytearrayr   allowed_charsappendextendr   )rb   resultrI   s      r   rg   zPdfName.__bytes__   sf    4 	, 	,AD&&&a    hl++++V}}r   N)rM   rN   rO   r|   r   rl   rq   r   classmethodr   r   ranger   rg   rQ   r   r   r   r      s        0 0 0, , ,     
  - - - 3 3 [3 Cb#''*I*I=*I*I*IIM    r   r   c                      e Zd Zd ZdS )PdfArrayc                L    dd                     d | D                       z   dz   S )Ns   [     c              3  4   K   | ]}t          |          V  d S r?   )pdf_repr)rC   xs     r   rE   z%PdfArray.__bytes__.<locals>.<genexpr>   s(       ; ;! ; ; ; ; ; ;r   s    ])rH   ra   s    r   rg   zPdfArray.__bytes__   s-    tyy ; ;d ; ; ;;;;eCCr   N)rM   rN   rO   rg   rQ   r   r   r   r      s(        D D D D Dr   r   c                       e Zd Zd Zd Zd ZdS )PdfDictc                    |dk    r#t           j                            | ||           d S || |                    d          <   d S )Nr   rf   )collectionsUserDict__setattr__r   r~   s      r   r   zPdfDict.__setattr__  sH    &== ,,T3>>>>>+0DJ''(((r   c                   	 | |                     d                   }n"# t          $ r}t          |          |d }~ww xY wt          |t                    rt          |          }|                    d          r|                    d          r
|dd          }d}t          |          dk    rO|d         }t          |dd                   d	z  }t          |          d
k    r|t          |dd
                   z  }dd t          |          dz
           }t          j        |d t          |          dz            |          }|dv r9|d	z  }|dk    r|dz  }t          j        t          j        |          |z             }|S )Nrf   DatezD:   Z         <         z%Y%m%d%H%M%S)+-r   )r   r   AttributeErrorr   r   rJ   endswith
startswithrF   rX   timestrptimegmtimecalendartimegm)rb   r   r   erelationshipoffsetformats          r   __getattr__zPdfDict.__getattr__  s   	-J//0EE 	- 	- 	- %%1,	-eU## 	'&&E<< 	E%% "abb	L5zzB$RyU2b5\**R/u::??c%2,///F#$4c%jj1n$45FM%(9#f++/(9":FCCEz))"3&&bLFHOE$:$:V$CDDs    
=8=c                   t          d          }|                                 D ]\  }}|t          |          }|                    d           |                    t	          t          |                               |                    d           |                    |           |                    d           t	          |          S )N   <<   
r   s   
>>)r   itemsr   r   r   r   )rb   outr   r   s       r   rg   zPdfDict.__bytes__#  s    **,, 	 	JC}UOOEJJuJJuWS\\**+++JJtJJu

7Szzr   N)rM   rN   rO   r   r   rg   rQ   r   r   r   r     sA        1 1 1  6    r   r   c                      e Zd Zd Zd ZdS )	PdfBinaryc                    || _         d S r?   )r   )rb   r   s     r   r|   zPdfBinary.__init__2  s    			r   c                P    dd                     d | j        D                       z  S )Ns   <%s>r   c              3      K   | ]	}d |z  V  
dS )s   %02XNrQ   rC   rI   s     r   rE   z&PdfBinary.__bytes__.<locals>.<genexpr>6  s&      !A!A!'A+!A!A!A!A!A!Ar   )rH   r   ra   s    r   rg   zPdfBinary.__bytes__5  s*    !A!Aty!A!A!AAAAAr   N)rM   rN   rO   r|   rg   rQ   r   r   r   r   1  s7          B B B B Br   r   c                      e Zd Zd Zd ZdS )	PdfStreamc                "    || _         || _        d S r?   )
dictionarybuf)rb   r   r   s      r   r|   zPdfStream.__init__:  s    $r   c                T   	 | j         j        }n# t          $ r
 | j        cY S w xY w|dk    rR	 | j         j        }n# t          $ r | j         j        }Y nw xY wt          j        | j        t          |                    S dt          | j         j                   d}t          |          )Ns   FlateDecode)bufsizezstream filter z unknown/unsupported)r   Filterr   r   DLLengthzlib
decompressrX   r   NotImplementedError)rb   filterexpected_lengthr   s       r   rG   zPdfStream.decode>  s    	_+FF 	 	 	8OOO	^##9"&/"4! 9 9 9"&/"89?48S5I5IJJJJU4(>#?#?UUUC%c***s    ##: AAN)rM   rN   rO   r|   rG   rQ   r   r   r   r   9  s2          + + + + +r   r   c                   | du rdS | du rdS | dS t          | t          t          t          t          f          rt          |           S t          | t          t          f          r"t          |           	                    d          S t          | t          j                  r.dt          j        d|           	                    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
                    rJ|                     d
d          } |                     dd          } |                     d	d          } d| z   d	z   S t          |           S )NTs   trueFs   falses   nullrf   s   (D:z%Y%m%d%H%M%SZ   )   \s   \\   (   \(s   \))r   r   r   r   r   r   rX   floatr   r   r   struct_timestrftimedictlistr   r   replace)r   s    r   r   r   N  s   Dyyw	
ex	
w	A(I>	?	? Qxx	AU|	$	$ 1vv}}Z(((	At'	(	( oq99@@LLLtSS	At		 WQZZ   	At		 Xa[[!!!	As		 	A'''	Au		 IIeW%%IIdF##IIdF##ax$Qxxr   c                  z   e Zd ZdZdWdZd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zd Zd ZdXdZd Zd Zd Zed             Zd ZdXd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 j!        edz   ez   d z   ez   d!z   ez   d"z   ez   d#z   ez   d$z   e j"                  Z# e j!        edz   ez   d%z   ez   d!z   ez   d"z   ez   d#z   ez   e j"                  Z$d& Z%d' Z& e j!        e          Z' e j!        ed(z   ez   d)z             Z( e j!        ed*z             Z) e j!        ed+z   ez             Z*e+d,             Z, e j!        d-          Z-e+dYd/            Z. e j!        ed0z   ez   d)z             Z/ e j!        ed1z   ez   d)z             Z0 e j!        ed2z   ez   d)z             Z1 e j!        ed3z   ez   d)z             Z2 e j!        ed4z   ez   d)z             Z3 e j!        ed5z             Z4 e j!        ed6z             Z5 e j!        ed7z   ez   d8z             Z6 e j!        ed9z             Z7 e j!        ed:z   ez   d:z   ez   d;z   ez   d)z             Z8 e j!        ed:z   ez   d:z   ez   d<z   ez   d)z             Z9 e j!        ed=z   ez   d)z             Z: e j!        d>ez   d?z   ez   d@z             Z; e j!        edAz             Z< e j!        edBz   ez   d)z             Z=e+dZdD            Z> e j!        dE          Z?i dFdGdHdIdJdKdLdMdNdOd>d>d)d)dPdP e@dF          dG e@dH          dI e@dJ          dK e@dL          dM e@dN          dO e@d>          d> e@d)          d) e@dP          dPZAe+dQ             ZB e j!        edRz   ez             ZC e j!        ed"z   ez   d"z   ez   ez             ZD e j!        dS          ZEdT ZFd[dUZGdXdVZHdS )\r   z|Based on
    https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
    Supports PDF up to 1.4
    Nr   rbc                   |r|rd}t          |          || _        || _        || _        || _        d| _        d| _        | |t          ||          x| _        }d| _        |A|                     |          x| _        }d| _        |st          |d          r|j
        | _        i | _        |r|                                  ndx| _        | _        t                      | _        d | _        t                      | _        d | _        i | _        g | _        g | _        d | _        d | _        i | _        t5                      | _        d| j        _        |r|                                  d S d S )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_dictrv   
xref_tabler{   seek_end)rb   r  r   r   r  moder   s          r   r|   zPdfParser.__init__q  s    	$1 	$HCs### ( %!&AIh---DFQ%)D"=!33A666DHs$(D! '6 2 2 ' !  	*    9::D 4#6		DI DM		DI DM"$DDJ DO!DN,0D) "D'kkDO+/( 	MMOOOOO	 	r   c                    | S r?   rQ   ra   s    r   	__enter__zPdfParser.__enter__  s    r   c                .    |                                   dS )NF)close)rb   exc_type	exc_value	tracebacks       r   __exit__zPdfParser.__exit__  s    

ur   c                V    |                                   |                                  d S r?   )	close_bufr*  ra   s    r   start_writingzPdfParser.start_writing  s#    r   c                j    	 | j                                          n# t          $ r Y nw xY wd | _         d S r?   )r   r/  r   ra   s    r   r5  zPdfParser.close_buf  sG    	HNN 	 	 	D	s    
))c                    | j         r|                                  | j        )| j        r$| j                                         d | _        d S d S d S r?   )r  r5  r   r  r/  ra   s    r   r/  zPdfParser.close  sU      	NN6$"8FLLNNNDFFF r   c                P    | j                             dt          j                   d S )Nr   )r   seekosSEEK_ENDra   s    r   r*  zPdfParser.seek_end  s     Ar{#####r   c                :    | j                             d           d S )Ns	   %PDF-1.4
)r   r   ra   s    r   write_headerzPdfParser.write_header  s    ]#####r   c                f    | j                             d| d                                           d S )Nz% 
)r   r   r   )rb   r   s     r   write_commentzPdfParser.write_comment  s0    Z!ZZZ&&(()))))r   c                   |                                   |                     | j                                                  | _        |                     d          | _        |                                  |                     | j        t          d          | j                   |                     | j        t          d          t          | j
                  | j
                   | j        S )Nr      Catalog)TypePages   Pages)rD  CountKids)del_rootnext_object_idr   r   r   r&  rewrite_pages	write_objr   rF   r$  ra   s    r   write_catalogzPdfParser.write_catalog  s    ++DFKKMM::,,Q//t}7:+>+>dnUUUN""dj//	 	 	
 	
 	
 }r   c                Z   g }t          | j                  D ]\  }}| j        |         }| j        |j        = |                    |t          d                              || j        vrQi }|                                D ]\  }}|||	                                <   | j
        |d<    | j        di |}t          | j                  D ]\  }	}
|
|k    r
|| j        |	<   |D ]D}|r@| j        |         }|j        | j        v r| j        |j        = |                    dd           }|@Eg | _        d S )Ns   ParentParentr?   )r   r%  r  r)  rY   r   r   r$  r   r   r&  
write_pagerA   )rb   pages_tree_nodes_to_deleteipage_ref	page_infostringified_page_infor   r   new_page_refjcur_page_refpages_tree_node_refpages_tree_nodes                r   rK  zPdfParser.rewrite_pages  s   %'"$T_55 	1 	1KAx+H5I 23&--i	8J8J.KLLLtz))$&!'oo// A A
U;@%coo&7&788.2n!(+*4?II3HIIL#,TZ#8#8 1 1<8++$0DJqM1
 $> 	K 	K% K"&"56I"J&0DOCC(;(EF&5&9&9)T&J&J#	 & K
 r   c                   |r|                                   || _        | j        r |                     d | j                  | _        | j                            | j                  }t          | j                  }| j        |d}| j	        
| j	        |d<   | j        r
| j        |d<   || _	        | j                            dt          t          |                    z   d|z  z              d S )N)   Root   Size   Prev   Infos   trailer
s   
startxref
%d
%%%%EOF)rI  r   r!  rL  r"  r)  r   r   rF   r'  r   r   )rb   new_root_ref
start_xrefnum_entriesr(  s        r   write_xref_and_trailerz PdfParser.write_xref_and_trailer  s     	)MMOOO(DM9 	< NN4;;DM_**4622
$/**!%EE(4$($AL!9 	2$(ML!(2%GL))**+)J67	
 	
 	
 	
 	
r   c                    t          |t                    r| j        |         }d|vrt          d          |d<   d|vr
| j        |d<    | j        |g|R i |S )NrD     PagerO  )r   rX   r$  r   r&  rL  )rb   refobjsdict_objs       r   rP  zPdfParser.write_page  su    c3 	"*S/C!!&w//HV8##!%HXt~c5D555H555r   c                   | j         }|(|                     |                                          }n(|                                |j        f| j        |j        <   |                    t          t          |                      |	                    dd           }|t          |          |d<   |r"|                    t          |                     |D ]$}|                    t          |                     %|?|                    d           |                    |           |                    d           |                    d           |S )Nstreamr   s   stream
s   
endstream
s   endobj
)r   rJ  r   rZ   r)  rY   r   r   rs   r   rF   r   )rb   rf  rg  rh  r   rj  objs          r   rL  zPdfParser.write_obj  s.   F;%%affhh//CC./ffhh-GDOCM*	'-..///h--!$VHX 	(GGHX&&''' 	# 	#CGGHSMM""""GGK   GGFOOOGG$%%%	
r   c                l    | j         d S | j        | j         j        = | j        | j        d         j        = d S )NrF  )r   r)  rY   r  ra   s    r   rI  zPdfParser.del_root  s8    = FODM34ODIh/9:::r   c                   t          | d          r|                                 S t          | d          r|                                 S 	 t          j        |                                 dt          j                  S # t          $ r Y dS w xY w)N	getbuffergetvaluer   )accessr   )r  rn  ro  mmapfilenoACCESS_READ
ValueError)r   s    r   r  zPdfParser.get_buf_from_file  s    1k"" 	;;== Q
## 	::<<yQt7GHHHH   sss   
2A= =
B
Bc                   t          | j                  | _        | j        | j        z
  | _        |                                  | j        d         | _        | j                            dd           | _	        t          |                     | j                            | _        | j	        t                      | _        n,t          |                     | 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        d d          | _        d S )
Nr\  r_     Typez/Type missing in RootrC  z/Type in Root is not /CatalogrF  z/Pages missing in Rootz+/Pages in Root is not an indirect reference)rF   r   r  r  r  read_trailerr(  r   rA   r"  r   read_indirectr  r!  rU   r   r]   r&  r#  linearize_page_treer$  r%  ra   s    r   r  zPdfParser.read_pdf_info(  s   "48}}"2T5FF)'2)--gt<<D..t}==>>	= 		DII 2 24= A ABBDIw$)35LMMMIg*,.M	
 	
 	
 	x4946NOOOty*,=>>9	
 	
 	
 8,"00@@--d.ABB
 *QQQ-r   c                    	 t          t          | j                                                  dz   d          }n # t          $ r t          dd          }Y nw xY w||df| j        |j        <   |S )Nr   r   )r]   maxr)  r   rt  rY   )rb   r   	references      r   rJ  zPdfParser.next_object_idD  s    	0)#do.B.B.D.D*E*E*I1MMII 	0 	0 	0)!Q//III	04:A;DOI/0s   7: 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        |          }t          |d           |}|r9|}| j                            | j        |                                dz             }|9|s|}|                    d          }t          |                    d                    | _	        | 
                    |          | _        t                      | _        |                     | j	                   d| j        v r"|                     | j        d                    d S d S )N @  ztrailer end not found   r   r   xref_section_offsetr^  )rF   r   r  re_trailer_endsearchrU   startgrouprX   r'  interpret_trailerr(  rv   r)  read_xref_tableread_prev_trailer)rb   search_start_offsetm
last_matchtrailer_datas        r   rw  zPdfParser.read_trailerv  sE   !$(mme3!222"&"3&&tx1DEEq"9:::
 	EJ#**48QWWYY^DDA  	E  	Awwqzz(+AGGAJJ% 22<@@#++1NOOOd'''""4#4W#=>>>>> ('r   c                   |                      |          }| j                            | j        ||dz                      }t	          |d           |                    d          }t	          t          |                    d                    |k    d           |                     |          }d|v r|                     |d                    d S d S )Nr  r  zprevious trailer not foundr   r   zGxref section offset in previous trailer doesn't match what was expectedr^  )	r  re_trailer_prevr  r   rU   r  rX   r  r  )rb   r  trailer_offsetr  r  r(  s         r   r  zPdfParser.read_prev_trailer  s    --BU-VV ''H^nu&<<=
 
 	q">???wwqzz

OO22U	
 	
 	
 --l;;l""""<#899999 #"r   s   /([!-$&'*-.0-;=?-Z\\^-z|~]+)(?=r  r   s   >>c           	        i }d}	 | j                             ||          }|si| j                            ||          }t          |o$|                                t          |          k    dt          ||d                    z              nY|                     |                    d                    }| 	                    ||                                          \  }}|||<   t          d|v ot          |d         t                    d           t          d|v ot          |d         t                    d           |S )	Nr   Tz+name not found in trailer, remaining data: r   r]  z&/Size not in trailer or not an integerr\  z1/Root not in trailer or not an indirect reference)re_namematchre_dict_endrU   endrF   r   r   r  	get_valuer   rX   r]   )r   r  trailerr   r  r   r   s          r   r  zPdfParser.interpret_trailer  sO   	!!!,77A O)),??&6!%%''S%6%66A<0112  
 $$QWWQZZ00CMM,@@ME6 GCL	! 	wD:gg.>#D#D4	
 	
 	
 	wR:gg.>@Q#R#R?	
 	
 	
 r   s   ([^#]*)(#([0-9a-fA-F]{2}))?Fc                   d}| j                             |          D ]}|                    d          rZ||                    d          t                              |                    d                              d                    z   z  }q||                    d          z  }|r|                    d          S t          |          S )Nr      r   rf   zutf-8)re_hashes_in_namefinditerr  r   fromhexrG   r   )r   rawas_textr   r  s        r   r   zPdfParser.interpret_name  s    &//44 	# 	#Awwqzz #

Y%6%6qwwqzz7H7H7T7T%U%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(?=r   c                Z   |dk    rdS | j                             ||          }|r|                                }| j                            ||          }|rEt	          t          |                    d                    dk    d           t	          t          |                    d                    dk    d           t	          |d u pS|t          t          |                    d                    t          |                    d                              k    d           |                     ||                                |dz
            \  }}||d fS | j	                            ||          }t	          |d	           ||                                fS t	          | d
           | j
                            ||          }|rt	          t          |                    d                    dk    d           t	          t          |                    d                    dk    d           t          t          |                    d                    t          |                    d                              |                                fS | j                            ||          }|r|                                }i }| j                            ||          }|sj|                     |||dz
            \  }}||d fS |                     |||dz
            \  }	}|	||<   ||d fS | j                            ||          }|j|                                }| j                            ||          }|r	 t          |d                   }
nH# t          t          t           f$ r.}d|                    dd           z  }t%          |          |d }~ww xY w||                                |                                |
z            }| j                            ||                                |
z             }t	          |d           |                                }t)          t+          |          |          }nt+          |          }||fS | j                            ||          }|r|                                }g }| j                            ||          }|sV|                     |||dz
            \  }	}|                    |	           ||d fS | j                            ||          }|V||                                fS | j                            ||          }|rd |                                fS | j                            ||          }|rd|                                fS | j                            ||          }|rd|                                fS | j                            ||          }|rIt;          |                     |                    d                              |                                fS | j                            ||          }|r6t          |                    d                    |                                fS | j                             ||          }|r6tC          |                    d                    |                                fS | j"                            ||          }|rtG          d |                    d          D                       }tI          |          dz  dk    r"|                    tK          d                     tF          &                    |'                    d                    |                                fS | j(                            ||          }|r(| )                    ||                                          S dtU          |||dz                      z   }t%          |          )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 |V  dS )s   0123456789abcdefABCDEFNrQ   r   s     r   rE   z&PdfParser.get_value.<locals>.<genexpr>l  s6       # #.G)G)G)G)G)G)G# #r      0rf   zunrecognized object:     )+
re_commentr  r  re_indirect_def_startrU   rX   r  r]   r  re_indirect_def_endre_indirect_referencere_dict_startr  re_stream_start	TypeErrorr   rt  rA   rL   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   rF   r   r  rG   re_string_litget_literal_stringr   )r   r   r   expect_indirectr  r  objectr   r   r   
stream_lenr   r   stream_data
hex_strings                  r   r  zPdfParser.get_value  s   !:N  v.. 	UUWWF%++D&99 	#"AGGAJJ!#N   #AGGAJJ1$M   #4' G"$S__c!''!**ooFFGD	   !]]4kTUo]VVNFF~t|#'--dF;;A"1&PQQQ15577?"!G	
 	
 	
 %++D&99 		P"AGGAJJ!#M   #AGGAJJ1$L   %S__c!''!**ooFFOO##D&11 	"UUWWFF%%dF33A 8!mmD&kTUomVVV>!4<' #dFVW X Xv#s>!4<'O))$77  8 UUWWF#))$77A )5!$VI%6!7!7JJ!8Z8 5 5 5E

!4I I C )--14	5
 #15577QUUWWz-A#AB%++D!%%''J2FGG&q*@AAA"76??K@@ 6>!$$T622 
	#UUWWFF &&tV44A 9 #dFVW X Xve$$$>!4<'$**488  9 15577?"KdF++ 	!= KdF++ 	!= LtV,, 	"!%%''>!KdF++ 	D3--aggajj99::AEEGGCCJT6** 	,qwwqzz??AEEGG++KdF++ 	.$$aeegg--##D&11 	M" # #771::# # #  J :"a''!!#d)),,,$$Z%6%6z%B%BCCQUUWWLL##D&11 	9))$888%T&6B;2F-G(H(HHS!!!s   N O 2)OO sF   (\\[nrtbf()\\])|(\\[0-9]{1,3})|(\\(\r\n|\r|\n))|(\r\n|\r|\n)|(\()|(\))   nr      r      t   	   b      f   r  c                   d}t                      }| j                            ||          D ]}|                    |||                                                    |                    d          r;|                    | j        |                    d          d                             n|                    d          r?|                    t          |                    d          dd          d                     n|                    d          rn|                    d          r|                    d           n|                    d          r|                    d	           |dz  }nZ|                    d
          rE|dk    r%t          |          |
                                fc S |                    d           |dz  }|
                                }d}t          |          )Nr   r   r      r     r      r     r  zunfinished literal string)r   re_lit_str_tokenr  r   r  r  escaped_charsr   rX   r   r  rL   )r   r   r   nesting_depthr   r  r   s          r   r  zPdfParser.get_literal_string  s   %..tV<< 	 	AMM$v		12333wwqzz #c/

1>???? #c!''!**QRR.!445555 # 	#e$$$$ #d###" # A%% ==!%%''1111d###"UUWWFF)S!!!r   s   xrefs+   ([0-9]{10}) ([0-9]{5}) ([fn])( \r| \n|\r\n)c                `   d}| j                             | j        || j        z             }t	          |d           |                                }	 | j                            | j        |          }|st	          |d           n*d}|                                }t          |                    d                    }t          |                    d                    }t          |||z             D ]}| j
                            | j        |          }t	          |d           |                                }|                    d          d	k    }|sYt          |                    d                    }	t          |                    d                    |	f}
|| j        vr
|
| j        |<   ^|S )
NFzxref section start not foundTzxref subsection start not foundr   r   zxref entry not foundr  r  )re_xref_section_startr  r   r  rU   r  re_xref_subsection_startrX   r  r   re_xref_entryr)  )rb   r  subsection_foundr  r   first_objectnum_objectsrR  is_freerZ   	new_entrys              r   r  zPdfParser.read_xref_table  s    &,,H)D,==
 
 	q"@AAA	7-33DHfEEA &$&G   #UUWWFqwwqzz??Laggajj//K<)CDD 	7 	7&,,TXv>>&q*@AAA''!**, 7!$QWWQZZJ!$QWWQZZ* =I//-6*)	7* r   c                   | j         |d                  \  }}t          ||d         k    d|d          d|d          d| d|            |                     | 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)  rU   r  r   r  r]   r  )rb   rf  r  r   rZ   r   s         r   rx  zPdfParser.read_indirect  s    !_SV4
#a& O3q6 O O#a& O O/9O OFLO O	
 	
 	

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

  $)C r   c                &   || j         }t          |d         dk    d           g }|d         D ]b}|                     |          }|d         dk    r|                    |           9|                    |                     |                     c|S )Nrv  rF  z%/Type of page tree node is not /Pagess   Kidsre  )node)r#  rU   rx  r   r   ry  )rb   r  r$  kid
kid_objects        r   ry  zPdfParser.linearize_page_tree  s    <&DMX%'N	
 	
 	
 = 	H 	HC++C00J'"g--S!!!!T55:5FFGGGGr   )NNNr   r  r?   )F)Nr   )r   )IrM   rN   rO   rP   r|   r-  r3  r6  r5  r/  r*  r>  rA  rM  rK  rc  rP  rL  rI  staticmethodr  r  rJ  	delimiterdelimiter_or_ws
whitespacewhitespace_or_hexwhitespace_optionalwhitespace_mandatorywhitespace_optional_no_nlnewline_onlynewlinerecompileDOTALLr  r  rw  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  rx  ry  rQ   r   r   r   r   k  s        
$ $ $ $L          $ $ $$ $ $* * *    :
 
 
 
(6 6 6  *; ; ; 	 	 \	( ( (8    !I>O/J?$t+%, 7L',69RRGRZ
	
	 	 		
 	 	 	 	 		 
	 	 		 N !bj
	
	 	 		
 	 	 	 	 		 
	 		 O? ? ?*: : : (RZ(;<<bj
-	.
	 	 G BJ2V;<<M"*069<OOPPK  [4 #
#BCC
 
 
 [
 bj,{:_LuTUUGbj,{:_LuTUUGrz-<NQVVWWHRZ22_DuL F bj
7	8
	 	 G  RZ 3f <==N2:1E9::LBJf$'887B M BJ2V;<<M&BJ
	
	 	 		
 	 	 		 	 'BJ
	
	 	 		
 	 	 		 	 %"*m+o=E  ##m3g=F J !bj!4!FGGOBJ..@5H M x" x" x" [x"t "rz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 'BJ':X'E'OPP)rz
	
	 	 		
 	    BJNOOM  <         r   r   )%
__future__r   r   r   r   rq  r;  r  r   r   typingr   r   r   r   r   r   r@   rJ   r  rL   rU   rW   r]   rs   rv   r   r   r   r   r   	_DictBaser   r   r   r   r   rQ   r   r   <module>r     s   " " " " " "        				 				   > > > > > > > > > > > > > >
7 7 7*(*(* 	(* 	(	*
 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(* 	(*  	(!*" 	(#* *$ 	(%*& 	('*( 	()** 	(+*, 	(-*. 	(/*0 	(1*2 	(3*4 	(5*6 	(7*8 	(9*: 	(;*< 	(=*> 	(?*@ 	(A*B 	(C*D 	(E* *F 





S* * *ZJ J J	 	 	 	 	\ 	 	 	, , ,
    Z   
7 7 7 7 7. 7 7 7*9 9 9 9 9) 9 9 9
Y Y Y Y Y Y Y Yx$ $ $ $ $ $ $ $ND D D D DtCy D D D
  %$U3:%6%;<II$I- - - - -i - - -`B B B B B B B B+ + + + + + + +*  :E
 E
 E
 E
 E
 E
 E
 E
 E
 E
r   