Commit 6991cc6a authored by Steven Fackler's avatar Steven Fackler
Browse files

Convert to new IO.

parent 1b4a2eef
Loading
Loading
Loading
Loading
+28 −30
Original line number Diff line number Diff line
use libc::{c_void, c_int};
use std::old_io::{EndOfFile, IoResult, IoError, OtherIoError};
use std::old_io::{Reader, Writer};
use std::io;
use std::io::prelude::*;
use std::ptr;
use std::cmp;
use std::num::Int;

use ffi;
use ssl::error::{SslError};
@@ -57,49 +59,45 @@ impl MemBio {
    }
}

impl Reader for MemBio {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
impl Read for MemBio {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let len = cmp::min(<c_int as Int>::max_value() as usize, buf.len()) as c_int;
        let ret = unsafe {
            ffi::BIO_read(self.bio, buf.as_ptr() as *mut c_void,
                          buf.len() as c_int)
            ffi::BIO_read(self.bio, buf.as_ptr() as *mut c_void, len)
        };

        if ret <= 0 {
            let is_eof = unsafe { ffi::BIO_eof(self.bio) };
            let err = if is_eof {
                IoError {
                    kind: EndOfFile,
                    desc: "MemBio EOF",
                    detail: None
                }
            if is_eof {
                Ok(0)
            } else {
                IoError {
                    kind: OtherIoError,
                    desc: "MemBio read error",
                    detail: Some(format!("{:?}", SslError::get()))
                Err(io::Error::new(io::ErrorKind::Other,
                                   "MemBio read error",
                                   Some(format!("{:?}", SslError::get()))))
            }
            };
            Err(err)
        } else {
            Ok(ret as usize)
        }
    }
}

impl Writer for MemBio {
    fn write_all(&mut self, buf: &[u8]) -> IoResult<()> {
impl Write for MemBio {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let len = cmp::min(<c_int as Int>::max_value() as usize, buf.len()) as c_int;
        let ret = unsafe {
            ffi::BIO_write(self.bio, buf.as_ptr() as *const c_void,
                           buf.len() as c_int)
            ffi::BIO_write(self.bio, buf.as_ptr() as *const c_void, len)
        };
        if buf.len() != ret as usize {
            Err(IoError {
                kind: OtherIoError,
                desc: "MemBio write error",
                detail: Some(format!("{:?}", SslError::get()))
            })

        if ret < 0 {
                Err(io::Error::new(io::ErrorKind::Other,
                                   "MemBio write error",
                                   Some(format!("{:?}", SslError::get()))))
        } else {
            Ok(())
            Ok(ret as usize)
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}
+11 −6
Original line number Diff line number Diff line
use libc::c_uint;
use std::iter::repeat;
use std::old_io::{IoError, Writer};
use std::io::prelude::*;
use std::io;

use ffi;

@@ -73,10 +74,10 @@ use self::State::*;
/// assert_eq!(res, spec);
/// ```
///
/// Use the `Writer` trait to supply the input in chunks.
/// Use the `Write` trait to supply the input in chunks.
///
/// ```
/// use std::old_io::Writer;
/// use std::io::prelude::*;
/// use openssl::crypto::hash::{Hasher, Type};
/// let data = [b"\x42\xF4", b"\x97\xE0"];
/// let spec = b"\x7c\x43\x0f\x17\x8a\xef\xdf\x14\x87\xfe\xe7\x14\x4e\x96\x41\xe2";
@@ -168,10 +169,14 @@ impl Hasher {
    }
}

impl Writer for Hasher {
impl Write for Hasher {
    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> Result<(), IoError> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.update(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}
@@ -213,7 +218,7 @@ pub fn hash(t: Type, data: &[u8]) -> Vec<u8> {
mod tests {
    use serialize::hex::{FromHex, ToHex};
    use super::{hash, Hasher, Type};
    use std::old_io::Writer;
    use std::io::prelude::*;

    fn hash_test(hashtype: Type, hashtest: &(&str, &str)) {
        let res = hash(hashtype, &*hashtest.0.from_hex().unwrap());
+11 −6
Original line number Diff line number Diff line
@@ -16,7 +16,8 @@

use libc::{c_int, c_uint};
use std::iter::repeat;
use std::old_io::{IoError, Writer};
use std::io;
use std::io::prelude::*;

use crypto::hash::Type;
use ffi;
@@ -46,10 +47,10 @@ use self::State::*;
/// assert_eq!(spec, res);
/// ```
///
/// Use the `Writer` trait to supply the input in chunks.
/// Use the `Write` trait to supply the input in chunks.
///
/// ```
/// use std::old_io::Writer;
/// use std::io::prelude::*;
/// use openssl::crypto::hash::Type;
/// use openssl::crypto::hmac::HMAC;
/// let key = b"Jefe";
@@ -150,10 +151,14 @@ impl HMAC {
    }
}

impl Writer for HMAC {
impl Write for HMAC {
    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> Result<(), IoError> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.update(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}
@@ -197,7 +202,7 @@ mod tests {
    use crypto::hash::Type;
    use crypto::hash::Type::*;
    use super::{hmac, HMAC};
    use std::old_io::Writer;
    use std::io::prelude::*;

    fn test_hmac(ty: Type, tests: &[(Vec<u8>, Vec<u8>, Vec<u8>)]) {
        for &(ref key, ref data, ref res) in tests.iter() {
+5 −3
Original line number Diff line number Diff line
use libc::{c_int, c_uint, c_ulong};
use std::io::prelude::*;
use std::iter::repeat;
use std::mem;
use std::ptr;
@@ -142,15 +143,16 @@ impl PKey {

    /// Stores private key as a PEM
    // FIXME: also add password and encryption
    pub fn write_pem(&self, writer: &mut Writer/*, password: Option<String>*/) -> Result<(), SslError> {
    pub fn write_pem<W: Write>(&self, writer: &mut W/*, password: Option<String>*/) -> Result<(), SslError> {
        let mut mem_bio = try!(MemBio::new());
        unsafe {
            try_ssl!(ffi::PEM_write_bio_PrivateKey(mem_bio.get_handle(), self.evp, ptr::null(),
                                                   ptr::null_mut(), -1, None, ptr::null_mut()));

        }
        let buf = try!(mem_bio.read_to_end().map_err(StreamError));
        writer.write_all(buf.as_slice()).map_err(StreamError)
        let mut buf = vec![];
        try!(mem_bio.read_to_end(&mut buf).map_err(StreamError));
        writer.write_all(&buf).map_err(StreamError)
    }

    /**
+2 −1
Original line number Diff line number Diff line
#![feature(unsafe_destructor, core, old_io, std_misc, old_path)]
#![feature(unsafe_destructor, core, io, std_misc, path, os)]
#![cfg_attr(test, feature(net, fs))]
#![doc(html_root_url="https://sfackler.github.io/rust-openssl/doc/openssl")]

extern crate libc;
Loading