o
    Xzj~  ã                   @   s‚   d Z ddlZddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ee d¡dd… Ž Zejejd	�ZG d
d„ dƒZdS )z6
This module contains the BinaryReader utility class.
é    N)ÚdatetimeÚ	timedeltaÚtimezoneé   )ÚTypeNotFoundError)Ú	tlobjects)Úcore_objectsé   )Útzinfoc                   @   sÀ   e Zd ZdZdd„ Zdd„ Zd/dd„Zd/d	d
„Zdd„ Zdd„ Z	d/dd„Z
d0dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.S )1ÚBinaryReaderz2
    Small utility class to read binary data.
    c                 C   s   |pd| _ d| _d | _d S )Nó    r   )ÚstreamÚpositionÚ_last)ÚselfÚdata© r   ú[/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/extensions/binaryreader.pyÚ__init__   s   

zBinaryReader.__init__c                 C   ó&   t  d| j| j¡\}|  jd7  _|S )zReads a single byte value.z<Bé   ©ÚstructÚunpack_fromr   r   ©r   Úvaluer   r   r   Ú	read_byte   ó   zBinaryReader.read_byteTc                 C   ó2   |rdnd}t  || j| j¡\}|  jd7  _|S )z!Reads an integer (4 bytes) value.z<iz<Ié   r   ©r   ÚsignedÚfmtr   r   r   r   Úread_int$   ó   zBinaryReader.read_intc                 C   r   )z%Reads a long integer (8 bytes) value.z<qz<Qé   r   r    r   r   r   Ú	read_long+   r$   zBinaryReader.read_longc                 C   r   )z,Reads a real floating point (4 bytes) value.z<fr   r   r   r   r   r   Ú
read_float2   r   zBinaryReader.read_floatc                 C   r   )z,Reads a real floating point (8 bytes) value.z<dr%   r   r   r   r   r   Úread_double8   r   zBinaryReader.read_doublec                 C   s   t j|  |d ¡d|d�S )z"Reads a n-bits long integer value.r%   Úlittle)Ú	byteorderr!   )ÚintÚ
from_bytesÚread)r   Úbitsr!   r   r   r   Úread_large_int>   s   ÿzBinaryReader.read_large_intéÿÿÿÿc                 C   s�   |dkr| j | j| j| … }|  j|7  _n| j | jd… }|  jt|ƒ7  _|dkrCt|ƒ|krCtd |t|ƒt|ƒt| jƒ¡ƒ‚|| _|S )z<Read the given amount of bytes, or -1 to read all remaining.r   Nz=No more data left to read (need {}, got {}: {}); last read {})r   r   ÚlenÚBufferErrorÚformatÚreprr   )r   ÚlengthÚresultr   r   r   r-   C   s   þzBinaryReader.readc                 C   ó   | j S )z?Gets the byte array representing the current buffer as a whole.©r   ©r   r   r   r   Ú	get_bytesT   ó   zBinaryReader.get_bytesc                 C   sr   |   ¡ }|dkr|   ¡ |   ¡ d> B |   ¡ d> B }|d }n|}|d d }|  |¡}|dkr7d| }|  |¡ |S )zi
        Reads a Telegram-encoded byte array, without the need of
        specifying its length.
        éþ   r%   é   r   r   r   )r   r-   )r   Ú
first_byter5   Úpaddingr   r   r   r   Útgread_bytes\   s   
ÿ


zBinaryReader.tgread_bytesc                 C   s   t |  ¡ ddd�S )z Reads a Telegram-encoded string.zutf-8Úreplace)ÚencodingÚerrors)Ústrr@   r9   r   r   r   Útgread_stringq   s   zBinaryReader.tgread_stringc                 C   s6   | j dd�}|dkrdS |dkrdS td t|ƒ¡ƒ‚)zReads a Telegram boolean value.F©r!   ì   µuä2 Tì   7óx zInvalid boolean code {})r#   ÚRuntimeErrorr3   Úhexr   r   r   r   Útgread_boolu   s   zBinaryReader.tgread_boolc                 C   s   |   ¡ }tt|d� S )zbReads and converts Unix time (used by Telegram)
           into a Python datetime object.
        )Úseconds)r#   Ú_EPOCHr   r   r   r   r   Útgread_date   s   zBinaryReader.tgread_datec                    sª   ˆ j dd�}t |d¡}|du rP|}|dkrdS |dkrdS |dkr/‡ fdd	„tˆ   ¡ ƒD ƒS t |d¡}|du rPˆ  d
¡ ˆ  ¡ }t|ˆ  ¡ ƒ}ˆ  	|¡ |‚| 
ˆ ¡S )zReads a Telegram object.FrF   NrG   TrH   éÄµc                    ó   g | ]}ˆ   ¡ ‘qS r   ©Útgread_object©Ú.0Ú_r9   r   r   Ú
<listcomp>“   ó    z.BinaryReader.tgread_object.<locals>.<listcomp>éüÿÿÿ)r#   r   ÚgetÚranger   ÚseekÚtell_positionr   r-   Úset_positionÚfrom_reader)r   Úconstructor_idÚclazzr   ÚposÚerrorr   r9   r   rR   †   s$   


zBinaryReader.tgread_objectc                    s6   dˆ j dd�krtdƒ‚ˆ   ¡ }‡ fdd„t|ƒD ƒS )z,Reads a vector (a list) of Telegram objects.rO   FrF   z-Invalid constructor code, vector was expectedc                    rP   r   rQ   rS   r9   r   r   rV   ¦   rW   z.BinaryReader.tgread_vector.<locals>.<listcomp>)r#   rI   rZ   )r   Úcountr   r9   r   Útgread_vector    s   zBinaryReader.tgread_vectorc                 C   s
   d| _ dS )z.Closes the reader, freeing the BytesIO stream.r   Nr8   r9   r   r   r   Úcloseª   ó   
zBinaryReader.closec                 C   r7   )z)Tells the current position on the stream.©r   r9   r   r   r   r\   °   r;   zBinaryReader.tell_positionc                 C   s
   || _ dS )z(Sets the current position on the stream.Nrg   )r   r   r   r   r   r]   ´   rf   zBinaryReader.set_positionc                 C   s   |  j |7  _ dS )zz
        Seeks the stream position given an offset from the current position.
        The offset may be negative.
        Nrg   )r   Úoffsetr   r   r   r[   ¸   s   zBinaryReader.seekc                 C   s   | S ©Nr   r9   r   r   r   Ú	__enter__Ã   s   zBinaryReader.__enter__c                 C   s   |   ¡  d S ri   )re   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__Æ   s   zBinaryReader.__exit__N)T)r0   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r&   r'   r(   r/   r-   r:   r@   rE   rK   rN   rR   rd   re   r\   r]   r[   rj   rn   r   r   r   r   r      s.    	





r   )rr   r   Útimer   r   r   rC   r   Útl.alltlobjectsr   Útl.corer   ÚgmtimeÚ_EPOCH_NAIVErA   ÚutcrM   r   r   r   r   r   Ú<module>   s    