qualia_core_db/medical/dicom_dataset/
header.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum DatasetError {
3 NotPart10,
4 Truncated,
5 Overflow,
6 ByteLimit,
7 IndexLimit,
8 DepthLimit,
9 ElementLimit,
10 ItemLimit,
11 WorkLimit,
12 MalformedHeader,
13 MalformedDelimiter,
14 Boundary,
15 Duplicate(u32),
16 Order,
17 Missing(u32),
18 Invalid(u32),
19 UnsupportedSyntax,
20 UnsupportedImplicitSequence,
21 NotSequence,
22 InvalidId,
23}
24impl std::fmt::Display for DatasetError {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "DICOM dataset: {self:?}")
27 }
28}
29impl std::error::Error for DatasetError {}
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub enum TransferSyntax {
32 ImplicitLittleEndian,
33 ExplicitLittleEndian,
34 EncapsulatedExplicitLittleEndian,
35}
36#[derive(Clone, Copy, Debug)]
37pub struct ParseLimits {
38 pub max_bytes: usize,
39 pub max_index_bytes: usize,
40 pub max_elements: usize,
41 pub max_items: usize,
42 pub max_depth: usize,
43 pub max_work: usize,
44}
45impl Default for ParseLimits {
46 fn default() -> Self {
47 Self {
48 max_bytes: 256 * 1024 * 1024,
49 max_index_bytes: 2 * 1024 * 1024,
50 max_elements: 8192,
51 max_items: 4096,
52 max_depth: 32,
53 max_work: 32768,
54 }
55 }
56}
57#[derive(Clone, Copy)]
58pub(super) struct Header {
59 pub tag: u32,
60 pub vr: Option<[u8; 2]>,
61 pub len: u32,
62 pub value: usize,
63}
64pub(super) fn u16le(bytes: &[u8], at: usize) -> Result<u16, DatasetError> {
65 let b = bytes
66 .get(at..at.checked_add(2).ok_or(DatasetError::Overflow)?)
67 .ok_or(DatasetError::Truncated)?;
68 Ok(u16::from_le_bytes([b[0], b[1]]))
69}
70pub(super) fn u32le(bytes: &[u8], at: usize) -> Result<u32, DatasetError> {
71 let b = bytes
72 .get(at..at.checked_add(4).ok_or(DatasetError::Overflow)?)
73 .ok_or(DatasetError::Truncated)?;
74 Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
75}
76pub(super) fn long_vr(vr: &[u8; 2]) -> bool {
77 matches!(
78 vr,
79 b"OB"
80 | b"OD"
81 | b"OF"
82 | b"OL"
83 | b"OV"
84 | b"OW"
85 | b"SQ"
86 | b"SV"
87 | b"UC"
88 | b"UN"
89 | b"UR"
90 | b"UT"
91 | b"UV"
92 )
93}
94pub(super) fn header(
95 bytes: &[u8],
96 at: usize,
97 explicit: bool,
98 end: usize,
99) -> Result<Header, DatasetError> {
100 let bounded = bytes.get(..end).ok_or(DatasetError::Boundary)?;
101 let tag = ((u16le(bounded, at)? as u32) << 16)
102 | u16le(bounded, at.checked_add(2).ok_or(DatasetError::Overflow)?)? as u32;
103 let add = |n| at.checked_add(n).ok_or(DatasetError::Overflow);
104 if tag >> 16 == 0xfffe || !explicit {
105 return Ok(Header {
106 tag,
107 vr: None,
108 len: u32le(bounded, add(4)?)?,
109 value: add(8)?,
110 });
111 }
112 let vr = [
113 *bounded.get(add(4)?).ok_or(DatasetError::Truncated)?,
114 *bounded.get(add(5)?).ok_or(DatasetError::Truncated)?,
115 ];
116 let short = matches!(
117 &vr,
118 b"AE"
119 | b"AS"
120 | b"AT"
121 | b"CS"
122 | b"DA"
123 | b"DS"
124 | b"DT"
125 | b"FL"
126 | b"FD"
127 | b"IS"
128 | b"LO"
129 | b"LT"
130 | b"PN"
131 | b"SH"
132 | b"SL"
133 | b"SS"
134 | b"ST"
135 | b"TM"
136 | b"UI"
137 | b"UL"
138 | b"US"
139 );
140 if long_vr(&vr) {
141 if u16le(bounded, add(6)?)? != 0 {
142 return Err(DatasetError::MalformedHeader);
143 }
144 Ok(Header {
145 tag,
146 vr: Some(vr),
147 len: u32le(bounded, add(8)?)?,
148 value: add(12)?,
149 })
150 } else if short {
151 Ok(Header {
152 tag,
153 vr: Some(vr),
154 len: u16le(bounded, add(6)?)? as u32,
155 value: add(8)?,
156 })
157 } else {
158 Err(DatasetError::MalformedHeader)
159 }
160}
161pub(super) fn implicit_sequence(tag: u32) -> bool {
162 matches!(
163 tag,
164 0x52009229
165 | 0x52009230
166 | 0x00209113
167 | 0x00209116
168 | 0x00289110
169 | 0x00289145
170 | 0x00209111
171 | 0x00409096
172 | 0x004008ea
173 | 0x00081140
174 | 0x00089124
175 | 0x00082218
176 )
177}