From 848404a2d2b6d5dc2c9c653f0bc8ac9588d2f620 Mon Sep 17 00:00:00 2001 From: Eduardo Pinho Date: Sat, 5 Oct 2024 19:38:38 +0100 Subject: [PATCH] [object] adjust meta information group length calculation - account for possible padding in optional attributes --- object/src/meta.rs | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/object/src/meta.rs b/object/src/meta.rs index e1b16cc11..522126c2a 100644 --- a/object/src/meta.rs +++ b/object/src/meta.rs @@ -447,32 +447,32 @@ impl FileMetaTable { + self .implementation_version_name .as_ref() - .map(|s| 8 + s.len() as u32) + .map(|s| 8 + dicom_len(s)) .unwrap_or(0) + self .source_application_entity_title .as_ref() - .map(|s| 8 + s.len() as u32) + .map(|s| 8 + dicom_len(s)) .unwrap_or(0) + self .sending_application_entity_title .as_ref() - .map(|s| 8 + s.len() as u32) + .map(|s| 8 + dicom_len(s)) .unwrap_or(0) + self .receiving_application_entity_title .as_ref() - .map(|s| 8 + s.len() as u32) + .map(|s| 8 + dicom_len(s)) .unwrap_or(0) + self .private_information_creator_uid .as_ref() - .map(|s| 8 + s.len() as u32) + .map(|s| 8 + dicom_len(s)) .unwrap_or(0) + self .private_information .as_ref() - .map(|x| 12 + x.len() as u32) + .map(|x| 12 + ((x.len() as u32 + 1) & !1)) .unwrap_or(0) } @@ -1383,4 +1383,34 @@ mod tests { "1.2.840.10008.5.1.4.1.1.7", ); } + + /// writing file meta information and reading it back + /// should not fail and the the group length should be the same + #[test] + fn write_read_does_not_fail() { + let mut table = FileMetaTable { + information_group_length: 0, + information_version: [0u8, 1u8], + media_storage_sop_class_uid: "1.2.840.10008.5.1.4.1.1.7".to_owned(), + media_storage_sop_instance_uid: "2.25.137731752600317795446120660167595746868".to_owned(), + transfer_syntax: "1.2.840.10008.1.2.4.91".to_owned(), + implementation_class_uid: "2.25.305828488182831875890203105390285383139".to_owned(), + implementation_version_name: Some("MYTOOL100".to_owned()), + source_application_entity_title: Some("RUSTY".to_owned()), + receiving_application_entity_title: None, + sending_application_entity_title: None, + private_information_creator_uid: None, + private_information: None, + }; + + table.update_information_group_length(); + + let mut buf = vec![b'D', b'I', b'C', b'M']; + table.write(&mut buf).unwrap(); + + let table2 = FileMetaTable::from_reader(&mut buf.as_slice()) + .expect("Should not fail to read the table from the written data"); + + assert_eq!(table.information_group_length, table2.information_group_length); + } }