@@ -20,37 +20,6 @@ use crate::{
2020use base64ct:: { Base64 , Encoding } ;
2121use core:: { convert:: TryFrom , str} ;
2222
23- fn decode_encapsulated_text < ' i , ' o > (
24- encapsulation : & Encapsulation < ' i > ,
25- buf : & ' o mut [ u8 ] ,
26- out_len : & mut usize ,
27- ) -> Result < ( ) > {
28- for line in encapsulation. encapsulated_text ( ) {
29- let line = line?;
30-
31- match Base64 :: decode ( line, & mut buf[ * out_len..] ) {
32- Err ( error) => {
33- // in the case that we are decoding the first line
34- // and we error, then attribute the error to an unsupported header
35- // if a colon char is present in the line
36- if * out_len == 0 && line. iter ( ) . any ( |& b| b == grammar:: CHAR_COLON ) {
37- return Err ( Error :: HeaderDisallowed ) ;
38- } else {
39- return Err ( error. into ( ) ) ;
40- }
41- }
42- Ok ( out) => * out_len += out. len ( ) ,
43- }
44- }
45- Ok ( ( ) )
46- }
47- /// Decode the encapsulation boundaries of a PEM document according to RFC 7468's "Strict" grammar.
48- ///
49- /// On success, returning the decoded label.
50- pub fn decode_label ( pem : & [ u8 ] ) -> Result < & str > {
51- Ok ( Encapsulation :: try_from ( pem) ?. label ( ) )
52- }
53-
5423/// Decode a PEM document according to RFC 7468's "Strict" grammar.
5524///
5625/// On success, writes the decoded document into the provided buffer, returning
@@ -60,9 +29,7 @@ pub fn decode<'i, 'o>(pem: &'i [u8], buf: &'o mut [u8]) -> Result<(&'i str, &'o
6029 let encapsulation = Encapsulation :: try_from ( pem) ?;
6130 let label = encapsulation. label ( ) ;
6231 let mut out_len = 0 ;
63-
6432 decode_encapsulated_text ( & encapsulation, buf, & mut out_len) ?;
65-
6633 Ok ( ( label, & buf[ ..out_len] ) )
6734}
6835
@@ -86,6 +53,38 @@ pub fn decode_vec(pem: &[u8]) -> Result<(&str, Vec<u8>)> {
8653 Ok ( ( label, result) )
8754}
8855
56+ /// Decode the encapsulation boundaries of a PEM document according to RFC 7468's "Strict" grammar.
57+ ///
58+ /// On success, returning the decoded label.
59+ pub fn decode_label ( pem : & [ u8 ] ) -> Result < & str > {
60+ Ok ( Encapsulation :: try_from ( pem) ?. label ( ) )
61+ }
62+
63+ fn decode_encapsulated_text < ' i , ' o > (
64+ encapsulation : & Encapsulation < ' i > ,
65+ buf : & ' o mut [ u8 ] ,
66+ out_len : & mut usize ,
67+ ) -> Result < ( ) > {
68+ for line in encapsulation. encapsulated_text ( ) {
69+ let line = line?;
70+
71+ match Base64 :: decode ( line, & mut buf[ * out_len..] ) {
72+ Err ( error) => {
73+ // in the case that we are decoding the first line
74+ // and we error, then attribute the error to an unsupported header
75+ // if a colon char is present in the line
76+ if * out_len == 0 && line. iter ( ) . any ( |& b| b == grammar:: CHAR_COLON ) {
77+ return Err ( Error :: HeaderDisallowed ) ;
78+ } else {
79+ return Err ( error. into ( ) ) ;
80+ }
81+ }
82+ Ok ( out) => * out_len += out. len ( ) ,
83+ }
84+ }
85+ Ok ( ( ) )
86+ }
87+
8988/// PEM encapsulation parser.
9089///
9190/// This parser performs an initial pass over the data, locating the
0 commit comments