File: dcasccfg.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (806 lines) | stat: -rw-r--r-- 25,391 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  Copyright (C) 2003-2022, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module:  dcmnet
 *
 *  Author:  Marco Eichelberg
 *
 *  Purpose:
 *    class DcmAssociationConfiguration
 *
 */

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmnet/dcasccfg.h"
#include "dcmtk/dcmdata/dcerror.h"  /* for EC_IllegalCall */

DcmAssociationConfiguration::DcmAssociationConfiguration()
: xferSyntaxes_()
, contexts_()
, roleselection_()
, extneg_()
, profiles_()
, alwaysAcceptDefaultRole_(OFFalse)
{
}

DcmAssociationConfiguration::~DcmAssociationConfiguration()
{
}

// Copy constructor, performs deep copy
DcmAssociationConfiguration::DcmAssociationConfiguration(const DcmAssociationConfiguration& arg) :
  xferSyntaxes_(arg.xferSyntaxes_),
  contexts_(arg.contexts_),
  roleselection_(arg.roleselection_),
  extneg_(arg.extneg_),
  profiles_(arg.profiles_)
{
}

/// Copy assignment operator, performs deep copy
DcmAssociationConfiguration& DcmAssociationConfiguration::operator=(const DcmAssociationConfiguration& arg)
{
  /* No self assignment */
  if (this != &arg)
  {
    this->clear();
    xferSyntaxes_ = arg.xferSyntaxes_;
    contexts_ = arg.contexts_;
    roleselection_ = arg.roleselection_;
    extneg_ = arg.extneg_;
    profiles_ = arg.profiles_;
  }
  return *this;
}

void DcmAssociationConfiguration::clear()
{
  xferSyntaxes_.clear();
  contexts_.clear();
  roleselection_.clear();
  extneg_.clear();
  profiles_.clear();
  alwaysAcceptDefaultRole_ = OFFalse;
}

OFCondition DcmAssociationConfiguration::addTransferSyntax(
  const char *key,
  const char *transferSyntaxUID)
{
  return xferSyntaxes_.add(key, transferSyntaxUID);
}

OFCondition DcmAssociationConfiguration::addPresentationContext(
  const char *key,
  const char *abstractSyntaxUID,
  const char *transferSyntaxKey,
  OFBool scuMode)
{
  if ((!key)||(!abstractSyntaxUID)||(!transferSyntaxKey)) return EC_IllegalCall;

  if (! xferSyntaxes_.isKnownKey(transferSyntaxKey))
  {
    // error: key undefined
    OFString s("transfer syntax key undefined: ");
    s += transferSyntaxKey;
    return makeOFCondition(OFM_dcmnet, 1031, OF_error, s.c_str());
  }

  return contexts_.add(key, abstractSyntaxUID, transferSyntaxKey, scuMode);
}

OFCondition DcmAssociationConfiguration::addRole(
  const char *key,
  const char *abstractSyntaxUID,
  T_ASC_SC_ROLE role)
{
  return roleselection_.add(key, abstractSyntaxUID, role);
}

OFCondition DcmAssociationConfiguration::createEmptyRoleList(
  const char* key)
{
  return roleselection_.addEmpty(key);
}

OFCondition DcmAssociationConfiguration::addExtendedNegotiation(
  const char *key,
  const char *abstractSyntaxUID,
  const unsigned char *rawData,
  Uint32 length)
{
  return extneg_.add(key, abstractSyntaxUID, rawData, length);
}

OFCondition DcmAssociationConfiguration::addProfile(
  const char *key,
  const char *presentationContextKey,
  const char *roleSelectionKey,
  const char *extendedNegotiationKey)
{
  if ((!key)||(!presentationContextKey)) return EC_IllegalCall;

  if (!contexts_.isKnownKey(presentationContextKey))
  {
    // error: key undefined
    OFString s("presentation context key undefined: ");
    s += presentationContextKey;
    return makeOFCondition(OFM_dcmnet, 1033, OF_error, s.c_str());
  }

  OFCondition status = EC_Normal;
  if (roleSelectionKey)
  {
    // check that role selection list exists and matches presentation context list
    status = roleselection_.checkConsistency(roleSelectionKey, contexts_, presentationContextKey);
    if (status.bad()) return status;
  }

  if (extendedNegotiationKey)
  {
    // check that extended negotiation list exists and matches presentation context list
    status = extneg_.checkConsistency(extendedNegotiationKey, contexts_, presentationContextKey);
    if (status.bad()) return status;
  }

  return profiles_.add(key, presentationContextKey, roleSelectionKey, extendedNegotiationKey);
}


OFBool DcmAssociationConfiguration::isKnownProfile(const char *key) const
{
  return profiles_.isKnownKey(key);
}


const DcmProfileEntry* DcmAssociationConfiguration::getProfileEntry(const OFString& profileName)
{
  OFMap<OFString, DcmProfileEntry*>::const_iterator it =  profiles_.begin();
  while ( it != profiles_.end() )
  {
    if ( (*it).first == profileName )
    {
      return (*it).second;
    }
    it++;
  }
  return NULL;
}


OFBool DcmAssociationConfiguration::isValidSCPProfile(const char *key) const
{
  const char *contextKey = profiles_.getPresentationContextKey(key);
  const DcmPresentationContextList *contextList = contexts_.getPresentationContextList(contextKey);
  if (contextList)
  {
    // now loop through all presentation contexts
    OFListConstIterator(DcmPresentationContextItem) outerfirst = contextList->begin();
    OFListConstIterator(DcmPresentationContextItem) innerfirst;
    OFListConstIterator(DcmPresentationContextItem) last = contextList->end();
    OFString outerAbstractSyntax;
    while (outerfirst != last)
    {
      innerfirst = outerfirst;
      ++innerfirst;
      outerAbstractSyntax = (*outerfirst).getAbstractSyntax();
      while (innerfirst != last)
      {
        if (outerAbstractSyntax == (*innerfirst).getAbstractSyntax())
        {
          // we have found two instances of the same abstract syntax in this list,
          // so this is not a valid SCP profile.
          return OFFalse;
        }
        ++innerfirst;
      }
      ++outerfirst;
    }
    // there are no duplicate abstract syntaxes, so this is a valid SCP profile.
    return OFTrue;
  }
  return OFFalse;
}


OFBool DcmAssociationConfiguration::isValidSCUProfile(const char *key) const
{
  const char *contextKey = profiles_.getPresentationContextKey(key);
  const DcmPresentationContextList *contextList = contexts_.getPresentationContextList(contextKey);
  if (contextList)
  {
    return (contextList->size() <= 128);
  }
  return OFFalse;

}


OFCondition DcmAssociationConfiguration::setAssociationParameters(
  const char *profile,
  T_ASC_Parameters& params) const
{
  if (!profile) return EC_IllegalCall;

  const char *contextKey = profiles_.getPresentationContextKey(profile);
  if (! contextKey)
  {
    // error: key undefined
    OFString s("profile key undefined: ");
    s += profile;
    return makeOFCondition(OFM_dcmnet, 1062, OF_error, s.c_str());
  }

  // these two keys may be NULL
  const char *roleKey = profiles_.getRoleSelectionKey(profile);
  const char *extnegKey = profiles_.getExtendedNegotiationKey(profile);

  // pointer to list of presentation contexts
  const DcmPresentationContextList *contextList = contexts_.getPresentationContextList(contextKey);
  if (! contextList)
  {
    // error: key undefined
    OFString s("presentation context key undefined: ");
    s += contextKey;
    return makeOFCondition(OFM_dcmnet, 1063, OF_error, s.c_str());
  }

  // pointer to list of SCP/SCU role selection items
  const DcmRoleSelectionList *roleSelectionList = NULL;
  if (roleKey)
  {
    roleSelectionList = roleselection_.getRoleSelectionList(roleKey);
    if (! roleSelectionList)
    {
      // error: key undefined
      OFString s("role selection key undefined: ");
      s += roleKey;
      return makeOFCondition(OFM_dcmnet, 1064, OF_error, s.c_str());
    }
  }

  // pointer to list of SCP/SCU role selection items
  const DcmExtendedNegotiationList *extendedNegotiationList = NULL;
  if (extnegKey)
  {
    extendedNegotiationList = extneg_.getExtendedNegotiationList(extnegKey);
    if (! extendedNegotiationList)
    {
      // error: key undefined
      OFString s("extended negotiation key undefined: ");
      s += extnegKey;
      return makeOFCondition(OFM_dcmnet, 1065, OF_error, s.c_str());
    }
  }

  // initialize presentation context ID counter
  T_ASC_PresentationContextID cid = 1;
  OFCondition result = EC_Normal;
  const char *abstractSyntax = NULL;
  const char *transferSyntaxKey = NULL;
  const DcmTransferSyntaxList *transferSyntaxList = NULL;
  OFListConstIterator(DcmUIDHandler) xffirst;
  OFListConstIterator(DcmUIDHandler) xflast;
  int numXferSyntaxes = 0;
  const char **xferSyntaxes = NULL;
  T_ASC_SC_ROLE proposedRole;
  OFListConstIterator(DcmRoleSelectionItem) rsfirst;
  OFListConstIterator(DcmRoleSelectionItem) rslast;
  DcmUIDHandler uid;

  // now loop through all presentation contexts
  OFListConstIterator(DcmPresentationContextItem) first = contextList->begin();
  OFListConstIterator(DcmPresentationContextItem) last = contextList->end();
  while (first != last)
  {
    // determine abstract syntax
    abstractSyntax = (*first).getAbstractSyntax();
    transferSyntaxKey = (*first).getTransferSyntaxKey();
    transferSyntaxList = xferSyntaxes_.getTransferSyntaxList(transferSyntaxKey);

    if (! transferSyntaxList)
    {
      // error: key undefined
      OFString s("transfer syntax key undefined: ");
      s += transferSyntaxKey;
      return makeOFCondition(OFM_dcmnet, 1066, OF_error, s.c_str());
    }

    size_t transferSyntaxList_size = transferSyntaxList->size();

    if (transferSyntaxList_size < 1)
    {
      // error: key undefined
      OFString s("transfer syntax list empty: ");
      s += transferSyntaxKey;
      return makeOFCondition(OFM_dcmnet, 1068, OF_error, s.c_str());
    }

    // copy list of transfer syntaxes into array
    xferSyntaxes = new const char *[transferSyntaxList_size];
    numXferSyntaxes = 0;
    xffirst = transferSyntaxList->begin();
    xflast  = transferSyntaxList->end();
    while (xffirst != xflast)
    {
      xferSyntaxes[numXferSyntaxes++] = (*xffirst).c_str();
      ++xffirst;
    }

    // look up SCP/SCU role for this abstract syntax
    proposedRole = ASC_SC_ROLE_DEFAULT;
    if (roleSelectionList)
    {
      // look-up abstract syntax in role selection list
      rsfirst = roleSelectionList->begin();
      rslast = roleSelectionList->end();
      uid = abstractSyntax;
      while (rsfirst != rslast)
      {
        if ((*rsfirst).matches(uid))
        {
          // found abstract syntax, set role accordingly
          proposedRole = (*rsfirst).getRole();
          break; /* out of while loop */
        }
        ++rsfirst;
      }
    }

    // finally add presentation context to params
    result = ASC_addPresentationContext(&params, cid, abstractSyntax, xferSyntaxes, numXferSyntaxes, proposedRole);
    if (result.bad())
    {
      delete[] xferSyntaxes;
      return result;
    }

    // increase index by 2
    cid += 2;
    ++first;
  }

  // create extended negotiation items if present
  if (extendedNegotiationList)
  {
    // create extended negotiation item list as required by dcmnet code
    SOPClassExtendedNegotiationSubItemList *enlist = new SOPClassExtendedNegotiationSubItemList();
    ASC_setRequestedExtNegList(&params, enlist);
    SOPClassExtendedNegotiationSubItem *currentItem = NULL;

    // for each entry in our list
    OFListConstIterator(DcmExtendedNegotiationItem) enfirst = extendedNegotiationList->begin();
    OFListConstIterator(DcmExtendedNegotiationItem) enlast = extendedNegotiationList->end();
    while (enfirst != enlast)
    {
      // create and populate SOPClassExtendedNegotiationSubItem object
      currentItem = new SOPClassExtendedNegotiationSubItem();
      currentItem->sopClassUID = (*enfirst).getAbstractSyntaxC();
      currentItem->serviceClassAppInfoLength = OFstatic_cast(unsigned short, (*enfirst).getLength());
      currentItem->serviceClassAppInfo = new unsigned char[currentItem->serviceClassAppInfoLength];
      memcpy(currentItem->serviceClassAppInfo, (*enfirst).getRaw() , currentItem->serviceClassAppInfoLength);
      enlist->push_back(currentItem);
      ++enfirst;
    }
  }

  delete[] xferSyntaxes;
  return result;
}

OFCondition DcmAssociationConfiguration::evaluateAssociationParameters(
  const char *profile,
  T_ASC_Association &assoc) const
{
  if (!profile) return EC_IllegalCall;

  const char *contextKey = profiles_.getPresentationContextKey(profile);
  if (! contextKey)
  {
    // error: key undefined
    OFString s("profile key undefined: ");
    s += profile;
    return makeOFCondition(OFM_dcmnet, 1069, OF_error, s.c_str());
  }

  // these two keys may be NULL
  const char *roleKey = profiles_.getRoleSelectionKey(profile);
  const char *extnegKey = profiles_.getExtendedNegotiationKey(profile);

  // pointer to list of presentation contexts
  const DcmPresentationContextList *contextList = contexts_.getPresentationContextList(contextKey);
  if (! contextList)
  {
    // error: key undefined
    OFString s("presentation context key undefined: ");
    s += contextKey;
    return makeOFCondition(OFM_dcmnet, 1070, OF_error, s.c_str());
  }

  // pointer to list of SCP/SCU role selection items
  const DcmRoleSelectionList *roleSelectionList = NULL;
  if (roleKey)
  {
    roleSelectionList = roleselection_.getRoleSelectionList(roleKey);
    if (! roleSelectionList)
    {
      // error: key undefined
      OFString s("role selection key undefined: ");
      s += roleKey;
      return makeOFCondition(OFM_dcmnet, 1071, OF_error, s.c_str());
    }
  }

  // pointer to list of SCP/SCU role selection items
  const DcmExtendedNegotiationList *extendedNegotiationList = NULL;
  if (extnegKey)
  {
    extendedNegotiationList = extneg_.getExtendedNegotiationList(extnegKey);
    if (! extendedNegotiationList)
    {
      // error: key undefined
      OFString s("extended negotiation key undefined: ");
      s += extnegKey;
      return makeOFCondition(OFM_dcmnet, 1072, OF_error, s.c_str());
    }
  }

  // declare a few variables we need later
  OFCondition result = EC_Normal;
  T_ASC_PresentationContext pc;
  const char *transferSyntaxKey = NULL;
  OFBool found;
  T_ASC_SC_ROLE acceptedRole;
  OFListConstIterator(DcmRoleSelectionItem) rsfirst;
  OFListConstIterator(DcmRoleSelectionItem) rslast;
  DcmUIDHandler uid;
  const DcmTransferSyntaxList *transferSyntaxList = NULL;
  OFListConstIterator(DcmPresentationContextItem) first;
  OFListConstIterator(DcmPresentationContextItem) last = contextList->end();
  OFListConstIterator(DcmUIDHandler) tsfirst;
  OFListConstIterator(DcmUIDHandler) tslast;
  int numContexts = ASC_countPresentationContexts(assoc.params);
  unsigned char j = 0;

  // traverse list of presentation contexts
  for (int i = 0; i < numContexts; ++i)
  {
    // retrieve presentation context
    result = ASC_getPresentationContext(assoc.params, i, &pc);
    if (result.bad()) return result;

    // check if the abstract syntax is in our list of abstract syntaxes
    first = contextList->begin();
    found = OFFalse;
    while (first != last)
    {
      if (0 == strcmp((*first).getAbstractSyntax(), pc.abstractSyntax))
      {
        found = OFTrue;
        break; /* out of while loop */
      }
      ++first;
    }

    if (!found)
    {
      // abstract syntax not supported, reject presentation context
      result = ASC_refusePresentationContext(assoc.params, pc.presentationContextID,
        ASC_P_ABSTRACTSYNTAXNOTSUPPORTED);
      if (result.bad()) return result;
    }
    else
    {
      // abstract syntax is supported, get transfer syntax list and SCP/SCU role
      transferSyntaxKey = (*first).getTransferSyntaxKey();
      transferSyntaxList = xferSyntaxes_.getTransferSyntaxList(transferSyntaxKey);
      if (! transferSyntaxList)
      {
        // error: key undefined
        OFString s("transfer syntax key undefined: ");
        s += transferSyntaxKey;
        return makeOFCondition(OFM_dcmnet, 1073, OF_error, s.c_str());
      }

      // look up SCP/SCU role for this abstract syntax in configuration.
      // default is no role selection, thus, default role applies, i.e.
      // no role selection item would be sent back
      acceptedRole = ASC_SC_ROLE_DEFAULT;
      if (roleSelectionList)
      {
        // look-up abstract syntax in role selection list
        rsfirst = roleSelectionList->begin();
        rslast = roleSelectionList->end();
        uid = pc.abstractSyntax;
        while (rsfirst != rslast)
        {
          if ((*rsfirst).matches(uid))
          {
            // found explicit role for this abstract syntax in configuration, set role accordingly
            acceptedRole = (*rsfirst).getRole();
            break; /* out of while loop */
          }
          ++rsfirst;
        }
      }

      // try to accept the presentation context with the most preferred transfer syntax
      tsfirst = transferSyntaxList->begin();
      tslast = transferSyntaxList->end();
      found = OFFalse;

      // loop through our list of preferred transfer syntaxes for this abstract syntax,
      // starting with the most preferred one
      while ((tsfirst != tslast) && !found)
      {
        // loop through list of transfer syntaxes in presentation context
        for (j=0; (!found && (j < pc.transferSyntaxCount)); ++j)
        {
          if ((*tsfirst).str() == pc.proposedTransferSyntaxes[j])
          {
            // matching transfer syntax found, accept context
            found = OFTrue;
            result = ASC_acceptPresentationContext(
                assoc.params, pc.presentationContextID,
                pc.proposedTransferSyntaxes[j], acceptedRole, alwaysAcceptDefaultRole_);
            // SCP/SCU role selection failed, reject presentation context
            if (result == ASC_SCPSCUROLESELECTIONFAILED)
            {
              result = ASC_refusePresentationContext(assoc.params,
                pc.presentationContextID, ASC_P_NOREASON);
            }
            if (result.bad()) return result;
          }
        }
        ++tsfirst;
      }

      // no matching transfer syntax found, reject context
      if (!found)
      {
        // transfer syntax not supported, reject presentation context
        result = ASC_refusePresentationContext(assoc.params, pc.presentationContextID,
          ASC_P_TRANSFERSYNTAXESNOTSUPPORTED);
        if (result.bad()) return result;
      }
    }
  }

  // create extended negotiation items if present
  if (extendedNegotiationList)
  {
    // create extended negotiation item list as required by dcmnet code
    SOPClassExtendedNegotiationSubItemList *enlist = new SOPClassExtendedNegotiationSubItemList();
    SOPClassExtendedNegotiationSubItem *currentItem = NULL;

    // for each entry in our list
    OFListConstIterator(DcmExtendedNegotiationItem) enfirst = extendedNegotiationList->begin();
    OFListConstIterator(DcmExtendedNegotiationItem) enlast = extendedNegotiationList->end();
    while (enfirst != enlast)
    {
      // check if we have an accepted presentation context for this abstract syntax
      if (0 != ASC_findAcceptedPresentationContextID(&assoc, (*enfirst).getAbstractSyntaxC()))
      {
        // create and populate SOPClassExtendedNegotiationSubItem object
        currentItem = new SOPClassExtendedNegotiationSubItem();
        currentItem->sopClassUID = (*enfirst).getAbstractSyntaxC();
        currentItem->serviceClassAppInfoLength = OFstatic_cast(unsigned short, (*enfirst).getLength());
        currentItem->serviceClassAppInfo = new unsigned char[currentItem->serviceClassAppInfoLength];
        memcpy(currentItem->serviceClassAppInfo, (*enfirst).getRaw() , currentItem->serviceClassAppInfoLength);
        enlist->push_back(currentItem);
      }
      ++enfirst;
    }

    if (enlist->size() > 0)
      ASC_setAcceptedExtNegList(assoc.params, enlist);
      else delete enlist;
  }

  return result;
}

void DcmAssociationConfiguration::setAlwaysAcceptDefaultRole(const OFBool enabled)
{
  alwaysAcceptDefaultRole_ = enabled;
}



void DcmAssociationConfiguration::dumpProfiles(
    STD_NAMESPACE ostream &out,
    const OFString& profileName)
{
  const DcmProfileEntry* profile = NULL;
  // dump single profile if desired
  if ( !profileName.empty() )
  {
    profile = profiles_.getProfile(profileName);
    if (profile == NULL)
    {
      out << "No such profile: " << profileName << OFendl;
      return;
    }
    else
    {
      dumpProfile(out, profile, profileName);
    }
  }
  else
  {
    // no profile given: dump all profiles
    OFMap<OFString, DcmProfileEntry*>::const_iterator it = profiles_.begin();
    OFMap<OFString, DcmProfileEntry*>::const_iterator end = profiles_.end();
    if ( it == end )
    {
      out << "No profiles defined" << OFendl;
      return;
    }
    while ( it != end )
    {
      dumpProfile(out, (*it).second, profileName);
      it++;
    }
  }
  return;
}


void DcmAssociationConfiguration::dumpProfile(
  STD_NAMESPACE ostream &out,
  const DcmProfileEntry* profile,
  const OFString& profileName)
{
  if ( profile == NULL )
  {
    out << "No such profile (NULL)" << OFendl;
    return;
  }
  // print header for this profile
  out << "-----------------------------------------------------------" << OFendl;
  out << "Dumping Profile: " << profileName << OFendl;

  OFString presContext = profile->getPresentationContextKey();
  if ( presContext.empty() )
  {
    out << "Presentation context list name empty, no presentation contexts configured" << OFendl;
  }
  else
  {
    // print all presentation contexts
    const DcmPresentationContextList* pclist = contexts_.getPresentationContextList(presContext);
    if ( pclist != NULL )
    {
      out << "Dumping presentation context list " << presContext << ": " << OFendl;
      size_t count = 1;
      // for each presentation context, list all of its configured transfer syntaxes
      OFListConstIterator(DcmPresentationContextItem) pc = pclist->begin();
      while ( pc != pclist->end() )
      {
        OFString ts = (*pc).getTransferSyntaxKey();
        const DcmTransferSyntaxList *tsList = NULL;
        if (!ts.empty())
        {
          tsList = xferSyntaxes_.getTransferSyntaxList(ts.c_str());
        }
        out << "Presentation Context #" << count << ", abstract syntax: " << pc->getAbstractSyntax() << OFendl;
        out << "Dumping Transfer Syntaxes list " << ts << OFendl;
        if ( ts.empty() || (tsList == NULL) || (tsList->empty()))
        {
          out << "None defined" << OFendl;
        }
        else
        {
          OFListConstIterator(DcmUIDHandler) uid = tsList->begin();
          size_t tsnum = 1;
          while ( uid != tsList->end() )
          {
            out << "  Transfer Syntax #" << tsnum << ": " << (uid)->c_str() << OFendl;
            uid++;
          }
        }
        pc++;
        count++;
      }
    }
    else
    {
       out << "No such presentation context list with that name: " << presContext << OFendl;
    }
  }
  // dump extended negotiation items
  const char * ext = profile->getExtendedNegotiationKey();
  if ( ext )
  {
    out << "Extended negotiation " << ext << " configured (not dumped)" << OFendl;
  }
  else
  {
    out << "No extended negotiation configured" << OFendl;
  }
  // dump role selection items
  const char* role = profile->getRoleSelectionKey();
  if ( role )
  {
     out << "Dumping Role selection items " << role << OFendl;
     const DcmRoleSelectionList* roleList = roleselection_.getRoleSelectionList(role);
     if ( roleList )
     {
       OFListConstIterator(DcmRoleSelectionItem) item = roleList->begin();
       size_t count = 1;
       while ( item != roleList->end() )
       {
         out << "  Item #" << count << ": Abstract syntax " << (*item).getAbstractSyntax().str() << ", role " << ASC_role2String((*item).getRole()) << OFendl;
         item++; count++;
       }
     }
     else
     {
       out << "Error: Role list is configured but empty" << OFendl;
     }
  }
  else
  {
    out << "No role selection items configured" << OFendl;
  }
  // print footer for this profile
  out << "-----------------------------------------------------------" << OFendl;
}

OFString DcmAssociationConfiguration::findTSKey(
  const OFList<OFString>& tslist)
{
  if ( tslist.empty() )
    return "";

  // loop over all transfer syntax lists configured
  OFMap<OFString, DcmTransferSyntaxList*>::const_iterator configIT = xferSyntaxes_.begin();
  while (configIT !=  xferSyntaxes_.end())
  {
    // loop over transfer syntaxes in one of the configured lists
    // lists must have at least the same size, otherwise this is not a match
    if ( (*configIT).second->size() == tslist.size())
    {
      OFListIterator(DcmUIDHandler) configTS = (*configIT).second->begin(); // single configured ts
      OFListConstIterator(OFString) inputTS = tslist.begin(); // single input ts
      OFBool isMatch = OFTrue;
      // now compare each configured ts in list with each input ts pairwise.
      // this works since size is the same and order is significant
      while (configTS !=  (*configIT).second->end())
      {
        // as long as we ts match, continue
        if ( (*configTS).str() == *inputTS )
        {
          configTS++;
          inputTS++;
        }
        else
        {
          isMatch = OFFalse;
          break;
        }
      }
      // if we found a match, return the ts key of the matching list
      if ( isMatch )
      {
        return (*configIT).first;
      }
    } // else ts list sizes differ -> no match
    // try next configured ts list
    configIT++;
  }

  // no matching ts list found, return empty key
  return "";
}