To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

Commit 9db8ac97 authored by qinsoon's avatar qinsoon
Browse files

utils mod

parent b6a1ffc3
......@@ -594,13 +594,13 @@ impl BlockContent {
}
Instruction_::Branch1(ref dest) => {
let mut live_outs = dest.get_arguments(&ops);
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
Instruction_::Branch2{ref true_dest, ref false_dest, ..} => {
let mut live_outs = true_dest.get_arguments(&ops);
live_outs.append(&mut false_dest.get_arguments(&ops));
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
Instruction_::Watchpoint{ref disable_dest, ref resume, ..} => {
let mut live_outs = vec![];
......@@ -611,13 +611,13 @@ impl BlockContent {
live_outs.append(&mut resume.normal_dest.get_arguments(&ops));
live_outs.append(&mut resume.exn_dest.get_arguments(&ops));
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
Instruction_::WPBranch{ref disable_dest, ref enable_dest, ..} => {
let mut live_outs = vec![];
live_outs.append(&mut disable_dest.get_arguments(&ops));
live_outs.append(&mut enable_dest.get_arguments(&ops));
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
Instruction_::Call{ref resume, ..}
| Instruction_::CCall{ref resume, ..}
......@@ -626,7 +626,7 @@ impl BlockContent {
let mut live_outs = vec![];
live_outs.append(&mut resume.normal_dest.get_arguments(&ops));
live_outs.append(&mut resume.exn_dest.get_arguments(&ops));
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
Instruction_::Switch{ref default, ref branches, ..} => {
let mut live_outs = vec![];
......@@ -634,7 +634,7 @@ impl BlockContent {
for &(_, ref dest) in branches {
live_outs.append(&mut dest.get_arguments(&ops));
}
vec_utils::append_unique(&mut ret, &mut live_outs);
vec_utils::add_all_unique(&mut ret, &mut live_outs);
}
_ => panic!("didn't expect last inst as {}", inst)
......
......@@ -12,25 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// u8
/// returns true if the nth bit (count from least significant bit) is the same as val (as boolean)
/// otherwise returns false
#[inline(always)]
pub fn test_nth_bit_u8(value: u8, index: usize, val: u8) -> bool {
((value >> index) & 1) as u8 == val
}
/// returns the lower n bits
#[inline(always)]
pub fn lower_bits_u8(value: u8, len: usize) -> u8 {
value & ((1 << len) - 1)
}
// u64
/// sets the nth bit (count from least significant bit) as val (treat the val as boolean, either 1 or 0)
#[inline(always)]
pub fn set_nth_bit_u64 (value: u64, index: usize, set_value: u8) -> u64 {
value ^ (((-(set_value as i64) as u64) ^ value) & (1 << index))
}
/// returns true if the nth bit (count from least significant bit) is the same as val (as boolean)
/// otherwise returns false
#[inline(always)]
pub fn test_nth_bit_u64(value: u64, index: usize, val: u8) -> bool {
((value >> index) & 1) as u8 == val
......
......@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/// returns the exponent if the given number is power of two, otherwise return None
pub fn is_power_of_two(x: usize) -> Option<u8> {
use std::u8;
......@@ -29,10 +30,8 @@ pub fn is_power_of_two(x: usize) -> Option<u8> {
}
}
/// aligns up a number
/// (returns the nearest multiply of the align value that is larger than the given value)
pub fn align_up(x: usize, align: usize) -> usize {
if x % align == 0 {
x
} else {
(x / align + 1) * align
}
(x + align - 1) & !(align - 1)
}
\ No newline at end of file
......@@ -12,21 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/// cross-platform mmap crate
pub extern crate memmap;
/// secured memory operations: memset, memzero, etc.
pub extern crate memsec;
use Word;
#[allow(unused_imports)]
#[allow(unused_imports)] // import both endianness (we may not use big endian though)
use byteorder::{LittleEndian, BigEndian, ReadBytesExt, WriteBytesExt, ByteOrder};
/// returns bit representations for u64
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn u64_to_raw(val: u64) -> Word {
let mut ret = vec![];
ret.write_u64::<LittleEndian>(val).unwrap();
as_word(ret)
}
/// returns bit representations for f32
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn f32_to_raw(val: f32) -> Word {
let mut ret = vec![];
......@@ -34,6 +37,7 @@ pub fn f32_to_raw(val: f32) -> Word {
as_word(ret)
}
/// returns bit representations for f64
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn f64_to_raw(val: f64) -> Word {
let mut ret = vec![];
......@@ -41,6 +45,7 @@ pub fn f64_to_raw(val: f64) -> Word {
as_word(ret)
}
/// returns bit representations for Vec<u8>
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn as_word(mut u8_array: Vec<u8>) -> Word {
LittleEndian::read_uint(&mut u8_array, 8) as Word
......
......@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/// replaces bytes in the given string 's' with string 'replace', this replacement starts
/// at 'index' position of 's', and happens for 'replace_len'.
pub fn replace(s: &mut String, index: usize, replace: &String, replace_len: usize) {
let vec = unsafe {s.as_mut_vec()};
let vec_replace = replace.as_bytes();
......
......@@ -14,37 +14,7 @@
use std::fmt;
pub fn is_identical_to_str_ignore_order<T: Ord + fmt::Display + Clone, Q: Ord + fmt::Display + Clone> (vec: &Vec<T>, mut expect: Vec<Q>) -> bool {
let mut vec_copy = vec.to_vec();
vec_copy.sort();
expect.sort();
let a = as_str(&vec_copy);
let b = as_str(&expect);
a == b
}
pub fn is_identical_ignore_order<T: Ord + Clone> (vec: &Vec<T>, vec2: &Vec<T>) -> bool {
if vec.len() != vec2.len() {
return false;
}
let mut vec = vec.to_vec();
let mut vec2 = vec2.to_vec();
vec.sort();
vec2.sort();
for i in 0..vec.len() {
if vec[i] != vec2[i] {
return false;
}
}
return true;
}
/// returns a formatted String for a Vec<T> (T needs Display trait)
pub fn as_str<T: fmt::Display>(vec: &Vec<T>) -> String {
let mut ret = String::new();
for i in 0..vec.len() {
......@@ -56,38 +26,24 @@ pub fn as_str<T: fmt::Display>(vec: &Vec<T>) -> String {
ret
}
pub fn add_all<T: Copy + PartialEq> (vec: &mut Vec<T>, vec2: &Vec<T>) -> bool {
let mut is_changed = false;
for i in vec2.iter() {
if !vec.contains(i) {
vec.push(*i);
is_changed = true;
}
}
is_changed
}
/// adds a value to the vector if the vector does not contains a same value
pub fn add_unique<T: PartialEq> (vec: &mut Vec<T>, val: T) {
if !vec.contains(&val) {
vec.push(val);
}
}
pub fn append_unique<T: PartialEq> (vec: &mut Vec<T>, vec2: &mut Vec<T>) {
/// adds all values from 2nd vector to the first one if the vector does not contains a same value
/// This function will pop all elements from the 2nd vector.
pub fn add_all_unique<T: PartialEq> (vec: &mut Vec<T>, vec2: &mut Vec<T>) {
while !vec2.is_empty() {
let val = vec2.pop().unwrap();
add_unique(vec, val);
}
}
pub fn append_clone_unique<T: PartialEq + Clone> (vec: &mut Vec<T>, vec2: &Vec<T>) {
for ele in vec2 {
add_unique(vec, ele.clone());
}
}
/// returns the index of a given value in the vector
/// returns None if the vector does not contains the value
pub fn find_value<T: PartialEq> (vec: &Vec<T>, val: T) -> Option<usize> {
for i in 0..vec.len() {
if vec[i] == val {
......@@ -98,6 +54,8 @@ pub fn find_value<T: PartialEq> (vec: &Vec<T>, val: T) -> Option<usize> {
None
}
/// intersects 1st vector with the 2nd one
/// (retains elements that also appear in the 2nd vector, and deletes elements that do not)
pub fn intersect<T: PartialEq + Clone> (vec: &mut Vec<T>, vec2: &Vec<T>) -> bool {
let mut indices_to_delete = vec![];
......@@ -114,6 +72,7 @@ pub fn intersect<T: PartialEq + Clone> (vec: &mut Vec<T>, vec2: &Vec<T>) -> bool
indices_to_delete.len() != 0
}
/// removes a value from a vector (if there are several appearances, delete the first one)
pub fn remove_value<T: PartialEq> (vec: &mut Vec<T>, val: T) {
match find_value(vec, val) {
Some(index) => {vec.remove(index);},
......@@ -121,48 +80,7 @@ pub fn remove_value<T: PartialEq> (vec: &mut Vec<T>, val: T) {
}
}
pub fn remove_value_if_true<T, F> (vec: &mut Vec<T>, cond: F) -> Vec<T>
where F : Fn(&T) -> bool
{
let mut new_vec = vec![];
let mut removed = vec![];
while !vec.is_empty() {
let val = vec.pop().unwrap();
if cond(&val) {
// true - remove
removed.push(val);
} else {
new_vec.push(val);
}
}
vec.append(&mut new_vec);
removed
}
pub fn remove_value_if_false<T, F> (vec: &mut Vec<T>, cond: F) -> Vec<T>
where F : Fn(&T) -> bool
{
let mut new_vec = vec![];
let mut removed = vec![];
while !vec.is_empty() {
let val = vec.pop().unwrap();
if cond(&val) {
new_vec.push(val)
} else {
// false - remove
removed.push(val)
}
}
vec.append(&mut new_vec);
removed
}
/// maps each element in the vector with a map function, and returns the new vector
pub fn map<T, Q, F> (vec: &Vec<T>, map_func: F) -> Vec<Q>
where F : Fn(&T) -> Q {
let mut ret = vec![];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment