Multiple types are allowed: ASN1_Packet, ASN1F_field and ASN1F_PACKET(), See layers/x509.py for examples. Other ASN1F_field instances than ASN1F_PACKET instances must not be used.
| 702 | |
| 703 | |
| 704 | class ASN1F_CHOICE(ASN1F_field[_CHOICE_T, ASN1_Object[Any]]): |
| 705 | """ |
| 706 | Multiple types are allowed: ASN1_Packet, ASN1F_field and ASN1F_PACKET(), |
| 707 | See layers/x509.py for examples. |
| 708 | Other ASN1F_field instances than ASN1F_PACKET instances must not be used. |
| 709 | """ |
| 710 | holds_packets = 1 |
| 711 | ASN1_tag = ASN1_Class_UNIVERSAL.ANY |
| 712 | |
| 713 | def __init__(self, name, default, *args, **kwargs): |
| 714 | # type: (str, Any, *_CHOICE_T, **Any) -> None |
| 715 | if "implicit_tag" in kwargs: |
| 716 | err_msg = "ASN1F_CHOICE has been called with an implicit_tag" |
| 717 | raise ASN1_Error(err_msg) |
| 718 | self.implicit_tag = None |
| 719 | for kwarg in ["context", "explicit_tag"]: |
| 720 | setattr(self, kwarg, kwargs.get(kwarg)) |
| 721 | super(ASN1F_CHOICE, self).__init__( |
| 722 | name, None, context=self.context, |
| 723 | explicit_tag=self.explicit_tag |
| 724 | ) |
| 725 | self.default = default |
| 726 | self.current_choice = None |
| 727 | self.choices = {} # type: Dict[int, _CHOICE_T] |
| 728 | self.pktchoices = {} |
| 729 | for p in args: |
| 730 | if hasattr(p, "ASN1_root"): |
| 731 | p = cast('ASN1_Packet', p) |
| 732 | # should be ASN1_Packet |
| 733 | if hasattr(p.ASN1_root, "choices"): |
| 734 | root = cast(ASN1F_CHOICE, p.ASN1_root) |
| 735 | for k, v in root.choices.items(): |
| 736 | # ASN1F_CHOICE recursion |
| 737 | self.choices[k] = v |
| 738 | else: |
| 739 | self.choices[p.ASN1_root.network_tag] = p |
| 740 | elif hasattr(p, "ASN1_tag"): |
| 741 | if isinstance(p, type): |
| 742 | # should be ASN1F_field class |
| 743 | self.choices[int(p.ASN1_tag)] = p |
| 744 | else: |
| 745 | # should be ASN1F_field instance |
| 746 | self.choices[p.network_tag] = p |
| 747 | self.pktchoices[hash(p.cls)] = (p.implicit_tag, p.explicit_tag) # noqa: E501 |
| 748 | else: |
| 749 | raise ASN1_Error("ASN1F_CHOICE: no tag found for one field") |
| 750 | |
| 751 | def m2i(self, pkt, s): |
| 752 | # type: (ASN1_Packet, bytes) -> Tuple[ASN1_Object[Any], bytes] |
| 753 | """ |
| 754 | First we have to retrieve the appropriate choice. |
| 755 | Then we extract the field/packet, according to this choice. |
| 756 | """ |
| 757 | if len(s) == 0: |
| 758 | raise ASN1_Error("ASN1F_CHOICE: got empty string") |
| 759 | _, s = BER_tagging_dec(s, hidden_tag=self.ASN1_tag, |
| 760 | explicit_tag=self.explicit_tag) |
| 761 | tag, _ = BER_id_dec(s) |
no outgoing calls
no test coverage detected