Adjacent \X2\...\X0\ runs, as step_out writes one per character, now decode: the backslash opening the next run was pushed as a literal, so a name exported as 圆角 came back as 圆\X2\89D2\X0\ (also every linked component insert, which round-trips through STEP). STEP and IGES bytes that are not UTF-8 are decoded in their detected legacy encoding (chardetng + encoding_rs) instead of from_utf8_lossy, so GBK or Shift_JIS names from vendor files no longer turn into U+FFFD. Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
Generated
+13
@@ -813,6 +813,17 @@ dependencies = [
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chardetng"
|
||||
version = "0.1.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14b8f0b65b7b08ae3c8187e8d77174de20cb6777864c6b832d8ad365999cf1ea"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"encoding_rs",
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.45"
|
||||
@@ -4198,6 +4209,8 @@ dependencies = [
|
||||
"cadmpeg-core",
|
||||
"cadmpeg-ir",
|
||||
"cadmpeg-registry",
|
||||
"chardetng",
|
||||
"encoding_rs",
|
||||
"gltf",
|
||||
"log",
|
||||
"quick-xml",
|
||||
|
||||
@@ -55,6 +55,11 @@ rayon = "1.12"
|
||||
# packages (3MF)
|
||||
zip = { version = "9", default-features = false, features = ["deflate"] }
|
||||
quick-xml = "0.41"
|
||||
# STEP text in a legacy encoding (GBK, Shift_JIS, …) instead of Part 21 escapes (#2344): detect
|
||||
# and decode it. Both MIT OR Apache-2.0 (encoding_rs adds BSD-3-Clause data); encoding_rs is
|
||||
# already in the tree through cadmpeg-codec-step.
|
||||
chardetng = "0.1"
|
||||
encoding_rs = "0.8"
|
||||
# glTF 2.0 import (MIT OR Apache-2.0): the document model and GLB container only; buffers are
|
||||
# read by crates/io itself, so no image decoding or file access comes with it.
|
||||
gltf = { version = "1.4", default-features = false, features = ["names"] }
|
||||
|
||||
@@ -20,6 +20,8 @@ gltf = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
chardetng = { workspace = true }
|
||||
encoding_rs = { workspace = true }
|
||||
|
||||
# A plain fsync where a network share refuses Apple's full flush (src/safe_write.rs); already in
|
||||
# the tree through tempfile.
|
||||
|
||||
+51
-1
@@ -155,6 +155,35 @@ pub struct StepFeature {
|
||||
pub import: std::sync::Arc<solidcraft_kernel::StepImport>,
|
||||
}
|
||||
|
||||
/// Bytes the encoding detector sees at most: the lines with 8-bit bytes, which in a STEP or IGES
|
||||
/// file are the names and descriptions.
|
||||
const ENCODING_SAMPLE_BYTES: usize = 64 << 10;
|
||||
|
||||
/// STEP or IGES bytes as text. UTF-8 stays as it is. Some exporters write names in the system's
|
||||
/// legacy encoding instead of Part 21 escapes (GBK from Chinese vendors, Shift_JIS from Japanese
|
||||
/// ones): that encoding is detected from the lines holding 8-bit bytes and the file decoded with
|
||||
/// it, so a name reads `圆角` rather than U+FFFD noise (#2344). The syntax itself is ASCII in every
|
||||
/// such encoding.
|
||||
fn step_text(bytes: &[u8]) -> String {
|
||||
if let Ok(text) = std::str::from_utf8(bytes) {
|
||||
return text.to_owned();
|
||||
}
|
||||
let mut detector = chardetng::EncodingDetector::new();
|
||||
let mut fed = 0;
|
||||
for line in bytes.split(|b| *b == b'\n').filter(|line| !line.is_ascii()) {
|
||||
if fed >= ENCODING_SAMPLE_BYTES {
|
||||
break;
|
||||
}
|
||||
let line = line.get(..line.len().min(ENCODING_SAMPLE_BYTES - fed)).unwrap_or_default();
|
||||
detector.feed(line, false);
|
||||
detector.feed(b"\n", false);
|
||||
fed += line.len() + 1;
|
||||
}
|
||||
detector.feed(&[], true);
|
||||
let encoding = detector.guess(None, false);
|
||||
encoding.decode_without_bom_handling(bytes).0.into_owned()
|
||||
}
|
||||
|
||||
/// Read STEP bytes (`file` is the source file name) into an Import base feature. The file is
|
||||
/// read once here to fail early and collect names; the feature holds the STEP text.
|
||||
pub fn step_import_feature(bytes: &[u8], file: &str) -> Result<StepFeature> {
|
||||
@@ -171,7 +200,7 @@ pub fn step_import_feature_named(bytes: &[u8], file: &str, names: StepNames) ->
|
||||
let (text, iges_warnings) = if is_native_cad_path(file) {
|
||||
native_cad_to_step(bytes, file).map_err(IoError::Invalid)?
|
||||
} else {
|
||||
let text = String::from_utf8_lossy(bytes).into_owned();
|
||||
let text = step_text(bytes);
|
||||
if is_iges_path(file) { solidcraft_kernel::iges_to_step(&text).map_err(IoError::Invalid)? } else { (text, Vec::new()) }
|
||||
};
|
||||
let imp = solidcraft_kernel::step_import_shared(&text)?;
|
||||
@@ -748,6 +777,27 @@ mod tests {
|
||||
assert!(!model_xml(export(&m.state(), &[], Format::ThreeMf, "t").unwrap()).contains("Title"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_names_in_a_legacy_encoding() {
|
||||
// Exporters that write names in the system encoding instead of Part 21 escapes (#2344).
|
||||
let (_, m) = model();
|
||||
let step = export(&m.state(), &[], Format::Step, "t").unwrap();
|
||||
let at = step.windows(7).position(|w| w == b"'Body1'").unwrap();
|
||||
for (encoding, name) in [(encoding_rs::GBK, "圆角20"), (encoding_rs::SHIFT_JIS, "ネジ穴の面取り"), (encoding_rs::WINDOWS_1252, "Gehäuse")]
|
||||
{
|
||||
let mut bytes = step[..at + 1].to_vec();
|
||||
bytes.extend_from_slice(&encoding.encode(name).0);
|
||||
bytes.extend_from_slice(&step[at + 6..]);
|
||||
assert!(std::str::from_utf8(&bytes).is_err(), "{name}: the test file is not UTF-8");
|
||||
assert_eq!(step_import_feature(&bytes, "t.step").unwrap().body_names, [name], "{}", encoding.name());
|
||||
}
|
||||
// UTF-8 names are read as they are.
|
||||
let mut bytes = step[..at + 1].to_vec();
|
||||
bytes.extend_from_slice("圆角".as_bytes());
|
||||
bytes.extend_from_slice(&step[at + 6..]);
|
||||
assert_eq!(step_import_feature(&bytes, "t.step").unwrap().body_names, ["圆角"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_and_design() {
|
||||
let (doc, m) = model();
|
||||
|
||||
@@ -348,6 +348,7 @@ fn decode_string(raw: &[u8]) -> String {
|
||||
i += 5;
|
||||
continue;
|
||||
}
|
||||
let mut run = false;
|
||||
for (tag, width) in [(&b"\\X2\\"[..], 4usize), (&b"\\X4\\"[..], 8)] {
|
||||
if rest.starts_with(tag)
|
||||
&& let Some(end) = rest.windows(4).position(|w| w == b"\\X0\\")
|
||||
@@ -361,20 +362,23 @@ fn decode_string(raw: &[u8]) -> String {
|
||||
out.extend(units.iter().map(|u| char::from_u32(*u).unwrap_or('?')));
|
||||
}
|
||||
i += end + 4;
|
||||
run = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if i < raw.len() && raw.get(i) == Some(&b'\\') {
|
||||
if rest.starts_with(b"\\S\\")
|
||||
&& let Some(&ch) = rest.get(3)
|
||||
{
|
||||
out.push(char::from_u32(ch as u32 + 128).unwrap_or('?'));
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
out.push('\\');
|
||||
i += 1;
|
||||
// A run may be followed straight away by the backslash of the next escape (#2344).
|
||||
if run {
|
||||
continue;
|
||||
}
|
||||
if rest.starts_with(b"\\S\\")
|
||||
&& let Some(&ch) = rest.get(3)
|
||||
{
|
||||
out.push(char::from_u32(ch as u32 + 128).unwrap_or('?'));
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
out.push('\\');
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// Plain bytes: keep UTF-8 runs intact.
|
||||
@@ -556,5 +560,8 @@ mod tests {
|
||||
assert_eq!(decode_string(b"\\X\\E9"), "\u{e9}");
|
||||
assert_eq!(decode_string(b"a\\X2\\00E900E8\\X0\\b"), "a\u{e9}\u{e8}b");
|
||||
assert_eq!(decode_string(b"\\"), "\\");
|
||||
// One run per character, as `step_out` writes them (#2344).
|
||||
assert_eq!(decode_string(b"\\X2\\5706\\X0\\\\X2\\89D2\\X0\\20"), "\u{5706}\u{89d2}20");
|
||||
assert_eq!(decode_string(b"\\X4\\0001F600\\X0\\\\X2\\00E9\\X0\\\\S\\a"), "\u{1f600}\u{e9}\u{e1}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1854,6 +1854,11 @@ mod tests {
|
||||
let p = ex.get(1).unwrap().params();
|
||||
assert_eq!(p[0].as_str(), Some("a'b\\é"));
|
||||
assert_eq!(p[1].as_f64(), Some(1e-6));
|
||||
// Names of several non-ASCII characters in a row read back whole (#2344).
|
||||
for name in ["圆角", "凸台-拉伸7", "ネジ穴", "Gehäuse 😀"] {
|
||||
let ex = p21::parse(&format!("ISO-10303-21;DATA;#1=A({});ENDSEC;END-ISO-10303-21;", string(name))).unwrap();
|
||||
assert_eq!(ex.get(1).unwrap().params()[0].as_str(), Some(name));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user