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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Handling of file's metadata
//!
//! See <https://www.acid.org/info/sauce/sauce.htm>
//!

use chrono::NaiveDate;
use std::{
    fs::File,
    io::{Read, Seek, SeekFrom},
    str,
};

use crate::cp437::*;

/// A structure representing a file's metadata
#[doc(alias = "Sauce")]
#[derive(Clone)]
pub struct Meta {
    /// The image's title
    pub title: String,
    /// The image's author
    pub author: String,
    /// The image author's team or group
    #[doc(alias = "team")]
    pub group: String,
    /// The image creation date, in the YYYYMMDD format
    pub date: String,
    /// The size of the file, sans this metadata
    pub size: u32,
    /// The type of this file
    ///
    /// Only supported values are
    /// * `(1, 0)` → `Character/ASCII`
    /// * `(1, 1)` → `Character/ANSI`
    ///
    /// See <https://www.acid.org/info/sauce/sauce.htm#FileType>
    ///
    pub r#type: (u8, u8),
    /// Width of the image
    pub width: u16,
    /// Height of the image
    pub height: u16,
    /// A bitfield of flags that define how to process an image
    ///
    /// See <https://www.acid.org/info/sauce/sauce.htm#ANSiFlags>
    ///
    #[doc(alias = "AR")]
    #[doc(alias = "aspect ratio")]
    #[doc(alias = "LS")]
    #[doc(alias = "letter spacing")]
    #[doc(alias = "B")]
    #[doc(alias = "ice colour")]
    #[doc(alias = "non-blink mode")]
    pub flags: u8,
    /// The name of the font this image uses
    ///
    /// Only IBM VGA is supported.
    ///
    pub font: String,
    /// A list of comments on this image
    #[doc(alias = "comments")]
    pub notes: Vec<String>,
}

/// Get a file's metadata via its path
///
/// Arguments:
/// * `path`: Path pointing to file. Can be relative to cwd.
///
pub fn get(path: &str) -> Result<Option<Meta>, String> {
    return read(&mut File::open(path).map_err(|x| return x.to_string())?);
}

/// Get a file's metadata via a file reference
///
/// Arguments:
/// * `file`: File to read
///
pub fn read(file: &mut File) -> Result<Option<Meta>, String> {
    return read_raw(file).map(|x| {
        return x.map(|meta| {
            return Meta {
                title: meta[meta.len() - 121..meta.len() - 86]
                    .iter()
                    .map(|x| return CP437_TO_UTF8[*x as usize])
                    .collect::<String>()
                    .trim()
                    .to_string(),
                author: meta[meta.len() - 86..meta.len() - 66]
                    .iter()
                    .map(|x| return CP437_TO_UTF8[*x as usize])
                    .collect::<String>()
                    .trim()
                    .to_string(),
                group: meta[meta.len() - 66..meta.len() - 46]
                    .iter()
                    .map(|x| return CP437_TO_UTF8[*x as usize])
                    .collect::<String>()
                    .trim()
                    .to_string(),
                date: meta[meta.len() - 46..meta.len() - 38]
                    .iter()
                    .map(|x| return CP437_TO_UTF8[*x as usize])
                    .collect::<String>()
                    .trim()
                    .to_string(),
                size: u32::from_le_bytes(
                    meta[meta.len() - 38..meta.len() - 34].try_into().unwrap(),
                ),
                r#type: (meta[meta.len() - 34], meta[meta.len() - 33]),
                width: u16::from_le_bytes(
                    meta[meta.len() - 32..meta.len() - 30].try_into().unwrap(),
                ),
                height: u16::from_le_bytes(
                    meta[meta.len() - 30..meta.len() - 28].try_into().unwrap(),
                ),
                flags: meta[meta.len() - 23],
                font: meta[meta.len() - 22..]
                    .iter()
                    .map(|x| return CP437_TO_UTF8[*x as usize])
                    .collect::<String>()
                    .trim()
                    .to_string(),
                notes: (0..meta[meta.len() - 24] as usize)
                    .rev()
                    .map(|i| {
                        let offset = meta.len() - (i + 3) * 64;
                        return meta[offset..offset + 64]
                            .iter()
                            .map(|x| return CP437_TO_UTF8[*x as usize])
                            .collect::<String>()
                            .trim()
                            .to_string();
                    })
                    .collect(),
            };
        });
    });
}

/// Check that a given file's metadata is valid and supported
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check(meta: &Option<Meta>) -> Result<(), String> {
    check_title(meta)?;
    check_author(meta)?;
    check_group(meta)?;
    check_date(meta)?;
    check_type(meta)?;
    check_flags(meta)?;
    check_font(meta)?;
    check_notes(meta)?;

    return Ok(());
}

/// Check that the title is valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_title(meta: &Option<Meta>) -> Result<(), String> {
    return meta
        .as_ref()
        .map(|m| return check_str(&m.title, "Title", 35))
        .unwrap_or(Ok(()));
}

/// Check that the author is valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_author(meta: &Option<Meta>) -> Result<(), String> {
    return meta
        .as_ref()
        .map(|m| return check_str(&m.author, "Author", 20))
        .unwrap_or(Ok(()));
}

/// Check that the group is valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_group(meta: &Option<Meta>) -> Result<(), String> {
    return meta
        .as_ref()
        .map(|m| return check_str(&m.group, "Group", 20))
        .unwrap_or(Ok(()));
}

/// Check that the date is valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_date(meta: &Option<Meta>) -> Result<(), String> {
    if let Some(m) = meta {
        if ![0, 8].contains(&m.date.len()) {
            return Err(format!(
                "Date length is wrong (expected =8, got {})",
                m.date.len()
            ));
        } else if let Err(e) = NaiveDate::parse_from_str(&m.date, "%Y%m%d") {
            return Err(format!("Date is wrong ({})", e));
        }
    }

    return Ok(());
}

/// Check that the type is valid and supported
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_type(meta: &Option<Meta>) -> Result<(), String> {
    if let Some(m) = meta {
        if m.r#type.0 != 1 {
            return Err(format!(
                "Type is unsupported ({})",
                match m.r#type.0 {
                    // 0 => String::from("None"),
                    // 1 => String::from("Character"),
                    2 => String::from("Bitmap"),
                    3 => String::from("Vector"),
                    4 => String::from("Audio"),
                    5 => String::from("BinaryText"),
                    6 => String::from("XBin"),
                    7 => String::from("Archive"),
                    8 => String::from("Executable"),
                    _ => format!("Unknown {}", m.r#type.0),
                }
            ));
        } else if ![0, 1].contains(&m.r#type.1) {
            return Err(format!(
                "Type is unsupported ({})",
                match m.r#type.0 {
                    // 0 => String::from("Character/ASCII"),
                    // 1 => String::from("Character/ANSi"),
                    2 => String::from("Character/ANSiMation"),
                    3 => String::from("Character/RIPScript"),
                    4 => String::from("Character/PCBoardt"),
                    5 => String::from("Character/Avatar"),
                    6 => String::from("Character/HTML"),
                    7 => String::from("Character/Source"),
                    8 => String::from("Character/TundraDraw"),
                    _ => format!("Character/Unknown {}", m.r#type.1),
                }
            ));
        }
    }

    return Ok(());
}

/// Check that the flags are valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_flags(meta: &Option<Meta>) -> Result<(), String> {
    if let Some(m) = meta {
        if m.flags & 0x01 == 0x00 {
            // Only intended to support iCE colours
            return Err(String::from("Blink mode is unsupported"));
        } else if m.flags & 0x06 == 0x06 {
            return Err(String::from("Invalid letter spacing"));
        } else if m.flags & 0x18 == 0x18 {
            return Err(String::from("Invalid aspect ratio"));
        } else if m.flags > 0x1F {
            return Err(String::from("Invalid flags"));
        }
    }

    return Ok(());
}

/// Check that the font is valid and supported
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_font(meta: &Option<Meta>) -> Result<(), String> {
    if let Some(m) = meta {
        if !["IBM VGA", "IBM VGA 437", ""].contains(&m.font.as_str()) {
            // IBM VGA is by far the most common font, haven't even tried to
            // support any others.
            return Err(format!("Font is unsupported ({})", m.font));
        }
    }

    return Ok(());
}

/// Check that the notes are valid
///
/// Arguments:
/// * `meta`: The metadata to check
///
pub fn check_notes(meta: &Option<Meta>) -> Result<(), String> {
    if let Some(m) = meta {
        for (i, note) in m.notes.iter().enumerate() {
            check_str(
                &note,
                &format!(
                    "Notes[{:0width$}]",
                    i,
                    width = (m.notes.len() as f32).log10().ceil() as usize
                ),
                64,
            )?;
        }
    }

    return Ok(());
}

fn check_str(string: &String, name: &str, max_length: usize) -> Result<(), String> {
    if string.len() > max_length {
        return Err(format!(
            "{} is too long (expected <={}, got {})",
            name,
            max_length,
            string.len()
        ));
    } else if !string.chars().all(valid_char) {
        return Err(format!("{} contains illegal characters", name,));
    }

    return Ok(());
}

fn valid_char(c: char) -> bool {
    return UTF8_TO_CP437[0x20..=0xFE]
        .keys()
        .cloned()
        .collect::<Vec<char>>()
        .contains(&c);
}

fn read_raw(file: &mut File) -> Result<Option<Vec<u8>>, String> {
    let mut sauce = vec![0; 128];
    file.seek(SeekFrom::End(-128))
        .map_err(|x| return x.to_string())?;
    file.read_exact(&mut sauce)
        .map_err(|x| return x.to_string())?;

    if &sauce[..7] != "SAUCE00".as_bytes() {
        return Ok(None);
    } else {
        let offset = sauce[104] as usize * 64 + (if sauce[104] > 0 { 134 } else { 129 });
        file.seek(SeekFrom::End(-(offset as i64)))
            .map_err(|x| return x.to_string())?;
        let mut raw = vec![0; offset];
        file.read_exact(&mut raw)
            .map_err(|x| return x.to_string())?;
        if raw[0] != 0x1A || (offset > 129 && &raw[1..6] != "COMNT".as_bytes()) {
            return Ok(None);
        }
        return Ok(Some(raw));
    }
}