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
//! AES Crypt file-header parsing.
//!
//! This module exposes [`read_version`], a fast, allocation-free reader for the
//! 3- to 5-byte fixed prefix shared by every AES Crypt file format version (v0–v3).
//! It is the cheapest way to triage a file before deciding whether to invoke
//! [`crate::decrypt()`].
//!
//! For the full header layout — including iteration count, public IV, extensions,
//! encrypted session block, and HMAC — see [`crate::decryption::read_file_version`]
//! (the strict version-prefix parser used by [`crate::decrypt()`]) and the
//! per-stage table in [`crate::decryption`].
use crateAescryptError;
use Read;
/// Reads the AES Crypt format version from the head of `reader` without decrypting.
///
/// `read_version` consumes the minimum number of bytes needed to identify the
/// format version (3 to 5 bytes depending on the version) and returns the
/// version number `0..=3`. It is the canonical entry point for batch tools that
/// need to triage `.aes` files before committing to a full
/// [`crate::decrypt()`] call.
///
/// # Format
///
/// All AES Crypt files start with the ASCII bytes `"AES"`. The bytes that follow
/// disambiguate the version:
///
/// | Bytes after `"AES"` | Detected version | Notes |
/// | ------------------- | :--------------: | --------------------------------------- |
/// | none (EOF) | `0` | Classic 3-byte v0 stub. |
/// | `\x00` (1 byte) | `0` | 4-byte v0 stub. |
/// | `\x00 \x00` | `0` | 5-byte v0 with zero modulo. |
/// | `\x00 X` (X any) | `0` | 5-byte v0; the modulo byte is ignored. |
/// | `\x01 \x00` | `1` | v1; reserved byte must be `0x00`. |
/// | `\x02 \x00` | `2` | v2; reserved byte must be `0x00`. |
/// | `\x03 \x00` | `3` | v3; reserved byte must be `0x00`. |
///
/// # Compatibility
///
/// This function only parses the version prefix. It does not validate extension
/// blocks, the iteration count, the public IV, the encrypted session block, or
/// the payload HMAC — all of which live further into the file and are validated
/// by [`crate::decrypt()`].
///
/// # Errors
///
/// - [`AescryptError::Io`] — the reader returned an error before the first three
/// bytes could be read (the trailing 1–2 bytes are read with `read`, not
/// `read_exact`, so EOF after `"AES"` is **not** an error and is reported as
/// v0).
/// - [`AescryptError::Header`] — the magic is not `b"AES"`, the version byte
/// exceeds `3`, or the reserved byte after a v1/v2/v3 version is not `0x00`.
///
/// # Panics
///
/// Never panics on valid or malformed input. The internal `unreachable!` is
/// guarded by a 2-byte buffer for which `Read::read` only returns `0..=2`.
///
/// # Security
///
/// `read_version` is deliberately permissive about the trailing v0 modulo byte
/// (it accepts any value) because legacy AES Crypt files in the wild are not
/// always padded with `0x00`. The reserved-byte check for v1–v3 is **strict**
/// (must be `0x00`) to reject crafted headers that hide payload behind an
/// otherwise-valid magic. No keys, IVs, or plaintext are touched.
///
/// # Thread Safety
///
/// `read_version` borrows the reader; it is `Send + Sync` whenever the reader is.
/// The function holds no shared state and is safe to call concurrently on
/// independent readers.
///
/// # Examples
///
/// ```
/// use aescrypt_rs::read_version;
/// use std::io::Cursor;
///
/// // v3 file header
/// let header = b"AES\x03\x00";
/// let version = read_version(Cursor::new(header))?;
/// assert_eq!(version, 3);
///
/// // v0 file header (3-byte)
/// let header = b"AES";
/// let version = read_version(Cursor::new(header))?;
/// assert_eq!(version, 0);
/// # Ok::<(), aescrypt_rs::AescryptError>(())
/// ```
///
/// # See also
///
/// - [`crate::decrypt()`] — full decryption pipeline.
/// - [`crate::decryption::read_file_version`] — internal version reader used by
/// [`crate::decrypt()`]; stricter than `read_version` (always reads 5 bytes).