cuprate_epee_encoding/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2//! Epee Encoding
3//!
4//! This library contains the Epee binary format found in Monero, unlike other
5//! crates this crate does not use serde.
6//!
7//! See [`epee_object`] for how to easily implement [`EpeeObject`] for your types.
8//!
9//! example without macro:
10//! ```rust
11//! # use cuprate_epee_encoding::{EpeeObject, EpeeObjectBuilder, read_epee_value, write_field, to_bytes, from_bytes};
12//! # use bytes::{Buf, BufMut};
13//!
14//! pub struct Test {
15//!     val: u64
16//! }
17//!
18//! #[derive(Default)]
19//! pub struct __TestEpeeBuilder {
20//!     val: Option<u64>,
21//! }
22//!
23//! impl EpeeObjectBuilder<Test> for __TestEpeeBuilder {
24//!     fn add_field<B: Buf>(&mut self, name: &str, r: &mut B) -> cuprate_epee_encoding::error::Result<bool> {
25//!         match name {
26//!             "val" => {self.val = Some(read_epee_value(r)?);}
27//!             _ => return Ok(false),
28//!         }
29//!         Ok(true)
30//!     }
31//!
32//!     fn finish(self) -> cuprate_epee_encoding::error::Result<Test> {
33//!         Ok(
34//!             Test {
35//!                 val: self.val.ok_or_else(|| cuprate_epee_encoding::error::Error::Format("Required field was not found!"))?
36//!             }
37//!         )
38//!     }
39//! }
40//!
41//! impl EpeeObject for Test {
42//!     type Builder = __TestEpeeBuilder;
43//!
44//!     fn number_of_fields(&self) -> u64 {
45//!         1
46//!     }
47//!
48//!     fn write_fields<B: BufMut>(self, w: &mut B) -> cuprate_epee_encoding::error::Result<()> {
49//!        // write the fields
50//!        write_field(self.val, "val", w)
51//!    }
52//! }
53//!
54//!
55//! let data = [1, 17, 1, 1, 1, 1, 2, 1, 1, 4, 3, 118, 97, 108, 5, 4, 0, 0, 0, 0, 0, 0, 0]; // the data to decode;
56//! let val: Test = from_bytes(&mut data.as_slice()).unwrap();
57//! let data = to_bytes(val).unwrap();
58//!
59//!
60//! ```
61
62#[cfg(test)]
63use hex as _;
64
65extern crate alloc;
66
67use alloc::string::ToString;
68use core::str::from_utf8 as str_from_utf8;
69
70use bytes::{Buf, BufMut, Bytes, BytesMut};
71
72pub mod container_as_blob;
73pub mod error;
74mod io;
75pub mod macros;
76pub mod marker;
77mod value;
78mod varint;
79
80pub use error::*;
81use io::*;
82pub use marker::{InnerMarker, Marker};
83pub use value::EpeeValue;
84pub use varint::{read_varint, write_varint};
85
86/// Header that needs to be at the beginning of every binary blob that follows
87/// this binary serialization format.
88const HEADER: &[u8] = b"\x01\x11\x01\x01\x01\x01\x02\x01\x01";
89/// The maximum length a byte array (marked as a string) can be.
90const MAX_STRING_LEN_POSSIBLE: usize = 2000000000;
91/// The maximum depth of skipped objects.
92const MAX_DEPTH_OF_SKIPPED_OBJECTS: u8 = 20;
93/// The maximum number of fields in an object.
94const MAX_NUM_FIELDS: u64 = 1000;
95
96/// A trait for an object that can build a type `T` from the epee format.
97pub trait EpeeObjectBuilder<T>: Default + Sized {
98    /// Called when a field names has been read no other bytes following the field
99    /// name will have been read.
100    ///
101    /// Returns a bool if true then the field has been read otherwise the field is not
102    /// needed and has not been read.
103    fn add_field<B: Buf>(&mut self, name: &str, b: &mut B) -> Result<bool>;
104
105    /// Called when the number of fields has been read.
106    fn finish(self) -> Result<T>;
107}
108
109/// A trait for an object that can be turned into epee bytes.
110pub trait EpeeObject: Sized {
111    type Builder: EpeeObjectBuilder<Self>;
112
113    /// Returns the number of fields to be encoded.
114    fn number_of_fields(&self) -> u64;
115
116    /// write the objects fields into the writer.
117    fn write_fields<B: BufMut>(self, w: &mut B) -> Result<()>;
118}
119
120/// Read the object `T` from a byte array.
121pub fn from_bytes<T: EpeeObject, B: Buf>(buf: &mut B) -> Result<T> {
122    read_head_object(buf)
123}
124
125/// Turn the object into epee bytes.
126pub fn to_bytes<T: EpeeObject>(val: T) -> Result<BytesMut> {
127    let mut buf = BytesMut::new();
128    write_head_object(val, &mut buf)?;
129    Ok(buf)
130}
131
132fn read_header<B: Buf>(r: &mut B) -> Result<()> {
133    let buf = checked_read(r, |b: &mut B| b.copy_to_bytes(HEADER.len()), HEADER.len())?;
134
135    if &*buf != HEADER {
136        return Err(Error::Format("Data does not contain header"));
137    }
138    Ok(())
139}
140
141fn write_header<B: BufMut>(w: &mut B) -> Result<()> {
142    checked_write(w, BufMut::put_slice, HEADER, HEADER.len())
143}
144
145fn write_head_object<T: EpeeObject, B: BufMut>(val: T, w: &mut B) -> Result<()> {
146    write_header(w)?;
147    val.write(w)
148}
149
150fn read_head_object<T: EpeeObject, B: Buf>(r: &mut B) -> Result<T> {
151    read_header(r)?;
152    let mut skipped_objects = 0;
153    read_object(r, &mut skipped_objects)
154}
155
156fn read_field_name_bytes<B: Buf>(r: &mut B) -> Result<Bytes> {
157    let len: usize = r.get_u8().into();
158
159    checked_read(r, |b: &mut B| b.copy_to_bytes(len), len)
160}
161
162fn write_field_name<B: BufMut>(val: &str, w: &mut B) -> Result<()> {
163    checked_write_primitive(w, BufMut::put_u8, val.len().try_into()?)?;
164    let slice = val.as_bytes();
165    checked_write(w, BufMut::put_slice, slice, slice.len())
166}
167
168/// Write an epee field.
169pub fn write_field<T: EpeeValue, B: BufMut>(val: T, field_name: &str, w: &mut B) -> Result<()> {
170    if val.should_write() {
171        write_field_name(field_name, w)?;
172        write_epee_value(val, w)?;
173    }
174    Ok(())
175}
176
177fn read_object<T: EpeeObject, B: Buf>(r: &mut B, skipped_objects: &mut u8) -> Result<T> {
178    let mut object_builder = T::Builder::default();
179
180    let number_o_field = read_varint(r)?;
181
182    if number_o_field > MAX_NUM_FIELDS {
183        return Err(Error::Format(
184            "Data has object with more fields than the maximum allowed",
185        ));
186    }
187
188    for _ in 0..number_o_field {
189        let field_name_bytes = read_field_name_bytes(r)?;
190        let field_name = str_from_utf8(&field_name_bytes)?;
191
192        if !object_builder.add_field(field_name, r)? {
193            skip_epee_value(r, skipped_objects)?;
194        }
195    }
196    object_builder.finish()
197}
198
199/// Read a marker from the [`Buf`], this function should only be used for
200/// custom serialisation based on the marker otherwise just use [`read_epee_value`].
201pub fn read_marker<B: Buf>(r: &mut B) -> Result<Marker> {
202    Marker::try_from(checked_read_primitive(r, Buf::get_u8)?)
203}
204
205/// Read an epee value from the stream, an epee value is the part after the key
206/// including the marker.
207pub fn read_epee_value<T: EpeeValue, B: Buf>(r: &mut B) -> Result<T> {
208    let marker = read_marker(r)?;
209    T::read(r, &marker)
210}
211
212/// Write an epee value to the stream, an epee value is the part after the key
213/// including the marker.
214fn write_epee_value<T: EpeeValue, B: BufMut>(val: T, w: &mut B) -> Result<()> {
215    checked_write_primitive(w, BufMut::put_u8, T::MARKER.as_u8())?;
216    val.write(w)
217}
218
219/// Write a byte array to `w` with [`write_varint`].
220///
221/// This function:
222/// - Writes the length of `t`'s bytes into `w` using [`write_varint`]
223/// - Writes `t`'s bytes into `w`
224///
225/// It is used as the internal [`EpeeValue::write`]
226/// implementation of byte-like containers such as:
227/// - [`EpeeValue::<Vec<u8>>::write`]
228/// - [`EpeeValue::<String>::write`]
229///
230/// # Errors
231/// This will error if:
232/// - [`write_varint`] fails
233/// - `w` does not have enough capacity
234///
235/// # Example
236/// ```rust
237/// let t: [u8; 8] = [3, 0, 0, 0, 1, 0, 0, 0];
238/// let mut w = vec![];
239///
240/// cuprate_epee_encoding::write_bytes(t, &mut w).unwrap();
241///
242/// assert_eq!(w.len(), 9); // length of bytes + bytes
243/// assert_eq!(w[1..], t);
244/// ```
245pub fn write_bytes<T: AsRef<[u8]>, B: BufMut>(t: T, w: &mut B) -> Result<()> {
246    let bytes = t.as_ref();
247    let len = bytes.len();
248
249    write_varint(len, w)?;
250
251    if w.remaining_mut() < len {
252        return Err(Error::IO("Not enough capacity to write bytes"));
253    }
254
255    w.put_slice(bytes);
256
257    Ok(())
258}
259
260/// Write an [`Iterator`] of [`EpeeValue`]s to `w` with [`write_varint`].
261///
262/// This function:
263/// - Writes the length of the `iterator`, into `w` using [`write_varint`]
264/// - [`EpeeValue::write`]s each `T` of the iterator into `w`
265///
266/// It is used as the internal [`EpeeValue::write`]
267/// implementation of containers such as [`EpeeValue::<Vec<T>>::write`].
268///
269/// # Errors
270/// This will error if:
271/// - [`write_varint`] fails
272/// - [`EpeeValue::<T>::write`] fails
273///
274/// # Example
275/// ```rust
276/// let t: u64 = 3;
277/// let vec: Vec<u64> = vec![t, t];
278/// let mut w = vec![];
279///
280/// let iter: std::vec::IntoIter<u64> = vec.into_iter();
281/// cuprate_epee_encoding::write_iterator(iter, &mut w).unwrap();
282///
283/// assert_eq!(w.len(), 17);
284/// assert_eq!(w[1..9], [3, 0, 0, 0, 0, 0, 0, 0]);
285/// assert_eq!(w[9..], [3, 0, 0, 0, 0, 0, 0, 0]);
286/// ```
287pub fn write_iterator<T, I, B>(iterator: I, w: &mut B) -> Result<()>
288where
289    T: EpeeValue,
290    I: Iterator<Item = T> + ExactSizeIterator,
291    B: BufMut,
292{
293    write_varint(iterator.len(), w)?;
294    for item in iterator {
295        item.write(w)?;
296    }
297    Ok(())
298}
299
300/// A helper object builder that just skips every field.
301#[derive(Default)]
302struct SkipObjectBuilder;
303
304impl EpeeObjectBuilder<SkipObject> for SkipObjectBuilder {
305    fn add_field<B: Buf>(&mut self, _name: &str, _r: &mut B) -> Result<bool> {
306        Ok(false)
307    }
308
309    fn finish(self) -> Result<SkipObject> {
310        Ok(SkipObject)
311    }
312}
313
314/// A helper object that just skips every field.
315struct SkipObject;
316
317impl EpeeObject for SkipObject {
318    type Builder = SkipObjectBuilder;
319
320    fn number_of_fields(&self) -> u64 {
321        panic!("This is a helper function to use when de-serialising")
322    }
323
324    fn write_fields<B: BufMut>(self, _w: &mut B) -> Result<()> {
325        panic!("This is a helper function to use when de-serialising")
326    }
327}
328
329/// Skip an epee value, should be used when you do not need the value
330/// stored at a key.
331fn skip_epee_value<B: Buf>(r: &mut B, skipped_objects: &mut u8) -> Result<()> {
332    let marker = read_marker(r)?;
333
334    let len = if marker.is_seq { read_varint(r)? } else { 1 };
335
336    if let Some(size) = marker.inner_marker.size() {
337        let bytes_to_skip = size
338            .checked_mul(len.try_into()?)
339            .ok_or(Error::Value("List is too big".to_string()))?;
340        return advance(bytes_to_skip, r);
341    };
342
343    for _ in 0..len {
344        match marker.inner_marker {
345            InnerMarker::I64
346            | InnerMarker::U64
347            | InnerMarker::F64
348            | InnerMarker::I32
349            | InnerMarker::U32
350            | InnerMarker::I16
351            | InnerMarker::U16
352            | InnerMarker::I8
353            | InnerMarker::U8
354            | InnerMarker::Bool => unreachable!("These types are constant size."),
355            InnerMarker::String => {
356                let len = read_varint(r)?;
357                advance(len, r)?;
358            }
359            InnerMarker::Object => {
360                *skipped_objects += 1;
361                if *skipped_objects > MAX_DEPTH_OF_SKIPPED_OBJECTS {
362                    return Err(Error::Format("Depth of skipped objects exceeded maximum"));
363                }
364                read_object::<SkipObject, _>(r, skipped_objects)?;
365                *skipped_objects -= 1;
366            }
367        };
368    }
369    Ok(())
370}
371
372fn advance<B: Buf>(n: usize, b: &mut B) -> Result<()> {
373    checked_read(b, |b: &mut B| b.advance(n), n)
374}