Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pem-rfc7468/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
ENCAPSULATION_BOUNDARY_DELIMITER, POST_ENCAPSULATION_BOUNDARY, PRE_ENCAPSULATION_BOUNDARY,
};
use base64ct::{Base64, Encoding};
use core::str;

#[cfg(feature = "alloc")]
use alloc::string::String;
Expand All @@ -18,11 +19,11 @@ pub fn encode<'o>(
line_ending: LineEnding,
input: &[u8],
buf: &'o mut [u8],
) -> Result<&'o [u8]> {
) -> Result<&'o str> {
let mut encoder = Encoder::new(type_label, line_ending, buf)?;
encoder.encode(input)?;
let encoded_len = encoder.finish()?;
Ok(&buf[..encoded_len])
Ok(str::from_utf8(&buf[..encoded_len])?)
}

/// Get the length of a PEM encoded document with the given bytes and label.
Expand All @@ -42,8 +43,10 @@ pub fn encoded_len(label: &str, line_ending: LineEnding, input: &[u8]) -> usize
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn encode_string(label: &str, line_ending: LineEnding, input: &[u8]) -> Result<String> {
let mut buf = vec![0u8; encoded_len(label, line_ending, input)];
encode(label, line_ending, input, &mut buf)?;
let expected_len = encoded_len(label, line_ending, input);
let mut buf = vec![0u8; expected_len];
let actual_len = encode(label, line_ending, input, &mut buf)?.len();
debug_assert_eq!(expected_len, actual_len);
String::from_utf8(buf).map_err(|_| Error::CharacterEncoding)
}

Expand Down
6 changes: 6 additions & 0 deletions pem-rfc7468/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl From<base64ct::InvalidLengthError> for Error {
}
}

impl From<core::str::Utf8Error> for Error {
fn from(_: core::str::Utf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<Error> for std::io::Error {
Expand Down