1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Ordered serialization/deserialization for primitive types and byte arrays.
//!
//! If you need to serialize or deserialize a primitive type (e.g. for use as a key), it is better
//! to use [`SerializableValue`] trait methods on primitive types directly, without using [`serde`].
//!
//! Serialize method `to_write()` writes results to [`WriteBytes`] trait impl,
//!  deserialize method `from_reader()` reads from [`ReadBytes`].
//!
//! **Deserializing a value which was serialized for different [`EncodingParams`](crate::params::EncodingParams)
//! is unchecked and is undefined behaviour!**
//!
//! Note that `u128` and `i128` may not be supported on some platforms.
//!
//! ### Encoding details
//! - unsigned integers are encoded in big-endian layout
//! - integers are encoded min-value-complemented, big-endian layout
//!
//! ### Parameters
//! Encoding parameters are passed via impl of `EncodingParams` (usually ZST struct).

use crate::{Result, Error, buf::{ReadBytes, WriteBytes}, params::{EncodingParams, Order, Endianness}};
use core::convert::TryInto;

/// Serializable value
///
/// This crate implements this trait for all primitive types. For complex types, use
/// provided _serde_ serializer and deserializer.
pub trait SerializableValue: Sized {
    fn to_writer<P: EncodingParams>(&self, writer: impl WriteBytes, params: P) -> Result;
    fn from_reader<P: EncodingParams>(reader: impl ReadBytes, params: P) -> Result<Self>;
}

/// Serialization data format version
pub const VERSION: u8 = 1;

macro_rules! ord_cond {
    ($param:ident, $desc:expr, $asc:expr) => {
        match <$param>::ORDER {
            Order::Ascending|Order::Unordered => { $asc },
            Order::Descending =>  { $desc },
        }
    }
}

macro_rules! to_bytes {
    ($param:ident, $v:expr) => {
        &match <$param>::ENDIANNESS {
            Endianness::Little => $v.to_le_bytes(),
            Endianness::Big    => $v.to_be_bytes(),
            Endianness::Native => $v.to_ne_bytes(),
        }
    }
}

macro_rules! from_bytes {
    ($param:ident, $ut:ty, $v:expr) => {
        match <$param>::ENDIANNESS {
            Endianness::Little => <$ut>::from_le_bytes($v.try_into().unwrap()),
            Endianness::Big    => <$ut>::from_be_bytes($v.try_into().unwrap()),
            Endianness::Native => <$ut>::from_ne_bytes($v.try_into().unwrap()),
        }
    }
}

// Ordered serialization of integers
macro_rules! serialize_int {
    ($ut:ty, $it:ty) => {
        impl SerializableValue for $ut {
            #[inline]
            fn to_writer<P: EncodingParams>(&self, mut writer: impl WriteBytes, _params: P) -> Result
            {
                writer.write(to_bytes!(P, &{ord_cond!(P, !*self, *self)}))
            }
            #[inline]
            fn from_reader<P: EncodingParams>(mut reader: impl ReadBytes, _params: P) -> Result<Self>
            {
                const N: usize = core::mem::size_of::<$ut>();
                reader.read(N, |buf| {
                    let rv = from_bytes!(P, $ut, buf);
                    Ok(ord_cond!(P, !rv, rv))
                })
            }
        }
        impl SerializableValue for $it {
            #[inline]
            fn to_writer<P: EncodingParams>(&self, writer: impl WriteBytes, params: P) -> Result
            {
                ((self ^ <$it>::min_value()) as $ut).to_writer(writer, params)
            }
            #[inline]
            fn from_reader<P: EncodingParams>(reader: impl ReadBytes, params: P) -> Result<Self>
            {
                <$ut>::from_reader(reader, params).map(|u| { (u as $it) ^ <$it>::min_value() })
            }
        }
    }
}

serialize_int!(u8,  i8);
serialize_int!(u16, i16);
serialize_int!(u32, i32);
serialize_int!(u64, i64);

#[cfg(not(no_i128))]
serialize_int!(u128, i128);

impl SerializableValue for bool {
    fn to_writer<P: EncodingParams>(&self, writer: impl WriteBytes, params: P) -> Result {
        let v: u8 = if *self { 1 } else { 0 };
        v.to_writer(writer, params)
    }

    fn from_reader<P: EncodingParams>(reader: impl ReadBytes, params: P) -> Result<Self> {
        <u8>::from_reader(reader, params).map(|v| v != 0)
    }
}

impl SerializableValue for char {
    fn to_writer<P: EncodingParams>(&self, writer: impl WriteBytes, params: P) -> Result {
        (*self as u32).to_writer(writer, params)
    }

    fn from_reader<P: EncodingParams>(reader: impl ReadBytes, params: P) -> Result<Self> {
        let ch = u32::from_reader(reader, params)?;
        core::char::from_u32(ch).ok_or(Error::InvalidUtf8Encoding)
    }
}

// Ordered serialization of floats
macro_rules! serialize_float {
    ($ft:ty, $ift:ty, $uft:ty) => {
        impl SerializableValue for $ft {
            #[inline]
            fn to_writer<P: EncodingParams>(&self, mut writer: impl WriteBytes, _params: P) -> Result {
                let t = self.to_bits() as $ift;
                let ov = if matches!(P::ENDIANNESS, Endianness::Big) {
                    const MSBOFFS: usize = core::mem::size_of::<$ift>() * 8 - 1; // # of bits - 1
                    t ^ ((t >> MSBOFFS) | <$ift>::min_value())
                } else {
                    t
                };
                writer.write(to_bytes!(P, &ord_cond!(P, !ov, ov)))
            }
            #[inline]
            fn from_reader<P: EncodingParams>(reader: impl ReadBytes, params: P) -> Result<Self> {
                const MSBOFFS: usize = core::mem::size_of::<$ift>() * 8 - 1; // # of bits - 1
                let val = <$uft>::from_reader(reader, params)? as $ift;
                if matches!(P::ENDIANNESS, Endianness::Big) {
                    let t = ((val ^ <$ift>::min_value()) >> MSBOFFS) | <$ift>::min_value();
                    Ok(<$ft>::from_bits((val ^ t) as $uft))
                } else {
                    Ok(<$ft>::from_bits(val as $uft))
                }
            }
        }
    }
}

serialize_float!(f32, i32, u32);
serialize_float!(f64, i64, u64);

/// Bitwise invert contents of a buffer
pub fn invert_buffer(buf: &mut [u8])
{
    for b in buf {
        *b = !*b;
    }
}