How to use the cbor2.compat.byte_as_integer 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_simple_value(self):
        # XXX Set shareable?
        return CBORSimpleValue(byte_as_integer(self.read(1)))
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
# NOTE: It may seem redundant to repeat this code to handle UTF-8
            # strings but there is a reason to do this separately to
            # byte-strings. Specifically, the CBOR spec states (in sec. 2.2):
            #
            #     Text strings with indefinite lengths act the same as byte
            #     strings with indefinite lengths, except that all their chunks
            #     MUST be definite-length text strings.  Note that this implies
            #     that the bytes of a single UTF-8 character cannot be spread
            #     between chunks: a new chunk can only be started at a
            #     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(self, immutable=False, unshared=False):
        if immutable:
            old_immutable = self._immutable
            self._immutable = True
        if unshared:
            old_index = self._share_index
            self._share_index = None
        try:
            initial_byte = byte_as_integer(self.read(1))
            major_type = initial_byte >> 5
            subtype = initial_byte & 31
            decoder = major_decoders[major_type]
            return decoder(self, subtype)
        finally:
            if immutable:
                self._immutable = old_immutable
            if unshared:
                self._share_index = old_index
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
def decode_bytestring(self, subtype):
        # 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)