ASN_Null.cc 21.4 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *   Tatarka, Gabor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
23
24
25
26
27
28
29
30
31
#include <stdarg.h>

#include "ASN_Null.hh"
#include "Parameters.h"
#include "Param_Types.hh"
#include "Error.hh"
#include "Logger.hh"
#include "Encdec.hh"
#include "BER.hh"
32
#include "OER.hh"
Elemer Lelik's avatar
Elemer Lelik committed
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

#include "../common/dbgnew.hh"

ASN_NULL::ASN_NULL()
{
  bound_flag = FALSE;
}

ASN_NULL::ASN_NULL(asn_null_type)
{
  bound_flag = TRUE;
}

ASN_NULL::ASN_NULL(const ASN_NULL& other_value)
: Base_Type(other_value)
{
  if (!other_value.bound_flag)
    TTCN_error("Copying an unbound ASN.1 NULL value.");
  bound_flag = TRUE;
}

ASN_NULL& ASN_NULL::operator=(asn_null_type)
{
  bound_flag = TRUE;
  return *this;
}

ASN_NULL& ASN_NULL::operator=(const ASN_NULL& other_value)
{
  if (!other_value.bound_flag)
    TTCN_error("Assignment of an unbound ASN.1 NULL value.");
  bound_flag = TRUE;
  return *this;
}

boolean ASN_NULL::operator==(asn_null_type) const
{
  if (!bound_flag) TTCN_error("The left operand of comparison is an unbound "
    "ASN.1 NULL value.");
  return TRUE;
}

boolean ASN_NULL::operator==(const ASN_NULL& other_value) const
{
  if (!bound_flag) TTCN_error("The left operand of comparison is an unbound "
    "ASN.1 NULL value.");
  if (!other_value.bound_flag) TTCN_error("The right operand of comparison "
    "is an unbound ASN.1 NULL value.");
  return TRUE;
}

void ASN_NULL::log() const
{
  if (bound_flag) TTCN_Logger::log_event_str("NULL");
  else TTCN_Logger::log_event_unbound();
}

void ASN_NULL::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_VALUE, "NULL value");
Elemer Lelik's avatar
Elemer Lelik committed
92
  Module_Param_Ptr mp = &param;
93
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
94
95
96
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
97
#endif
Elemer Lelik's avatar
Elemer Lelik committed
98
  if (mp->get_type()!=Module_Param::MP_Asn_Null) param.type_error("NULL value");
Elemer Lelik's avatar
Elemer Lelik committed
99
100
101
  bound_flag = TRUE;
}

102
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
103
104
105
106
107
108
109
Module_Param* ASN_NULL::get_param(Module_Param_Name& /* param_name */) const
{
  if (!is_bound()) {
    return new Module_Param_Unbound();
  }
  return new Module_Param_Asn_Null();
}
110
#endif
Elemer Lelik's avatar
Elemer Lelik committed
111

Elemer Lelik's avatar
Elemer Lelik committed
112
113
114
115
116
117
118
119
120
121
122
123
void ASN_NULL::encode_text(Text_Buf&) const
{
  if (!bound_flag)
    TTCN_error("Text encoder: Encoding an unbound ASN.1 NULL value.");
}

void ASN_NULL::decode_text(Text_Buf&)
{
  bound_flag = TRUE;
}

void ASN_NULL::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
124
                      int p_coding, ...) const
Elemer Lelik's avatar
Elemer Lelik committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
  va_list pvar;
  va_start(pvar, p_coding);
  switch(p_coding) {
  case TTCN_EncDec::CT_BER: {
    TTCN_EncDec_ErrorContext ec("While BER-encoding type '%s': ", p_td.name);
    unsigned BER_coding=va_arg(pvar, unsigned);
    BER_encode_chk_coding(BER_coding);
    ASN_BER_TLV_t *tlv=BER_encode_TLV(p_td, BER_coding);
    tlv->put_in_buffer(p_buf);
    ASN_BER_TLV_t::destruct(tlv);
    break;}
  case TTCN_EncDec::CT_XER: {
    TTCN_EncDec_ErrorContext ec("While XER-encoding type '%s': ", p_td.name);
    unsigned XER_coding=va_arg(pvar, unsigned);
140
    XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0, 0);
Elemer Lelik's avatar
Elemer Lelik committed
141
142
143
144
145
146
147
    break;}
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name);
    if(!p_td.json)
      TTCN_EncDec_ErrorContext::error_internal
        ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok(va_arg(pvar, int) != 0);
148
    JSON_encode(p_td, tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
149
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
Elemer Lelik's avatar
Elemer Lelik committed
150
    break;}
151
152
153
154
155
156
  case TTCN_EncDec::CT_OER: {
    TTCN_EncDec_ErrorContext ec("While OER-encoding type '%s': ", p_td.name);
    if(!p_td.oer)  TTCN_EncDec_ErrorContext::error_internal(
      "No OER descriptor available for type '%s'.", p_td.name);
    OER_encode(p_td, p_buf);
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
157
158
159
160
161
162
163
164
165
  case TTCN_EncDec::CT_RAW:
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void ASN_NULL::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
166
                      int p_coding, ...)
Elemer Lelik's avatar
Elemer Lelik committed
167
168
{
  va_list pvar;
169
  va_start(pvar, p_coding);
Elemer Lelik's avatar
Elemer Lelik committed
170
171
172
173
174
175
176
177
178
179
  switch(p_coding) {
  case TTCN_EncDec::CT_BER: {
    TTCN_EncDec_ErrorContext ec("While BER-decoding type '%s': ", p_td.name);
    unsigned L_form=va_arg(pvar, unsigned);
    ASN_BER_TLV_t tlv;
    BER_decode_str2TLV(p_buf, tlv, L_form);
    BER_decode_TLV(p_td, tlv, L_form);
    if(tlv.isComplete) p_buf.increase_pos(tlv.get_len());
    break;}
  case TTCN_EncDec::CT_XER: {
Elemer Lelik's avatar
Elemer Lelik committed
180
    TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name);
Elemer Lelik's avatar
Elemer Lelik committed
181
182
183
184
185
186
187
188
    unsigned XER_coding=va_arg(pvar, unsigned);
    XmlReaderWrap reader(p_buf);
    int success = reader.Read();
    for (; success==1; success=reader.Read()) {
      int type = reader.NodeType();
      if (type==XML_READER_TYPE_ELEMENT)
	break;
    }
Elemer Lelik's avatar
Elemer Lelik committed
189
    XER_decode(*p_td.xer, reader, XER_coding, XER_NONE, 0);
Elemer Lelik's avatar
Elemer Lelik committed
190
191
192
    size_t bytes = reader.ByteConsumed();
    p_buf.set_pos(bytes);
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
193
194
195
196
197
198
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name);
    if(!p_td.json)
      TTCN_EncDec_ErrorContext::error_internal
        ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok((const char*)p_buf.get_data(), p_buf.get_len());
199
    if(JSON_decode(p_td, tok, FALSE, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
200
201
202
203
204
205
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,
               "Can not decode type '%s', because invalid or incomplete"
               " message was received"
               , p_td.name);
    p_buf.set_pos(tok.get_buf_pos());
    break;}
206
207
208
209
210
211
212
  case TTCN_EncDec::CT_OER: {
      TTCN_EncDec_ErrorContext ec("While OER-decoding type '%s': ", p_td.name);
    if(!p_td.oer)  TTCN_EncDec_ErrorContext::error_internal(
      "No OER descriptor available for type '%s'.", p_td.name);
    OER_struct p_oer;
    OER_decode(p_td, p_buf, p_oer);
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
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
  case TTCN_EncDec::CT_RAW:
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

ASN_BER_TLV_t*
ASN_NULL::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
                         unsigned p_coding) const
{
  BER_chk_descr(p_td);
  ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());
  if(!new_tlv) {
    new_tlv=ASN_BER_TLV_t::construct(0, NULL);
  }
  new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);
  return new_tlv;
}

boolean ASN_NULL::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                                 const ASN_BER_TLV_t& p_tlv,
                                 unsigned L_form)
{
  bound_flag = FALSE;
  BER_chk_descr(p_td);
  ASN_BER_TLV_t stripped_tlv;
  BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);
  TTCN_EncDec_ErrorContext ec("While decoding NULL type: ");
  stripped_tlv.chk_constructed_flag(FALSE);
  if(!stripped_tlv.V_tlvs_selected && stripped_tlv.V.str.Vlen!=0)
    ec.error(TTCN_EncDec::ET_INVAL_MSG, "Length of V-part is not 0.");
  bound_flag=TRUE;
  return TRUE;
}

int ASN_NULL::XER_encode(const XERdescriptor_t& p_td,
251
    TTCN_Buffer& p_buf, unsigned int flavor, unsigned int /*flavor2*/, int indent, embed_values_enc_struct_t*) const
Elemer Lelik's avatar
Elemer Lelik committed
252
{
253
  boolean exer  = is_exer(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  TTCN_EncDec_ErrorContext ec("While XER encoding NULL type: ");
  if(!is_bound()) {
    TTCN_EncDec_ErrorContext::error
    (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound ASN.1 NULL value.");
  }

  int indenting = !is_canonical(flavor) && !is_record_of(flavor);
  int encoded_length=(int)p_buf.get_len();

  if (indenting) do_indent(p_buf, indent);
  p_buf.put_c('<');

  // empty element tag
  if (exer) write_ns_prefix(p_td, p_buf);
  p_buf.put_s((size_t)p_td.namelens[exer]-2, (const unsigned char*)p_td.names[exer]);

  p_buf.put_s(2 + indenting , (const unsigned char*)"/>\n");
  return (int)p_buf.get_len() - encoded_length;
}

int ASN_NULL::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
Elemer Lelik's avatar
Elemer Lelik committed
275
    unsigned int flavor, unsigned int /*flavor2*/, embed_values_dec_struct_t*)
Elemer Lelik's avatar
Elemer Lelik committed
276
{
277
  boolean exer  = is_exer(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
278
279
280
281
282
  TTCN_EncDec_ErrorContext ec("While XER decoding NULL type: ");
  int success = reader.Ok(), depth = -1;
  for (; success == 1; success = reader.Read()) {
    int type = reader.NodeType();
    if (XML_READER_TYPE_ELEMENT == type) {
283
284
285
286
287
      // If our parent is optional and there is an unexpected tag then return and
      // we stay unbound.
      if ((flavor & XER_OPTIONAL) && !check_name((const char*)reader.LocalName(), p_td, exer)) {
        return -1;
      }
Elemer Lelik's avatar
Elemer Lelik committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      verify_name(reader, p_td, exer);
      depth = reader.Depth();
      break;
    }
  }
  bound_flag = TRUE;
  int gol = reader.IsEmptyElement();
  if (!gol) { // shouldn't happen
    for (success = reader.Read(); success == 1; success = reader.Read()) {
      int type = reader.NodeType();
      if (XML_READER_TYPE_END_ELEMENT == type) {
	verify_end(reader, p_td, depth, exer);
	// FIXME reader.Read() ??
	break;
      }
    } // next
  } // if gol

  reader.Read();
  return 1; // decode successful
}

310
int ASN_NULL::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok, boolean) const
Elemer Lelik's avatar
Elemer Lelik committed
311
312
313
314
315
316
317
318
319
320
{
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound ASN.1 NULL value.");
    return -1;
  }
  
  return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL);
}

321
int ASN_NULL::JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok, boolean p_silent, boolean, int)
Elemer Lelik's avatar
Elemer Lelik committed
322
323
{
  json_token_t token = JSON_TOKEN_NONE;
324
  size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);
Elemer Lelik's avatar
Elemer Lelik committed
325
326
327
328
329
330
331
332
  if (JSON_TOKEN_ERROR == token) {
    JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, "");
    return JSON_ERROR_FATAL;
  }
  else if (JSON_TOKEN_LITERAL_NULL != token) {
    return JSON_ERROR_INVALID_TOKEN;
  }
  bound_flag = TRUE;
333
  return (int)dec_len;
Elemer Lelik's avatar
Elemer Lelik committed
334
335
}

336
337
338
339
340
341
342
343
344
345
346
347
348
349
int ASN_NULL::OER_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const {
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound ASN.1 NULL value.");
    return -1;
  }
  return 0;
}

int ASN_NULL::OER_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, OER_struct&) {
  bound_flag = TRUE;
  return 0;
}

Elemer Lelik's avatar
Elemer Lelik committed
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
boolean operator==(asn_null_type, const ASN_NULL& other_value)
{
  if (!other_value.is_bound()) TTCN_error("The right operand of comparison "
    "is an unbound ASN.1 NULL value.");
  return TRUE;
}

void ASN_NULL_template::clean_up()
{
  if (template_selection == VALUE_LIST ||
    template_selection == COMPLEMENTED_LIST) delete [] value_list.list_value;
  template_selection = UNINITIALIZED_TEMPLATE;
}

void ASN_NULL_template::copy_template(const ASN_NULL_template& other_value)
{
  switch (other_value.template_selection) {
  case SPECIFIC_VALUE:
  case OMIT_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
      break;
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    value_list.n_values = other_value.value_list.n_values;
    value_list.list_value = new ASN_NULL_template[value_list.n_values];
    for (unsigned int i = 0; i < value_list.n_values; i++)
      value_list.list_value[i].copy_template(
	other_value.value_list.list_value[i]);
    break;
  default:
    TTCN_error("Copying an uninitialized/unsupported template of ASN.1 "
      "NULL type.");
  }
  set_selection(other_value);
}

ASN_NULL_template::ASN_NULL_template()
{

}

ASN_NULL_template::ASN_NULL_template(template_sel other_value)
  : Base_Template(other_value)
{
  check_single_selection(other_value);
}

ASN_NULL_template::ASN_NULL_template(asn_null_type)
  : Base_Template(SPECIFIC_VALUE)
{

}

ASN_NULL_template::ASN_NULL_template(const ASN_NULL& other_value)
  : Base_Template(SPECIFIC_VALUE)
{
  if (!other_value.is_bound())
    TTCN_error("Creating a template from an unbound ASN.1 NULL value.");
}

ASN_NULL_template::ASN_NULL_template(const OPTIONAL<ASN_NULL>& other_value)
{
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
420
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
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
    TTCN_error("Creating a template of ASN.1 NULL type from an unbound "
      "optional field.");
  }
}

ASN_NULL_template::ASN_NULL_template(const ASN_NULL_template& other_value)
: Base_Template()
{
  copy_template(other_value);
}

ASN_NULL_template::~ASN_NULL_template()
{
  clean_up();
}

ASN_NULL_template& ASN_NULL_template::operator=(template_sel other_value)
{
  check_single_selection(other_value);
  clean_up();
  set_selection(other_value);
  return *this;
}

ASN_NULL_template& ASN_NULL_template::operator=(asn_null_type)
{
  clean_up();
  set_selection(SPECIFIC_VALUE);
  return *this;
}

ASN_NULL_template& ASN_NULL_template::operator=(const ASN_NULL& other_value)
{
  if (!other_value.is_bound()) TTCN_error("Assignment of an unbound ASN.1 "
    "NULL value to a template.");
  clean_up();
  set_selection(SPECIFIC_VALUE);
  return *this;
}

ASN_NULL_template& ASN_NULL_template::operator=
  (const OPTIONAL<ASN_NULL>& other_value)
{
  clean_up();
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
472
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    TTCN_error("Assignment of an unbound optional field to a template of "
      "ASN.1 NULL type.");
  }
  return *this;
}

ASN_NULL_template& ASN_NULL_template::operator=
  (const ASN_NULL_template& other_value)
{
  if (&other_value != this) {
    clean_up();
    copy_template(other_value);
  }
  return *this;
}

Elemer Lelik's avatar
Elemer Lelik committed
489
490
boolean ASN_NULL_template::match(asn_null_type other_value,
                                 boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
{
  switch (template_selection) {
  case OMIT_VALUE:
    return FALSE;
  case SPECIFIC_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
    return TRUE;
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    for (unsigned int i = 0; i < value_list.n_values; i++)
      if (value_list.list_value[i].match(other_value))
	return template_selection == VALUE_LIST;
    return template_selection == COMPLEMENTED_LIST;
  default:
    TTCN_error("Matching with an uninitialized/unsupported template of "
      "ASN.1 NULL type.");
  }
  return FALSE;
}

Elemer Lelik's avatar
Elemer Lelik committed
512
513
boolean ASN_NULL_template::match(const ASN_NULL& other_value,
                                 boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
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
{
  if (!other_value.is_bound()) return FALSE;
  return match(ASN_NULL_VALUE);
}

asn_null_type ASN_NULL_template::valueof() const
{
  if (template_selection != SPECIFIC_VALUE || is_ifpresent)
    TTCN_error("Performing a valueof "
    "or send operation on a non-specific template of ASN.1 NULL type.");
  return ASN_NULL_VALUE;
}

void ASN_NULL_template::set_type(template_sel template_type,
   unsigned int list_length)
{
  if (template_type != VALUE_LIST && template_type != COMPLEMENTED_LIST)
    TTCN_error("Setting an invalid list type for a template of ASN.1 NULL "
      "type.");
  clean_up();
  set_selection(template_type);
  value_list.n_values = list_length;
  value_list.list_value = new ASN_NULL_template[list_length];
}

ASN_NULL_template& ASN_NULL_template::list_item(unsigned int list_index)
{
  if (template_selection != VALUE_LIST &&
    template_selection != COMPLEMENTED_LIST) TTCN_error("Accessing a list "
      "element of a non-list template for ASN.1 NULL type.");
  if (list_index >= value_list.n_values)
    TTCN_error("Index overflow in a value list template of ASN.1 NULL type.");
  return value_list.list_value[list_index];
}

void ASN_NULL_template::log() const
{
  switch (template_selection) {
  case SPECIFIC_VALUE:
    TTCN_Logger::log_event_str("NULL");
    break;
  case COMPLEMENTED_LIST:
556
    TTCN_Logger::log_event_str("complement");
Elemer Lelik's avatar
Elemer Lelik committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  case VALUE_LIST:
    TTCN_Logger::log_char('(');
    for (unsigned int i = 0; i < value_list.n_values; i++) {
      if (i > 0) TTCN_Logger::log_event_str(", ");
      value_list.list_value[i].log();
    }
    TTCN_Logger::log_char(')');
    break;
  default:
    log_generic();
  }
  log_ifpresent();
}

Elemer Lelik's avatar
Elemer Lelik committed
571
572
void ASN_NULL_template::log_match(const ASN_NULL& match_value,
                                  boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
{
  if(TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity()){
    TTCN_Logger::print_logmatch_buffer();
    TTCN_Logger::log_event_str(" := ");
  }
  match_value.log();
  TTCN_Logger::log_event_str(" with ");
  log();
  if (match(match_value)) TTCN_Logger::log_event_str(" matched");
  else TTCN_Logger::log_event_str(" unmatched");
}

void ASN_NULL_template::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_TEMPLATE, "NULL template");
Elemer Lelik's avatar
Elemer Lelik committed
587
  Module_Param_Ptr mp = &param;
588
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
589
590
591
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
592
#endif
Elemer Lelik's avatar
Elemer Lelik committed
593
  switch (mp->get_type()) {
Elemer Lelik's avatar
Elemer Lelik committed
594
595
596
597
598
599
600
601
602
603
  case Module_Param::MP_Omit:
    *this = OMIT_VALUE;
    break;
  case Module_Param::MP_Any:
    *this = ANY_VALUE;
    break;
  case Module_Param::MP_AnyOrNone:
    *this = ANY_OR_OMIT;
    break;
  case Module_Param::MP_List_Template:
Elemer Lelik's avatar
Elemer Lelik committed
604
605
606
607
608
609
  case Module_Param::MP_ComplementList_Template: {
    ASN_NULL_template temp;
    temp.set_type(mp->get_type() == Module_Param::MP_List_Template ?
      VALUE_LIST : COMPLEMENTED_LIST, mp->get_size());
    for (size_t i=0; i<mp->get_size(); i++) {
      temp.list_item(i).set_param(*mp->get_elem(i));
Elemer Lelik's avatar
Elemer Lelik committed
610
    }
Elemer Lelik's avatar
Elemer Lelik committed
611
612
    *this = temp;
    break; }
Elemer Lelik's avatar
Elemer Lelik committed
613
614
615
616
617
618
  case Module_Param::MP_Asn_Null:
    *this = ASN_NULL_VALUE;
    break;
  default:
    param.type_error("NULL template");
  }
Elemer Lelik's avatar
Elemer Lelik committed
619
620
621
  is_ifpresent = param.get_ifpresent() || mp->get_ifpresent();
}

622
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
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
Module_Param* ASN_NULL_template::get_param(Module_Param_Name& param_name) const
{
  Module_Param* mp = NULL;
  switch (template_selection) {
  case UNINITIALIZED_TEMPLATE:
    mp = new Module_Param_Unbound();
    break;
  case OMIT_VALUE:
    mp = new Module_Param_Omit();
    break;
  case ANY_VALUE:
    mp = new Module_Param_Any();
    break;
  case ANY_OR_OMIT:
    mp = new Module_Param_AnyOrNone();
    break;
  case SPECIFIC_VALUE:
    mp = new Module_Param_Asn_Null();
    break;
  case VALUE_LIST:
  case COMPLEMENTED_LIST: {
    if (template_selection == VALUE_LIST) {
      mp = new Module_Param_List_Template();
    }
    else {
      mp = new Module_Param_ComplementList_Template();
    }
    for (size_t i = 0; i < value_list.n_values; ++i) {
      mp->add_elem(value_list.list_value[i].get_param(param_name));
    }
    break; }
  default:
655
    TTCN_error("Referencing an uninitialized/unsupported ASN.1 NULL template.");
Elemer Lelik's avatar
Elemer Lelik committed
656
657
658
659
660
661
    break;
  }
  if (is_ifpresent) {
    mp->set_ifpresent();
  }
  return mp;
Elemer Lelik's avatar
Elemer Lelik committed
662
}
663
#endif
Elemer Lelik's avatar
Elemer Lelik committed
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

void ASN_NULL_template::encode_text(Text_Buf& text_buf) const
{
  encode_text_base(text_buf);
  switch (template_selection) {
  case SPECIFIC_VALUE:
  case OMIT_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
    break;
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    text_buf.push_int(value_list.n_values);
    for (unsigned int i = 0; i < value_list.n_values; i++)
      value_list.list_value[i].encode_text(text_buf);
    break;
  default:
    TTCN_error("Text encoder: Encoding an undefined/unsupported template "
      "of ASN.1 NULL type.");
  }
}

void ASN_NULL_template::decode_text(Text_Buf& text_buf)
{
  clean_up();
  decode_text_base(text_buf);
  switch (template_selection) {
  case SPECIFIC_VALUE:
  case OMIT_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
    break;
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    value_list.n_values = text_buf.pull_int().get_val();
    value_list.list_value = new ASN_NULL_template[value_list.n_values];
    for (unsigned int i = 0; i < value_list.n_values; i++)
      value_list.list_value[i].decode_text(text_buf);
    break;
  default:
    TTCN_error("Text decoder: An unknown/unsupported selection was received "
      "in a template for ASN.1 NULL type.");
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
709
boolean ASN_NULL_template::is_present(boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
710
711
{
  if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
712
  return !match_omit(legacy);
Elemer Lelik's avatar
Elemer Lelik committed
713
714
}

Elemer Lelik's avatar
Elemer Lelik committed
715
boolean ASN_NULL_template::match_omit(boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
716
717
718
719
720
721
722
723
{
  if (is_ifpresent) return TRUE;
  switch (template_selection) {
  case OMIT_VALUE:
  case ANY_OR_OMIT:
    return TRUE;
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
Elemer Lelik's avatar
Elemer Lelik committed
724
725
726
727
728
729
730
731
    if (legacy) {
      // legacy behavior: 'omit' can appear in the value/complement list
      for (unsigned int i=0; i<value_list.n_values; i++)
        if (value_list.list_value[i].match_omit())
          return template_selection==VALUE_LIST;
      return template_selection==COMPLEMENTED_LIST;
    }
    // else fall through
Elemer Lelik's avatar
Elemer Lelik committed
732
733
734
735
736
737
738
  default:
    return FALSE;
  }
  return FALSE;
}

#ifndef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
739
740
void ASN_NULL_template::check_restriction(template_res t_res, const char* t_name,
                                          boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
741
742
743
744
745
746
747
748
749
750
751
{
  if (template_selection==UNINITIALIZED_TEMPLATE) return;
  switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {
  case TR_VALUE:
    if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return;
    break;
  case TR_OMIT:
    if (!is_ifpresent && (template_selection==OMIT_VALUE ||
        template_selection==SPECIFIC_VALUE)) return;
    break;
  case TR_PRESENT:
Elemer Lelik's avatar
Elemer Lelik committed
752
    if (!match_omit(legacy)) return;
Elemer Lelik's avatar
Elemer Lelik committed
753
754
755
756
757
758
759
760
    break;
  default:
    return;
  }
  TTCN_error("Restriction `%s' on template of type %s violated.",
             get_res_name(t_res), t_name ? t_name : "ASN.1 NULL");
}
#endif