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: |