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//! ```
6162#[cfg(test)]
63use hex as _;
6465extern crate alloc;
6667use alloc::string::ToString;
68use core::str::from_utf8 as str_from_utf8;
6970use bytes::{Buf, BufMut, Bytes, BytesMut};
7172pub mod container_as_blob;
73pub mod error;
74mod io;
75pub mod macros;
76pub mod marker;
77mod value;
78mod varint;
7980pub use error::*;
81use io::*;
82pub use marker::{InnerMarker, Marker};
83pub use value::EpeeValue;
84pub use varint::{read_varint, write_varint};
8586/// 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;
9596/// 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.
103fn add_field<B: Buf>(&mut self, name: &str, b: &mut B) -> Result<bool>;
104105/// Called when the number of fields has been read.
106fn finish(self) -> Result<T>;
107}
108109/// A trait for an object that can be turned into epee bytes.
110pub trait EpeeObject: Sized {
111type Builder: EpeeObjectBuilder<Self>;
112113/// Returns the number of fields to be encoded.
114fn number_of_fields(&self) -> u64;
115116/// write the objects fields into the writer.
117fn write_fields<B: BufMut>(self, w: &mut B) -> Result<()>;
118}
119120/// 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}
124125/// Turn the object into epee bytes.
126pub fn to_bytes<T: EpeeObject>(val: T) -> Result<BytesMut> {
127let mut buf = BytesMut::new();
128 write_head_object(val, &mut buf)?;
129Ok(buf)
130}
131132fn read_header<B: Buf>(r: &mut B) -> Result<()> {
133let buf = checked_read(r, |b: &mut B| b.copy_to_bytes(HEADER.len()), HEADER.len())?;
134135if &*buf != HEADER {
136return Err(Error::Format("Data does not contain header"));
137 }
138Ok(())
139}
140141fn write_header<B: BufMut>(w: &mut B) -> Result<()> {
142 checked_write(w, BufMut::put_slice, HEADER, HEADER.len())
143}
144145fn write_head_object<T: EpeeObject, B: BufMut>(val: T, w: &mut B) -> Result<()> {
146 write_header(w)?;
147 val.write(w)
148}
149150fn read_head_object<T: EpeeObject, B: Buf>(r: &mut B) -> Result<T> {
151 read_header(r)?;
152let mut skipped_objects = 0;
153 read_object(r, &mut skipped_objects)
154}
155156fn read_field_name_bytes<B: Buf>(r: &mut B) -> Result<Bytes> {
157let len: usize = r.get_u8().into();
158159 checked_read(r, |b: &mut B| b.copy_to_bytes(len), len)
160}
161162fn write_field_name<B: BufMut>(val: &str, w: &mut B) -> Result<()> {
163 checked_write_primitive(w, BufMut::put_u8, val.len().try_into()?)?;
164let slice = val.as_bytes();
165 checked_write(w, BufMut::put_slice, slice, slice.len())
166}
167168/// Write an epee field.
169pub fn write_field<T: EpeeValue, B: BufMut>(val: T, field_name: &str, w: &mut B) -> Result<()> {
170if val.should_write() {
171 write_field_name(field_name, w)?;
172 write_epee_value(val, w)?;
173 }
174Ok(())
175}
176177fn read_object<T: EpeeObject, B: Buf>(r: &mut B, skipped_objects: &mut u8) -> Result<T> {
178let mut object_builder = T::Builder::default();
179180let number_o_field = read_varint(r)?;
181182if number_o_field > MAX_NUM_FIELDS {
183return Err(Error::Format(
184"Data has object with more fields than the maximum allowed",
185 ));
186 }
187188for _ in 0..number_o_field {
189let field_name_bytes = read_field_name_bytes(r)?;
190let field_name = str_from_utf8(&field_name_bytes)?;
191192if !object_builder.add_field(field_name, r)? {
193 skip_epee_value(r, skipped_objects)?;
194 }
195 }
196 object_builder.finish()
197}
198199/// 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}
204205/// 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> {
208let marker = read_marker(r)?;
209 T::read(r, &marker)
210}
211212/// 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}
218219/// 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<()> {
246let bytes = t.as_ref();
247let len = bytes.len();
248249 write_varint(len, w)?;
250251if w.remaining_mut() < len {
252return Err(Error::IO("Not enough capacity to write bytes"));
253 }
254255 w.put_slice(bytes);
256257Ok(())
258}
259260/// 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
289T: EpeeValue,
290 I: Iterator<Item = T> + ExactSizeIterator,
291 B: BufMut,
292{
293 write_varint(iterator.len(), w)?;
294for item in iterator {
295 item.write(w)?;
296 }
297Ok(())
298}
299300/// A helper object builder that just skips every field.
301#[derive(Default)]
302struct SkipObjectBuilder;
303304impl EpeeObjectBuilder<SkipObject> for SkipObjectBuilder {
305fn add_field<B: Buf>(&mut self, _name: &str, _r: &mut B) -> Result<bool> {
306Ok(false)
307 }
308309fn finish(self) -> Result<SkipObject> {
310Ok(SkipObject)
311 }
312}
313314/// A helper object that just skips every field.
315struct SkipObject;
316317impl EpeeObject for SkipObject {
318type Builder = SkipObjectBuilder;
319320fn number_of_fields(&self) -> u64 {
321panic!("This is a helper function to use when de-serialising")
322 }
323324fn write_fields<B: BufMut>(self, _w: &mut B) -> Result<()> {
325panic!("This is a helper function to use when de-serialising")
326 }
327}
328329/// 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<()> {
332let marker = read_marker(r)?;
333334let len = if marker.is_seq { read_varint(r)? } else { 1 };
335336if let Some(size) = marker.inner_marker.size() {
337let bytes_to_skip = size
338 .checked_mul(len.try_into()?)
339 .ok_or(Error::Value("List is too big".to_string()))?;
340return advance(bytes_to_skip, r);
341 };
342343for _ in 0..len {
344match 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 => {
356let len = read_varint(r)?;
357 advance(len, r)?;
358 }
359 InnerMarker::Object => {
360*skipped_objects += 1;
361if *skipped_objects > MAX_DEPTH_OF_SKIPPED_OBJECTS {
362return Err(Error::Format("Depth of skipped objects exceeded maximum"));
363 }
364 read_object::<SkipObject, _>(r, skipped_objects)?;
365*skipped_objects -= 1;
366 }
367 };
368 }
369Ok(())
370}
371372fn advance<B: Buf>(n: usize, b: &mut B) -> Result<()> {
373 checked_read(b, |b: &mut B| b.advance(n), n)
374}
375376/// Max upfront capacity when deserializing heap items.
377///
378/// Allocating upfront saves allocations however this must be limited otherwise an attacker can easily
379/// cause an OOM.
380const fn max_upfront_capacity<T>() -> usize {
3812_000_000 / size_of::<T>()
382}