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
// FalconSharedMemoryAreaString
mod string_id;
use std::{collections::HashMap, mem::size_of};

pub use string_id::*;

use crate::{MemoryFile, RawMemoryFile};

#[repr(C)]
#[derive(Debug, Default)]
pub struct StringAreaHeader {
    version_num: u32,
    no_of_strings: u32,
    data_size: u32,
}

#[repr(C)]
#[derive(Debug, Default)]
pub struct StringHeader {
    str_id: StringId,
    length: u32,
}

pub struct StringData;

impl StringData {
    pub fn read() -> Result<HashMap<StringId, String>, Box<dyn std::error::Error>> {
        let header =
            unsafe { MemoryFile::<StringAreaHeader>::new("FalconSharedMemoryAreaString")? };

        let header = header.read();

        let mut offset = size_of::<StringAreaHeader>();
        let mut strings: HashMap<StringId, String> = HashMap::new();

        for _ in 0..header.no_of_strings {
            if offset >= header.data_size as usize {
                break;
            }

            let header_size = size_of::<StringHeader>();
            let string_header = unsafe {
                MemoryFile::<StringHeader>::new_with_offset_and_size(
                    "FalconSharedMemoryAreaString",
                    offset,
                    header_size,
                )?
            };

            offset += header_size;
            if offset >= header.data_size as usize {
                break;
            }

            let string_data = string_header.read();

            let string = unsafe {
                RawMemoryFile::new_with_offset_and_size(
                    "FalconSharedMemoryAreaString",
                    offset,
                    string_data.length as usize,
                )?
            };
            let string = String::from_utf8_lossy(string.read());

            strings.insert(string_data.str_id, string.to_string());

            offset += string_data.length as usize + 1;
        }

        Ok(strings)
    }
}