Boolean.cc 37.5 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2020 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
23
24
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Feher, Csaba
 *   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
25
26
27
28
29
30
31
32
33
34
35
#include <string.h>

#include "Boolean.hh"
#include "../common/memory.h"
#include "Parameters.h"
#include "Param_Types.hh"
#include "Error.hh"
#include "Logger.hh"
#include "Encdec.hh"
#include "RAW.hh"
#include "BER.hh"
36
#include "OER.hh"
Elemer Lelik's avatar
Elemer Lelik committed
37
38
39
#include "TEXT.hh"
#include "Charstring.hh"
#include "XmlReader.hh"
40
#include "Optional.hh"
Elemer Lelik's avatar
Elemer Lelik committed
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

static const Token_Match boolean_true_match("^(true).*$",TRUE);
static const Token_Match boolean_false_match("^(false).*$",TRUE);

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

BOOLEAN::BOOLEAN(boolean other_value)
{
  bound_flag = TRUE;
  boolean_value = other_value;
}

BOOLEAN::BOOLEAN(const BOOLEAN& other_value)
: Base_Type(other_value)
{
  other_value.must_bound("Copying an unbound boolean value.");
  bound_flag = TRUE;
  boolean_value = other_value.boolean_value;
}

BOOLEAN& BOOLEAN::operator=(boolean other_value)
{
  bound_flag = TRUE;
  boolean_value = other_value;
  return *this;
}

BOOLEAN& BOOLEAN::operator=(const BOOLEAN& other_value)
{
  other_value.must_bound("Assignment of an unbound boolean value.");
  bound_flag = TRUE;
  boolean_value = other_value.boolean_value;
  return *this;
}

boolean BOOLEAN::operator!() const
{
  must_bound("The operand of not operator is an unbound boolean value.");
  return !boolean_value;
}

boolean BOOLEAN::operator&&(boolean other_value) const
{
  must_bound("The left operand of and operator is an unbound boolean value.");
  return boolean_value && other_value;
}

boolean BOOLEAN::operator&&(const BOOLEAN& other_value) const
{
  must_bound("The left operand of and operator is an unbound boolean value.");
  if (!boolean_value) return FALSE;
  other_value.must_bound("The right operand of and operator is an unbound "
    "boolean value.");
  return other_value.boolean_value;
}

boolean BOOLEAN::operator^(boolean other_value) const
{
  must_bound("The left operand of xor operator is an unbound boolean value.");
  return boolean_value != other_value;
}

boolean BOOLEAN::operator^(const BOOLEAN& other_value) const
{
  must_bound("The left operand of xor operator is an unbound boolean value.");
  other_value.must_bound("The right operand of xor operator is an unbound "
    "boolean value.");
  return boolean_value != other_value.boolean_value;
}

boolean BOOLEAN::operator||(boolean other_value) const
{
  must_bound("The left operand of or operator is an unbound boolean value.");
  return boolean_value || other_value;
}

boolean BOOLEAN::operator||(const BOOLEAN& other_value) const
{
  must_bound("The left operand of or operator is an unbound boolean value.");
  if (boolean_value) return TRUE;
  other_value.must_bound("The right operand of or operator is an unbound "
    "boolean value.");
  return other_value.boolean_value;
}

boolean BOOLEAN::operator==(boolean other_value) const
{
  must_bound("The left operand of comparison is an unbound boolean value.");
  return boolean_value == other_value;
}

boolean BOOLEAN::operator==(const BOOLEAN& other_value) const
{
  must_bound("The left operand of comparison is an unbound boolean value.");
  other_value.must_bound("The right operand of comparison is an unbound "
    "boolean value.");
  return boolean_value == other_value.boolean_value;
}

BOOLEAN::operator boolean() const
{
  must_bound("Using the value of an unbound boolean variable.");
  return boolean_value;
}

void BOOLEAN::clean_up()
{
  bound_flag = FALSE;
}

void BOOLEAN::log() const
{
  if (bound_flag) TTCN_Logger::log_event_str(boolean_value ? "true" : "false");
  else TTCN_Logger::log_event_unbound();
}

void BOOLEAN::encode_text(Text_Buf& text_buf) const
{
  must_bound("Text encoder: Encoding an unbound boolean value.");
  text_buf.push_int(boolean_value ? 1 : 0);
}

void BOOLEAN::decode_text(Text_Buf& text_buf)
{
  int int_value = text_buf.pull_int().get_val();
  switch (int_value) {
  case 0:
    boolean_value = FALSE;
    break;
  case 1:
    boolean_value = TRUE;
    break;
  default:
    TTCN_error("Text decoder: An invalid boolean value (%d) was received.",
      int_value);
  }
  bound_flag = TRUE;
}

void BOOLEAN::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_VALUE, "boolean value");
Elemer Lelik's avatar
Elemer Lelik committed
185
  Module_Param_Ptr mp = &param;
186
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
187
188
189
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
190
#endif
Elemer Lelik's avatar
Elemer Lelik committed
191
  if (mp->get_type()!=Module_Param::MP_Boolean) param.type_error("boolean value");
Elemer Lelik's avatar
Elemer Lelik committed
192
  bound_flag = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
193
194
195
  boolean_value = mp->get_boolean();
}

196
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
197
198
199
200
201
202
Module_Param* BOOLEAN::get_param(Module_Param_Name& /* param_name */) const
{
  if (!is_bound()) {
    return new Module_Param_Unbound();
  }
  return new Module_Param_Boolean(boolean_value);
Elemer Lelik's avatar
Elemer Lelik committed
203
}
204
#endif
Elemer Lelik's avatar
Elemer Lelik committed
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

void BOOLEAN::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
                     TTCN_EncDec::coding_t p_coding, ...) const
{
  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_RAW: {
    TTCN_EncDec_ErrorContext ec("While RAW-encoding type '%s': ", p_td.name);
    if(!p_td.raw)
    TTCN_EncDec_ErrorContext::error_internal
      ("No RAW descriptor available for type '%s'.", p_td.name);
    RAW_enc_tr_pos rp;
    rp.level=0;
    rp.pos=NULL;
    RAW_enc_tree root(TRUE,NULL,&rp,1,p_td.raw);
    RAW_encode(p_td, root);
    root.put_to_buf(p_buf);
    break;}
  case TTCN_EncDec::CT_TEXT: {
    TTCN_EncDec_ErrorContext ec("While TEXT-encoding type '%s': ", p_td.name);
    if(!p_td.text)
      TTCN_EncDec_ErrorContext::error_internal
        ("No TEXT descriptor available for type '%s'.", p_td.name);
    TEXT_encode(p_td,p_buf);
    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);
242
    XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0, 0);
Elemer Lelik's avatar
Elemer Lelik committed
243
244
245
246
247
248
249
    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);
250
    JSON_encode(p_td, tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
251
252
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
    break;}
253
254
255
256
257
258
  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
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
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void BOOLEAN::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
                     TTCN_EncDec::coding_t p_coding, ...)
{
  va_list pvar;
  va_start(pvar, p_coding);
  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_RAW: {
    TTCN_EncDec_ErrorContext ec("While RAW-decoding type '%s': ", p_td.name);
    if(!p_td.raw)
    TTCN_EncDec_ErrorContext::error_internal
      ("No RAW descriptor available for type '%s'.", p_td.name);
    raw_order_t order;
    switch(p_td.raw->top_bit_order){
    case TOP_BIT_LEFT:
      order=ORDER_LSB;
      break;
    case TOP_BIT_RIGHT:
    default:
        order=ORDER_MSB;
    }
    if(RAW_decode(p_td, p_buf, p_buf.get_len()*8, order)<0)
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,
        "Can not decode type '%s', because invalid or incomplete"
        " message was received"
        , p_td.name);
    break;}
  case TTCN_EncDec::CT_TEXT: {
    Limit_Token_List limit;
    TTCN_EncDec_ErrorContext ec("While TEXT-decoding type '%s': ", p_td.name);
    if(!p_td.text)
      TTCN_EncDec_ErrorContext::error_internal
        ("No TEXT descriptor available for type '%s'.", p_td.name);
    const unsigned char *b=p_buf.get_data();
307
    int null_added=0;
Elemer Lelik's avatar
Elemer Lelik committed
308
    if(b[p_buf.get_len()-1]!='\0'){
309
      null_added=1;
Elemer Lelik's avatar
Elemer Lelik committed
310
311
312
313
314
315
316
317
318
      p_buf.set_pos(p_buf.get_len());
      p_buf.put_zero(8,ORDER_LSB);
      p_buf.rewind();
    }
    if(TEXT_decode(p_td,p_buf,limit)<0)
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,
               "Can not decode type '%s', because invalid or incomplete"
               " message was received"
               , p_td.name);
319
320
321
322
323
324
    if(null_added){
      size_t actpos=p_buf.get_pos();
      p_buf.set_pos(p_buf.get_len()-1);
      p_buf.cut_end();
      p_buf.set_pos(actpos);
    }
Elemer Lelik's avatar
Elemer Lelik committed
325
326
    break;}
  case TTCN_EncDec::CT_XER: {
Elemer Lelik's avatar
Elemer Lelik committed
327
    TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name);
Elemer Lelik's avatar
Elemer Lelik committed
328
329
330
331
332
333
334
    unsigned XER_coding=va_arg(pvar, unsigned);
    XmlReaderWrap reader(p_buf);
    for (int success = reader.Read(); success==1; success=reader.Read()) {
      int type = reader.NodeType();
      if (type==XML_READER_TYPE_ELEMENT)
        break;
    }
Elemer Lelik's avatar
Elemer Lelik committed
335
    XER_decode(*p_td.xer, reader, XER_coding, XER_NONE, 0);
Elemer Lelik's avatar
Elemer Lelik committed
336
337
338
339
340
341
342
343
344
    size_t bytes = reader.ByteConsumed();
    p_buf.set_pos(bytes);
    break;}
  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());
345
    if(JSON_decode(p_td, tok, FALSE, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
346
347
348
349
350
351
      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;}
352
353
354
355
  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);
356
357
    OER_struct p_oer;
    OER_decode(p_td, p_buf, p_oer);
358
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
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
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

ASN_BER_TLV_t*
BOOLEAN::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(1, NULL);
    new_tlv->V.str.Vstr[0]=boolean_value==TRUE?0xFF:0x00;
  }
  new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);
  return new_tlv;
}

boolean BOOLEAN::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 BOOLEAN type: ");
  stripped_tlv.chk_constructed_flag(FALSE);
  if (!stripped_tlv.isComplete) return FALSE;
  if(stripped_tlv.V.str.Vlen!=1)
    ec.error(TTCN_EncDec::ET_INVAL_MSG,
             "Length of V-part is %lu (instead of 1).",
             (unsigned long) stripped_tlv.V.str.Vlen);
  if(stripped_tlv.V.str.Vlen>=1) {
    switch(stripped_tlv.V.str.Vstr[0]) {
    case 0x00:
      boolean_value=FALSE;
      break;
    default:
      /* warning? */
    case 0xFF:
      boolean_value=TRUE;
      break;
    } // switch
    bound_flag = TRUE;
    return TRUE;
  } else return FALSE;
}

int BOOLEAN::TEXT_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
  Limit_Token_List&, boolean no_err, boolean /*first_call*/)
{

  int decoded_length = 0;
  int str_len = 0;
  if (p_td.text->begin_decode) {
    int tl;
    if ((tl = p_td.text->begin_decode->match_begin(buff)) < 0) {
      if (no_err) return -1;
      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR,
        "The specified token '%s' not found for '%s': ",
        (const char*) *(p_td.text->begin_decode), p_td.name);
      return 0;
    }
    decoded_length += tl;
    buff.increase_pos(tl);
  }
  if (buff.get_read_len() < 1 && no_err) return -TTCN_EncDec::ET_LEN_ERR;

  boolean found = FALSE;

  if ( p_td.text->val.bool_values
    && p_td.text->val.bool_values->true_decode_token) {
    int tl;
    if ((tl = p_td.text->val.bool_values->true_decode_token->match_begin(buff)) > -1) {
      str_len = tl;
      found = TRUE;
      boolean_value = TRUE;
    }
  }
  else {
    int tl;
    if ((tl = boolean_true_match.match_begin(buff)) >= 0) {
      str_len = tl;
      found = TRUE;
      boolean_value = TRUE;
    }
  }

  if (!found) {
    if ( p_td.text->val.bool_values
      && p_td.text->val.bool_values->false_decode_token) {
      int tl;
      if ((tl = p_td.text->val.bool_values->false_decode_token->match_begin(buff)) > -1) {
        str_len = tl;
        found = TRUE;
        boolean_value = FALSE;
      }
    }
    else {
      int tl;
      if ((tl = boolean_false_match.match_begin(buff)) >= 0) {
        str_len = tl;
        found = TRUE;
        boolean_value = FALSE;
      }
    }
  }

  if (found) {
    decoded_length += str_len;
    buff.increase_pos(str_len);
  }
  else {
    if (no_err) return -1;
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR,
      "No boolean token found for '%s': ", p_td.name);
    return decoded_length;
  }

  if (p_td.text->end_decode) {
    int tl;
    if ((tl = p_td.text->end_decode->match_begin(buff)) < 0) {
      if (no_err) return -1;
      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR,
        "The specified token '%s' not found for '%s': ",
        (const char*) *(p_td.text->end_decode), p_td.name);
      return 0;
    }
    decoded_length += tl;
    buff.increase_pos(tl);
  }
  bound_flag = TRUE;
  return decoded_length;
}


int BOOLEAN::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
                 TTCN_Buffer& buff) const{
  int encoded_length=0;
  if(p_td.text->begin_encode){
    buff.put_cs(*p_td.text->begin_encode);
    encoded_length+=p_td.text->begin_encode->lengthof();
  }
  if(!is_bound()) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
    if(p_td.text->end_encode){
      buff.put_cs(*p_td.text->end_encode);
      encoded_length+=p_td.text->end_encode->lengthof();
    }
    return encoded_length;
  }

  if(p_td.text->val.bool_values==NULL){
    if(boolean_value){
      buff.put_s(4,(const unsigned char*)"true");
      encoded_length+=4;
    }
    else {
      buff.put_s(5,(const unsigned char*)"false");
      encoded_length+=5;
    }
  } else {
    if(boolean_value){
      if(p_td.text->val.bool_values->true_encode_token){
        buff.put_cs(*p_td.text->val.bool_values->true_encode_token);
        encoded_length+=p_td.text->
            val.bool_values->true_encode_token->lengthof();
      } else {
        buff.put_s(4,(const unsigned char*)"true");
        encoded_length+=4;
      }
    }
    else {
      if(p_td.text->val.bool_values->false_encode_token){
        buff.put_cs(*p_td.text->val.bool_values->false_encode_token);
        encoded_length+=p_td.text->
            val.bool_values->false_encode_token->lengthof();
      } else {
        buff.put_s(5,(const unsigned char*)"false");
        encoded_length+=5;
      }
    }
  }

  if(p_td.text->end_encode){
    buff.put_cs(*p_td.text->end_encode);
    encoded_length+=p_td.text->end_encode->lengthof();
  }
  return encoded_length;
}

int BOOLEAN::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const
{
  unsigned char *bc;
  int loc_length = p_td.raw->fieldlength ? p_td.raw->fieldlength : 1;
  int length = (loc_length + 7) / 8;
  unsigned char tmp;
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound value.");
    tmp = '\0';
  }
  else tmp = boolean_value ? 0xFF : 0x00;
//  myleaf.ext_bit=EXT_BIT_NO;
  if (myleaf.must_free) Free(myleaf.body.leaf.data_ptr);
  if (length > RAW_INT_ENC_LENGTH) {
    myleaf.body.leaf.data_ptr = bc = (unsigned char*)Malloc(length*sizeof(*bc));
    myleaf.must_free = TRUE;
    myleaf.data_ptr_used = TRUE;
  }
  else bc = myleaf.body.leaf.data_array;

  memset(bc, tmp, length * sizeof(*bc));
Elemer Lelik's avatar
Elemer Lelik committed
577
578
579
580
  if (boolean_value && loc_length % 8 != 0) {
    // remove the extra ones from the last octet
    bc[length - 1] &= BitMaskTable[loc_length % 8];
  }
581
  myleaf.coding_par.csn1lh = p_td.raw->csn1lh;
Elemer Lelik's avatar
Elemer Lelik committed
582
583
584
585
586
  return myleaf.length = loc_length;
}

int BOOLEAN::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
  int limit, raw_order_t top_bit_ord, boolean no_err, int /*sel_field*/,
587
  boolean /*first_call*/, const RAW_Force_Omit* /*force_omit*/)
Elemer Lelik's avatar
Elemer Lelik committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
{
  bound_flag = FALSE;
  int prepaddlength = buff.increase_pos_padd(p_td.raw->prepadding);
  limit -= prepaddlength;
  int decode_length = p_td.raw->fieldlength > 0 ? p_td.raw->fieldlength : 1;
  if (decode_length > limit) {
    if (no_err) return -TTCN_EncDec::ET_LEN_ERR;
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR,
      "There is not enough bits in the buffer to decode type %s (needed: %d, "
        "found: %d).", p_td.name, decode_length, limit);
    decode_length = limit;
  }
  int nof_unread_bits = buff.unread_len_bit();
  if (decode_length > nof_unread_bits) {
    if (no_err) return -TTCN_EncDec::ET_INCOMPL_MSG;
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG,
      "There is not enough bits in the buffer to decode type %s (needed: %d, "
        "found: %d).", p_td.name, decode_length, nof_unread_bits);
    decode_length = nof_unread_bits;
  }
  if (decode_length < 0) return -1;
  else if (decode_length == 0) boolean_value = FALSE;
  else {
    RAW_coding_par cp;
Kristof Szabados's avatar
Kristof Szabados committed
612
    boolean orders = p_td.raw->bitorderinoctet == ORDER_MSB;
Elemer Lelik's avatar
Elemer Lelik committed
613
614
    if (p_td.raw->bitorderinfield == ORDER_MSB) orders = !orders;
    cp.bitorder = orders ? ORDER_MSB : ORDER_LSB;
Kristof Szabados's avatar
Kristof Szabados committed
615
    orders = p_td.raw->byteorder == ORDER_MSB;
Elemer Lelik's avatar
Elemer Lelik committed
616
617
618
619
    if (p_td.raw->bitorderinfield == ORDER_MSB) orders = !orders;
    cp.byteorder = orders ? ORDER_MSB : ORDER_LSB;
    cp.fieldorder = p_td.raw->fieldorder;
    cp.hexorder = ORDER_LSB;
620
    cp.csn1lh = p_td.raw->csn1lh;
Elemer Lelik's avatar
Elemer Lelik committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
    int length = (decode_length + 7) / 8;
    unsigned char *data = (unsigned char*)Malloc(length*sizeof(unsigned char));
    buff.get_b((size_t)decode_length, data, cp, top_bit_ord);
    if(decode_length % 8){
      data[length - 1] &= BitMaskTable[decode_length % 8];
    }
    unsigned char ch = '\0';
    for (int a = 0; a < length; a++) ch |= data[a];
    Free(data);
    boolean_value = ch != '\0';
  }
  bound_flag = TRUE;
  decode_length += buff.increase_pos_padd(p_td.raw->padding);
  return decode_length + prepaddlength;
}

int BOOLEAN::XER_encode(const XERdescriptor_t& p_td,
638
  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
639
640
641
642
643
644
645
{
  if(!is_bound()) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound boolean value.");
  }
  int encoded_length=(int)p_buf.get_len();

646
  boolean exer  = is_exer(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
647
648

  flavor |= (SIMPLE_TYPE | BXER_EMPTY_ELEM);
649
  if (begin_xml(p_td, p_buf, flavor, indent, FALSE) == -1) --encoded_length;
Elemer Lelik's avatar
Elemer Lelik committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664

  if (exer) {
    if (p_td.xer_bits & XER_TEXT) {
      p_buf.put_c(boolean_value ? '1' : '0');
    }
    else {
      if (boolean_value) p_buf.put_s(4, (cbyte*)"true");
      else               p_buf.put_s(5, (cbyte*)"false");
    }
  }
  else {
    if (boolean_value) p_buf.put_s(7, (cbyte*)"<true/>");
    else               p_buf.put_s(8, (cbyte*)"<false/>");
  }

665
  end_xml(p_td, p_buf, flavor, indent, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
666
667
668
669
670

  return (int)p_buf.get_len() - encoded_length;
}

int BOOLEAN::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
Elemer Lelik's avatar
Elemer Lelik committed
671
  unsigned int flavor, unsigned int /*flavor2*/, embed_values_dec_struct_t*)
Elemer Lelik's avatar
Elemer Lelik committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
{
  const boolean exer = is_exer(flavor);
  int XMLValueList = !exer && is_record_of(flavor);
  const boolean notag = (exer && (p_td.xer_bits & (UNTAGGED))) ||
    is_exerlist(flavor) || XMLValueList;
  int depth = -1, success, type;
  const char *value = 0;

  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
    verify_name(reader, p_td, exer);
    value = (const char *)reader.Value();
  }
  else {
    for (success = reader.Ok(); success == 1; success = reader.Read()) {
      type = reader.NodeType();
      if (!notag && depth == -1) {
        if (XML_READER_TYPE_ELEMENT == type) {
689
690
691
692
693
          // 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
          verify_name(reader, p_td, exer);
          depth = reader.Depth();

          if (exer && (p_td.dfeValue != 0) && reader.IsEmptyElement()) {
            *this = *static_cast<const BOOLEAN*>(p_td.dfeValue);
            (void)reader.Read();
            goto fini;
          }
          continue;
        } // if type
      }
      else { // found the enclosing tag already
        if (!exer && XML_READER_TYPE_ELEMENT == type) {
          // this must be EmptyElement Boolean
          if (!reader.IsEmptyElement()) TTCN_EncDec_ErrorContext::error(
            TTCN_EncDec::ET_INVAL_MSG, "Boolean must be empty element");
          value = (const char*)reader.LocalName();
        }
712
        else if (XML_READER_TYPE_TEXT == type || XML_READER_TYPE_ATTRIBUTE == type) {
Elemer Lelik's avatar
Elemer Lelik committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
          // TextBoolean
          value = (const char*)reader.Value();
        }

        // Must not modify the buffer when attempting to find the selected alternative for USE-UNION
        if (!exer || !(flavor & EXIT_ON_ERROR)) reader.Read();
        break;
      } // if depth
    } // next read
  } // if not attribute

  if (value != 0 && *value != 0) {
    // extract the data
    if (value[1]=='\0' && (*value & 0x3E) == '0')
    {
728
      bound_flag = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
729
730
731
      boolean_value = *value == '1';
    }
    else if (!strcmp(value, "true")) {
732
733
      boolean_value = TRUE;
      bound_flag    = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
734
735
    }
    else if (!strcmp(value, "false")) {
736
737
      boolean_value = FALSE;
      bound_flag    = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
    }

  }

  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { // I am an attribute
    // Let the caller do reader.AdvanceAttribute();
  }
  else if (!notag) {
    for (success = reader.Ok(); success == 1; success = reader.Read()) {
      type = reader.NodeType();
      if (XML_READER_TYPE_END_ELEMENT == type) {
        verify_end(reader, p_td, depth, exer);
        reader.Read(); // one last time
        break;
      }
    } // next
  }
fini:
  return 1;
}

759
int BOOLEAN::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok, boolean) const
Elemer Lelik's avatar
Elemer Lelik committed
760
761
762
763
764
765
766
767
768
{
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound boolean value.");
    return -1;
  }
  return p_tok.put_next_token((boolean_value) ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE, NULL);
}

769
int BOOLEAN::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent, boolean, int)
Elemer Lelik's avatar
Elemer Lelik committed
770
771
{
  json_token_t token = JSON_TOKEN_NONE;
772
  size_t dec_len = 0;
Elemer Lelik's avatar
Elemer Lelik committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  if (p_td.json->default_value && 0 == p_tok.get_buffer_length()) {
    // No JSON data in the buffer -> use default value
    if (strcmp(p_td.json->default_value, "true") == 0) {
      token = JSON_TOKEN_LITERAL_TRUE;
    } 
    else {
      token = JSON_TOKEN_LITERAL_FALSE;
    }
  } else {
    dec_len = p_tok.get_next_token(&token, NULL, NULL);
  }
  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_TRUE == token) {
789
790
    bound_flag = TRUE;
    boolean_value = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
791
792
  }
  else if (JSON_TOKEN_LITERAL_FALSE == token) {
793
794
    bound_flag = TRUE;
    boolean_value = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
795
796
  } 
  else {
797
    bound_flag = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
798
799
    return JSON_ERROR_INVALID_TOKEN;
  }
800
  return (int)dec_len;
Elemer Lelik's avatar
Elemer Lelik committed
801
802
}

803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
int BOOLEAN::OER_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer& p_buf) const
{
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound boolean value.");
    return -1;
  }
  if (boolean_value) {
    p_buf.put_c(255); // FF
  } else {
    p_buf.put_c(0); // 00
  }
  return 1;
}

818
int BOOLEAN::OER_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer& p_buf, OER_struct&)
819
820
821
822
823
824
825
826
827
828
829
830
{
  const unsigned char* c = p_buf.get_read_data();
  p_buf.increase_pos(1);
  if (*c == 0) {
    boolean_value = FALSE;
  } else {
    boolean_value = TRUE;
  }
  bound_flag = TRUE;
  return 1;
}

Elemer Lelik's avatar
Elemer Lelik committed
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

boolean operator&&(boolean bool_value, const BOOLEAN& other_value)
{
  if (!bool_value) return FALSE;
  other_value.must_bound("The right operand of and operator is an unbound "
    "boolean value.");
  return other_value.boolean_value;
}

boolean operator^(boolean bool_value, const BOOLEAN& other_value)
{
  other_value.must_bound("The right operand of xor operator is an unbound "
    "boolean value.");
  return bool_value != other_value.boolean_value;
}

boolean operator||(boolean bool_value, const BOOLEAN& other_value)
{
  if (bool_value) return TRUE;
  other_value.must_bound("The right operand of or operator is an unbound "
    "boolean value.");
  return other_value.boolean_value;
}

boolean operator==(boolean bool_value, const BOOLEAN& other_value)
{
  other_value.must_bound("The right operand of comparison is an unbound "
    "boolean value.");
  return bool_value == other_value.boolean_value;
}

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

void BOOLEAN_template::copy_template(const BOOLEAN_template& other_value)
{
  switch (other_value.template_selection) {
  case SPECIFIC_VALUE:
    single_value = other_value.single_value;
    break;
  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 BOOLEAN_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 boolean template.");
  }
  set_selection(other_value);
}

BOOLEAN_template::BOOLEAN_template()
{

}

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

BOOLEAN_template::BOOLEAN_template(boolean other_value)
  : Base_Template(SPECIFIC_VALUE)
{
  single_value = other_value;
}

BOOLEAN_template::BOOLEAN_template(const BOOLEAN& other_value)
  : Base_Template(SPECIFIC_VALUE)
{
  other_value.must_bound("Creating a template from an unbound integer value.");
  single_value = other_value.boolean_value;
}

BOOLEAN_template::BOOLEAN_template(const OPTIONAL<BOOLEAN>& other_value)
{
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    single_value = (boolean)(const BOOLEAN&)other_value;
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
928
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
    TTCN_error("Creating a boolean template from an unbound optional field.");
  }
}

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

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

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

BOOLEAN_template& BOOLEAN_template::operator=(boolean other_value)
{
  clean_up();
  set_selection(SPECIFIC_VALUE);
  single_value = other_value;
  return *this;
}

BOOLEAN_template& BOOLEAN_template::operator=(const BOOLEAN& other_value)
{
  other_value.must_bound("Assignment of an unbound boolean value to a "
    "template.");
  clean_up();
  set_selection(SPECIFIC_VALUE);
  single_value = other_value.boolean_value;
  return *this;
}

BOOLEAN_template& BOOLEAN_template::operator=
  (const OPTIONAL<BOOLEAN>& other_value)
{
  clean_up();
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    single_value = (boolean)(const BOOLEAN&)other_value;
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
982
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
    TTCN_error("Assignment of an unbound optional field to a boolean "
      "template.");
  }
  return *this;
}

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

Elemer Lelik's avatar
Elemer Lelik committed
999
1000
boolean BOOLEAN_template::match(boolean other_value,
                                boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
{
  switch (template_selection) {
  case SPECIFIC_VALUE:
    return single_value == other_value;
  case OMIT_VALUE:
    return FALSE;
  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 boolean template.");
  }
  return FALSE;
}

Elemer Lelik's avatar
Elemer Lelik committed
1022
1023
boolean BOOLEAN_template::match(const BOOLEAN& other_value,
                                boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
{
  if (!other_value.is_bound()) return FALSE;
  return match(other_value.boolean_value);
}

boolean BOOLEAN_template::valueof() const
{
  if (template_selection != SPECIFIC_VALUE || is_ifpresent)
    TTCN_error("Performing valueof or "
               "send operation on a non-specific boolean template.");
  return single_value;
}

void BOOLEAN_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 boolean template.");
  clean_up();
  set_selection(template_type);
  value_list.n_values = list_length;
  value_list.list_value = new BOOLEAN_template[list_length];
}

BOOLEAN_template& BOOLEAN_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 boolean template.");
  if (list_index >= value_list.n_values)
    TTCN_error("Index overflow in a boolean value list template.");
  return value_list.list_value[list_index];
}

void BOOLEAN_template::log() const
{
  switch (template_selection) {
  case SPECIFIC_VALUE:
    TTCN_Logger::log_event_str(single_value ? "true" : "false");
    break;
  case COMPLEMENTED_LIST:
1065
    TTCN_Logger::log_event_str("complement");
Elemer Lelik's avatar
Elemer Lelik committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
    // no break
  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();
    break;
  }
  log_ifpresent();
}

Elemer Lelik's avatar
Elemer Lelik committed
1082
1083
void BOOLEAN_template::log_match(const BOOLEAN& match_value,
                                 boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
{
  if (TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity()
  &&  TTCN_Logger::get_logmatch_buffer_len() != 0) {
    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 BOOLEAN_template::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_TEMPLATE, "boolean template");
Elemer Lelik's avatar
Elemer Lelik committed
1099
  Module_Param_Ptr mp = &param;
1100
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1101
1102
1103
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
1104
#endif
Elemer Lelik's avatar
Elemer Lelik committed
1105
  switch (mp->get_type()) {
Elemer Lelik's avatar
Elemer Lelik committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  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
1116
1117
1118
1119
1120
1121
  case Module_Param::MP_ComplementList_Template: {
    BOOLEAN_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
1122
    }
Elemer Lelik's avatar
Elemer Lelik committed
1123
1124
    *this = temp;
    break; }
Elemer Lelik's avatar
Elemer Lelik committed
1125
  case Module_Param::MP_Boolean:
Elemer Lelik's avatar
Elemer Lelik committed
1126
    *this = mp->get_boolean();
Elemer Lelik's avatar
Elemer Lelik committed
1127
1128
1129
1130
    break;
  default:
    param.type_error("boolean template");
  }
Elemer Lelik's avatar
Elemer Lelik committed
1131
1132
1133
  is_ifpresent = param.get_ifpresent() || mp->get_ifpresent();
}

1134
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
Module_Param* BOOLEAN_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_Boolean(single_value);
    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:
1167
    TTCN_error("Referencing an uninitialized/unsupported boolean template.");
Elemer Lelik's avatar
Elemer Lelik committed
1168
1169
1170
1171
1172
1173
    break;
  }
  if (is_ifpresent) {
    mp->set_ifpresent();
  }
  return mp;
Elemer Lelik's avatar
Elemer Lelik committed
1174
}
1175
#endif
Elemer Lelik's avatar
Elemer Lelik committed
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235

void BOOLEAN_template::encode_text(Text_Buf& text_buf) const
{
  encode_text_base(text_buf);
  switch (template_selection) {
  case OMIT_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
    break;
  case SPECIFIC_VALUE:
    text_buf.push_int(single_value ? 1 : 0);
    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 uninitialized/unsupported boolean "
      "template.");
  }
}

void BOOLEAN_template::decode_text(Text_Buf& text_buf)
{
  clean_up();
  decode_text_base(text_buf);
  switch (template_selection) {
  case OMIT_VALUE:
  case ANY_VALUE:
  case ANY_OR_OMIT:
    break;
  case SPECIFIC_VALUE: {
    int int_value = text_buf.pull_int().get_val();
    switch (int_value) {
    case 0:
      single_value = FALSE;
      break;
    case 1:
      single_value = TRUE;
      break;
    default:
      TTCN_error("Text decoder: An invalid boolean value (%d) was received for "
	"a template.", int_value);
    }
    break; }
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    value_list.n_values = text_buf.pull_int().get_val();
    value_list.list_value = new BOOLEAN_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 for a boolean template.");
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
1236
boolean BOOLEAN_template::is_present(boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
1237
1238
{
  if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1239
  return !match_omit(legacy);
Elemer Lelik's avatar
Elemer Lelik committed
1240
1241
}

Elemer Lelik's avatar
Elemer Lelik committed
1242
boolean BOOLEAN_template::match_omit(boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
1243
1244
1245
1246
1247
1248
1249
1250
{
  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
1251
1252
1253
1254
1255
1256
1257
1258
    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
1259
1260
1261
1262
1263
1264
1265
  default:
    return FALSE;
  }
  return FALSE;
}

#ifndef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
1266
1267
void BOOLEAN_template::check_restriction(template_res t_res, const char* t_name,
                                         boolean legacy /* = FALSE */) const
Elemer Lelik's avatar
Elemer Lelik committed
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
{
  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
1279
    if (!match_omit(legacy)) return;
Elemer Lelik's avatar
Elemer Lelik committed
1280
1281
1282
1283
1284
1285
1286
1287
    break;
  default:
    return;
  }
  TTCN_error("Restriction `%s' on template of type %s violated.",
             get_res_name(t_res), t_name ? t_name : "boolean");
}
#endif