steam-vdf-parser 0.1.1

Zero-copy parser for Steam's VDF (Valve Data Format) files
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Error types for VDF parsing.

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

/// Result type for VDF operations.
pub type Result<T> = core::result::Result<T, Error>;

/// Create a parse error with truncated input snippet (max 50 chars).
///
/// The snippet is limited to 50 characters to keep error messages manageable.
///
/// # Parameters
/// - `input`: The input string being parsed
/// - `offset`: Byte offset where the error occurred
/// - `context`: Description of what was expected
pub fn parse_error(input: &str, offset: usize, context: impl Into<String>) -> Error {
    let snippet = input.chars().take(50).collect::<String>();
    Error::ParseError {
        input: snippet,
        offset,
        context: context.into(),
    }
}

/// Errors that can occur during VDF parsing.
#[derive(Debug)]
pub enum Error {
    /// Binary format errors
    /// ---------------------

    /// Invalid magic number in binary VDF header.
    InvalidMagic {
        /// The magic number that was found.
        found: u32,
        /// Expected magic numbers for this format.
        expected: &'static [u32],
    },

    /// Unknown type byte encountered.
    UnknownType {
        /// The type byte that was found.
        type_byte: u8,
        /// Offset in the input where this occurred.
        offset: usize,
    },

    /// Invalid string index into the string table.
    InvalidStringIndex {
        /// The index that was requested.
        index: usize,
        /// The maximum valid index.
        max: usize,
    },

    /// Unexpected end of input while parsing.
    UnexpectedEndOfInput {
        /// Description of what was being read.
        context: &'static str,
        /// Offset in the input where this occurred.
        offset: usize,
        /// Expected minimum number of bytes.
        expected: usize,
        /// Actual number of bytes available.
        actual: usize,
    },

    /// Invalid UTF-8 sequence in binary data.
    InvalidUtf8 {
        /// Offset where the error occurred.
        offset: usize,
    },

    /// Invalid UTF-16 sequence in binary data.
    InvalidUtf16 {
        /// Offset where the error occurred.
        offset: usize,
        /// Position of the unpaired surrogate.
        position: usize,
    },

    /// Text format errors
    /// ------------------

    /// Parse error with context.
    ParseError {
        /// A snippet of the input near the error.
        input: String,
        /// Offset in the input where this occurred.
        offset: usize,
        /// Context describing what was expected.
        context: String,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidMagic { found, expected } => {
                let expected_str: String = expected
                    .iter()
                    .map(|v| format!("0x{:08x}", v))
                    .collect::<Vec<_>>()
                    .join(" or ");
                write!(
                    f,
                    "Invalid magic number: expected {}, found 0x{:08x}",
                    expected_str, found
                )
            }
            Error::UnknownType { type_byte, offset } => {
                write!(
                    f,
                    "Unknown type byte 0x{:02x} at offset {}",
                    type_byte, offset
                )
            }
            Error::InvalidStringIndex { index, max } => {
                if *max == 0 {
                    write!(f, "Invalid string index {}: string table is empty", index)
                } else {
                    write!(
                        f,
                        "Invalid string index {}: string table has {} entries (valid range: 0..{})",
                        index, max, max
                    )
                }
            }
            Error::UnexpectedEndOfInput {
                context,
                offset,
                expected,
                actual,
            } => {
                write!(
                    f,
                    "Unexpected end of input at offset {} while {}: expected {} bytes, found {}",
                    offset, context, expected, actual
                )
            }
            Error::InvalidUtf8 { offset } => {
                write!(f, "Invalid UTF-8 sequence at offset {}", offset)
            }
            Error::InvalidUtf16 { offset, position } => {
                write!(
                    f,
                    "Invalid UTF-16 sequence at offset {} (surrogate position {})",
                    offset, position
                )
            }
            Error::ParseError {
                input,
                offset,
                context,
            } => {
                let snippet = if input.len() > 50 {
                    format!("{}...", &input[..50])
                } else {
                    input.clone()
                };
                write!(
                    f,
                    "Parse error at offset {}: {} (near: \"{}\")",
                    offset, context, snippet
                )
            }
        }
    }
}

impl core::error::Error for Error {}

impl Error {
    /// Adjusts the offset in error variants that contain position information.
    ///
    /// This is used to add a base offset when parsing from a sub-slice,
    /// converting relative offsets to absolute offsets in the original input.
    fn with_offset(mut self, base: usize) -> Self {
        match &mut self {
            Error::UnexpectedEndOfInput { offset, .. } => *offset += base,
            Error::InvalidUtf8 { offset } => *offset += base,
            Error::InvalidUtf16 { offset, .. } => *offset += base,
            Error::UnknownType { offset, .. } => *offset += base,
            Error::ParseError { offset, .. } => *offset += base,
            // Other variants don't have offsets to adjust
            Error::InvalidMagic { .. } | Error::InvalidStringIndex { .. } => {}
        }
        self
    }
}

/// Returns a closure that adds an offset to an error.
///
/// This is used with `.map_err()` to adjust error offsets when parsing from sub-slices.
pub(crate) fn with_offset(base: usize) -> impl Fn(Error) -> Error {
    move |err| err.with_offset(base)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::binary::{APPINFO_MAGIC_40, APPINFO_MAGIC_41};
    use alloc::format;
    use alloc::string::ToString;

    #[test]
    fn test_error_display_invalid_magic() {
        let err = Error::InvalidMagic {
            found: 0xDEADBEEF,
            expected: &[APPINFO_MAGIC_40, APPINFO_MAGIC_41],
        };
        let msg = format!("{}", err);
        assert!(msg.contains("0xdeadbeef"));
        assert!(msg.contains("0x07564428"));
    }

    #[test]
    fn test_error_display_unknown_type() {
        let err = Error::UnknownType {
            type_byte: 0xFF,
            offset: 42,
        };
        let msg = format!("{}", err);
        assert!(msg.contains("0xff"));
        assert!(msg.contains("42"));
    }

    #[test]
    fn test_error_display_invalid_string_index() {
        let err = Error::InvalidStringIndex { index: 5, max: 3 };
        let msg = format!("{}", err);
        assert!(msg.contains("5"));
        assert!(msg.contains("3"));
    }

    #[test]
    fn test_error_display_invalid_string_index_empty() {
        let err = Error::InvalidStringIndex { index: 0, max: 0 };
        let msg = format!("{}", err);
        assert!(msg.contains("empty"));
    }

    #[test]
    fn test_error_display_unexpected_end_of_input() {
        let err = Error::UnexpectedEndOfInput {
            context: "reading string",
            offset: 10,
            expected: 4,
            actual: 2,
        };
        let msg = format!("{}", err);
        assert!(msg.contains("reading string"));
        assert!(msg.contains("10"));
        assert!(msg.contains("expected 4"));
        assert!(msg.contains("found 2"));
    }

    #[test]
    fn test_error_display_invalid_utf8() {
        let err = Error::InvalidUtf8 { offset: 15 };
        let msg = format!("{}", err);
        assert!(msg.contains("15"));
    }

    #[test]
    fn test_error_display_invalid_utf16() {
        let err = Error::InvalidUtf16 {
            offset: 20,
            position: 3,
        };
        let msg = format!("{}", err);
        assert!(msg.contains("20"));
        assert!(msg.contains("3"));
    }

    #[test]
    fn test_error_display_parse_error() {
        let err = Error::ParseError {
            input: "some very long input that should be truncated in the message".to_string(),
            offset: 5,
            context: "expected quote".to_string(),
        };
        let msg = format!("{}", err);
        assert!(msg.contains("5"));
        assert!(msg.contains("expected quote"));
        // The input should be truncated to 50 chars, so the message shouldn't be too long
        assert!(msg.len() < 150);
    }

    #[test]
    fn test_error_with_offset_unexpected_end() {
        let err = Error::UnexpectedEndOfInput {
            context: "test",
            offset: 10,
            expected: 4,
            actual: 2,
        };
        let adjusted = err.with_offset(100);
        match adjusted {
            Error::UnexpectedEndOfInput { offset, .. } => {
                assert_eq!(offset, 110);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_error_with_offset_invalid_utf8() {
        let err = Error::InvalidUtf8 { offset: 5 };
        let adjusted = err.with_offset(100);
        match adjusted {
            Error::InvalidUtf8 { offset } => {
                assert_eq!(offset, 105);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_error_with_offset_invalid_utf16() {
        let err = Error::InvalidUtf16 {
            offset: 10,
            position: 2,
        };
        let adjusted = err.with_offset(100);
        match adjusted {
            Error::InvalidUtf16 { offset, position } => {
                assert_eq!(offset, 110);
                assert_eq!(position, 2);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_error_with_offset_unknown_type() {
        let err = Error::UnknownType {
            type_byte: 0x42,
            offset: 7,
        };
        let adjusted = err.with_offset(100);
        match adjusted {
            Error::UnknownType { type_byte, offset } => {
                assert_eq!(type_byte, 0x42);
                assert_eq!(offset, 107);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_error_with_offset_parse_error() {
        let err = Error::ParseError {
            input: "test".to_string(),
            offset: 3,
            context: "context".to_string(),
        };
        let adjusted = err.with_offset(100);
        match adjusted {
            Error::ParseError { offset, .. } => {
                assert_eq!(offset, 103);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_error_with_offset_no_change_for_non_offset_variants() {
        let err = Error::InvalidMagic {
            found: 0x12345678,
            expected: &[APPINFO_MAGIC_40],
        };
        let adjusted = err.with_offset(100);
        // InvalidMagic doesn't have an offset field, so it should be unchanged
        match adjusted {
            Error::InvalidMagic { found, .. } => {
                assert_eq!(found, 0x12345678);
            }
            _ => panic!("Unexpected error type"),
        }
    }

    #[test]
    fn test_parse_error_truncates_long_input() {
        let long_input = "a".repeat(100);
        let err = parse_error(&long_input, 0, "test context");
        match err {
            Error::ParseError { input, .. } => {
                assert!(input.len() <= 50, "Input should be truncated to 50 chars");
            }
            _ => panic!("Expected ParseError variant"),
        }
    }

    #[test]
    fn test_with_offset_closure() {
        let base_offset = 100;
        let f = with_offset(base_offset);
        let err = Error::InvalidUtf8 { offset: 5 };
        let adjusted = f(err);
        match adjusted {
            Error::InvalidUtf8 { offset } => {
                assert_eq!(offset, 105);
            }
            _ => panic!("Unexpected error type"),
        }
    }
}