コード例 #1
0
 def _text_string_parts(self):
     while True:
         initial_byte = ord(self.read(1))
         if 0x60 <= initial_byte < 0x7c:
             yield self.decode_text_string(initial_byte)
         elif initial_byte == 0xff:
             break
         else:
             raise BadInitialByteError(
                 f'bad initial byte 0x{initial_byte:x} in '
                 f'indefinite-length text string')
コード例 #2
0
 async def decode_length(self, initial_byte):
     minor = initial_byte & 0x1f
     if minor < 24:
         return minor
     if minor < 28:
         kind = minor - 24
         length, = uint_unpackers[kind](await self.read(1 << kind))
         return length
     if initial_byte in {0x5f, 0x7f, 0x9f, 0xbf}:
         return None
     raise BadInitialByteError(f'bad initial byte 0x{initial_byte:x}')
コード例 #3
0
 def decode_text_string(self, initial_byte):
     length = self.decode_length(initial_byte)
     read = self.read
     if length is None:
         yield ContextILTextString()
         decode_length = self.decode_length
         while True:
             initial_byte = ord(read(1))
             if 0x60 <= initial_byte < 0x7c:
                 yield self._decode_text(read(decode_length(initial_byte)))
             elif initial_byte == 0xff:
                 break
             else:
                 raise BadInitialByteError(
                     f'bad initial byte 0x{initial_byte:x} in '
                     f'indefinite-length byte string')
         yield Break
     else:
         yield self._decode_text(self.read(length))
コード例 #4
0
 async def decode_byte_string(self, initial_byte):
     length = await self.decode_length(initial_byte)
     read = self.read
     if length is None:
         yield ContextILByteString()
         decode_length = self.decode_length
         while True:
             initial_byte = ord(await read(1))
             if 0x40 <= initial_byte < 0x5c:
                 yield await read(await decode_length(initial_byte))
             elif initial_byte == 0xff:
                 break
             else:
                 raise BadInitialByteError(
                     f'bad initial byte 0x{initial_byte:x} in '
                     f'indefinite-length byte string')
         yield Break
     else:
         yield await read(length)
コード例 #5
0
 def decode_simple(self, initial_byte):
     # Pass initial_byte not length to detect ill-formed simples
     value = initial_byte & 0x1f
     if value < 20:
         yield self._simple_value(value)
     elif value < 24:
         yield CBORSimple.assigned_values[value]
     elif value == 24:
         value = ord(self.read(1))
         if value < 32:
             raise BadSimpleError(
                 f'simple value 0x{value:x} encoded with extra byte')
         yield self._simple_value(value)
     elif value < 28:
         length = 1 << (value - 24)
         float_value, = be_float_unpackers[value - 25](self.read(length))
         yield float_value
     elif value == 31:
         raise MisplacedBreakError(
             'break code outside indefinite-length object')
     else:
         raise BadInitialByteError(f'bad initial byte 0x{initial_byte:x}')
コード例 #6
0
 def decode_length(self, initial_byte):
     minor = initial_byte & 0x1f
     if minor < 24:
         return minor
     if minor < 28:
         kind = minor - 24
         length, = uint_unpackers[kind](self.read(1 << kind))
         if self._deterministic & DeterministicFlags.LENGTH and length < uint_minima[
                 kind]:
             if initial_byte < 0x20:
                 raise DeterministicError(
                     f'value {length:,d} is not minimally encoded')
             elif initial_byte < 0x40:
                 raise DeterministicError(
                     f'value {-1 - length:,d} is not minimally encoded')
             else:
                 raise DeterministicError(
                     f'length {length:,d} is not minimally encoded')
         return length
     if initial_byte in {0x5f, 0x7f, 0x9f, 0xbf}:
         return -1
     raise BadInitialByteError(f'bad initial byte 0x{initial_byte:x}')
コード例 #7
0
 def decode_simple(self, initial_byte):
     value = initial_byte & 0x1f
     if value < 20:
         return self._simple_value(value)
     if value < 24:
         return CBORSimple.assigned_values[value]
     if value == 24:
         value = ord(self.read(1))
         if value < 32:
             raise BadSimpleError(
                 f'simple value 0x{value:x} encoded with extra byte')
         return self._simple_value(value)
     if value < 28:
         length = 1 << (value - 24)
         float_value, = be_float_unpackers[value - 25](self.read(length))
         if value > 25 and self._deterministic & DeterministicFlags.FLOAT:
             if length != len(pack_cbor_short_float(float_value)) - 1:
                 raise DeterministicError(
                     f'float {float_value} is not minimally encoded')
         return float_value
     if value == 31:
         raise MisplacedBreakError(
             'break code outside indefinite-length object')
     raise BadInitialByteError(f'bad initial byte 0x{initial_byte:x}')