aboutsummaryrefslogtreecommitdiff
path: root/src/primitive/string.rs
blob: dcd4f7c776847db2375d1291fc7b130e20bec126 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
extern crate byteorder;

use std::result::Result;
use std::vec::Vec;

use failure::Error;

use log::trace;

use crate::util;
use crate::{Deserialize, DeserializeUTF8, Serialize, SerializeUTF8};

/// We Shadow the String type here as we can only use impl on types in our own scope.
///
/// Strings are serialized as an i32 for the length in bytes, then the chars represented in UTF-16 in bytes.
///
/// Strings can only be serialized as UTF-8 null-terminated ByteArrays with (de)serialize_utf8().
impl Serialize for String {
    fn serialize(&self) -> Result<Vec<u8>, Error> {
        let mut res: Vec<u8> = Vec::new();

        let utf16: Vec<u16> = self.encode_utf16().collect();
        for i in utf16 {
            res.extend(i.to_be_bytes().iter());
        }

        util::prepend_byte_len(&mut res);
        return Ok(res);
    }
}

impl SerializeUTF8 for String {
    fn serialize_utf8(&self) -> Result<Vec<u8>, Error> {
        let mut res: Vec<u8> = Vec::new();
        res.extend(self.clone().into_bytes());
        util::prepend_byte_len(&mut res);
        return Ok(res);
    }
}

impl Deserialize for String {
    fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
        // Parse Length
        let (_, len) = i32::parse(&b[0..4])?;
        trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]);

        if len == -1 {
            return Ok((4, "".to_string()));
        }

        // length as usize
        let ulen = len as usize;
        let mut pos: usize = 4;
        let mut chars: Vec<u16> = Vec::new();
        loop {
            // if position is behind the length plus our 4 bytes of the length we already parsed
            if pos >= (ulen + 4) {
                break;
            }
            let (slen, uchar) = u16::parse(&b[pos..(pos + 2)])?;
            chars.push(uchar);
            pos += slen;
        }

        let res: String = String::from_utf16(&chars).unwrap();
        trace!("parsed string: {}", res);
        return Ok((pos, res));
    }
}

impl DeserializeUTF8 for String {
    fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> {
        let (_, len) = i32::parse(&b[0..4])?;

        trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]);

        if len <= 0 {
            return Ok((4, "".to_string()));
        }

        let ulen = len as usize;

        let mut res: String = String::from_utf8(b[4..(ulen + 4)].to_vec())?;
        trace!("parsed string: {}", res);

        // If the last byte is zero remove it
        // Receiving a string as bytearray will sometimes have
        // the string null terminated
        if res.chars().last().unwrap() == '\u{0}' {
            let _ = res.pop();
        }

        trace!("parsed string after trunc: {}", res);

        return Ok((ulen + 4, res));
    }
}