unbanished_formats/save/
events.rs1use super::*;
4
5#[derive(Debug, Clone, PartialEq)]
8pub struct SavedEvent {
9 pub color: u32,
11 pub text: String,
13 pub kind: i32,
15 pub severity: i32,
17 pub cell: i32,
19 pub category: i32,
21 pub icon: i32,
23}
24
25#[derive(Debug, Clone)]
28pub struct SavedEventLog {
29 pub notify: [bool; 6],
31 pub entries: Vec<SavedEvent>,
33}
34
35pub(super) fn skip_rest(r: &mut Reader) {
36 let rest = r.remaining();
37 let _ = r.bytes(rest);
38}
39
40pub(super) fn parse_event_block(b: &[u8]) -> Option<SavedEventLog> {
41 let u32_at = |o: usize| {
42 b.get(o..o + 4)
43 .map(|s| u32::from_le_bytes(s.try_into().unwrap()))
44 };
45 let size = u32_at(4)? as usize;
47 if size < 14 || u32_at(8 + size)? != 0xbbbb_bbbb || u32_at(8)? != 1 {
48 return None;
49 }
50 let body = &b[8..8 + size];
51 if body[4..10].iter().any(|&v| v > 1) {
52 return None;
53 }
54 let notify: [bool; 6] = std::array::from_fn(|i| body[4 + i] == 1);
55 let mut o = 10;
56 let rd = |o: &mut usize| -> Option<u32> {
57 let v = body.get(*o..*o + 4)?;
58 *o += 4;
59 Some(u32::from_le_bytes(v.try_into().unwrap()))
60 };
61 let count = rd(&mut o)? as usize;
62 if count > 100_000 {
63 return None;
64 }
65 let mut entries = Vec::with_capacity(count);
66 for _ in 0..count {
67 let color = rd(&mut o)?;
68 let len = rd(&mut o)?;
69 let text = if len & 0x8000_0000 != 0 {
72 let n = (len & 0x7fff_ffff) as usize;
73 let text = body.get(o..o + n.checked_sub(1)?)?;
74 o += n;
75 String::from_utf8_lossy(text).into_owned()
76 } else {
77 let n = len as usize;
78 let units: Vec<u16> = body
79 .get(o..o + 2 * n.checked_sub(1)?)?
80 .chunks_exact(2)
81 .map(|c| u16::from_le_bytes([c[0], c[1]]))
82 .collect();
83 o += 2 * n;
84 String::from_utf16_lossy(&units)
85 };
86 let mut i = || rd(&mut o).map(|v| v as i32);
87 entries.push(SavedEvent {
88 color,
89 text,
90 kind: i()?,
91 severity: i()?,
92 cell: i()?,
93 category: i()?,
94 icon: i()?,
95 });
96 }
97 (o == body.len()).then_some(SavedEventLog { notify, entries })
98}