hickory_proto/serialize/binary/
mod.rs

1// Copyright 2015-2023 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Binary serialization types
9
10mod decoder;
11mod encoder;
12mod restrict;
13
14use alloc::vec::Vec;
15
16pub use self::decoder::{BinDecoder, DecodeError};
17pub use self::encoder::BinEncoder;
18pub use self::encoder::EncodeMode;
19pub use self::restrict::{Restrict, RestrictedMath, Verified};
20
21#[cfg(test)]
22pub(crate) mod bin_tests;
23
24use crate::error::*;
25
26/// A type which can be encoded into a DNS binary format
27pub trait BinEncodable {
28    /// Write the type to the stream
29    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()>;
30
31    /// Returns the object in binary form
32    fn to_bytes(&self) -> ProtoResult<Vec<u8>> {
33        let mut bytes = Vec::<u8>::new();
34        {
35            let mut encoder = BinEncoder::new(&mut bytes);
36            self.emit(&mut encoder)?;
37        }
38
39        Ok(bytes)
40    }
41}
42
43/// A trait for types which are serializable to and from DNS binary formats
44pub trait BinDecodable<'r>: Sized {
45    /// Read the type from the stream
46    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self>;
47
48    /// Returns the object in binary form
49    fn from_bytes(bytes: &'r [u8]) -> ProtoResult<Self> {
50        let mut decoder = BinDecoder::new(bytes);
51        Self::read(&mut decoder)
52    }
53}
54
55impl BinEncodable for u16 {
56    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
57        encoder.emit_u16(*self)
58    }
59}
60
61impl BinDecodable<'_> for u16 {
62    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
63        decoder
64            .read_u16()
65            .map(Restrict::unverified)
66            .map_err(Into::into)
67    }
68}
69
70impl BinEncodable for i32 {
71    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
72        encoder.emit_i32(*self)
73    }
74}
75
76impl<'r> BinDecodable<'r> for i32 {
77    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self> {
78        decoder
79            .read_i32()
80            .map(Restrict::unverified)
81            .map_err(Into::into)
82    }
83}
84
85impl BinEncodable for u32 {
86    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
87        encoder.emit_u32(*self)
88    }
89}
90
91impl BinDecodable<'_> for u32 {
92    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
93        decoder
94            .read_u32()
95            .map(Restrict::unverified)
96            .map_err(Into::into)
97    }
98}
99
100impl BinEncodable for Vec<u8> {
101    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
102        encoder.emit_vec(self)
103    }
104}