eq-network 0.1.2

Native EverQuest login, world, and zone client
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
411
412
413
414
415
416
417
418
419
420
421
use anyhow::{ensure, Context, Result};
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, BTreeSet},
    fs,
    io::Read,
    path::{Path, PathBuf},
};

/// Measured CRC-32 and size for one client file.
#[derive(Debug, Serialize, Deserialize)]
pub struct FileChecksum {
    /// CRC-32 expected by server-side file validation.
    pub crc32: u32,
    /// File length in bytes.
    pub size: u64,
}

/// Checksum inventory for one patched client installation.
#[derive(Debug, Serialize, Deserialize)]
pub struct Assets {
    /// Human-readable client profile represented by the inventory.
    pub client: String,
    /// Case-normalized filename to measured checksum, or `None` when explicitly unknown.
    pub files: BTreeMap<String, Option<FileChecksum>>,
}

/// A manifest response and the unscanned filenames that received checksum zero.
#[derive(Debug, Eq, PartialEq)]
pub struct FileResponse {
    /// Encoded manifest hash and per-file CRC values.
    pub body: Vec<u8>,
    /// Requested filenames that were absent from the inventory.
    pub unknown_files: Vec<String>,
}

impl Assets {
    /// Load the checksum inventory compiled into the library without filesystem access.
    ///
    /// # Errors
    ///
    /// Returns an error if the bundled inventory cannot be decoded.
    pub fn bundled() -> Result<Self> {
        serde_json::from_slice(include_bytes!("../assets.json"))
            .context("parse built-in asset inventory")
    }

    /// Inventory installed validation files, zone/model archives, and zone lists.
    ///
    /// # Errors
    ///
    /// Returns an error when the installation cannot be read or a discovered
    /// inventory entry is invalid.
    pub fn scan_all(directory: &Path) -> Result<Self> {
        let mut names: BTreeSet<String> = include_str!("../protocol/p99-v62-files.txt")
            .lines()
            .map(str::to_ascii_lowercase)
            .collect();
        for (name, path) in directory_entries(directory)? {
            if path.is_file()
                && [".s3d", ".eqg", "_chr.txt", "_assets.txt"]
                    .iter()
                    .any(|suffix| name.ends_with(suffix))
            {
                names.insert(name);
            }
        }
        Self::scan(directory, &names.into_iter().collect::<Vec<_>>())
    }

    /// Measure requested files in the installation, omitting files that are absent.
    ///
    /// # Errors
    ///
    /// Returns an error when a requested name is unsafe or a present file cannot
    /// be read.
    pub fn scan(directory: &Path, names: &[String]) -> Result<Self> {
        let installed = directory_entries(directory)?;
        let resources = if names
            .iter()
            .any(|name| name.eq_ignore_ascii_case("GlobalLoad.txt"))
        {
            installed
                .get("resources")
                .map(|path| directory_entries(path))
                .transpose()?
        } else {
            None
        };
        let mut files = BTreeMap::new();
        for name in names {
            ensure!(
                valid_asset_name(name),
                "asset inventory entries must be filenames"
            );
            let name = name.to_ascii_lowercase();
            let path = if name == "globalload.txt" {
                resources.as_ref().and_then(|files| files.get(&name))
            } else {
                installed.get(&name)
            };
            let Some(path) = path else {
                continue;
            };
            let mut file = fs::File::open(path)?;
            let size = file.metadata()?.len();
            let mut buffer = vec![0; 65536];
            let mut crc = crc32fast::Hasher::new();
            loop {
                let count = file.read(&mut buffer)?;
                if count == 0 {
                    break;
                }
                crc.update(&buffer[..count]);
            }
            files.insert(
                name,
                Some(FileChecksum {
                    crc32: crc.finalize(),
                    size,
                }),
            );
        }
        Ok(Self {
            client: "Titanium/P99-V62".into(),
            files,
        })
    }

    /// Build the checksum response requested by a world or zone manifest.
    /// Unknown files receive zero and are returned for caller diagnostics;
    /// the server decides whether that response is sufficient for admission.
    ///
    /// # Errors
    ///
    /// Returns an error when the manifest is malformed.
    pub fn file_response(&self, manifest: &[u8]) -> Result<FileResponse> {
        let mut output = crc32fast::hash(manifest).to_le_bytes().to_vec();
        let mut unknown_files = Vec::new();
        for entry in parse_manifest(manifest)? {
            if entry.is_skipped() {
                continue;
            }
            let crc = match self.files.get(&entry.name.to_ascii_lowercase()) {
                Some(Some(checksum)) => checksum.crc32,
                Some(None) => 0,
                None => {
                    unknown_files.push(entry.name);
                    0
                }
            };
            output.extend_from_slice(&entry.id.to_le_bytes());
            output.extend_from_slice(&crc.to_le_bytes());
        }
        Ok(FileResponse {
            body: output,
            unknown_files,
        })
    }

    /// Return the spell-file metadata used by the V62 CRC1 response.
    ///
    /// # Errors
    ///
    /// Returns an error when the inventory has no measured spell file.
    pub fn spells(&self) -> Result<&FileChecksum> {
        self.files
            .get("spells_us.txt")
            .and_then(Option::as_ref)
            .context("spells_us.txt missing from asset inventory")
    }
}

/// Index filenames once so a full installation scan does not repeatedly list it.
fn directory_entries(directory: &Path) -> Result<BTreeMap<String, PathBuf>> {
    let mut entries = BTreeMap::new();
    for entry in fs::read_dir(directory)? {
        let entry = entry?;
        let name = entry.file_name().to_string_lossy().to_ascii_lowercase();
        ensure!(
            entries.insert(name, entry.path()).is_none(),
            "ambiguous case-insensitive filename in asset directory"
        );
    }
    Ok(entries)
}

fn valid_asset_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 200
        && name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'.' || b == b'_')
        && name != "."
        && name != ".."
}

/// One validated request from a V62 file manifest.
#[derive(Debug, Eq, PartialEq)]
pub struct ManifestEntry {
    /// Server-provided identifier echoed in the response.
    pub id: u16,
    /// Manifest behavior flags.
    pub flags: u8,
    /// Validated filename without path components.
    pub name: String,
}

impl ManifestEntry {
    const fn is_skipped(&self) -> bool {
        self.flags == 2
    }
}

/// Parse and validate a V62 file manifest without accepting path components.
///
/// # Errors
///
/// Returns an error for truncated entries, unsafe filenames, unsupported flags,
/// duplicate identifiers, or an empty manifest.
pub fn parse_manifest(mut bytes: &[u8]) -> Result<Vec<ManifestEntry>> {
    let mut entries = Vec::new();
    while !bytes.is_empty() {
        ensure!(bytes.len() >= 4, "truncated file manifest");
        let id = u16::from_le_bytes([bytes[0], bytes[1]]);
        let flags = bytes[2];
        ensure!(
            matches!(flags, 1 | 2 | 5 | 9),
            "unsupported file manifest flags"
        );
        let end = bytes[3..]
            .iter()
            .position(|&b| b == 0)
            .context("unterminated manifest filename")?
            + 3;
        let name = std::str::from_utf8(&bytes[3..end])?;
        ensure!(
            !name.is_empty()
                && name.len() <= 200
                && name
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b == b'.' || b == b'_'),
            "invalid manifest filename"
        );
        ensure!(
            !entries.iter().any(|entry: &ManifestEntry| entry.id == id),
            "duplicate manifest ID"
        );
        entries.push(ManifestEntry {
            id,
            flags,
            name: name.to_owned(),
        });
        bytes = &bytes[end + 1..];
    }
    ensure!(!entries.is_empty(), "empty file manifest");
    Ok(entries)
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Installation(PathBuf);

    impl Installation {
        fn new() -> Self {
            let path =
                std::env::temp_dir().join(format!("p99-assets-{:016x}", rand::random::<u64>()));
            fs::create_dir(&path).unwrap();
            Self(path)
        }

        fn write(&self, name: &str, body: &[u8]) {
            let path = self.0.join(name);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, body).unwrap();
        }
    }

    impl Drop for Installation {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    fn manifest(entries: &[(u16, u8, &str)]) -> Vec<u8> {
        let mut bytes = Vec::new();
        for (id, flags, name) in entries {
            bytes.extend_from_slice(&id.to_le_bytes());
            bytes.push(*flags);
            bytes.extend_from_slice(name.as_bytes());
            bytes.push(0);
        }
        bytes
    }

    #[test]
    fn discovers_measured_assets_without_absent_or_personal_files() {
        let install = Installation::new();
        install.write("NewZone.S3D", b"zone archive");
        install.write("NewZone_chr.txt", b"2\nabc,missing_chr\ndef,present\n");
        install.write("NewZone_assets.txt", b"missing_object.eqg\n");
        install.write("Present.EQG", b"model archive");
        install.write("AnotherZone.eqg", b"new format zone");
        install.write("resources/GLOBALLOAD.TXT", b"global model list");
        install.write("GlobalLoad.txt", b"incorrect root copy");
        install.write("eqclient.ini", b"private settings");
        install.write("eqlog_ExampleCharacter.txt", b"private messages");
        install.write("notes.txt", b"private notes");

        let assets = Assets::scan_all(&install.0).unwrap();
        for (name, content) in [
            ("newzone.s3d", b"zone archive".as_slice()),
            ("present.eqg", b"model archive".as_slice()),
            ("anotherzone.eqg", b"new format zone".as_slice()),
            ("globalload.txt", b"global model list".as_slice()),
        ] {
            let file = assets.files[name].as_ref().unwrap();
            assert_eq!(file.crc32, crc32fast::hash(content));
            assert_eq!(file.size, content.len() as u64);
        }
        for name in [
            "newzone_2_obj.s3d",
            "newzone.eqg",
            "anotherzone_chr.txt",
            "missing_chr.s3d",
            "missing_chr.eqg",
            "missing_object.eqg",
        ] {
            assert!(!assets.files.contains_key(name), "{name}");
        }
        assert!(assets.files.values().all(Option::is_some));
        assert!(assets.files.contains_key("newzone_chr.txt"));
        assert!(assets.files.contains_key("newzone_assets.txt"));
        for name in ["eqclient.ini", "eqlog_examplecharacter.txt", "notes.txt"] {
            assert!(!assets.files.contains_key(name), "{name}");
        }
    }

    #[test]
    fn responds_only_to_requested_files_with_server_ids_and_manifest_order() {
        let install = Installation::new();
        install.write("FirstZone.s3d", b"first zone");
        install.write("SecondZone.s3d", b"second zone");
        let mut assets = Assets::scan_all(&install.0).unwrap();
        // Older inventories may still explicitly mark absent files with null.
        assets.files.insert("secondzone_assets.txt".into(), None);
        let request = manifest(&[
            (42, 1, "SECONDZONE.S3D"),
            (8, 9, "secondzone_assets.txt"),
            (99, 2, "unscanned_skipped_file.eqg"),
        ]);
        let mut expected = crc32fast::hash(&request).to_le_bytes().to_vec();
        expected.extend_from_slice(&42_u16.to_le_bytes());
        expected.extend_from_slice(&crc32fast::hash(b"second zone").to_le_bytes());
        expected.extend_from_slice(&8_u16.to_le_bytes());
        expected.extend_from_slice(&0_u32.to_le_bytes());
        let response = assets.file_response(&request).unwrap();
        assert_eq!(response.body, expected);
        assert!(response.unknown_files.is_empty());
        assets.files.remove("secondzone_assets.txt");
        let response = assets.file_response(&request).unwrap();
        assert_eq!(response.body, expected);
        assert_eq!(response.unknown_files, ["secondzone_assets.txt"]);
    }

    #[test]
    fn unknown_files_receive_zero_without_losing_known_checksums_or_inventory_state() {
        let install = Installation::new();
        install.write("Present.s3d", b"present");
        let mut assets = Assets::scan(&install.0, &["present.s3d".into()]).unwrap();
        assets.files.insert("absent.eqg".into(), None);
        let request = manifest(&[
            (17, 1, "UNKNOWN.EQG"),
            (22, 5, "PRESENT.S3D"),
            (9, 9, "absent.eqg"),
            (31, 2, "skipped.eqg"),
        ]);
        let response = assets.file_response(&request).unwrap();
        let mut expected = crc32fast::hash(&request).to_le_bytes().to_vec();
        for (id, crc) in [(17u16, 0u32), (22, crc32fast::hash(b"present")), (9, 0)] {
            expected.extend(id.to_le_bytes());
            expected.extend(crc.to_le_bytes());
        }
        assert_eq!(response.body, expected);
        assert_eq!(response.unknown_files, ["UNKNOWN.EQG"]);
        assert_eq!(assets.files.len(), 2);
        assert!(!assets.files.contains_key("unknown.eqg"));
    }

    #[test]
    fn zero_fallback_still_rejects_malformed_manifests() {
        let assets = Assets {
            client: "test".into(),
            files: BTreeMap::new(),
        };
        for invalid in [
            vec![],
            vec![1, 0, 1],
            manifest(&[(1, 3, "unknown.eqg")]),
            manifest(&[(1, 1, "../unknown.eqg")]),
            manifest(&[(1, 1, "first.eqg"), (1, 1, "second.eqg")]),
        ] {
            assert!(assets.file_response(&invalid).is_err());
        }
    }

    #[test]
    fn explicit_scan_is_limited_to_requested_filenames() {
        let install = Installation::new();
        install.write("NewZone.s3d", b"zone");
        install.write("OtherZone.s3d", b"other");
        let assets =
            Assets::scan(&install.0, &["newzone.s3d".into(), "missing.eqg".into()]).unwrap();
        assert_eq!(assets.files.len(), 1);
        assert!(assets.files["newzone.s3d"].is_some());
        assert!(!assets.files.contains_key("missing.eqg"));
        assert!(Assets::scan(&install.0, &["../outside.s3d".into()]).is_err());
    }
}