3 }\͊2@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z yddl m Z Wne k rvej Z YnXe rddZ nddZ dd Z d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3)Zd4d5ZGd6d7d7eZd8d9ZGd:d;d;ejdgZGd?d@d@eZGdAdBdBZGdCdDdDZGdEdFdFeZGdGdHdHe ZGdIdJdJZGdKdLdLZdMdNZGdOdPdPZdS)QN)py3)UserDictcCs |jdS)Nzus-ascii)encode)sre/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-i584jbuk/Pillow/PIL/PdfParser.py make_bytessr cCs|S)Nr)rrrrr scCstj|jdS)N utf_16_be)codecs BOM_UTF16_BEr)rrrr encode_textsr 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™ufiufluŁuŒuŠuŸuŽuıułuœušužu€))cCs`|dttjtjkr0|ttjdjdStrHdjdd|DSdjdd|DSdS)Nr css|]}tj|t|VqdS)N)PDFDocEncodinggetchr).0byterrr Pszdecode_text..css|]}tjt||VqdS)N)r9r:ord)r<r=rrrr>Rs)lenr r decoderjoin)brrr decode_textLs rDc@seZdZdZdS)PdfFormatErrorz\An error that probably indicates a syntactic or semantic error in the PDF file structureN)__name__ __module__ __qualname____doc__rrrrrEUsrEcCs|s t|dS)N)rE) condition error_messagerrrcheck_format_condition[srLc@s4eZdZddZddZddZddZd d Zd S) IndirectReferencecCsd|S)Nz%s %s Rr)selfrrr__str__bszIndirectReference.__str__cCs|jjdS)Nzus-ascii)rOr)rNrrr __bytes__eszIndirectReference.__bytes__cCs$|j|jko"|j|jko"|j|jkS)N) __class__ object_id generation)rNotherrrr__eq__hs  zIndirectReference.__eq__cCs ||k S)Nr)rNrTrrr__ne__mszIndirectReference.__ne__cCst|j|jfS)N)hashrRrS)rNrrr__hash__pszIndirectReference.__hash__N)rFrGrHrOrPrUrVrXrrrrrM`s rMZIndirectReferenceTuplerRrSc@seZdZddZdS)IndirectObjectDefcCsd|S)Nz %s %s objr)rNrrrrOuszIndirectObjectDef.__str__N)rFrGrHrOrrrrrYtsrYc@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS) XrefTablecCs i|_i|_ddi|_d|_dS)NriF)existing_entries new_entriesdeleted_entriesreading_finished)rNrrr__init__zs zXrefTable.__init__cCs2|jr||j|<n ||j|<||jkr.|j|=dS)N)r^r\r[r])rNkeyvaluerrr __setitem__s    zXrefTable.__setitem__c Cs*y |j|Stk r$|j|SXdS)N)r\KeyErrorr[)rNr`rrr __getitem__s zXrefTable.__getitem__cCs||jkr0|j|dd}|j|=||j|<nR||jkrX|j|dd}||j|<n*||jkrn|j|}ntdt|ddS)Nrz object ID z+ cannot be deleted because it doesn't exist)r\r]r[ IndexErrorstr)rNr`rSrrr __delitem__s       zXrefTable.__delitem__cCs||jkp||jkS)N)r[r\)rNr`rrr __contains__szXrefTable.__contains__cCs.tt|jjt|jjBt|jjBS)N)r@setr[keysr\r])rNrrr__len__szXrefTable.__len__cCs*t|jjt|jjt|jjBS)N)rir[rjr]r\)rNrrrrjs zXrefTable.keysc Cs`tt|jjt|jjB}tt|jj}|j}|jdx|rZd}xPt|D]<\}}|dksx|d|kr~|}q\|d|}||d}Pq\W|}d}|jtd|dt |fx|D]} | |jkr|jtd|j| q|j d} t | | kd| | fy |d} Wnt k r6d} YnX|jtd| |j| fqWqHW|S)Nsxref rz%d %d rz%010d %05d n z>expected the next deleted object ID to be %s, instead found %sz%010d %05d f ) sortedrir\rjr]tellwrite enumerater r@poprLre) rNfrjZ deleted_keysZ startxrefprevindexr`Zcontiguous_keysrRZthis_deleted_object_idZnext_in_linked_listrrrrnsB          zXrefTable.writeN) rFrGrHr_rbrdrgrhrkrjrnrrrrrZysrZc@sleZdZddZddZddZddZd d Zed d Z e e d de dddDZ ddZ e ZdS)PdfNamecCs6t|tr|j|_nt|tr&||_n |jd|_dS)Nzus-ascii) isinstancertnamebytesr)rNrvrrrr_s    zPdfName.__init__cCs |jjdS)Nzus-ascii)rvrA)rNrrr name_as_strszPdfName.name_as_strcCs t|tr|j|jkp||jkS)N)rurtrv)rNrTrrrrUszPdfName.__eq__cCs t|jS)N)rWrv)rNrrrrXszPdfName.__hash__cCsdt|jS)Nz PdfName(%s))reprrv)rNrrr__repr__szPdfName.__repr__cCs|tj|S)N) PdfParserinterpret_name)clsdatarrrfrom_pdf_streamszPdfName.from_pdf_stream!ccs|]}t|VqdS)N)r?)r<crrrr>szPdfName.z #%/()<>[]{}cCsztd}xh|jD]^}trB||jkr.|j|qn|jtd|qt||jkr\|j|q|jdt|qWt|S)N/z#%02Xs#%02X) bytearrayrvr allowed_charsappendextendr r?rw)rNresultrCrrrrPs    zPdfName.__bytes__N)rFrGrHr_rxrUrXrz classmethodrrirangerrPrOrrrrrts  rtc@seZdZddZeZdS)PdfArraycCsddjdd|DdS)Ns[  css|]}t|VqdS)N)pdf_repr)r<xrrrr>sz%PdfArray.__bytes__..s ])rB)rNrrrrPszPdfArray.__bytes__N)rFrGrHrPrOrrrrrsrc@s,eZdZddZddZddZes(eZdS)PdfDictcCs@|dkr.ttdr"tj|||q<||j|<n|||jd<dS)Nr~ __setattr__zus-ascii)hasattrrr__dict__r)rNr`rarrrr s   zPdfDict.__setattr__c Csy||jd}Wntk r.t|YnXt|trBt|}|jdr|jdrd|dd}d}t|dkr|d}t |ddd }t|d kr|t |d d 7}d dt|d}t j |dt|d|}|dkr|d 9}|d kr|d9}t j t j||}|S)Nzus-asciiDatezD:Z<z %Y%m%d%H%M%S+-r)rr)rrcAttributeErrorrurwrDendswith startswithr@inttimestrptimegmtimecalendartimegm)rNr`raZ relationshipoffsetformatrrr __getattr__s.        zPdfDict.__getattr__cCsrtd}xV|jD]J\}}|dkr$qt|}|jd|jtt||jd|j|qW|jdt|S)Ns<< rs >>)ritemsrrrwrt)rNoutr`rarrrrP-s   zPdfDict.__bytes__N)rFrGrHrrrPrrOrrrrrs   rc@s*eZdZddZerddZnddZdS) PdfBinarycCs ||_dS)N)r~)rNr~rrrr_?szPdfBinary.__init__cCstddjdd|jDS)Nz<%s>r8css|]}d|VqdS)z%02XNr)r<rCrrrr>Dsz&PdfBinary.__bytes__..)r rBr~)rNrrrrPCszPdfBinary.__bytes__cCsddjdd|jDS)Nz<%s>r8css|]}dt|VqdS)z%02XN)r?)r<rCrrrr>Gsz$PdfBinary.__str__..)rBr~)rNrrrrOFszPdfBinary.__str__N)rFrGrHr_rrPrOrrrrr>s rc@seZdZddZddZdS) PdfStreamcCs||_||_dS)N) dictionarybuf)rNrrrrrr_KszPdfStream.__init__cCsy |jj}Wntk r"|jSX|dkrjy |jj}Wntk rT|jj}YnXtj|jt|dSt dt |jjdS)Ns FlateDecode)bufsizez$stream filter %s unknown/unsupported) rFilterrrZDLLengthzlib decompressrNotImplementedErrorry)rNfilterZexpected_lengthrrrrAOs  zPdfStream.decodeN)rFrGrHr_rArrrrrJsrcCs|dkr dS|dkrdS|dkr$dSt|ttttfr>t|St|trVt|jdSt|t j r|dt j d|jdd St|t rtt|St|t rtt|Strt|tst rt|trtt|St|tr|jd d }|jd d }|jd d}d |d St|SdS)NTstrueFsfalsesnullzus-asciis(D:z %Y%m%d%H%M%SZ)\s\\(s\(s\))rurtrrrrwrrfrr struct_timestrftimedictlistrunicoderr replace)rrrrr`s2            rc @seZdZdZdqddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZdrddZddZdd Zd!d"Zed#d$Zd%d&Zdsd'd(Zd)Zd*Zd+Zd,Zed-Zed.Zd/ZeeeZej ed0ed1ed2ed3ed4ed5ej!Z"ej ed0ed6ed2ed3ed4eej!Z#d7d8Z$d9d:Z%ej eZ&ej ed;ed<Z'ej ed=Z(ej ed>eZ)e*d?d@Z+ej dAZ,e*dtdCdDZ-ej edEed<Z.ej edFed<Z/ej edGed<Z0ej edHed<Z1ej edIed<Z2ej edJZ3ej edKZ4ej edLedMZ5ej edNZ6ej edOedOedPed<Z7ej edOedOedQed<Z8ej edRed<Z9ej dSedTedUZ:ej edVZ;ej edWed<Zd\d]d^d_d`dadbdcdddedSdSd{}/%]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[\r\n]+strailers \<\<(.*\>\>)s startxrefs([0-9]+)s%%EOF$s \<\<(.*?\>\>)cCst|jd}||jkr|j}|jj|j|}t|d|}x$|r`|}|jj|j|jd}q>W|sj|}|jd}t|jd|_ |j ||_ t |_ |j|j dd|j kr|j|j ddS)Ni@ztrailer end not foundrr)xref_section_offsetsPrev)r@rrre_trailer_endsearchrLstartgrouprrinterpret_trailerrrZrread_xref_tableread_prev_trailer)rNZsearch_start_offsetmZ last_match trailer_datarrrrns$     zPdfParser.read_trailercCsv|j|d}|jj|j||d}t|d|jd}tt|jd|kd|j|}d|krr|j|ddS)N)ri@zprevious trailer not foundrrzGxref section offset in previous trailer doesn't match what was expectedsPrev) rre_trailer_prevr rrLr rr r)rNrZtrailer_offsetrrrrrrrs   zPdfParser.read_prev_trailers/([!-$&'*-.0-;=?-Z\\^-z|~]+)(?=rs\<\\>cCsi}d}x||jj||}|sV|jj||}t|o>|jt|kdt||dP|j|jd}|j ||j\}}|||<q Wtd|kot |dt dtd|kot |dt d|S)Nrz+name not found in trailer, remaining data: rsSizez&/Size not in trailer or not an integersRootz1/Root not in trailer or not an indirect reference) re_namematch re_dict_endrLendr@ryr|r  get_valuerurrM)r}rtrailerrrr`rarrrr s,  zPdfParser.interpret_trailers([^#]*)(#([0-9a-fA-F]{2}))?FcCsrd}xR|jj|D]B}|jdrF||jdtj|jdjd7}q||jd7}qW|rf|jdSt|SdS)Nrrzus-asciizutf-8)re_hashes_in_namefinditerr rfromhexrArw)r}rawZas_textrvrrrrr|s   zPdfParser.interpret_namesnull(?=strue(?=sfalse(?=s([-+]?[0-9]+)(?=s)([-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+))(?=s\[]s\<(s*)\>s\(s ([-+]?[0-9]+)sR(?=sobj(?=s endobj(?=rs %[^\r\n]*s)*s stream\r?\ns endstream(?=rc Cs|dkr dS|jj||}|r&|j}|jj||}|rtt|jddkdtt|jddkdt|dkp|tt|jdt|jdkd|j||j|dd\}}|dkr|dfS|j j||}t|d||jfSt| d |j j||}|r^tt|jddkd tt|jddkd tt|jdt|jd|jfS|j j||}|r|j}i}|j j||}xv|s|j|||dd\}}|dkr|dfS|j|||dd\} }| ||<|dkr|dfS|j j||}qW|j}|j j||}|ryt|d } Wn0tttfk r`td |jd dYnX||j|j| } |jj||j| }t|d|j}tt|| }nt|}||fS|jj||}|rL|j}g}|jj||}xN|s>|j|||dd\} }|j| |dkr,|dfS|jj||}qW||jfS|jj||}|rld|jfS|jj||}|rd|jfS|jj||}|rd|jfS|jj||}|rt|j|jd|jfS|jj||}|rt|jd|jfS|j j||}|r0t!|jd|jfS|j"j||}|rt#dd|jdD} t$| ddkr|| jt%dt#j&| j'd|jfS|j(j||}|r|j)||jStdt*|||ddS)NrrzMsz'PdfParser.get_value..0zus-asciizunrecognized object: )NN)+ re_commentrrre_indirect_def_startrLrr rMrre_indirect_def_endre_indirect_reference re_dict_startrre_stream_start TypeErrorrcrrEr: re_stream_endrrre_array_start re_array_endrre_nullre_truere_falserrtr|re_intre_realfloat re_string_hexrr@r?rrA re_string_litget_literal_stringry) r}r~rexpect_indirectrrobjectrr`raZ stream_lenZ stream_dataZ hex_stringrrrrs   &         zPdfParser.get_valuesF(\\[nrtbf()\\])|(\\[0-9]{1,3})|(\\(\r\n|\r|\n))|(\r\n|\r|\n)|(\()|(\))nrr t bf rcCsd}t}x|jj||D]}|j|||j|jdrZ|j|j|jddn|jdr|jt|jddddnt|jdrnh|jdr|jdnR|jdr|jd |d7}n4|jd r|dkrt ||j fS|jd |d8}|j }qWt d dS) Nrrrrrrrzunfinished literal string) rre_lit_str_tokenrrr r  escaped_charsrrrwrrE)r}r~rZ nesting_depthrrrrrr5ps,            zPdfParser.get_literal_stringsxrefs+([0-9]{10}) ([0-9]{5}) ([fn])( \r| \n|\r\n)c Csd}|jj|j||j}t|d|j}x|jj|j|}|sNt|dPd}|j}t|jd}t|jd}xt |||D]}|j j|j|}t|d|j}|jdd k}t|jd} |st|jd| f} t||j kp|j || kd | |j |<qWq.W|S) NFzxref section start not foundzxref subsection start not foundTrrzxref entry not foundrr?z)xref entry duplicated (and not identical)) re_xref_section_startrrrrLrre_xref_subsection_startrr r re_xref_entryr) rNrZsubsection_foundrrZ first_object num_objectsrZis_freerSZ new_entryrrrrs:    zPdfParser.read_xref_tablecCsh|j|d\}}t||dkd|d|d||f|j|j||jt||dd}||j|<|S)Nrrzgexpected to find generation %s for object ID %s in xref table, instead found generation %s at offset %s)r6r)rrLrrrrMr)rNrrrrSrarrrrs   zPdfParser.read_indirectcCsn|dkr|j}t|ddkdg}xD|dD]8}|j|}|ddkrT|j|q.|j|j|dq.W|S)NsTypesPagesz%/Type of page tree node is not /PagessKidssPage)node)rrLrrrr)rNrKrZkidZ kid_objectrrrrs   zPdfParser.linearize_page_tree)NNNrr)N)N)Fr)Nrr)r)N)HrFrGrHrIr_rrrrrrrrrrrrrr staticmethodrrr delimiterZdelimiter_or_ws whitespaceZwhitespace_or_hexZwhitespace_optionalZwhitespace_mandatoryZ newline_onlynewlinerecompileDOTALLr rrrZre_whitespace_optionalrr'rrr rr|r-r.r/r0r1r+r,r3r4r&r$r%r#r(r*rrEr?rFr5rGrHrIrrrrrrrr{~s &     62    " x    r{)rr collectionsrrrPrrZ_utilrr ImportErrorr r r9rDrrErL namedtuplerMrYrZrtrrrrrrr{rrrrs     Z.6