Bitstring.cc 76.8 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
2
 * Copyright (c) 2000-2019 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
 *
 * Contributors:
 *   Baji, Laszlo
 *   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
24
25
26
27
28
29
30
31
32
33
34
#include <string.h>

#include "Bitstring.hh"
#include "../common/memory.h"
#include "Integer.hh"
#include "String_struct.hh"
#include "Parameters.h"
#include "Param_Types.hh"
#include "Error.hh"
#include "Logger.hh"
#include "Encdec.hh"
35
#include "Addfunc.hh"
36
#include "Optional.hh"
Elemer Lelik's avatar
Elemer Lelik committed
37
38

#include "../common/dbgnew.hh"
39
#include "OER.hh"
Elemer Lelik's avatar
Elemer Lelik committed
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

// bitstring value class

/** The amount of memory needed for a bitstring containing n bits. */
#define MEMORY_SIZE(n) (sizeof(bitstring_struct) - sizeof(int) + ((n) + 7) / 8)

void BITSTRING::init_struct(int n_bits)
{
  if (n_bits < 0) {
    val_ptr = NULL;
    TTCN_error("Initializing a bitstring with a negative length.");
  } else if (n_bits == 0) {
    /** This will represent the empty strings so they won't need allocated
      * memory, this delays the memory allocation until it is really needed.
      */
    static bitstring_struct empty_string = { 1, 0, "" };
    val_ptr = &empty_string;
    empty_string.ref_count++;
  } else {
    val_ptr = (bitstring_struct*)Malloc(MEMORY_SIZE(n_bits));
    val_ptr->ref_count = 1;
    val_ptr->n_bits = n_bits;
  }
}

boolean BITSTRING::get_bit(int bit_index) const
{
  return val_ptr->bits_ptr[bit_index / 8] & (1 << (bit_index % 8));
}

void BITSTRING::set_bit(int bit_index, boolean new_value)
{
  unsigned char mask = 1 << (bit_index % 8);
  if (new_value) val_ptr->bits_ptr[bit_index / 8] |= mask;
  else           val_ptr->bits_ptr[bit_index / 8] &= ~mask;
}

void BITSTRING::copy_value()
{
  if (val_ptr == NULL || val_ptr->n_bits <= 0)
    TTCN_error("Internal error: Invalid internal data structure when copying "
      "the memory area of a bitstring value.");
  if (val_ptr->ref_count > 1) {
    bitstring_struct *old_ptr = val_ptr;
    old_ptr->ref_count--;
    init_struct(old_ptr->n_bits);
    memcpy(val_ptr->bits_ptr, old_ptr->bits_ptr, (old_ptr->n_bits + 7) / 8);
  }
}

void BITSTRING::clear_unused_bits() const
{
  int n_bits = val_ptr->n_bits;
  if (n_bits % 8 != 0) val_ptr->bits_ptr[(n_bits - 1) / 8] &=
                         (unsigned char)'\377' >> (7 - (n_bits - 1) % 8);
}

BITSTRING::BITSTRING(int n_bits)
{
  init_struct(n_bits);
}

BITSTRING::BITSTRING()
{
  val_ptr = NULL;
}

BITSTRING::BITSTRING(int n_bits, const unsigned char *bits_ptr)
{
  init_struct(n_bits);
  memcpy(val_ptr->bits_ptr, bits_ptr, (n_bits + 7) / 8);
  clear_unused_bits();
}

BITSTRING::BITSTRING(const BITSTRING& other_value)
: Base_Type(other_value)
{
  other_value.must_bound("Copying an unbound bitstring value.");
  val_ptr = other_value.val_ptr;
  val_ptr->ref_count++;
}

BITSTRING::BITSTRING(const BITSTRING_ELEMENT& other_value)
{
  other_value.must_bound("Copying an unbound bitstring element.");
  init_struct(1);
  val_ptr->bits_ptr[0] = other_value.get_bit() ? 1 : 0;
}

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

void BITSTRING::clean_up()
{
  if (val_ptr != NULL) {
    if (val_ptr->ref_count > 1) val_ptr->ref_count--;
    else if (val_ptr->ref_count == 1) Free(val_ptr);
    else TTCN_error("Internal error: Invalid reference counter in a bitstring "
      "value.");
    val_ptr = NULL;
  }
}

BITSTRING& BITSTRING::operator=(const BITSTRING& other_value)
{
  other_value.must_bound("Assignment of an unbound bitstring value.");
  if (&other_value != this) {
    clean_up();
    val_ptr = other_value.val_ptr;
    val_ptr->ref_count++;
  }
  return *this;
}

BITSTRING& BITSTRING::operator=(const BITSTRING_ELEMENT& other_value)
{
  other_value.must_bound("Assignment of an unbound bitstring element to a "
    "bitstring.");
  boolean bit_value = other_value.get_bit();
  clean_up();
  init_struct(1);
  val_ptr->bits_ptr[0] = bit_value ? 1 : 0;
  return *this;
}

boolean BITSTRING::operator==(const BITSTRING& other_value) const
{
  must_bound("Unbound left operand of bitstring comparison.");
  other_value.must_bound("Unbound right operand of bitstring comparison.");
  int n_bits = val_ptr->n_bits;
  if (n_bits != other_value.val_ptr->n_bits) return FALSE;
  if (n_bits == 0) return TRUE;
  clear_unused_bits();
  other_value.clear_unused_bits();
  return !memcmp(val_ptr->bits_ptr, other_value.val_ptr->bits_ptr,
                 (n_bits + 7) / 8);
}

boolean BITSTRING::operator==(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Unbound left operand of bitstring comparison.");
  other_value.must_bound("Unbound right operand of bitstring element "
    "comparison.");
  if (val_ptr->n_bits != 1) return FALSE;
  return get_bit(0) == other_value.get_bit();
}

BITSTRING BITSTRING::operator+(const BITSTRING& other_value) const
{
  must_bound("Unbound left operand of bitstring concatenation.");
  other_value.must_bound("Unbound right operand of bitstring concatenation.");

  int left_n_bits = val_ptr->n_bits;
  if (left_n_bits == 0) return other_value;

  int right_n_bits = other_value.val_ptr->n_bits;
  if (right_n_bits == 0) return *this;

  // the length of result
  int n_bits = left_n_bits + right_n_bits;

  // the number of bytes used
  int left_n_bytes = (left_n_bits + 7) / 8;
  int right_n_bytes = (right_n_bits + 7) / 8;

  // the number of bits used in the last incomplete octet of the left operand
  int left_empty_bits = left_n_bits % 8;

  // the result
  BITSTRING ret_val(n_bits);

  // pointers to the data areas
  const unsigned char *left_ptr = val_ptr->bits_ptr;
  const unsigned char *right_ptr = other_value.val_ptr->bits_ptr;
  unsigned char *dest_ptr = ret_val.val_ptr->bits_ptr;

  // copying the left fragment into the result
  memcpy(dest_ptr, left_ptr, left_n_bytes);

  if (left_empty_bits != 0) {
    // non-trivial case: the length of left fragment is not a multiply of 8
    // the bytes used in the result
    int n_bytes = (n_bits + 7) / 8;
    // placing the bytes from the right fragment until the result is filled
    for (int i = left_n_bytes; i < n_bytes; i++) {
      unsigned char right_byte = right_ptr[i - left_n_bytes];
      // finish filling the previous byte
      dest_ptr[i - 1] |= right_byte << left_empty_bits;
      // start filling the actual byte
      dest_ptr[i] = right_byte >> (8 - left_empty_bits);
    }
    if (left_n_bytes + right_n_bytes > n_bytes) {
      // if the result data area is shorter than the two operands together
      // the last bits of right fragment were not placed into the result
      // in the previous for loop
      dest_ptr[n_bytes - 1] |= right_ptr[right_n_bytes - 1] << left_empty_bits;
    }
  } else {
    // trivial case: just append the bytes of the right fragment
    memcpy(dest_ptr + left_n_bytes, right_ptr, right_n_bytes);
  }
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING::operator+(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Unbound left operand of bitstring concatenation.");
  other_value.must_bound("Unbound right operand of bitstring element "
                         "concatenation.");

  int n_bits = val_ptr->n_bits;
  BITSTRING ret_val(n_bits + 1);
  memcpy(ret_val.val_ptr->bits_ptr, val_ptr->bits_ptr, (n_bits + 7) / 8);
  ret_val.set_bit(n_bits, other_value.get_bit());
  return ret_val;
}

BITSTRING BITSTRING::operator~() const
{
  must_bound("Unbound bitstring operand of operator not4b.");
  int n_bytes = (val_ptr->n_bits + 7) / 8;
  if (n_bytes == 0) return *this;
  BITSTRING ret_val(val_ptr->n_bits);
  for (int i = 0; i < n_bytes; i++)
    ret_val.val_ptr->bits_ptr[i] = ~val_ptr->bits_ptr[i];
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING::operator&(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator and4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator and4b is an unbound "
    "bitstring value.");
  int n_bits = val_ptr->n_bits;
  if (n_bits != other_value.val_ptr->n_bits)
    TTCN_error("The bitstring operands of operator and4b must have the "
               "same length.");
  if (n_bits == 0) return *this;
  BITSTRING ret_val(n_bits);
  int n_bytes = (n_bits + 7) / 8;
  for (int i = 0; i < n_bytes; i++)
    ret_val.val_ptr->bits_ptr[i] = val_ptr->bits_ptr[i] &
      other_value.val_ptr->bits_ptr[i];
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING::operator&(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator and4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator and4b is an unbound "
    "bitstring element.");
  if (val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands of "
               "operator and4b must have the same length.");
  unsigned char result = get_bit(0) && other_value.get_bit() ? 1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING::operator|(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator or4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator or4b is an unbound "
    "bitstring value.");
  int n_bits = val_ptr->n_bits;
  if (n_bits != other_value.val_ptr->n_bits)
    TTCN_error("The bitstring operands of operator or4b must have the "
               "same length.");
  if (n_bits == 0) return *this;
  BITSTRING ret_val(n_bits);
  int n_bytes = (n_bits + 7) / 8;
  for (int i = 0; i < n_bytes; i++)
    ret_val.val_ptr->bits_ptr[i] = val_ptr->bits_ptr[i] |
      other_value.val_ptr->bits_ptr[i];
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING::operator|(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator or4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator or4b is an unbound "
    "bitstring element.");
  if (val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands of "
               "operator or4b must have the same length.");
  unsigned char result = get_bit(0) || other_value.get_bit() ? 1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING::operator^(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator xor4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator xor4b is an unbound "
    "bitstring value.");
  int n_bits = val_ptr->n_bits;
  if (n_bits != other_value.val_ptr->n_bits)
    TTCN_error("The bitstring operands of operator xor4b must have the "
               "same length.");
  if (n_bits == 0) return *this;
  BITSTRING ret_val(n_bits);
  int n_bytes = (n_bits + 7) / 8;
  for (int i = 0; i < n_bytes; i++)
    ret_val.val_ptr->bits_ptr[i] = val_ptr->bits_ptr[i] ^
      other_value.val_ptr->bits_ptr[i];
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING::operator^(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator xor4b is an unbound bitstring value.");
  other_value.must_bound("Right operand of operator xor4b is an unbound "
    "bitstring element.");
  if (val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands of "
               "operator xor4b must have the same length.");
  unsigned char result = get_bit(0) != other_value.get_bit() ? 1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING::operator<<(int shift_count) const
{
  must_bound("Unbound bitstring operand of shift left operator.");
  if (shift_count > 0) {
    int n_bits = val_ptr->n_bits;
    if (n_bits == 0) return *this;
    BITSTRING ret_val(n_bits);
    int n_bytes = (n_bits + 7) / 8;
    clear_unused_bits();
    if (shift_count > n_bits) shift_count = n_bits;
    int shift_bytes = shift_count / 8,
      shift_bits = shift_count % 8;
    if (shift_bits != 0) {
      int byte_count = 0;
      for ( ; byte_count < n_bytes - shift_bytes - 1; byte_count++) {
        ret_val.val_ptr->bits_ptr[byte_count] =
          (val_ptr->bits_ptr[byte_count + shift_bytes] >> shift_bits)|
          (val_ptr->bits_ptr[byte_count + shift_bytes + 1] <<
           (8 - shift_bits));
      }
      ret_val.val_ptr->bits_ptr[n_bytes - shift_bytes - 1] =
        val_ptr->bits_ptr[n_bytes - 1] >> shift_bits;
    } else {
      memcpy(ret_val.val_ptr->bits_ptr, val_ptr->bits_ptr + shift_bytes,
             n_bytes - shift_bytes);
    }
    memset(ret_val.val_ptr->bits_ptr + n_bytes - shift_bytes, 0,
           shift_bytes);
    ret_val.clear_unused_bits();
    return ret_val;
  } else if (shift_count == 0) return *this;
  else return *this >> (-shift_count);
}

BITSTRING BITSTRING::operator<<(const INTEGER& shift_count) const
{
  shift_count.must_bound("Unbound right operand of bitstring shift left "
    "operator.");
  return *this << (int)shift_count;
}

BITSTRING BITSTRING::operator>>(int shift_count) const
{
  must_bound("Unbound bitstring operand of shift right operator.");
  if (shift_count > 0) {
    int n_bits = val_ptr->n_bits;
    if (n_bits == 0) return *this;
    BITSTRING ret_val(n_bits);
    int n_bytes = (n_bits + 7) / 8;
    clear_unused_bits();
    if (shift_count > n_bits) shift_count = n_bits;
    int shift_bytes = shift_count / 8, shift_bits = shift_count % 8;
    memset(ret_val.val_ptr->bits_ptr, 0, shift_bytes);
    if (shift_bits != 0) {
      ret_val.val_ptr->bits_ptr[shift_bytes] =
        val_ptr->bits_ptr[0] << shift_bits;
      for (int byte_count = shift_bytes + 1; byte_count < n_bytes; byte_count++)
      {
        ret_val.val_ptr->bits_ptr[byte_count] =
          (val_ptr->bits_ptr[byte_count - shift_bytes - 1] >> (8 - shift_bits))
	  | (val_ptr->bits_ptr[byte_count - shift_bytes] << shift_bits);
      }
    } else {
      memcpy(ret_val.val_ptr->bits_ptr + shift_bytes, val_ptr->bits_ptr,
             n_bytes - shift_bytes);
    }
    ret_val.clear_unused_bits();
    return ret_val;
  } else if (shift_count == 0) return *this;
  else return *this << (-shift_count);
}

BITSTRING BITSTRING::operator>>(const INTEGER& shift_count) const
{
  shift_count.must_bound("Unbound right operand of bitstring shift right "
    "operator.");
  return *this >> (int)shift_count;
}

BITSTRING BITSTRING::operator<<=(int rotate_count) const
{
Kristof Szabados's avatar
Kristof Szabados committed
446
  must_bound("Unbound bitstring operand of rotate left operator.");
Elemer Lelik's avatar
Elemer Lelik committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  int n_bits = val_ptr->n_bits;
  if (n_bits == 0) return *this;
  if (rotate_count >= 0) {
    rotate_count %= n_bits;
    if (rotate_count == 0) return *this;
    else return (*this << rotate_count) |
           (*this >> (n_bits - rotate_count));
  } else return *this >>= (-rotate_count);
}

BITSTRING BITSTRING::operator<<=(const INTEGER& rotate_count) const
{
  rotate_count.must_bound("Unbound right operand of bitstring rotate left "
    "operator.");
  return *this <<= (int)rotate_count;
}

BITSTRING BITSTRING::operator>>=(int rotate_count) const
{
Kristof Szabados's avatar
Kristof Szabados committed
466
  must_bound("Unbound bitstring operand of rotate right operator.");
Elemer Lelik's avatar
Elemer Lelik committed
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
  int n_bits = val_ptr->n_bits;
  if (n_bits == 0) return *this;
  if (rotate_count >= 0) {
    rotate_count %= n_bits;
    if (rotate_count == 0) return *this;
    else return (*this >> rotate_count) |
           (*this << (n_bits - rotate_count));
  } else return *this <<= (-rotate_count);
}

BITSTRING BITSTRING::operator>>=(const INTEGER& rotate_count) const
{
  rotate_count.must_bound("Unbound right operand of bitstring rotate right "
    "operator.");
  return *this >>= (int)rotate_count;
}

BITSTRING_ELEMENT BITSTRING::operator[](int index_value)
{
  if (val_ptr == NULL && index_value == 0) {
    init_struct(1);
    clear_unused_bits();
    return BITSTRING_ELEMENT(FALSE, *this, 0);
  } else {
    must_bound("Accessing an element of an unbound bitstring value.");
    if (index_value < 0) TTCN_error("Accessing an bitstring element using "
      "a negative index (%d).", index_value);
    int n_bits = val_ptr->n_bits;
    if (index_value > n_bits) TTCN_error("Index overflow when accessing a "
      "bitstring element: The index is %d, but the string has only %d bits.",
      index_value, n_bits);
    if (index_value == n_bits) {
      if (val_ptr->ref_count == 1) {
	if (n_bits % 8 == 0) val_ptr = (bitstring_struct*)
	  Realloc(val_ptr, MEMORY_SIZE(n_bits + 1));
	val_ptr->n_bits++;
      } else {
	bitstring_struct *old_ptr = val_ptr;
	old_ptr->ref_count--;
	init_struct(n_bits + 1);
	memcpy(val_ptr->bits_ptr, old_ptr->bits_ptr, (n_bits + 7) / 8);
      }
      clear_unused_bits();
      return BITSTRING_ELEMENT(FALSE, *this, index_value);
    } else return BITSTRING_ELEMENT(TRUE, *this, index_value);
  }
}

BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value)
{
  index_value.must_bound("Indexing a bitstring value with an unbound integer "
    "value.");
  return (*this)[(int)index_value];
}

const BITSTRING_ELEMENT BITSTRING::operator[](int index_value) const
{
  must_bound("Accessing an element of an unbound bitstring value.");
  if (index_value < 0) TTCN_error("Accessing an bitstring element using a "
    "negative index (%d).", index_value);
  if (index_value >= val_ptr->n_bits) TTCN_error("Index overflow when "
    "accessing a bitstring element: The index is %d, but the string has only "
    "%d bits.", index_value, val_ptr->n_bits);
  return BITSTRING_ELEMENT(TRUE, const_cast<BITSTRING&>(*this), index_value);
}

const BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value) const
{
  index_value.must_bound("Indexing a bitstring value with an unbound integer "
    "value.");
  return (*this)[(int)index_value];
}

int BITSTRING::lengthof() const
{
  must_bound("Getting the length of an unbound bitstring value.");
  return val_ptr->n_bits;
}

BITSTRING::operator const unsigned char*() const
{
  must_bound("Casting an unbound bitstring value to const unsigned char*.");
  return val_ptr->bits_ptr;
}

void BITSTRING::log() const
{
  if (val_ptr != NULL) {
    TTCN_Logger::log_char('\'');
    for (int bit_count = 0; bit_count < val_ptr->n_bits; bit_count++)
      TTCN_Logger::log_char(get_bit(bit_count) ? '1' : '0');
    TTCN_Logger::log_event_str("'B");
  } else TTCN_Logger::log_event_unbound();
}

void BITSTRING::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, "bitstring value");
Elemer Lelik's avatar
Elemer Lelik committed
564
  Module_Param_Ptr mp = &param;
565
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
566
567
568
  if (param.get_type() == Module_Param::MP_Reference) {
    mp = param.get_referenced_param();
  }
569
#endif
Elemer Lelik's avatar
Elemer Lelik committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  switch (mp->get_type()) {
  case Module_Param::MP_Bitstring:
    switch (param.get_operation_type()) {
    case Module_Param::OT_ASSIGN:
      clean_up();
      init_struct(mp->get_string_size());
      memcpy(val_ptr->bits_ptr, mp->get_string_data(), (val_ptr->n_bits + 7) / 8);
      clear_unused_bits();
      break;
    case Module_Param::OT_CONCAT:
      if (is_bound()) {
        *this = *this + BITSTRING(mp->get_string_size(), (unsigned char*)mp->get_string_data());
      } else {
        *this = BITSTRING(mp->get_string_size(), (unsigned char*)mp->get_string_data());
      }
      break;
    default:
      TTCN_error("Internal error: BITSTRING::set_param()");
    }
Elemer Lelik's avatar
Elemer Lelik committed
589
    break;
Elemer Lelik's avatar
Elemer Lelik committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
  case Module_Param::MP_Expression:
    if (mp->get_expr_type() == Module_Param::EXPR_CONCATENATE) {
      BITSTRING operand1, operand2;
      operand1.set_param(*mp->get_operand1());
      operand2.set_param(*mp->get_operand2());
      if (param.get_operation_type() == Module_Param::OT_CONCAT) {
        *this = *this + operand1 + operand2;
      }
      else {
        *this = operand1 + operand2;
      }
    }
    else {
      param.expr_type_error("a bitstring");
Elemer Lelik's avatar
Elemer Lelik committed
604
605
606
    }
    break;
  default:
Elemer Lelik's avatar
Elemer Lelik committed
607
608
    param.type_error("bitstring value");
    break;
Elemer Lelik's avatar
Elemer Lelik committed
609
610
611
  }
}

612
#ifdef TITAN_RUNTIME_2
Elemer Lelik's avatar
Elemer Lelik committed
613
614
615
616
617
618
619
620
621
622
Module_Param* BITSTRING::get_param(Module_Param_Name& /* param_name */) const
{
  if (!is_bound()) {
    return new Module_Param_Unbound();
  }
  int n_bytes = (val_ptr->n_bits + 7) / 8;
  unsigned char* val_cpy = (unsigned char *)Malloc(n_bytes);
  memcpy(val_cpy, val_ptr->bits_ptr, n_bytes);
  return new Module_Param_Bitstring(val_ptr->n_bits, val_cpy);
}
623
#endif
Elemer Lelik's avatar
Elemer Lelik committed
624

Elemer Lelik's avatar
Elemer Lelik committed
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
void BITSTRING::encode_text(Text_Buf& text_buf) const
{
  must_bound("Text encoder: Encoding an unbound bitstring value.");
  int n_bits = val_ptr->n_bits;
  text_buf.push_int(n_bits);
  if (n_bits > 0) text_buf.push_raw((n_bits + 7) / 8, val_ptr->bits_ptr);
}

void BITSTRING::decode_text(Text_Buf& text_buf)
{
  int n_bits = text_buf.pull_int().get_val();
  if (n_bits < 0)
    TTCN_error("Text decoder: Invalid length was received for a bitstring.");
  clean_up();
  init_struct(n_bits);
  if (n_bits > 0) {
    text_buf.pull_raw((n_bits + 7) / 8, val_ptr->bits_ptr);
    clear_unused_bits();
  }
}

void BITSTRING::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;
668
    RAW_enc_tree root(TRUE,NULL,&rp,1,p_td.raw);
Elemer Lelik's avatar
Elemer Lelik committed
669
670
671
672
673
674
    RAW_encode(p_td, root);
    root.put_to_buf(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);
675
    XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0, 0);
Elemer Lelik's avatar
Elemer Lelik committed
676
677
    break;}
  case TTCN_EncDec::CT_JSON: {
Elemer Lelik's avatar
Elemer Lelik committed
678
    TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name);
Elemer Lelik's avatar
Elemer Lelik committed
679
680
681
682
683
684
685
    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);
    JSON_encode(p_td, tok);
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
    break;}
686
687
688
689
690
691
  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
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
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void BITSTRING::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_XER: {
Elemer Lelik's avatar
Elemer Lelik committed
734
    TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name);
Elemer Lelik's avatar
Elemer Lelik committed
735
736
737
738
739
740
741
742
    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
743
    XER_decode(*p_td.xer, reader, XER_coding, XER_NONE, 0);
Elemer Lelik's avatar
Elemer Lelik committed
744
745
746
747
748
749
750
751
752
    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());
753
    if(JSON_decode(p_td, tok, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
754
755
756
757
758
759
      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;}
760
761
762
763
764
765
766
  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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void BITSTRING::BER_encode_putbits(unsigned char *target,
                                   unsigned int bitnum_start,
                                   unsigned int bit_count) const
{
  unsigned int nof_bits, nof_octets, i, j;
  unsigned char c;

  nof_bits=val_ptr->n_bits;
  if(bitnum_start>nof_bits
     || bitnum_start+bit_count>nof_bits)
    TTCN_EncDec_ErrorContext::error_internal
      ("In BITSTRING::BER_encode_putbits(): Index overflow.");
  nof_octets=(bit_count+7)/8;
  if(!nof_octets) {
    target[0]=0x00;
    return;
  }
791
  target[0] = static_cast<unsigned char>(nof_octets*8-bit_count);
Elemer Lelik's avatar
Elemer Lelik committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
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
928
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  for(i=0; i<nof_octets-1; i++) {
    c=0;
    for(j=0; j<8; j++) {
      c<<=1;
      if(get_bit(bitnum_start+8*i+j)) c|=0x01;
    }
    target[1+i]=c;
  } // for
  c=0;
  for(j=0; j<8; j++) {
    c<<=1;
    if(8*i+j<bit_count)
      if(get_bit(bitnum_start+8*i+j)) c|=0x01;
  }
  target[1+i]=c;
}

ASN_BER_TLV_t*
BITSTRING::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) {
    unsigned char *V_ptr;
    size_t V_len;
    unsigned int nof_bits=val_ptr->n_bits;
    unsigned int nof_octets=(nof_bits+7)/8;
    unsigned int nof_fragments=0;
    if(p_coding==BER_ENCODE_CER) {
      nof_fragments=(nof_octets+998)/999;
      if(!nof_fragments) nof_fragments=1;
    }
    else /*if(coding==BER_ENCODE_DER)*/ {
      nof_fragments=1;
    }

    boolean is_constructed=nof_fragments>1;
    if(!is_constructed) {
      V_len=nof_octets+1;
      V_ptr=(unsigned char*)Malloc(V_len);
      BER_encode_putbits(V_ptr, 0, nof_bits);
      new_tlv=ASN_BER_TLV_t::construct(V_len, V_ptr);
    }
    else { // is constructed
      ASN_BER_TLV_t *tmp_tlv=NULL;
      new_tlv=ASN_BER_TLV_t::construct(NULL);
      unsigned int rest_octets=nof_octets-(nof_fragments-1)*999;
      /*
      V_len=(nof_fragments-1)*1004;
      V_len+=rest_octets<128?2:4;
      V_len+=rest_octets+1;
      V_ptr=(unsigned char*)Malloc(V_len);
      */
      V_len=999;
      unsigned int nof_bits_curr=8*999;
      for(unsigned int i=0; i<nof_fragments; i++) {
        if(i==nof_fragments-1) {
          V_len=rest_octets;
          nof_bits_curr=nof_bits-i*8*999;
        }
        V_ptr=(unsigned char*)Malloc(V_len+1); // because of unused bits-octet
        /*
        V_ptr[0]=0x03;
        V_ptr[1]=0x82;
        V_ptr[2]=0x03;
        V_ptr[3]=0xE8;
        */
        BER_encode_putbits(V_ptr, i*8*999, nof_bits_curr);
        /*
        V_ptr+=1004;
        */
        tmp_tlv=ASN_BER_TLV_t::construct(V_len+1, V_ptr);
        tmp_tlv=ASN_BER_V2TLV(tmp_tlv, BITSTRING_descr_, p_coding);
        new_tlv->add_TLV(tmp_tlv);
      }
      /*
      V_ptr[0]=0x03;
      if(rest_octets<128) {
        V_ptr[1]=(rest_octets+1) & '\x7F';
        V_ptr+=2;
      }
      else {
        V_ptr[1]=0x82;
        V_ptr[2]=((rest_octets+1)/256) & 0xFF;
        V_ptr[3]=(rest_octets+1) & 0xFF;
        V_ptr+=4;
      }
      BER_encode_putbits(V_ptr, i*8*999, nof_bits-i*8*999);
      */
    }
  }
  new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);
  return new_tlv;
}

void BITSTRING::BER_decode_getbits(const unsigned char *source,
                                   size_t s_len, unsigned int& bitnum_start)
{
  unsigned int i, j;
  unsigned char c;
  if(s_len<1) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_INVAL_MSG, "Length of V-part of bitstring"
       " cannot be 0.");
    return;
  }
  unsigned int nof_octets=s_len-1;
  unsigned int nof_restbits=8-source[0];
  if(nof_octets==0) {
    if(nof_restbits!=8)
      TTCN_EncDec_ErrorContext::error
        (TTCN_EncDec::ET_INVAL_MSG,
         "If the bitstring is empty,"
         " the initial octet shall be 0, not %u [see X.690 clause 8.6.2.3].",
         source[0]);
    return;
  }
  if(source[0]>7) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_INVAL_MSG, "The number of unused bits in bitstring"
       " cannot be %u (should be less than 8) [see X.690 clause 8.6.2.2].",
       source[0]);
    nof_restbits=1;
  }
  // And what about overflow? :)
  i = (nof_octets - 1) * 8 + nof_restbits;
  if (i > 0) {
    if (val_ptr->ref_count > 1) {
      bitstring_struct *old_ptr = val_ptr;
      old_ptr->ref_count--;
      init_struct(bitnum_start + i);
      memcpy(val_ptr->bits_ptr, old_ptr->bits_ptr, (old_ptr->n_bits + 7) / 8);
    } else {
      if ((bitnum_start + i + 7) / 8 > ((unsigned int)val_ptr->n_bits + 7) / 8)
	val_ptr = (bitstring_struct*)Realloc(val_ptr,
	  MEMORY_SIZE(bitnum_start + i));
      val_ptr->n_bits = bitnum_start + i;
    }
  }
  for(i=0; i<nof_octets-1; i++) {
    c=source[1+i];
    for(j=0; j<8; j++) {
      set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE);
      c<<=1;
    }
  }
  c=source[1+i];
  for(j=0; j<nof_restbits; j++) {
    set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE);
    c<<=1;
  }
  bitnum_start+=(nof_octets-1)*8+nof_restbits;
}

void BITSTRING::BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form,
                                unsigned int& bitnum_start)
{
  if(!p_tlv.isConstructed) {
    if (p_tlv.isComplete || p_tlv.V.str.Vlen > 0)
      BER_decode_getbits(p_tlv.V.str.Vstr, p_tlv.V.str.Vlen, bitnum_start);
  }
  else { // is constructed
    ASN_BER_TLV_t tlv2;
    size_t V_pos=0;
    boolean doit=TRUE;
    while(doit) {
      if(!ASN_BER_str2TLV(p_tlv.V.str.Vlen-V_pos, p_tlv.V.str.Vstr+V_pos,
                          tlv2, L_form)) {
        TTCN_EncDec_ErrorContext::error
          (TTCN_EncDec::ET_INCOMPL_MSG,
           "Incomplete TLV in a constructed BITSTRING TLV.");
        return;
      }
      if(!p_tlv.isLenDefinite && tlv2.tagnumber==0
         && tlv2.tagclass==ASN_TAG_UNIV)
        doit=FALSE; // End-of-contents
      if(doit) {
        ASN_BER_TLV_t stripped_tlv;
        BER_decode_strip_tags(BITSTRING_ber_, tlv2, L_form, stripped_tlv);
	BER_decode_TLV_(tlv2, L_form, bitnum_start);
	V_pos+=tlv2.get_len();
	if(V_pos>=p_tlv.V.str.Vlen) doit=FALSE;
      }
    } // while(doit)
  } // else / is constructed
}

boolean BITSTRING::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                                  const ASN_BER_TLV_t& p_tlv,
                                  unsigned L_form)
{
  clean_up();
  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 BITSTRING type: ");
  init_struct(0);
  unsigned int bitnum_start = 0;
  BER_decode_TLV_(stripped_tlv, L_form, bitnum_start);
  return TRUE;
}

int BITSTRING::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const
{
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound value.");
  }
  int bl = val_ptr->n_bits;
  int align_length = p_td.raw->fieldlength ? p_td.raw->fieldlength - bl : 0;
  if ((bl + align_length) < val_ptr->n_bits) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR,
      "There is no sufficient bits to encode '%s': ", p_td.name);
    bl = p_td.raw->fieldlength;
    align_length = 0;
  }
//  myleaf.ext_bit=EXT_BIT_NO;
  if (myleaf.must_free) Free(myleaf.body.leaf.data_ptr);
1011
1012
  myleaf.must_free = FALSE;
  myleaf.data_ptr_used = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1013
  myleaf.body.leaf.data_ptr = val_ptr->bits_ptr;
1014
1015
  boolean orders = FALSE;
  if (p_td.raw->byteorder       == ORDER_MSB) orders = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1016
1017
  if (p_td.raw->bitorderinfield == ORDER_LSB) orders = !orders;
  myleaf.coding_par.byteorder = orders ? ORDER_MSB : ORDER_LSB;
1018
1019
  orders = FALSE;
  if (p_td.raw->bitorderinoctet == ORDER_MSB) orders = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  if (p_td.raw->bitorderinfield == ORDER_LSB) orders = !orders;
  myleaf.coding_par.bitorder = orders ? ORDER_MSB : ORDER_LSB;

  if (p_td.raw->endianness == ORDER_MSB) myleaf.align =  align_length;
  else                                   myleaf.align = -align_length;

  return myleaf.length = bl + align_length;
}

int BITSTRING::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*/,
1031
  boolean /*first_call*/, const RAW_Force_Omit* /*force_omit*/)
Elemer Lelik's avatar
Elemer Lelik committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
{
  int prepaddlength = buff.increase_pos_padd(p_td.raw->prepadding);
  limit -= prepaddlength;
  int decode_length = p_td.raw->fieldlength == 0
    ? limit : p_td.raw->fieldlength;
  if ( p_td.raw->fieldlength > limit
    || p_td.raw->fieldlength > (int) buff.unread_len_bit()) {
    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.", p_td.name);
    decode_length = limit > (int) buff.unread_len_bit()
      ? buff.unread_len_bit() : limit;
  }
  clean_up();
  init_struct(decode_length);
  RAW_coding_par cp;
1048
1049
  boolean orders = FALSE;
  if (p_td.raw->bitorderinoctet == ORDER_MSB) orders = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1050
1051
  if (p_td.raw->bitorderinfield == ORDER_LSB) orders = !orders;
  cp.bitorder = orders ? ORDER_MSB : ORDER_LSB;
1052
1053
  orders = FALSE;
  if (p_td.raw->byteorder == ORDER_MSB) orders = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1054
1055
1056
1057
1058
  if (p_td.raw->bitorderinfield == ORDER_LSB) orders = !orders;
  cp.byteorder = orders ? ORDER_MSB : ORDER_LSB;
  cp.fieldorder = p_td.raw->fieldorder;
  cp.hexorder = ORDER_LSB;
  buff.get_b((size_t) decode_length, val_ptr->bits_ptr, cp, top_bit_ord);
1059
1060
  if (p_td.raw->length_restrition != -1 &&
      decode_length > p_td.raw->length_restrition) {
Elemer Lelik's avatar
Elemer Lelik committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
    val_ptr->n_bits = p_td.raw->length_restrition;
    if (p_td.raw->endianness == ORDER_LSB) {
      if ((decode_length - val_ptr->n_bits) % 8) {
        int bound = (decode_length - val_ptr->n_bits) % 8;
        int maxindex = (decode_length - 1) / 8;
        for (int a = 0, b = (decode_length - val_ptr->n_bits - 1) / 8;
          a < (val_ptr->n_bits + 7) / 8; a++, b++) {
          val_ptr->bits_ptr[a] = val_ptr->bits_ptr[b] >> bound;
          if (b < maxindex) {
            val_ptr->bits_ptr[a] = val_ptr->bits_ptr[b + 1] << (8 - bound);
          }
        }
      }
      else memmove(val_ptr->bits_ptr,
        val_ptr->bits_ptr + (decode_length - val_ptr->n_bits) / 8,
        val_ptr->n_bits / 8 * sizeof(unsigned char));
    }
  }
  decode_length += buff.increase_pos_padd(p_td.raw->padding);
  clear_unused_bits();
  return decode_length + prepaddlength;
}

int BITSTRING::XER_encode(const XERdescriptor_t& p_td,
1085
  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
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
{
  if(!is_bound()) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound bitstring value.");
  }

  int encoded_length=(int)p_buf.get_len();
  int empty_element = val_ptr==NULL || val_ptr->n_bits == 0;
  flavor |= SIMPLE_TYPE;
  flavor &= ~XER_RECOF; // bitstring doesn't care

  begin_xml(p_td, p_buf, flavor, indent, empty_element);

  if (!empty_element) {
    for (int bit_count = 0; bit_count < val_ptr->n_bits; bit_count++)
      p_buf.put_c(get_bit(bit_count) ? '1' : '0');
  }

  end_xml(p_td, p_buf, flavor, indent, empty_element);
  return (int)p_buf.get_len() - encoded_length;
}

int BITSTRING::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
Elemer Lelik's avatar
Elemer Lelik committed
1109
                          unsigned int flavor, unsigned int /*flavor2*/, embed_values_dec_struct_t*)
Elemer Lelik's avatar
Elemer Lelik committed
1110
{
1111
  boolean exer  = is_exer(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
  int success = reader.Ok(), depth = -1, type;
  boolean own_tag = !is_exerlist(flavor) && !(exer && (p_td.xer_bits & UNTAGGED));

  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
    const char * name = verify_name(reader, p_td, exer);
    (void)name;
  }
  else if (own_tag) {
    for (; success == 1; success = reader.Read()) {
      type = reader.NodeType();
      if (XML_READER_TYPE_ELEMENT == type) {
1123
1124
1125
1126
1127
        // 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
1128
1129
1130
1131
1132
1133
1134
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
1167
1168
1169
1170
1171
1172
1173
1174
1175
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
        verify_name(reader, p_td, exer);
        depth = reader.Depth();
        if (reader.IsEmptyElement()) {
          init_struct(0);
          reader.Read();
          return 1;
        }
      }
      else if (XML_READER_TYPE_TEXT == type && depth != -1) break;
      else if (XML_READER_TYPE_END_ELEMENT == type) {
        // End tag without intervening #text == empty content
        verify_end(reader, p_td, depth, exer);
        init_struct(0);
        reader.Read();
        return 1;
      }
    }
  }

  type = reader.NodeType();
  if (success == 1 && (XML_READER_TYPE_TEXT == type || XML_READER_TYPE_ATTRIBUTE == type)) {
    const char* value = (const char *)reader.Value();
    size_t num_bits = strlen(value);
    init_struct(num_bits);
    for (size_t i = 0; i < num_bits; ++i) {
      if (value[i] < '0' || value[i] > '1') {
        if (exer && (flavor & EXIT_ON_ERROR)) {
          clean_up();
          return -1;
        } else {
          TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG,
            "The bitstring value may only contain ones and zeros.");
        }
      }
      set_bit(i, value[i] - '0');
    }
  }
  
  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
    // Let the caller do reader.AdvanceAttribute();
  }
  else if (own_tag) {
    for (success = reader.Read(); 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;
      }
    }
  }
  return 1;
}

int BITSTRING::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok) const
{
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound bitstring value.");
    return -1;
  }
  
  char* tmp_str = (char*)Malloc(val_ptr->n_bits + 3);
  tmp_str[0] = '\"';
  tmp_str[val_ptr->n_bits + 1] = '\"';
  for (int i = 0; i < val_ptr->n_bits; ++i) {
    tmp_str[i + 1] = get_bit(i) ? '1' : '0';
  }
  tmp_str[val_ptr->n_bits + 2] = 0;
  int enc_len = p_tok.put_next_token(JSON_TOKEN_STRING, tmp_str);
  Free(tmp_str);
  return enc_len;
}

1202
int BITSTRING::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent, int)
Elemer Lelik's avatar
Elemer Lelik committed
1203
1204
1205
1206
{
  json_token_t token = JSON_TOKEN_NONE;
  char* value = 0;
  size_t value_len = 0;
1207
  boolean error = FALSE;
1208
  size_t dec_len = 0;
Elemer Lelik's avatar
Elemer Lelik committed
1209
1210
1211
  boolean use_default = p_td.json->default_value && 0 == p_tok.get_buffer_length();
  if (use_default) {
    // No JSON data in the buffer -> use default value
1212
    value = const_cast<char*>(p_td.json->default_value);
Elemer Lelik's avatar
Elemer Lelik committed
1213
1214
1215
1216
1217
1218
1219
1220
1221
    value_len = strlen(value);
  } else {
    dec_len = p_tok.get_next_token(&token, &value, &value_len);
  }
  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_STRING == token || use_default) {
1222
    if (use_default || (value_len >= 2 && value[0] == '\"' && value[value_len - 1] == '\"')) {
Elemer Lelik's avatar
Elemer Lelik committed
1223
1224
1225
1226
1227
      if (!use_default) {
        // The default value doesn't have quotes around it
        value_len -= 2;
        ++value;
      }
1228
1229
1230
      // White spaces are ignored, so the resulting bitstring might be shorter
      // than the extracted JSON string
      int bits = value_len;
Elemer Lelik's avatar
Elemer Lelik committed
1231
      for (size_t i = 0; i < value_len; ++i) {
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
        if (value[i] == ' ') {
          --bits;
        }
        else if (value[i] != '0' && value[i] != '1') {
          if (value[i] == '\\' && i + 1 < value_len &&
              (value[i + 1] == 'n' || value[i + 1] == 'r' || value[i + 1] == 't')) {
            // Escaped white space character
            ++i;
            bits -= 2;
          }
          else {
            error = TRUE;
            break;
          }
        }
      }
      if (!error) {
        init_struct(bits);
        int bit_index = 0;
        for (size_t i = 0; i < value_len; ++i) {
          if (value[i] == '0' || value[i] == '1') {
            set_bit(bit_index, value[i] - '0');
            ++bit_index;
          }
Elemer Lelik's avatar
Elemer Lelik committed
1256
1257
1258
        }
      }
    } else {
1259
      error = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1260
1261
1262
1263
1264
1265
1266
1267
1268
    }
  } else {
    return JSON_ERROR_INVALID_TOKEN;
  }
  
  if (error) {
    JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FORMAT_ERROR, "string", "bitstring");
    return JSON_ERROR_FATAL;    
  }
1269
  return (int)dec_len;
Elemer Lelik's avatar
Elemer Lelik committed
1270
1271
}

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
int BITSTRING::OER_encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf) const {
  if (!is_bound()) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound bitstring value.");
    return -1;
  }
  if (p_td.oer->length == -1) {
    size_t bytes = 1 + val_ptr->n_bits / 8;
    int rem = (val_ptr->n_bits % 8);
    if (rem != 0) {
      bytes++;
    }
    encode_oer_length(bytes, p_buf, FALSE);
    p_buf.put_c(rem == 0 ? 0 : 8 - rem);
  }
  char c = 0;
  int pos = 8;
  for (int i = 0; i < val_ptr->n_bits; i++) {
    pos--;
    c += get_bit(i) << pos;
    if (pos == 0) {
      p_buf.put_c(c);
      c = 0;
      pos = 8;
    }
  }
  if (pos != 8) {
    p_buf.put_c(c);
  }
  return 0;
}

int BITSTRING::OER_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, OER_struct&) {
  
  size_t bytes;
  if (p_td.oer->length == -1) {
    bytes = decode_oer_length(p_buf, FALSE) - 1;
    const unsigned char* uc = p_buf.get_read_data();
    init_struct((bytes * 8) - uc[0]);
    p_buf.increase_pos(1);
  } else {
    bytes = p_td.oer->length / 8;
    if (p_td.oer->length % 8 != 0) {
      bytes++;
    }
    init_struct(p_td.oer->length);
  }

  const unsigned char* uc = p_buf.get_read_data();
  int bit_pos = 0;
  for (size_t i = 0; i < bytes; i++) {
    for (int j = 7; j >= 0 && bit_pos < val_ptr->n_bits; j--) {
      set_bit(bit_pos, (uc[i] & (1 << j)));
      bit_pos++;
    }
  }
  p_buf.increase_pos(bytes);
  return 0;
}
Elemer Lelik's avatar
Elemer Lelik committed
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515

// bitstring element class

BITSTRING_ELEMENT::BITSTRING_ELEMENT(boolean par_bound_flag,
  BITSTRING& par_str_val, int par_bit_pos)
  : bound_flag(par_bound_flag), str_val(par_str_val), bit_pos(par_bit_pos)
{
}

BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator=(const BITSTRING& other_value)
{
  other_value.must_bound("Assignment of an unbound bitstring value.");
  if(other_value.val_ptr->n_bits != 1)
    TTCN_error("Assignment of a bitstring "
               "value with length other than 1 to a bitstring element.");
  bound_flag = TRUE;
  str_val.copy_value();
  str_val.set_bit(bit_pos, other_value.get_bit(0));
  return *this;
}

BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator=
(const BITSTRING_ELEMENT& other_value)
{
  other_value.must_bound("Assignment of an unbound bitstring element.");
  bound_flag = TRUE;
  str_val.copy_value();
  str_val.set_bit(bit_pos, other_value.str_val.get_bit(other_value.bit_pos));
  return *this;
}

boolean BITSTRING_ELEMENT::operator==(const BITSTRING& other_value) const
{
  must_bound("Unbound left operand of bitstring element comparison.");
  other_value.must_bound("Unbound right operand of bitstring comparison.");
  if(other_value.val_ptr->n_bits != 1) return FALSE;
  return str_val.get_bit(bit_pos) == other_value.get_bit(0);
}

boolean BITSTRING_ELEMENT::operator==
(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Unbound left operand of bitstring element comparison.");
  other_value.must_bound("Unbound right operand of bitstring element "
    "comparison.");
  return str_val.get_bit(bit_pos) ==
    other_value.str_val.get_bit(other_value.bit_pos);
}

BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING& other_value) const
{
  must_bound("Unbound left operand of bitstring element concatenation.");
  other_value.must_bound("Unbound right operand of bitstring concatenation.");
  int n_bits = other_value.val_ptr->n_bits;
  BITSTRING ret_val(n_bits + 1);
  ret_val.val_ptr->bits_ptr[0] = str_val.get_bit(bit_pos) ? 1 : 0;
  int n_bytes = (n_bits + 7) / 8;
  for (int byte_count = 0; byte_count < n_bytes; byte_count++) {
    ret_val.val_ptr->bits_ptr[byte_count] |=
      other_value.val_ptr->bits_ptr[byte_count] << 1;
    if (n_bits > byte_count * 8 + 7)
      ret_val.val_ptr->bits_ptr[byte_count + 1] =
	other_value.val_ptr->bits_ptr[byte_count] >> 7;
  }
  ret_val.clear_unused_bits();
  return ret_val;
}

BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING_ELEMENT& other_value)
  const
{
  must_bound("Unbound left operand of bitstring element concatenation.");
  other_value.must_bound("Unbound right operand of bitstring element "
                         "concatenation.");
  unsigned char result = str_val.get_bit(bit_pos) ? 1 : 0;
  if (other_value.str_val.get_bit(other_value.bit_pos)) result |= 2;
  return BITSTRING(2, &result);
}

BITSTRING BITSTRING_ELEMENT::operator~() const
{
  must_bound("Unbound bitstring element operand of operator not4b.");
  unsigned char result = str_val.get_bit(bit_pos) ? 0 : 1;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator&(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator and4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator and4b is an unbound "
    "bitstring value.");
  if (other_value.val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands "
               "of operator and4b must have the same length.");
  unsigned char result = str_val.get_bit(bit_pos) && other_value.get_bit(0) ?
    1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator&
(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator and4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator and4b is an unbound "
    "bitstring element.");
  unsigned char result = str_val.get_bit(bit_pos) &&
    other_value.str_val.get_bit(other_value.bit_pos) ? 1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator|(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator or4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator or4b is an unbound "
    "bitstring value.");
  if (other_value.val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands "
               "of operator or4b must have the same length.");
  unsigned char result = str_val.get_bit(bit_pos) || other_value.get_bit(0) ?
    1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator|
(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator or4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator or4b is an unbound "
    "bitstring element.");
  unsigned char result = str_val.get_bit(bit_pos) ||
    other_value.str_val.get_bit(other_value.bit_pos) ? 1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator^(const BITSTRING& other_value) const
{
  must_bound("Left operand of operator xor4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator xor4b is an unbound "
    "bitstring value.");
  if (other_value.val_ptr->n_bits != 1)
    TTCN_error("The bitstring operands "
               "of operator xor4b must have the same length.");
  unsigned char result = str_val.get_bit(bit_pos) != other_value.get_bit(0) ?
    1 : 0;
  return BITSTRING(1, &result);
}

BITSTRING BITSTRING_ELEMENT::operator^
(const BITSTRING_ELEMENT& other_value) const
{
  must_bound("Left operand of operator xor4b is an unbound bitstring element.");
  other_value.must_bound("Right operand of operator xor4b is an unbound "
    "bitstring element.");
  unsigned char result = str_val.get_bit(bit_pos) !=
    other_value.str_val.get_bit(other_value.bit_pos) ? 1 : 0;
  return BITSTRING(1, &result);
}

boolean BITSTRING_ELEMENT::get_bit() const
{
  return str_val.get_bit(bit_pos);
}

void BITSTRING_ELEMENT::log() const
{
  if (bound_flag) TTCN_Logger::log_event("'%c'B", str_val.get_bit(bit_pos) ?
    '1' : '0');
  else TTCN_Logger::log_event_unbound();
}

// bitstring template class

void BITSTRING_template::clean_up()
{
  switch (template_selection) {
  case VALUE_LIST:
  case COMPLEMENTED_LIST:
    delete [] value_list.list_value;
    break;
  case STRING_PATTERN:
    if (pattern_value->ref_count > 1) pattern_value->ref_count--;
    else if (pattern_value->ref_count == 1) Free(pattern_value);
    else TTCN_error("Internal error: Invalid reference counter in a bitstring "
      "pattern.");
    break;
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
  case DECODE_MATCH:
    if (dec_match->ref_count > 1) {
      dec_match->ref_count--;
    }
    else if (dec_match->ref_count == 1) {
      delete dec_match->instance;
      delete dec_match;
    }
    else {
      TTCN_error("Internal error: Invalid reference counter in a "
        "decoded content match.");
    }
    break;
Elemer Lelik's avatar
Elemer Lelik committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
  default:
    break;
  }
  template_selection = UNINITIALIZED_TEMPLATE;
}

void BITSTRING_template::copy_template(const BITSTRING_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 BITSTRING_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;
  case STRING_PATTERN:
    pattern_value = other_value.pattern_value;
    pattern_value->ref_count++;
    break;
1557
1558
1559
1560
  case DECODE_MATCH:
    dec_match = other_value.dec_match;
    dec_match->ref_count++;
    break;
Elemer Lelik's avatar
Elemer Lelik committed
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
  default:
    TTCN_error("Copying an uninitialized/unsupported bitstring template.");
  }
  set_selection(other_value);
}

/*
  This is the same algorithm that match_array uses
    to match 'record of' types.
  The only differences are: how two elements are matched and
    how an asterisk or ? is identified in the template
*/
boolean BITSTRING_template::match_pattern(
  const bitstring_pattern_struct *string_pattern,
  const BITSTRING::bitstring_struct *string_value)
{
  if(string_pattern->n_elements == 0) return string_value->n_bits == 0;

  int value_index = 0;
  unsigned int template_index = 0;
  int last_asterisk = -1;
  int last_value_to_asterisk = -1;

  for(;;)
  {
    switch(string_pattern->elements_ptr[template_index]) {
      case 0:
        if (!(string_value->bits_ptr[value_index / 8] &
          (1 << (value_index % 8))))
        {
          value_index++;
          template_index++;
        }else{
          if(last_asterisk == -1) return FALSE;
          template_index = last_asterisk +1;
          value_index = ++last_value_to_asterisk;
        }
        break;
      case 1:
        if (string_value->bits_ptr[value_index / 8] & (1 << (value_index % 8)))
        {
          value_index++;
          template_index++;
        }else {
          if(last_asterisk == -1) return FALSE;
          template_index = last_asterisk +1;
          value_index = ++last_value_to_asterisk;
        }
        break;
      case 2:
        //we found a ? element, it matches anything
        value_index++;
        template_index++;
        break;
      case 3:
        //we found an asterisk
        last_asterisk = template_index++;
        last_value_to_asterisk = value_index;
        break;
      default:
        TTCN_error("Internal error: invalid element in bitstring pattern.");
    }

    if(value_index == string_value->n_bits
      && template_index == string_pattern->n_elements)
    {
      return TRUE;
    }else if(template_index == string_pattern->n_elements)
    {
      if(string_pattern->elements_ptr[template_index-1] == 3)
      {
        return TRUE;
      } else if (last_asterisk == -1){
        return FALSE;
      } else{
        template_index = last_asterisk+1;
        value_index = ++last_value_to_asterisk;
      }
    } else if(value_index == string_value->n_bits)
    {
      while(template_index < string_pattern->n_elements &&
            string_pattern->elements_ptr[template_index] == 3)
        template_index++;

      return template_index == string_pattern->n_elements;
    }
  }
}

BITSTRING_template::BITSTRING_template()
{
}

BITSTRING_template::BITSTRING_template(template_sel other_value)
  : Restricted_Length_Template(other_value)
{
  check_single_selection(other_value);
}

BITSTRING_template::BITSTRING_template(const BITSTRING& other_value)
  : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value)
{
}

BITSTRING_template::BITSTRING_template(const BITSTRING_ELEMENT& other_value)
  : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value)
{
}

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

BITSTRING_template::BITSTRING_template(const OPTIONAL<BITSTRING>& other_value)
{
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    single_value = (const BITSTRING&)other_value;
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
1685
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
    TTCN_error("Creating a bitstring template from an unbound optional field.");
  }
}

BITSTRING_template::BITSTRING_template(unsigned int n_elements,
  const unsigned char *pattern_elements)
  : Restricted_Length_Template(STRING_PATTERN)
{
  pattern_value = (bitstring_pattern_struct*)
    Malloc(sizeof(bitstring_pattern_struct) + n_elements - 1);
  pattern_value->ref_count = 1;
  pattern_value->n_elements = n_elements;
  memcpy(pattern_value->elements_ptr, pattern_elements, n_elements);
}

BITSTRING_template::BITSTRING_template(const BITSTRING_template& other_value)
: Restricted_Length_Template()
{
  copy_template(other_value);
}

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

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

BITSTRING_template& BITSTRING_template::operator=
  (const BITSTRING_ELEMENT& other_value)
{
  other_value.must_bound("Assignment of an unbound bitstring element to a "
                         "template.");
  clean_up();
  set_selection(SPECIFIC_VALUE);
  single_value = other_value;
  return *this;
}

BITSTRING_template& BITSTRING_template::operator=
  (const OPTIONAL<BITSTRING>& other_value)
{
  clean_up();
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    set_selection(SPECIFIC_VALUE);
    single_value = (const BITSTRING&)other_value;
    break;
  case OPTIONAL_OMIT:
    set_selection(OMIT_VALUE);
    break;
1748
  case OPTIONAL_UNBOUND:
Elemer Lelik's avatar
Elemer Lelik committed
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
    TTCN_error("Assignment of an unbound optional field to a bitstring "
      "template.");
  }
  return *this;
}

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

1765
#ifdef TITAN_RUNTIME_2
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
void BITSTRING_template::concat(Vector<unsigned char>& v) const
{
  switch (template_selection) {
  case ANY_VALUE:
  case ANY_OR_OMIT:
    switch (length_restriction_type) {
    case NO_LENGTH_RESTRICTION:
      if (template_selection == ANY_VALUE) {
        // ? => '*'
        if (v.size() == 0 || v[v.size() - 1] != 3) {
          // '**' == '*', so just ignore the second '*'
          v.push_back(3);
        }
      }
      else {
        TTCN_error("Operand of bitstring template concatenation is an "
          "AnyValueOrNone (*) matching mechanism with no length restriction");
      }
      break;
    case RANGE_LENGTH_RESTRICTION:
      if (!length_restriction.range_length.max_length ||
          length_restriction.range_length.max_length != length_restriction.range_length.min_length) {
        TTCN_error("Operand of bitstring template concatenation is an %s "
          "matching mechanism with non-fixed length restriction",
          template_selection == ANY_VALUE ? "AnyValue (?)" : "AnyValueOrNone (*)");
      }
      // else fall through (range length restriction is allowed if the minimum
      // and maximum value are the same)
    case SINGLE_LENGTH_RESTRICTION: {
      // ? length(N) or * length(N) => '??...?' N times
      int len = length_restriction_type == SINGLE_LENGTH_RESTRICTION ?
        length_restriction.single_length : length_restriction.range_length.min_length;
      for (int i = 0; i < len; ++i) {
        v.push_back(2);
      }
      break; }
    }
    break;
  case SPECIFIC_VALUE:
    concat(v, single_value);
    break;
  case STRING_PATTERN:
    for (unsigned int i = 0; i < pattern_value->n_elements; ++i) {
      v.push_back(pattern_value->elements_ptr[i]);
    }
    break;
  default:
    TTCN_error("Operand of bitstring template concatenation is an "
      "uninitialized or unsupported template.");
  }
}

void BITSTRING_template::concat(Vector<unsigned char>& v, const BITSTRING& val)
{
  if (!val.is_bound()) {
    TTCN_error("Operand of bitstring template concatenation is an "
      "unbound value.");
  }
  for (int i = 0; i < val.val_ptr->n_bits; ++i) {
    v.push_back(val.get_bit(i));
  }
}

void BITSTRING_template::concat(Vector<unsigned char>& v, template_sel sel)
{
  if (sel == ANY_VALUE) {
    // ? => '*'
    if (v.size() == 0 || v[v.size() - 1] != 3) {
      // '**' == '*', so just ignore the second '*'
      v.push_back(3);
    }
  }
  else {
    TTCN_error("Operand of bitstring template concatenation is an "
      "uninitialized or unsupported template.");
  }
}


BITSTRING_template BITSTRING_template::operator+(
  const BITSTRING_template& other_value) const
{
  if (template_selection == SPECIFIC_VALUE &&
      other_value.template_selection == SPECIFIC_VALUE) {
    // result is a specific value template
    return single_value + other_value.single_value;
  }
  if (template_selection == ANY_VALUE &&
      other_value.template_selection == ANY_VALUE &&
      length_restriction_type == NO_LENGTH_RESTRICTION &&
      other_value.length_restriction_type == NO_LENGTH_RESTRICTION) {
    // special case: ? & ? => ?
    return BITSTRING_template(ANY_VALUE);
  }
  // otherwise the result is an bitstring pattern
  Vector<unsigned char> v;
  concat(v);
  other_value.concat(v);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template BITSTRING_template::operator+(
  const BITSTRING& other_value) const
{
  if (template_selection == SPECIFIC_VALUE) {
    // result is a specific value template
    return single_value + other_value;
  }
  // otherwise the result is an bitstring pattern
  Vector<unsigned char> v;
  concat(v);
  concat(v, other_value);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template BITSTRING_template::operator+(
  const BITSTRING_ELEMENT& other_value) const
{
  return *this + BITSTRING(other_value);
}

BITSTRING_template BITSTRING_template::operator+(
  template_sel other_template_sel) const
{
  if (template_selection == ANY_VALUE && other_template_sel == ANY_VALUE &&
      length_restriction_type == NO_LENGTH_RESTRICTION) {
    // special case: ? & ? => ?
    return BITSTRING_template(ANY_VALUE);
  }
  // the result is always an bitstring pattern
  Vector<unsigned char> v;
  concat(v);
  concat(v, other_template_sel);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template operator+(const BITSTRING& left_value,
  const BITSTRING_template& right_template)
{
  if (right_template.template_selection == SPECIFIC_VALUE) {
    // result is a specific value template
    return left_value + right_template.single_value;
  }
  // otherwise the result is an bitstring pattern
  Vector<unsigned char> v;
  BITSTRING_template::concat(v, left_value);
  right_template.concat(v);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template operator+(const BITSTRING_ELEMENT& left_value,
  const BITSTRING_template& right_template)
{
  return BITSTRING(left_value) + right_template;
}

BITSTRING_template operator+(template_sel left_template_sel,
  const BITSTRING_template& right_template)
{
  if (left_template_sel == ANY_VALUE &&
      right_template.template_selection == ANY_VALUE &&
      right_template.length_restriction_type ==
      Restricted_Length_Template::NO_LENGTH_RESTRICTION) {
    // special case: ? & ? => ?
    return BITSTRING_template(ANY_VALUE);
  }
  // the result is always an bitstring pattern
  Vector<unsigned char> v;
  BITSTRING_template::concat(v, left_template_sel);
  right_template.concat(v);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template operator+(const BITSTRING& left_value,
  template_sel right_template_sel)
{
  // the result is always an bitstring pattern
  Vector<unsigned char> v;
  BITSTRING_template::concat(v, left_value);
  BITSTRING_template::concat(v, right_template_sel);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template operator+(const BITSTRING_ELEMENT& left_value,
  template_sel right_template_sel)
{
  return BITSTRING(left_value) + right_template_sel;
}

BITSTRING_template operator+(template_sel left_template_sel,
  const BITSTRING& right_value)
{
  // the result is always an bitstring pattern
  Vector<unsigned char> v;
  BITSTRING_template::concat(v, left_template_sel);
  BITSTRING_template::concat(v, right_value);
  return BITSTRING_template(v.size(), v.data_ptr());
}

BITSTRING_template operator+(template_sel left_template_sel,
  const BITSTRING_ELEMENT& right_value)
{
  return left_template_sel + BITSTRING(right_value);
}
1970
#endif // TITAN_RUNTIME_2
1971

Elemer Lelik's avatar
Elemer Lelik committed
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value)
{
  if (template_selection != SPECIFIC_VALUE || is_ifpresent)
    TTCN_error("Accessing a bitstring element of a non-specific bitstring "
               "template.");
  return single_value[index_value];
}

BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value)
{
  index_value.must_bound("Indexing a bitstring template with an unbound "
                         "integer value.");
  return (*this)[(int)index_value];
}

const BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value) const
{
  if (template_selection != SPECIFIC_VALUE || is_ifpresent)
    TTCN_error("Accessing a bitstring element of a non-specific bitstring "
               "template.");
  return single_value[index_value];
}

const BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value) const
{
  index_value.must_bound("Indexing a bitstring template with an unbound "
                         "integer value.");
  return (*this)[(int)index_value];
}

Elemer Lelik's avatar
Elemer Lelik committed
2002
2003
boolean BITSTRING_template::match(const BITSTRING& other_value,
                                  boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
{
  if (!other_value.is_bound()) return FALSE;
  if (!match_length(other_value.val_ptr->n_bits)) return FALSE;
  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;
  case STRING_PATTERN:
    return match_pattern(pattern_value, other_value.val_ptr);
2023
2024
2025
2026
2027
2028
2029
2030
2031
  case DECODE_MATCH: {
    TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
    TTCN_EncDec::clear_error();
    OCTETSTRING os(bit2oct(other_value));
    TTCN_Buffer buff(os);
    boolean ret_val = dec_match->instance->match(buff);
    TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL,TTCN_EncDec::EB_DEFAULT);
    TTCN_EncDec::clear_error();
    return ret_val; }
Elemer Lelik's avatar
Elemer Lelik committed
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
  default:
    TTCN_error("Matching an uninitialized/unsupported bitstring template.");
  }
  return FALSE;
}

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

int BITSTRING_template::lengthof() const
{
  int min_length;
  boolean has_any_or_none;
  if (is_ifpresent)
    TTCN_error("Performing lengthof() operation on a bitstring template "
               "which has an ifpresent attribute.");
  switch (template_selection)
  {
  case SPECIFIC_VALUE:
    min_length = single_value.lengthof();
    has_any_or_none = FALSE;
    break;
  case OMIT_VALUE:
    TTCN_error("Performing lengthof() operation on a bitstring template "
               "containing omit value.");
  case ANY_VALUE:
  case ANY_OR_OMIT:
    min_length = 0;
    has_any_or_none = TRUE; // max. length is infinity
    break;
  case VALUE_LIST:
  {
    // error if any element does not have length or the lengths differ
    if (value_list.n_values<1)
      TTCN_error("Internal error: "
                 "Performing lengthof() operation on a bitstring template "
                 "containing an empty list.");
    int item_length = value_list.list_value[0].lengthof();
    for (unsigned int i = 1; i < value_list.n_values; i++) {
      if (value_list.list_value[i].lengthof()!=item_length)
        TTCN_error("Performing lengthof() operation on a bitstring template "
                   "containing a value list with different lengths.");
    }
    min_length = item_length;
    has_any_or_none = FALSE;
    break;
  }
  case COMPLEMENTED_LIST:
    TTCN_error("Performing lengthof() operation on a bitstring template "
               "containing complemented list.");
  case STRING_PATTERN:
    min_length = 0;
    has_any_or_none = FALSE; // TRUE if * chars in the pattern
    for (unsigned int i = 0; i < pattern_value->n_elements; i++)
    {
      if (pattern_value->elements_ptr[i] < 3) min_length++; // case of 1, 0, ?
      else has_any_or_none = TRUE;   // case of * character
    }
    break;
  default:
    TTCN_error("Performing lengthof() operation on an "
               "uninitialized/unsupported bitstring template.");
  }
  return check_section_is_single(min_length, has_any_or_none,
                                 "length", "a", "bitstring template");
}

void BITSTRING_template::set_type(template_sel template_type,
2105
  unsigned int list_length /* = 0 */)
Elemer Lelik's avatar
Elemer Lelik committed
2106
{
2107
2108
  if (template_type != VALUE_LIST && template_type != COMPLEMENTED_LIST &&
      template_type != DECODE_MATCH)
Elemer Lelik's avatar
Elemer Lelik committed
2109
2110
2111
    TTCN_error("Setting an invalid list type for a bitstring template.");
  clean_up();
  set_selection(template_type);
2112
2113
2114
2115
  if (template_type != DECODE_MATCH) {
    value_list.n_values = list_length;
    value_list.list_value = new BITSTRING_template[list_length];
  }
Elemer Lelik's avatar
Elemer Lelik committed
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
}

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

2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
void BITSTRING_template::set_decmatch(Dec_Match_Interface* new_instance)
{
  if (template_selection != DECODE_MATCH) {
    TTCN_error("Setting the decoded content matching mechanism of a non-decmatch "
      "bitstring template.");
  }
  dec_match = new decmatch_struct;
  dec_match->ref_count = 1;
  dec_match->instance = new_instance;
}

2139
2140
2141
2142
2143
2144
2145
2146
2147
void* BITSTRING_template::get_decmatch_dec_res() const
{
  if (template_selection != DECODE_MATCH) {
    TTCN_error("Retrieving the decoding result of a non-decmatch bitstring "
      "template.");
  }
  return dec_match->instance->get_dec_res();
}

2148
2149
2150
2151
2152
2153
2154
2155
2156
const TTCN_Typedescriptor_t* BITSTRING_template::get_decmatch_type_descr() const
{
  if (template_selection != DECODE_MATCH) {
    TTCN_error("Retrieving the decoded type's descriptor in a non-decmatch "
      "bitstring template.");
  }
  return dec_match->instance->get_type_descr();
}

Elemer Lelik's avatar
Elemer Lelik committed
2157
2158
2159
2160
2161
2162
2163
2164
2165
static const char patterns[] = { '0', '1', '?', '*' };

void BITSTRING_template::log() const
{
  switch (template_selection) {
  case SPECIFIC_VALUE:
    single_value.log();
    break;
  case COMPLEMENTED_LIST:
2166
    TTCN_Logger::log_event_str("complement");
Elemer Lelik's avatar
Elemer Lelik committed
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
    // 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;
  case STRING_PATTERN:
    TTCN_Logger::log_char('\'');
    for (unsigned int i = 0; i < pattern_value->n_elements; i++) {
      unsigned char pattern = pattern_value->elements_ptr[i];
      if (pattern < 4) TTCN_Logger::log_char(patterns[pattern]);
      else TTCN_Logger::log_event_str("<unknown>");
    }
    TTCN_Logger::log_event_str("'B");
    break;
2185
2186
2187
2188
  case DECODE_MATCH:
    TTCN_Logger::log_event_str("decmatch ");
    dec_match->instance->log();
    break;
Elemer Lelik's avatar
Elemer Lelik committed
2189
2190
2191
2192
2193
2194
2195
2196
  default:
    log_generic();
    break;
  }
  log_restricted();
  log_ifpresent();
}

Elemer Lelik's avatar
Elemer Lelik committed
2197
2198
void BITSTRING_template::log_match(const BITSTRING& match_value,
                                   boolean /* legacy */) const
Elemer Lelik's avatar
Elemer Lelik committed
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
{
  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 BITSTRING_template::set_param(Module_Param& param) {
  param.basic_check(Module_Param::BC_TEMPLATE|Module_Param::BC_LIST, "bitstring template");
Elemer Lelik's avatar
Elemer Lelik committed
2214
  Module_Param_Ptr mp = &param;