1
// Copyright 2015-2023 Benjamin Fry <benjaminfry@me.com>
2
//
3
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5
// http://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

            
10
mod decoder;
11
mod encoder;
12
mod restrict;
13

            
14
pub use self::decoder::{BinDecoder, DecodeError};
15
pub use self::encoder::BinEncoder;
16
pub use self::encoder::EncodeMode;
17
pub use self::restrict::{Restrict, RestrictedMath, Verified};
18

            
19
#[cfg(test)]
20
pub mod bin_tests;
21

            
22
use crate::error::*;
23

            
24
/// A type which can be encoded into a DNS binary format
25
pub trait BinEncodable {
26
    /// Write the type to the stream
27
    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()>;
28

            
29
    /// Returns the object in binary form
30
    fn to_bytes(&self) -> ProtoResult<Vec<u8>> {
31
        let mut bytes = Vec::<u8>::new();
32
        {
33
            let mut encoder = BinEncoder::new(&mut bytes);
34
            self.emit(&mut encoder)?;
35
        }
36

            
37
        Ok(bytes)
38
    }
39
}
40

            
41
/// A trait for types which are serializable to and from DNS binary formats
42
pub trait BinDecodable<'r>: Sized {
43
    /// Read the type from the stream
44
    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self>;
45

            
46
    /// Returns the object in binary form
47
    fn from_bytes(bytes: &'r [u8]) -> ProtoResult<Self> {
48
        let mut decoder = BinDecoder::new(bytes);
49
        Self::read(&mut decoder)
50
    }
51
}
52

            
53
impl BinEncodable for u16 {
54
    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
55
        encoder.emit_u16(*self)
56
    }
57
}
58

            
59
impl<'r> BinDecodable<'r> for u16 {
60
    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
61
        decoder
62
            .read_u16()
63
            .map(Restrict::unverified)
64
            .map_err(Into::into)
65
    }
66
}
67

            
68
impl BinEncodable for i32 {
69
    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
70
        encoder.emit_i32(*self)
71
    }
72
}
73

            
74
impl<'r> BinDecodable<'r> for i32 {
75
    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self> {
76
        decoder
77
            .read_i32()
78
            .map(Restrict::unverified)
79
            .map_err(Into::into)
80
    }
81
}
82

            
83
impl BinEncodable for u32 {
84
    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
85
        encoder.emit_u32(*self)
86
    }
87
}
88

            
89
impl<'r> BinDecodable<'r> for u32 {
90
    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
91
        decoder
92
            .read_u32()
93
            .map(Restrict::unverified)
94
            .map_err(Into::into)
95
    }
96
}
97

            
98
impl BinEncodable for Vec<u8> {
99
    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
100
        encoder.emit_vec(self)
101
    }
102
}