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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::error::streamtyped::StreamTypedError;
const START_PATTERN: [u8; 2] = [0x0001, 0x002b];
const END_PATTERN: [u8; 2] = [0x0086, 0x0084];
pub fn parse(mut stream: Vec<u8>) -> Result<String, StreamTypedError> {
for idx in 0..stream.len() {
if idx + 2 > stream.len() {
return Err(StreamTypedError::NoStartPattern);
}
let part = &stream[idx..idx + 2];
if part == START_PATTERN {
stream.drain(..idx + 2);
break;
}
}
for idx in 1..stream.len() {
if idx >= stream.len() - 2 {
return Err(StreamTypedError::NoEndPattern);
}
let part = &stream[idx..idx + 2];
if part == END_PATTERN {
stream.truncate(idx);
break;
}
}
match String::from_utf8(stream)
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
{
Ok(string) => drop_chars(1, string),
Err(string) => drop_chars(3, string),
}
}
fn drop_chars(offset: usize, mut string: String) -> Result<String, StreamTypedError> {
let (position, _) = string
.char_indices()
.nth(offset)
.ok_or(StreamTypedError::InvalidPrefix)?;
string.drain(..position);
Ok(string)
}
#[cfg(test)]
mod tests {
use std::env::current_dir;
use std::fs::File;
use std::io::Read;
use std::vec;
use crate::util::streamtyped::{drop_chars, parse};
#[test]
fn test_parse_text_clean() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/AttributedBodyTextOnly");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "Noter test".to_string();
assert_eq!(parsed, expected);
}
#[test]
fn test_parse_text_space() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/AttributedBodyTextOnly2");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "Test 3".to_string();
assert_eq!(parsed, expected);
}
#[test]
fn test_parse_text_weird_font() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/WeirdText");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "𝖍𝖊𝖑𝖑𝖔 𝖜𝖔𝖗𝖑𝖉".to_string();
assert_eq!(parsed, expected);
}
#[test]
fn test_parse_text_url() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/URL");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "https://github.com/ReagentX/Logria".to_string();
assert_eq!(parsed, expected);
}
#[test]
fn test_parse_text_multi_part() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/MultiPart");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "\u{FFFC}test 1\u{FFFC}test 2 \u{FFFC}test 3".to_string();
assert_eq!(parsed, expected);
}
#[test]
fn test_parse_text_app() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/ExtraData");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes).unwrap();
let expected = "This is parsing";
assert_eq!(&parsed[..expected.len()], expected);
}
#[test]
fn test_parse_text_blank() {
let plist_path = current_dir()
.unwrap()
.as_path()
.join("test_data/streamtyped/Blank");
let mut file = File::open(plist_path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let parsed = parse(bytes);
assert!(&parsed.is_err());
}
#[test]
fn test_can_drop_chars() {
assert_eq!(
drop_chars(1, String::from("Hello world")).unwrap(),
String::from("ello world")
);
}
#[test]
fn test_can_drop_chars_none() {
assert_eq!(
drop_chars(0, String::from("Hello world")).unwrap(),
String::from("Hello world")
);
}
#[test]
fn test_cant_drop_all() {
assert!(drop_chars(1000, String::from("Hello world")).is_err());
}
}