3 L]2#@s\dZddlZddlmZddlmZmZmZmZddl m Z m Z m Z m Z GdddeZdS)z& Implementation of an SSH2 "message". N)util) zero_bytemax_byteone_byteasbytes)longBytesIOu integer_typesc@seZdZdZedZd>ddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Z d:d;Z!dszMessage.__repr__cCs |jjS)zK Return the byte stream content of this Message, as bytes. )r r)r rrrrDszMessage.asbytescCs|jjddS)zk Rewind the message to the beginning as if no items had been parsed out of it yet. rN)r seek)r rrrrewindJszMessage.rewindcCs$|jj}|jj}|jj||S)zv Return the bytes (as a `str`) of this message that haven't already been parsed and returned. )r tellreadr)r position remainderrrr get_remainderQs   zMessage.get_remaindercCs|jj}|j|jj|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 rrr)r rrrr get_so_far[s zMessage.get_so_farcCsD|jj|}d}t||ko&|knr@|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 rlenr)r nbZ max_pad_sizerrr get_byteses  zMessage.get_bytescCs |jdS)a  Return the next byte of the message, without decomposing it. This is equivalent to `get_bytes(1) `. :return: the next (`str`) byte of the message, or ``''`` if there aren't any bytes remaining. r)r$)r rrrget_byters zMessage.get_bytecCs|jd}|tkS)z2 Fetch a boolean from the stream. r)r$r)r r#rrr get_boolean}s zMessage.get_booleancCs>|jd}|tkr tj|jS||jd7}tjd|dS)zZ Fetch an int from the stream. :return: a 32-bit unsigned `int`. rz>Ir)r$rr inflate_long get_binarystructunpack)r byterrrget_adaptive_ints  zMessage.get_adaptive_intcCstjd|jddS)z/ Fetch an int from the stream. z>Ir)r*r+r$)r rrrget_intszMessage.get_intcCstjd|jddS)zk Fetch a 64-bit int from the stream. :return: a 64-bit unsigned integer (`long`). z>Qr)r*r+r$)r rrr get_int64szMessage.get_int64cCstj|jS)zs Fetch a long int (mpint) from the stream. :return: an arbitrary-length integer (`long`). )rr(r))r rrr get_mpintszMessage.get_mpintcCs|j|jS)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$r/)r rrr get_stringszMessage.get_stringcCs t|jS)z9 Fetch a Unicode string from the stream. )r r3)r rrrget_textszMessage.get_textcCs|j|jS)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.) )r$r/)r rrrr)szMessage.get_binarycCs|jjdS)z Fetch a list of `strings ` from the stream. These are trivially encoded as comma-separated values in a string. ,)r4split)r rrrget_listszMessage.get_listcCs|jj||S)zh Write bytes to the stream, without any formatting. :param str b: bytes to add )r write)r r#rrr add_bytess zMessage.add_bytescCs|jj||S)zo Write a single byte to the stream, without any formatting. :param str b: byte to add )r r8)r r#rrradd_bytes zMessage.add_bytecCs"|r|jjtn |jjt|S)za Add a boolean value to the stream. :param bool b: boolean value to add )r r8rr)r r#rrr add_booleans zMessage.add_booleancCs|jjtjd||S)zU Add an integer to the stream. :param int n: integer to add z>I)r r8r*pack)r r"rrradd_intszMessage.add_intcCs@|tjkr(|jjt|jtj|n|jjtj d||S)zU Add an integer to the stream. :param int n: integer to add z>I) r big_intr r8r add_stringr deflate_longr*r<)r r"rrradd_adaptive_ints   zMessage.add_adaptive_intcCs|jjtjd||S)zY Add a 64-bit int to the stream. :param long n: long int to add z>Q)r r8r*r<)r r"rrr add_int64szMessage.add_int64cCs|jtj||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 )r?rr@)r zrrr add_mpintszMessage.add_mpintcCs&t|}|jt||jj||S)zR Add a string to the stream. :param str s: string to add )rr=r!r r8)r srrrr? s zMessage.add_stringcCs|jdj||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 r5)r?join)r lrrradd_listszMessage.add_listcCsNt|tkr|j|St|tr*|j|St|tkr@|j|S|j|SdS)N) typeboolr; isinstancer rAlistrHr?)r irrr_add!s      z Message._addcGsx|D]}|j|qWdS)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)rN)r seqitemrrradd+s z Message.add)N)#__name__ __module__ __qualname____doc__rr>rrrrrrrr$r%r&r-r/r1r2r3r4r)r7r9r:r;r=rArBrDr?rHrNrQrrrrr s>                 r )rUr*ZparamikorZparamiko.commonrrrrZparamiko.py3compatrrr r objectr rrrrs