o
    Þý°jJ!  ã                   @   s”   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 d dlmZ zd dl	Z	dZ
W n ey7   dZ
Y nw dd„ Zdd	„ Zi i i fd
d„ZdS )é    N)Ú	unhexlify)ÚFileNotFoundErrorTFc                 C   s^  d}g }G dd„ dt ƒ}d}d}d}		 |d7 }| ¡ }
|
s)|dur&| |¡ 	 |S |
 ¡ }
|
 d¡s4|
s7d}	q|
 d¡rM|durE| |¡ d}| |
¡ q|	rg|d7 }d	}	|dur^| |¡ |d
||f |ƒ}t d|
¡}|sx| j|
g7  _n6| d¡ 	¡ }| d¡ 	¡ }| 
|d¡}|du r¦t|ƒd dkrœd| }t||t |¡ƒ nt||||ƒƒ q)z×Load and parse a test vector file

    Return a list of objects, one per group of adjacent
    KV lines or for a single line in the form "[.*]".

    For a group of lines, the object has one attribute per line.
    r   c                   @   s   e Zd Zdd„ ZdS )z_load_tests.<locals>.TestVectorc                 S   s   || _ || _g | _d S )N)ÚdescÚcountÚothers)ÚselfÚdescriptionr   © r	   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/Cryptodome/SelfTest/loader.pyÚ__init__=   s   
z(_load_tests.<locals>.TestVector.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r	   r	   r	   r
   Ú
TestVector<   s    r   NTé   ú#ú[Fz%s (#%d)z([A-Za-z0-9]+) = ?(.*)é   Ú0)ÚobjectÚreadlineÚappendÚstripÚ
startswithÚreÚmatchr   ÚgroupÚlowerÚgetÚlenÚsetattrÚbinasciir   )Ú	dir_compsÚfile_inr   ÚconversionsÚline_numberÚresultsr   Útest_vectorr   Ú	new_groupÚlineÚresÚtokenÚdataÚ
conversionr	   r	   r
   Ú_load_tests0   sT   	
&Û



×r.   c                 C   sÀ   d}zIt sttjt tj¡|ƒ‚d||f }tj tj	¡}tj 
tjj
|g| ¢R Ž |¡}t|ƒ�}t| |||ƒ}W d  ƒ W |S 1 sDw   Y  W |S  ty_   tjd| tdd� Y |S w )a²  Load and parse a test vector file, formatted using the NIST style.

    Args:
        dir_comps (list of strings):
          The path components under the ``pycryptodome_test_vectors`` package.
          For instance ``("Cipher", "AES")``.
        file_name (string):
          The name of the file with the test vectors.
        description (string):
          A description applicable to the test vectors in the file.
        conversions (dictionary):
          The dictionary contains functions.
          Values in the file that have an entry in this dictionary
          will be converted usign the matching function.
          Otherwise, values will be considered as hexadecimal and
          converted to binary.

    Returns:
        A list of test vector objects.

    The file is formatted in the following way:

    - Lines starting with "#" are comments and will be ignored.
    - Each test vector is a sequence of 1 or more adjacent lines, where
      each lines is an assignement.
    - Test vectors are separated by an empty line, a comment, or
      a line starting with "[".

    A test vector object has the following attributes:

    - desc (string): description
    - counter (int): the order of the test vector in the file (from 1)
    - others (list): zero or more lines of the test vector that were not assignments
    - left-hand side of each assignment (lowercase): the value of the
      assignement, either converted or bytes.
    Nz%s test (%s)ú%Warning: skipping extended tests for r   ©Ú
stacklevel)Útest_vectors_availabler   ÚerrnoÚENOENTÚosÚstrerrorÚpathÚdirnameÚpycryptodome_test_vectorsÚ__file__ÚjoinÚopenr.   ÚwarningsÚwarnÚUserWarning)r"   Ú	file_namer   r$   r&   Úinit_dirÚfull_file_namer#   r	   r	   r
   Úload_test_vectorsu   s.   &
þ
ÿøû
þûrC   c                 C   s>  g }z>t sttjt tj¡|ƒ‚tj tj	¡}tj 
tjj
|g| ¢R Ž |¡}t|ƒ�}	t |	¡}
W d   ƒ n1 s:w   Y  W n tyU   tjd| tdd� | Y S w G dd„ dtƒ}h d£}|t| ¡ ƒ8 }i }| ¡ D ]
\}}||
ƒ||< qp|
d D ]�}i }| ¡ D ]
\}}||ƒ||< q‡|d D ]…}|ƒ }| ¡ D ]
\}}t|||ƒ qŸ| ¡ D ]
\}}t|||ƒ q®|d	 |_|d
 |_|D ]%}||v rêzt||t|| ƒƒ W qÅ tjyé   td||j|f ƒ‚w qÅ||_| ¡ D ]\}}t||||ƒƒ qò|d dk|_|d dk|_| d¡|_ ||_| !|¡ q–q|S )Nr/   r   r0   c                   @   s   e Zd ZdS )z0load_test_vectors_wycheproof.<locals>.TestVectorN)r   r   r   r	   r	   r	   r
   r   Ç   s    r   >   ÚctÚivÚaadÚikmÚkeyÚmsgÚokmÚsigÚtagÚinfoÚsaltÚlabelÚpublicÚsharedÚ
testGroupsÚtestsÚtcIdÚcommentz0Error decoding attribute '%s' (tcId=%s, file %s)ÚresultÚinvalidÚ
acceptableÚflags)"r2   r   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   ÚjsonÚloadr=   r>   r?   r   ÚsetÚkeysÚitemsr    ÚidrU   r   r!   ÚErrorÚ
ValueErrorÚfilenameÚvalidÚwarningr   rY   r   )r"   r@   r   Úroot_tagÚ	group_tagÚunit_tagrV   rA   rB   r#   Útv_treer   Úunit_attr_hexÚcommon_rootÚkÚvr   Úcommon_groupÚtestÚtvÚattrr	   r	   r
   Úload_test_vectors_wycheproof²   sp   
þ
ÿ€
þü

ÿýårq   )r5   r   rZ   r3   r!   r=   r   ÚCryptodome.Util.py3compatr   r9   r2   ÚImportErrorr.   rC   rq   r	   r	   r	   r
   Ú<module>   s$   ÿE>ÿ