Merge pull request #3 from skade/utf8-password-buffer

Use a byte buffer for the password
This commit is contained in:
Ticki 2016-03-08 11:55:51 +01:00
commit e145bf6592
2 changed files with 9 additions and 4 deletions

View File

@ -16,6 +16,8 @@ pub enum TerminalError {
StdinError,
/// Failed to parse number.
ParseError,
/// Failed to read unicode encoded data.
UnicodeError
}
impl TerminalError {
@ -27,6 +29,7 @@ impl TerminalError {
TerminalError::StdoutError => "Failed to write to stdout.",
TerminalError::StdinError => "Failed to read from stdin.",
TerminalError::ParseError => "Failed to parse number.",
TerminalError::UnicodeError => "Failed to read unicode encoded data.",
}
}
}

View File

@ -13,17 +13,19 @@ pub trait ReadExt {
impl<R: Read> ReadExt for R {
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError> {
let _raw = try!(writer.into_raw_mode());
let mut string = String::with_capacity(30);
let mut passbuf = Vec::with_capacity(30);
for c in self.bytes() {
match c {
Err(_) => return Err(TerminalError::StdinError),
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
Ok(b'\n') | Ok(b'\r') => return Ok(Some(string)),
Ok(c) => string.push(c as char),
Ok(b'\n') | Ok(b'\r') => break,
Ok(c) => passbuf.push(c),
}
}
Ok(Some(string))
let passwd = try!(String::from_utf8(passbuf).map_err(|_| TerminalError::UnicodeError ));
Ok(Some(passwd))
}
}