U
    ÓZjD#  ã                   @   s\   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
mZmZmZ G dd„ deƒZdS )z&
Implementation of an SSH2 "message".
é    N)Úutil)Ú	zero_byteÚmax_byteÚone_byteÚasbytes)ÚlongÚBytesIOÚuÚinteger_typesc                   @   s  e Zd ZdZedƒZd>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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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"dS )?ÚMessagea™  
    An SSH2 message is a stream of bytes that encodes some combination of
    strings, integers, bools, and infinite-precision integers (known in Python
    as longs).  This class builds or breaks down such a byte stream.

    Normally you don't need to deal with anything this low-level, but it's
    exposed for people implementing custom extensions, or features that
    paramiko doesn't support yet.
    l      ~ Nc                 C   s    |dk	rt |ƒ| _nt ƒ | _dS )z¼
        Create a new SSH2 message.

        :param str content:
            the byte stream to use as the message content (passed in only when
            decomposing a message).
        N)r   Úpacket)ÚselfÚcontent© r   úS/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/paramiko/message.pyÚ__init__+   s    zMessage.__init__c                 C   s   |   ¡ S )zX
        Return the byte stream content of this message, as a string/bytes obj.
        )r   ©r   r   r   r   Ú__str__8   s    zMessage.__str__c                 C   s   dt | j ¡ ƒ d S )zP
        Returns a string representation of this object, for debugging.
        zparamiko.Message(ú))Úreprr   Úgetvaluer   r   r   r   Ú__repr__>   s    zMessage.__repr__c                 C   s
   | j  ¡ S )zK
        Return the byte stream content of this Message, as bytes.
        )r   r   r   r   r   r   r   D   s    zMessage.asbytesc                 C   s   | j  d¡ dS )zk
        Rewind the message to the beginning as if no items had been parsed
        out of it yet.
        r   N)r   Úseekr   r   r   r   ÚrewindJ   s    zMessage.rewindc                 C   s$   | j  ¡ }| j  ¡ }| j  |¡ |S )zv
        Return the bytes (as a `str`) of this message that haven't already been
        parsed and returned.
        )r   ÚtellÚreadr   )r   ÚpositionÚ	remainderr   r   r   Úget_remainderQ   s    

zMessage.get_remainderc                 C   s   | j  ¡ }|  ¡  | j  |¡S )zä
        Returns the `str` bytes of this message that have been parsed and
        returned. The string passed into a message's constructor can be
        regenerated by concatenating ``get_so_far`` and `get_remainder`.
        )r   r   r   r   )r   r   r   r   r   Ú
get_so_far[   s    
zMessage.get_so_farc                 C   sD   | j  |¡}d}t|ƒ|  k r(|k r@n n|t|t|ƒ   S |S )a  
        Return the next ``n`` bytes of the message (as a `str`), without
        decomposing into an int, decoded string, etc.  Just the raw bytes are
        returned. Returns a string of ``n`` zero bytes if there weren't ``n``
        bytes remaining in the message.
        i   )r   r   Úlenr   )r   ÚnÚbZmax_pad_sizer   r   r   Ú	get_bytese   s
    zMessage.get_bytesc                 C   s
   |   d¡S )a	  
        Return the next byte of the message, without decomposing it.  This
        is equivalent to `get_bytes(1) <get_bytes>`.

        :return:
            the next (`str`) byte of the message, or ``' '`` if there aren't
            any bytes remaining.
        é   )r#   r   r   r   r   Úget_byter   s    	zMessage.get_bytec                 C   s   |   d¡}|tkS )z2
        Fetch a boolean from the stream.
        r$   )r#   r   ©r   r"   r   r   r   Úget_boolean}   s    
zMessage.get_booleanc                 C   s>   |   d¡}|tkr t |  ¡ ¡S ||   d¡7 }t d|¡d S )zZ
        Fetch an int from the stream.

        :return: a 32-bit unsigned `int`.
        r$   é   ú>Ir   )r#   r   r   Úinflate_longÚ
get_binaryÚstructÚunpack)r   Úbyter   r   r   Úget_adaptive_int„   s
    
zMessage.get_adaptive_intc                 C   s   t  d|  d¡¡d S )z/
        Fetch an int from the stream.
        r)   é   r   ©r,   r-   r#   r   r   r   r   Úget_int�   s    zMessage.get_intc                 C   s   t  d|  d¡¡d S )zk
        Fetch a 64-bit int from the stream.

        :return: a 64-bit unsigned integer (`long`).
        ú>Qé   r   r1   r   r   r   r   Ú	get_int64–   s    zMessage.get_int64c                 C   s   t  |  ¡ ¡S )zs
        Fetch a long int (mpint) from the stream.

        :return: an arbitrary-length integer (`long`).
        )r   r*   r+   r   r   r   r   Ú	get_mpintž   s    zMessage.get_mpintc                 C   s   |   |  ¡ ¡S )zÑ
        Fetch a `str` from the stream.  This could be a byte string and may
        contain unprintable characters.  (It's not unheard of for a string to
        contain another byte-stream message.)
        ©r#   r2   r   r   r   r   Ú
get_string¦   s    zMessage.get_stringc                 C   s   t |  ¡ ƒS )z9
        Fetch a Unicode string from the stream.
        )r	   r8   r   r   r   r   Úget_text®   s    zMessage.get_textc                 C   s   |   |  ¡ ¡S )zÒ
        Fetch a string from the stream.  This could be a byte string and may
        contain unprintable characters.  (It's not unheard of for a string to
        contain another byte-stream Message.)
        r7   r   r   r   r   r+   ´   s    zMessage.get_binaryc                 C   s   |   ¡  d¡S )zŽ
        Fetch a list of `strings <str>` from the stream.

        These are trivially encoded as comma-separated values in a string.
        ú,)r9   Úsplitr   r   r   r   Úget_list¼   s    zMessage.get_listc                 C   s   | j  |¡ | S )zh
        Write bytes to the stream, without any formatting.

        :param str b: bytes to add
        ©r   Úwriter&   r   r   r   Ú	add_bytesÄ   s    zMessage.add_bytesc                 C   s   | j  |¡ | S )zo
        Write a single byte to the stream, without any formatting.

        :param str b: byte to add
        r=   r&   r   r   r   Úadd_byteÍ   s    zMessage.add_bytec                 C   s"   |r| j  t¡ n| j  t¡ | S )za
        Add a boolean value to the stream.

        :param bool b: boolean value to add
        )r   r>   r   r   r&   r   r   r   Úadd_booleanÖ   s    zMessage.add_booleanc                 C   s   | j  t d|¡¡ | S ©zU
        Add an integer to the stream.

        :param int n: integer to add
        r)   ©r   r>   r,   Úpack©r   r!   r   r   r   Úadd_intâ   s    zMessage.add_intc                 C   s@   |t jkr(| j t¡ |  t |¡¡ n| j t 	d|¡¡ | S rB   )
r   Úbig_intr   r>   r   Ú
add_stringr   Údeflate_longr,   rD   rE   r   r   r   Úadd_adaptive_intë   s
    
zMessage.add_adaptive_intc                 C   s   | j  t d|¡¡ | S )zY
        Add a 64-bit int to the stream.

        :param long n: long int to add
        r3   rC   rE   r   r   r   Ú	add_int64ø   s    zMessage.add_int64c                 C   s   |   t |¡¡ | S )z¶
        Add a long int to the stream, encoded as an infinite-precision
        integer.  This method only works on positive numbers.

        :param long z: long int to add
        )rH   r   rI   )r   Úzr   r   r   Ú	add_mpint  s    zMessage.add_mpintc                 C   s&   t |ƒ}|  t|ƒ¡ | j |¡ | S )zR
        Add a string to the stream.

        :param str s: string to add
        )r   rF   r    r   r>   )r   Úsr   r   r   rH     s    zMessage.add_stringc                 C   s   |   d |¡¡ | S )zé
        Add a list of strings to the stream.  They are encoded identically to
        a single string of values separated by commas.  (Yes, really, that's
        how SSH2 does it.)

        :param l: list of strings to add
        r:   )rH   Újoin)r   Úlr   r   r   Úadd_list  s    zMessage.add_listc                 C   sN   t |ƒtkr|  |¡S t|tƒr*|  |¡S t |ƒtkr@|  |¡S |  |¡S d S )N)	ÚtypeÚboolrA   Ú
isinstancer
   rJ   ÚlistrQ   rH   )r   Úir   r   r   Ú_add!  s    



zMessage._addc                 G   s   |D ]}|   |¡ qdS )a  
        Add a sequence of items to the stream.  The values are encoded based
        on their type: str, int, bool, list, or long.

        .. warning::
            Longs are encoded non-deterministically.  Don't use this method.

        :param seq: the sequence of items
        N)rW   )r   ÚseqÚitemr   r   r   Úadd+  s    
zMessage.add)N)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rG   r   r   r   r   r   r   r   r#   r%   r'   r/   r2   r5   r6   r8   r9   r+   r<   r?   r@   rA   rF   rJ   rK   rM   rH   rQ   rW   rZ   r   r   r   r   r      s>   



				

r   )r^   r,   Zparamikor   Zparamiko.commonr   r   r   r   Zparamiko.py3compatr   r   r	   r
   Úobjectr   r   r   r   r   Ú<module>   s
   