o
    Ñ­j5ˆ  ã                	   @  sD  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 dvd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/“d0d1“d2d3“d4d5“d6d7“d8d9“d:d;“d<d=“d>d?“d@dA“dBdC“dDdE“dFdG“dHdI“dJdK“dLdM“¥dNdOdPdQdRdSdTdUœ¥ZdVdW„ ZG dXdY„ dYeƒZdwd^d_„ZG d`da„ daeƒZG dbdc„ dceƒZG ddde„ deeƒZG dfdg„ dgƒZG dhdi„ diƒZG djdk„ dkee ƒZerüejeeef ef ZnejZG dldm„ dmeƒZ G dndo„ doƒZ!G dpdq„ dqƒZ"drds„ Z#G dtdu„ duƒZ$dS )xé    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚListÚ
NamedTupleÚUnionÚsÚstrÚreturnÚbytesc                 C  s   t j|  d¡ S )NÚ	utf_16_be)ÚcodecsÚBOM_UTF16_BEÚencode)r   © r   úJ/var/www/html/CropPilot/venv/lib/python3.10/site-packages/PIL/PdfParser.pyÚencode_text   ó   r   é   úé   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                 C  sD   | d t tjƒ… tjkr| t tjƒd …  d¡S d dd„ | D ƒ¡S )Nr   Ú c                 s  s    � | ]}t  |t|ƒ¡V  qd S ©N)ÚPDFDocEncodingÚgetÚchr)Ú.0Úbyter   r   r   Ú	<genexpr>E   s   € zdecode_text.<locals>.<genexpr>)Úlenr   r   ÚdecodeÚjoin)Úbr   r   r   Údecode_textA   s   rJ   c                   @  s   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   r   r   rK   H   s    rK   Ú	conditionÚboolÚerror_messageÚNonec                 C  s   | st |ƒ‚d S r?   )rK   )rP   rR   r   r   r   Úcheck_format_conditionO   s   ÿrT   c                   @  s   e Zd ZU ded< ded< dS )ÚIndirectReferenceTupleÚintÚ	object_idÚ
generationN)rL   rM   rN   Ú__annotations__r   r   r   r   rU   T   s   
 rU   c                   @  s<   e Zd Zddd„Zddd„Zddd„Zdd„ Zddd„ZdS )ÚIndirectReferencer
   r	   c                 C  ó   | j › d| j› d�S )Nú z R©rW   rX   ©Úselfr   r   r   Ú__str__Z   ó   zIndirectReference.__str__r   c                 C  s   |   ¡  d¡S ©Núus-ascii)r`   r   r^   r   r   r   Ú	__bytes__]   s   zIndirectReference.__bytes__ÚotherÚobjectrQ   c                 C  s6   | j |j urdS t|tƒsJ ‚|j| jko|j| jkS )NF)Ú	__class__Ú
isinstancerZ   rW   rX   ©r_   re   r   r   r   Ú__eq__`   s   zIndirectReference.__eq__c                 C  s
   | |k S r?   r   ri   r   r   r   Ú__ne__f   ó   
zIndirectReference.__ne__rV   c                 C  s   t | j| jfƒS r?   )ÚhashrW   rX   r^   r   r   r   Ú__hash__i   r   zIndirectReference.__hash__N©r
   r	   ©r
   r   )re   rf   r
   rQ   ©r
   rV   )rL   rM   rN   r`   rd   rj   rk   rn   r   r   r   r   rZ   Y   s    


rZ   c                   @  ó   e Zd Zddd„ZdS )ÚIndirectObjectDefr
   r	   c                 C  r[   )Nr\   z objr]   r^   r   r   r   r`   n   ra   zIndirectObjectDef.__str__Nro   )rL   rM   rN   r`   r   r   r   r   rs   m   ó    rs   c                   @  sN   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zddd„Zdd„ Z	dd„ Z
dS )Ú	XrefTablec                 C  s    i | _ i | _ddi| _d| _d S )Nr   i   F)Úexisting_entriesÚnew_entriesÚdeleted_entriesÚreading_finishedr^   r   r   r   Ú__init__s   s   

zXrefTable.__init__c                 C  s6   | j r	|| j|< n|| j|< || jv r| j|= d S d S r?   )ry   rw   rv   rx   ©r_   ÚkeyÚvaluer   r   r   Ú__setitem__y   s   

ÿzXrefTable.__setitem__c                 C  s*   z| j | W S  ty   | j|  Y S w r?   )rw   ÚKeyErrorrv   ©r_   r|   r   r   r   Ú__getitem__�   s
   ÿzXrefTable.__getitem__c                 C  sˆ   || j v r| j | d d }| j |= || j|< d S || jv r.| j| d d }|| j|< d S || jv r:| j| }d S d|› d�}t|ƒ‚)Né   z
object ID z+ cannot be deleted because it doesn't exist)rw   rx   rv   Ú
IndexError)r_   r|   rX   Úmsgr   r   r   Ú__delitem__‡   s   


zXrefTable.__delitem__c                 C  s   || j v p	|| jv S r?   )rv   rw   r€   r   r   r   Ú__contains__•   ra   zXrefTable.__contains__r
   rV   c                 C  s.   t t| j ¡ ƒt| j ¡ ƒB t| j ¡ ƒB ƒS r?   )rF   Úsetrv   Úkeysrw   rx   r^   r   r   r   Ú__len__˜   s   ÿþÿzXrefTable.__len__c                 C  s*   t | j ¡ ƒt | j ¡ ƒ t | j ¡ ƒB S r?   )r‡   rv   rˆ   rx   rw   r^   r   r   r   rˆ   Ÿ   s   þzXrefTable.keysc              	   C  sF  t t| j ¡ ƒt| j ¡ ƒB ƒ}t t| j ¡ ƒƒ}| ¡ }| d¡ |r¡d }t|ƒD ]\}}|d u s8|d |kr;|}q*|d |… }||d … } n|}d }| d|d t|ƒf ¡ |D ]B}	|	| jv rn| d| j|	  ¡ q\| 	d¡}
t
|	|
kd|	› d|
› �ƒ z|d }W n ty‘   d}Y nw | d|| j|	 f ¡ q\|s$|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‡   rw   rˆ   rx   ÚtellÚwriteÚ	enumeraterF   ÚpoprT   rƒ   )r_   Úfrˆ   Údeleted_keysÚ	startxrefÚprevÚindexr|   Úcontiguous_keysrW   Úthis_deleted_object_idÚnext_in_linked_listr   r   r   rŒ   ¤   sJ    


ÿþÿÿÿä zXrefTable.writeNrq   )rL   rM   rN   rz   r~   r�   r…   r†   r‰   rˆ   rŒ   r   r   r   r   ru   r   s    
ru   c                   @  sl   e Zd Zdd„ Zddd„Zdd„ Zdd
d„Zddd„Zedd„ ƒZ	e
eddƒƒdd„ dD ƒ Zddd„ZdS )ÚPdfNamec                 C  s:   t |tƒr|j| _d S t |tƒr|| _d S | d¡| _d S rb   )rh   r—   Únamer   r   )r_   r˜   r   r   r   rz   Í   s
   


zPdfName.__init__r
   r	   c                 C  s   | j  d¡S rb   )r˜   rG   r^   r   r   r   Úname_as_strÕ   ó   zPdfName.name_as_strc                 C  s    t |tƒo
|j| jkp|| jkS r?   )rh   r—   r˜   ri   r   r   r   rj   Ø   s   ÿþzPdfName.__eq__rV   c                 C  s
   t | jƒS r?   )rm   r˜   r^   r   r   r   rn   Ý   rl   zPdfName.__hash__c                 C  s   | j j› dt| jƒ› d�S )Nú(ú))rg   rL   Úreprr˜   r^   r   r   r   Ú__repr__à   ó   zPdfName.__repr__c                 C  s   | t  |¡ƒS r?   )Ú	PdfParserÚinterpret_name)ÚclsÚdatar   r   r   Úfrom_pdf_streamã   s   zPdfName.from_pdf_streamé!   é   c                 C  s   h | ]}t |ƒ’qS r   )Úord)rC   Úcr   r   r   Ú	<setcomp>ç   s    zPdfName.<setcomp>z#%/()<>[]{}r   c                 C  s@   t dƒ}| jD ]}|| jv r| |¡ q| d| ¡ qt|ƒS )Nó   /s   #%02X)Ú	bytearrayr˜   Úallowed_charsÚappendÚextendr   )r_   ÚresultrI   r   r   r   rd   é   s   

zPdfName.__bytes__Nro   rq   rp   )rL   rM   rN   rz   r™   rj   rn   rž   Úclassmethodr¤   r‡   Úranger¬   rd   r   r   r   r   r—   Ì   s    



r—   c                   @  rr   )ÚPdfArrayr
   r   c                 C  s   dd  dd„ | D ƒ¡ d S )Ns   [ ó    c                 s  s   � | ]}t |ƒV  qd S r?   )Úpdf_repr)rC   Úxr   r   r   rE   õ   ó   € z%PdfArray.__bytes__.<locals>.<genexpr>s    ])rH   r^   r   r   r   rd   ô   ó   zPdfArray.__bytes__Nrp   )rL   rM   rN   rd   r   r   r   r   r²   ó   rt   r²   c                   @  s&   e Zd Zdd„ Zdd„ Zd
dd„Zd	S )ÚPdfDictc                 C  s.   |dkrt j | ||¡ d S || | d¡< d S )Nr£   rc   )ÚcollectionsÚUserDictÚ__setattr__r   r{   r   r   r   r»   ÿ   s   zPdfDict.__setattr__c              
   C  s  z	| |  d¡ }W n ty } zt|ƒ|‚d }~ww t|tƒr$t|ƒ}| d¡rŒ| d¡r4|dd … }d}t|ƒdkrZ|d }t	|dd… ƒd	 }t|ƒd
krZ|t	|dd
… ƒ7 }dd t|ƒd … }t
 |d t|ƒd … |¡}|dv rŒ|d	9 }|dkr‚|d9 }t
 t |¡| ¡}|S )Nrc   ÚDatezD:é   ÚZé   é   é   é<   é   é   z%Y%m%d%H%M%S)ú+ú-rÅ   éÿÿÿÿ)r   r   ÚAttributeErrorrh   r   rJ   ÚendswithÚ
startswithrF   rV   ÚtimeÚstrptimeÚgmtimeÚcalendarÚtimegm)r_   r|   r}   ÚeÚrelationshipÚoffsetÚformatr   r   r   Ú__getattr__  s2   
€ÿ


zPdfDict.__getattr__r
   r   c                 C  sn   t dƒ}|  ¡ D ]%\}}|d u rqt|ƒ}| d¡ | tt|ƒƒ¡ | d¡ | |¡ q| d¡ t|ƒS )Nó   <<ó   
r³   s   
>>)r«   Úitemsr´   r®   r   r—   )r_   Úoutr|   r}   r   r   r   rd      s   


zPdfDict.__bytes__Nrp   )rL   rM   rN   r»   rÔ   rd   r   r   r   r   r¸   þ   s    r¸   c                   @  s   e Zd Zdd„ Zddd„ZdS )	Ú	PdfBinaryc                 C  s
   || _ d S r?   )r£   )r_   r£   r   r   r   rz   /  rl   zPdfBinary.__init__r
   r   c                 C  s   dd  dd„ | jD ƒ¡ S )Ns   <%s>ó    c                 s  s   � | ]}d | V  qdS )s   %02XNr   ©rC   rI   r   r   r   rE   3  r¶   z&PdfBinary.__bytes__.<locals>.<genexpr>)rH   r£   r^   r   r   r   rd   2  rŸ   zPdfBinary.__bytes__Nrp   )rL   rM   rN   rz   rd   r   r   r   r   rÙ   .  s    rÙ   c                   @  s   e Zd Zdd„ Zdd„ ZdS )Ú	PdfStreamc                 C  s   || _ || _d S r?   )Ú
dictionaryÚbuf)r_   rÝ   rÞ   r   r   r   rz   7  s   
zPdfStream.__init__c                 C  s†   z| j j}W n ty   | j Y S w |dkr5z| j j}W n ty*   | j j}Y nw tj| jt|ƒd�S dt	| j jƒ› d�}t
|ƒ‚)Ns   FlateDecode)Úbufsizezstream filter z unknown/unsupported)rÝ   ÚFilterrÈ   rÞ   ÚDLÚLengthÚzlibÚ
decompressrV   r�   ÚNotImplementedError)r_   ÚfilterÚexpected_lengthr„   r   r   r   rG   ;  s   
ÿÿzPdfStream.decodeN)rL   rM   rN   rz   rG   r   r   r   r   rÜ   6  s    rÜ   c                 C  s  | du rdS | du rdS | d u r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
 d| ¡ 	d¡ d	 S t | tƒrKtt| ƒƒS t | tƒrVtt| ƒƒS t | tƒratt| ƒƒS t | tƒr~|  d
d¡} |  dd¡} |  d	d¡} d|  d	 S t| ƒS )NTs   trueFs   falses   nullrc   s   (D:z%Y%m%d%H%M%SZó   )ó   \s   \\ó   (ó   \(s   \))rh   r—   r¸   r²   rÙ   r   rV   Úfloatr	   r   rË   Ústruct_timeÚstrftimeÚdictÚlistr´   r   Úreplace)rµ   r   r   r   r´   K  s0   



r´   c                   @  s$  e Zd ZdZdwdd„Zdxdd	„Zdydd„Zdzdd„Zdzdd„Zdzdd„Z	dzdd„Z
dzdd„Zdd„ Zd{dd„Zdzdd„Zd|d d!„Zd"d#„ Zd$d%„ Zdzd&d'„Zed(d)„ ƒZdzd*d+„Zd|d,d-„Zd.Zd/Zd0Zd1Zed2 Zed3 Zd4Zd5Zee e Ze  !ed6 e d7 e d8 e d9 e d: e d; e j"¡Z#e  !ed6 e d< e d8 e d9 e d: e e j"¡Z$d=d>„ Z%d?d@„ Z&e  !e¡Z'e  !edA e dB ¡Z(e  !edC ¡Z)e  !edD e ¡Z*e+dEdF„ ƒZ,e  !dG¡Z-e+d}dIdJ„ƒZ.e  !edK e dB ¡Z/e  !edL e dB ¡Z0e  !edM e dB ¡Z1e  !edN e dB ¡Z2e  !edO e dB ¡Z3e  !edP ¡Z4e  !edQ ¡Z5e  !edR e dS ¡Z6e  !edT ¡Z7e  !edU e dU e dV e dB ¡Z8e  !edU e dU e dW e dB ¡Z9e  !edX e dB ¡Z:e  !dYe dZ e d[ ¡Z;e  !ed\ ¡Z<e  !ed] e dB ¡Z=e+d~d_d`„ƒZ>e  !da¡Z?i dbdc“ddde“dfdg“dhdi“djdk“dYdY“dBdB“dldl“e@dbƒdc“e@ddƒde“e@dfƒdg“e@dhƒdi“e@djƒdk“e@dYƒdY“e@dBƒdB“e@dlƒdl“ZAe+dmdn„ ƒZBe  !edo e ¡ZCe  !ed9 e d9 e e ¡ZDe  !dp¡ZEdqdr„ ZFddsdt„ZGd|dudv„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                 C  s  |r
|r
d}t |ƒ‚|| _|| _|| _|| _d| _d| _|d ur/|d u r/t||ƒ | _}d| _|d urI|  |¡ | _}d| _|sIt	|dƒrI|j
| _i | _|rS|  ¡  n*d | _| _tƒ | _d | _tƒ | _d | _i | _g | _g | _d | _d | _i | _tƒ | _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_dictru   Ú
xref_tablery   Úseek_end)r_   rô   r�   rÞ   rõ   Úmoder„   r   r   r   rz   n  sH   
ÿzPdfParser.__init__r
   c                 C  s   | S r?   r   r^   r   r   r   Ú	__enter__”  s   zPdfParser.__enter__Úargsrf   rS   c                 G  s   |   ¡  d S r?   )Úclose)r_   r  r   r   r   Ú__exit__—  rš   zPdfParser.__exit__c                 C  s   |   ¡  |  ¡  d S r?   )Ú	close_bufr
  r^   r   r   r   Ústart_writingš  s   zPdfParser.start_writingc                 C  s,   z| j  ¡  W n	 ty   Y nw d | _ d S r?   )rÞ   r  rÈ   r^   r   r   r   r  ž  s   ÿ
zPdfParser.close_bufc                 C  s:   | j r|  ¡  | jd ur| jr| j ¡  d | _d S d S d S r?   )rö   r  r�   r÷   r  r^   r   r   r   r  ¥  s   

þzPdfParser.closec                 C  s   | j  dtj¡ d S )Nr   )r�   ÚseekÚosÚSEEK_ENDr^   r   r   r   r
  ¬  ra   zPdfParser.seek_endc                 C  s   | j  d¡ d S )Ns	   %PDF-1.4
)r�   rŒ   r^   r   r   r   Úwrite_header¯  r   zPdfParser.write_headerc                 C  s   | j  d|› d� ¡ ¡ d S )Nz% Ú
)r�   rŒ   r   )r_   r   r   r   r   Úwrite_comment²  r·   zPdfParser.write_commentrZ   c                 C  sl   |   ¡  |  | j ¡ ¡| _|  d¡| _|  ¡  | j| jtdƒ| jd� | j| jtdƒt	| j
ƒ| j
d� | jS )Nr   ó   Catalog)ÚTypeÚPagesó   Pages)r  ÚCountÚKids)Údel_rootÚnext_object_idr�   r‹   r   r  Úrewrite_pagesÚ	write_objr—   rF   r  r^   r   r   r   Úwrite_catalogµ  s   üzPdfParser.write_catalogc                 C  sô   g }t | jƒD ]N\}}| j| }| j|j= | |tdƒ ¡ || jvr$qi }| ¡ D ]
\}}||| 	¡ < q*| j
|d< | jdi |¤Ž}t | jƒD ]\}	}
|
|krT|| j|	< qGq|D ]}|rt| j| }|j| jv rl| j|j= | dd ¡}|s\qXg | _d S )Ns   ParentÚParentr?   )r�   r  rû   r	  rW   r­   r—   r  r×   r™   r  Ú
write_pagerA   )r_   Ú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_noder   r   r   r   Ã  s4   




€ý

ü€
zPdfParser.rewrite_pagesc                 C  sš   |r	|   ¡  || _| jr|  d | j¡| _| j | j¡}t| jƒ}| j|dœ}| j	d ur0| j	|d< | jr8| j|d< || _	| j dt
t|ƒƒ d|  ¡ d S )N)ó   Rootó   Sizeó   Prevó   Infos   trailer
s   
startxref
%d
%%%%EOF)r  r   r  r!  r  r	  rŒ   r�   rF   r  r   r¸   )r_   Únew_root_refÚ
start_xrefÚnum_entriesr  r   r   r   Úwrite_xref_and_trailerà  s(   




ÿþÿz PdfParser.write_xref_and_trailerc                 O  sR   t |tƒr
| j| }d|vrtdƒ|d< d|vr| j|d< | j|g|¢R i |¤ŽS )Nr  ó   Pager#  )rh   rV   r  r—   r  r!  )r_   ÚrefÚobjsÚdict_objr   r   r   r$  ô  s   


zPdfParser.write_pagec                 O  sÄ   | j }|d u r|  | ¡ ¡}n| ¡ |jf| j|j< | tt|Ž ƒ¡ | 	dd ¡}|d ur3t
|ƒ|d< |r<| t|ƒ¡ |D ]	}| t|ƒ¡ q>|d ur[| d¡ | |¡ | d¡ | d¡ |S )NÚstreamrâ   s   stream
s   
endstream
s   endobj
)r�   r  r‹   rX   r	  rW   rŒ   r   rs   rŽ   rF   r´   )r_   r8  r9  r:  r�   r;  Úobjr   r   r   r!  ý  s$   



zPdfParser.write_objc                 C  s.   | j d u rd S | j| j j= | j| jd j= d S )Nr  )r   r	  rW   rÿ   r^   r   r   r   r    s   
zPdfParser.del_rootc                 C  sR   t | dƒr	|  ¡ S t | dƒr|  ¡ S ztj|  ¡ dtjd�W S  ty(   Y dS w )NÚ	getbufferÚgetvaluer   )ÚaccessrÚ   )rú   r=  r>  ÚmmapÚfilenoÚACCESS_READÚ
ValueError)r�   r   r   r   rù     s   

ÿzPdfParser.get_buf_from_filec                 C  sü   t | jƒ| _| j| j | _|  ¡  | jd | _| j dd ¡| _	t
|  | j¡ƒ| _| j	d u r2t
ƒ | _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/  r2  ó   Typez/Type missing in Rootr  z/Type in Root is not /Catalogr  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  rT   rh   rZ   r  r  Úlinearize_page_treer  r  r^   r   r   r   rü   $  s,   

ÿþzPdfParser.read_pdf_infoc                 C  sV   zt t| j ¡ ƒd dƒ}W n ty   t ddƒ}Y nw |d ur)|df| j|j< |S )Nr‚   r   )rZ   Úmaxr	  rˆ   rC  rW   )r_   rÒ   Ú	referencer   r   r   r  @  s   ÿzPdfParser.next_object_ids   [][()<>{}/%]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                 C  sÆ   t | jƒd }|| jk r| j}| j | j|¡}t|dƒ |}|r0|}| j | j| ¡ d ¡}|s |s4|}| d¡}t| d¡ƒ| _	|  
|¡| _tƒ | _| j| j	d� d| jv ra|  | jd ¡ d S d S )Né @  ztrailer end not foundé   r‚   r½   ©Úxref_section_offsetr1  )rF   rÞ   rõ   Úre_trailer_endÚsearchrT   ÚstartÚgrouprV   r  Úinterpret_trailerr  ru   r	  Úread_xref_tableÚread_prev_trailer)r_   Úsearch_start_offsetÚmÚ
last_matchÚtrailer_datar   r   r   rE  r  s(   

þ

ÿzPdfParser.read_trailerc                 C  sz   | j |d�}| j | j||d … ¡}t|dƒ | d¡}tt| d¡ƒ|kdƒ |  |¡}d|v r;|  |d ¡ d S d S )NrO  rM  zprevious trailer not foundr‚   r½   zGxref section offset in previous trailer doesn't match what was expectedr1  )	rV  Úre_trailer_prevrR  rÞ   rT   rT  rV   rU  rW  )r_   rP  Útrailer_offsetrY  r[  r  r   r   r   rW  ‡  s   ÿ

þ
ÿzPdfParser.read_prev_trailers   /([!-$&'*-.0-;=?-Z\\^-z|~]+)(?=rè   rÕ   s   >>c                 C  sÀ   i }d}	 | j  ||¡}|s+| j ||¡}t|o| ¡ t|ƒkdt||d … ƒ ƒ n|  | d¡¡}|  	|| ¡ ¡\}}|||< qtd|v oMt
|d tƒdƒ td|v o[t
|d tƒdƒ |S )	Nr   Tz+name not found in trailer, remaining data: r‚   r0  z&/Size not in trailer or not an integerr/  z1/Root not in trailer or not an indirect reference)Úre_nameÚmatchÚre_dict_endrT   ÚendrF   r�   r¡   rT  Ú	get_valuerh   rV   rZ   )r¢   r[  ÚtrailerrÒ   rY  r|   r}   r   r   r   rU     s4   ÿþôþþzPdfParser.interpret_trailers   ([^#]*)(#([0-9a-fA-F]{2}))?Fc                 C  sj   d}| j  |¡D ]!}| d¡r"|| d¡t | d¡ d¡¡ 7 }q|| d¡7 }q|r1| d¡S t|ƒS )NrÚ   é   r‚   rc   zutf-8)Úre_hashes_in_nameÚfinditerrT  r«   ÚfromhexrG   r   )r¢   ÚrawÚas_textr˜   rY  r   r   r   r¡   ½  s   
&
zPdfParser.interpret_names   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              
   C  sÜ  |dkrdS | j  ||¡}|r| ¡ }| j ||¡}|rttt| d¡ƒdkdƒ tt| d¡ƒdkdƒ t|d u pI|tt| d¡ƒt| d¡ƒƒkdƒ | j|| ¡ |d d�\}}|d u rb|d fS | j	 ||¡}t|d	ƒ || ¡ fS t| d
ƒ | j
 ||¡}|r®tt| d¡ƒdkdƒ tt| d¡ƒdkdƒ tt| d¡ƒt| d¡ƒƒ| ¡ fS | j ||¡}|�rc| ¡ }i }| j ||¡}|sü| j|||d d�\}}|d u rÛ|d fS | j|||d d�\}	}|	||< |d u ró|d fS | j ||¡}|rÇ| ¡ }| j ||¡}|�r[z| d¡}
t|
ƒ}W n ttf�y/ } zd|
› d�}t|ƒ|‚d }~ww || ¡ | ¡ | … }| j || ¡ | ¡}t|dƒ | ¡ }tt|ƒ|ƒ}||fS t|ƒ}||fS | j ||¡}|�r§| ¡ }g }| j ||¡}|�s¡| j|||d d�\}	}| |	¡ |d u �r—|d fS | j ||¡}|�r}|| ¡ fS | j ||¡}|�r·d | ¡ fS | j ||¡}|�rÇd| ¡ fS | j ||¡}|�r×d| ¡ fS | j ||¡}|�rït|  | d¡¡ƒ| ¡ fS | j ||¡}|�rt| d¡ƒ| ¡ fS | j ||¡}|�rt | d¡ƒ| ¡ fS | j! ||¡}|�rKt"dd„ | d¡D ƒƒ}t#|ƒd dk�r?| t$dƒ¡ t" %| &d¡¡| ¡ fS | j' ||¡}|�r]|  (|| ¡ ¡S dt)|||d … ƒ› �}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                 s  s   � | ]	}|d v r|V  qdS )s   0123456789abcdefABCDEFNr   rÛ   r   r   r   rE   g  s   € ÿz&PdfParser.get_value.<locals>.<genexpr>ó   0rc   zunrecognized object: é    )*Ú
re_commentr_  ra  Úre_indirect_def_startrT   rV   rT  rZ   rb  Úre_indirect_def_endÚre_indirect_referenceÚre_dict_startr`  Úre_stream_startrA   Ú	TypeErrorrC  rK   Ú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§   rg  rG   Úre_string_litÚget_literal_stringr�   )r¢   r£   rÒ   Úexpect_indirectrk  rY  rf   r¯   r|   r}   Ústream_len_strÚ
stream_lenrÐ   r„   Ústream_dataÚ
hex_stringr   r   r   rb  û  sð   þþÿü
ÿþþ&ø	

€þ
ÿ

ûÿzPdfParser.get_valuesF   (\\[nrtbf()\\])|(\\[0-9]{1,3})|(\\(\r\n|\r|\n))|(\r\n|\r|\n)|(\()|(\))ó   nrÖ   ó   ró   ó   tó   	ó   bó   ó   fó   ré   c                 C  s  d}t ƒ }| j ||¡D ]u}| ||| ¡ … ¡ | d¡r,| | j| d¡d  ¡ nQ| d¡rA| t| d¡dd … dƒ¡ n<| d¡rGn6| d¡rR| d¡ n+| d¡ra| d	¡ |d7 }n| d
¡r}|dkrtt	|ƒ| 
¡ f  S | d¡ |d8 }| 
¡ }qd}t|ƒ‚)Nr   r‚   r½   é   rd  é   rÖ   é   rê   é   rè   zunfinished literal string)r«   Úre_lit_str_tokenrf  r®   rS  rT  Úescaped_charsr­   rV   r   ra  rK   )r¢   r£   rÒ   Únesting_depthr¯   rY  r„   r   r   r   r  ‹  s.   

 







zPdfParser.get_literal_strings   xrefs+   ([0-9]{10}) ([0-9]{5}) ([fn])( \r| \n|\r\n)c                 C  sø   d}| j  | j|| j ¡}t|dƒ | ¡ }	 | j | j|¡}|s)t|dƒ 	 |S d}| ¡ }t| d¡ƒ}t| d¡ƒ}t	||| ƒD ]6}| j
 | j|¡}t|dƒ | ¡ }| d¡d	k}|szt| d¡ƒ}	t| d¡ƒ|	f}
|| jvrz|
| j|< qDq)
NFzxref section start not foundTzxref subsection start not foundr‚   r½   zxref entry not foundrd  rŒ  )Úre_xref_section_startr_  rÞ   rõ   rT   ra  Úre_xref_subsection_startrV   rT  r±   Úre_xref_entryr	  )r_   rP  Úsubsection_foundrY  rÒ   Úfirst_objectÚnum_objectsr&  Úis_freerX   Ú	new_entryr   r   r   rV  °  s<   ÿ
ÿò


€ìzPdfParser.read_xref_tablec              
   C  st   | j |d  \}}t||d kd|d › d|d › d|› d|› �ƒ | j| j|| j t|Ž |d�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€  rk  )r	  rT   rb  rÞ   rõ   rZ   rû   )r_   r8  rk  rÒ   rX   r}   r   r   r   rF  Î  s&   
ÿÿþüû
zPdfParser.read_indirectc                 C  sj   |d u r| j }t|d dkdƒ g }|d D ]}|  |¡}|d dkr)| |¡ q| | j|d�¡ q|S )NrD  r  z%/Type of page tree node is not /Pagess   Kidsr7  )Únode)r  rT   rF  r­   r®   rG  )r_   r�  r  ÚkidÚ
kid_objectr   r   r   rG  Þ  s   ÿ
zPdfParser.linearize_page_tree)NNNr   rò   )r
   r    )r  rf   r
   rS   )r
   rS   )r
   rZ   r?   )F)NrÇ   )rÇ   )IrL   rM   rN   rO   rz   r  r  r  r  r  r
  r  r  r"  r   r6  r$  r!  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ÚDOTALLrQ  r\  rE  rW  Úre_whitespace_optionalr^  rr  r`  r°   rU  re  r¡   rx  ry  rz  r{  r|  rv  rw  r}  r~  rq  ro  rp  rn  rs  ru  rb  r’  r§   r“  r  r•  r–  r—  rV  rF  rG  r   r   r   r   r    h  s¼   

&








	




ÿþýüûúùø	÷
öõóÿþýüûúùø	÷
öô
ÿþýÿ

ÿÿþýÿÿÿþýüûúùÿ
ÿþýüûúùÿ
ÿÿÿyÿÿþýüûúùø	÷
öõôóòñð
ÿþýüûÿ

r    )r   r	   r
   r   )rP   rQ   rR   r	   r
   rS   )%Ú
__future__r   rÎ   r   r¹   r@  r  rª  rË   rã   Útypingr   r   r   r   r   r   r@   rJ   ró   rK   rT   rU   rZ   rs   ru   r—   r²   rº   r	   r   Ú	_DictBaser¸   rÙ   rÜ   r´   r    r   r   r   r   Ú<module>   sÒ    
ÿþýüûúùø	÷
öõôóòñðïîíìëêéèçæåäãâá à!ß"Þ#×-
Z'0