use crate::bit::{BitReader, HandleRef};
use crate::core::error::{DwgError, ErrorKind};
use crate::core::result::Result;
use crate::dwg::version::DwgVersion;
use crate::objects::dictionary::{
decode_common_object_handles, resolve_handle_ref, version_at_least, CommonObjectHandles,
};
use crate::objects::handle::Handle;
#[derive(Debug, Clone, PartialEq)]
pub enum XDataValue {
String(String),
Real(f64),
Real2(f64),
Int16(i16),
Int32(i32),
Int64(i64),
Bool(bool),
Binary(Vec<u8>),
Handle(HandleRef),
Raw(Vec<u8>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct XDataGroup {
pub code: i16,
pub value: XDataValue,
}
#[derive(Debug, Clone, PartialEq)]
pub struct XRecord {
pub handle: Handle,
pub xdata_size: u32,
pub xdata: Vec<u8>,
pub groups: Vec<XDataGroup>,
pub cloning: Option<u16>,
pub num_reactors: u32,
pub is_xdic_missing: bool,
pub has_ds_data: bool,
pub owner_handle: Option<Handle>,
pub reactors: Vec<Handle>,
pub xdic_handle: Option<Handle>,
pub objid_handles: Vec<Handle>,
}
impl XRecord {
pub fn first_string(&self, code: i16) -> Option<&str> {
self.groups.iter().find_map(|g| {
if g.code == code {
if let XDataValue::String(s) = &g.value {
return Some(s.as_str());
}
}
None
})
}
pub fn first_i32(&self, code: i16) -> Option<i32> {
self.groups.iter().find_map(|g| {
if g.code == code {
if let XDataValue::Int32(v) = &g.value {
return Some(*v);
}
}
None
})
}
}
#[derive(Debug, Clone)]
pub struct XRecordDecodeCtx {
pub version: DwgVersion,
pub object_handle: u64,
}
pub fn decode_xrecord_data(
reader: &mut BitReader<'_>,
ctx: &XRecordDecodeCtx,
) -> Result<XRecordData> {
decode_xrecord_data_with_end(reader, ctx, None)
}
pub fn decode_xrecord_data_with_end(
reader: &mut BitReader<'_>,
ctx: &XRecordDecodeCtx,
data_section_end_bit: Option<u64>,
) -> Result<XRecordData> {
let xdata_size = reader.read_bl()?;
const MAX_XDATA: u32 = 16 * 1024 * 1024;
if xdata_size > MAX_XDATA {
return Err(DwgError::new(
ErrorKind::Format,
format!("XRECORD xdata_size {xdata_size} exceeds safe limit {MAX_XDATA}"),
));
}
let xdata = reader.read_rcs(xdata_size as usize)?;
let cloning = if version_at_least(&ctx.version, &DwgVersion::R2000) {
Some(reader.read_bs()?)
} else {
None
};
if let Some(end) = data_section_end_bit {
let now = reader.tell_bits();
let gap = end.saturating_sub(now);
if gap > 1 {
return Err(DwgError::new(
ErrorKind::Format,
format!(
"XRECORD data section ends at bit {now}, handle stream at {end} \
(gap {gap} bits; expected 0 or 1). Caller likely skipped the \
wrong preamble or mis-read xdata_size."
),
));
}
}
let r2007 = version_at_least(&ctx.version, &DwgVersion::R2007);
let groups = parse_xdata_groups_versioned(&xdata, r2007)?;
Ok(XRecordData {
xdata_size,
xdata,
groups,
cloning,
})
}
#[derive(Debug, Clone, PartialEq)]
pub struct XRecordData {
pub xdata_size: u32,
pub xdata: Vec<u8>,
pub groups: Vec<XDataGroup>,
pub cloning: Option<u16>,
}
pub fn decode_xrecord_handles(
handle_reader: &mut BitReader<'_>,
version: &DwgVersion,
object_handle: u64,
handle_stream_end_bits: u64,
data_section_common: Option<(u32, bool, bool)>,
) -> Result<XRecordHandles> {
let common = match data_section_common {
Some((num_reactors, is_xdic_missing, has_ds_data)) => {
crate::objects::dictionary::decode_common_object_handle_refs(
handle_reader,
version,
object_handle,
num_reactors,
is_xdic_missing,
has_ds_data,
)?
}
None => decode_common_object_handles(handle_reader, version, object_handle)?,
};
let mut objid_handles = Vec::new();
const MAX_OBJID: usize = 100_000;
while (handle_reader.tell_bits() as u64) < handle_stream_end_bits
&& objid_handles.len() < MAX_OBJID
{
let href = match handle_reader.read_h() {
Ok(h) => h,
Err(_) => break,
};
match resolve_handle_ref(&href, object_handle)? {
Some(h) if h != 0 => objid_handles.push(Handle(h)),
_ => break,
}
}
Ok(XRecordHandles {
common,
objid_handles,
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XRecordHandles {
pub common: CommonObjectHandles,
pub objid_handles: Vec<Handle>,
}
pub fn assemble_xrecord(object_handle: u64, data: XRecordData, handles: XRecordHandles) -> XRecord {
XRecord {
handle: Handle(object_handle),
xdata_size: data.xdata_size,
xdata: data.xdata,
groups: data.groups,
cloning: data.cloning,
num_reactors: handles.common.num_reactors,
is_xdic_missing: handles.common.is_xdic_missing,
has_ds_data: handles.common.has_ds_data,
owner_handle: handles.common.owner_handle,
reactors: handles.common.reactors,
xdic_handle: handles.common.xdic_handle,
objid_handles: handles.objid_handles,
}
}
pub fn decode_xrecord(
reader: &mut BitReader<'_>,
handle_reader: &mut BitReader<'_>,
ctx: &XRecordDecodeCtx,
handle_stream_end_bits: u64,
data_section_common: Option<(u32, bool, bool)>,
) -> Result<XRecord> {
let data = decode_xrecord_data(reader, ctx)?;
let handles = decode_xrecord_handles(
handle_reader,
&ctx.version,
ctx.object_handle,
handle_stream_end_bits,
data_section_common,
)?;
Ok(assemble_xrecord(ctx.object_handle, data, handles))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XDataParseStop {
CleanEnd,
InvalidCode { code: i16, byte_offset: usize },
TruncatedValue { code: i16, byte_offset: usize },
UnknownCode { code: i16, byte_offset: usize },
MaxGroups,
}
#[derive(Debug, Clone, PartialEq)]
pub struct XDataParseResult {
pub groups: Vec<XDataGroup>,
pub stop: XDataParseStop,
pub bytes_consumed: usize,
}
pub fn parse_xdata_groups(xdata: &[u8]) -> Result<Vec<XDataGroup>> {
parse_xdata_groups_versioned(xdata, true)
}
pub fn parse_xdata_groups_versioned(xdata: &[u8], r2007_plus: bool) -> Result<Vec<XDataGroup>> {
let detailed = parse_xdata_groups_detailed(xdata, r2007_plus);
match detailed.stop {
XDataParseStop::CleanEnd => Ok(detailed.groups),
XDataParseStop::InvalidCode { code, byte_offset } => Err(DwgError::new(
ErrorKind::Format,
format!("XDATA invalid group code {code} at byte {byte_offset}"),
)),
XDataParseStop::TruncatedValue { code, byte_offset } => Err(DwgError::new(
ErrorKind::Format,
format!("XDATA truncated value for group {code} at byte {byte_offset}"),
)),
XDataParseStop::UnknownCode { code, byte_offset } => Err(DwgError::new(
ErrorKind::Format,
format!("XDATA unknown group code {code} at byte {byte_offset}"),
)),
XDataParseStop::MaxGroups => Err(DwgError::new(
ErrorKind::Format,
"XDATA exceeded maximum group count".to_string(),
)),
}
}
pub fn parse_xdata_groups_detailed(xdata: &[u8], r2007_plus: bool) -> XDataParseResult {
let mut reader = BitReader::new(xdata);
reader.set_pos(0, 0);
let mut groups = Vec::new();
const MAX_GROUPS: usize = 1_000_000;
if xdata.is_empty() {
return XDataParseResult {
groups,
stop: XDataParseStop::CleanEnd,
bytes_consumed: 0,
};
}
loop {
let (byte_pos, bit_pos) = reader.get_pos();
if byte_pos >= xdata.len() {
return XDataParseResult {
groups,
stop: XDataParseStop::CleanEnd,
bytes_consumed: byte_pos,
};
}
if byte_pos + 2 > xdata.len() {
if bit_pos == 0 && byte_pos == xdata.len() {
return XDataParseResult {
groups,
stop: XDataParseStop::CleanEnd,
bytes_consumed: byte_pos,
};
}
if groups.is_empty() && byte_pos == 0 {
}
return XDataParseResult {
groups,
stop: if bit_pos == 0 && byte_pos == xdata.len() {
XDataParseStop::CleanEnd
} else {
XDataParseStop::TruncatedValue {
code: -1,
byte_offset: byte_pos,
}
},
bytes_consumed: byte_pos,
};
}
if groups.len() >= MAX_GROUPS {
return XDataParseResult {
groups,
stop: XDataParseStop::MaxGroups,
bytes_consumed: byte_pos,
};
}
let code = match reader.read_rs(crate::bit::Endian::Little) {
Ok(c) => c as i16,
Err(_) => {
return XDataParseResult {
groups,
stop: XDataParseStop::TruncatedValue {
code: -1,
byte_offset: byte_pos,
},
bytes_consumed: byte_pos,
};
}
};
if code < 0 || code >= 2000 {
return XDataParseResult {
groups,
stop: XDataParseStop::InvalidCode {
code,
byte_offset: byte_pos,
},
bytes_consumed: byte_pos,
};
}
let value = match read_xdata_value(&mut reader, code, r2007_plus) {
Ok(v) => v,
Err(e) => {
let msg = e.to_string();
let stop = if msg.contains("unknown XDATA group") {
XDataParseStop::UnknownCode {
code,
byte_offset: byte_pos,
}
} else {
XDataParseStop::TruncatedValue {
code,
byte_offset: byte_pos,
}
};
return XDataParseResult {
groups,
stop,
bytes_consumed: byte_pos,
};
}
};
groups.push(XDataGroup { code, value });
}
}
fn read_xdata_value(reader: &mut BitReader<'_>, code: i16, r2007_plus: bool) -> Result<XDataValue> {
let abs = code.unsigned_abs();
match abs {
0..=9 | 100..=104 | 300..=309 | 410..=419 | 430..=439 | 470..=479 | 999 => {
if r2007_plus {
let length = reader.read_rs(crate::bit::Endian::Little)? as usize;
if length > 32_767 {
return Err(DwgError::new(
ErrorKind::Format,
format!("XDATA string length {length} exceeds UCS-2 max"),
));
}
let mut chars = Vec::with_capacity(length);
for _ in 0..length {
let cu = reader.read_rs(crate::bit::Endian::Little)?;
if cu == 0 {
break;
}
if let Some(c) = char::from_u32(u32::from(cu)) {
chars.push(c);
}
}
Ok(XDataValue::String(chars.into_iter().collect()))
} else {
let length = reader.read_rs(crate::bit::Endian::Little)? as usize;
let _codepage = reader.read_rc()?;
let bytes = reader.read_rcs(length)?;
let s = String::from_utf8_lossy(&bytes)
.trim_end_matches('\0')
.to_string();
Ok(XDataValue::String(s))
}
}
10..=59 => {
let v = reader.read_rd(crate::bit::Endian::Little)?;
if abs <= 39 {
Ok(XDataValue::Real(v))
} else {
Ok(XDataValue::Real2(v))
}
}
60..=79 | 170..=179 | 270..=289 | 370..=379 | 400..=409 | 1060..=1070 => {
let v = reader.read_rs(crate::bit::Endian::Little)? as i16;
Ok(XDataValue::Int16(v))
}
90..=99 | 420..=429 | 440..=449 | 1071 => {
let v = reader.read_rl(crate::bit::Endian::Little)? as i32;
Ok(XDataValue::Int32(v))
}
160..=169 => {
let lo = reader.read_rl(crate::bit::Endian::Little)? as u64;
let hi = reader.read_rl(crate::bit::Endian::Little)? as u64;
let v = ((hi << 32) | lo) as i64;
Ok(XDataValue::Int64(v))
}
290..=299 => {
let v = reader.read_rc()? != 0;
Ok(XDataValue::Bool(v))
}
310..=319 => {
let len = reader.read_rc()? as usize;
let bytes = reader.read_rcs(len)?;
Ok(XDataValue::Binary(bytes))
}
320..=369 | 390..=399 => {
let lo = reader.read_rl(crate::bit::Endian::Little)? as u64;
let hi = reader.read_rl(crate::bit::Endian::Little)? as u64;
let absref = (hi << 32) | lo;
Ok(XDataValue::Handle(crate::bit::HandleRef {
code: 0,
counter: 8,
value: absref,
}))
}
_ => Err(DwgError::new(
ErrorKind::Format,
format!("unknown XDATA group code {code}"),
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bit::BitWriter;
#[test]
fn parse_r2007_ucs2_string_group() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 301).unwrap(); w.write_rs(crate::bit::Endian::Little, 4).unwrap(); for cu in [b'T' as u16, b'E' as u16, b'S' as u16, b'T' as u16] {
w.write_rs(crate::bit::Endian::Little, cu).unwrap();
}
let bytes = w.into_bytes();
let groups = parse_xdata_groups_versioned(&bytes, true).unwrap();
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].code, 301);
assert_eq!(groups[0].value, XDataValue::String("TEST".into()));
}
#[test]
fn parse_r2007_ucs2_german_sharp_s() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 8).unwrap(); w.write_rs(crate::bit::Endian::Little, 3).unwrap();
for cu in [0x004Du16, 0x0061u16, 0x00DFu16] {
w.write_rs(crate::bit::Endian::Little, cu).unwrap();
}
let bytes = w.into_bytes();
let groups = parse_xdata_groups_versioned(&bytes, true).unwrap();
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].value, XDataValue::String("Maß".into()));
}
#[test]
fn parse_pre_r2007_string_with_codepage() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 1).unwrap();
w.write_rs(crate::bit::Endian::Little, 4).unwrap(); w.write_rc(30).unwrap(); for b in b"TEST" {
w.write_rc(*b).unwrap();
}
let bytes = w.into_bytes();
let groups = parse_xdata_groups_versioned(&bytes, false).unwrap();
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].value, XDataValue::String("TEST".into()));
}
#[test]
fn parse_int32_group_90() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 90).unwrap();
w.write_rl(crate::bit::Endian::Little, 0x0000_07FF).unwrap();
let bytes = w.into_bytes();
let groups = parse_xdata_groups_versioned(&bytes, true).unwrap();
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].code, 90);
assert_eq!(groups[0].value, XDataValue::Int32(0x7FF));
}
#[test]
fn parse_mask_91_and_description_301() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 91).unwrap();
w.write_rl(crate::bit::Endian::Little, 2047).unwrap();
w.write_rs(crate::bit::Endian::Little, 301).unwrap();
w.write_rs(crate::bit::Endian::Little, 5).unwrap();
for cu in [b'L', b'a', b'y', b'e', b'r'].map(|b| b as u16) {
w.write_rs(crate::bit::Endian::Little, cu).unwrap();
}
let bytes = w.into_bytes();
let groups = parse_xdata_groups_versioned(&bytes, true).unwrap();
assert_eq!(groups.len(), 2);
assert_eq!(groups[0].value, XDataValue::Int32(2047));
assert_eq!(groups[1].value, XDataValue::String("Layer".into()));
}
#[test]
fn parse_errors_on_invalid_group_code() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 90).unwrap();
w.write_rl(crate::bit::Endian::Little, 1).unwrap();
w.write_rs(crate::bit::Endian::Little, 2500).unwrap(); let bytes = w.into_bytes();
let err = parse_xdata_groups_versioned(&bytes, true).unwrap_err();
assert!(err.to_string().contains("invalid group code"));
let detailed = parse_xdata_groups_detailed(&bytes, true);
assert_eq!(detailed.groups.len(), 1);
assert!(matches!(
detailed.stop,
XDataParseStop::InvalidCode { code: 2500, .. }
));
}
#[test]
fn parse_empty_xdata() {
assert!(parse_xdata_groups(&[]).unwrap().is_empty());
}
#[test]
fn xrecord_data_empty_payload_with_cloning() {
let mut w = BitWriter::new();
w.write_bb(0b10).unwrap(); w.write_bb(0b01).unwrap(); w.write_rc(1).unwrap(); let bytes = w.into_bytes();
let mut reader = BitReader::new(&bytes);
let ctx = XRecordDecodeCtx {
version: DwgVersion::R2018,
object_handle: 0x30,
};
let data = decode_xrecord_data(&mut reader, &ctx).unwrap();
assert_eq!(data.xdata_size, 0);
assert!(data.groups.is_empty());
assert_eq!(data.cloning, Some(1));
}
#[test]
fn parse_errors_on_unknown_group_code() {
let mut w = BitWriter::new();
w.write_rs(crate::bit::Endian::Little, 9999).unwrap(); let bytes = w.into_bytes();
assert!(parse_xdata_groups_versioned(&bytes, true).is_err());
}
}