Ejemplo n.º 1
0
    def decode(self, shareable_index=None):
        """
        Decode the next value from the stream.

        :raises EOFError: if EOF is encountered while reading
        :raises CBORDecodeError: if there is any problem decoding the stream

        """
        byte = self.fp.read(1)
        if not byte:
            raise EOFError()

        try:
            initial_byte = byte_as_integer(byte)
            major_type = initial_byte >> 5
            subtype = initial_byte & 31
        except Exception as e:
            raise CBORDecodeError('error reading major type at index {}: {}'
                                  .format(self.fp.tell(), e))

        decoder = major_decoders[major_type]
        try:
            return decoder(self, subtype, shareable_index)
        except CBORDecodeError:
            raise
        except Exception as e:
            raise CBORDecodeError('error decoding value at index {}: {}'.format(self.fp.tell(), e))
Ejemplo n.º 2
0
def decode_bytestring(decoder, subtype, shareable_index=None):
    # Major tag 2
    length = decode_uint(decoder, subtype, allow_infinite=True)
    if length is None:
        # Indefinite length
        buf = bytearray()
        while True:
            initial_byte = byte_as_integer(decoder.read(1))
            if initial_byte == 255:
                return buf
            else:
                length = decode_uint(decoder, initial_byte & 31)
                value = decoder.read(length)
                buf.extend(value)
    else:
        return decoder.read(length)
Ejemplo n.º 3
0
 def decode_bytestring(self, subtype, fp, shareable_index=None):
     # Major tag 2
     length = self.decode_uint(subtype, fp, allow_infinite=True)
     if length is None:
         # Indefinite length
         buf = bytearray()
         while True:
             initial_byte = byte_as_integer(fp.read(1))
             if initial_byte == 255:
                 return buf
             else:
                 length = self.decode_uint(initial_byte & 31, fp)
                 value = fp.read(length)
                 buf.extend(value)
     else:
         return fp.read(length)
Ejemplo n.º 4
0
    def decode(self, fp, shareable_index=None):
        """Decode the next value from the stream."""
        try:
            initial_byte = byte_as_integer(fp.read(1))
            major_type = initial_byte >> 5
            subtype = initial_byte & 31
        except Exception as e:
            raise CBORDecodeError('error reading major type at index {}: {}'
                                  .format(fp.tell(), e))

        decoder = self.major_decoders[major_type]
        try:
            return decoder(self, subtype, fp, shareable_index)
        except CBORDecodeError:
            raise
        except Exception as e:
            raise CBORDecodeError('error decoding value at index {}: {}'.format(fp.tell(), e))