antelope/
util.rs

1use std::io::Write;
2
3use flate2::{write::ZlibEncoder, Compression};
4use hex::{decode, encode};
5
6pub fn hex_to_bytes(hex: &str) -> Vec<u8> {
7    decode(hex).unwrap()
8}
9
10pub fn bytes_to_hex(bytes: &Vec<u8>) -> String {
11    encode(bytes)
12}
13
14pub fn array_equals<T: PartialEq>(a: &[T], b: &[T]) -> bool {
15    a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x == y)
16}
17
18pub fn array_to_hex(bytes: &[u8]) -> String {
19    //bytes.iter().map(|b| format!("{:02x}", b)).collect()
20    encode(bytes)
21}
22
23pub fn slice_copy(dst: &mut [u8], src: &[u8]) {
24    dst.copy_from_slice(src);
25    // assert!(dst.len() == src.len(), "copy_slice: length not the same!");
26    // unsafe { memcpy(dst.as_mut_ptr(), src.as_ptr(), dst.len()); }
27}
28
29pub fn zlib_compress(bytes: &[u8]) -> Result<Vec<u8>, String> {
30    let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
31    if e.write_all(bytes).is_err() {
32        return Err("Error during compression".into());
33    }
34    let compressed_bytes = e.finish();
35    if compressed_bytes.is_err() {
36        return Err("Error during compression".into());
37    }
38    Ok(compressed_bytes.unwrap())
39}