Rewrite metadata::get_item without unsafe

This commit is contained in:
Jack Grigg
2019-11-28 17:55:51 +00:00
parent bd5669d9ef
commit 8b86a0f578
+12 -23
View File
@@ -31,48 +31,37 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use crate::{consts::*, error::Error, serialization::*, transaction::Transaction, Buffer}; use crate::{consts::*, error::Error, serialization::*, transaction::Transaction, Buffer};
use std::{ptr, slice}; use std::ptr;
use zeroize::Zeroizing; use zeroize::Zeroizing;
/// Get metadata item /// Get metadata item
pub(crate) fn get_item(data: &[u8], tag: u8) -> Result<&[u8], Error> { pub(crate) fn get_item(data: &[u8], tag: u8) -> Result<&[u8], Error> {
let mut p_temp: *const u8 = data.as_ptr();
let mut cb_temp: usize = 0; let mut cb_temp: usize = 0;
let mut tag_temp: u8; let mut offset = 0;
unsafe { while offset < data.len() {
while p_temp < data.as_ptr().add(data.len()) { let tag_temp = data[offset];
tag_temp = *p_temp; offset += 1;
p_temp = p_temp.add(1);
let p_slice = slice::from_raw_parts( if !has_valid_length(&data[offset..], data.len() - 1) {
p_temp,
data.as_ptr() as usize + data.len() - p_temp as usize,
);
if !has_valid_length(
p_slice,
data.as_ptr().add(data.len()) as usize - p_temp as usize,
) {
return Err(Error::SizeError); return Err(Error::SizeError);
} }
p_temp = p_temp.add(get_length(p_slice, &mut cb_temp)); offset += get_length(&data[offset..], &mut cb_temp);
if tag_temp == tag { if tag_temp == tag {
// found tag // found tag
break; break;
} }
p_temp = p_temp.add(cb_temp); offset += cb_temp;
}
if p_temp < data.as_ptr().add(data.len()) {
return Ok(slice::from_raw_parts(p_temp, cb_temp));
}
} }
if offset < data.len() {
Ok(&data[offset..offset + cb_temp])
} else {
Err(Error::GenericError) Err(Error::GenericError)
}
} }
/// Set metadata item /// Set metadata item