o
    0 iw                  
   @  sj  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ dd	lmZmZmZmZmZmZmZ erd d
l m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dlm+Z+ d dl,m-Z- edZ.e/dZ0edZ1G dd dZ2e2j3e2j4e2j5e2j6e2j7e2j8e2j9e2j:dZ;de<d< dd dd dd dd e2j=e2j>e2j?e2j@dd d	ZAde<d < i d e2jBde2jCd!e2jDd"e2jEd#e2jFd$e2jGd%e2jHd&e2jId'e2jJd(e2jKd)e2jLd*e2jMd+e2jNd,e2jOd-e2jPd.e2jQd/e2jRe2jSe2jTe2jUe2jVd0ZWde<d1< 			2dCdDd=d>ZX			2dCdEdAdBZYdS )F    )annotationsN)getincrementaldecoder)CallableMappingSequence)datedatetime	timedeltatimezone)BytesIO)IOTYPE_CHECKINGAnyTypeVarcastoverload   )CBORDecodeEOFCBORDecodeValueErrorCBORSimpleValueCBORTag
FrozenDictbreak_marker	undefinedDecimal)MessageFraction)IPv4AddressIPv4NetworkIPv6AddressIPv6Network)LiteralUUIDTzX^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(?:\.(\d{1,6})\d*)?(?:Z|([+-])(\d\d):(\d\d))$utf-8c                   @  sn  e Zd ZU dZdZded< ded< 			ddddZedddZedddZ	e	j
dddZ	edddZej
dddZedddZej
dd!dZedd"d#Zej
dd$d#Zdd&d'Zdd,d-Zdd0d1Zddd6d7Zdd9d:Zdd<d=Zedd?d@ZeddDd@ZdddEd@ZddFdGZddHdIZddJdKZddMdNZddPdQZddSdTZddUdVZddWdXZddZd[Zdd\d]Zdd_d`Z ddadbZ!ddcddZ"ddedfZ#ddhdiZ$ddjdkZ%ddldmZ&ddndoZ'ddpdqZ(ddsdtZ)ddvdwZ*ddydzZ+dd|d}Z,dddZ-dddZ.dddZ/dddZ0dddZ1dddZ2dddZ3dddZ4dddZ5dddZ6dS )CBORDecodera  
    The CBORDecoder class implements a fully featured `CBOR`_ decoder with
    several extensions for handling shared references, big integers, rational
    numbers and so on. Typically the class is not used directly, but the
    :func:`load` and :func:`loads` functions are called to indirectly construct
    and use the class.

    When the class is constructed manually, the main entry points are
    :meth:`decode` and :meth:`decode_from_bytes`.

    .. _CBOR: https://cbor.io/
    )		_tag_hook_object_hook_share_index_shareables_fp_fp_read
_immutable_str_errors_stringref_namespace	IO[bytes]r-   zCallable[[int], bytes]r.   Nstrictfptag_hook,Callable[[CBORDecoder, CBORTag], Any] | Noneobject_hook3Callable[[CBORDecoder, dict[Any, Any]], Any] | None
str_errors%Literal['strict', 'error', 'replace']c                 C  s4   || _ || _|| _|| _d| _g | _d| _d| _dS )a  
        :param fp:
            the file to read from (any file-like object opened for reading in binary
            mode)
        :param tag_hook:
            callable that takes 2 arguments: the decoder instance, and the
            :class:`.CBORTag` to be decoded. This callback is invoked for any tags
            for which there is no built-in decoder. The return value is substituted
            for the :class:`.CBORTag` object in the deserialized output
        :param object_hook:
            callable that takes 2 arguments: the decoder instance, and a
            dictionary. This callback is invoked for each deserialized
            :class:`dict` object. The return value is substituted for the dict in
            the deserialized output.
        :param str_errors:
            determines how to handle unicode decoding errors (see the `Error Handlers`_
            section in the standard library documentation for details)

        .. _Error Handlers: https://docs.python.org/3/library/codecs.html#error-handlers

        NF)r4   r5   r7   r9   r+   r,   r1   r/   )selfr4   r5   r7   r9    r<   Z/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/cbor2/_decoder.py__init__C   s   
zCBORDecoder.__init__returnboolc                 C     | j S )z
        Used by decoders to check if the calling context requires an immutable
        type.  Object_hook or tag_hook should raise an exception if this flag
        is set unless the result can be safely used as a dict key.
        )r/   r;   r<   r<   r=   	immutableh   s   zCBORDecoder.immutablec                 C  rA   N)r-   rB   r<   r<   r=   r4   q      zCBORDecoder.fpvalueNonec                 C  s@   zt |js
tdW n ty   tdw || _|j| _d S )Nzfp.read is not callablezfp object has no read method)callableread
ValueErrorAttributeErrorr-   r.   r;   rF   r<   r<   r=   r4   u   s   
c                 C  rA   rD   )r)   rB   r<   r<   r=   r5      rE   zCBORDecoder.tag_hookc                 C  "   |d u st |r|| _d S td)Nz#tag_hook must be None or a callable)rH   r)   rJ   rL   r<   r<   r=   r5         
c                 C  rA   rD   )r*   rB   r<   r<   r=   r7      rE   zCBORDecoder.object_hook6Callable[[CBORDecoder, Mapping[Any, Any]], Any] | Nonec                 C  rM   )Nz&object_hook must be None or a callable)rH   r*   rJ   rL   r<   r<   r=   r7      rN   c                 C  rA   rD   )r0   rB   r<   r<   r=   r9      rE   zCBORDecoder.str_errorsc                 C  s"   |dv r	|| _ d S td|d)N)r3   errorreplacezinvalid str_errors value z1 (must be one of 'strict', 'error', or 'replace'))r0   rJ   rL   r<   r<   r=   r9      s
   

r&   c                 C  s   | j dur|| j| j < |S )z
        Set the shareable value for the last encountered shared value marker,
        if any. If the current shared index is ``None``, nothing is done.

        :param value: the shared value
        :returns: the shared value to permit chaining
        N)r+   r,   rL   r<   r<   r=   set_shareable   s   
zCBORDecoder.set_shareablestringstr | byteslengthintc                 C  s   | j d ur<t| j }|dk r|dk}n|dk r|dk}n|dk r%|dk}n|dk r.|dk}n|d	k}|r>| j | d S d S d S )
N                  l              )r1   lenappend)r;   rS   rU   Z
next_indexZis_referencedr<   r<   r=   _stringref_namespace_add   s   





z$CBORDecoder._stringref_namespace_addamountbytesc                 C  s4   |  |}t||k rtd| dt| d|S )zj
        Read bytes from the data stream.

        :param int amount: the number of bytes to read
        z*premature end of stream (expected to read z bytes, got z	 instead))r.   r_   r   )r;   rb   datar<   r<   r=   rI      s   
zCBORDecoder.readFrC   unsharedr   c           	      C  s   |r| j }d| _ |r| j}d | _z$| dd }|d? }|d@ }t| }|| |W |r.|| _ |r4|| _S S |r:|| _ |r?|| _w )NTr   r   r\      )r/   r+   rI   major_decoders)	r;   rC   re   Zold_immutable	old_indexinitial_byteZ
major_typesubtypedecoderr<   r<   r=   _decode   s*   
zCBORDecoder._decodeobjectc                 C     |   S )z
        Decode the next value from the stream.

        :raises CBORDecodeError: if there is any problem decoding the stream
        rl   rB   r<   r<   r=   decode   s   zCBORDecoder.decodebufc                 C  sJ   t |}| j}|| _|  }|| _|W  d   S 1 sw   Y  dS )aA  
        Wrap the given bytestring as a file and call :meth:`decode` with it as
        the argument.

        This method was intended to be used from the ``tag_hook`` hook when an
        object needs to be decoded separately from the rest but while still
        taking advantage of the shared value registry.
        N)r   r4   rl   )r;   rq   r4   Zold_fpretvalr<   r<   r=   decode_from_bytes   s   
	$zCBORDecoder.decode_from_bytesrj   c                 C     d S rD   r<   r;   rj   r<   r<   r=   _decode_length      zCBORDecoder._decode_lengthallow_indefiniteLiteral[True]
int | Nonec                 C  rt   rD   r<   r;   rj   rx   r<   r<   r=   rv      rw   c                 C  s   |dk r|S |dkr|  dd S |dkr#tttd|  dd S |dkr5tttd|  d	d S |d
krGtttd|  dd S |dkrO|rOd S td|d)NrW   r   r      z>H      z>LrZ      z>Q   rf   z#unknown unsigned integer subtype 0xx)rI   r   rV   structunpackr   r{   r<   r<   r=   rv     s   c                 C  s   |  | |S rD   rR   rv   ru   r<   r<   r=   decode_uint  s   zCBORDecoder.decode_uintc                 C  s   |  | | d S Nr   r   ru   r<   r<   r=   decode_negint  s   zCBORDecoder.decode_negintc           
      C  s  | j |dd}|d u rNg }	 | dd }|dkrd|}n.|d? dkrH|  |d	@ }|d u s5|tjkr=td
|d| |}|| ntdqn;|tjkr[td|d|dkre| |}n|}t }|rt|d}	|	| |	 ||	8 }|slt
|}| || | |S )NTrx   r   r          r\   r}   rf   z1invalid length for indefinite bytestring chunk 0xr   z4non-bytestring found in indefinite length bytestringz invalid length for bytestring 0xr[   )rv   rI   joinsysmaxsizer   r`   	bytearrayminextendrc   ra   rR   )
r;   rj   rU   rq   ri   resultrF   leftbuffer
chunk_sizer<   r<   r=   decode_bytestring  sF   





zCBORDecoder.decode_bytestringstrc              
   C  s  | j |dd}|d u rgg }	 | dd }|dkrd|}nG|d? dkra|  |d	@ }|d u s5|tjkr=td
|dz| |d| j}W n tyZ } ztd|d }~ww |	| ntdqnn|tjkrttd|d|dkrz| |d| j}W nJ ty } ztd|d }~ww t
| j}|}	d}|	rt|	d}
|	|
k}z||| |
|7 }W n ty } ztd|d }~ww |	|
8 }	|	s| || | |S )NTr   r   r   r    r\   rX   rf   z-invalid length for indefinite string chunk 0xr   r'   zerror decoding unicode stringz,non-string found in indefinite length stringzinvalid length for string 0xr[   )rv   rI   r   r   r   r   rp   r0   UnicodeDecodeErrorr`   incremental_utf8_decoderr   ra   rR   )r;   rj   rU   rq   ri   r   rF   exccodecr   r   finalr<   r<   r=   decode_stringE  sb   









zCBORDecoder.decode_stringSequence[Any]c                 C  s   | j |dd}|d u r&g }| js| | 	 |  }|tu rn|| qn%|tjkr3td|dg }| js=| | t	|D ]	}||   qA| jrYt
|}| | |S |S )NTr   zinvalid length for array 0xr   )rv   r/   rR   rl   r   r`   r   r   r   rangetuple)r;   rj   rU   itemsrF   indexZitems_tupler<   r<   r=   decode_array  s0   




zCBORDecoder.decode_arrayMapping[Any, Any]c                 C  s   | j |dd}|d u r)i }| | 	 | jddd}|tu rn	| jdd||< qni }| | t|D ]}| jddd}| jdd||< q4| jrV| | |}| | |S | jrdt|}| | |S |S )NTr   rC   re   re   )rv   rR   rl   r   r   r*   r/   r   )r;   rj   rU   
dictionarykey_Zfrozen_dictr<   r<   r=   
decode_map  s2   



zCBORDecoder.decode_mapc                 C  s^   |  |}t| }r|| S t|d }| | | jdd|_| jr*| | |}| |S )NTr   )rv   semantic_decodersgetr   rR   rl   rF   r)   )r;   rj   ZtagnumZsemantic_decodertagr<   r<   r=   decode_semantic  s   



zCBORDecoder.decode_semanticc              
   C  sJ   |dk rt |S zt| | W S  ty$ } z	td|d|d }~ww )N   z*Undefined Reserved major type 7 subtype 0xr   )r   special_decodersKeyErrorr   )r;   rj   er<   r<   r=   decode_special  s   
zCBORDecoder.decode_specialr   c                 C  s   |   }| t|d S )Ni;
 )rl   rR   r   fromordinalrL   r<   r<   r=   decode_epoch_date  s   zCBORDecoder.decode_epoch_datec                 C  s   |   }| t|S rD   )rl   rR   r   fromisoformatrL   r<   r<   r=   decode_date_string  s   zCBORDecoder.decode_date_stringr   c                 C  s   |   }t|}|rc| \
}}}}}}}	}
}}|	d u r d}nt|	d}|rF|
dkr/d}nd}t|| }t|| }tt||d}ntj}| t	t|t|t|t|t|t|||S t
d|)Nr   z<06-r   )hoursminuteszinvalid datetime string: )rl   timestamp_rematchgroupsrV   r
   r	   utcrR   r   r   )r;   rF   r   yearmonthdayhourminutesecondZsecfracZoffset_signZoffset_hZoffset_mmicrosecondsignr   r   tzr<   r<   r=   decode_datetime_string  sN   
z"CBORDecoder.decode_datetime_stringc              
   C  sN   |   }z	t|tj}W n tttfy! } ztd|d }~ww | 	|S )Nz"error decoding datetime from epoch)
rl   r   fromtimestampr
   r   OverflowErrorOSErrorrJ   r   rR   )r;   rF   tmpr   r<   r<   r=   decode_epoch_datetime  s   

z!CBORDecoder.decode_epoch_datetimec                 C  sB   ddl m} |  }t|tstdt| | t||dS )Nr   )hexlifyzinvalid bignum value    )	binasciir   rl   
isinstancerc   r   r   rR   rV   )r;   r   rF   r<   r<   r=   decode_positive_bignum*  s
   
z"CBORDecoder.decode_positive_bignumc                 C  s   |  |   d S r   )rR   r   rB   r<   r<   r=   decode_negative_bignum4  s   z"CBORDecoder.decode_negative_bignumr   c              
   C  sh   ddl m} z|  \}}W n ttfy! } ztd|d }~ww || }| ||j|j	|fS )Nr   r   zIncorrect tag 4 payload)
decimalr   rl   	TypeErrorrJ   r   as_tuplerR   r   digits)r;   r   expsigr   r   r<   r<   r=   decode_fraction8  s   
zCBORDecoder.decode_fractionc              
   C  s^   ddl m} z|  \}}W n ttfy! } ztd|d }~ww | ||d||  S )Nr   r   zIncorrect tag 5 payloadr}   )r   r   rl   r   rJ   r   rR   )r;   r   r   r   r   r<   r<   r=   decode_bigfloatC  s   
zCBORDecoder.decode_bigfloatc                 C  sF   | j d u r	td|  }z| j | }W |S  ty"   td| w )Nz%string reference outside of namespacezstring reference %d not found)r1   r   rl   
IndexError)r;   r   rF   r<   r<   r=   decode_stringrefN  s   
zCBORDecoder.decode_stringrefc                 C  s8   | j }t| j| _ | jd  z|  W || _ S || _ w rD   )r+   r_   r,   r`   rl   )r;   rh   r<   r<   r=   decode_shareable[  s   zCBORDecoder.decode_shareablec                 C  sN   | j dd}z| j| }W n ty   td| w |d u r%td| |S )NTr   zshared reference %d not foundz(shared value %d has not been initialized)rl   r,   r   r   )r;   rF   Zsharedr<   r<   r=   decode_sharedrefe  s   zCBORDecoder.decode_sharedrefcomplexc              
   C  s\   | j ddd}zt| }W n ty( } zt|tstdd td|d }~ww | |S )NTr   z3error decoding complex: input value was not a tuplezerror decoding complex)rl   r   r   r   r   r   rR   )r;   inputvalrF   r   r<   r<   r=   decode_complexr  s   


zCBORDecoder.decode_complexr   c              
   C  sl   ddl m} | jddd}z|| }W n ttfy0 } zt|ts'tdd td|d }~ww | |S )Nr   r   Tr   z4error decoding rational: input value was not a tuplezerror decoding rational)		fractionsr   rl   r   ZeroDivisionErrorr   r   r   rR   )r;   r   r   rF   r   r<   r<   r=   decode_rational  s   


zCBORDecoder.decode_rationalre.Pattern[str]c              
   C  sB   z	t |  }W n t jy } ztd|d }~ww | |S )Nz!error decoding regular expression)recompilerl   rP   r   rR   )r;   rF   r   r<   r<   r=   decode_regexp  s   

zCBORDecoder.decode_regexpr   c              
   C  sN   ddl m} z
| |  }W n ty! } ztd|d }~ww | |S )Nr   )Parserzerror decoding MIME message)email.parserr   parsestrrl   r   r   rR   )r;   r   rF   r   r<   r<   r=   decode_mime  s   

zCBORDecoder.decode_mimer%   c              
   C  sP   ddl m} z	||  d}W n ttfy" } ztd|d }~ww | |S )Nr   r$   )rc   zerror decoding UUID value)uuidr%   rl   r   rJ   r   rR   )r;   r%   rF   r   r<   r<   r=   decode_uuid  s   

zCBORDecoder.decode_uuidc                 C  s   | j }g | _ |  }|| _ |S rD   )r1   rl   )r;   Zold_namespacerF   r<   r<   r=   decode_stringref_namespace  s
   z&CBORDecoder.decode_stringref_namespaceset[Any] | frozenset[Any]c                 C  s2   | j r| t| jddS | t| jddS )NT)rC   )r/   rR   	frozensetrl   setrB   r<   r<   r=   
decode_set  s   zCBORDecoder.decode_set#IPv4Address | IPv6Address | CBORTagc                 C  s|   ddl m} |  }t|trt|dvrtd|t|dv r)| ||S t|dkr7| td|S td|)Nr   )
ip_address)rZ      r   zinvalid ipaddress value )rZ   r   r     )		ipaddressr   rp   r   rc   r_   r   rR   r   )r;   r   rq   r<   r<   r=   decode_ipaddress  s   zCBORDecoder.decode_ipaddressIPv4Network | IPv6Networkc              
   C  sv   ddl m} |  }t|tr4t|dkr4| D ]}z| ||ddW   S  tt	fy3   Y  nw t
d|)Nr   )
ip_networkr   F)r3   zinvalid ipnetwork value )r   r   rp   r   r   r_   r   rR   r   rJ   r   )r;   r   Znet_mapnetr<   r<   r=   decode_ipnetwork  s   zCBORDecoder.decode_ipnetworkc                 C  rn   rD   ro   rB   r<   r<   r=   decode_self_describe_cbor  s   z%CBORDecoder.decode_self_describe_cborr   c                 C  s   t | dd S )Nr   r   )r   rI   rB   r<   r<   r=   decode_simple_value  s   zCBORDecoder.decode_simple_valuefloatc              
   C  "   |  tttd| dd S )Nz>er}   r   rR   r   r  r   r   rI   rB   r<   r<   r=   decode_float16     "zCBORDecoder.decode_float16c              
   C  r  )Nz>frZ   r   r  rB   r<   r<   r=   decode_float32  r  zCBORDecoder.decode_float32c              
   C  r  )Nz>dr   r   r  rB   r<   r<   r=   decode_float64  r  zCBORDecoder.decode_float64NNr3   )r4   r2   r5   r6   r7   r8   r9   r:   )r?   r@   )r?   r2   )rF   r2   r?   rG   )r?   r6   )rF   r6   r?   rG   )r?   r8   )rF   rO   r?   rG   )r?   r:   )rF   r:   r?   rG   )rF   r&   r?   r&   )rS   rT   rU   rV   r?   rG   )rb   rV   r?   rc   )FF)rC   r@   re   r@   r?   r   )r?   rm   )rq   rc   r?   rm   )rj   rV   r?   rV   )rj   rV   rx   ry   r?   rz   )F)rj   rV   rx   r@   r?   rz   )rj   rV   r?   rc   )rj   rV   r?   r   )rj   rV   r?   r   )rj   rV   r?   r   )rj   rV   r?   r   )r?   r   )r?   r   )r?   rV   )r?   r   )r?   rT   )r?   r   )r?   r   )r?   r   )r?   r   )r?   r   )r?   r%   )r?   r   )r?   r   )r?   r   )r?   r   )r?   r  )7__name__
__module____qualname____doc__	__slots____annotations__r>   propertyrC   r4   setterr5   r7   r9   rR   ra   rI   rl   rp   rs   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r<   r<   r<   r=   r(   &   s   
 %

	







+
C
 





0












	








r(   )r   r   r}   rX   rZ   r\   r   r]   z,dict[int, Callable[[CBORDecoder, int], Any]]rg   c                 C     dS )NFr<   rB   r<   r<   r=   <lambda>      r  c                 C  r  )NTr<   rB   r<   r<   r=   r     r  c                 C  rt   rD   r<   rB   r<   r<   r=   r    r  c                 C     t S rD   )r   rB   r<   r<   r=   r    r  c                 C  r  rD   )r   rB   r<   r<   r=   r    r  )	r            rW   r|   r~   r   rf   z'dict[int, Callable[[CBORDecoder], Any]]r   r}   rX   rZ   r\   r|            #   $   %   d   rY   i  r   )i  i  i  i  r   r3   sbytes | bytearray | memoryviewr5   r6   r7   r8   r9   r:   r?   r   c                 C  s@   t | }t||||d W  d   S 1 sw   Y  dS )a  
    Deserialize an object from a bytestring.

    :param bytes s:
        the bytestring to deserialize
    :param tag_hook:
        callable that takes 2 arguments: the decoder instance, and the :class:`.CBORTag`
        to be decoded. This callback is invoked for any tags for which there is no
        built-in decoder. The return value is substituted for the :class:`.CBORTag`
        object in the deserialized output
    :param object_hook:
        callable that takes 2 arguments: the decoder instance, and a dictionary. This
        callback is invoked for each deserialized :class:`dict` object. The return value
        is substituted for the dict in the deserialized output.
    :param str_errors:
        determines how to handle unicode decoding errors (see the `Error Handlers`_
        section in the standard library documentation for details)
    :return:
        the deserialized object

    .. _Error Handlers: https://docs.python.org/3/library/codecs.html#error-handlers

    r5   r7   r9   N)r   r(   rp   )r   r5   r7   r9   r4   r<   r<   r=   loads#  s   
$r#  r4   r2   c                 C  s   t | |||d S )a  
    Deserialize an object from an open file.

    :param fp:
        the file to read from (any file-like object opened for reading in binary mode)
    :param tag_hook:
        callable that takes 2 arguments: the decoder instance, and the :class:`.CBORTag`
        to be decoded. This callback is invoked for any tags for which there is no
        built-in decoder. The return value is substituted for the :class:`.CBORTag`
        object in the deserialized output
    :param object_hook:
        callable that takes 2 arguments: the decoder instance, and a dictionary. This
        callback is invoked for each deserialized :class:`dict` object. The return value
        is substituted for the dict in the deserialized output.
    :param str_errors:
        determines how to handle unicode decoding errors (see the `Error Handlers`_
        section in the standard library documentation for details)
    :return:
        the deserialized object

    .. _Error Handlers: https://docs.python.org/3/library/codecs.html#error-handlers

    r"  )r(   rp   )r4   r5   r7   r9   r<   r<   r=   loadF  s
   r$  r	  )
r   r!  r5   r6   r7   r8   r9   r:   r?   r   )
r4   r2   r5   r6   r7   r8   r9   r:   r?   r   )Z
__future__r   r   r   r   codecsr   collections.abcr   r   r   r   r   r	   r
   ior   typingr   r   r   r   r   r   _typesr   r   r   r   r   r   r   r   r   email.messager   r   r   r   r   r    r!   r"   r#   r   r%   r&   r   r   r   r(   r   r   r   r   r   r   r   r   rg   r  r  r  r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#  r$  r<   r<   r<   r=   <module>   s     $
     S	
%