@@ -6,8 +6,7 @@ use napi::{
66} ;
77use num_bigint:: BigInt ;
88use rasn:: {
9- ber:: { de:: Error as BERError , decode, encode} ,
10- de:: Needed ,
9+ ber:: { decode, encode} ,
1110 types:: { Any , BitString , Class , OctetString , PrintableString } ,
1211 Decode , Tag ,
1312} ;
@@ -18,7 +17,7 @@ use crate::{
1817 ASN1BitString , ASN1Context , ASN1ContextTag , ASN1Object , ASN1RawBitString , ASN1Set , ASN1OID ,
1918 } ,
2019 types:: { ASN1Data , JsType } ,
21- utils:: { get_utc_date_time_from_asn1_milli, get_vec_from_js_unknown, repair_bit_string_data } ,
20+ utils:: { get_utc_date_time_from_asn1_milli, get_vec_from_js_unknown} ,
2221 ASN1NAPIError ,
2322} ;
2423
@@ -76,18 +75,10 @@ impl ASN1Encoder {
7675 }
7776
7877 /// Encode ASN1Data to a Vec<u8> of ASN.1 encoded data.
79- /// TODO Mising bits that the rasn library truncates.
8078 pub ( crate ) fn encode ( & self ) -> Result < Vec < u8 > > {
81- if let Ok ( mut data) = encode ( & self . 0 ) {
82- match & self . 0 {
83- ASN1Data :: Object ( ASN1Object :: BitString ( val) ) => {
84- repair_bit_string_data ( & mut data, val. clone ( ) . into_vec ( ) , true ) ;
85- Ok ( data)
86- }
87- _ => Ok ( data) ,
88- }
89- } else {
90- bail ! ( ASN1NAPIError :: MalformedData )
79+ match encode ( & self . 0 ) {
80+ Ok ( data) => Ok ( data) ,
81+ Err ( _) => bail ! ( ASN1NAPIError :: InvalidDataEncoding ) ,
9182 }
9283 }
9384
@@ -162,58 +153,32 @@ impl ASN1Decoder {
162153 /// Create an instance of ANS1 from Base64 encoded data.
163154 #[ napi]
164155 pub fn from_base64 ( value : String ) -> Result < ASN1Decoder > {
165- if let Ok ( result) = base64:: decode ( value) {
166- Self :: try_from ( result. as_slice ( ) )
167- } else {
168- bail ! ( ASN1NAPIError :: UnknownStringFormat )
156+ match base64:: decode ( value) {
157+ Ok ( result) => Self :: try_from ( result. as_slice ( ) ) ,
158+ Err ( _) => bail ! ( ASN1NAPIError :: UnknownStringFormat ) ,
169159 }
170160 }
171161
172162 /// Create an instance of ANS1 from hex encoded data.
173163 #[ napi]
174164 pub fn from_hex ( value : String ) -> Result < ASN1Decoder > {
175- if let Ok ( result) = hex:: decode ( value) {
176- Self :: try_from ( result. as_slice ( ) )
177- } else {
178- bail ! ( ASN1NAPIError :: UnknownStringFormat )
165+ match hex:: decode ( value) {
166+ Ok ( result) => Self :: try_from ( result. as_slice ( ) ) ,
167+ Err ( _) => bail ! ( ASN1NAPIError :: UnknownStringFormat ) ,
179168 }
180169 }
181170
182171 /// Decode ASN1 encoded data.
183- /// TODO Mising bits that the rasn library truncates.
184172 pub ( crate ) fn decode < T : Decode > ( & self ) -> Result < T > {
185173 match decode ( & self . data ) {
186174 Ok ( data) => Ok ( data) ,
187- Err ( err) => match err {
188- BERError :: Incomplete {
189- needed : Needed :: Size ( val) ,
190- } => {
191- let data = [ ..val] . iter ( ) . fold ( self . data . clone ( ) , |mut data, _| {
192- data. push ( 0x00 ) ;
193- data
194- } ) ;
195- if let Ok ( result) = decode ( & data) {
196- Ok ( result)
197- } else {
198- bail ! ( ASN1NAPIError :: InvalidBitString )
199- }
200- }
201- _ => bail ! ( ASN1NAPIError :: MalformedData ) ,
202- } ,
175+ Err ( _) => bail ! ( ASN1NAPIError :: MalformedData ) ,
203176 }
204177 }
205178
206179 /// Get a ASN1BitString object.
207180 pub ( crate ) fn get_raw_bit_string ( & self ) -> Result < ASN1RawBitString > {
208- if let Ok ( result) = self . decode :: < ASN1RawBitString > ( ) {
209- let mut data = result. into_vec ( ) ;
210-
211- repair_bit_string_data ( & mut data, self . data . clone ( ) , false ) ;
212-
213- Ok ( ASN1RawBitString :: new ( BitString :: from_vec ( data) ) )
214- } else {
215- bail ! ( ASN1NAPIError :: InvalidBitString )
216- }
181+ self . decode :: < ASN1RawBitString > ( )
217182 }
218183
219184 /// Get a Context object.
@@ -227,12 +192,8 @@ impl ASN1Decoder {
227192 }
228193
229194 /// Decode an object to an ASN1Object.
230- /// TODO Mising bits that the rasn library truncates.
231195 pub ( crate ) fn into_object ( self ) -> Result < ASN1Object > {
232- match self . get_tag ( ) {
233- & Tag :: BIT_STRING => Ok ( ASN1Object :: BitString ( self . get_raw_bit_string ( ) ?) ) ,
234- _ => self . decode :: < ASN1Object > ( ) ,
235- }
196+ self . decode :: < ASN1Object > ( )
236197 }
237198
238199 /// Convert to a big integer.
0 commit comments