U q`@#@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 |jS)zK Return the byte stream content of this Message, as bytes. )r rrrrrrDszMessage.asbytescCs|jddS)zk Rewind the message to the beginning as if no items had been parsed out of it yet. rN)r seekrrrrrewindJszMessage.rewindcCs$|j}|j}|j||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|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 rrr)r rrrr get_so_far[s zMessage.get_so_farcCsD|j|}d}t||kr(|kr@nn|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 |dS)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#rrrrget_byters zMessage.get_bytecCs|d}|tkS)z2 Fetch a boolean from the stream. r$)r#rr r"rrr get_boolean}s zMessage.get_booleancCs>|d}|tkr t|S||d7}td|dS)zZ Fetch an int from the stream. :return: a 32-bit unsigned `int`. r$>Ir)r#rr inflate_long get_binarystructunpack)r byterrrget_adaptive_ints  zMessage.get_adaptive_intcCstd|ddS)z/ Fetch an int from the stream. r)rr,r-r#rrrrget_intszMessage.get_intcCstd|ddS)zk Fetch a 64-bit int from the stream. :return: a 64-bit unsigned integer (`long`). >Qrr1rrrr get_int64szMessage.get_int64cCst|S)zs Fetch a long int (mpint) from the stream. :return: an arbitrary-length integer (`long`). )rr*r+rrrr get_mpintszMessage.get_mpintcCs||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#r2rrrr get_stringszMessage.get_stringcCs t|S)z9 Fetch a Unicode string from the stream. )r r8rrrrget_textszMessage.get_textcCs||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.) r7rrrrr+szMessage.get_binarycCs|dS)z Fetch a list of `strings ` from the stream. These are trivially encoded as comma-separated values in a string. ,)r9splitrrrrget_listszMessage.get_listcCs|j||S)zh Write bytes to the stream, without any formatting. :param str b: bytes to add r writer&rrr add_bytess zMessage.add_bytescCs|j||S)zo Write a single byte to the stream, without any formatting. :param str b: byte to add r=r&rrradd_bytes zMessage.add_bytecCs"|r|jtn |jt|S)za Add a boolean value to the stream. :param bool b: boolean value to add )r r>rrr&rrr add_booleans zMessage.add_booleancCs|jtd||SzU Add an integer to the stream. :param int n: integer to add r)r r>r,packr r!rrradd_intszMessage.add_intcCs@|tjkr(|jt|t|n|jt d||SrB) r big_intr r>r add_stringr deflate_longr,rDrErrradd_adaptive_ints   zMessage.add_adaptive_intcCs|jtd||S)zY Add a 64-bit int to the stream. :param long n: long int to add r3rCrErrr add_int64szMessage.add_int64cCs|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 )rHrrI)r zrrr add_mpintszMessage.add_mpintcCs&t|}|t||j||S)zR Add a string to the stream. :param str s: string to add )rrFr r r>)r srrrrH s zMessage.add_stringcCs|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:)rHjoin)r lrrradd_listszMessage.add_listcCsNt|tkr||St|tr*||St|tkr@||S||SdS)N) typeboolrA isinstancer rJlistrQrH)r irrr_add!s      z Message._addcGs|D]}||qdS)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 seqitemrrradd+s z Message.add)N)#__name__ __module__ __qualname____doc__rrGrrrrrrrr#r%r'r/r2r5r6r8r9r+r<r?r@rArFrJrKrMrHrQrWrZrrrrr s>                 r )r^r,ZparamikorZparamiko.commonrrrrZparamiko.py3compatrrr r objectr rrrrs