
    a@#                     b    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e      Zy)z&
Implementation of an SSH2 "message".
    N)util)	zero_bytemax_byteone_byteasbytes)longBytesIOuinteger_typesc                       e Zd ZdZ ed      Zd!dZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"y)"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                 H    |t        |      | _        yt               | _        y)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contents     0lib/python3.12/site-packages/paramiko/message.py__init__zMessage.__init__+   s     !'*DK!)DK    c                 "    | j                         S )zX
        Return the byte stream content of this message, as a string/bytes obj.
        )r   r   s    r   __str__zMessage.__str__8   s     ||~r   c                 T    dt        | j                  j                               z   dz   S )zP
        Returns a string representation of this object, for debugging.
        zparamiko.Message())reprr   getvaluer   s    r   __repr__zMessage.__repr__>   s&     #T$++*>*>*@%AACGGr   c                 6    | j                   j                         S )zK
        Return the byte stream content of this Message, as bytes.
        )r   r   r   s    r   r   zMessage.asbytesD   s     {{##%%r   c                 :    | j                   j                  d       y)zk
        Rewind the message to the beginning as if no items had been parsed
        out of it yet.
        r   N)r   seekr   s    r   rewindzMessage.rewindJ   s    
 	r   c                     | j                   j                         }| j                   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	remainders      r   get_remainderzMessage.get_remainderQ   s@    
 ;;##%KK$$&	"r   c                     | j                   j                         }| j                          | 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$   s     r   
get_so_farzMessage.get_so_far[   s4     ;;##%{{))r   c                     | j                   j                  |      }d}t        |      |cxk  r|k  rn |S |t        |t        |      z
  z  z   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bmax_pad_sizes       r   	get_byteszMessage.get_bytese   sR     KKQq6A$$ yAAJ///r   c                 $    | j                  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   s    r   get_bytezMessage.get_byter   s     ~~a  r   c                 6    | j                  d      }|t        k7  S )z2
        Fetch a boolean from the stream.
        r0   )r.   r   r   r,   s     r   get_booleanzMessage.get_boolean}   s     NN1I~r   c                     | j                  d      }|t        k(  r#t        j                  | j	                               S || j                  d      z  }t        j                  d|      d   S )zZ
        Fetch an int from the stream.

        :return: a 32-bit unsigned `int`.
        r0      >Ir   )r.   r   r   inflate_long
get_binarystructunpack)r   bytes     r   get_adaptive_intzMessage.get_adaptive_int   s[     ~~a 8$$T__%677q!!}}T4(++r   c                 R    t        j                  d| j                  d            d   S )z/
        Fetch an int from the stream.
        r7      r   r:   r;   r.   r   s    r   get_intzMessage.get_int   s#     }}T4>>!#45a88r   c                 R    t        j                  d| j                  d            d   S )zk
        Fetch a 64-bit int from the stream.

        :return: a 64-bit unsigned integer (`long`).
        >Q   r   r@   r   s    r   	get_int64zMessage.get_int64   s#     }}T4>>!#45a88r   c                 H    t        j                  | j                               S )zs
        Fetch a long int (mpint) from the stream.

        :return: an arbitrary-length integer (`long`).
        )r   r8   r9   r   s    r   	get_mpintzMessage.get_mpint   s       !233r   c                 @    | j                  | j                               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.   rA   r   s    r   
get_stringzMessage.get_string        ~~dlln--r   c                 4    t        | j                               S )z9
        Fetch a Unicode string from the stream.
        )r
   rJ   r   s    r   get_textzMessage.get_text   s     "##r   c                 @    | j                  | j                               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.)
        rI   r   s    r   r9   zMessage.get_binary   rK   r   c                 @    | j                         j                  d      S )z
        Fetch a list of `strings <str>` from the stream.

        These are trivially encoded as comma-separated values in a string.
        ,)rM   splitr   s    r   get_listzMessage.get_list   s     }}$$S))r   c                 <    | j                   j                  |       | S )zh
        Write bytes to the stream, without any formatting.

        :param str b: bytes to add
        r   writer3   s     r   	add_byteszMessage.add_bytes        	!r   c                 <    | j                   j                  |       | S )zo
        Write a single byte to the stream, without any formatting.

        :param str b: byte to add
        rT   r3   s     r   add_bytezMessage.add_byte   rW   r   c                     |r!| j                   j                  t               | S | j                   j                  t               | S )za
        Add a boolean value to the stream.

        :param bool b: boolean value to add
        )r   rU   r   r   r3   s     r   add_booleanzMessage.add_boolean   s9     KKh'  KKi(r   c                 d    | j                   j                  t        j                  d|             | S zU
        Add an integer to the stream.

        :param int n: integer to add
        r7   r   rU   r:   packr   r+   s     r   add_intzMessage.add_int   &     	&++dA./r   c                    |t         j                  k\  rE| j                  j                  t               | j                  t        j                  |             | S | j                  j                  t        j                  d|             | S r]   )
r   big_intr   rU   r   
add_stringr   deflate_longr:   r_   r`   s     r   add_adaptive_intzMessage.add_adaptive_int   sd     KKh'OOD--a01  KKfkk$23r   c                 d    | j                   j                  t        j                  d|             | S )zY
        Add a 64-bit int to the stream.

        :param long n: long int to add
        rC   r^   r`   s     r   	add_int64zMessage.add_int64   rb   r   c                 N    | j                  t        j                  |             | 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
        )re   r   rf   )r   zs     r   	add_mpintzMessage.add_mpint  s      	))!,-r   c                     t        |      }| j                  t        |             | j                  j	                  |       | S )zR
        Add a string to the stream.

        :param str s: string to add
        )r   ra   r*   r   rU   )r   ss     r   re   zMessage.add_string  s4     AJSV!r   c                 F    | j                  dj                  |             | 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
        rP   )re   join)r   ls     r   add_listzMessage.add_list  s     	$r   c                     t        |      t        u r| j                  |      S t        |t              r| j                  |      S t        |      t        u r| j                  |      S | j                  |      S N)	typeboolr[   
isinstancer   rg   listrr   re   )r   is     r   _addzMessage._add!  sb    7d?##A&&=)((++!W_==##??1%%r   c                 4    |D ]  }| j                  |        y)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)rz   )r   seqitems      r   addzMessage.add+  s      	DIIdO	r   rt   )#__name__
__module____qualname____doc__r   rd   r   r   r   r   r    r&   r(   r.   r1   r4   r=   rA   rE   rG   rJ   rM   r9   rR   rV   rY   r[   ra   rg   ri   rl   re   rr   rz   r~    r   r   r   r      s     :G$H&*	!
,994.$.*
		&r   r   )r   r:   paramikor   paramiko.commonr   r   r   r   paramiko.py3compatr   r	   r
   r   objectr   r   r   r   <module>r      s,   &   B B > >Xf Xr   