
    mg                     L   d Z ddlmZmZmZmZmZmZmZ ddl	m
Z
 ddlmZ eedf   Zeeeedgdf   Z G d d	e      Zd
edefdZd
edefdZd
edefdZd
edefdZd
edefdZd
eeef   defdZd
edefdZd
ee   defdZ G d d      Z G d d      Z G d de      Zy)z*SSH packet encoding and decoding functions    )AnyCallableIterableMappingOptionalSequenceUnion   )	SSHLogger)plural	SSHPacketNc                       e Zd ZdZy)PacketDecodeErrorzPacket decoding errorN)__name__
__module____qualname____doc__     /lib/python3.12/site-packages/asyncssh/packet.pyr   r   !   s    r   r   valuereturnc                     t        | f      S )zEncode a single byte)bytesr   s    r   Byter   %   s     %?r   c                 *    t        t        |             S )zEncode a boolean value)r   boolr   s    r   Booleanr   +   s     Ur   c                 &    | j                  dd      S )zEncode a 16-bit integer value   bigto_bytesr   s    r   UInt16r%   1        >>!U##r   c                 &    | j                  dd      S )zEncode a 32-bit integer value   r"   r#   r   s    r   UInt32r)   7   r&   r   c                 &    | j                  dd      S )zEncode a 64-bit integer value   r"   r#   r   s    r   UInt64r,   =   r&   r   c                     t        | t              r| j                  dd      } t        |       j	                  dd      | z   S )z*Encode a byte string or UTF-8 string valuezutf-8strict)errorsr(   r"   )
isinstancestrencodelenr$   r   s    r   Stringr4   C   s<     %WX6u:q%(500r   c                     | j                         }||dz  dk(  xr | dk7  xr | d|dz
  z  k7  z  }|dz   dz  }|j                  dd      | j                  |dd	      z   S )
z)Encode a multiple precision integer valuer+   r   r
      r(   r"   Tsigned)
bit_lengthr$   )r   ls     r   MPIntr<   L   sq     	A!a%1*
>!
>A(>?A	
Q1A::a%..E$."GGGr   c                 6    t        dj                  |             S )z-Encode a comma-separated list of byte strings   ,)r4   joinr   s    r   NameListr@   V   s     $))E"##r   c                       e Zd ZdZdefdZdefdZddZdefdZ	defd	Z
defd
ZdedefdZdefdZdefdZdefdZdefdZdefdZdefdZdefdZdee   fdZy)r   zDecoder class for SSH packetspacketc                 @    || _         d| _        t        |      | _        y )Nr   )_packet_idxr3   _len)selfrB   s     r   __init__zSSHPacket.__init___   s    	K	r   r   c                 4    | j                   | j                  k7  S )N)rE   rF   rG   s    r   __bool__zSSHPacket.__bool__d   s    yyDII%%r   Nc                     | rt        d      y)z<Confirm that all of the data in the packet has been consumedz Unexpected data at end of packetN)r   rJ   s    r   	check_endzSSHPacket.check_endg   s     #$FGG r   c                 4    | j                   d| j                   S )z0Return the portion of the packet consumed so farNrD   rE   rJ   s    r   get_consumed_payloadzSSHPacket.get_consumed_payloadm   s     ||JTYY''r   c                 4    | j                   | j                  d S )z1Return the portion of the packet not yet consumedNrO   rJ   s    r   get_remaining_payloadzSSHPacket.get_remaining_payloadr   s     ||DIIJ''r   c                     | j                   S )zReturn the full packet)rD   rJ   s    r   get_full_payloadzSSHPacket.get_full_payloadw   s     ||r   sizec                     | j                   |z   | j                  kD  rt        d      | j                  | j                   | j                   |z    }| xj                   |z  c_         |S )z5Extract the requested number of bytes from the packetzIncomplete packet)rE   rF   r   rD   )rG   rU   r   s      r   	get_byteszSSHPacket.get_bytes|   sT     99tdii'#$788TYYtyy~6		T	r   c                 *    | j                  d      d   S )z%Extract a single byte from the packetr
   r   )rW   rJ   s    r   get_bytezSSHPacket.get_byte   s     ~~a ##r   c                 4    t        | j                               S )z!Extract a boolean from the packet)r   rY   rJ   s    r   get_booleanzSSHPacket.get_boolean   s     DMMO$$r   c                 L    t         j                  | j                  d      d      S )z(Extract a 16-bit integer from the packetr!   r"   int
from_bytesrW   rJ   s    r   
get_uint16zSSHPacket.get_uint16        ~~dnnQ/77r   c                 L    t         j                  | j                  d      d      S )z(Extract a 32-bit integer from the packetr(   r"   r]   rJ   s    r   
get_uint32zSSHPacket.get_uint32   ra   r   c                 L    t         j                  | j                  d      d      S )z(Extract a 64-bit integer from the packetr+   r"   r]   rJ   s    r   
get_uint64zSSHPacket.get_uint64   ra   r   c                 @    | j                  | j                               S )z&Extract a UTF-8 string from the packet)rW   rc   rJ   s    r   
get_stringzSSHPacket.get_string   s     ~~doo/00r   c                 N    t         j                  | j                         dd      S )z4Extract a multiple precision integer from the packetr"   Tr8   )r^   r_   rg   rJ   s    r   	get_mpintzSSHPacket.get_mpint   s      ~~doo/t~DDr   c                 L    | j                         }|r|j                  d      S g S )z>Extract a comma-separated list of byte strings from the packetr>   )rg   split)rG   namelists     r   get_namelistzSSHPacket.get_namelist   s&     ??$'/x~~d#7R7r   )r   N)r   r   r   r   r   rH   r   rK   rM   rP   rR   rT   r^   rW   rY   r[   r`   rc   re   rg   ri   r   rm   r   r   r   r   r   \   s    ' u  
&$ &H(e (
(u (
% 
c e $# $
%T %
8C 8
8C 8
8C 8
1E 1
E3 E
8huo 8r   c                       e Zd ZU dZi Zeeef   ed<   e	de
fd       Zdededee   ded	edd
fdZ	 ddedee   ded	edd
f
dZ	 ddedee   ded	edd
f
dZy
)SSHPacketLoggerz#Parent class for SSH packet loggers_handler_namesr   c                     t         )z$The logger to use for packet loggingNotImplementedErrorrJ   s    r   loggerzSSHPacketLogger.logger   
     "!r   msgpkttypepktidrB   noteNc           	         t        |t              r|j                         }	 d| j                  |   |fz  }t        t        |      d      }|rd|z  }| j                  j                  ||d||||       y# t        $ r d|z  }Y Nw xY w)zLog a sent/received packetz%s (%d)zpacket type %dbytez (%s)z%s %s, %s%sN)	r0   r   rT   rp   KeyErrorr   r3   rt   rB   )rG   rv   rw   rx   rB   ry   namecounts           r   _log_packetzSSHPacketLogger._log_packet   s     fi(,,.F	. 3 3G <gFFD s6{F+T>D5&-eT	3  	.#g-D	.s   A4 4BBc                 .    | j                  d||||       y)zLog a sent packetSentNr   rG   rw   rx   rB   ry   s        r   log_sent_packetzSSHPacketLogger.log_sent_packet   s     	%>r   c                 .    | j                  d||||       y)zLog a received packetReceivedNr   r   s        r   log_received_packetz#SSHPacketLogger.log_received_packet   s     	WeVTBr   ) )r   r   r   r   rp   r   r^   r1   __annotations__propertyr   rt   r   _LoggedPacketr   r   r   r   r   r   ro   ro      s    -(*NGCH%*"	 " "
3s 3S 3# 3)31439=3* <>?s ?8C= ? -?58?BF? @BC3 Cx} C$1C9<CFJCr   ro   c                   X    e Zd ZU dZi Zeeef   ed<   e	de
fd       ZdedededefdZy	)
SSHPacketHandlerz$Parent class for SSH packet handlers_packet_handlersr   c                     t         )z.The logger associated with this packet handlerrr   rJ   s    r   rt   zSSHPacketHandler.logger   ru   r   rw   rx   rB   c                 R    || j                   v r | j                   |   | |||       yy)z!Log and process a received packetTF)r   )rG   rw   rx   rB   s       r   process_packetzSSHPacketHandler.process_packet   s3     d+++*D!!'*4%Hr   N)r   r   r   r   r   r   r^   _PacketHandlerr   r   r   rt   r   r   r   r   r   r   r   r      sV    .57gc>127"	 " "
c # (-1r   r   ) r   typingr   r   r   r   r   r   r	   loggingr   miscr   r   r   r^   r   
ValueErrorr   r   r   r   r%   r)   r,   r1   r4   r<   r@   r   ro   r   r   r   r   <module>r      s  * 1 N N N   e[()3S+6<= 
    4 E $# $% $$# $% $$# $% $1%s
# 1 1H H H$HUO $ $Q8 Q8h*C *CZ r   