o
    U»h$  ć                   @   sV   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZ G dd dZdS )	z&
Implementation of an SSH2 "message".
é    N)ŚBytesIO)Śutil)Ś	zero_byteŚmax_byteŚone_byte)Śuc                   @   sž   e Zd ZdZ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.  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urt || _dS t  | _dS )z¾
        Create a new SSH2 message.

        :param bytes content:
            the byte stream to use as the message content (passed in only when
            decomposing a message).
        N)r   Śpacket)ŚselfŚcontent© r   śC/var/www/html/venv/lib/python3.10/site-packages/paramiko/message.pyŚ__init__,   s   zMessage.__init__c                 C   s   |   ” S ©N)Śasbytes©r
   r   r   r   Ś	__bytes__9   s   zMessage.__bytes__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__<   ó   zMessage.__repr__c                 C   s
   | j  ” S )zO
        Return the byte stream content of this Message, as a `bytes`.
        )r	   r   r   r   r   r   r   C   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   ŚrewindI   s   zMessage.rewindc                 C   s$   | j  ” }| j  ” }| j  |” |S )zk
        Return the `bytes` of this message that haven't already been parsed and
        returned.
        )r	   ŚtellŚreadr   )r
   ŚpositionŚ	remainderr   r   r   Śget_remainderP   s   

zMessage.get_remainderc                 C   s   | j  ” }|  ”  | j  |”S )zą
        Returns the `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_farZ   s   
zMessage.get_so_farc                 C   sF   | j  |”}d}t||  k r|k r!n |S |t|t|   S |S )a	  
        Return the next ``n`` bytes of the message, 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ŚbŚmax_pad_sizer   r   r   Ś	get_bytesd   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 (`bytes`) byte of the message, or ``b' '`` if there
            aren't any bytes remaining.
        é   )r$   r   r   r   r   Śget_byteq   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   ó   t  d|  d””d S )z/
        Fetch an int from the stream.
        r*   é   r   ©r-   r.   r$   r   r   r   r   Śget_int   r   zMessage.get_intc                 C   r1   )zj
        Fetch a 64-bit int from the stream.

        :return: a 64-bit unsigned integer (`int`).
        ś>Qé   r   r3   r   r   r   r   Ś	get_int64   s   zMessage.get_int64c                 C   s   t  |  ” ”S )zr
        Fetch a long int (mpint) from the stream.

        :return: an arbitrary-length integer (`int`).
        )r   r+   r,   r   r   r   r   Ś	get_mpint   ó   zMessage.get_mpintc                 C   ó   |   |  ” ”S )zą
        Fetch a "string" from the stream.  This will actually be a `bytes`
        object, and may contain unprintable characters.  (It's not unheard of
        for a string to contain another byte-stream message.)
        ©r$   r4   r   r   r   r   Ś
get_string§   r9   zMessage.get_stringc                 C   s   t |  ” S )z
        Fetch a Unicode string from the stream.

        This currently operates by attempting to encode the next "string" as
        ``utf-8``.
        )r   r<   r   r   r   r   Śget_text±   s   zMessage.get_textc                 C   r:   )z@
        Alias for `get_string` (obtains a bytestring).
        r;   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.
        ś,)r=   Śsplitr   r   r   r   Śget_listĄ   r9   zMessage.get_listc                 C   ó   | j  |” | S )zj
        Write bytes to the stream, without any formatting.

        :param bytes b: bytes to add
        ©r	   Śwriter'   r   r   r   Ś	add_bytesČ   ó   zMessage.add_bytesc                 C   rA   )zq
        Write a single byte to the stream, without any formatting.

        :param bytes b: byte to add
        rB   r'   r   r   r   Śadd_byteŃ   rE   zMessage.add_bytec                 C   s$   |r
| j  t” | S | j  t” | S )za
        Add a boolean value to the stream.

        :param bool b: boolean value to add
        )r	   rC   r   r   r'   r   r   r   Śadd_booleanŚ   s
   ’zMessage.add_booleanc                 C   ó   | j  t d|”” | S ©zU
        Add an integer to the stream.

        :param int n: integer to add
        r*   ©r	   rC   r-   Śpack©r
   r!   r   r   r   Śadd_intę   ó   zMessage.add_intc                 C   sB   |t jkr| j t” |  t |”” | S | j t 	d|”” | S rI   )
r   Śbig_intr	   rC   r   Ś
add_stringr   Śdeflate_longr-   rK   rL   r   r   r   Śadd_adaptive_intļ   s   
’zMessage.add_adaptive_intc                 C   rH   )zX
        Add a 64-bit int to the stream.

        :param int n: long int to add
        r5   rJ   rL   r   r   r   Ś	add_int64ü   rN   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 int z: long int to add
        )rP   r   rQ   )r
   Śzr   r   r   Ś	add_mpint  s   zMessage.add_mpintc                 C   s(   t  |”}|  t|” | j |” | S )z[
        Add a bytestring to the stream.

        :param byte s: bytestring to add
        )r   r   rM   r    r	   rC   )r
   Śsr   r   r   rP     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>   )rP   Śjoin)r
   Ślr   r   r   Śadd_list  s   zMessage.add_listc                 C   sJ   t |tu r|  |”S t|tr|  |”S t |tu r |  |”S |  |”S r   )	ŚtypeŚboolrG   Ś
isinstanceŚintrR   ŚlistrY   rP   )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: bytes, str, int, bool, or list.

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

        :param seq: the sequence of items
        N)r`   )r
   ŚseqŚitemr   r   r   Śadd3  s   
’zMessage.addr   )"Ś__name__Ś
__module__Ś__qualname__Ś__doc__rO   r   r   r   r   r   r   r   r$   r&   r(   r0   r4   r7   r8   r<   r=   r,   r@   rD   rF   rG   rM   rR   rS   rU   rP   rY   r`   rc   r   r   r   r   r      s@    





					r   )rg   r-   Śior   Śparamikor   Śparamiko.commonr   r   r   Śparamiko.utilr   r   r   r   r   r   Ś<module>   s   