djin_protocol/types/
cstring.rs

1use crate::{hint, util, Error, Parcel, Settings};
2use std::ffi::CString;
3use std::io::prelude::{Read, Write};
4
5impl Parcel for CString {
6    const TYPE_NAME: &'static str = "CString";
7
8    fn read_field(
9        read: &mut dyn Read,
10        settings: &Settings,
11        _hints: &mut hint::Hints,
12    ) -> Result<Self, Error> {
13        let mut result = Vec::new();
14        // this logic is susceptible to DoS attacks by never providing
15        //   a null character and will be fixed by
16        //   https://github.com/dylanmckay/protocol/issues/14
17        loop {
18            let c: u8 = Parcel::read(read, settings)?;
19            if c == 0x00 {
20                return Ok(CString::new(result)?);
21            }
22            result.push(c);
23        }
24    }
25
26    fn write_field(
27        &self,
28        write: &mut dyn Write,
29        settings: &Settings,
30        _hints: &mut hint::Hints,
31    ) -> Result<(), Error> {
32        util::write_items(self.clone().into_bytes_with_nul().iter(), write, settings)
33    }
34}
35
36#[cfg(test)]
37mod test {
38    use crate::{Parcel, Settings};
39    use std::ffi::CString;
40    use std::io::Cursor;
41
42    #[test]
43    fn can_read_cstring() {
44        let mut data = Cursor::new([0x41, 0x42, 0x43, 0]);
45        let read_back: CString = Parcel::read(&mut data, &Settings::default()).unwrap();
46        assert_eq!(read_back, CString::new("ABC").unwrap());
47    }
48
49    #[test]
50    fn can_write_cstring() {
51        let mut buffer = Cursor::new(Vec::new());
52
53        CString::new("ABC")
54            .unwrap()
55            .write(&mut buffer, &Settings::default())
56            .unwrap();
57        assert_eq!(buffer.into_inner(), vec![0x41, 0x42, 0x43, 0]);
58    }
59}