a
    zn„fÖJ  ã                   @  sX  d Z ddlmZ ddlZddlZddlZddlZG dd„ dƒZG dd„ dƒZG dd	„ d	eeƒZ	G d
d„ dƒZ
G dd„ dee
ƒZG dd„ dƒZG dd„ deeƒZG dd„ dƒZG dd„ deeƒZejje	ejjeejjeejjeiZeƒ Ze
ƒ Zeƒ Zeƒ Ze d¡Ze d¡Zd$dd„Zdd„ Zdd„ Zee_ee_dZ e d¡Z!d dd d!œd"d#„Z"dS )%z¥
This module defines an interface to compression algorithms used by ROOT, as well
as functions for compressing and decompressing a :doc:`uproot.source.chunk.Chunk`.
é    )ÚannotationsNc                   @  sd   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZe	d
d„ ƒZ
e	dd„ ƒZe	dd„ ƒZdd„ ZdS )ÚCompressionzU
    Abstract class for objects that describe compression algorithms and levels.
    c                 C  s
   || _ d S ©N©Úlevel©Úselfr   © r	   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/compression.pyÚ__init__   s    zCompression.__init__c                 C  s   t | ƒj› d| j› d�S )Nú(ú))ÚtypeÚ__name__Ú_level©r   r	   r	   r
   Ú__repr__   s    zCompression.__repr__c                 C  s   |   |d |d ¡S )zn
        Constructs a :doc:`uproot.compression.Compression` from a raw
        ``fCompress`` integer.
        éd   )Úfrom_code_pair)ÚclsÚcoder	   r	   r
   Ú	from_code   s    zCompression.from_codec                 C  s:   |dks|dkrdS |t v r(t | |ƒS td|› �ƒ‚dS )zŽ
        Constructs a :doc:`uproot.compression.Compression` from a pair of
        integers representing ``algorithm`` and ``level``.
        r   Nz)unrecognized compression algorithm code: )Úalgorithm_codesÚ
ValueError)r   Ú	algorithmr   r	   r	   r
   r   %   s
    zCompression.from_code_pairc                 C  s   | j \}}|d | S )zd
        This :doc:`uproot.compression.Compression` as a raw ``fCompress``
        integer.
        r   )Ú	code_pair)r   r   r   r	   r	   r
   r   2   s    
zCompression.codec                 C  sB   t  ¡ D ]"\}}t| ƒ|u r|| jf  S qtdt| ƒ› �ƒ‚dS )z
        This :doc:`uproot.compression.Compression` as a 2-tuple of integers
        representing algorithm and level.
        zunrecognized compression type: N)r   Úitemsr   r   r   )r   Úconstr   r	   r	   r
   r   ;   s    zCompression.code_pairc                 C  s   | j S )z´
        The compression level: 0 is uncompressed, 1 is minimally compressed, and
        higher levels increase compression. (See specific algorithms for maximum values.)
        ©r   r   r	   r	   r
   r   G   s    zCompression.levelc                 C  s*   t |tƒr"| j|jko | j|jkS dS d S )NF)Ú
isinstancer   Únamer   )r   Úotherr	   r	   r
   Ú__eq__O   s    
zCompression.__eq__N)r   Ú
__module__Ú__qualname__Ú__doc__r   r   Úclassmethodr   r   Úpropertyr   r   r   r"   r	   r	   r	   r
   r      s   




r   c                   @  s.   e Zd ZdZdZdZdZd
dddœdd	„ZdS )Ú_DecompressZLIBÚZLIBs   ZLó   ÚzlibNÚbytes©ÚdataÚreturnc                 C  sŒ   |d u rt dƒ‚| jdkr0dd l}|j||d�S | jdkrTtj ¡ j}|j||d�S | jdkrvtj ¡ }|j	||d�S t d| j›d�ƒ‚d S )	Nz<zlib decompression requires the number of uncompressed bytesr+   r   )ÚbufsizeÚisalÚdeflateúunrecognized ZLIB.library: ú,; must be one of ['zlib', 'isal', 'deflate'])
r   Úlibraryr+   Ú
decompressÚuprootÚextrasr1   Ú	isal_zlibr2   Zzlib_decompress)r   r.   Úuncompressed_bytesr+   r9   r2   r	   r	   r
   r6   \   s     ÿ



ÿz_DecompressZLIB.decompress)N)r   r#   r$   r    Ú_2byteÚ_methodr5   r6   r	   r	   r	   r
   r(   V   s
   r(   c                   @  sB   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdddœd	d
„ZdS )r)   aË  
    Args:
        level (int, 0-9): Compression level: 0 is uncompressed, 1 is minimally
            compressed, and 9 is maximally compressed.

    Represents the ZLIB compression algorithm.

    If ``ZLIB.library`` is ``"zlib"`` (default), Uproot uses ``zlib`` from the
    Python standard library.

    If ``ZLIB.library`` is ``"isal"``, Uproot uses ``isal.isal_zlib``.

    If ``ZLIB.library`` is ``"deflate"``, Uproot uses ``deflate.deflate_zlib``.
    c                 C  s   t  | ¡ t | |¡ d S r   )r(   r   r   r   r	   r	   r
   r   „   s    
zZLIB.__init__c                 C  s   | j S )ag  
        The compression level: 0 is uncompressed, 1 is minimally compressed, and
        9 is maximally compressed.

        This value to adapted to the ISAL compression levels if that library is used.
        Note: with ISAL 0 is lowest compression, not uncompressed!
              as such, we don't allow 0 in isal mode for compatibility reasons.
        r   r   r	   r	   r
   r   ˆ   s    
z
ZLIB.levelc                 C  s@   t j |¡stdƒ‚d|  kr(dks2n tdƒ‚t|ƒ| _d S ©Nú$Compression level must be an integerr   é	   z5Compression level must be between 0 and 9 (inclusive)©r7   Z_utilZisintÚ	TypeErrorr   Úintr   ©r   Úvaluer	   r	   r
   r   ”   s
    r,   r-   c                 C  s°   | j dkr"dd l}|j|| jd�S | j dkrbtj ¡ j}| jdkrJtdƒ‚|j|t	| jd ƒd�S | j dkrštj 
¡ }| jdkrˆtdƒ‚| |t	| jƒ¡S td	| j ›d
�ƒ‚d S )Nr+   r   r   r1   zdZLIB.library="isal", and therefore requesting no compression implicitly with level 0 is not allowed.é   r2   zgZLIB.library="deflate", and therefore requesting no compression implicitly with level 0 is not allowed.r3   r4   )r5   r+   Úcompressr   r7   r8   r1   r9   r   Úroundr2   Zzlib_compress)r   r.   r+   r9   r2   r	   r	   r
   rF   œ   s(    


ÿ


ÿÿzZLIB.compressN©	r   r#   r$   r%   r   r'   r   ÚsetterrF   r	   r	   r	   r
   r)   t   s   

r)   c                   @  s*   e Zd ZdZdZdZd	dddœdd„ZdS )
Ú_DecompressLZMAÚLZMAs   XZó    Nr,   r-   c                 C  sb   t j ¡ }t|dd ƒp(tt|dd ƒdd ƒ}|d u rDdd l}| |¡S |d u rTtdƒ‚|j||d�S )NÚxzÚexperimentalÚlzmar   z<lzma decompression requires the number of uncompressed bytes©Z
output_len)r7   r8   ÚcramjamÚgetattrrO   r6   r   )r   r.   r:   rQ   rO   r	   r	   r
   r6   ¿   s    
ÿ
ÿz_DecompressLZMA.decompress)N©r   r#   r$   r    r;   r<   r6   r	   r	   r	   r
   rJ   º   s   rJ   c                   @  sB   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdddœd	d
„ZdS )rK   zý
    Args:
        level (int, 0-9): Compression level: 0 is uncompressed, 1 is minimally
            compressed, and 9 is maximally compressed.

    Represents the LZMA compression algorithm.

    Uproot uses ``lzma`` from the ``cramjam`` package.
    c                 C  s   t  | ¡ t | |¡ d S r   )rJ   r   r   r   r	   r	   r
   r   Ú   s    
zLZMA.__init__c                 C  s   | j S )z}
        The compression level: 0 is uncompressed, 1 is minimally compressed, and
        9 is maximally compressed.
        r   r   r	   r	   r
   r   Þ   s    z
LZMA.levelc                 C  s@   t j |¡stdƒ‚d|  kr(dks2n tdƒ‚t|ƒ| _d S r=   r@   rC   r	   r	   r
   r   æ   s
    r,   r-   c                 C  sJ   t j ¡ }t|dd ƒp(tt|dd ƒdd ƒ}|d u r:dd l}|j|| jd�S )NrM   rN   rO   r   )Úpreset)r7   r8   rQ   rR   rO   rF   r   )r   r.   rQ   rO   r	   r	   r
   rF   î   s    
ÿzLZMA.compressNrH   r	   r	   r	   r
   rK   Ï   s   


rK   c                   @  s*   e Zd ZdZdZdZd	dddœdd„ZdS )
Ú_DecompressLZ4ÚLZ4s   L4ó   Nr,   r-   c                 C  s*   t j ¡ j}|d u rtdƒ‚|j||d�S )NzAlz4 block decompression requires the number of uncompressed bytesrP   )r7   r8   rQ   Úlz4r   Zdecompress_block)r   r.   r:   rX   r	   r	   r
   r6   ý   s    ÿz_DecompressLZ4.decompress)NrS   r	   r	   r	   r
   rU   ø   s   rU   c                   @  sB   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdddœd	d
„ZdS )rV   a  
    Args:
        level (int, 0-9): Compression level: 0 is uncompressed, 1 is minimally
            compressed, and 9 is maximally compressed.

    Represents the LZ4 compression algorithm.

    The ``cramjam`` and ``xxhash`` libraries must be installed.
    c                 C  s   t  | ¡ t | |¡ d S r   )rU   r   r   r   r	   r	   r
   r     s    
zLZ4.__init__c                 C  s   | j S )z~
        The compression level: 0 is uncompressed, 1 is minimally compressed, and
        12 is maximally compressed.
        r   r   r	   r	   r
   r     s    z	LZ4.levelc                 C  s@   t j |¡stdƒ‚d|  kr(dks2n tdƒ‚t|ƒ| _d S )Nr>   r   é   z6Compression level must be between 0 and 12 (inclusive)r@   rC   r	   r	   r
   r     s
    r,   r-   c                 C  s   t j ¡ j}|j|| jdd�S )NF)ÚcompressionZ
store_size)r7   r8   rQ   rX   Zcompress_blockr   )r   r.   rX   r	   r	   r
   rF   %  s    zLZ4.compressNrH   r	   r	   r	   r
   rV     s   


rV   c                   @  s*   e Zd ZdZdZdZd	dddœdd„ZdS )
Ú_DecompressZSTDÚZSTDs   ZSrW   Nr,   r-   c                 C  s*   t j ¡ j}|d u rtdƒ‚|j||d�S )NzBzstd block decompression requires the number of uncompressed bytesrP   )r7   r8   rQ   Úzstdr   r6   )r   r.   r:   r]   r	   r	   r
   r6   /  s    ÿz_DecompressZSTD.decompress)NrS   r	   r	   r	   r
   r[   *  s   r[   c                   @  sB   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdddœd	d
„ZdS )r\   zõ
    Args:
        level (int, 0-9): Compression level: 0 is uncompressed, 1 is minimally
            compressed, and 9 is maximally compressed.

    Represents the ZSTD compression algorithm.

    The ``cramjam`` library must be installed.
    c                 C  s    t  | ¡ t | |¡ d | _d S r   )r[   r   r   Ú_compressorr   r	   r	   r
   r   C  s    
zZSTD.__init__c                 C  s   | j S )z~
        The compression level: 0 is uncompressed, 1 is minimally compressed, and
        22 is maximally compressed.
        r   r   r	   r	   r
   r   H  s    z
ZSTD.levelc                 C  s@   t j |¡stdƒ‚d|  kr(dks2n tdƒ‚t|ƒ| _d S )Nr>   r   é   z6Compression level must be between 0 and 22 (inclusive)r@   rC   r	   r	   r
   r   P  s
    r,   r-   c                 C  s   t j ¡ j}|j|| jd�S )Nr   )r7   r8   rQ   r]   rF   r   )r   r.   r]   r	   r	   r
   rF   X  s    zZSTD.compressNrH   r	   r	   r	   r
   r\   8  s   


r\   Z	2sBBBBBBBz>Qc                 C  s¦  |dksJ ‚|dksJ ‚|  ¡ }d}d}| |¡|k �r’tj| |||||||d� | | t|¡\}	}
}}}}}}||d>  |d>  }||d>  |d>  }|	tjkr¶t}| | ||¡}nð|	t	jkrÔt	}| | ||¡}nÒ|	t
jk�rJt
}|d8 }| | t|¡}| | ||¡}tj ¡ }| |¡ ¡ }||k�r¦td|› d|› d| jj› �ƒ‚n\|	tjk�rjt}| | ||¡}n<|	dk�rŽtd	|	› d
| jj› �ƒ‚ntd|	› d| jj› �ƒ‚|du�rÂ| |j||f¡ | ||¡}t|ƒ|k�r
td|› d|› dt|ƒ› d|› d| jj› �
ƒ‚tj|tjjjjd�}|dk�rT||k�r>|}�q’ntj|tjjjjd�}||||| …< ||7 }|d7 }tj| ||||||||d�	 q(tjjj  | j|¡S )a  
    Args:
        chunk (:doc:`uproot.source.chunk.Chunk`): Buffer of contiguous data
            from the file :doc:`uproot.source.chunk.Source`.
        cursor (:doc:`uproot.source.cursor.Cursor`): Current position in
            that ``chunk``.
        context (dict): Auxiliary data used in deserialization.
        compressed_bytes (int): Number of compressed bytes to decompress.
        uncompressed_bytes (int): Number of uncompressed bytes to expect after
            decompression.
        block_info (None or empty list): List to fill with
            ``(compression type class, num compressed bytes, num uncompressed bytes)``
            observed in each compressed block.

    Decompresses ``compressed_bytes`` of a :doc:`uproot.source.chunk.Chunk`
    of data, starting at the ``cursor``.

    This function parses ROOT's 9-byte compression headers (17 bytes for LZ4
    because it includes a checksum), combining blocks if there are more than
    one, returning the result as a new :doc:`uproot.source.chunk.Chunk`.
    r   )ÚchunkÚcursorÚcontextÚcompressed_bytesr:   ÚstartÚfilledÚ
num_blocksé   é   zcomputed checksum z  didn't match expected checksum z	
in file s   CSz#unsupported compression algorithm: zG (according to ROOT comments, it hasn't been used in 20 years!
in file z$unrecognized compression algorithm: Nz!after successfully decompressing z$ blocks, a block of compressed size z decompressed to z% bytes, but the block header expects z bytes.
in file )Zdtypeé   )	r`   ra   rb   rc   r:   rd   re   rf   Úoutput)!ÚcopyZdisplacementr6   Úhook_before_blockÚfieldsÚ_decompress_header_formatÚ_decompress_ZLIBr;   r,   Ú_decompress_LZMAÚ_decompress_LZ4ÚfieldÚ_decompress_checksum_formatr7   r8   ÚxxhashÚxxh64Ú	intdigestr   ÚsourceÚ	file_pathÚ_decompress_ZSTDÚappendr    ÚlenÚnumpyZ
frombufferr`   ZChunkZ_dtypeÚemptyÚhook_after_blockÚwrap)r`   ra   rb   rc   r:   Z
block_inford   re   rf   ÚalgoÚmethodÚc1Úc2Úc3Úu1Úu2Úu3Zblock_compressed_bytesZblock_uncompressed_bytesZdecompressorr.   Zexpected_checksumrt   Úcomputed_checksumZuncompressed_bytestringZuncompressed_arrayrj   r	   r	   r
   r6   m  sÈ    øÿ

ÿ

ÿÿ
þÿÿÿ

ÿÿÿÿþýÿÿ

ÿ÷r6   c                  K  s   d S r   r	   ©Úkwargsr	   r	   r
   rl   ý  s    rl   c                  K  s   d S r   r	   r‰   r	   r	   r
   r~     s    r~   iÿÿÿ z<Ir,   )r.   rZ   r/   c                 C  s
  |du s|j dkr| S g }| }t|ƒdkrâ|dt… |td…  }}| |¡}t|ƒ}t|tƒrŒtj ¡ }| 	|¡ 
¡ }t |¡}	|d7 }nd}	t t|ƒ¡dd… }
t |¡dd… }| |j|j | |
 |	 ¡ | |¡ qd |¡}t|ƒt| ƒk �r|S | S dS )aÌ  
    Args:
        data (bytes, memoryview, or NumPy array): Data to compress.
        compression (:doc:`uproot.compression.Compression`): Algorithm and level
            to use in compressing the data.

    Compress ``data`` using ``compression`` and return a bytes object (or the
    original object in some circumstances).

    This function generates ROOT's 9-byte compression headers (17 bytes for LZ4
    because it includes a checksum), with multiple blocks for each 2**24 - 1 bytes
    of input. The return value has all blocks concatenated into a single bytes
    object, and the splitting into multiple blocks is necessary because the compression
    headers can only specify uncompressed and compressed sizes up to 2**24 - 1.

    If the compression level (``compression.level``) is zero or the compressed
    output would be larger than the input, the input is returned instead, in whatever
    format (bytes, memoryview, or NumPy array) it was provided.
    Nr   rg   ó    éÿÿÿÿ)r   r{   Ú
_3BYTE_MAXrF   r   rV   r7   r8   rt   ru   rv   rs   ÚpackÚ_4byterz   r;   r<   Újoin)r.   rZ   ÚoutÚnextÚblockÚ
compressedZlen_compressedrt   rˆ   ZchecksumÚuncompressed_sizeZcompressed_sizer	   r	   r
   rF     s@    




ÿþýüÿ
rF   )N)#r%   Ú
__future__r   Ústructr|   r7   Zuproot.constr   r(   r)   rJ   rK   rU   rV   r[   r\   r   ZkZLIBZkLZMAZkLZ4ZkZSTDr   ro   rp   rq   ry   ÚStructrn   rs   r6   rl   r~   r�   r�   rF   r	   r	   r	   r
   Ú<module>   sF   DF)$&ü

 ÿ
 
