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
//! CP949 ↔ UTF-8 helpers for GRF filenames.
//!
//! Ragnarok Online's `.grf` archives store filenames in CP949 — the
//! Microsoft Korean codepage, a superset of EUC-KR with extra
//! private-use mappings. Filenames seen on disk are CP949 bytes; the
//! fstool `Filesystem` trait surface is UTF-8 strings. Conversion
//! happens once at the GRF boundary (during table parse / write).
//!
//! We use `encoding_rs::EUC_KR`. The WHATWG `euc-kr` label is
//! intentionally defined to be CP949 for web-compatibility — the spec
//! mapping is the union we want.
use Cow;
use EUC_KR;
/// Decode a CP949 byte slice into UTF-8. Stray bytes that don't map
/// (corruption, truncation) are replaced with U+FFFD — we don't
/// surface decoding errors because GRFs in the wild ship occasional
/// junk filenames and refusing the whole archive helps nobody.
/// Encode a UTF-8 string back to CP949 bytes. Characters outside
/// CP949 are emitted as numeric character references (`&#NNNN;`)
/// because that's what `encoding_rs` does for unmappable codepoints
/// — practical for round-trip with our own reader since we'll just
/// decode them back to the references. Filenames are normally ASCII
/// or Hangul, so this path is rare.