xref: /webrtc/mdns/src/message/packer.rs (revision ffe74184)
1 use super::*;
2 use crate::error::*;
3 
4 // pack_bytes appends the wire format of field to msg.
pack_bytes(mut msg: Vec<u8>, field: &[u8]) -> Vec<u8>5 pub(crate) fn pack_bytes(mut msg: Vec<u8>, field: &[u8]) -> Vec<u8> {
6     msg.extend_from_slice(field);
7     msg
8 }
9 
unpack_bytes(msg: &[u8], off: usize, field: &mut [u8]) -> Result<usize>10 pub(crate) fn unpack_bytes(msg: &[u8], off: usize, field: &mut [u8]) -> Result<usize> {
11     let new_off = off + field.len();
12     if new_off > msg.len() {
13         return Err(Error::ErrBaseLen);
14     }
15     field.copy_from_slice(&msg[off..new_off]);
16     Ok(new_off)
17 }
18 
19 // pack_uint16 appends the wire format of field to msg.
pack_uint16(mut msg: Vec<u8>, field: u16) -> Vec<u8>20 pub(crate) fn pack_uint16(mut msg: Vec<u8>, field: u16) -> Vec<u8> {
21     msg.extend_from_slice(&field.to_be_bytes());
22     msg
23 }
24 
unpack_uint16(msg: &[u8], off: usize) -> Result<(u16, usize)>25 pub(crate) fn unpack_uint16(msg: &[u8], off: usize) -> Result<(u16, usize)> {
26     if off + UINT16LEN > msg.len() {
27         return Err(Error::ErrBaseLen);
28     }
29 
30     Ok((
31         (msg[off] as u16) << 8 | (msg[off + 1] as u16),
32         off + UINT16LEN,
33     ))
34 }
35 
skip_uint16(msg: &[u8], off: usize) -> Result<usize>36 pub(crate) fn skip_uint16(msg: &[u8], off: usize) -> Result<usize> {
37     if off + UINT16LEN > msg.len() {
38         return Err(Error::ErrBaseLen);
39     }
40     Ok(off + UINT16LEN)
41 }
42 
43 // pack_uint32 appends the wire format of field to msg.
pack_uint32(mut msg: Vec<u8>, field: u32) -> Vec<u8>44 pub(crate) fn pack_uint32(mut msg: Vec<u8>, field: u32) -> Vec<u8> {
45     msg.extend_from_slice(&field.to_be_bytes());
46     msg
47 }
48 
unpack_uint32(msg: &[u8], off: usize) -> Result<(u32, usize)>49 pub(crate) fn unpack_uint32(msg: &[u8], off: usize) -> Result<(u32, usize)> {
50     if off + UINT32LEN > msg.len() {
51         return Err(Error::ErrBaseLen);
52     }
53     let v = (msg[off] as u32) << 24
54         | (msg[off + 1] as u32) << 16
55         | (msg[off + 2] as u32) << 8
56         | (msg[off + 3] as u32);
57     Ok((v, off + UINT32LEN))
58 }
59 
skip_uint32(msg: &[u8], off: usize) -> Result<usize>60 pub(crate) fn skip_uint32(msg: &[u8], off: usize) -> Result<usize> {
61     if off + UINT32LEN > msg.len() {
62         return Err(Error::ErrBaseLen);
63     }
64     Ok(off + UINT32LEN)
65 }
66 
67 // pack_text appends the wire format of field to msg.
pack_str(mut msg: Vec<u8>, field: &str) -> Result<Vec<u8>>68 pub(crate) fn pack_str(mut msg: Vec<u8>, field: &str) -> Result<Vec<u8>> {
69     let l = field.len();
70     if l > 255 {
71         return Err(Error::ErrStringTooLong);
72     }
73     msg.push(l as u8);
74     msg.extend_from_slice(field.as_bytes());
75     Ok(msg)
76 }
77 
unpack_str(msg: &[u8], off: usize) -> Result<(String, usize)>78 pub(crate) fn unpack_str(msg: &[u8], off: usize) -> Result<(String, usize)> {
79     if off >= msg.len() {
80         return Err(Error::ErrBaseLen);
81     }
82     let begin_off = off + 1;
83     let end_off = begin_off + msg[off] as usize;
84     if end_off > msg.len() {
85         return Err(Error::ErrCalcLen);
86     }
87 
88     Ok((
89         String::from_utf8(msg[begin_off..end_off].to_vec())?,
90         end_off,
91     ))
92 }
93