1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/// PDU reader module
use crate::pdu::*;
use bytes::Buf;
use dicom_encoding::text::{DefaultCharacterSetCodec, TextCodec};
use snafu::{ensure, OptionExt, ResultExt};
use tracing::warn;
pub type Error = crate::pdu::ReadError;
pub type Result<T> = std::result::Result<T, Error>;
/// Read a PDU from the given byte buffer.
pub fn read_pdu(mut buf: impl Buf, max_pdu_length: u32, strict: bool) -> Result<Option<Pdu>> {
ensure!(
max_pdu_length >= super::MINIMUM_PDU_SIZE,
InvalidMaxPduSnafu { max_pdu_length },
);
// If we can't read 2 bytes here, that means that there is no PDU
// available. Normally, we want to just return the UnexpectedEof error. However,
// this method can block and wake up when stream is closed, so in this case, we
// want to know if we had trouble even beginning to read a PDU. We still return
// UnexpectedEof if we get after we have already began reading a PDU message.
if buf.remaining() < 2 {
return Ok(None);
}
let bytes = buf.copy_to_bytes(2);
let pdu_type = bytes[0];
if buf.remaining() < 4 {
return Ok(None);
}
let pdu_length = buf.get_u32();
// Check max_pdu_length
ensure!(
!strict || pdu_length <= max_pdu_length,
PduTooLargeSnafu {
pdu_length,
max_pdu_length,
}
);
if buf.remaining() < pdu_length as usize {
return Ok(None);
}
let mut bytes = buf.copy_to_bytes(pdu_length as usize);
let codec = DefaultCharacterSetCodec;
match pdu_type {
0x01 => {
// A-ASSOCIATE-RQ PDU Structure
let mut application_context_name: Option<String> = None;
let mut presentation_contexts = vec![];
let mut user_variables = vec![];
// 7-8 - Protocol-version - This two byte field shall use one bit to identify each
// version of the DICOM UL protocol supported by the calling end-system. This is
// Version 1 and shall be identified with bit 0 set. A receiver of this PDU
// implementing only this version of the DICOM UL protocol shall only test that bit
// 0 is set.
ensure!(
bytes.remaining() >= 2 + 2 + 16 + 16 + 32,
InvalidPduFieldLengthSnafu {},
);
let protocol_version = bytes.get_u16();
// 9-10 - Reserved - This reserved field shall be sent with a value 0000H but not
// tested to this value when received.
bytes.get_u16();
// 11-26 - Called-AE-title - Destination DICOM Application Name. It shall be encoded
// as 16 characters as defined by the ISO 646:1990-Basic G0 Set with leading and
// trailing spaces (20H) being non-significant. The value made of 16 spaces (20H)
// meaning "no Application Name specified" shall not be used. For a complete
// description of the use of this field, see Section 7.1.1.4.
let ae_bytes = bytes.copy_to_bytes(16);
let called_ae_title = codec
.decode(ae_bytes.as_ref())
.context(DecodeTextSnafu {
field: "Called-AE-title",
})?
.trim()
.to_string();
// 27-42 - Calling-AE-title - Source DICOM Application Name. It shall be encoded as
// 16 characters as defined by the ISO 646:1990-Basic G0 Set with leading and
// trailing spaces (20H) being non-significant. The value made of 16 spaces (20H)
// meaning "no Application Name specified" shall not be used. For a complete
// description of the use of this field, see Section 7.1.1.3.
let ae_bytes = bytes.copy_to_bytes(16);
let calling_ae_title = codec
.decode(ae_bytes.as_ref())
.context(DecodeTextSnafu {
field: "Calling-AE-title",
})?
.trim()
.to_string();
// 43-74 - Reserved - This reserved field shall be sent with a value 00H for all
// bytes but not tested to this value when received
bytes.advance(32);
// 75-xxx - Variable items - This variable field shall contain the following items:
// one Application Context Item, one or more Presentation Context Items and one User
// Information Item. For a complete description of the use of these items see
// Section 7.1.1.2, Section 7.1.1.13, and Section 7.1.1.6.
while bytes.has_remaining() {
match read_pdu_variable(&mut bytes, &codec)? {
Some(PduVariableItem::ApplicationContext(val)) => {
application_context_name = Some(val);
}
Some(PduVariableItem::PresentationContextProposed(val)) => {
presentation_contexts.push(val);
}
Some(PduVariableItem::UserVariables(val)) => {
user_variables = val;
}
Some(var_item) => {
return InvalidPduVariableSnafu { var_item }.fail();
}
None => {
tracing::debug!("PDU variable none");
return ReadUserVariableSnafu {}.fail();
}
}
}
Ok(Some(Pdu::AssociationRQ(AssociationRQ {
protocol_version,
application_context_name: application_context_name
.context(MissingApplicationContextNameSnafu)?,
called_ae_title,
calling_ae_title,
presentation_contexts,
user_variables,
})))
}
0x02 => {
// A-ASSOCIATE-AC PDU Structure
let mut application_context_name: Option<String> = None;
let mut presentation_contexts = vec![];
let mut user_variables = vec![];
// 7-8 - Protocol-version - This two byte field shall use one bit to identify each
// version of the DICOM UL protocol supported by the calling end-system. This is
// Version 1 and shall be identified with bit 0 set. A receiver of this PDU
// implementing only this version of the DICOM UL protocol shall only test that bit
// 0 is set.
ensure!(
bytes.remaining() >= 2 + 2 + 16 + 16 + 32,
InvalidPduFieldLengthSnafu {},
);
let protocol_version = bytes.get_u16();
// 9-10 - Reserved - This reserved field shall be sent with a value 0000H but not
// tested to this value when received.
bytes.get_u16();
// 11-26 - Reserved - This reserved field shall be sent with a value identical to
// the value received in the same field of the A-ASSOCIATE-RQ PDU, but its value
// shall not be tested when received.
let ae_bytes = bytes.copy_to_bytes(16);
let called_ae_title = codec
.decode(&ae_bytes)
.context(DecodeTextSnafu {
field: "Called-AE-title",
})?
.trim()
.to_string();
// 27-42 - Reserved - This reserved field shall be sent with a value identical to
// the value received in the same field of the A-ASSOCIATE-RQ PDU, but its value
// shall not be tested when received.
let ae_bytes = bytes.copy_to_bytes(16);
let calling_ae_title = codec
.decode(&ae_bytes)
.context(DecodeTextSnafu {
field: "Calling-AE-title",
})?
.trim()
.to_string();
// 43-74 - Reserved - This reserved field shall be sent with a value identical to
// the value received in the same field of the A-ASSOCIATE-RQ PDU, but its value
// shall not be tested when received.
bytes.advance(32);
// 75-xxx - Variable items - This variable field shall contain the following items:
// one Application Context Item, one or more Presentation Context Item(s) and one
// User Information Item. For a complete description of these items see Section
// 7.1.1.2, Section 7.1.1.14, and Section 7.1.1.6.
while bytes.has_remaining() {
match read_pdu_variable(&mut bytes, &codec)? {
Some(PduVariableItem::ApplicationContext(val)) => {
application_context_name = Some(val);
}
Some(PduVariableItem::PresentationContextResult(val)) => {
presentation_contexts.push(val);
}
Some(PduVariableItem::UserVariables(val)) => {
user_variables = val;
}
Some(var_item) => {
return InvalidPduVariableSnafu { var_item }.fail();
}
None => {
return ReadUserVariableSnafu {}.fail();
}
}
}
Ok(Some(Pdu::AssociationAC(AssociationAC {
protocol_version,
application_context_name: application_context_name
.context(MissingApplicationContextNameSnafu)?,
called_ae_title,
calling_ae_title,
presentation_contexts,
user_variables,
})))
}
0x03 => {
// A-ASSOCIATE-RJ PDU Structure
// 7 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
ensure!(
bytes.remaining() >= 1 + 1 + 2,
InvalidPduFieldLengthSnafu {}
);
bytes.get_u8();
// 8 - Result - This Result field shall contain an integer value encoded as an unsigned
// binary number. One of the following values shall be used:
// 1 - rejected-permanent
// 2 - rejected-transient
let result = AssociationRJResult::from(bytes.get_u8())
.context(InvalidRejectSourceOrReasonSnafu)?;
// 9 - Source - This Source field shall contain an integer value encoded as an unsigned
// binary number. One of the following values shall be used: 1 - DICOM UL
// service-user 2 - DICOM UL service-provider (ACSE related function)
// 3 - DICOM UL service-provider (Presentation related function)
// 10 - Reason/Diag. - This field shall contain an integer value encoded as an unsigned
// binary number. If the Source field has the value (1) "DICOM UL
// service-user", it shall take one of the following:
// 1 - no-reason-given
// 2 - application-context-name-not-supported
// 3 - calling-AE-title-not-recognized
// 4-6 - reserved
// 7 - called-AE-title-not-recognized
// 8-10 - reserved
// If the Source field has the value (2) "DICOM UL service provided (ACSE related
// function)", it shall take one of the following: 1 - no-reason-given
// 2 - protocol-version-not-supported
// If the Source field has the value (3) "DICOM UL service provided (Presentation
// related function)", it shall take one of the following: 0 - reserved
// 1 - temporary-congestio
// 2 - local-limit-exceeded
// 3-7 - reserved
let source = AssociationRJSource::from(bytes.get_u8(), bytes.get_u8())
.context(InvalidRejectSourceOrReasonSnafu)?;
Ok(Some(Pdu::AssociationRJ(AssociationRJ { result, source })))
}
0x04 => {
// P-DATA-TF PDU Structure
// 7-xxx - Presentation-data-value Item(s) - This variable data field shall contain one
// or more Presentation-data-value Items(s). For a complete description of the use of
// this field see Section 9.3.5.1
let mut values = vec![];
while bytes.has_remaining() {
// Presentation Data Value Item Structure
// 1-4 - Item-length - This Item-length shall be the number of bytes from the first
// byte of the following field to the last byte of the Presentation-data-value
// field. It shall be encoded as an unsigned binary number.
// Clippy warns here that `>= x+1` could be written as `> x`,
// unaware that 4 + 1 + 1 is the total length to expect and
// thus clearer to read, so we override it.
#[allow(clippy::int_plus_one)]
let enough_remaining = bytes.remaining() >= 4 + 1 + 1;
ensure!(enough_remaining, InvalidPduFieldLengthSnafu {});
let item_length = bytes.get_u32();
ensure!(
item_length >= 2,
InvalidItemLengthSnafu {
length: item_length
},
);
// 5 - Presentation-context-ID - Presentation-context-ID values shall be odd
// integers between 1 and 255, encoded as an unsigned binary number. For a complete
// description of the use of this field see Section 7.1.1.13.
let presentation_context_id = bytes.get_u8();
// 6-xxx - Presentation-data-value - This Presentation-data-value field shall
// contain DICOM message information (command and/or data set) with a message
// control header. For a complete description of the use of this field see Annex E.
// The Message Control Header shall be made of one byte with the least significant
// bit (bit 0) taking one of the following values: If bit 0 is set
// to 1, the following fragment shall contain Message Command information.
// If bit 0 is set to 0, the following fragment shall contain Message Data Set
// information. The next least significant bit (bit 1) shall be
// defined by the following rules: If bit 1 is set to 1, the
// following fragment shall contain the last fragment of a Message Data Set or of a
// Message Command. If bit 1 is set to 0, the following fragment
// does not contain the last fragment of a Message Data Set or of a Message Command.
let header = bytes.get_u8();
let value_type = if header & 0x01 > 0 {
PDataValueType::Command
} else {
PDataValueType::Data
};
let is_last = (header & 0x02) > 0;
ensure!(
bytes.remaining() >= (item_length - 2) as usize,
InvalidPduFieldLengthSnafu {},
);
values.push(PDataValue {
presentation_context_id,
value_type,
is_last,
data: bytes.copy_to_bytes((item_length - 2) as usize).to_vec(),
});
}
Ok(Some(Pdu::PData { data: values }))
}
0x05 => {
// A-RELEASE-RQ PDU Structure
// 7-10 - Reserved - This reserved field shall be sent with a value 00000000H but not
// tested to this value when received.
ensure!(bytes.remaining() >= 4, InvalidPduFieldLengthSnafu {});
bytes.advance(4);
Ok(Some(Pdu::ReleaseRQ))
}
0x06 => {
// A-RELEASE-RP PDU Structure
// 7-10 - Reserved - This reserved field shall be sent with a value 00000000H but not
// tested to this value when received.
ensure!(bytes.remaining() >= 4, InvalidPduFieldLengthSnafu {});
bytes.advance(4);
Ok(Some(Pdu::ReleaseRP))
}
0x07 => {
// A-ABORT PDU Structure
// 7 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
// 8 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
ensure!(bytes.remaining() >= 2 + 2, InvalidPduFieldLengthSnafu {});
let _ = bytes.copy_to_bytes(2);
// 9 - Source - This Source field shall contain an integer value encoded as an unsigned
// binary number. One of the following values shall be used:
// - 0 - DICOM UL service-user (initiated abort)
// - 1 - reserved
// - 2 - DICOM UL service-provider (initiated abort)
// 10 - Reason/Diag - This field shall contain an integer value encoded as an unsigned
// binary number. If the Source field has the value (2) "DICOM UL
// service-provider", it shall take one of the following:
// - 0 - reason-not-specified1 - unrecognized-PDU
// - 2 - unexpected-PDU
// - 3 - reserved
// - 4 - unrecognized-PDU parameter
// - 5 - unexpected-PDU parameter
// - 6 - invalid-PDU-parameter value
let source = AbortRQSource::from(bytes.get_u8(), bytes.get_u8())
.context(InvalidAbortSourceOrReasonSnafu)?;
Ok(Some(Pdu::AbortRQ { source }))
}
_ => {
ensure!(
bytes.remaining() >= pdu_length as usize,
InvalidPduFieldLengthSnafu {},
);
Ok(Some(Pdu::Unknown {
pdu_type,
data: bytes.copy_to_bytes(pdu_length as usize).to_vec(),
}))
}
}
}
fn read_pdu_variable(mut buf: impl Buf, codec: &dyn TextCodec) -> Result<Option<PduVariableItem>> {
// 1 - Item-type - XXH
if buf.remaining() < 1 {
return Ok(None);
}
let item_type = buf.get_u8();
// 2 - Reserved
if buf.remaining() < 1 {
return Ok(None);
}
buf.get_u8();
// 3-4 - Item-length
if buf.remaining() < 2 {
return Ok(None);
}
let item_length = buf.get_u16();
if buf.remaining() < item_length as usize {
return Ok(None);
}
let mut bytes = buf.copy_to_bytes(item_length as usize);
match item_type {
0x10 => {
// Application Context Item Structure
// 5-xxx - Application-context-name - A valid Application-context-name shall be encoded
// as defined in Annex F. For a description of the use of this field see Section
// 7.1.1.2. Application-context-names are structured as UIDs as defined in PS3.5 (see
// Annex A for an overview of this concept). DICOM Application-context-names are
// registered in PS3.7.
let val = codec.decode(bytes.as_ref()).context(DecodeTextSnafu {
field: "Application-context-name",
})?;
Ok(Some(PduVariableItem::ApplicationContext(val)))
}
0x20 => {
// Presentation Context Item Structure (proposed)
let mut abstract_syntax: Option<String> = None;
let mut transfer_syntaxes = vec![];
// 5 - Presentation-context-ID - Presentation-context-ID values shall be odd integers
// between 1 and 255, encoded as an unsigned binary number. For a complete description
// of the use of this field see Section 7.1.1.13.
if bytes.remaining() < 1 {
return Ok(None);
}
let presentation_context_id = bytes.get_u8();
// 6 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 7 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 8 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 9-xxx - Abstract/Transfer Syntax Sub-Items - This variable field shall contain the
// following sub-items: one Abstract Syntax and one or more Transfer Syntax(es). For a
// complete description of the use and encoding of these sub-items see Section 9.3.2.2.1
// and Section 9.3.2.2.2.
while bytes.has_remaining() {
// 1 - Item-type - XXH
if bytes.remaining() < 1 {
return Ok(None);
}
let item_type = bytes.get_u8();
// 2 - Reserved - This reserved field shall be sent with a value 00H but not tested
// to this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 3-4 - Item-length
if bytes.remaining() < 2 {
return Ok(None);
}
let item_length = bytes.get_u16();
match item_type {
0x30 => {
// Abstract Syntax Sub-Item Structure
// 5-xxx - Abstract-syntax-name - This variable field shall contain the
// Abstract-syntax-name related to the proposed presentation context. A
// valid Abstract-syntax-name shall be encoded as defined in Annex F. For a
// description of the use of this field see Section 7.1.1.13.
// Abstract-syntax-names are structured as UIDs as defined in PS3.5 (see
// Annex B for an overview of this concept). DICOM Abstract-syntax-names are
// registered in PS3.4.
if bytes.remaining() < item_length as usize {
return Ok(None);
}
abstract_syntax = Some(
codec
.decode(bytes.copy_to_bytes(item_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "Abstract-syntax-name",
})?
.trim()
.to_string(),
);
}
0x40 => {
// Transfer Syntax Sub-Item Structure
// 5-xxx - Transfer-syntax-name(s) - This variable field shall contain the
// Transfer-syntax-name proposed for this presentation context. A valid
// Transfer-syntax-name shall be encoded as defined in Annex F. For a
// description of the use of this field see Section 7.1.1.13.
// Transfer-syntax-names are structured as UIDs as defined in PS3.5 (see
// Annex B for an overview of this concept). DICOM Transfer-syntax-names are
// registered in PS3.5.
if bytes.remaining() < item_length as usize {
return Ok(None);
}
transfer_syntaxes.push(
codec
.decode(bytes.copy_to_bytes(item_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "Transfer-syntax-name",
})?
.trim()
.to_string(),
);
}
_ => {
return UnknownPresentationContextSubItemSnafu.fail();
}
}
}
Ok(Some(PduVariableItem::PresentationContextProposed(
PresentationContextProposed {
id: presentation_context_id,
abstract_syntax: abstract_syntax.context(MissingAbstractSyntaxSnafu)?,
transfer_syntaxes,
},
)))
}
0x21 => {
// Presentation Context Item Structure (result)
let mut transfer_syntax: Option<String> = None;
// 5 - Presentation-context-ID - Presentation-context-ID values shall be odd integers
// between 1 and 255, encoded as an unsigned binary number. For a complete description
// of the use of this field see Section 7.1.1.13.
if bytes.remaining() < 1 {
return Ok(None);
}
let presentation_context_id = bytes.get_u8();
// 6 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 7 - Result/Reason - This Result/Reason field shall contain an integer value encoded
// as an unsigned binary number. One of the following values shall be used:
// 0 - acceptance
// 1 - user-rejection
// 2 - no-reason (provider rejection)
// 3 - abstract-syntax-not-supported (provider rejection)
// 4 - transfer-syntaxes-not-supported (provider rejection)
if bytes.remaining() < 1 {
return Ok(None);
}
let reason = PresentationContextResultReason::from(bytes.get_u8())
.context(InvalidPresentationContextResultReasonSnafu)?;
// 8 - Reserved - This reserved field shall be sent with a value 00H but not tested to
// this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 9-xxx - Transfer syntax sub-item - This variable field shall contain one Transfer
// Syntax Sub-Item. When the Result/Reason field has a value other than acceptance (0),
// this field shall not be significant and its value shall not be tested when received.
// For a complete description of the use and encoding of this item see Section
// 9.3.3.2.1.
while bytes.has_remaining() {
// 1 - Item-type - XXH
if bytes.remaining() < 1 {
return Ok(None);
}
let item_type = bytes.get_u8();
// 2 - Reserved - This reserved field shall be sent with a value 00H but not tested
// to this value when received.
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 3-4 - Item-length
if bytes.remaining() < 2 {
return Ok(None);
}
let item_length = bytes.get_u16();
match item_type {
0x40 => {
// Transfer Syntax Sub-Item Structure
// 5-xxx - Transfer-syntax-name(s) - This variable field shall contain the
// Transfer-syntax-name proposed for this presentation context. A valid
// Transfer-syntax-name shall be encoded as defined in Annex F. For a
// description of the use of this field see Section 7.1.1.13.
// Transfer-syntax-names are structured as UIDs as defined in PS3.5 (see
// Annex B for an overview of this concept). DICOM Transfer-syntax-names are
// registered in PS3.5.
match transfer_syntax {
Some(_) => {
// Multiple transfer syntax values cannot be proposed.
return MultipleTransferSyntaxesAcceptedSnafu.fail();
}
None => {
if bytes.remaining() < item_length as usize {
return Ok(None);
}
transfer_syntax = Some(
codec
.decode(bytes.copy_to_bytes(item_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "Transfer-syntax-name",
})?
.trim()
.to_string(),
);
}
}
}
_ => {
return InvalidTransferSyntaxSubItemSnafu.fail();
}
}
}
Ok(Some(PduVariableItem::PresentationContextResult(
PresentationContextResult {
id: presentation_context_id,
reason,
transfer_syntax: transfer_syntax.context(MissingTransferSyntaxSnafu)?,
},
)))
}
0x50 => {
// User Information Item Structure
let mut user_variables = vec![];
// 5-xxx - User-data - This variable field shall contain User-data sub-items as defined
// by the DICOM Application Entity. The structure and content of these sub-items is
// defined in Annex D.
while bytes.has_remaining() {
// 1 - Item-type - XXH
if bytes.remaining() < 1 {
return Ok(None);
}
let item_type = bytes.get_u8();
// 2 - Reserved
if bytes.remaining() < 1 {
return Ok(None);
}
bytes.get_u8();
// 3-4 - Item-length
if bytes.remaining() < 2 {
return Ok(None);
}
let item_length = bytes.get_u16();
match item_type {
0x51 => {
// Maximum Length Sub-Item Structure
// 5-8 - Maximum-length-received - This parameter allows the
// association-requestor to restrict the maximum length of the variable
// field of the P-DATA-TF PDUs sent by the acceptor on the association once
// established. This length value is indicated as a number of bytes encoded
// as an unsigned binary number. The value of (0) indicates that no maximum
// length is specified. This maximum length value shall never be exceeded by
// the PDU length values used in the PDU-length field of the P-DATA-TF PDUs
// received by the association-requestor. Otherwise, it shall be a protocol
// error.
if bytes.remaining() < 4 {
return Ok(None);
}
user_variables.push(UserVariableItem::MaxLength(bytes.get_u32()));
}
0x52 => {
// Implementation Class UID Sub-Item Structure
// 5 - xxx - Implementation-class-uid - This variable field shall contain
// the Implementation-class-uid of the Association-acceptor as defined in
// Section D.3.3.2. The Implementation-class-uid field is structured as a
// UID as defined in PS3.5.
if bytes.remaining() < item_length as usize {
return Ok(None);
}
let implementation_class_uid = codec
.decode(bytes.copy_to_bytes(item_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "Implementation-class-uid",
})?
.trim()
.to_string();
user_variables.push(UserVariableItem::ImplementationClassUID(
implementation_class_uid,
));
}
0x55 => {
// Implementation Version Name Structure
// 5 - xxx - Implementation-version-name - This variable field shall contain
// the Implementation-version-name of the Association-acceptor as defined in
// Section D.3.3.2. It shall be encoded as a string of 1 to 16 ISO 646:1990
// (basic G0 set) characters.
if bytes.remaining() < item_length as usize {
return Ok(None);
}
let implementation_version_name = codec
.decode(bytes.copy_to_bytes(item_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "Implementation-version-name",
})?
.trim()
.to_string();
user_variables.push(UserVariableItem::ImplementationVersionName(
implementation_version_name,
));
}
0x56 => {
// SOP Class Extended Negotiation Sub-Item
// 5-6 - SOP-class-uid-length - The SOP-class-uid-length shall be the number
// of bytes from the first byte of the following field to the last byte of the
// SOP-class-uid field. It shall be encoded as an unsigned binary number.
if bytes.remaining() < 2 {
return Ok(None);
}
let sop_class_uid_length = bytes.get_u16();
// 7 - xxx - SOP-class-uid - The SOP Class or Meta SOP Class identifier
// encoded as a UID as defined in Section 9 “Unique Identifiers (UIDs)” in PS3.5.
if bytes.remaining() < sop_class_uid_length as usize {
return Ok(None);
}
let sop_class_uid = codec
.decode(bytes.copy_to_bytes(sop_class_uid_length as usize).as_ref())
.context(DecodeTextSnafu {
field: "SOP-class-uid",
})?
.trim()
.to_string();
// The fixed part of the Extended Negotiation Sub-Item length includes only
// the SOP Class UID's length, which is a 2-byte field. The variable part
// includes the SOP Class UID and the Service-Class-Application-Information
// (PS3.7 D.3.3.5.1). We want to calculate the size of the latter, which
// equals the total item length minus the other fixed and variable lengths.
let data_length = (item_length - 2 - sop_class_uid_length) as usize;
if bytes.remaining() < data_length {
return Ok(None);
}
// xxx-xxx - Service-class-application-information -This field shall contain
// the application information specific to the Service Class specification
// identified by the SOP-class-uid. The semantics and value of this field
// is defined in the identified Service Class specification.
let data = bytes.copy_to_bytes(data_length);
user_variables.push(UserVariableItem::SopClassExtendedNegotiationSubItem(
sop_class_uid,
data.to_vec(),
));
}
0x58 => {
// User Identity Negotiation
// 5 - User Identity Type
if bytes.remaining() < 1 {
return Ok(None);
}
let user_identity_type = bytes.get_u8();
// 6 - Positive-response-requested
if bytes.remaining() < 1 {
return Ok(None);
}
let positive_response_requested = bytes.get_u8();
// 7-8 - Primary Field Length
if bytes.remaining() < 2 {
return Ok(None);
}
let primary_field_length = bytes.get_u16();
// 9-n - Primary Field
if bytes.remaining() < primary_field_length as usize {
return Ok(None);
}
let primary_field = bytes.copy_to_bytes(primary_field_length as usize);
// n+1-n+2 - Secondary Field Length
// Only non-zero if user identity type is 2 (username and password)
if bytes.remaining() < 2 {
return Ok(None);
}
let secondary_field_length = bytes.get_u16();
// n+3-m - Secondary Field
if bytes.remaining() < secondary_field_length as usize {
return Ok(None);
}
let secondary_field = bytes.copy_to_bytes(secondary_field_length as usize);
match UserIdentityType::from(user_identity_type) {
Some(user_identity_type) => {
user_variables.push(UserVariableItem::UserIdentityItem(
UserIdentity::new(
positive_response_requested == 1,
user_identity_type,
primary_field.to_vec(),
secondary_field.to_vec(),
),
));
}
None => {
warn!("Unknown User Identity Type code {}", user_identity_type);
}
}
}
_ => {
if bytes.remaining() < item_length as usize {
return Ok(None);
}
user_variables.push(UserVariableItem::Unknown(
item_type,
bytes.copy_to_bytes(item_length as usize).to_vec(),
));
}
}
}
Ok(Some(PduVariableItem::UserVariables(user_variables)))
}
_ => Ok(Some(PduVariableItem::Unknown(item_type))),
}
}