33mod config;
44pub ( super ) mod parser;
55
6- use super :: identifier:: Identifier ;
6+ use super :: { identifier:: Identifier , ExtensionGroupState } ;
77use crate :: {
88 types:: {
99 self ,
@@ -29,6 +29,7 @@ pub struct Decoder<'input> {
2929 input : & ' input [ u8 ] ,
3030 config : DecoderOptions ,
3131 initial_len : usize ,
32+ extension_group : ExtensionGroupState ,
3233}
3334
3435impl < ' input > Decoder < ' input > {
@@ -49,9 +50,14 @@ impl<'input> Decoder<'input> {
4950 input,
5051 config,
5152 initial_len : input. len ( ) ,
53+ extension_group : ExtensionGroupState :: None ,
5254 }
5355 }
5456
57+ fn translate_tag ( & self , tag : Tag ) -> Tag {
58+ tag. with_context_offset ( self . extension_group . base_tag ( ) )
59+ }
60+
5561 /// Return a number of the decoded bytes by this decoder
5662 #[ must_use]
5763 pub fn decoded_len ( & self ) -> usize {
@@ -85,6 +91,9 @@ impl<'input> Decoder<'input> {
8591 {
8692 return Ok ( None ) ;
8793 }
94+
95+ let tag = self . translate_tag ( tag) ;
96+
8897 if tag != Tag :: EOC {
8998 let upcoming_tag = self . peek_tag ( ) ?;
9099 if tag != upcoming_tag {
@@ -102,13 +111,15 @@ impl<'input> Decoder<'input> {
102111 }
103112
104113 pub ( crate ) fn parse_value ( & mut self , tag : Tag ) -> Result < ( Identifier , Option < & ' input [ u8 ] > ) > {
114+ let tag = self . translate_tag ( tag) ;
105115 let ( input, ( identifier, contents) ) =
106116 self :: parser:: parse_value ( self . config , self . input , Some ( tag) ) ?;
107117 self . input = input;
108118 Ok ( ( identifier, contents) )
109119 }
110120
111121 pub ( crate ) fn parse_primitive_value ( & mut self , tag : Tag ) -> Result < ( Identifier , & ' input [ u8 ] ) > {
122+ let tag = self . translate_tag ( tag) ;
112123 let ( input, ( identifier, contents) ) =
113124 self :: parser:: parse_value ( self . config , self . input , Some ( tag) ) ?;
114125 self . input = input;
@@ -762,6 +773,28 @@ impl<'input> crate::Decoder for Decoder<'input> {
762773 default_initializer_fn : Option < DF > ,
763774 decode_fn : F ,
764775 ) -> Result < D > {
776+ if tag == Tag :: SEQUENCE && matches ! ( self . extension_group, ExtensionGroupState :: Pending ( _) ) {
777+ // Extension addition groups are encoded flattened: skip the SEQUENCE wrapper once.
778+ if let ExtensionGroupState :: Pending ( tag) = self . extension_group {
779+ self . extension_group = ExtensionGroupState :: Active ( tag) ;
780+ }
781+ return if D :: FIELDS . is_empty ( ) && D :: EXTENDED_FIELDS . is_none ( )
782+ || ( D :: FIELDS . len ( ) == D :: FIELDS . number_of_optional_and_default_fields ( )
783+ && self . input . is_empty ( ) )
784+ {
785+ if let Some ( default_initializer_fn) = default_initializer_fn {
786+ Ok ( ( default_initializer_fn) ( ) )
787+ } else {
788+ Err ( DecodeError :: from_kind (
789+ DecodeErrorKind :: UnexpectedEmptyInput ,
790+ self . codec ( ) ,
791+ ) )
792+ }
793+ } else {
794+ ( decode_fn) ( self )
795+ } ;
796+ }
797+
765798 self . parse_constructed_contents ( tag, true , |decoder| {
766799 // If there are no fields, or the input is empty and we know that
767800 // all fields are optional or default fields, we call the default
@@ -806,7 +839,7 @@ impl<'input> crate::Decoder for Decoder<'input> {
806839 D : Fn ( & mut Self , usize , Tag ) -> Result < FIELDS , Self :: Error > ,
807840 F : FnOnce ( Vec < FIELDS > ) -> Result < SET , Self :: Error > ,
808841 {
809- self . parse_constructed_contents ( tag , true , |decoder| {
842+ let collect_fields = |decoder : & mut Self | -> Result < Vec < FIELDS > , Self :: Error > {
810843 let mut fields = Vec :: new ( ) ;
811844
812845 loop {
@@ -823,6 +856,20 @@ impl<'input> crate::Decoder for Decoder<'input> {
823856 }
824857 }
825858
859+ Ok ( fields)
860+ } ;
861+
862+ if tag == Tag :: SET && matches ! ( self . extension_group, ExtensionGroupState :: Pending ( _) ) {
863+ // Extension addition groups are encoded flattened: skip the SET wrapper once.
864+ if let ExtensionGroupState :: Pending ( tag) = self . extension_group {
865+ self . extension_group = ExtensionGroupState :: Active ( tag) ;
866+ }
867+ let fields = collect_fields ( self ) ?;
868+ return ( field_fn) ( fields) ;
869+ }
870+
871+ self . parse_constructed_contents ( tag, true , |decoder| {
872+ let fields = collect_fields ( decoder) ?;
826873 ( field_fn) ( fields)
827874 } )
828875 }
@@ -897,9 +944,26 @@ impl<'input> crate::Decoder for Decoder<'input> {
897944 D : Decode + crate :: types:: Constructed < RL , EL > ,
898945 > (
899946 & mut self ,
900- _tag : Tag ,
947+ tag : Tag ,
901948 ) -> Result < Option < D > , Self :: Error > {
902- <Option < D > >:: decode ( self )
949+ if self . input . is_empty ( ) {
950+ return Ok ( None ) ;
951+ }
952+
953+ let ( _, identifier) = parser:: parse_identifier_octet ( self . input ) . map_err ( |e| match e {
954+ ParseNumberError :: Nom ( e) => DecodeError :: map_nom_err ( e, self . codec ( ) ) ,
955+ ParseNumberError :: Overflow => DecodeError :: integer_overflow ( 32u32 , self . codec ( ) ) ,
956+ } ) ?;
957+
958+ if identifier. tag == tag {
959+ let previous = self . extension_group ;
960+ self . extension_group = ExtensionGroupState :: Pending ( tag) ;
961+ let result = D :: decode ( self ) . map ( Some ) ;
962+ self . extension_group = previous;
963+ result
964+ } else {
965+ Ok ( None )
966+ }
903967 }
904968}
905969
0 commit comments