Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

fix: decode ANSI folder aliases without corrupting Chinese names

hansu527c784d7514
diff --git a/crates/rf-win/src/encoding.rs b/crates/rf-win/src/encoding.rs
index 18650f68..e7f945b5 100644
--- a/crates/rf-win/src/encoding.rs
+++ b/crates/rf-win/src/encoding.rs
@@ -30,6 +30,12 @@ fn decode(raw: &[u8], cp: u32) -> Option<String> {
(n > 0).then(|| String::from_utf16_lossy(&out[..n as usize]))
}
+/// Complete INI values must not discard a trailing byte as a truncated preview.
+pub(crate) fn decode_ini_ansi(raw: &[u8], cp: u32) -> String {
+ std::str::from_utf8(raw).map(str::to_owned)
+ .unwrap_or_else(|_| decode(raw, cp).unwrap_or_else(|| String::from_utf8_lossy(raw).into_owned()))
+}
+
/// Text from bytes of unknown encoding: UTF-8 if valid (a truncated trailing sequence is
/// tolerated, since previews read only the head of a file), otherwise the legacy ANSI page.
pub fn decode_text(raw: &[u8]) -> String {
diff --git a/crates/rf-win/src/localized.rs b/crates/rf-win/src/localized.rs
index cd4d52d8..23f90dd9 100644
--- a/crates/rf-win/src/localized.rs
+++ b/crates/rf-win/src/localized.rs
@@ -95,13 +95,17 @@ pub fn is_network(p: &Path) -> bool {
unsafe { windows::Win32::Storage::FileSystem::GetDriveTypeW(PCWSTR(root.as_ptr())) == 4 }
}
-/// desktop.ini is UTF-16LE with a BOM, or the ANSI code page (ASCII in practice).
+/// Keep Unicode files intact; older shell tools write localized names in ANSI.
fn decode(raw: &[u8]) -> String {
+ decode_with_codepage(raw, crate::encoding::legacy_ansi_codepage())
+}
+
+fn decode_with_codepage(raw: &[u8], codepage: u32) -> String {
if raw.starts_with(&[0xFF, 0xFE]) {
let units: Vec<u16> = raw[2..].chunks_exact(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect();
String::from_utf16_lossy(&units)
} else {
- String::from_utf8_lossy(raw.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(raw)).into_owned()
+ crate::encoding::decode_ini_ansi(raw.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(raw), codepage)
}
}
@@ -181,6 +185,18 @@ pub fn expand_env(s: &str) -> String {
mod tests {
use super::*;
+ #[test]
+ fn chinese_ansi_alias_and_unicode_aliases_are_preserved() {
+ let mut gbk = b"[.ShellClassInfo]\r\nLocalizedResourceName=".to_vec();
+ // GBK bytes for a Chinese folder alias, independent of the test machine's locale.
+ gbk.extend_from_slice(&[0xD6, 0xD0, 0xCE, 0xC4]);
+ assert_eq!(resource_name(&decode_with_codepage(&gbk, 936)).as_deref(), Some("中文"));
+ let utf8 = "[.ShellClassInfo]\r\nLocalizedResourceName=中文";
+ assert_eq!(resource_name(&decode_with_codepage(utf8.as_bytes(), 936)).as_deref(), Some("中文"));
+ let utf16: Vec<u8> = [0xff, 0xfe].into_iter().chain(utf8.encode_utf16().flat_map(u16::to_le_bytes)).collect();
+ assert_eq!(resource_name(&decode_with_codepage(&utf16, 936)).as_deref(), Some("中文"));
+ }
+
#[test]
fn parses_ini() {
let ini = "\r\n[.ShellClassInfo]\r\nLocalizedResourceName=@%SystemRoot%\\system32\\shell32.dll,-21813\r\n";