From 597d05b8f8ceb754cbfe5c3b6789be6786fb0149 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 6 Nov 2016 23:46:42 -0800 Subject: Add stack creation and push --- openssl/src/stack.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'openssl/src/stack.rs') diff --git a/openssl/src/stack.rs b/openssl/src/stack.rs index dae42ca8..87aa86c5 100644 --- a/openssl/src/stack.rs +++ b/openssl/src/stack.rs @@ -6,14 +6,18 @@ use std::marker::PhantomData; use libc::c_int; use std::mem; +use {cvt, cvt_p}; +use error::ErrorStack; use types::{OpenSslType, OpenSslTypeRef}; use util::Opaque; #[cfg(ossl10x)] use ffi::{sk_pop as OPENSSL_sk_pop, sk_free as OPENSSL_sk_free, sk_num as OPENSSL_sk_num, - sk_value as OPENSSL_sk_value, _STACK as OPENSSL_STACK}; + sk_value as OPENSSL_sk_value, _STACK as OPENSSL_STACK, + sk_new_null as OPENSSL_sk_new_null, sk_push as OPENSSL_sk_push}; #[cfg(ossl110)] -use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free, OPENSSL_sk_num, OPENSSL_sk_value, OPENSSL_STACK}; +use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free, OPENSSL_sk_num, OPENSSL_sk_value, OPENSSL_STACK, + OPENSSL_sk_new_null, OPENSSL_sk_push}; /// Trait implemented by types which can be placed in a stack. /// @@ -31,9 +35,11 @@ pub trait Stackable: OpenSslType { pub struct Stack(*mut T::StackType); impl Stack { - /// Return a new Stack, taking ownership of the handle - pub unsafe fn from_ptr(stack: *mut T::StackType) -> Stack { - Stack(stack) + pub fn new() -> Result, ErrorStack> { + unsafe { + let ptr = try!(cvt_p(OPENSSL_sk_new_null())); + Ok(Stack(ptr as *mut _)) + } } } @@ -79,6 +85,10 @@ impl OpenSslType for Stack { unsafe fn from_ptr(ptr: *mut T::StackType) -> Stack { Stack(ptr) } + + fn as_ptr(&self) -> *mut T::StackType { + self.0 + } } impl Deref for Stack { @@ -198,6 +208,15 @@ impl StackRef { } } + /// Pushes a value onto the top of the stack. + pub fn push(&mut self, data: T) -> Result<(), ErrorStack> { + unsafe { + try!(cvt(OPENSSL_sk_push(self.as_stack(), data.as_ptr() as *mut _))); + mem::forget(data); + Ok(()) + } + } + /// Removes the last element from the stack and returns it. pub fn pop(&mut self) -> Option { unsafe { -- cgit v1.2.3