o
    §…ËY²]  ã                   @   s8  d 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	m
Z
mZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dZdZejZd	Zd
ZdZdZdZ dZ!dZ"e#ddƒD ]Z$e"d	e$> B Z"q]G dd„ dƒZ%G dd„ deƒZ&dZ'er~dd„ Z(ndd„ Z(dd„ Z)dd„ Z*ed +dd„ e' ,¡  -¡ D ƒ¡ƒZ.dBd d!„Z/d"d#„ Z0e.d
dfd$d%„Z1d&d'„ Z2dBd(d)„Z3d*d+„ Z4d,d-„ Z5				.dCd/d0„Z6G d1d2„ d2e%eƒZ7				.dCd3d4„Z8				.dCd5d6„Z9				.dCd7d8„Z:d9d:„ Z;d;d<„ Z<e=d=k�rd>d?„ ej>D ƒZ?e?�r
d@d?„ ej>D ƒe_>e@e?ƒZdAej>v �re5ƒ  dS e<ƒ  dS dS )Dz3.3.0é    N)Ú
getBytesIOÚmd5ÚasBytesÚint2ByteÚchar2intÚ
rawUnicodeÚrawBytesÚisPy3)ÚCanvas)Úpdfutils)Ú	PDFObject)ÚFlowable)Ú	rl_configÚasciié   é   é   é   é   é    é   é   c                   @   sL   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zddd„Zdd„ Z	dd„ Z
dS )ÚStandardEncryptionr   Nr   é(   c                 C   s^   || _ || _|dkrd| _n|dkrd| _|| _|| _|| _|| _d | _ | _ | _	| _
dS )a*  
        This class defines the encryption properties to be used while creating a pdf document.
        Once initiated, a StandardEncryption object can be applied to a Canvas or a BaseDocTemplate.
        The userPassword parameter sets the user password on the encrypted pdf.
        The ownerPassword parameter sets the owner password on the encrypted pdf.
        The boolean flags canPrint, canModify, canCopy, canAnnotate determine wether a user can
        perform the corresponding actions on the pdf when only a user password has been supplied.
        If the user supplies the owner password while opening the pdf, all actions can be performed regardless
        of the flags.
        Note that the security provided by these encryption settings (and even more so for the flags) is very weak.
        r   r   é€   é   N)ÚownerPasswordÚuserPasswordÚrevisionÚcanPrintÚ	canModifyÚcanCopyÚcanAnnotateÚOÚUÚPÚkey)Úselfr   r   r   r    r!   r"   Ústrength© r)   úJ/var/www/html/env/lib/python3.10/site-packages/reportlab/lib/pdfencrypt.pyÚ__init__"   s   zStandardEncryption.__init__c                 C   s   | | _  | _ | _| _d S ©N)r   r    r!   r"   )r'   Úvaluer)   r)   r*   ÚsetAllPermissions9   s   ýÿz$StandardEncryption.setAllPermissionsc                 C   sH   d}| j r	|tB }| jr|tB }| jr|tB }| jr|tB }|tB }|S )Nr   )	r   Ú	printabler    Ú
modifiabler!   Úcopypastabler"   ÚannotatableÚ
higherbits)r'   Úpr)   r)   r*   ÚpermissionBits>   s   z!StandardEncryption.permissionBitsc                 C   s:   | j stdƒ‚| jdu rtdƒ‚t| j| j| j|| jd�S )zencode a string, stream, textúencryption not prepared!Nznot registered in PDF object©r   )ÚpreparedÚ
ValueErrorÚobjnumÚ	encodePDFr&   Úversionr   )r'   Útr)   r)   r*   ÚencodeF   s
   
zStandardEncryption.encodec                 C   s>  t r	td| j ƒ | jrtdƒ‚|r|}n| ¡ }|j ¡ }tr"d}t r8td| j	 ƒ td| j
 ƒ td| ƒ t|  ¡ d ƒ| _trFd| _t rQtd	t| jƒ ƒ t| j	| j
| jƒ| _t rftd
t| jƒ ƒ t| j	| j| j|| jd�| _t rtdt| jƒ ƒ t| j| j|d�| _t r”tdt| jƒ ƒ d  | _| _d| _d S )Nz-StandardEncryption.prepare(...) - revision %dzencryption already prepared!ÚxxxxxxxxxxxxxxxxzuserPassword    = %szownerPassword   = %szinternalID      = %sl        iÔÿÿÿzself.P          = %szself.O (as hex) = %sr7   zself.key (hex)  = %s)r   Ú
documentIdzself.U (as hex) = %sr   )ÚDEBUGÚprintr   r8   r9   ÚIDÚ	signatureÚdigestÚ	CLOBBERIDr   r   Úintr5   r%   ÚCLOBBERPERMISSIONSÚreprÚcomputeOr#   ÚhexTextÚencryptionkeyr&   ÚcomputeUr$   r:   r<   )r'   ÚdocumentÚ
overrideIDÚ
internalIDÚ
externalIDr)   r)   r*   ÚprepareM   s8   


zStandardEncryption.preparec                 C   s   | j stdƒ‚|| _|| _d S )Nr6   )r8   r9   r:   r<   )r'   r:   r<   r)   r)   r*   Úregisters   s   
zStandardEncryption.registerc                 C   s&   | j stdƒ‚t| j| j| j| jd�S )Nr6   ©r#   r$   r%   r   )r8   r9   ÚStandardEncryptionDictionaryr#   r$   r%   r   ©r'   r)   r)   r*   Úinfoy   s   zStandardEncryption.info©Nr   r   r   r   r   r,   )Ú__name__Ú
__module__Ú__qualname__r8   r+   r.   r5   r>   rR   rS   rW   r)   r)   r)   r*   r       s    

&r   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )rU   r   c                 C   s    |||| _ | _| _|| _d S r,   rT   )r'   r#   r$   r%   r   r)   r)   r*   r+   �   s   
z%StandardEncryptionDictionary.__init__c           	      C   s„   ddl m}m}m}m} |ƒ }|dƒt| jƒt| jƒ| jdœ}| j	dkr1d|d< d|d< d	|d
< nd	|d< d|d
< ||ƒ}| 
|¡S )Nr   )ÚDummyDocÚPDFDictionaryÚ	PDFStringÚPDFNameÚStandard)ÚFilterr#   r$   r%   r   r   ÚLengthÚRr   ÚVr   )Úreportlab.pdfbase.pdfdocr]   r^   r_   r`   rK   r#   r$   r%   r   Úformat)	r'   rN   r]   r^   r_   r`   ÚdummyÚdictÚpdfdictr)   r)   r*   rg   „   s   ý


z#StandardEncryptionDictionary.formatN)rY   rZ   r[   Ú__RefOnly__r+   rg   r)   r)   r)   r*   rU      s    rU   za
28 BF 4E 5E 4E 75 8A 41 64 00 4E 56 FF FA 01 08
2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A
c                    s"   ˆ dkr|S t ‡ fdd„|D ƒƒS )z9xor's each byte of the key with the number, which is <256r   c                 3   s   � | ]}ˆ |A V  qd S r,   r)   ©Ú.0Úk©Únumr)   r*   Ú	<genexpr>Ÿ   s   € úxorKey.<locals>.<genexpr>)Úbytes©rp   r&   r)   ro   r*   ÚxorKeyœ   s   ru   c                    s$   ˆ dkr|S d  ‡ fdd„|D ƒ¡S )z:xor's each bytes of the key with the number, which is <256r   Ú c                 3   s    � | ]}t ˆ t|ƒA ƒV  qd S r,   )ÚchrÚordrl   ro   r)   r*   rq   ¤   s   € rr   )Újoinrt   r)   ro   r*   ru   ¡   s   c                 C   s    dd  dd„ t| ƒD ƒ¡ d S )z'a legitimate way to show strings in PDFú<rv   c                 s   s   � | ]	}d t |ƒ V  qdS )z%02XN)rx   ©rm   Úcr)   r)   r*   rq   ¨   s   € zhexText.<locals>.<genexpr>ú>)ry   r   )Útextr)   r)   r*   rK   ¦   s    rK   c                 C   s~   t | d ddƒ t | d ddƒ | dd… } d}ttt| ƒd ƒƒD ]}| |d	 |d	 d	 … }ttd
| ƒƒ}|| }q"|S )Nr   rz   zbad hex textéÿÿÿÿr}   r   ó    g       @r   Ú0x)ÚequalityCheckÚrangerG   Úlenr   Úeval)rK   ÚoutÚiÚsliceÚcharr)   r)   r*   Ú	unHexTextª   s   
rŠ   rv   c                 c   s   � | ]
}t t|d ƒƒV  qdS )r   N)rw   rG   r{   r)   r)   r*   rq   µ   s   € rq   c              
   C   sþ   t | ƒt } | d d… } |}d}tdƒD ]}|d@ }|d? }|t|d ƒ7 }qtt | ƒƒ}	|	 t |ƒ¡ |	 t |ƒ¡ |	 t |ƒ¡ |	 ¡ }
|dkrQ|
d d… }n|d	krhtd
ƒD ]}t|
ƒ ¡ }
qY|
d d… }tr}tdt	dd„ | |||||fD ƒƒ ƒ |S )Nr   r€   r   éÿ   r   é   r   é   r   é2   r   z"encryptionkey(%s,%s,%s,%s,%s)==>%sc                 S   ó   g | ]}t t|ƒƒ‘qS r)   ©rK   Ústr©rm   Úxr)   r)   r*   Ú
<listcomp>Ô   ó    z!encryptionkey.<locals>.<listcomp>)
r   Ú	PadStringrƒ   r   r   ÚupdaterE   rA   rB   Útuple)ÚpasswordÚOwnerKeyÚPermissionsÚFileId1r   r4   ÚpermissionsStringr‡   ÚbyteÚhashÚ	md5outputr&   r“   r)   r)   r*   rL   ·   s*   *rL   c                 C   s6  ddl m} |dv sJ d| ƒ‚|s| }t|ƒt }|dd… }t| ƒt }|d d… }t|ƒ ¡ }trJtdttƒt|ƒt|ƒt|ƒt|ƒ|f ƒ |dkrZ||d d… ƒ 	|¡}n,|d	kr†t
d
ƒD ]}	t|ƒ ¡ }qb|d d… }|}t
dƒD ]}	t|	|ƒ}
||
ƒ 	|¡}qwtr™tdtdd„ | |||fD ƒƒ ƒ |S )Nr   ©ÚArcIV)r   r   zUnknown algorithm revision %sr   zEPadString=%s
ownerPad=%s
password=%s
userPad=%s
digest=%s
revision=%sr   r�   r   rŽ   r   é   zcomputeO(%s,%s,%s)==>%sc                 S   r�   r)   r�   r’   r)   r)   r*   r”   ð   r•   zcomputeO.<locals>.<listcomp>)Úreportlab.lib.arcivr¢   r   r–   r   rE   rA   rB   r   r>   rƒ   ru   r˜   )r   r   r   r¢   ÚownerPadr™   ÚuserPadrE   r#   r‡   ÚthisKeyr)   r)   r*   rJ   ×   s,   0
&rJ   c           
   	   C   sà   ddl m} |dkr|| ƒ |¡}nH|dkrZ|d usJ dƒ‚ttƒ}| t|ƒ¡ | ¡ }|| ƒ |¡}tddƒD ]}t	|| ƒ}	||	ƒ |¡}q9t
|ƒdk rX|d	7 }t
|ƒdk sN|}trntd
tdd„ | ||||fD ƒƒ ƒ |S )Nr   r¡   r   r   z+Revision 3 algorithm needs the document ID!r   r£   r   ó    zcomputeU(%s,%s,%s,%s)==>%sc                 S   r�   r)   r�   r’   r)   r)   r*   r”     r•   zcomputeU.<locals>.<listcomp>)r¤   r¢   r>   r   r–   r—   r   rE   rƒ   ru   r„   rA   rB   r˜   )
rL   Úencodestringr   r@   r¢   ÚresultÚhÚtmpÚnr§   r)   r)   r*   rM   ó   s$   
ÿ(rM   c                 C   s6   t | |ƒ}|tkrt|ƒttƒkrtdƒ‚tdƒ‚d S )Nz&lengths don't match! (password failed)z;decode of U doesn't match fixed padstring (password failed))rM   r–   r„   r9   )rL   r$   Údecodedr)   r)   r*   ÚcheckU  s   
ýr¯   c              
   C   sÎ   | }|}t dƒD ]}|t|d@ ƒ7 }|d? }q|}t dƒD ]}|t|d@ ƒ7 }|d? }qt|ƒ ¡ }|dkr=|dd… } n|dkrC|} ddlm}	 |	| ƒ |¡}
tretd	t	d
d„ | |||||
fD ƒƒ ƒ |
S )zEncodes a string or streamr   r‹   r   r   Né
   r   r¡   zencodePDF(%s,%s,%s,%s,%s)==>%sc                 S   r�   r)   r�   r’   r)   r)   r*   r”   $  r•   zencodePDF.<locals>.<listcomp>)
rƒ   r   r   rE   r¤   r¢   r>   rA   rB   r˜   )r&   ÚobjectNumberÚgenerationNumberÚstringr   Únewkeyr­   r‡   r    r¢   Ú	encryptedr)   r)   r*   r;     s$   

*r;   c                 C   s   | |ksJ d||| f ƒ‚d S )Nz%s
 expected=%s
 observed=%sr)   )ÚobservedÚexpectedÚlabelr)   r)   r*   r‚   '  s   r‚   c                  C   sÀ   t dddd�} | jd dd� d}d}d	}tt| jƒ|d
ƒ tt| jƒ|dƒ tt| jƒ|dƒ t dddd�} | jd dd� d}d}d}tt| jƒ|dƒ tt| jƒ|dƒ tt| jƒ|dƒ d S )NÚUserÚOwnerr   ©r(   r?   )rO   zB<FA7F558FACF8205D25A7F1ABFA02629F707AE7B0211A2BB26F5DF4C30F684301>zB<09F26CF46190AF8F93B304AD50C16B615DC43C228C9B2D2EA34951A80617B2B1>z<BB2C00EB3D>z40 bit O valuez40 bit U valuez40 bit key valueÚuserpassÚ	ownerpassr   zB<68E5704AC779A5F0CD89704406587A52F25BF61CADC56A0F8DB6C4DB0052534D>zB<A9AE45CDE827FE0B7D6536267948836A00000000000000000000000000000000>z"<13DDE7585D9BE366C976DDD56AF541D1>z128 bit O valuez128 bit U valuez128 key value)r   rR   r‚   rK   r#   r$   r&   )ÚencÚ	expectedOÚ	expectedUÚexpectedKeyr)   r)   r*   Útest.  s    rÂ   r   c           	   	   C   s"   t |||||||d�}|| j_dS )z2Applies encryption to the document being generatedr»   N)r   Ú_docÚencrypt)	Úcanvasr   r   r   r    r!   r"   r(   r¾   r)   r)   r*   ÚencryptCanvasM  s
   þrÆ   c                   @   r\   )ÚEncryptionFlowablez’Drop this in your Platypus story and it will set up the encryption options.

    If you do it multiple times, the last one before saving will win.c                 C   s   dS )N)r   r   r)   )r'   Ú
availWidthÚavailHeightr)   r)   r*   Úwrap_  s   zEncryptionFlowable.wrapc                 C   s&   t | j| j| j| j| j| j| jƒ d S r,   )rÆ   Úcanvr   r   r   r    r!   r"   rV   r)   r)   r*   Údrawb  s   úzEncryptionFlowable.drawN)rY   rZ   r[   Ú__doc__rÊ   rÌ   r)   r)   r)   r*   rÇ   Z  s    rÇ   c                 C   s   t dƒ‚)z*For use in Platypus.  Call before build().zNot implemented yet)Ú	Exception)Údtr   r   r   r    r!   r"   r(   r)   r)   r*   ÚencryptDocTemplatem  s   rÐ   c              
   C   sÜ   z
ddl m}m}	 W n ty   tdƒ‚w || ddd�\}
}t|
 ¡ ƒ}|
d dd… }tƒ }t||d	�}tr<d
|j	_
t||||||||d� |	||ƒ}|D ]}| |
| dd… ¡ | |¡ | ¡  qO| ¡  | ¡ S )a  accepts a PDF file 'as a byte array in memory'; return encrypted one.

    This is a high level convenience and does not touch the hard disk in any way.
    If you are encrypting the same file over and over again, it's better to use
    pageCatcher and cache the results.r   )ÚstoreFormsInMemoryÚrestoreFormsInMemoryz~reportlab.lib.pdfencrypt.encryptPdfInMemory failed because rlextra cannot be imported.
See https://www.reportlab.com/downloadsr   )ÚallÚBBoxesÚ
PageForms0r   N)Úpagesizez&[(xxxxxxxxxxxxxxxx)(xxxxxxxxxxxxxxxx)]r»   )Úrlextra.pageCatcher.pageCatcherrÑ   rÒ   ÚImportErrorÚlistÚkeysr   r
   rF   rÃ   Ú_IDrÆ   ÚsetPageSizeÚdoFormÚshowPageÚsaveÚgetvalue)ÚinputPDFr   r   r   r    r!   r"   r(   rÑ   rÒ   ÚbboxInfoÚpickledFormsÚnamesÚfirstPageSizeÚbufrË   Ú	formNamesÚformNamer)   r)   r*   ÚencryptPdfInMemoryu  s0   
ÿý


ré   c	              
   C   s>   t | dƒ ¡ }	t|	|||||||d�}
t |dƒ |
¡ t|
ƒS )z>Creates encrypted file OUTPUTFILENAME.  Returns size in bytes.Úrbr»   Úwb)ÚopenÚreadré   Úwriter„   )ÚinputFileNameÚoutputFileNamer   r   r   r    r!   r"   r(   rá   Ú	outputPDFr)   r)   r*   ÚencryptPdfOnDiskž  s   ýrò   c                  C   sH  t jd d … } d}g d¢}d}d}d}d}d}d}d}	| d }
t| ƒdd … }t|ƒdk�r|d dks:|d dkr@t|ƒ d S t|ƒd	k rJtd
ƒ‚|d |vrg|d }|dd … }tj |¡sftd| ƒ‚ntdƒ‚d}d|v rxd}| 	d¡ d|v rƒd}| 	d¡ d|v s‹d|v rÂd|v r—| 
d¡}d}nd|v r¢| 
d¡}d}z
t||d  ƒ}W n   d}Y | 	||d  ¡ | 	|¡ nddlm} dd|dfdd|dfdd|dfdd|dfdd|dfdd|dfd d!|d"fd#d!|d"fd$d%|d&fd'd%|d&fd(d)|d*fd+d)|d*fd,d-|	d.fd/d-|	d.ff}d0}|D ]x}|d |v �r‹| 
|d ¡}|d |v �r<||d  d1v�r<td2|d  ƒ‚zE||d  |v�r|d |v �rVt|d d3 ƒ nt|d d4 ƒ |�rotd5|d6 ||d  f ƒ | 	||d  ¡ | 	|d ¡ W �q   d7|d6  ‚�q|d8k�rÉtd9|ƒ td:|ƒ td;|	ƒ td<|ƒ td=|ƒ td>|ƒ td?|ƒ td@|ƒ tdA|ƒ td;|	ƒ tdB|ƒ |	dk�rí|dCd … dDk�sà|dCd … dEk�rç|d dC… }n|}|dF }	t||	|||||||dG�	}|�rtdH|	|||f ƒ t|ƒdk�rtdIt|ƒddJ… |f ƒ‚d S t|ƒ d S )KNa"  PDFENCRYPT USAGE:

PdfEncrypt encrypts your PDF files.

Line mode usage:

% pdfencrypt.exe pdffile [-o ownerpassword] | [owner ownerpassword],
	[-u userpassword] | [user userpassword],
	[-p 1|0] | [printable 1|0],
	[-m 1|0] | [modifiable 1|0],
	[-c 1|0] | [copypastable 1|0],
	[-a 1|0] | [annotatable 1|0],
	[-s savefilename] | [savefile savefilename],
	[-v 1|0] | [verbose 1|0],
	[-e128], [encrypt128],
	[-h] | [help]

-o or owner set the owner password.
-u or user set the user password.
-p or printable set the printable attribute (must be 1 or 0).
-m or modifiable sets the modifiable attribute (must be 1 or 0).
-c or copypastable sets the copypastable attribute (must be 1 or 0).
-a or annotatable sets the annotatable attribute (must be 1 or 0).
-s or savefile sets the name for the output PDF file
-v or verbose prints useful output to the screen.
      (this defaults to 'pdffile_encrypted.pdf').
'-e128' or 'encrypt128' allows you to use 128 bit encryption (in beta).

-h or help prints this message.

See PdfEncryptIntro.pdf for more information.
)ú-oÚownerú-uÚuserú-pr/   ú-mr0   ú-cr1   ú-ar2   ú-sÚsavefileú-vÚverboseú-hÚhelpú-e128Ú
encrypt128rv   r   zencrypted.pdfr   rÿ   r   r   z2Must include a filename and one or more arguments!zCan't open input file '%s'!z2First argument must be name of the PDF input file!r   r  r   r  rý   rþ   ©rþ   ró   ÚOWNERzOwner passwordrô   rõ   ÚUSERzUser passwordrö   r÷   Ú	PRINTABLEz'Printable'r/   rø   Ú
MODIFIABLEz'Modifiable'r0   rù   ÚCOPYPASTABLEz'Copypastable'r1   rú   ÚANNOTATABLEz'Annotatable'r2   rû   ÚSAVEFILEzOutput filerü   )r÷   r/   rø   r0   r1   rù   r2   rú   )Ú1Ú0z#%s value must be either '1' or '0'!z = int(argv[pos+1])z = argv[pos+1]z%s set to: '%s'.r   zUnable to set %s.r   z
infile:z	STRENGTH:z	SAVEFILE:zUSER:zOWNER:z
PRINTABLE:zMODIFIABLE:zCOPYPASTABLE:zANNOTATABLE:zVERBOSE:éüÿÿÿz.pdfz.PDFz_encrypted.pdfr»   zQwrote output file '%s'(%s bytes)
  owner password is '%s'
  user password is '%s'z4
Unrecognised arguments : %s
known arguments are:
%sr   )ÚsysÚargvrÙ   r„   rB   r9   ÚosÚpathÚisfileÚremoveÚindexrG   Úreportlab.rl_configrþ   Úexecrò   r‘   )Úsys_argvÚusageÚknown_modesr  r  r  r  r  r	  r
  Úcallerr  ÚinfileÚSTRENGTHÚposÚargrþ   ÚarglistÚbinaryrequiredÚthisargÚtinfileÚfilesizer)   r)   r*   ÚscriptInterp­  sØ   !ÿ

















ó€ð












$
þÿr$  c                  C   s   ddl m}  tƒ  d S )Nr   r  )r  rþ   r$  r  r)   r)   r*   ÚmainV  s   
r%  Ú__main__c                 C   s    g | ]}|d d… dkr|‘qS ©Né   z--debugr)   r’   r)   r)   r*   r”   [  ó     r”   c                 C   s    g | ]}|d d… dkr|‘qS r'  r)   r’   r)   r)   r*   r”   ]  r)  z--test)r   rX   )AÚ__version__r  r  ÚtempfileÚreportlab.lib.utilsr   r   r   r   r   r   r   r	   Úreportlab.pdfgen.canvasr
   Úreportlab.pdfbaser   rf   r   Úreportlab.platypus.flowablesr   Ú	reportlabr   r   rF   rH   ÚdebugrA   Ú	reserved1Ú	reserved2r/   r0   r1   r2   r3   rƒ   r‡   r   rU   Úpaddingru   rK   rŠ   ry   ÚstripÚsplitr–   rL   rJ   rM   r¯   r;   r‚   rÂ   rÆ   rÇ   rÐ   ré   rò   r$  r%  rY   r  Úar„   r)   r)   r)   r*   Ú<module>   s‚   (_
 
 
 
ý
ý	
ý*
ý *

ú