o
    {j6h                     @   sb   d dl Z d dlZd dlmZ d dlmZ G dd de jZdd Zdd	 Z	dddZ
dddZdS )    N)error)univc                   @   sd   e Zd ZdZdd Zdd Zdd Zdejfd	d
Z	dddZ
edd Zejdd Zdd ZdS )CachingStreamWrappera  Wrapper around non-seekable streams.

    Note that the implementation is tied to the decoder,
    not checking for dangerous arguments for the sake
    of performance.

    The read bytes are kept in an internal cache until
    setting _markedPosition which may reset the cache.
    c                 C   s   || _ t | _d| _d S Nr   )_rawioBytesIO_cache_markedPosition)selfraw r   e/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/pyasn1/codec/streaming.py__init__   s   

zCachingStreamWrapper.__init__c                 C   s$   |  |}| jt| tj |S N)readr	   seeklenosSEEK_CUR)r   nresultr   r   r   peek   s   
zCachingStreamWrapper.peekc                 C   s   dS )NTr   r   r   r   r   seekable!   s   zCachingStreamWrapper.seekablec                 C   s   | j ||S r   )r	   r   )r   r   whencer   r   r   r   $   s   zCachingStreamWrapper.seekc                 C   sH   | j |}|dkr|t|8 }|s|S | j|}| j | || S )Nr   )r	   r   r   r   write)r   r   read_from_cacheread_from_rawr   r   r   r   (   s   zCachingStreamWrapper.readc                 C   s   | j S )a  Position where the currently processed element starts.

        This is used for back-tracking in SingleItemDecoder.__call__
        and (indefLen)ValueDecoder and should not be used for other purposes.
        The client is not supposed to ever seek before this position.
        )r
   r   r   r   r   markedPosition5   s   z#CachingStreamWrapper.markedPositionc                 C   s6   || _ | j tjkrt| j | _d| _ d S d S r   )r
   r	   tellr   DEFAULT_BUFFER_SIZEr   r   )r   valuer   r   r   r    ?   s
   
c                 C   s
   | j  S r   )r	   r!   r   r   r   r   r!   M   s   
zCachingStreamWrapper.tellNr   )__name__
__module____qualname____doc__r   r   r   r   SEEK_SETr   r   propertyr    setterr!   r   r   r   r   r      s    	

	
r   c                 C   s|   t | tjr| S t | trt| S t | tjrt|  S z|  r'| W S t| W S  t	y=   t
d| jj d w )ai  Convert object to seekable byte-stream.

    Parameters
    ----------
    substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString`

    Returns
    -------
    : :py:class:`io.IOBase`

    Raises
    ------
    : :py:class:`~pyasn1.error.PyAsn1Error`
        If the supplied substrate cannot be converted to a seekable stream.
    zCannot convert z to a seekable bit stream.)
isinstancer   r   bytesr   OctetStringasOctetsr   r   AttributeErrorr   UnsupportedSubstrateError	__class__r%   )	substrater   r   r   asSeekableStreamQ   s"   



r4   c                 c   s~    t | tjr$|  }| dtj |  |k}| |tj |V  dS | d}|du r0dV  |r9| dtj	 | V  dS )a  Check whether we have reached the end of a stream.

    Although it is more effective to read and catch exceptions, this
    function

    Parameters
    ----------
    substrate: :py:class:`IOBase`
        Stream to check

    Returns
    -------
    : :py:class:`bool`
    r      Nr   )
r,   r   r   r!   r   r   SEEK_ENDr)   r   r   )r3   cpr   receivedr   r   r   isEndOfStreamv   s   

r9   r   c              	   c   s    t | dr&| |}|du rdV  t||k r!dV  t||k s|V  dS |  }zt| |D ]}|V  q0W | | dS | | w )a?  Peek into stream.

    Parameters
    ----------
    substrate: :py:class:`IOBase`
        Stream to read from.

    size: :py:class:`int`
        How many bytes to peek (-1 = all available)

    Returns
    -------
    : :py:class:`bytes` or :py:class:`str`
        The return type depends on Python major version
    r   N)hasattrr   r   r!   readFromStreamr   )r3   sizer8   current_positionchunkr   r   r   peekIntoStream   s   


r?   c                 c   s|    	 |  |}|du rtj|dV  n%|s|dkrtj|dt||k r7| t| tj tj|dV  nnq|V  dS )a  Read from the stream.

    Parameters
    ----------
    substrate: :py:class:`IOBase`
        Stream to read from.

    Keyword parameters
    ------------------
    size: :py:class:`int`
        How many bytes to read (-1 = all available)

    context: :py:class:`dict`
        Opaque caller context will be attached to exception objects created
        by this function.

    Yields
    ------
    : :py:class:`bytes` or :py:class:`str` or :py:class:`SubstrateUnderrunError`
        Read data or :py:class:`~pyasn1.error.SubstrateUnderrunError`
        object if no `size` bytes is readily available in the stream. The
        data type depends on Python major version

    Raises
    ------
    : :py:class:`~pyasn1.error.EndOfStreamError`
        Input stream is exhausted
    TN)contextr   )r   r   SubstrateUnderrunErrorEndOfStreamErrorr   r   r   r   )r3   r<   r@   r8   r   r   r   r;      s   

r;   r$   )r   N)r   r   pyasn1r   pyasn1.typer   IOBaser   r4   r9   r?   r;   r   r   r   r   <module>   s   D%
!$