File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/pyasn1.zip
Back
PK ?�Z���� � codec/streaming.pynu �[��� # # This file is part of pyasn1 software. # # Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> # License: https://pyasn1.readthedocs.io/en/latest/license.html # import io import os from pyasn1 import error from pyasn1.type import univ class CachingStreamWrapper(io.IOBase): """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. """ def __init__(self, raw): self._raw = raw self._cache = io.BytesIO() self._markedPosition = 0 def peek(self, n): result = self.read(n) self._cache.seek(-len(result), os.SEEK_CUR) return result def seekable(self): return True def seek(self, n=-1, whence=os.SEEK_SET): # Note that this not safe for seeking forward. return self._cache.seek(n, whence) def read(self, n=-1): read_from_cache = self._cache.read(n) if n != -1: n -= len(read_from_cache) if not n: # 0 bytes left to read return read_from_cache read_from_raw = self._raw.read(n) self._cache.write(read_from_raw) return read_from_cache + read_from_raw @property def markedPosition(self): """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. """ return self._markedPosition @markedPosition.setter def markedPosition(self, value): # By setting the value, we ensure we won't seek back before it. # `value` should be the same as the current position # We don't check for this for performance reasons. self._markedPosition = value # Whenever we set _marked_position, we know for sure # that we will not return back, and thus it is # safe to drop all cached data. if self._cache.tell() > io.DEFAULT_BUFFER_SIZE: self._cache = io.BytesIO(self._cache.read()) self._markedPosition = 0 def tell(self): return self._cache.tell() def asSeekableStream(substrate): """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. """ if isinstance(substrate, io.BytesIO): return substrate elif isinstance(substrate, bytes): return io.BytesIO(substrate) elif isinstance(substrate, univ.OctetString): return io.BytesIO(substrate.asOctets()) try: if substrate.seekable(): # Will fail for most invalid types return substrate else: return CachingStreamWrapper(substrate) except AttributeError: raise error.UnsupportedSubstrateError( "Cannot convert " + substrate.__class__.__name__ + " to a seekable bit stream.") def isEndOfStream(substrate): """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` """ if isinstance(substrate, io.BytesIO): cp = substrate.tell() substrate.seek(0, os.SEEK_END) result = substrate.tell() == cp substrate.seek(cp, os.SEEK_SET) yield result else: received = substrate.read(1) if received is None: yield if received: substrate.seek(-1, os.SEEK_CUR) yield not received def peekIntoStream(substrate, size=-1): """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 """ if hasattr(substrate, "peek"): received = substrate.peek(size) if received is None: yield while len(received) < size: yield yield received else: current_position = substrate.tell() try: for chunk in readFromStream(substrate, size): yield chunk finally: substrate.seek(current_position) def readFromStream(substrate, size=-1, context=None): """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 """ while True: # this will block unless stream is non-blocking received = substrate.read(size) if received is None: # non-blocking stream can do this yield error.SubstrateUnderrunError(context=context) elif not received and size != 0: # end-of-stream raise error.EndOfStreamError(context=context) elif len(received) < size: substrate.seek(-len(received), os.SEEK_CUR) # behave like a non-blocking stream yield error.SubstrateUnderrunError(context=context) else: break yield received PK ?�Z+��# �# codec/native/decoder.pynu �[��� # # This file is part of pyasn1 software. # # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com> # License: https://pyasn1.readthedocs.io/en/latest/license.html # import warnings from pyasn1 import debug from pyasn1 import error from pyasn1.compat import _MISSING from pyasn1.type import base from pyasn1.type import char from pyasn1.type import tag from pyasn1.type import univ from pyasn1.type import useful __all__ = ['decode'] LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_DECODER) class AbstractScalarPayloadDecoder(object): def __call__(self, pyObject, asn1Spec, decodeFun=None, **options): return asn1Spec.clone(pyObject) class BitStringPayloadDecoder(AbstractScalarPayloadDecoder): def __call__(self, pyObject, asn1Spec, decodeFun=None, **options): return asn1Spec.clone(univ.BitString.fromBinaryString(pyObject)) class SequenceOrSetPayloadDecoder(object): def __call__(self, pyObject, asn1Spec, decodeFun=None, **options): asn1Value = asn1Spec.clone() componentsTypes = asn1Spec.componentType for field in asn1Value: if field in pyObject: asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options) return asn1Value class SequenceOfOrSetOfPayloadDecoder(object): def __call__(self, pyObject, asn1Spec, decodeFun=None, **options): asn1Value = asn1Spec.clone() for pyValue in pyObject: asn1Value.append(decodeFun(pyValue, asn1Spec.componentType), **options) return asn1Value class ChoicePayloadDecoder(object): def __call__(self, pyObject, asn1Spec, decodeFun=None, **options): asn1Value = asn1Spec.clone() componentsTypes = asn1Spec.componentType for field in pyObject: if field in componentsTypes: asn1Value[field] = decodeFun(pyObject[field], componentsTypes[field].asn1Object, **options) break return asn1Value TAG_MAP = { univ.Integer.tagSet: AbstractScalarPayloadDecoder(), univ.Boolean.tagSet: AbstractScalarPayloadDecoder(), univ.BitString.tagSet: BitStringPayloadDecoder(), univ.OctetString.tagSet: AbstractScalarPayloadDecoder(), univ.Null.tagSet: AbstractScalarPayloadDecoder(), univ.ObjectIdentifier.tagSet: AbstractScalarPayloadDecoder(), univ.RelativeOID.tagSet: AbstractScalarPayloadDecoder(), univ.Enumerated.tagSet: AbstractScalarPayloadDecoder(), univ.Real.tagSet: AbstractScalarPayloadDecoder(), univ.Sequence.tagSet: SequenceOrSetPayloadDecoder(), # conflicts with SequenceOf univ.Set.tagSet: SequenceOrSetPayloadDecoder(), # conflicts with SetOf univ.Choice.tagSet: ChoicePayloadDecoder(), # conflicts with Any # character string types char.UTF8String.tagSet: AbstractScalarPayloadDecoder(), char.NumericString.tagSet: AbstractScalarPayloadDecoder(), char.PrintableString.tagSet: AbstractScalarPayloadDecoder(), char.TeletexString.tagSet: AbstractScalarPayloadDecoder(), char.VideotexString.tagSet: AbstractScalarPayloadDecoder(), char.IA5String.tagSet: AbstractScalarPayloadDecoder(), char.GraphicString.tagSet: AbstractScalarPayloadDecoder(), char.VisibleString.tagSet: AbstractScalarPayloadDecoder(), char.GeneralString.tagSet: AbstractScalarPayloadDecoder(), char.UniversalString.tagSet: AbstractScalarPayloadDecoder(), char.BMPString.tagSet: AbstractScalarPayloadDecoder(), # useful types useful.ObjectDescriptor.tagSet: AbstractScalarPayloadDecoder(), useful.GeneralizedTime.tagSet: AbstractScalarPayloadDecoder(), useful.UTCTime.tagSet: AbstractScalarPayloadDecoder() } # Put in ambiguous & non-ambiguous types for faster codec lookup TYPE_MAP = { univ.Integer.typeId: AbstractScalarPayloadDecoder(), univ.Boolean.typeId: AbstractScalarPayloadDecoder(), univ.BitString.typeId: BitStringPayloadDecoder(), univ.OctetString.typeId: AbstractScalarPayloadDecoder(), univ.Null.typeId: AbstractScalarPayloadDecoder(), univ.ObjectIdentifier.typeId: AbstractScalarPayloadDecoder(), univ.RelativeOID.typeId: AbstractScalarPayloadDecoder(), univ.Enumerated.typeId: AbstractScalarPayloadDecoder(), univ.Real.typeId: AbstractScalarPayloadDecoder(), # ambiguous base types univ.Set.typeId: SequenceOrSetPayloadDecoder(), univ.SetOf.typeId: SequenceOfOrSetOfPayloadDecoder(), univ.Sequence.typeId: SequenceOrSetPayloadDecoder(), univ.SequenceOf.typeId: SequenceOfOrSetOfPayloadDecoder(), univ.Choice.typeId: ChoicePayloadDecoder(), univ.Any.typeId: AbstractScalarPayloadDecoder(), # character string types char.UTF8String.typeId: AbstractScalarPayloadDecoder(), char.NumericString.typeId: AbstractScalarPayloadDecoder(), char.PrintableString.typeId: AbstractScalarPayloadDecoder(), char.TeletexString.typeId: AbstractScalarPayloadDecoder(), char.VideotexString.typeId: AbstractScalarPayloadDecoder(), char.IA5String.typeId: AbstractScalarPayloadDecoder(), char.GraphicString.typeId: AbstractScalarPayloadDecoder(), char.VisibleString.typeId: AbstractScalarPayloadDecoder(), char.GeneralString.typeId: AbstractScalarPayloadDecoder(), char.UniversalString.typeId: AbstractScalarPayloadDecoder(), char.BMPString.typeId: AbstractScalarPayloadDecoder(), # useful types useful.ObjectDescriptor.typeId: AbstractScalarPayloadDecoder(), useful.GeneralizedTime.typeId: AbstractScalarPayloadDecoder(), useful.UTCTime.typeId: AbstractScalarPayloadDecoder() } class SingleItemDecoder(object): TAG_MAP = TAG_MAP TYPE_MAP = TYPE_MAP def __init__(self, tagMap=_MISSING, typeMap=_MISSING, **ignored): self._tagMap = tagMap if tagMap is not _MISSING else self.TAG_MAP self._typeMap = typeMap if typeMap is not _MISSING else self.TYPE_MAP def __call__(self, pyObject, asn1Spec, **options): if LOG: debug.scope.push(type(pyObject).__name__) LOG('decoder called at scope %s, working with ' 'type %s' % (debug.scope, type(pyObject).__name__)) if asn1Spec is None or not isinstance(asn1Spec, base.Asn1Item): raise error.PyAsn1Error( 'asn1Spec is not valid (should be an instance of an ASN.1 ' 'Item, not %s)' % asn1Spec.__class__.__name__) try: valueDecoder = self._typeMap[asn1Spec.typeId] except KeyError: # use base type for codec lookup to recover untagged types baseTagSet = tag.TagSet(asn1Spec.tagSet.baseTag, asn1Spec.tagSet.baseTag) try: valueDecoder = self._tagMap[baseTagSet] except KeyError: raise error.PyAsn1Error('Unknown ASN.1 tag %s' % asn1Spec.tagSet) if LOG: LOG('calling decoder %s on Python type %s ' '<%s>' % (type(valueDecoder).__name__, type(pyObject).__name__, repr(pyObject))) value = valueDecoder(pyObject, asn1Spec, self, **options) if LOG: LOG('decoder %s produced ASN.1 type %s ' '<%s>' % (type(valueDecoder).__name__, type(value).__name__, repr(value))) debug.scope.pop() return value class Decoder(object): SINGLE_ITEM_DECODER = SingleItemDecoder def __init__(self, **options): self._singleItemDecoder = self.SINGLE_ITEM_DECODER(**options) def __call__(self, pyObject, asn1Spec=None, **kwargs): return self._singleItemDecoder(pyObject, asn1Spec=asn1Spec, **kwargs) #: Turns Python objects of built-in types into ASN.1 objects. #: #: Takes Python objects of built-in types and turns them into a tree of #: ASN.1 objects (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which #: may be a scalar or an arbitrary nested structure. #: #: Parameters #: ---------- #: pyObject: :py:class:`object` #: A scalar or nested Python objects #: #: Keyword Args #: ------------ #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative #: A pyasn1 type object to act as a template guiding the decoder. It is required #: for successful interpretation of Python objects mapping into their ASN.1 #: representations. #: #: Returns #: ------- #: : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative #: A scalar or constructed pyasn1 object #: #: Raises #: ------ #: ~pyasn1.error.PyAsn1Error #: On decoding errors #: #: Examples #: -------- #: Decode native Python object into ASN.1 objects with ASN.1 schema #: #: .. code-block:: pycon #: #: >>> seq = SequenceOf(componentType=Integer()) #: >>> s, _ = decode([1, 2, 3], asn1Spec=seq) #: >>> str(s) #: SequenceOf: #: 1 2 3 #: decode = Decoder() def __getattr__(attr: str): if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr): warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning) return globals()[newAttr] raise AttributeError(attr) PK ?�Z�zɋ 0 codec/native/__pycache__/decoder.cpython-310.pycnu �[��� o �h�# � @ s� d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z d dlm Z d d lmZ d gZej eejd�ZG dd � d e�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�Zi e jje� �e jje� �e jje� �e jje� �e jje� �e jje� �e jje� �e jje� �e j je� �e j!je� �e j"je� �e j#je� �ej$je� �ej%je� �ej&je� �ej'je� �ej(je� �ej)je� ej*je� ej+je� ej,je� ej-je� ej.je� ej/je� ej0je� ej1je� i �Z2i e jj3e� �e jj3e� �e jj3e� �e jj3e� �e jj3e� �e jj3e� �e jj3e� �e jj3e� �e j j3e� �e j"j3e� �e j4j3e� �e j!j3e� �e j5j3e� �e j#j3e� �e j6j3e� �ej$j3e� �ej%j3e� �ej&j3e� ej'j3e� ej(j3e� ej)j3e� ej*j3e� ej+j3e� ej,j3e� ej-j3e� ej.j3e� ej/j3e� ej0j3e� ej1j3e� i�Z7G dd� de�Z8G dd� de�Z9e9� Z:de;fdd�Z<dS )� N)�debug)�error)�_MISSING)�base)�char)�tag)�univ)�useful�decode)�flagsc @ � e Zd Zddd�ZdS )�AbstractScalarPayloadDecoderNc K s |� |�S �N)�clone��self�pyObject�asn1Spec� decodeFun�options� r �N/usr/local/CyberCP/lib/python3.10/site-packages/pyasn1/codec/native/decoder.py�__call__ s z%AbstractScalarPayloadDecoder.__call__r ��__name__� __module__�__qualname__r r r r r r � r c @ r )�BitStringPayloadDecoderNc K s |� tj�|��S r )r r � BitString�fromBinaryStringr r r r r s z BitStringPayloadDecoder.__call__r r r r r r r r r c @ r )�SequenceOrSetPayloadDecoderNc K sD |� � }|j}|D ]}||v r||| || jfi |��||<