ezdwg-core 0.22.0

Pure Rust DWG reader and native AC1015 writer
Documentation
use std::collections::HashMap;

use crate::container::section_directory::{SectionDirectory, SectionKind, SectionLocatorRecord};
use crate::container::section_loader;
use crate::core::config::ParseConfig;
use crate::core::error::{DwgError, ErrorKind};
use crate::core::result::Result;
use crate::io::ByteReader;
use crate::objects::{Handle, ObjectRef};

#[derive(Debug, Clone)]
pub struct ObjectIndex {
    pub objects: Vec<ObjectRef>,
    by_handle: HashMap<Handle, usize>,
}

impl ObjectIndex {
    pub fn get(&self, handle: Handle) -> Option<&ObjectRef> {
        self.by_handle.get(&handle).map(|idx| &self.objects[*idx])
    }

    pub fn len(&self) -> usize {
        self.objects.len()
    }

    pub fn is_empty(&self) -> bool {
        self.objects.is_empty()
    }

    pub fn from_objects(objects: Vec<ObjectRef>) -> Self {
        let mut by_handle = HashMap::with_capacity(objects.len());
        for (idx, obj) in objects.iter().enumerate() {
            let should_replace = match by_handle.get(&obj.handle).copied() {
                Some(prev_idx) => {
                    let prev_offset = objects
                        .get(prev_idx)
                        .map(|prev: &ObjectRef| prev.offset)
                        .unwrap_or(0);
                    obj.offset > prev_offset
                }
                None => true,
            };
            if should_replace {
                by_handle.insert(obj.handle, idx);
            }
        }
        Self { objects, by_handle }
    }
}

pub fn build_object_index(bytes: &[u8], config: &ParseConfig) -> Result<ObjectIndex> {
    let directory = crate::container::section_directory::parse_with_config(bytes, config)?;
    build_object_index_from_directory(bytes, &directory, config)
}

pub fn build_object_index_from_directory(
    bytes: &[u8],
    directory: &SectionDirectory,
    config: &ParseConfig,
) -> Result<ObjectIndex> {
    let record = find_object_map_record(directory).ok_or_else(|| {
        DwgError::new(
            ErrorKind::Format,
            "object map section not found in section directory",
        )
    })?;
    let section = section_loader::load_section(bytes, record, config)?;
    parse_object_map(section.data.as_ref(), config)
}

fn find_object_map_record(directory: &SectionDirectory) -> Option<SectionLocatorRecord> {
    directory
        .records
        .iter()
        .find(|record| record.kind() == SectionKind::ObjectMap)
        .cloned()
}

fn parse_object_map(bytes: &[u8], _config: &ParseConfig) -> Result<ObjectIndex> {
    let mut reader = ByteReader::new(bytes);
    let mut objects = Vec::new();

    // Assigned at each Handles-page boundary before first use (absolute first
    // handleoff/offset). Leave uninitialised so rustc does not warn about a
    // dead initial write that is always overwritten.
    let mut last_handle: u64;
    let mut last_offset: i64;
    loop {
        if reader.remaining() < 2 {
            break;
        }
        let section_size = read_u16_be(&mut reader)? as usize;
        if section_size == 2 {
            break;
        }
        if section_size < 2 {
            return Err(DwgError::new(
                ErrorKind::Format,
                format!("invalid object map block size {section_size}"),
            ));
        }
        if reader.remaining() < section_size - 2 {
            return Err(DwgError::new(
                ErrorKind::Format,
                "object map block exceeds remaining data",
            )
            .with_offset(reader.tell()));
        }

        let start = reader.tell();
        // Each Handles page encodes the first handleoff as an *absolute*
        // handle (and the first offset as absolute). LibreDWG seeks by
        // offset and then reads the body handle; when we materialise the
        // map itself we must restart the handle accumulator at 0 per page
        // or page-start absolute handleoffs are added on top of the
        // previous page's last handle (observed on AC1032 multi-page AC1032 maps: from
        // object 548 onward every map handle was wrong while offsets
        // stayed exact).
        last_handle = 0;
        last_offset = 0;
        let _ = _config;

        while (reader.tell() - start) < (section_size as u64 - 2) {
            let delta_handle = read_unsigned_modular_char(&mut reader)?;
            let delta_offset = read_modular_char(&mut reader)?;
            last_handle = last_handle.checked_add(delta_handle).ok_or_else(|| {
                DwgError::new(ErrorKind::Format, "object map handle overflow")
                    .with_offset(reader.tell())
            })?;
            last_offset = last_offset.checked_add(delta_offset).ok_or_else(|| {
                DwgError::new(ErrorKind::Format, "object map offset overflow")
                    .with_offset(reader.tell())
            })?;
            if last_offset < 0 {
                return Err(DwgError::new(
                    ErrorKind::Format,
                    "object map contains negative handle/offset",
                )
                .with_offset(reader.tell()));
            }
            if last_offset > u32::MAX as i64 {
                return Err(DwgError::new(
                    ErrorKind::Format,
                    "object map offset exceeds u32 range",
                )
                .with_offset(reader.tell()));
            }
            objects.push(ObjectRef {
                handle: Handle(last_handle),
                offset: last_offset as u32,
            });
        }

        // CRC (big-endian) - currently ignored
        if reader.remaining() < 2 {
            break;
        }
        let _crc = read_u16_be(&mut reader)?;
    }

    Ok(ObjectIndex::from_objects(objects))
}

fn read_u16_be(reader: &mut ByteReader<'_>) -> Result<u16> {
    let hi = reader.read_u8()? as u16;
    let lo = reader.read_u8()? as u16;
    Ok((hi << 8) | lo)
}

/// Object-map handle offsets are unsigned modular chars: the ODA specification
/// notes that "there is no negation used; handles in the object map are always
/// in increasing order". Decoding them with the signed reader turned every
/// terminating byte with bit 0x40 set into a negative delta, which shifted the
/// handles of all following entries in that section (and dropped entries as
/// "negative"), so objects were indexed under the wrong handles.
///
/// A value runs until the byte without the continuation bit. A handle is an
/// unsigned 64-bit value, so a delta can take up to ten bytes: nine groups of
/// seven bits and one last bit. A drawing whose handles jump above 2^35 writes
/// six or more; one with handles at or above 2^63 writes ten. Stopping early
/// left the rest of the value unread, and every following entry and page of
/// the map was read out of step ("object map block exceeds remaining data").
/// The R2004 and R2007 object maps use the same two readers.
pub(crate) fn read_unsigned_modular_char(reader: &mut ByteReader<'_>) -> Result<u64> {
    let mut value: u64 = 0;

    for group in 0..10 {
        let byte = reader.read_u8()?;
        let bits = (byte & 0x7F) as u64;
        // The tenth group holds bit 63 only.
        if group == 9 && bits > 1 {
            break;
        }
        value |= bits << (group * 7);
        if (byte & 0x80) == 0 {
            return Ok(value);
        }
    }
    Err(
        DwgError::new(ErrorKind::Format, "object map handle delta is too long")
            .with_offset(reader.tell()),
    )
}

/// Signed modular char: the last byte carries the sign in bit 0x40. As with the
/// unsigned form, the value runs until the byte without the continuation bit;
/// an offset delta of 32 bits takes five bytes.
pub(crate) fn read_modular_char(reader: &mut ByteReader<'_>) -> Result<i64> {
    let mut value: i64 = 0;
    let mut shift = 0;

    for _ in 0..9 {
        let mut byte = reader.read_u8()?;
        if (byte & 0x80) == 0 {
            let negative = (byte & 0x40) != 0;
            if negative {
                byte &= 0xBF;
            }
            value |= (byte as i64) << shift;
            if negative {
                value = -value;
            }
            return Ok(value);
        }
        byte &= 0x7F;
        value |= (byte as i64) << shift;
        shift += 7;
    }
    Err(
        DwgError::new(ErrorKind::Format, "object map offset delta is too long")
            .with_offset(reader.tell()),
    )
}

#[cfg(test)]
mod tests {
    use super::{parse_object_map, ObjectIndex};
    use crate::core::config::ParseConfig;
    use crate::objects::{Handle, ObjectRef};

    #[test]
    fn object_index_prefers_highest_offset_for_duplicate_handles() {
        let index = ObjectIndex::from_objects(vec![
            ObjectRef {
                handle: Handle(237),
                offset: 527_255,
            },
            ObjectRef {
                handle: Handle(237),
                offset: 12_157,
            },
            ObjectRef {
                handle: Handle(237),
                offset: 204_892,
            },
        ]);

        let resolved = index.get(Handle(237)).expect("duplicate-handle entry");
        assert_eq!(resolved.offset, 527_255);
    }

    #[test]
    fn parse_multiblock_object_map_handle_and_offset_reset_per_block() {
        // Each Handles page (≈2032 bytes) encodes the first handleoff as an
        // absolute handle and the first offset as absolute. Within a page,
        // both accumulate as signed/unsigned modular-char deltas. Verified
        // against LibreDWG -v5 dumps of AC1032 multi-page AC1032 maps: continuing the handle
        // accumulator across pages doubled absolute page-start handleoffs
        // onto the previous page's last handle (object 548+).
        let bytes = vec![
            0x00, 0x06, // block 1: 2-byte header + 4-byte payload
            0x01, 0x0A, // +1, +10
            0x02, 0x04, // +2, +4
            0x00, 0x00, // crc
            0x00, 0x06, // block 2: handle + offset both restart at 0
            0x07, 0x08, // abs 7, +8
            0x02, 0x03, // +2, +3
            0x00, 0x00, // crc
            0x00, 0x02, // terminator block
        ];
        let config = ParseConfig::default();
        let index = parse_object_map(&bytes, &config).expect("index");
        let refs: Vec<(u64, u32)> = index
            .objects
            .iter()
            .map(|obj| (obj.handle.0, obj.offset))
            .collect();
        // handle: 1, 3, then reset -> 7, 9
        // offset: 10, 14, then reset -> 8, 11
        assert_eq!(refs, vec![(1, 10), (3, 14), (7, 8), (9, 11)]);
    }

    #[test]
    fn handle_deltas_longer_than_five_bytes_keep_the_map_in_step() {
        // Handles above 2^35 need a six-byte delta. Reading only five bytes left
        // the sixth for the offset and shifted every later entry and page.
        let bytes = vec![
            0x00, 0x0E, // block 1: 2-byte header + 12-byte payload
            0x05, 0x0A, // handle 5, offset 10
            // +0xF_FFFF_FFFA (36 bits) in six bytes, offset +4
            0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x04, //
            0x01, 0x82, 0x01, // +1, offset +130 (two bytes)
            0x00, 0x00, // crc
            0x00, 0x04, // block 2
            0x07, 0x08, // abs 7, +8
            0x00, 0x00, // crc
            0x00, 0x02, // terminator block
        ];
        let config = ParseConfig::default();
        let index = parse_object_map(&bytes, &config).expect("index");
        let refs: Vec<(u64, u32)> = index
            .objects
            .iter()
            .map(|obj| (obj.handle.0, obj.offset))
            .collect();
        assert_eq!(
            refs,
            vec![(5, 10), (0xF_FFFF_FFFF, 14), (0x10_0000_0000, 144), (7, 8)]
        );
        // A value whose continuation bit never clears is an error, not a
        // silent stop in the middle of the value.
        let endless = vec![
            0x00, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
            0x00, 0x00, 0x00, 0x02,
        ];
        assert!(parse_object_map(&endless, &config).is_err());
    }
}