1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::ffi::CString;
use std::io::prelude::{Read, Write};
use {hint, util, Error, Parcel, Settings};

impl Parcel for CString {
    const TYPE_NAME: &'static str = "CString";

    fn read_field(
        read: &mut dyn Read,
        settings: &Settings,
        _hints: &mut hint::Hints,
    ) -> Result<Self, Error> {
        let mut result = Vec::new();
        // this logic is susceptible to DoS attacks by never providing
        //   a null character and will be fixed by
        //   https://github.com/dylanmckay/protocol/issues/14
        loop {
            let c: u8 = Parcel::read(read, settings)?;
            if c == 0x00 {
                return Ok(CString::new(result)?);
            }
            result.push(c);
        }
    }

    fn write_field(
        &self,
        write: &mut dyn Write,
        settings: &Settings,
        _hints: &mut hint::Hints,
    ) -> Result<(), Error> {
        util::write_items(self.clone().into_bytes_with_nul().iter(), write, settings)
    }
}

#[cfg(test)]
mod test {
    use std::ffi::CString;
    use std::io::Cursor;
    use {Parcel, Settings};

    #[test]
    fn can_read_cstring() {
        let mut data = Cursor::new([0x41, 0x42, 0x43, 0]);
        let read_back: CString = Parcel::read(&mut data, &Settings::default()).unwrap();
        assert_eq!(read_back, CString::new("ABC").unwrap());
    }

    #[test]
    fn can_write_cstring() {
        let mut buffer = Cursor::new(Vec::new());

        CString::new("ABC")
            .unwrap()
            .write(&mut buffer, &Settings::default())
            .unwrap();
        assert_eq!(buffer.into_inner(), vec![0x41, 0x42, 0x43, 0]);
    }
}