Skip to content

Commit d9f1404

Browse files
committed
pem: hold on to buffers while reading file
1 parent 368e753 commit d9f1404

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/pem.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ impl<T: PemObjectFilter + From<Vec<u8>>> PemObject for T {
9797
pub struct ReadIter<R, T> {
9898
rd: R,
9999
_ty: PhantomData<T>,
100+
line: Vec<u8>,
101+
b64_buf: Vec<u8>,
100102
}
101103

102104
#[cfg(feature = "std")]
@@ -106,6 +108,8 @@ impl<R: io::BufRead, T: PemObject> ReadIter<R, T> {
106108
Self {
107109
rd,
108110
_ty: PhantomData,
111+
line: Vec::with_capacity(80),
112+
b64_buf: Vec::with_capacity(1024),
109113
}
110114
}
111115
}
@@ -116,7 +120,8 @@ impl<R: io::BufRead, T: PemObject> Iterator for ReadIter<R, T> {
116120

117121
fn next(&mut self) -> Option<Self::Item> {
118122
loop {
119-
return match from_buf(&mut self.rd) {
123+
self.b64_buf.clear();
124+
return match from_buf_inner(&mut self.rd, &mut self.line, &mut self.b64_buf) {
120125
Ok(Some((sec, item))) => match T::from_pem(sec, item) {
121126
Some(res) => Some(Ok(res)),
122127
None => continue,
@@ -221,20 +226,28 @@ fn from_slice(mut input: &[u8]) -> Result<Option<((SectionKind, Vec<u8>), &[u8])
221226
#[cfg(feature = "std")]
222227
pub fn from_buf(rd: &mut dyn io::BufRead) -> Result<Option<(SectionKind, Vec<u8>)>, Error> {
223228
let mut b64buf = Vec::with_capacity(1024);
224-
let mut section = None;
225229
let mut line = Vec::with_capacity(80);
230+
from_buf_inner(rd, &mut line, &mut b64buf)
231+
}
226232

233+
#[cfg(feature = "std")]
234+
fn from_buf_inner(
235+
rd: &mut dyn io::BufRead,
236+
line: &mut Vec<u8>,
237+
b64buf: &mut Vec<u8>,
238+
) -> Result<Option<(SectionKind, Vec<u8>)>, Error> {
239+
let mut section = None;
227240
loop {
228241
line.clear();
229-
let len = read_until_newline(rd, &mut line).map_err(Error::Io)?;
242+
let len = read_until_newline(rd, line).map_err(Error::Io)?;
230243

231244
let next_line = if len == 0 {
232245
None
233246
} else {
234247
Some(line.as_slice())
235248
};
236249

237-
match read(next_line, &mut section, &mut b64buf) {
250+
match read(next_line, &mut section, b64buf) {
238251
Ok(ControlFlow::Break(opt)) => return Ok(opt),
239252
Ok(ControlFlow::Continue(())) => continue,
240253
Err(e) => return Err(e),

0 commit comments

Comments
 (0)