File: dcasccff.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 (485 lines) | stat: -rw-r--r-- 13,247 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
/*
 *
 *  Copyright (C) 2003-2024, 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 DcmAssociationConfigurationFile
 *
 */

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmnet/dcasccff.h"
#include "dcmtk/dcmnet/dcasccfg.h"  /* for class DcmAssociationConfiguration */
#include "dcmtk/dcmdata/dcerror.h"  /* for EC_IllegalCall */
#include "dcmtk/ofstd/ofconfig.h"   /* for class OFConfigFile */
#include "dcmtk/ofstd/ofstd.h"

/* config file keys for the association configuration */

#define L2_EXTENDEDNEGOTIATION         "EXTENDEDNEGOTIATION"
#define L2_PRESENTATIONCONTEXTS        "PRESENTATIONCONTEXTS"
#define L2_PROFILES                    "PROFILES"
#define L2_SCPSCUROLESELECTION         "SCPSCUROLESELECTION"
#define L2_TRANSFERSYNTAXES            "TRANSFERSYNTAXES"

#define L0_EXTENDEDNEGOTIATION         "EXTENDEDNEGOTIATION"
#define L0_EXTENDEDNEGOTIATION_X       "EXTENDEDNEGOTIATION"
#define L0_PRESENTATIONCONTEXTS        "PRESENTATIONCONTEXTS"
#define L0_PRESENTATIONCONTEXT_X       "PRESENTATIONCONTEXT"
#define L0_ROLE_X                      "ROLE"
#define L0_SCPSCUROLESELECTION         "SCPSCUROLESELECTION"
#define L0_TRANSFERSYNTAX_X            "TRANSFERSYNTAX"


OFCondition DcmAssociationConfigurationFile::initialize(
  DcmAssociationConfiguration& cfg,
  const char *filename,
  OFBool scuMode)
{
  if (!filename) return EC_IllegalCall;

  FILE *cfgfile = fopen(filename, "rb");
  if (cfgfile == NULL)
  {
    OFString s("unable to open configuration file: ");
    s += filename;
    return makeOFCondition(OFM_dcmnet, 1042, OF_error, s.c_str());
  }

  OFConfigFile config(cfgfile);
  fclose(cfgfile);

  // parse transfer syntaxes
  OFCondition result = parseTransferSyntaxes(cfg, config);
  if (result.bad()) return result;

  // parse presentation contexts
  result = parsePresentationContexts(cfg, config, scuMode);
  if (result.bad()) return result;

  // parse role selection items
  result = parseRoleSelectionItems(cfg, config);
  if (result.bad()) return result;

  // parse extended negotiation items
  result = parseExtendedNegotiationItems(cfg, config);
  if (result.bad()) return result;

  // parse profiles
  result = parseProfiles(cfg, config);

  return result;
}

OFCondition DcmAssociationConfigurationFile::parseTransferSyntaxes(
  DcmAssociationConfiguration& cfg,
  OFConfigFile& config)
{
  config.set_section(2, L2_TRANSFERSYNTAXES);
  if (! config.section_valid(2))
  {
    OFString s("cannot find section [[");
    s += L2_TRANSFERSYNTAXES;
    s += "]] in config file";
    return makeOFCondition(OFM_dcmnet, 1050, OF_error, s.c_str());
  }

  char buf[64];
  unsigned int counter;
  OFBool found;
  const char *key = NULL;
  const char *value = NULL;
  OFCondition result = EC_Normal;

  config.first_section(1);
  while (config.section_valid(1))
  {
    key = config.get_keyword(1);
    counter = 0;
    found = OFTrue;
    while (found)
    {
      OFStandard::snprintf(buf, sizeof(buf), "%s%u", L0_TRANSFERSYNTAX_X, ++counter);
      value = config.get_entry(buf);
      if (value)
      {
        result = cfg.addTransferSyntax(key, value);
        if (result.bad()) return result;
      } else found = OFFalse;
    }
    config.next_section(1);
  }

  return result;
}

OFCondition DcmAssociationConfigurationFile::parsePresentationContexts(
  DcmAssociationConfiguration& cfg,
  OFConfigFile& config,
  OFBool scuMode)
{
  config.set_section(2, L2_PRESENTATIONCONTEXTS);
  if (! config.section_valid(2))
  {
    OFString s("cannot find section [[");
    s += L2_PRESENTATIONCONTEXTS;
    s += "]] in config file";
    return makeOFCondition(OFM_dcmnet, 1051, OF_error, s.c_str());
  }

  char buf[64];
  unsigned int counter;
  OFBool found;
  const char *key = NULL;
  const char *value = NULL;
  OFString abstractSyntaxUID;
  OFString transferSyntaxKey;
  size_t separator = 0;
  size_t i;
  size_t len;
  unsigned char c;
  OFCondition result = EC_Normal;

  config.first_section(1);
  while (config.section_valid(1))
  {
    key = config.get_keyword(1);
    counter = 0;
    found = OFTrue;
    while (found)
    {
      OFStandard::snprintf(buf, sizeof(buf), "%s%u", L0_PRESENTATIONCONTEXT_X, ++counter);
      value = config.get_entry(buf);
      if (value)
      {
        // value must consist of abstract syntax and transfer syntax, separated by '\\'
        abstractSyntaxUID = value;
        len = abstractSyntaxUID.size();
        separator = abstractSyntaxUID.find("\\");
        if (separator == OFString_npos)
        {
          OFString s("syntax error: missing '\\' in entry ");
          s += buf;
          s += " in config file";
          return makeOFCondition(OFM_dcmnet, 1052, OF_error, s.c_str());
        }

        // do name mangling for transfer syntax key
        transferSyntaxKey.clear();
        i = separator;
        while (++i < len)
        {
          c = abstractSyntaxUID.at(i);
          if (! OFStandard::isspace(c)) transferSyntaxKey += (char) (toupper(c));
        }

        // finally cut abstract syntax name
        abstractSyntaxUID.erase(separator);

        result = cfg.addPresentationContext(key, abstractSyntaxUID.c_str(), transferSyntaxKey.c_str(), scuMode);
        if (result.bad()) return result;

      } else found = OFFalse;
    }
    config.next_section(1);
  }

  return result;
}


#define TO_UCHAR(s) OFstatic_cast(unsigned char, (s))
OFCondition DcmAssociationConfigurationFile::parseRoleSelectionItems(
  DcmAssociationConfiguration& cfg,
  OFConfigFile& config)
{
  OFCondition result = EC_Normal;

  config.set_section(2, L2_SCPSCUROLESELECTION);
  if (! config.section_valid(2)) return result; // SCP/SCU role selection is optional, may be absent

  char buf[64];
  unsigned int counter;
  OFBool found;
  const char *key = NULL;
  const char *value = NULL;
  OFString abstractSyntaxUID;
  OFString roleKey;
  size_t separator = 0;
  size_t i;
  size_t len;
  char c;

  config.first_section(1);
  while (config.section_valid(1))
  {
    key = config.get_keyword(1);
    counter = 0;
    found = OFTrue;
    while (found)
    {
      OFStandard::snprintf(buf, sizeof(buf), "%s%u", L0_ROLE_X, ++counter);
      value = config.get_entry(buf);
      if (value)
      {
        // value must consist of abstract syntax and role, separated by '\\'
        abstractSyntaxUID = value;
        len = abstractSyntaxUID.size();
        separator = abstractSyntaxUID.find("\\");
        if (separator == OFString_npos)
        {
          OFString s("syntax error: missing '\\' in entry ");
          s += buf;
          s += " in config file";
          return makeOFCondition(OFM_dcmnet, 1054, OF_error, s.c_str());
        }

        // do name mangling for role key
        roleKey.clear();
        i = separator;
        while (++i < len)
        {
          c = abstractSyntaxUID.at(i);
          roleKey += (char) (toupper(TO_UCHAR(c)));
        }

        // finally cut abstract syntax name
        abstractSyntaxUID.erase(separator);

        // check role key
        if (roleKey == "SCU")
        {
          result = cfg.addRole(key, abstractSyntaxUID.c_str(), ASC_SC_ROLE_SCU);
        }
        else if (roleKey == "SCP")
        {
          result = cfg.addRole(key, abstractSyntaxUID.c_str(), ASC_SC_ROLE_SCP);
        }
        else if (roleKey == "BOTH")
        {
          result = cfg.addRole(key, abstractSyntaxUID.c_str(), ASC_SC_ROLE_SCUSCP);
        }
        else
        {
          OFString s("syntax error: unknown role key in entry ");
          s += buf;
          s += " in config file";
          result = makeOFCondition(OFM_dcmnet, 1055, OF_error, s.c_str());
        }

        if (result.bad()) return result;

      } else found = OFFalse;
    }
    config.next_section(1);
  }

  return result;
}


OFCondition DcmAssociationConfigurationFile::parseExtendedNegotiationItems(
  DcmAssociationConfiguration& cfg,
  OFConfigFile& config)
{
  OFCondition result = EC_Normal;

  config.set_section(2, L2_EXTENDEDNEGOTIATION);
  if (! config.section_valid(2)) return result; // extended negotiation is optional, may be absent

  char buf[64];
  unsigned char raw[1024];
  size_t rawSize = 0;
  unsigned int counter;
  OFBool found;
  const char *key = NULL;
  const char *value = NULL;
  OFString abstractSyntaxUID;
  OFString tempString;
  size_t separator = 0;
  size_t i;
  size_t len;
  unsigned short us = 0;
  OFBool parseError = OFFalse;
  char c;

  config.first_section(1);
  while (config.section_valid(1))
  {
    key = config.get_keyword(1);
    counter = 0;
    found = OFTrue;
    while (found)
    {
      OFStandard::snprintf(buf, sizeof(buf), "%s%u", L0_EXTENDEDNEGOTIATION_X, ++counter);
      value = config.get_entry(buf);
      if (value)
      {
        // value must consist of abstract syntax and extended negotiation data, separated by '\\'
        abstractSyntaxUID = value;
        len = abstractSyntaxUID.size();
        separator = abstractSyntaxUID.find("\\");
        if (separator == OFString_npos)
        {
          OFString s("syntax error: missing '\\' in entry ");
          s += buf;
          s += " in config file";
          return makeOFCondition(OFM_dcmnet, 1057, OF_error, s.c_str());
        }

        if (len - separator > 2048)
        {
          OFString s("extended negotiation data too long, cannot handle > 1024 bytes in entry ");
          s += buf;
          s += " in config file";
          return makeOFCondition(OFM_dcmnet, 1058, OF_error, s.c_str());
        }

        // do hex to binary conversion for raw data
        tempString.clear();
        i = separator + 1;
        rawSize = 0;
        while (i < len)
        {
          tempString = abstractSyntaxUID.substr(i, 2);
          parseError = OFTrue;
          if (tempString.size() == 2)
          {
            c = tempString[0];
            if (((c >= '0') && (c <= '9'))||((c >= 'a') && (c <= 'f'))||((c >= 'A') && (c <= 'F')))
            {
              c = tempString[1];
              if (((c >= '0') && (c <= '9'))||((c >= 'a') && (c <= 'f'))||((c >= 'A') && (c <= 'F')))
              {
                if (1 == sscanf(tempString.c_str(), "%hx", &us))
                {
                  raw[rawSize++] = (unsigned char) us;
                  parseError = OFFalse;
                }
              }
            }
          }

          if (parseError)
          {
            OFString s("parse error in extended negotiation data in entry ");
            s += buf;
            s += " in config file";
            return makeOFCondition(OFM_dcmnet, 1059, OF_error, s.c_str());
          }
          i += 2;
        }

        // finally cut abstract syntax name
        abstractSyntaxUID.erase(separator);

        result = cfg.addExtendedNegotiation(key, abstractSyntaxUID.c_str(), raw, (Uint32) rawSize);
        if (result.bad()) return result;

      } else found = OFFalse;
    }
    config.next_section(1);
  }

  return result;
}


OFCondition DcmAssociationConfigurationFile::parseProfiles(
  DcmAssociationConfiguration& cfg,
  OFConfigFile& config)
{
  config.set_section(2, L2_PROFILES);
  if (! config.section_valid(2))
  {
    OFString s("cannot find section [[");
    s += L2_PROFILES;
    s += "]] in config file";
    return makeOFCondition(OFM_dcmnet, 1060, OF_error, s.c_str());
  }

  const char *key = NULL;
  const char *context = NULL;
  const char *role = NULL;
  const char *extneg = NULL;
  OFString scontext;
  OFString srole;
  OFString sextneg;
  const char *c;
  OFCondition result = EC_Normal;

  config.first_section(1);
  while (config.section_valid(1))
  {
    key = config.get_keyword(1);
    context = config.get_entry(L0_PRESENTATIONCONTEXTS);
    if (! context)
    {
      OFString s("no ");
      s += L0_PRESENTATIONCONTEXTS;
      s += "  entry in section ";
      s += key;
      s += " in config file";
      return makeOFCondition(OFM_dcmnet, 1061, OF_error, s.c_str());
    }
    role = config.get_entry(L0_SCPSCUROLESELECTION);
    extneg = config.get_entry(L0_EXTENDEDNEGOTIATION);

    // do name mangling for presentation context key
    c = context;
    scontext.clear();
    while (*c)
    {
      if (! OFStandard::isspace(*c)) scontext += (char) (toupper(TO_UCHAR(*c)));
      ++c;
    }
    context = scontext.c_str();

    // do name mangling for role selection key
    if (role)
    {
      c = role;
      srole.clear();
      while (*c)
      {
        if (! OFStandard::isspace(*c)) srole += (char) (toupper(TO_UCHAR(*c)));
        ++c;
      }
      role = srole.c_str();
    }

    // do name mangling for extended negotiation key
    if (extneg)
    {
      c = extneg;
      sextneg.clear();
      while (*c)
      {
        if (! OFStandard::isspace(*c)) sextneg += (char) (toupper(TO_UCHAR(*c)));
        ++c;
      }
      extneg = sextneg.c_str();
    }

    result = cfg.addProfile(key, context, role, extneg);
    if (result.bad()) return result;
    config.next_section(1);
  }

  return result;
}

#undef TO_UCHAR