mercurial/utils/cborutil.py
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 43106 d783f945a701
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
   123     yield BREAK
   123     yield BREAK
   124 
   124 
   125 
   125 
   126 def streamencodeint(v):
   126 def streamencodeint(v):
   127     if v >= 18446744073709551616 or v < -18446744073709551616:
   127     if v >= 18446744073709551616 or v < -18446744073709551616:
   128         raise ValueError('big integers not supported')
   128         raise ValueError(b'big integers not supported')
   129 
   129 
   130     if v >= 0:
   130     if v >= 0:
   131         yield encodelength(MAJOR_TYPE_UINT, v)
   131         yield encodelength(MAJOR_TYPE_UINT, v)
   132     else:
   132     else:
   133         yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1)
   133         yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1)
   239                 continue
   239                 continue
   240             fn = STREAM_ENCODERS[ty]
   240             fn = STREAM_ENCODERS[ty]
   241             break
   241             break
   242 
   242 
   243     if not fn:
   243     if not fn:
   244         raise ValueError('do not know how to encode %s' % type(v))
   244         raise ValueError(b'do not know how to encode %s' % type(v))
   245 
   245 
   246     return fn(v)
   246     return fn(v)
   247 
   247 
   248 
   248 
   249 class CBORDecodeError(Exception):
   249 class CBORDecodeError(Exception):
   261     def _elementtointeger(b, i):
   261     def _elementtointeger(b, i):
   262         return ord(b[i])
   262         return ord(b[i])
   263 
   263 
   264 
   264 
   265 STRUCT_BIG_UBYTE = struct.Struct(r'>B')
   265 STRUCT_BIG_UBYTE = struct.Struct(r'>B')
   266 STRUCT_BIG_USHORT = struct.Struct('>H')
   266 STRUCT_BIG_USHORT = struct.Struct(b'>H')
   267 STRUCT_BIG_ULONG = struct.Struct('>L')
   267 STRUCT_BIG_ULONG = struct.Struct(b'>L')
   268 STRUCT_BIG_ULONGLONG = struct.Struct('>Q')
   268 STRUCT_BIG_ULONGLONG = struct.Struct(b'>Q')
   269 
   269 
   270 SPECIAL_NONE = 0
   270 SPECIAL_NONE = 0
   271 SPECIAL_START_INDEFINITE_BYTESTRING = 1
   271 SPECIAL_START_INDEFINITE_BYTESTRING = 1
   272 SPECIAL_START_ARRAY = 2
   272 SPECIAL_START_ARRAY = 2
   273 SPECIAL_START_MAP = 3
   273 SPECIAL_START_MAP = 3
   353         # It is an indefinite length bytestring.
   353         # It is an indefinite length bytestring.
   354         else:
   354         else:
   355             return True, None, 1, SPECIAL_START_INDEFINITE_BYTESTRING
   355             return True, None, 1, SPECIAL_START_INDEFINITE_BYTESTRING
   356 
   356 
   357     elif majortype == MAJOR_TYPE_STRING:
   357     elif majortype == MAJOR_TYPE_STRING:
   358         raise CBORDecodeError('string major type not supported')
   358         raise CBORDecodeError(b'string major type not supported')
   359 
   359 
   360     elif majortype == MAJOR_TYPE_ARRAY:
   360     elif majortype == MAJOR_TYPE_ARRAY:
   361         # Beginning of arrays are treated as uints in order to decode their
   361         # Beginning of arrays are treated as uints in order to decode their
   362         # length. We don't allow indefinite length arrays.
   362         # length. We don't allow indefinite length arrays.
   363         complete, size, readcount = decodeuint(subtype, b, offset)
   363         complete, size, readcount = decodeuint(subtype, b, offset)
   402             if not complete:
   402             if not complete:
   403                 return False, None, readcount2, SPECIAL_NONE
   403                 return False, None, readcount2, SPECIAL_NONE
   404 
   404 
   405             if special != SPECIAL_START_ARRAY:
   405             if special != SPECIAL_START_ARRAY:
   406                 raise CBORDecodeError(
   406                 raise CBORDecodeError(
   407                     'expected array after finite set ' 'semantic tag'
   407                     b'expected array after finite set ' b'semantic tag'
   408                 )
   408                 )
   409 
   409 
   410             return True, size, readcount + readcount2 + 1, SPECIAL_START_SET
   410             return True, size, readcount + readcount2 + 1, SPECIAL_START_SET
   411 
   411 
   412         else:
   412         else:
   413             raise CBORDecodeError('semantic tag %d not allowed' % tagvalue)
   413             raise CBORDecodeError(b'semantic tag %d not allowed' % tagvalue)
   414 
   414 
   415     elif majortype == MAJOR_TYPE_SPECIAL:
   415     elif majortype == MAJOR_TYPE_SPECIAL:
   416         # Only specific values for the information field are allowed.
   416         # Only specific values for the information field are allowed.
   417         if subtype == SUBTYPE_FALSE:
   417         if subtype == SUBTYPE_FALSE:
   418             return True, False, 1, SPECIAL_NONE
   418             return True, False, 1, SPECIAL_NONE
   422             return True, None, 1, SPECIAL_NONE
   422             return True, None, 1, SPECIAL_NONE
   423         elif subtype == SUBTYPE_INDEFINITE:
   423         elif subtype == SUBTYPE_INDEFINITE:
   424             return True, None, 1, SPECIAL_INDEFINITE_BREAK
   424             return True, None, 1, SPECIAL_INDEFINITE_BREAK
   425         # If value is 24, subtype is in next byte.
   425         # If value is 24, subtype is in next byte.
   426         else:
   426         else:
   427             raise CBORDecodeError('special type %d not allowed' % subtype)
   427             raise CBORDecodeError(b'special type %d not allowed' % subtype)
   428     else:
   428     else:
   429         assert False
   429         assert False
   430 
   430 
   431 
   431 
   432 def decodeuint(subtype, b, offset=0, allowindefinite=False):
   432 def decodeuint(subtype, b, offset=0, allowindefinite=False):
   455     # Indefinite length specifier.
   455     # Indefinite length specifier.
   456     elif subtype == 31:
   456     elif subtype == 31:
   457         if allowindefinite:
   457         if allowindefinite:
   458             return True, None, 0
   458             return True, None, 0
   459         else:
   459         else:
   460             raise CBORDecodeError('indefinite length uint not allowed here')
   460             raise CBORDecodeError(b'indefinite length uint not allowed here')
   461     elif subtype >= 28:
   461     elif subtype >= 28:
   462         raise CBORDecodeError(
   462         raise CBORDecodeError(
   463             'unsupported subtype on integer type: %d' % subtype
   463             b'unsupported subtype on integer type: %d' % subtype
   464         )
   464         )
   465 
   465 
   466     if subtype == 24:
   466     if subtype == 24:
   467         s = STRUCT_BIG_UBYTE
   467         s = STRUCT_BIG_UBYTE
   468     elif subtype == 25:
   468     elif subtype == 25:
   470     elif subtype == 26:
   470     elif subtype == 26:
   471         s = STRUCT_BIG_ULONG
   471         s = STRUCT_BIG_ULONG
   472     elif subtype == 27:
   472     elif subtype == 27:
   473         s = STRUCT_BIG_ULONGLONG
   473         s = STRUCT_BIG_ULONGLONG
   474     else:
   474     else:
   475         raise CBORDecodeError('bounds condition checking violation')
   475         raise CBORDecodeError(b'bounds condition checking violation')
   476 
   476 
   477     if len(b) - offset >= s.size:
   477     if len(b) - offset >= s.size:
   478         return True, s.unpack_from(b, offset)[0], s.size
   478         return True, s.unpack_from(b, offset)[0], s.size
   479     else:
   479     else:
   480         return False, None, len(b) - offset - s.size
   480         return False, None, len(b) - offset - s.size
   639                 if special == SPECIAL_NONE:
   639                 if special == SPECIAL_NONE:
   640                     self._decodedvalues.append(value)
   640                     self._decodedvalues.append(value)
   641 
   641 
   642                 elif special == SPECIAL_START_ARRAY:
   642                 elif special == SPECIAL_START_ARRAY:
   643                     self._collectionstack.append(
   643                     self._collectionstack.append(
   644                         {'remaining': value, 'v': [],}
   644                         {b'remaining': value, b'v': [],}
   645                     )
   645                     )
   646                     self._state = self._STATE_WANT_ARRAY_VALUE
   646                     self._state = self._STATE_WANT_ARRAY_VALUE
   647 
   647 
   648                 elif special == SPECIAL_START_MAP:
   648                 elif special == SPECIAL_START_MAP:
   649                     self._collectionstack.append(
   649                     self._collectionstack.append(
   650                         {'remaining': value, 'v': {},}
   650                         {b'remaining': value, b'v': {},}
   651                     )
   651                     )
   652                     self._state = self._STATE_WANT_MAP_KEY
   652                     self._state = self._STATE_WANT_MAP_KEY
   653 
   653 
   654                 elif special == SPECIAL_START_SET:
   654                 elif special == SPECIAL_START_SET:
   655                     self._collectionstack.append(
   655                     self._collectionstack.append(
   656                         {'remaining': value, 'v': set(),}
   656                         {b'remaining': value, b'v': set(),}
   657                     )
   657                     )
   658                     self._state = self._STATE_WANT_SET_VALUE
   658                     self._state = self._STATE_WANT_SET_VALUE
   659 
   659 
   660                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   660                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   661                     self._state = self._STATE_WANT_BYTESTRING_CHUNK_FIRST
   661                     self._state = self._STATE_WANT_BYTESTRING_CHUNK_FIRST
   662 
   662 
   663                 else:
   663                 else:
   664                     raise CBORDecodeError(
   664                     raise CBORDecodeError(
   665                         'unhandled special state: %d' % special
   665                         b'unhandled special state: %d' % special
   666                     )
   666                     )
   667 
   667 
   668             # This value becomes an element of the current array.
   668             # This value becomes an element of the current array.
   669             elif self._state == self._STATE_WANT_ARRAY_VALUE:
   669             elif self._state == self._STATE_WANT_ARRAY_VALUE:
   670                 # Simple values get appended.
   670                 # Simple values get appended.
   671                 if special == SPECIAL_NONE:
   671                 if special == SPECIAL_NONE:
   672                     c = self._collectionstack[-1]
   672                     c = self._collectionstack[-1]
   673                     c['v'].append(value)
   673                     c[b'v'].append(value)
   674                     c['remaining'] -= 1
   674                     c[b'remaining'] -= 1
   675 
   675 
   676                     # self._state doesn't need changed.
   676                     # self._state doesn't need changed.
   677 
   677 
   678                 # An array nested within an array.
   678                 # An array nested within an array.
   679                 elif special == SPECIAL_START_ARRAY:
   679                 elif special == SPECIAL_START_ARRAY:
   680                     lastc = self._collectionstack[-1]
   680                     lastc = self._collectionstack[-1]
   681                     newvalue = []
   681                     newvalue = []
   682 
   682 
   683                     lastc['v'].append(newvalue)
   683                     lastc[b'v'].append(newvalue)
   684                     lastc['remaining'] -= 1
   684                     lastc[b'remaining'] -= 1
   685 
   685 
   686                     self._collectionstack.append(
   686                     self._collectionstack.append(
   687                         {'remaining': value, 'v': newvalue,}
   687                         {b'remaining': value, b'v': newvalue,}
   688                     )
   688                     )
   689 
   689 
   690                     # self._state doesn't need changed.
   690                     # self._state doesn't need changed.
   691 
   691 
   692                 # A map nested within an array.
   692                 # A map nested within an array.
   693                 elif special == SPECIAL_START_MAP:
   693                 elif special == SPECIAL_START_MAP:
   694                     lastc = self._collectionstack[-1]
   694                     lastc = self._collectionstack[-1]
   695                     newvalue = {}
   695                     newvalue = {}
   696 
   696 
   697                     lastc['v'].append(newvalue)
   697                     lastc[b'v'].append(newvalue)
   698                     lastc['remaining'] -= 1
   698                     lastc[b'remaining'] -= 1
   699 
   699 
   700                     self._collectionstack.append(
   700                     self._collectionstack.append(
   701                         {'remaining': value, 'v': newvalue}
   701                         {b'remaining': value, b'v': newvalue}
   702                     )
   702                     )
   703 
   703 
   704                     self._state = self._STATE_WANT_MAP_KEY
   704                     self._state = self._STATE_WANT_MAP_KEY
   705 
   705 
   706                 elif special == SPECIAL_START_SET:
   706                 elif special == SPECIAL_START_SET:
   707                     lastc = self._collectionstack[-1]
   707                     lastc = self._collectionstack[-1]
   708                     newvalue = set()
   708                     newvalue = set()
   709 
   709 
   710                     lastc['v'].append(newvalue)
   710                     lastc[b'v'].append(newvalue)
   711                     lastc['remaining'] -= 1
   711                     lastc[b'remaining'] -= 1
   712 
   712 
   713                     self._collectionstack.append(
   713                     self._collectionstack.append(
   714                         {'remaining': value, 'v': newvalue,}
   714                         {b'remaining': value, b'v': newvalue,}
   715                     )
   715                     )
   716 
   716 
   717                     self._state = self._STATE_WANT_SET_VALUE
   717                     self._state = self._STATE_WANT_SET_VALUE
   718 
   718 
   719                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   719                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   720                     raise CBORDecodeError(
   720                     raise CBORDecodeError(
   721                         'indefinite length bytestrings '
   721                         b'indefinite length bytestrings '
   722                         'not allowed as array values'
   722                         b'not allowed as array values'
   723                     )
   723                     )
   724 
   724 
   725                 else:
   725                 else:
   726                     raise CBORDecodeError(
   726                     raise CBORDecodeError(
   727                         'unhandled special item when '
   727                         b'unhandled special item when '
   728                         'expecting array value: %d' % special
   728                         b'expecting array value: %d' % special
   729                     )
   729                     )
   730 
   730 
   731             # This value becomes the key of the current map instance.
   731             # This value becomes the key of the current map instance.
   732             elif self._state == self._STATE_WANT_MAP_KEY:
   732             elif self._state == self._STATE_WANT_MAP_KEY:
   733                 if special == SPECIAL_NONE:
   733                 if special == SPECIAL_NONE:
   734                     self._currentmapkey = value
   734                     self._currentmapkey = value
   735                     self._state = self._STATE_WANT_MAP_VALUE
   735                     self._state = self._STATE_WANT_MAP_VALUE
   736 
   736 
   737                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   737                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   738                     raise CBORDecodeError(
   738                     raise CBORDecodeError(
   739                         'indefinite length bytestrings '
   739                         b'indefinite length bytestrings '
   740                         'not allowed as map keys'
   740                         b'not allowed as map keys'
   741                     )
   741                     )
   742 
   742 
   743                 elif special in (
   743                 elif special in (
   744                     SPECIAL_START_ARRAY,
   744                     SPECIAL_START_ARRAY,
   745                     SPECIAL_START_MAP,
   745                     SPECIAL_START_MAP,
   746                     SPECIAL_START_SET,
   746                     SPECIAL_START_SET,
   747                 ):
   747                 ):
   748                     raise CBORDecodeError(
   748                     raise CBORDecodeError(
   749                         'collections not supported as map ' 'keys'
   749                         b'collections not supported as map ' b'keys'
   750                     )
   750                     )
   751 
   751 
   752                 # We do not allow special values to be used as map keys.
   752                 # We do not allow special values to be used as map keys.
   753                 else:
   753                 else:
   754                     raise CBORDecodeError(
   754                     raise CBORDecodeError(
   755                         'unhandled special item when '
   755                         b'unhandled special item when '
   756                         'expecting map key: %d' % special
   756                         b'expecting map key: %d' % special
   757                     )
   757                     )
   758 
   758 
   759             # This value becomes the value of the current map key.
   759             # This value becomes the value of the current map key.
   760             elif self._state == self._STATE_WANT_MAP_VALUE:
   760             elif self._state == self._STATE_WANT_MAP_VALUE:
   761                 # Simple values simply get inserted into the map.
   761                 # Simple values simply get inserted into the map.
   762                 if special == SPECIAL_NONE:
   762                 if special == SPECIAL_NONE:
   763                     lastc = self._collectionstack[-1]
   763                     lastc = self._collectionstack[-1]
   764                     lastc['v'][self._currentmapkey] = value
   764                     lastc[b'v'][self._currentmapkey] = value
   765                     lastc['remaining'] -= 1
   765                     lastc[b'remaining'] -= 1
   766 
   766 
   767                     self._state = self._STATE_WANT_MAP_KEY
   767                     self._state = self._STATE_WANT_MAP_KEY
   768 
   768 
   769                 # A new array is used as the map value.
   769                 # A new array is used as the map value.
   770                 elif special == SPECIAL_START_ARRAY:
   770                 elif special == SPECIAL_START_ARRAY:
   771                     lastc = self._collectionstack[-1]
   771                     lastc = self._collectionstack[-1]
   772                     newvalue = []
   772                     newvalue = []
   773 
   773 
   774                     lastc['v'][self._currentmapkey] = newvalue
   774                     lastc[b'v'][self._currentmapkey] = newvalue
   775                     lastc['remaining'] -= 1
   775                     lastc[b'remaining'] -= 1
   776 
   776 
   777                     self._collectionstack.append(
   777                     self._collectionstack.append(
   778                         {'remaining': value, 'v': newvalue,}
   778                         {b'remaining': value, b'v': newvalue,}
   779                     )
   779                     )
   780 
   780 
   781                     self._state = self._STATE_WANT_ARRAY_VALUE
   781                     self._state = self._STATE_WANT_ARRAY_VALUE
   782 
   782 
   783                 # A new map is used as the map value.
   783                 # A new map is used as the map value.
   784                 elif special == SPECIAL_START_MAP:
   784                 elif special == SPECIAL_START_MAP:
   785                     lastc = self._collectionstack[-1]
   785                     lastc = self._collectionstack[-1]
   786                     newvalue = {}
   786                     newvalue = {}
   787 
   787 
   788                     lastc['v'][self._currentmapkey] = newvalue
   788                     lastc[b'v'][self._currentmapkey] = newvalue
   789                     lastc['remaining'] -= 1
   789                     lastc[b'remaining'] -= 1
   790 
   790 
   791                     self._collectionstack.append(
   791                     self._collectionstack.append(
   792                         {'remaining': value, 'v': newvalue,}
   792                         {b'remaining': value, b'v': newvalue,}
   793                     )
   793                     )
   794 
   794 
   795                     self._state = self._STATE_WANT_MAP_KEY
   795                     self._state = self._STATE_WANT_MAP_KEY
   796 
   796 
   797                 # A new set is used as the map value.
   797                 # A new set is used as the map value.
   798                 elif special == SPECIAL_START_SET:
   798                 elif special == SPECIAL_START_SET:
   799                     lastc = self._collectionstack[-1]
   799                     lastc = self._collectionstack[-1]
   800                     newvalue = set()
   800                     newvalue = set()
   801 
   801 
   802                     lastc['v'][self._currentmapkey] = newvalue
   802                     lastc[b'v'][self._currentmapkey] = newvalue
   803                     lastc['remaining'] -= 1
   803                     lastc[b'remaining'] -= 1
   804 
   804 
   805                     self._collectionstack.append(
   805                     self._collectionstack.append(
   806                         {'remaining': value, 'v': newvalue,}
   806                         {b'remaining': value, b'v': newvalue,}
   807                     )
   807                     )
   808 
   808 
   809                     self._state = self._STATE_WANT_SET_VALUE
   809                     self._state = self._STATE_WANT_SET_VALUE
   810 
   810 
   811                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   811                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   812                     raise CBORDecodeError(
   812                     raise CBORDecodeError(
   813                         'indefinite length bytestrings not '
   813                         b'indefinite length bytestrings not '
   814                         'allowed as map values'
   814                         b'allowed as map values'
   815                     )
   815                     )
   816 
   816 
   817                 else:
   817                 else:
   818                     raise CBORDecodeError(
   818                     raise CBORDecodeError(
   819                         'unhandled special item when '
   819                         b'unhandled special item when '
   820                         'expecting map value: %d' % special
   820                         b'expecting map value: %d' % special
   821                     )
   821                     )
   822 
   822 
   823                 self._currentmapkey = None
   823                 self._currentmapkey = None
   824 
   824 
   825             # This value is added to the current set.
   825             # This value is added to the current set.
   826             elif self._state == self._STATE_WANT_SET_VALUE:
   826             elif self._state == self._STATE_WANT_SET_VALUE:
   827                 if special == SPECIAL_NONE:
   827                 if special == SPECIAL_NONE:
   828                     lastc = self._collectionstack[-1]
   828                     lastc = self._collectionstack[-1]
   829                     lastc['v'].add(value)
   829                     lastc[b'v'].add(value)
   830                     lastc['remaining'] -= 1
   830                     lastc[b'remaining'] -= 1
   831 
   831 
   832                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   832                 elif special == SPECIAL_START_INDEFINITE_BYTESTRING:
   833                     raise CBORDecodeError(
   833                     raise CBORDecodeError(
   834                         'indefinite length bytestrings not '
   834                         b'indefinite length bytestrings not '
   835                         'allowed as set values'
   835                         b'allowed as set values'
   836                     )
   836                     )
   837 
   837 
   838                 elif special in (
   838                 elif special in (
   839                     SPECIAL_START_ARRAY,
   839                     SPECIAL_START_ARRAY,
   840                     SPECIAL_START_MAP,
   840                     SPECIAL_START_MAP,
   841                     SPECIAL_START_SET,
   841                     SPECIAL_START_SET,
   842                 ):
   842                 ):
   843                     raise CBORDecodeError(
   843                     raise CBORDecodeError(
   844                         'collections not allowed as set ' 'values'
   844                         b'collections not allowed as set ' b'values'
   845                     )
   845                     )
   846 
   846 
   847                 # We don't allow non-trivial types to exist as set values.
   847                 # We don't allow non-trivial types to exist as set values.
   848                 else:
   848                 else:
   849                     raise CBORDecodeError(
   849                     raise CBORDecodeError(
   850                         'unhandled special item when '
   850                         b'unhandled special item when '
   851                         'expecting set value: %d' % special
   851                         b'expecting set value: %d' % special
   852                     )
   852                     )
   853 
   853 
   854             # This value represents the first chunk in an indefinite length
   854             # This value represents the first chunk in an indefinite length
   855             # bytestring.
   855             # bytestring.
   856             elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_FIRST:
   856             elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_FIRST:
   877                     assert not self._collectionstack
   877                     assert not self._collectionstack
   878                     self._state = self._STATE_NONE
   878                     self._state = self._STATE_NONE
   879 
   879 
   880                 else:
   880                 else:
   881                     raise CBORDecodeError(
   881                     raise CBORDecodeError(
   882                         'unexpected special value when '
   882                         b'unexpected special value when '
   883                         'expecting bytestring chunk: %d' % special
   883                         b'expecting bytestring chunk: %d' % special
   884                     )
   884                     )
   885 
   885 
   886             # This value represents the non-initial chunk in an indefinite
   886             # This value represents the non-initial chunk in an indefinite
   887             # length bytestring.
   887             # length bytestring.
   888             elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_SUBSEQUENT:
   888             elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_SUBSEQUENT:
   899                     assert not self._collectionstack
   899                     assert not self._collectionstack
   900                     self._state = self._STATE_NONE
   900                     self._state = self._STATE_NONE
   901 
   901 
   902                 else:
   902                 else:
   903                     raise CBORDecodeError(
   903                     raise CBORDecodeError(
   904                         'unexpected special value when '
   904                         b'unexpected special value when '
   905                         'expecting bytestring chunk: %d' % special
   905                         b'expecting bytestring chunk: %d' % special
   906                     )
   906                     )
   907 
   907 
   908             else:
   908             else:
   909                 raise CBORDecodeError(
   909                 raise CBORDecodeError(
   910                     'unhandled decoder state: %d' % self._state
   910                     b'unhandled decoder state: %d' % self._state
   911                 )
   911                 )
   912 
   912 
   913             # We could have just added the final value in a collection. End
   913             # We could have just added the final value in a collection. End
   914             # all complete collections at the top of the stack.
   914             # all complete collections at the top of the stack.
   915             while True:
   915             while True:
   922                     break
   922                     break
   923 
   923 
   924                 # Or we are expecting more items for this collection.
   924                 # Or we are expecting more items for this collection.
   925                 lastc = self._collectionstack[-1]
   925                 lastc = self._collectionstack[-1]
   926 
   926 
   927                 if lastc['remaining']:
   927                 if lastc[b'remaining']:
   928                     break
   928                     break
   929 
   929 
   930                 # The collection at the top of the stack is complete.
   930                 # The collection at the top of the stack is complete.
   931 
   931 
   932                 # Discard it, as it isn't needed for future items.
   932                 # Discard it, as it isn't needed for future items.
   939                 if self._collectionstack:
   939                 if self._collectionstack:
   940                     self._state = {
   940                     self._state = {
   941                         list: self._STATE_WANT_ARRAY_VALUE,
   941                         list: self._STATE_WANT_ARRAY_VALUE,
   942                         dict: self._STATE_WANT_MAP_KEY,
   942                         dict: self._STATE_WANT_MAP_KEY,
   943                         set: self._STATE_WANT_SET_VALUE,
   943                         set: self._STATE_WANT_SET_VALUE,
   944                     }[type(self._collectionstack[-1]['v'])]
   944                     }[type(self._collectionstack[-1][b'v'])]
   945 
   945 
   946                 # If this is the root collection, emit it.
   946                 # If this is the root collection, emit it.
   947                 else:
   947                 else:
   948                     self._decodedvalues.append(lastc['v'])
   948                     self._decodedvalues.append(lastc[b'v'])
   949                     self._state = self._STATE_NONE
   949                     self._state = self._STATE_NONE
   950 
   950 
   951         return (
   951         return (
   952             bool(self._decodedvalues),
   952             bool(self._decodedvalues),
   953             offset - initialoffset,
   953             offset - initialoffset,
  1051     decoder = sansiodecoder()
  1051     decoder = sansiodecoder()
  1052 
  1052 
  1053     havevalues, readcount, wantbytes = decoder.decode(b)
  1053     havevalues, readcount, wantbytes = decoder.decode(b)
  1054 
  1054 
  1055     if readcount != len(b):
  1055     if readcount != len(b):
  1056         raise CBORDecodeError('input data not fully consumed')
  1056         raise CBORDecodeError(b'input data not fully consumed')
  1057 
  1057 
  1058     if decoder.inprogress:
  1058     if decoder.inprogress:
  1059         raise CBORDecodeError('input data not complete')
  1059         raise CBORDecodeError(b'input data not complete')
  1060 
  1060 
  1061     return decoder.getavailable()
  1061     return decoder.getavailable()