dcmnorm_encoding/decode/explicit_be.rs
1//! Explicit VR Big Endian syntax transfer implementation.
2
3use crate::decode::basic::BigEndianBasicDecoder;
4use crate::decode::{
5 BadSequenceHeaderSnafu, BasicDecode, Decode, DecodeFrom, ReadHeaderTagSnafu,
6 ReadItemHeaderSnafu, ReadItemLengthSnafu, ReadLengthSnafu, ReadReservedSnafu, ReadTagSnafu,
7 ReadVrSnafu, Result,
8};
9use byteordered::byteorder::{BigEndian, ByteOrder};
10use dcmnorm_core::header::{DataElementHeader, Length, SequenceItemHeader};
11use dcmnorm_core::{Tag, VR};
12use snafu::ResultExt;
13use std::io::Read;
14
15/// A data element decoder for the Explicit VR Big Endian transfer syntax.
16#[derive(Debug, Default, Clone)]
17pub struct ExplicitVRBigEndianDecoder {
18 basic: BigEndianBasicDecoder,
19}
20
21impl Decode for ExplicitVRBigEndianDecoder {
22 fn decode_header<S>(&self, mut source: &mut S) -> Result<(DataElementHeader, usize)>
23 where
24 S: ?Sized + Read,
25 {
26 // retrieve tag
27 let Tag(group, element) = self
28 .basic
29 .decode_tag(&mut source)
30 .context(ReadHeaderTagSnafu)?;
31
32 let mut buf = [0u8; 4];
33 if group == 0xFFFE {
34 // item delimiters do not have VR or reserved field
35 source.read_exact(&mut buf).context(ReadItemLengthSnafu)?;
36 let len = BigEndian::read_u32(&buf);
37 return Ok((
38 DataElementHeader::new((group, element), VR::UN, Length(len)),
39 8, // tag + len
40 ));
41 }
42
43 // retrieve explicit VR
44 source.read_exact(&mut buf[0..2]).context(ReadVrSnafu)?;
45 let vr = VR::from_binary([buf[0], buf[1]]).unwrap_or(VR::UN);
46
47 let bytes_read;
48
49 // retrieve data length
50 let len = match vr {
51 // PS3.5 7.1.2:
52 // for VRs of AE, AS, AT, CS, DA, DS, DT, FL, FD, IS, LO, LT, PN,
53 // SH, SL, SS, ST, TM, UI, UL and US the Value Length Field is the
54 // 16-bit unsigned integer following the two byte VR Field (Table
55 // 7.1-2). The value of the Value Length Field shall equal the
56 // length of the Value Field.
57 VR::AE
58 | VR::AS
59 | VR::AT
60 | VR::CS
61 | VR::DA
62 | VR::DS
63 | VR::DT
64 | VR::FL
65 | VR::FD
66 | VR::IS
67 | VR::LO
68 | VR::LT
69 | VR::PN
70 | VR::SH
71 | VR::SL
72 | VR::SS
73 | VR::ST
74 | VR::TM
75 | VR::UI
76 | VR::UL
77 | VR::US => {
78 // read 2 bytes for the data length
79 source
80 .read_exact(&mut buf[0..2])
81 .context(ReadItemLengthSnafu)?;
82 bytes_read = 8;
83 u32::from(BigEndian::read_u16(&buf[0..2]))
84 }
85 // PS3.5 7.1.2:
86 // for all other VRs the 16 bits following the two byte VR Field
87 // are reserved for use by later versions of the DICOM Standard.
88 // These reserved bytes shall be set to 0000H and shall not be
89 // used or decoded (Table 7.1-1). The Value Length Field is a
90 // 32-bit unsigned integer.
91 _ => {
92 // read 2 reserved bytes, then 4 bytes for data length
93 source
94 .read_exact(&mut buf[0..2])
95 .context(ReadReservedSnafu)?;
96 source.read_exact(&mut buf).context(ReadLengthSnafu)?;
97 bytes_read = 12;
98 BigEndian::read_u32(&buf)
99 }
100 };
101
102 Ok((
103 DataElementHeader::new((group, element), vr, Length(len)),
104 bytes_read,
105 ))
106 }
107
108 fn decode_item_header<S>(&self, source: &mut S) -> Result<SequenceItemHeader>
109 where
110 S: ?Sized + Read,
111 {
112 let mut buf = [0u8; 8];
113 source.read_exact(&mut buf).context(ReadItemHeaderSnafu)?;
114 // retrieve tag
115 let group = BigEndian::read_u16(&buf[0..2]);
116 let element = BigEndian::read_u16(&buf[2..4]);
117 let len = BigEndian::read_u32(&buf[4..8]);
118
119 SequenceItemHeader::new((group, element), Length(len)).context(BadSequenceHeaderSnafu)
120 }
121
122 fn decode_tag<S>(&self, source: &mut S) -> Result<Tag>
123 where
124 S: ?Sized + Read,
125 {
126 let mut buf = [0u8; 4];
127 source.read_exact(&mut buf).context(ReadTagSnafu)?;
128 Ok(Tag(
129 BigEndian::read_u16(&buf[0..2]),
130 BigEndian::read_u16(&buf[2..4]),
131 ))
132 }
133}
134
135impl<S: ?Sized> DecodeFrom<S> for ExplicitVRBigEndianDecoder
136where
137 S: Read,
138{
139 #[inline]
140 fn decode_header(&self, source: &mut S) -> Result<(DataElementHeader, usize)> {
141 Decode::decode_header(self, source)
142 }
143
144 #[inline]
145 fn decode_item_header(&self, source: &mut S) -> Result<SequenceItemHeader> {
146 Decode::decode_item_header(self, source)
147 }
148
149 #[inline]
150 fn decode_tag(&self, source: &mut S) -> Result<Tag> {
151 Decode::decode_tag(self, source)
152 }
153
154 #[inline]
155 fn is_explicit_vr(&self) -> bool {
156 true
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use super::ExplicitVRBigEndianDecoder;
163 use crate::decode::Decode;
164 use dcmnorm_core::header::{HasLength, Header, Length};
165 use dcmnorm_core::{Tag, VR};
166 use std::io::{Cursor, Seek, SeekFrom};
167
168 // manually crafting some DICOM data elements
169 #[rustfmt::skip]
170 const RAW: &[u8] = &[
171 0x00, 0x02, 0x00, 0x02, // (0002,0002) (BE) Media Storage SOP Class UID
172 b'U', b'I', // VR: UI (UID)
173 0x00, 0x1A, // Length: 26 bytes (BE)
174 // UID: 1.2.840.10008.5.1.4.1.1.1
175 b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
176 b'5', b'.', b'1', b'.', b'4', b'.', b'1', b'.', b'1', b'.', b'1',
177 0x00, // Padding to make length even
178 0x00, 0x02, 0x00, 0x10, // (0002,0010) (BE) Transfer Syntax UID
179 b'U', b'I', // VR: UI (UID)
180 0x00, 0x14, // Length: 20 bytes (BE)
181 // UID: 1.2.840.10008.1.2.1 (ExplicitVRLittleEndian)
182 b'1', b'.', b'2', b'.', b'8', b'4', b'0', b'.', b'1', b'0', b'0', b'0', b'8', b'.',
183 b'1', b'.', b'2', b'.', b'1',
184 0x00, // Padding to make length even
185 0x00, 0x08, 0x00, 0x54, // (0008,0054) (BE) Retrieve AE Title
186 b'A', b'E', // VR: AE (Application Entity)
187 0x00, 0x06, // Length: 6 bytes (BE)
188 // String "TITLE"
189 b'T', b'I', b'T', b'L', b'E',
190 b' ', // Padding to make length even
191 0x00, 0x10, 0x10, 0x10, // (0010,1010) (BE) Patient's Age
192 b'A', b'S', // VR: AS (Age String)
193 0x00, 0x02, // Length: 2 bytes (BE)
194 // String "8Y"
195 b'8', b'Y',
196 0x00, 0x72, 0x00, 0x26, // (0072,0026) (BE) Selector Attribute
197 b'A', b'T', // VR: AT (Attribute Tag)
198 0x00, 0x04, // Length: 4 bytes (BE)
199 // Tag: (0028,2110) (BE) Lossy Image Compression
200 0x00, 0x28, 0x21, 0x10,
201 0x00, 0x10, 0x00, 0x40, // (0010,0040) (BE) Patient Sex
202 b'C', b'S', // VR: CS (Code String)
203 0x00, 0x02, // Length: 2 bytes (BE)
204 // Text: "O"
205 b'O',
206 b' ', // Padding to make length even
207 0x00, 0x10, 0x00, 0x30, // (0010,0030) (BE) Patient's Birth Date
208 b'D', b'A', // VR: DA (Date)
209 0x00, 0x08, // Length: 8 bytes (BE)
210 // String "19800101"
211 b'1', b'9', b'8', b'0', b'0', b'1', b'0', b'1',
212 0x00, 0x10, 0x10, 0x20, // (0010,1020) (BE) Patient's size
213 b'D', b'S', // VR: DS (Decimal String)
214 0x00, 0x04, // Length: 4 bytes (BE)
215 // String: "1.70"
216 b'1', b'.', b'7', b'0',
217 0x00, 0x08, 0x00, 0x15, // (0008,0015) (BE) Instance coercion datetime
218 b'D', b'T', // VR: DT (Date Time)
219 0x00, 0x0E, // Length: 14 bytes (BE)
220 // String "20051231235960"
221 b'2', b'0', b'0', b'5', b'1', b'2', b'3', b'1', b'2', b'3', b'5', b'9', b'6', b'0',
222 0x00, 0x10, 0x94, 0x31, // (0010,9431) (BE) Examined Body Thickness
223 b'F', b'L', // VR: FL (Floating-point IEEE-754)
224 0x00, 0x04, // Length: 4 bytes (BE)
225 // Value: 3.1415927 (BE)
226 0x40, 0x49, 0x0F, 0xDB,
227 0x00, 0x40, 0x92, 0x25, // (0040,9225) (BE) Real World Value Slope
228 b'F', b'D', // VR: FD (Double-precision floating-point IEEE-754)
229 0x00, 0x08, // Length: 8 bytes (BE)
230 // Value: 3.141592653589793 (BE)
231 0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18,
232 0x00, 0x20, 0x00, 0x13, // (0020,0013) (BE) Instance Number
233 b'I', b'S', // VR: IS (Integer String)
234 0x00, 0x08, // Length: 8 bytes (BE)
235 // String: "1234567"
236 b'1', b'2', b'3', b'4', b'5', b'6', b'7',
237 b' ', // Padding to make length even
238 0x00, 0x10, 0x00, 0x20, // (0010,0020) (BE) Patient ID
239 b'L', b'O', // VR: LO (Long string)
240 0x00, 0x0A, // Length: 10 bytes (BE)
241 // String "P12345678X"
242 b'P', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'X',
243 0x00, 0x10, 0x40, 0x00, // (0010,4000) (BE) Patient Comments
244 b'L', b'T', // VR: LT (Long Text)
245 0x00, 0x04, // Length: 4 bytes (BE)
246 // String "None"
247 b'N', b'o', b'n', b'e',
248 0x00, 0x08, 0x04, 0x1B, // (0008,041B) (BE) RecordKey
249 b'O', b'B', // VR: OB (Other Byte)
250 0x00, 0x00, // Reserved, always 0
251 0x00, 0x00, 0x00, 0x02, // Length: 2 bytes (BE)
252 // Value: [0x12, 0x34]
253 0x12, 0x34,
254 0x7F, 0xE0, 0x00, 0x09, // (7FE0,0009) (BE) Double Float Pixel Data
255 b'O', b'D', // VR: OD (Other Double)
256 0x00, 0x00, // Reserved, always 0
257 0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
258 // Value: [3.141592653589793] (BE)
259 0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18,
260 0x7F, 0xE0, 0x00, 0x08, // (7FE0,0008) (BE) Float Pixel Data
261 b'O', b'F', // VR: OF (Other Float)
262 0x00, 0x00, // Reserved, always 0
263 0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
264 // Value: [3.1415927] (BE)
265 0x40, 0x49, 0x0F, 0xDB,
266 0x00, 0x72, 0x00, 0x75, // (0072,0075) (BE) Selector OL Value
267 b'O', b'L', // VR: OL (Other Long)
268 0x00, 0x00, // Reserved, always 0
269 0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
270 // Value: [0x12345678] (BE)
271 0x12, 0x34, 0x56, 0x78,
272 0x00, 0x72, 0x00, 0x81, // (0072,0081) (BE) Selector OV Value
273 b'O', b'V', // VR: OV (Other Very long)
274 0x00, 0x00, // Reserved, always 0
275 0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
276 // Value: [0x192A3B4C5D6E7F80] (BE)
277 0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F, 0x80,
278 0x00, 0x72, 0x00, 0x69, // (0072,0069) (BE) Selector OW Value
279 b'O', b'W', // VR: OW (Other Word)
280 0x00, 0x00, // Reserved, always 0
281 0x00, 0x00, 0x00, 0x02, // Length: 2 bytes (BE)
282 // Value: [0x1234] (BE)
283 0x12, 0x34,
284 0x00, 0x10, 0x00, 0x10, // (0010,0010) (BE) Patient Name
285 b'P', b'N', // VR: PN (Person Name)
286 0x00, 0x08, // Length: 8 bytes (BE)
287 // String: "Doe^John"
288 b'D', b'o', b'e', b'^', b'J', b'o', b'h', b'n',
289 0x00, 0x40, 0x92, 0x10, // (0040,9210) (BE) LUT Label
290 b'S', b'H', // VR: SH (Short string)
291 0x00, 0x04, // Length: 4 bytes (BE)
292 // String: "LBL"
293 b'L', b'B', b'L',
294 b' ', // Padding to make length even
295 0x00, 0x18, 0x60, 0x20, // (0018,6020) (BE) Reference Pixel X0
296 b'S', b'L', // VR: SL (Signed Long)
297 0x00, 0x04, // Length: 4 bytes (BE)
298 // Value: -12345678 (BE)
299 0xFF, 0x43, 0x9E, 0xB2,
300 // Sequences (VR: SQ) tested elsewhere
301 0x00, 0x28, 0x95, 0x03, // (0028,9503) (BE) Vertices of the Region (VM 2-2n)
302 b'S', b'S', // VR: SS (Signed Short)
303 0x00, 0x04, // Length: 4 bytes (2 * 2) (BE)
304 // Value: -4567 (BE)
305 0xEE, 0x29,
306 // Value: 4321 (BE)
307 0x10, 0xE1,
308 0x00, 0x40, 0x02, 0x80, // (0040,0280) (BE) Comments on the Performed Procedure Step
309 b'S', b'T', // VR: ST (Short Text)
310 0x00, 0x0A, // Length: 10 bytes (BE)
311 // String: "No comment"
312 b'N', b'o', b' ', b'c', b'o', b'm', b'm', b'e', b'n', b't',
313 0x00, 0x72, 0x00, 0x82, // (0072,0082) (BE) SelectorSVValue (VM: 1-n)
314 b'S', b'V', // VR: SV (Signed Very long)
315 0x00, 0x00, // Reserved, always 0
316 0x00, 0x00, 0x00, 0x10, // Length: 16 bytes (2 * 8) (BE)
317 // Value: -123456789012345678 (BE)
318 0xFE, 0x49, 0x64, 0xB4, 0x59, 0xCF, 0x0C, 0xB2,
319 // Value: 123456789012345678 (BE)
320 0x01, 0xB6, 0x9B, 0x4B, 0xA6, 0x30, 0xF3, 0x4E,
321 0x00, 0x10, 0x00, 0x32, // (0010,0032) (BE) Patient's Birth Time
322 b'T', b'M', // VR: TM (Time)
323 0x00, 0x06, // Length: 6 bytes (BE)
324 // String: "123456"
325 b'1', b'2', b'3', b'4', b'5', b'6',
326 0x00, 0x08, 0x01, 0x19, // (0008,0119) (BE) Long Code Value
327 b'U', b'C', // VR: UC (Unlimited Characters)
328 0x00, 0x00, // Reserved, always 0
329 0x00, 0x00, 0x00, 0x04, // Length: 4 bytes (BE)
330 // String: "Code"
331 b'C', b'o', b'd', b'e',
332 // UI already tested above
333 0x00, 0x18, 0x60, 0x16, // (0018,6016) (BE) Region Flags
334 b'U', b'L',
335 0x00, 0x04, // Length: 4 bytes (BE)
336 // Value: 1 (BE)
337 0x00, 0x00, 0x00, 0x01,
338 0xC0, 0x01, 0x12, 0x34, // (C001,1234) (BE) (Private data element)
339 b'U', b'N', // VR: UN (Unknown)
340 0x00, 0x00, // Reserved, always 0
341 0x00, 0x00, 0x00, 0x06, // Length: 6 bytes (BE)
342 // Value: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
343 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
344 0x00, 0x08, 0x01, 0x0E, // (0008,010E) (BE) Coding Scheme URL
345 b'U', b'R', // VR: UR (Universal Resource Locator, URL)
346 0x00, 0x00, // Reserved, always 0
347 0x00, 0x00, 0x00, 0x12, // Length: 18 bytes (BE)
348 // String: "http://example.com"
349 b'h', b't', b't', b'p', b':', b'/', b'/', b'e', b'x', b'a', b'm', b'p', b'l', b'e',
350 b'.', b'c', b'o', b'm',
351 0x00, 0x08, 0x00, 0x40, // (0008,0040) (BE) Data Set Type
352 b'U', b'S', // VR: US (Unsigned Short)
353 0x00, 0x02, // Length: 2 bytes (BE)
354 // Value: 34567 (BE)
355 0x87, 0x07,
356 0x00, 0x18, 0x99, 0x17, // (0018,9917) (BE) Instruction Description
357 b'U', b'T', // VR: UT (Unlimited Text)
358 0x00, 0x00, // Reserved, always 0
359 0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
360 // String: "No text"
361 b'N', b'o', b' ', b't', b'e', b'x', b't',
362 b' ', // Padding to make length even
363 0x00, 0x08, 0x04, 0x0C, // (0008,040C) (BE) File Offset in Container
364 b'U', b'V', // VR: UV (Unsigned Very long)
365 0x00, 0x00, // Reserved, always 0
366 0x00, 0x00, 0x00, 0x08, // Length: 8 bytes (BE)
367 // Value: 12345678901234567890 (BE)
368 0xAB, 0x54, 0xA9, 0x8C, 0xEB, 0x1F, 0x0A, 0xD2,
369 ];
370
371 #[test]
372 fn decode_explicit_vr_be() {
373 let dec = ExplicitVRBigEndianDecoder::default();
374 let mut cursor = Cursor::new(RAW);
375
376 fn read_n<'a>(cursor: &mut Cursor<&'a [u8]>, n: usize) -> &'a [u8] {
377 let pos = cursor.position() as usize;
378 let slice = &cursor.get_ref()[pos..pos + n]; // panic if too short
379 cursor.set_position((pos + n) as u64);
380 slice
381 }
382
383 fn test_vr(
384 dec: &ExplicitVRBigEndianDecoder,
385 cursor: &mut Cursor<&[u8]>,
386 group: u16,
387 element: u16,
388 vr: VR,
389 value: &[u8],
390 ) {
391 let (elem, _bytes_read) = dec.decode_header(cursor).expect("should find an element");
392 assert_eq!(elem.tag(), Tag(group, element));
393 assert_eq!(elem.vr(), vr);
394 assert_eq!(elem.length(), Length(value.len() as u32));
395 let buffer = read_n(cursor, value.len());
396 assert_eq!(&buffer, &value);
397 }
398
399 // read first element
400 let (elem, bytes_read) = dec
401 .decode_header(&mut cursor)
402 .expect("should find an element");
403 assert_eq!(elem.tag(), Tag(0x0002, 0x0002));
404 assert_eq!(elem.vr(), VR::UI);
405 assert_eq!(elem.length(), Length(26));
406 assert_eq!(bytes_read, 8);
407 // read only half of the value data
408 let buffer = read_n(&mut cursor, 13);
409 assert_eq!(&buffer, b"1.2.840.10008");
410
411 // cursor should now be @ #21 (there is no automatic skipping)
412 assert_eq!(cursor.stream_position().unwrap(), 21);
413 // cursor should now be @ #34 after skipping
414 assert_eq!(cursor.seek(SeekFrom::Current(13)).unwrap(), 34);
415
416 // read second element
417 let (elem, _bytes_read) = dec
418 .decode_header(&mut cursor)
419 .expect("should find an element");
420 assert_eq!(elem.tag(), Tag(2, 16));
421 assert_eq!(elem.vr(), VR::UI);
422 assert_eq!(elem.length(), Length(20));
423 // read all data
424 let buffer = read_n(&mut cursor, 20);
425 assert_eq!(&buffer, b"1.2.840.10008.1.2.1\0");
426
427 // read various VRs
428 test_vr(&dec, &mut cursor, 0x0008, 0x0054, VR::AE, b"TITLE ");
429 test_vr(&dec, &mut cursor, 0x0010, 0x1010, VR::AS, b"8Y");
430 test_vr(
431 &dec,
432 &mut cursor,
433 0x0072,
434 0x0026,
435 VR::AT,
436 &[0x00, 0x28, 0x21, 0x10],
437 );
438 test_vr(&dec, &mut cursor, 0x0010, 0x0040, VR::CS, b"O ");
439 test_vr(&dec, &mut cursor, 0x0010, 0x0030, VR::DA, b"19800101");
440 test_vr(&dec, &mut cursor, 0x0010, 0x1020, VR::DS, b"1.70");
441 test_vr(&dec, &mut cursor, 0x0008, 0x0015, VR::DT, b"20051231235960");
442 test_vr(
443 &dec,
444 &mut cursor,
445 0x0010,
446 0x9431,
447 VR::FL,
448 &[0x40, 0x49, 0x0F, 0xDB],
449 );
450 test_vr(
451 &dec,
452 &mut cursor,
453 0x0040,
454 0x9225,
455 VR::FD,
456 &[0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18],
457 );
458 test_vr(&dec, &mut cursor, 0x0020, 0x0013, VR::IS, b"1234567 ");
459 test_vr(&dec, &mut cursor, 0x0010, 0x0020, VR::LO, b"P12345678X");
460 test_vr(&dec, &mut cursor, 0x0010, 0x4000, VR::LT, b"None");
461 test_vr(&dec, &mut cursor, 0x0008, 0x041B, VR::OB, &[0x12, 0x34]);
462 test_vr(
463 &dec,
464 &mut cursor,
465 0x7FE0,
466 0x0009,
467 VR::OD,
468 &[0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18],
469 );
470 test_vr(
471 &dec,
472 &mut cursor,
473 0x7FE0,
474 0x0008,
475 VR::OF,
476 &[0x40, 0x49, 0x0F, 0xDB],
477 );
478 test_vr(
479 &dec,
480 &mut cursor,
481 0x0072,
482 0x0075,
483 VR::OL,
484 &[0x12, 0x34, 0x56, 0x78],
485 );
486 test_vr(
487 &dec,
488 &mut cursor,
489 0x0072,
490 0x0081,
491 VR::OV,
492 &[0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F, 0x80],
493 );
494 test_vr(&dec, &mut cursor, 0x0072, 0x0069, VR::OW, &[0x12, 0x34]);
495 test_vr(&dec, &mut cursor, 0x0010, 0x0010, VR::PN, b"Doe^John");
496 test_vr(&dec, &mut cursor, 0x0040, 0x9210, VR::SH, b"LBL ");
497 test_vr(
498 &dec,
499 &mut cursor,
500 0x0018,
501 0x6020,
502 VR::SL,
503 &[0xFF, 0x43, 0x9E, 0xB2],
504 );
505 test_vr(
506 &dec,
507 &mut cursor,
508 0x0028,
509 0x9503,
510 VR::SS,
511 &[0xEE, 0x29, 0x10, 0xE1],
512 );
513 test_vr(&dec, &mut cursor, 0x0040, 0x0280, VR::ST, b"No comment");
514 test_vr(
515 &dec,
516 &mut cursor,
517 0x0072,
518 0x0082,
519 VR::SV,
520 &[
521 0xFE, 0x49, 0x64, 0xB4, 0x59, 0xCF, 0x0C, 0xB2, 0x01, 0xB6, 0x9B, 0x4B, 0xA6, 0x30,
522 0xF3, 0x4E,
523 ],
524 );
525 test_vr(&dec, &mut cursor, 0x0010, 0x0032, VR::TM, b"123456");
526 test_vr(&dec, &mut cursor, 0x0008, 0x0119, VR::UC, b"Code");
527 test_vr(
528 &dec,
529 &mut cursor,
530 0x0018,
531 0x6016,
532 VR::UL,
533 &[0x00, 0x00, 0x00, 0x01],
534 );
535 test_vr(
536 &dec,
537 &mut cursor,
538 0xC001,
539 0x1234,
540 VR::UN,
541 &[0x1, 0x2, 0x3, 0x4, 0x5, 0x6],
542 );
543 test_vr(
544 &dec,
545 &mut cursor,
546 0x0008,
547 0x010E,
548 VR::UR,
549 b"http://example.com",
550 );
551 test_vr(&dec, &mut cursor, 0x0008, 0x0040, VR::US, &[0x87, 0x07]);
552 test_vr(&dec, &mut cursor, 0x0018, 0x9917, VR::UT, b"No text ");
553 test_vr(
554 &dec,
555 &mut cursor,
556 0x0008,
557 0x040C,
558 VR::UV,
559 &[0xAB, 0x54, 0xA9, 0x8C, 0xEB, 0x1F, 0x0A, 0xD2],
560 );
561 }
562
563 // manually crafting some DICOM sequence/item delimiters
564 // Tag: (0008,103F) Series Description Code Sequence
565 // VR: SQ
566 // Reserved bytes: 0x0000
567 // Length: 0xFFFF_FFFF
568 // --
569 // Tag: (FFFE,E000) Item
570 // Length: 0xFFFF_FFFF (unspecified)
571 // --
572 // Tag: (FFFE,E00D) Item Delimitation Item
573 // Length: 0
574 // --
575 // Tag: (FFFE,E0DD) Sequence Delimitation Item
576 // Length: 0
577 // --
578 const RAW_SEQUENCE_ITEMS: &[u8] = &[
579 0x00, 0x08, 0x10, 0x3F, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xE0,
580 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xE0, 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE,
581 0xE0, 0xDD, 0x00, 0x00, 0x00, 0x00,
582 ];
583
584 #[test]
585 fn decode_items() {
586 let dec = ExplicitVRBigEndianDecoder::default();
587 let mut cursor = Cursor::new(RAW_SEQUENCE_ITEMS);
588 {
589 // read first element
590 let (elem, _bytes_read) = dec
591 .decode_header(&mut cursor)
592 .expect("should find an element header");
593 assert_eq!(elem.tag(), Tag(8, 0x103F));
594 assert_eq!(elem.vr(), VR::SQ);
595 assert!(elem.length().is_undefined());
596 }
597 // cursor should now be @ #12
598 assert_eq!(cursor.stream_position().unwrap(), 12);
599 {
600 let elem = dec
601 .decode_item_header(&mut cursor)
602 .expect("should find an item header");
603 assert!(elem.is_item());
604 assert_eq!(elem.tag(), Tag(0xFFFE, 0xE000));
605 assert!(elem.length().is_undefined());
606 }
607 // cursor should now be @ #20
608 assert_eq!(cursor.stream_position().unwrap(), 20);
609 {
610 let elem = dec
611 .decode_item_header(&mut cursor)
612 .expect("should find an item header");
613 assert!(elem.is_item_delimiter());
614 assert_eq!(elem.tag(), Tag(0xFFFE, 0xE00D));
615 assert_eq!(elem.length(), Length(0));
616 }
617 // cursor should now be @ #28
618 assert_eq!(cursor.stream_position().unwrap(), 28);
619 {
620 let elem = dec
621 .decode_item_header(&mut cursor)
622 .expect("should find an item header");
623 assert!(elem.is_sequence_delimiter());
624 assert_eq!(elem.tag(), Tag(0xFFFE, 0xE0DD));
625 assert_eq!(elem.length(), Length(0));
626 }
627 }
628}