How to use the cbor2.types.CBORDecodeValueError function in cbor2

To help you get started, we’ve selected a few cbor2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agronholm / cbor2 / cbor2 / decoder.py View on Github external
def decode_sharedref(self):
        # Semantic tag 29
        value = self._decode(unshared=True)
        try:
            shared = self._shareables[value]
        except IndexError:
            raise CBORDecodeValueError('shared reference %d not found' % value)

        if shared is None:
            raise CBORDecodeValueError(
                'shared value %d has not been initialized' % value)
        else:
            return shared
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
#     character boundary.
            #
            # This precludes using the indefinite bytestring decoder above as
            # that would happily ignore UTF-8 characters split across chunks.
            buf = []
            while True:
                initial_byte = byte_as_integer(self.read(1))
                if initial_byte == 0xff:
                    result = ''.join(buf)
                    break
                elif initial_byte >> 5 == 3:
                    length = self._decode_length(initial_byte & 0x1f)
                    value = self.read(length).decode('utf-8', self._str_errors)
                    buf.append(value)
                else:
                    raise CBORDecodeValueError(
                        "non-string found in indefinite length string")
        else:
            result = self.read(length).decode('utf-8', self._str_errors)
        return self.set_shareable(result)
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
def _decode_length(self, subtype, allow_indefinite=False):
        if subtype < 24:
            return subtype
        elif subtype == 24:
            return byte_as_integer(self.read(1))
        elif subtype == 25:
            return struct.unpack('>H', self.read(2))[0]
        elif subtype == 26:
            return struct.unpack('>L', self.read(4))[0]
        elif subtype == 27:
            return struct.unpack('>Q', self.read(8))[0]
        elif subtype == 31 and allow_indefinite:
            return None
        else:
            raise CBORDecodeValueError(
                'unknown unsigned integer subtype 0x%x' % subtype)
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
# Major tag 2
        length = self._decode_length(subtype, allow_indefinite=True)
        if length is None:
            # Indefinite length
            buf = []
            while True:
                initial_byte = byte_as_integer(self.read(1))
                if initial_byte == 0xff:
                    result = b''.join(buf)
                    break
                elif initial_byte >> 5 == 2:
                    length = self._decode_length(initial_byte & 0x1f)
                    value = self.read(length)
                    buf.append(value)
                else:
                    raise CBORDecodeValueError(
                        "non-bytestring found in indefinite length bytestring")
        else:
            result = self.read(length)
        return self.set_shareable(result)
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
def decode_ipnetwork(self):
        # Semantic tag 261
        from ipaddress import ip_network
        net_map = self.decode()
        if isinstance(net_map, Mapping) and len(net_map) == 1:
            for net in net_map.items():
                try:
                    return self.set_shareable(ip_network(net, strict=False))
                except (TypeError, ValueError):
                    break
        raise CBORDecodeValueError("invalid ipnetwork value %r" % net_map)
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
minute,
                second,
                micro,
                offset_h,
                offset_m,
            ) = match.groups()
            if offset_h:
                tz = timezone(timedelta(hours=int(offset_h), minutes=int(offset_m)))
            else:
                tz = timezone.utc

            return self.set_shareable(datetime(
                int(year), int(month), int(day),
                int(hour), int(minute), int(second), int(micro or 0), tz))
        else:
            raise CBORDecodeValueError(
                'invalid datetime string: {!r}'.format(value))
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
def decode_ipaddress(self):
        # Semantic tag 260
        from ipaddress import ip_address
        buf = self.decode()
        if not isinstance(buf, bytes) or len(buf) not in (4, 6, 16):
            raise CBORDecodeValueError("invalid ipaddress value %r" % buf)
        elif len(buf) in (4, 16):
            return self.set_shareable(ip_address(buf))
        elif len(buf) == 6:
            # MAC address
            return self.set_shareable(CBORTag(260, buf))