1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
19 * Szabo, Janos Zoltan – initial implementation
23 ******************************************************************************/
26 #include "Bitstring.hh"
27 #include "../common/memory.h"
29 #include "String_struct.hh"
30 #include "Parameters.h"
31 #include "Param_Types.hh"
36 #include "../common/dbgnew.hh"
38 // bitstring value class
40 /** The amount of memory needed for a bitstring containing n bits. */
41 #define MEMORY_SIZE(n) (sizeof(bitstring_struct) - sizeof(int) + ((n) + 7) / 8)
43 void BITSTRING::init_struct(int n_bits
)
47 TTCN_error("Initializing a bitstring with a negative length.");
48 } else if (n_bits
== 0) {
49 /** This will represent the empty strings so they won't need allocated
50 * memory, this delays the memory allocation until it is really needed.
52 static bitstring_struct empty_string
= { 1, 0, "" };
53 val_ptr
= &empty_string
;
54 empty_string
.ref_count
++;
56 val_ptr
= (bitstring_struct
*)Malloc(MEMORY_SIZE(n_bits
));
57 val_ptr
->ref_count
= 1;
58 val_ptr
->n_bits
= n_bits
;
62 boolean
BITSTRING::get_bit(int bit_index
) const
64 return val_ptr
->bits_ptr
[bit_index
/ 8] & (1 << (bit_index
% 8));
67 void BITSTRING::set_bit(int bit_index
, boolean new_value
)
69 unsigned char mask
= 1 << (bit_index
% 8);
70 if (new_value
) val_ptr
->bits_ptr
[bit_index
/ 8] |= mask
;
71 else val_ptr
->bits_ptr
[bit_index
/ 8] &= ~mask
;
74 void BITSTRING::copy_value()
76 if (val_ptr
== NULL
|| val_ptr
->n_bits
<= 0)
77 TTCN_error("Internal error: Invalid internal data structure when copying "
78 "the memory area of a bitstring value.");
79 if (val_ptr
->ref_count
> 1) {
80 bitstring_struct
*old_ptr
= val_ptr
;
82 init_struct(old_ptr
->n_bits
);
83 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (old_ptr
->n_bits
+ 7) / 8);
87 void BITSTRING::clear_unused_bits() const
89 int n_bits
= val_ptr
->n_bits
;
90 if (n_bits
% 8 != 0) val_ptr
->bits_ptr
[(n_bits
- 1) / 8] &=
91 (unsigned char)'\377' >> (7 - (n_bits
- 1) % 8);
94 BITSTRING::BITSTRING(int n_bits
)
99 BITSTRING::BITSTRING()
104 BITSTRING::BITSTRING(int n_bits
, const unsigned char *bits_ptr
)
107 memcpy(val_ptr
->bits_ptr
, bits_ptr
, (n_bits
+ 7) / 8);
111 BITSTRING::BITSTRING(const BITSTRING
& other_value
)
112 : Base_Type(other_value
)
114 other_value
.must_bound("Copying an unbound bitstring value.");
115 val_ptr
= other_value
.val_ptr
;
116 val_ptr
->ref_count
++;
119 BITSTRING::BITSTRING(const BITSTRING_ELEMENT
& other_value
)
121 other_value
.must_bound("Copying an unbound bitstring element.");
123 val_ptr
->bits_ptr
[0] = other_value
.get_bit() ? 1 : 0;
126 BITSTRING::~BITSTRING()
131 void BITSTRING::clean_up()
133 if (val_ptr
!= NULL
) {
134 if (val_ptr
->ref_count
> 1) val_ptr
->ref_count
--;
135 else if (val_ptr
->ref_count
== 1) Free(val_ptr
);
136 else TTCN_error("Internal error: Invalid reference counter in a bitstring "
142 BITSTRING
& BITSTRING::operator=(const BITSTRING
& other_value
)
144 other_value
.must_bound("Assignment of an unbound bitstring value.");
145 if (&other_value
!= this) {
147 val_ptr
= other_value
.val_ptr
;
148 val_ptr
->ref_count
++;
153 BITSTRING
& BITSTRING::operator=(const BITSTRING_ELEMENT
& other_value
)
155 other_value
.must_bound("Assignment of an unbound bitstring element to a "
157 boolean bit_value
= other_value
.get_bit();
160 val_ptr
->bits_ptr
[0] = bit_value
? 1 : 0;
164 boolean
BITSTRING::operator==(const BITSTRING
& other_value
) const
166 must_bound("Unbound left operand of bitstring comparison.");
167 other_value
.must_bound("Unbound right operand of bitstring comparison.");
168 int n_bits
= val_ptr
->n_bits
;
169 if (n_bits
!= other_value
.val_ptr
->n_bits
) return FALSE
;
170 if (n_bits
== 0) return TRUE
;
172 other_value
.clear_unused_bits();
173 return !memcmp(val_ptr
->bits_ptr
, other_value
.val_ptr
->bits_ptr
,
177 boolean
BITSTRING::operator==(const BITSTRING_ELEMENT
& other_value
) const
179 must_bound("Unbound left operand of bitstring comparison.");
180 other_value
.must_bound("Unbound right operand of bitstring element "
182 if (val_ptr
->n_bits
!= 1) return FALSE
;
183 return get_bit(0) == other_value
.get_bit();
186 BITSTRING
BITSTRING::operator+(const BITSTRING
& other_value
) const
188 must_bound("Unbound left operand of bitstring concatenation.");
189 other_value
.must_bound("Unbound right operand of bitstring concatenation.");
191 int left_n_bits
= val_ptr
->n_bits
;
192 if (left_n_bits
== 0) return other_value
;
194 int right_n_bits
= other_value
.val_ptr
->n_bits
;
195 if (right_n_bits
== 0) return *this;
197 // the length of result
198 int n_bits
= left_n_bits
+ right_n_bits
;
200 // the number of bytes used
201 int left_n_bytes
= (left_n_bits
+ 7) / 8;
202 int right_n_bytes
= (right_n_bits
+ 7) / 8;
204 // the number of bits used in the last incomplete octet of the left operand
205 int left_empty_bits
= left_n_bits
% 8;
208 BITSTRING
ret_val(n_bits
);
210 // pointers to the data areas
211 const unsigned char *left_ptr
= val_ptr
->bits_ptr
;
212 const unsigned char *right_ptr
= other_value
.val_ptr
->bits_ptr
;
213 unsigned char *dest_ptr
= ret_val
.val_ptr
->bits_ptr
;
215 // copying the left fragment into the result
216 memcpy(dest_ptr
, left_ptr
, left_n_bytes
);
218 if (left_empty_bits
!= 0) {
219 // non-trivial case: the length of left fragment is not a multiply of 8
220 // the bytes used in the result
221 int n_bytes
= (n_bits
+ 7) / 8;
222 // placing the bytes from the right fragment until the result is filled
223 for (int i
= left_n_bytes
; i
< n_bytes
; i
++) {
224 unsigned char right_byte
= right_ptr
[i
- left_n_bytes
];
225 // finish filling the previous byte
226 dest_ptr
[i
- 1] |= right_byte
<< left_empty_bits
;
227 // start filling the actual byte
228 dest_ptr
[i
] = right_byte
>> (8 - left_empty_bits
);
230 if (left_n_bytes
+ right_n_bytes
> n_bytes
) {
231 // if the result data area is shorter than the two operands together
232 // the last bits of right fragment were not placed into the result
233 // in the previous for loop
234 dest_ptr
[n_bytes
- 1] |= right_ptr
[right_n_bytes
- 1] << left_empty_bits
;
237 // trivial case: just append the bytes of the right fragment
238 memcpy(dest_ptr
+ left_n_bytes
, right_ptr
, right_n_bytes
);
240 ret_val
.clear_unused_bits();
244 BITSTRING
BITSTRING::operator+(const BITSTRING_ELEMENT
& other_value
) const
246 must_bound("Unbound left operand of bitstring concatenation.");
247 other_value
.must_bound("Unbound right operand of bitstring element "
250 int n_bits
= val_ptr
->n_bits
;
251 BITSTRING
ret_val(n_bits
+ 1);
252 memcpy(ret_val
.val_ptr
->bits_ptr
, val_ptr
->bits_ptr
, (n_bits
+ 7) / 8);
253 ret_val
.set_bit(n_bits
, other_value
.get_bit());
257 BITSTRING
BITSTRING::operator~() const
259 must_bound("Unbound bitstring operand of operator not4b.");
260 int n_bytes
= (val_ptr
->n_bits
+ 7) / 8;
261 if (n_bytes
== 0) return *this;
262 BITSTRING
ret_val(val_ptr
->n_bits
);
263 for (int i
= 0; i
< n_bytes
; i
++)
264 ret_val
.val_ptr
->bits_ptr
[i
] = ~val_ptr
->bits_ptr
[i
];
265 ret_val
.clear_unused_bits();
269 BITSTRING
BITSTRING::operator&(const BITSTRING
& other_value
) const
271 must_bound("Left operand of operator and4b is an unbound bitstring value.");
272 other_value
.must_bound("Right operand of operator and4b is an unbound "
274 int n_bits
= val_ptr
->n_bits
;
275 if (n_bits
!= other_value
.val_ptr
->n_bits
)
276 TTCN_error("The bitstring operands of operator and4b must have the "
278 if (n_bits
== 0) return *this;
279 BITSTRING
ret_val(n_bits
);
280 int n_bytes
= (n_bits
+ 7) / 8;
281 for (int i
= 0; i
< n_bytes
; i
++)
282 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] &
283 other_value
.val_ptr
->bits_ptr
[i
];
284 ret_val
.clear_unused_bits();
288 BITSTRING
BITSTRING::operator&(const BITSTRING_ELEMENT
& other_value
) const
290 must_bound("Left operand of operator and4b is an unbound bitstring value.");
291 other_value
.must_bound("Right operand of operator and4b is an unbound "
292 "bitstring element.");
293 if (val_ptr
->n_bits
!= 1)
294 TTCN_error("The bitstring operands of "
295 "operator and4b must have the same length.");
296 unsigned char result
= get_bit(0) && other_value
.get_bit() ? 1 : 0;
297 return BITSTRING(1, &result
);
300 BITSTRING
BITSTRING::operator|(const BITSTRING
& other_value
) const
302 must_bound("Left operand of operator or4b is an unbound bitstring value.");
303 other_value
.must_bound("Right operand of operator or4b is an unbound "
305 int n_bits
= val_ptr
->n_bits
;
306 if (n_bits
!= other_value
.val_ptr
->n_bits
)
307 TTCN_error("The bitstring operands of operator or4b must have the "
309 if (n_bits
== 0) return *this;
310 BITSTRING
ret_val(n_bits
);
311 int n_bytes
= (n_bits
+ 7) / 8;
312 for (int i
= 0; i
< n_bytes
; i
++)
313 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] |
314 other_value
.val_ptr
->bits_ptr
[i
];
315 ret_val
.clear_unused_bits();
319 BITSTRING
BITSTRING::operator|(const BITSTRING_ELEMENT
& other_value
) const
321 must_bound("Left operand of operator or4b is an unbound bitstring value.");
322 other_value
.must_bound("Right operand of operator or4b is an unbound "
323 "bitstring element.");
324 if (val_ptr
->n_bits
!= 1)
325 TTCN_error("The bitstring operands of "
326 "operator or4b must have the same length.");
327 unsigned char result
= get_bit(0) || other_value
.get_bit() ? 1 : 0;
328 return BITSTRING(1, &result
);
331 BITSTRING
BITSTRING::operator^(const BITSTRING
& other_value
) const
333 must_bound("Left operand of operator xor4b is an unbound bitstring value.");
334 other_value
.must_bound("Right operand of operator xor4b is an unbound "
336 int n_bits
= val_ptr
->n_bits
;
337 if (n_bits
!= other_value
.val_ptr
->n_bits
)
338 TTCN_error("The bitstring operands of operator xor4b must have the "
340 if (n_bits
== 0) return *this;
341 BITSTRING
ret_val(n_bits
);
342 int n_bytes
= (n_bits
+ 7) / 8;
343 for (int i
= 0; i
< n_bytes
; i
++)
344 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] ^
345 other_value
.val_ptr
->bits_ptr
[i
];
346 ret_val
.clear_unused_bits();
350 BITSTRING
BITSTRING::operator^(const BITSTRING_ELEMENT
& other_value
) const
352 must_bound("Left operand of operator xor4b is an unbound bitstring value.");
353 other_value
.must_bound("Right operand of operator xor4b is an unbound "
354 "bitstring element.");
355 if (val_ptr
->n_bits
!= 1)
356 TTCN_error("The bitstring operands of "
357 "operator xor4b must have the same length.");
358 unsigned char result
= get_bit(0) != other_value
.get_bit() ? 1 : 0;
359 return BITSTRING(1, &result
);
362 BITSTRING
BITSTRING::operator<<(int shift_count
) const
364 must_bound("Unbound bitstring operand of shift left operator.");
365 if (shift_count
> 0) {
366 int n_bits
= val_ptr
->n_bits
;
367 if (n_bits
== 0) return *this;
368 BITSTRING
ret_val(n_bits
);
369 int n_bytes
= (n_bits
+ 7) / 8;
371 if (shift_count
> n_bits
) shift_count
= n_bits
;
372 int shift_bytes
= shift_count
/ 8,
373 shift_bits
= shift_count
% 8;
374 if (shift_bits
!= 0) {
376 for ( ; byte_count
< n_bytes
- shift_bytes
- 1; byte_count
++) {
377 ret_val
.val_ptr
->bits_ptr
[byte_count
] =
378 (val_ptr
->bits_ptr
[byte_count
+ shift_bytes
] >> shift_bits
)|
379 (val_ptr
->bits_ptr
[byte_count
+ shift_bytes
+ 1] <<
382 ret_val
.val_ptr
->bits_ptr
[n_bytes
- shift_bytes
- 1] =
383 val_ptr
->bits_ptr
[n_bytes
- 1] >> shift_bits
;
385 memcpy(ret_val
.val_ptr
->bits_ptr
, val_ptr
->bits_ptr
+ shift_bytes
,
386 n_bytes
- shift_bytes
);
388 memset(ret_val
.val_ptr
->bits_ptr
+ n_bytes
- shift_bytes
, 0,
390 ret_val
.clear_unused_bits();
392 } else if (shift_count
== 0) return *this;
393 else return *this >> (-shift_count
);
396 BITSTRING
BITSTRING::operator<<(const INTEGER
& shift_count
) const
398 shift_count
.must_bound("Unbound right operand of bitstring shift left "
400 return *this << (int)shift_count
;
403 BITSTRING
BITSTRING::operator>>(int shift_count
) const
405 must_bound("Unbound bitstring operand of shift right operator.");
406 if (shift_count
> 0) {
407 int n_bits
= val_ptr
->n_bits
;
408 if (n_bits
== 0) return *this;
409 BITSTRING
ret_val(n_bits
);
410 int n_bytes
= (n_bits
+ 7) / 8;
412 if (shift_count
> n_bits
) shift_count
= n_bits
;
413 int shift_bytes
= shift_count
/ 8, shift_bits
= shift_count
% 8;
414 memset(ret_val
.val_ptr
->bits_ptr
, 0, shift_bytes
);
415 if (shift_bits
!= 0) {
416 ret_val
.val_ptr
->bits_ptr
[shift_bytes
] =
417 val_ptr
->bits_ptr
[0] << shift_bits
;
418 for (int byte_count
= shift_bytes
+ 1; byte_count
< n_bytes
; byte_count
++)
420 ret_val
.val_ptr
->bits_ptr
[byte_count
] =
421 (val_ptr
->bits_ptr
[byte_count
- shift_bytes
- 1] >> (8 - shift_bits
))
422 | (val_ptr
->bits_ptr
[byte_count
- shift_bytes
] << shift_bits
);
425 memcpy(ret_val
.val_ptr
->bits_ptr
+ shift_bytes
, val_ptr
->bits_ptr
,
426 n_bytes
- shift_bytes
);
428 ret_val
.clear_unused_bits();
430 } else if (shift_count
== 0) return *this;
431 else return *this << (-shift_count
);
434 BITSTRING
BITSTRING::operator>>(const INTEGER
& shift_count
) const
436 shift_count
.must_bound("Unbound right operand of bitstring shift right "
438 return *this >> (int)shift_count
;
441 BITSTRING
BITSTRING::operator<<=(int rotate_count
) const
443 must_bound("Unbound bistring operand of rotate left operator.");
444 int n_bits
= val_ptr
->n_bits
;
445 if (n_bits
== 0) return *this;
446 if (rotate_count
>= 0) {
447 rotate_count
%= n_bits
;
448 if (rotate_count
== 0) return *this;
449 else return (*this << rotate_count
) |
450 (*this >> (n_bits
- rotate_count
));
451 } else return *this >>= (-rotate_count
);
454 BITSTRING
BITSTRING::operator<<=(const INTEGER
& rotate_count
) const
456 rotate_count
.must_bound("Unbound right operand of bitstring rotate left "
458 return *this <<= (int)rotate_count
;
461 BITSTRING
BITSTRING::operator>>=(int rotate_count
) const
463 must_bound("Unbound bistring operand of rotate right operator.");
464 int n_bits
= val_ptr
->n_bits
;
465 if (n_bits
== 0) return *this;
466 if (rotate_count
>= 0) {
467 rotate_count
%= n_bits
;
468 if (rotate_count
== 0) return *this;
469 else return (*this >> rotate_count
) |
470 (*this << (n_bits
- rotate_count
));
471 } else return *this <<= (-rotate_count
);
474 BITSTRING
BITSTRING::operator>>=(const INTEGER
& rotate_count
) const
476 rotate_count
.must_bound("Unbound right operand of bitstring rotate right "
478 return *this >>= (int)rotate_count
;
481 BITSTRING_ELEMENT
BITSTRING::operator[](int index_value
)
483 if (val_ptr
== NULL
&& index_value
== 0) {
486 return BITSTRING_ELEMENT(FALSE
, *this, 0);
488 must_bound("Accessing an element of an unbound bitstring value.");
489 if (index_value
< 0) TTCN_error("Accessing an bitstring element using "
490 "a negative index (%d).", index_value
);
491 int n_bits
= val_ptr
->n_bits
;
492 if (index_value
> n_bits
) TTCN_error("Index overflow when accessing a "
493 "bitstring element: The index is %d, but the string has only %d bits.",
494 index_value
, n_bits
);
495 if (index_value
== n_bits
) {
496 if (val_ptr
->ref_count
== 1) {
497 if (n_bits
% 8 == 0) val_ptr
= (bitstring_struct
*)
498 Realloc(val_ptr
, MEMORY_SIZE(n_bits
+ 1));
501 bitstring_struct
*old_ptr
= val_ptr
;
502 old_ptr
->ref_count
--;
503 init_struct(n_bits
+ 1);
504 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (n_bits
+ 7) / 8);
507 return BITSTRING_ELEMENT(FALSE
, *this, index_value
);
508 } else return BITSTRING_ELEMENT(TRUE
, *this, index_value
);
512 BITSTRING_ELEMENT
BITSTRING::operator[](const INTEGER
& index_value
)
514 index_value
.must_bound("Indexing a bitstring value with an unbound integer "
516 return (*this)[(int)index_value
];
519 const BITSTRING_ELEMENT
BITSTRING::operator[](int index_value
) const
521 must_bound("Accessing an element of an unbound bitstring value.");
522 if (index_value
< 0) TTCN_error("Accessing an bitstring element using a "
523 "negative index (%d).", index_value
);
524 if (index_value
>= val_ptr
->n_bits
) TTCN_error("Index overflow when "
525 "accessing a bitstring element: The index is %d, but the string has only "
526 "%d bits.", index_value
, val_ptr
->n_bits
);
527 return BITSTRING_ELEMENT(TRUE
, const_cast<BITSTRING
&>(*this), index_value
);
530 const BITSTRING_ELEMENT
BITSTRING::operator[](const INTEGER
& index_value
) const
532 index_value
.must_bound("Indexing a bitstring value with an unbound integer "
534 return (*this)[(int)index_value
];
537 int BITSTRING::lengthof() const
539 must_bound("Getting the length of an unbound bitstring value.");
540 return val_ptr
->n_bits
;
543 BITSTRING::operator const unsigned char*() const
545 must_bound("Casting an unbound bitstring value to const unsigned char*.");
546 return val_ptr
->bits_ptr
;
549 void BITSTRING::log() const
551 if (val_ptr
!= NULL
) {
552 TTCN_Logger::log_char('\'');
553 for (int bit_count
= 0; bit_count
< val_ptr
->n_bits
; bit_count
++)
554 TTCN_Logger::log_char(get_bit(bit_count
) ? '1' : '0');
555 TTCN_Logger::log_event_str("'B");
556 } else TTCN_Logger::log_event_unbound();
559 void BITSTRING::set_param(Module_Param
& param
) {
560 param
.basic_check(Module_Param::BC_VALUE
|Module_Param::BC_LIST
, "bitstring value");
561 Module_Param_Ptr mp
= ¶m
;
562 if (param
.get_type() == Module_Param::MP_Reference
) {
563 mp
= param
.get_referenced_param();
565 switch (mp
->get_type()) {
566 case Module_Param::MP_Bitstring
:
567 switch (param
.get_operation_type()) {
568 case Module_Param::OT_ASSIGN
:
570 init_struct(mp
->get_string_size());
571 memcpy(val_ptr
->bits_ptr
, mp
->get_string_data(), (val_ptr
->n_bits
+ 7) / 8);
574 case Module_Param::OT_CONCAT
:
576 *this = *this + BITSTRING(mp
->get_string_size(), (unsigned char*)mp
->get_string_data());
578 *this = BITSTRING(mp
->get_string_size(), (unsigned char*)mp
->get_string_data());
582 TTCN_error("Internal error: BITSTRING::set_param()");
585 case Module_Param::MP_Expression
:
586 if (mp
->get_expr_type() == Module_Param::EXPR_CONCATENATE
) {
587 BITSTRING operand1
, operand2
;
588 operand1
.set_param(*mp
->get_operand1());
589 operand2
.set_param(*mp
->get_operand2());
590 if (param
.get_operation_type() == Module_Param::OT_CONCAT
) {
591 *this = *this + operand1
+ operand2
;
594 *this = operand1
+ operand2
;
598 param
.expr_type_error("a bitstring");
602 param
.type_error("bitstring value");
607 Module_Param
* BITSTRING::get_param(Module_Param_Name
& /* param_name */) const
610 return new Module_Param_Unbound();
612 int n_bytes
= (val_ptr
->n_bits
+ 7) / 8;
613 unsigned char* val_cpy
= (unsigned char *)Malloc(n_bytes
);
614 memcpy(val_cpy
, val_ptr
->bits_ptr
, n_bytes
);
615 return new Module_Param_Bitstring(val_ptr
->n_bits
, val_cpy
);
618 void BITSTRING::encode_text(Text_Buf
& text_buf
) const
620 must_bound("Text encoder: Encoding an unbound bitstring value.");
621 int n_bits
= val_ptr
->n_bits
;
622 text_buf
.push_int(n_bits
);
623 if (n_bits
> 0) text_buf
.push_raw((n_bits
+ 7) / 8, val_ptr
->bits_ptr
);
626 void BITSTRING::decode_text(Text_Buf
& text_buf
)
628 int n_bits
= text_buf
.pull_int().get_val();
630 TTCN_error("Text decoder: Invalid length was received for a bitstring.");
634 text_buf
.pull_raw((n_bits
+ 7) / 8, val_ptr
->bits_ptr
);
639 void BITSTRING::encode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
640 TTCN_EncDec::coding_t p_coding
, ...) const
643 va_start(pvar
, p_coding
);
645 case TTCN_EncDec::CT_BER
: {
646 TTCN_EncDec_ErrorContext
ec("While BER-encoding type '%s': ", p_td
.name
);
647 unsigned BER_coding
=va_arg(pvar
, unsigned);
648 BER_encode_chk_coding(BER_coding
);
649 ASN_BER_TLV_t
*tlv
=BER_encode_TLV(p_td
, BER_coding
);
650 tlv
->put_in_buffer(p_buf
);
651 ASN_BER_TLV_t::destruct(tlv
);
653 case TTCN_EncDec::CT_RAW
: {
654 TTCN_EncDec_ErrorContext
ec("While RAW-encoding type '%s': ", p_td
.name
);
656 TTCN_EncDec_ErrorContext::error_internal
657 ("No RAW descriptor available for type '%s'.", p_td
.name
);
661 RAW_enc_tree
root(true,NULL
,&rp
,1,p_td
.raw
);
662 RAW_encode(p_td
, root
);
663 root
.put_to_buf(p_buf
);
665 case TTCN_EncDec::CT_XER
: {
666 TTCN_EncDec_ErrorContext
ec("While XER-encoding type '%s': ", p_td
.name
);
667 unsigned XER_coding
=va_arg(pvar
, unsigned);
668 XER_encode(*p_td
.xer
, p_buf
, XER_coding
, 0, 0);
670 case TTCN_EncDec::CT_JSON
: {
671 TTCN_EncDec_ErrorContext
ec("While JSON-encoding type '%s': ", p_td
.name
);
673 TTCN_EncDec_ErrorContext::error_internal
674 ("No JSON descriptor available for type '%s'.", p_td
.name
);
675 JSON_Tokenizer
tok(va_arg(pvar
, int) != 0);
676 JSON_encode(p_td
, tok
);
677 p_buf
.put_s(tok
.get_buffer_length(), (const unsigned char*)tok
.get_buffer());
680 TTCN_error("Unknown coding method requested to encode type '%s'",
686 void BITSTRING::decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
687 TTCN_EncDec::coding_t p_coding
, ...)
690 va_start(pvar
, p_coding
);
692 case TTCN_EncDec::CT_BER
: {
693 TTCN_EncDec_ErrorContext
ec("While BER-decoding type '%s': ", p_td
.name
);
694 unsigned L_form
=va_arg(pvar
, unsigned);
696 BER_decode_str2TLV(p_buf
, tlv
, L_form
);
697 BER_decode_TLV(p_td
, tlv
, L_form
);
698 if(tlv
.isComplete
) p_buf
.increase_pos(tlv
.get_len());
700 case TTCN_EncDec::CT_RAW
: {
701 TTCN_EncDec_ErrorContext
ec("While RAW-decoding type '%s': ", p_td
.name
);
703 TTCN_EncDec_ErrorContext::error_internal
704 ("No RAW descriptor available for type '%s'.", p_td
.name
);
706 switch(p_td
.raw
->top_bit_order
){
714 if(RAW_decode(p_td
, p_buf
, p_buf
.get_len()*8, order
)<0)
715 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
716 "Can not decode type '%s', because invalid or incomplete"
717 " message was received"
720 case TTCN_EncDec::CT_XER
: {
721 TTCN_EncDec_ErrorContext
ec("While XER-decoding type '%s': ", p_td
.name
);
722 unsigned XER_coding
=va_arg(pvar
, unsigned);
723 XmlReaderWrap
reader(p_buf
);
724 int success
= reader
.Read();
725 for (; success
==1; success
=reader
.Read()) {
726 int type
= reader
.NodeType();
727 if (type
==XML_READER_TYPE_ELEMENT
)
730 XER_decode(*p_td
.xer
, reader
, XER_coding
, XER_NONE
, 0);
731 size_t bytes
= reader
.ByteConsumed();
732 p_buf
.set_pos(bytes
);
734 case TTCN_EncDec::CT_JSON
: {
735 TTCN_EncDec_ErrorContext
ec("While JSON-decoding type '%s': ", p_td
.name
);
737 TTCN_EncDec_ErrorContext::error_internal
738 ("No JSON descriptor available for type '%s'.", p_td
.name
);
739 JSON_Tokenizer
tok((const char*)p_buf
.get_data(), p_buf
.get_len());
740 if(JSON_decode(p_td
, tok
, false)<0)
741 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
742 "Can not decode type '%s', because invalid or incomplete"
743 " message was received"
745 p_buf
.set_pos(tok
.get_buf_pos());
748 TTCN_error("Unknown coding method requested to decode type '%s'",
754 void BITSTRING::BER_encode_putbits(unsigned char *target
,
755 unsigned int bitnum_start
,
756 unsigned int bit_count
) const
758 unsigned int nof_bits
, nof_octets
, i
, j
;
761 nof_bits
=val_ptr
->n_bits
;
762 if(bitnum_start
>nof_bits
763 || bitnum_start
+bit_count
>nof_bits
)
764 TTCN_EncDec_ErrorContext::error_internal
765 ("In BITSTRING::BER_encode_putbits(): Index overflow.");
766 nof_octets
=(bit_count
+7)/8;
771 target
[0]=(unsigned char)(nof_octets
*8-bit_count
);
772 for(i
=0; i
<nof_octets
-1; i
++) {
776 if(get_bit(bitnum_start
+8*i
+j
)) c
|=0x01;
784 if(get_bit(bitnum_start
+8*i
+j
)) c
|=0x01;
790 BITSTRING::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
791 unsigned p_coding
) const
794 ASN_BER_TLV_t
*new_tlv
=BER_encode_chk_bound(is_bound());
796 unsigned char *V_ptr
;
798 unsigned int nof_bits
=val_ptr
->n_bits
;
799 unsigned int nof_octets
=(nof_bits
+7)/8;
800 unsigned int nof_fragments
=0;
801 if(p_coding
==BER_ENCODE_CER
) {
802 nof_fragments
=(nof_octets
+998)/999;
803 if(!nof_fragments
) nof_fragments
=1;
805 else /*if(coding==BER_ENCODE_DER)*/ {
809 boolean is_constructed
=nof_fragments
>1;
810 if(!is_constructed
) {
812 V_ptr
=(unsigned char*)Malloc(V_len
);
813 BER_encode_putbits(V_ptr
, 0, nof_bits
);
814 new_tlv
=ASN_BER_TLV_t::construct(V_len
, V_ptr
);
816 else { // is constructed
817 ASN_BER_TLV_t
*tmp_tlv
=NULL
;
818 new_tlv
=ASN_BER_TLV_t::construct(NULL
);
819 unsigned int rest_octets
=nof_octets
-(nof_fragments
-1)*999;
821 V_len=(nof_fragments-1)*1004;
822 V_len+=rest_octets<128?2:4;
823 V_len+=rest_octets+1;
824 V_ptr=(unsigned char*)Malloc(V_len);
827 unsigned int nof_bits_curr
=8*999;
828 for(unsigned int i
=0; i
<nof_fragments
; i
++) {
829 if(i
==nof_fragments
-1) {
831 nof_bits_curr
=nof_bits
-i
*8*999;
833 V_ptr
=(unsigned char*)Malloc(V_len
+1); // because of unused bits-octet
840 BER_encode_putbits(V_ptr
, i
*8*999, nof_bits_curr
);
844 tmp_tlv
=ASN_BER_TLV_t::construct(V_len
+1, V_ptr
);
845 tmp_tlv
=ASN_BER_V2TLV(tmp_tlv
, BITSTRING_descr_
, p_coding
);
846 new_tlv
->add_TLV(tmp_tlv
);
850 if(rest_octets<128) {
851 V_ptr[1]=(rest_octets+1) & '\x7F';
856 V_ptr[2]=((rest_octets+1)/256) & 0xFF;
857 V_ptr[3]=(rest_octets+1) & 0xFF;
860 BER_encode_putbits(V_ptr, i*8*999, nof_bits-i*8*999);
864 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
868 void BITSTRING::BER_decode_getbits(const unsigned char *source
,
869 size_t s_len
, unsigned int& bitnum_start
)
874 TTCN_EncDec_ErrorContext::error
875 (TTCN_EncDec::ET_INVAL_MSG
, "Length of V-part of bitstring"
879 unsigned int nof_octets
=s_len
-1;
880 unsigned int nof_restbits
=8-source
[0];
883 TTCN_EncDec_ErrorContext::error
884 (TTCN_EncDec::ET_INVAL_MSG
,
885 "If the bitstring is empty,"
886 " the initial octet shall be 0, not %u [see X.690 clause 8.6.2.3].",
891 TTCN_EncDec_ErrorContext::error
892 (TTCN_EncDec::ET_INVAL_MSG
, "The number of unused bits in bitstring"
893 " cannot be %u (should be less than 8) [see X.690 clause 8.6.2.2].",
897 // And what about overflow? :)
898 i
= (nof_octets
- 1) * 8 + nof_restbits
;
900 if (val_ptr
->ref_count
> 1) {
901 bitstring_struct
*old_ptr
= val_ptr
;
902 old_ptr
->ref_count
--;
903 init_struct(bitnum_start
+ i
);
904 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (old_ptr
->n_bits
+ 7) / 8);
906 if ((bitnum_start
+ i
+ 7) / 8 > ((unsigned int)val_ptr
->n_bits
+ 7) / 8)
907 val_ptr
= (bitstring_struct
*)Realloc(val_ptr
,
908 MEMORY_SIZE(bitnum_start
+ i
));
909 val_ptr
->n_bits
= bitnum_start
+ i
;
912 for(i
=0; i
<nof_octets
-1; i
++) {
915 set_bit(bitnum_start
+8*i
+j
, c
& 0x80?TRUE
:FALSE
);
920 for(j
=0; j
<nof_restbits
; j
++) {
921 set_bit(bitnum_start
+8*i
+j
, c
& 0x80?TRUE
:FALSE
);
924 bitnum_start
+=(nof_octets
-1)*8+nof_restbits
;
927 void BITSTRING::BER_decode_TLV_(const ASN_BER_TLV_t
& p_tlv
, unsigned L_form
,
928 unsigned int& bitnum_start
)
930 if(!p_tlv
.isConstructed
) {
931 if (p_tlv
.isComplete
|| p_tlv
.V
.str
.Vlen
> 0)
932 BER_decode_getbits(p_tlv
.V
.str
.Vstr
, p_tlv
.V
.str
.Vlen
, bitnum_start
);
934 else { // is constructed
939 if(!ASN_BER_str2TLV(p_tlv
.V
.str
.Vlen
-V_pos
, p_tlv
.V
.str
.Vstr
+V_pos
,
941 TTCN_EncDec_ErrorContext::error
942 (TTCN_EncDec::ET_INCOMPL_MSG
,
943 "Incomplete TLV in a constructed BITSTRING TLV.");
946 if(!p_tlv
.isLenDefinite
&& tlv2
.tagnumber
==0
947 && tlv2
.tagclass
==ASN_TAG_UNIV
)
948 doit
=FALSE
; // End-of-contents
950 ASN_BER_TLV_t stripped_tlv
;
951 BER_decode_strip_tags(BITSTRING_ber_
, tlv2
, L_form
, stripped_tlv
);
952 BER_decode_TLV_(tlv2
, L_form
, bitnum_start
);
953 V_pos
+=tlv2
.get_len();
954 if(V_pos
>=p_tlv
.V
.str
.Vlen
) doit
=FALSE
;
957 } // else / is constructed
960 boolean
BITSTRING::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
961 const ASN_BER_TLV_t
& p_tlv
,
966 ASN_BER_TLV_t stripped_tlv
;
967 BER_decode_strip_tags(*p_td
.ber
, p_tlv
, L_form
, stripped_tlv
);
968 TTCN_EncDec_ErrorContext
ec("While decoding BITSTRING type: ");
970 unsigned int bitnum_start
= 0;
971 BER_decode_TLV_(stripped_tlv
, L_form
, bitnum_start
);
975 int BITSTRING::RAW_encode(const TTCN_Typedescriptor_t
& p_td
, RAW_enc_tree
& myleaf
) const
978 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
979 "Encoding an unbound value.");
981 int bl
= val_ptr
->n_bits
;
982 int align_length
= p_td
.raw
->fieldlength
? p_td
.raw
->fieldlength
- bl
: 0;
983 if ((bl
+ align_length
) < val_ptr
->n_bits
) {
984 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
985 "There is no sufficient bits to encode '%s': ", p_td
.name
);
986 bl
= p_td
.raw
->fieldlength
;
989 // myleaf.ext_bit=EXT_BIT_NO;
990 if (myleaf
.must_free
) Free(myleaf
.body
.leaf
.data_ptr
);
991 myleaf
.must_free
= false;
992 myleaf
.data_ptr_used
= true;
993 myleaf
.body
.leaf
.data_ptr
= val_ptr
->bits_ptr
;
995 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= true;
996 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
997 myleaf
.coding_par
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
999 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= true;
1000 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
1001 myleaf
.coding_par
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
1003 if (p_td
.raw
->endianness
== ORDER_MSB
) myleaf
.align
= align_length
;
1004 else myleaf
.align
= -align_length
;
1006 return myleaf
.length
= bl
+ align_length
;
1009 int BITSTRING::RAW_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
1010 int limit
, raw_order_t top_bit_ord
, boolean no_err
, int /*sel_field*/,
1011 boolean
/*first_call*/)
1013 int prepaddlength
= buff
.increase_pos_padd(p_td
.raw
->prepadding
);
1014 limit
-= prepaddlength
;
1015 int decode_length
= p_td
.raw
->fieldlength
== 0
1016 ? limit
: p_td
.raw
->fieldlength
;
1017 if ( p_td
.raw
->fieldlength
> limit
1018 || p_td
.raw
->fieldlength
> (int) buff
.unread_len_bit()) {
1019 if (no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
1020 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
1021 "There is not enough bits in the buffer to decode type %s.", p_td
.name
);
1022 decode_length
= limit
> (int) buff
.unread_len_bit()
1023 ? buff
.unread_len_bit() : limit
;
1026 init_struct(decode_length
);
1028 bool orders
= false;
1029 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= true;
1030 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
1031 cp
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
1033 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= true;
1034 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
1035 cp
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
1036 cp
.fieldorder
= p_td
.raw
->fieldorder
;
1037 cp
.hexorder
= ORDER_LSB
;
1038 buff
.get_b((size_t) decode_length
, val_ptr
->bits_ptr
, cp
, top_bit_ord
);
1039 if (p_td
.raw
->length_restrition
!= -1) {
1040 val_ptr
->n_bits
= p_td
.raw
->length_restrition
;
1041 if (p_td
.raw
->endianness
== ORDER_LSB
) {
1042 if ((decode_length
- val_ptr
->n_bits
) % 8) {
1043 int bound
= (decode_length
- val_ptr
->n_bits
) % 8;
1044 int maxindex
= (decode_length
- 1) / 8;
1045 for (int a
= 0, b
= (decode_length
- val_ptr
->n_bits
- 1) / 8;
1046 a
< (val_ptr
->n_bits
+ 7) / 8; a
++, b
++) {
1047 val_ptr
->bits_ptr
[a
] = val_ptr
->bits_ptr
[b
] >> bound
;
1049 val_ptr
->bits_ptr
[a
] = val_ptr
->bits_ptr
[b
+ 1] << (8 - bound
);
1053 else memmove(val_ptr
->bits_ptr
,
1054 val_ptr
->bits_ptr
+ (decode_length
- val_ptr
->n_bits
) / 8,
1055 val_ptr
->n_bits
/ 8 * sizeof(unsigned char));
1058 decode_length
+= buff
.increase_pos_padd(p_td
.raw
->padding
);
1059 clear_unused_bits();
1060 return decode_length
+ prepaddlength
;
1063 int BITSTRING::XER_encode(const XERdescriptor_t
& p_td
,
1064 TTCN_Buffer
& p_buf
, unsigned int flavor
, int indent
, embed_values_enc_struct_t
*) const
1067 TTCN_EncDec_ErrorContext::error
1068 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound bitstring value.");
1071 int encoded_length
=(int)p_buf
.get_len();
1072 int empty_element
= val_ptr
==NULL
|| val_ptr
->n_bits
== 0;
1073 flavor
|= SIMPLE_TYPE
;
1074 flavor
&= ~XER_RECOF
; // bitstring doesn't care
1076 begin_xml(p_td
, p_buf
, flavor
, indent
, empty_element
);
1078 if (!empty_element
) {
1079 for (int bit_count
= 0; bit_count
< val_ptr
->n_bits
; bit_count
++)
1080 p_buf
.put_c(get_bit(bit_count
) ? '1' : '0');
1083 end_xml(p_td
, p_buf
, flavor
, indent
, empty_element
);
1084 return (int)p_buf
.get_len() - encoded_length
;
1087 int BITSTRING::XER_decode(const XERdescriptor_t
& p_td
, XmlReaderWrap
& reader
,
1088 unsigned int flavor
, unsigned int /*flavor2*/, embed_values_dec_struct_t
*)
1090 int exer
= is_exer(flavor
);
1091 int success
= reader
.Ok(), depth
= -1, type
;
1092 boolean own_tag
= !is_exerlist(flavor
) && !(exer
&& (p_td
.xer_bits
& UNTAGGED
));
1094 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
1095 const char * name
= verify_name(reader
, p_td
, exer
);
1099 for (; success
== 1; success
= reader
.Read()) {
1100 type
= reader
.NodeType();
1101 if (XML_READER_TYPE_ELEMENT
== type
) {
1102 verify_name(reader
, p_td
, exer
);
1103 depth
= reader
.Depth();
1104 if (reader
.IsEmptyElement()) {
1110 else if (XML_READER_TYPE_TEXT
== type
&& depth
!= -1) break;
1111 else if (XML_READER_TYPE_END_ELEMENT
== type
) {
1112 // End tag without intervening #text == empty content
1113 verify_end(reader
, p_td
, depth
, exer
);
1121 type
= reader
.NodeType();
1122 if (success
== 1 && (XML_READER_TYPE_TEXT
== type
|| XML_READER_TYPE_ATTRIBUTE
== type
)) {
1123 const char* value
= (const char *)reader
.Value();
1124 size_t num_bits
= strlen(value
);
1125 init_struct(num_bits
);
1126 for (size_t i
= 0; i
< num_bits
; ++i
) {
1127 if (value
[i
] < '0' || value
[i
] > '1') {
1128 if (exer
&& (flavor
& EXIT_ON_ERROR
)) {
1132 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG
,
1133 "The bitstring value may only contain ones and zeros.");
1136 set_bit(i
, value
[i
] - '0');
1140 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
1141 // Let the caller do reader.AdvanceAttribute();
1144 for (success
= reader
.Read(); success
== 1; success
= reader
.Read()) {
1145 type
= reader
.NodeType();
1146 if (XML_READER_TYPE_END_ELEMENT
== type
) {
1147 verify_end(reader
, p_td
, depth
, exer
);
1148 reader
.Read(); // one last time
1156 int BITSTRING::JSON_encode(const TTCN_Typedescriptor_t
&, JSON_Tokenizer
& p_tok
) const
1159 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
1160 "Encoding an unbound bitstring value.");
1164 char* tmp_str
= (char*)Malloc(val_ptr
->n_bits
+ 3);
1166 tmp_str
[val_ptr
->n_bits
+ 1] = '\"';
1167 for (int i
= 0; i
< val_ptr
->n_bits
; ++i
) {
1168 tmp_str
[i
+ 1] = get_bit(i
) ? '1' : '0';
1170 tmp_str
[val_ptr
->n_bits
+ 2] = 0;
1171 int enc_len
= p_tok
.put_next_token(JSON_TOKEN_STRING
, tmp_str
);
1176 int BITSTRING::JSON_decode(const TTCN_Typedescriptor_t
& p_td
, JSON_Tokenizer
& p_tok
, boolean p_silent
)
1178 json_token_t token
= JSON_TOKEN_NONE
;
1180 size_t value_len
= 0;
1181 boolean error
= false;
1183 boolean use_default
= p_td
.json
->default_value
&& 0 == p_tok
.get_buffer_length();
1185 // No JSON data in the buffer -> use default value
1186 value
= (char*)p_td
.json
->default_value
;
1187 value_len
= strlen(value
);
1189 dec_len
= p_tok
.get_next_token(&token
, &value
, &value_len
);
1191 if (JSON_TOKEN_ERROR
== token
) {
1192 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_BAD_TOKEN_ERROR
, "");
1193 return JSON_ERROR_FATAL
;
1195 else if (JSON_TOKEN_STRING
== token
|| use_default
) {
1196 if (use_default
|| (value_len
> 2 && value
[0] == '\"' && value
[value_len
- 1] == '\"')) {
1198 // The default value doesn't have quotes around it
1202 init_struct(value_len
);
1203 for (size_t i
= 0; i
< value_len
; ++i
) {
1204 if ('0' <= value
[i
] && '1' >= value
[i
]) {
1205 set_bit(i
, value
[i
] - '0');
1215 return JSON_ERROR_INVALID_TOKEN
;
1219 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_FORMAT_ERROR
, "string", "bitstring");
1223 return JSON_ERROR_FATAL
;
1229 // bitstring element class
1231 BITSTRING_ELEMENT::BITSTRING_ELEMENT(boolean par_bound_flag
,
1232 BITSTRING
& par_str_val
, int par_bit_pos
)
1233 : bound_flag(par_bound_flag
), str_val(par_str_val
), bit_pos(par_bit_pos
)
1237 BITSTRING_ELEMENT
& BITSTRING_ELEMENT::operator=(const BITSTRING
& other_value
)
1239 other_value
.must_bound("Assignment of an unbound bitstring value.");
1240 if(other_value
.val_ptr
->n_bits
!= 1)
1241 TTCN_error("Assignment of a bitstring "
1242 "value with length other than 1 to a bitstring element.");
1244 str_val
.copy_value();
1245 str_val
.set_bit(bit_pos
, other_value
.get_bit(0));
1249 BITSTRING_ELEMENT
& BITSTRING_ELEMENT::operator=
1250 (const BITSTRING_ELEMENT
& other_value
)
1252 other_value
.must_bound("Assignment of an unbound bitstring element.");
1254 str_val
.copy_value();
1255 str_val
.set_bit(bit_pos
, other_value
.str_val
.get_bit(other_value
.bit_pos
));
1259 boolean
BITSTRING_ELEMENT::operator==(const BITSTRING
& other_value
) const
1261 must_bound("Unbound left operand of bitstring element comparison.");
1262 other_value
.must_bound("Unbound right operand of bitstring comparison.");
1263 if(other_value
.val_ptr
->n_bits
!= 1) return FALSE
;
1264 return str_val
.get_bit(bit_pos
) == other_value
.get_bit(0);
1267 boolean
BITSTRING_ELEMENT::operator==
1268 (const BITSTRING_ELEMENT
& other_value
) const
1270 must_bound("Unbound left operand of bitstring element comparison.");
1271 other_value
.must_bound("Unbound right operand of bitstring element "
1273 return str_val
.get_bit(bit_pos
) ==
1274 other_value
.str_val
.get_bit(other_value
.bit_pos
);
1277 BITSTRING
BITSTRING_ELEMENT::operator+(const BITSTRING
& other_value
) const
1279 must_bound("Unbound left operand of bitstring element concatenation.");
1280 other_value
.must_bound("Unbound right operand of bitstring concatenation.");
1281 int n_bits
= other_value
.val_ptr
->n_bits
;
1282 BITSTRING
ret_val(n_bits
+ 1);
1283 ret_val
.val_ptr
->bits_ptr
[0] = str_val
.get_bit(bit_pos
) ? 1 : 0;
1284 int n_bytes
= (n_bits
+ 7) / 8;
1285 for (int byte_count
= 0; byte_count
< n_bytes
; byte_count
++) {
1286 ret_val
.val_ptr
->bits_ptr
[byte_count
] |=
1287 other_value
.val_ptr
->bits_ptr
[byte_count
] << 1;
1288 if (n_bits
> byte_count
* 8 + 7)
1289 ret_val
.val_ptr
->bits_ptr
[byte_count
+ 1] =
1290 other_value
.val_ptr
->bits_ptr
[byte_count
] >> 7;
1292 ret_val
.clear_unused_bits();
1296 BITSTRING
BITSTRING_ELEMENT::operator+(const BITSTRING_ELEMENT
& other_value
)
1299 must_bound("Unbound left operand of bitstring element concatenation.");
1300 other_value
.must_bound("Unbound right operand of bitstring element "
1302 unsigned char result
= str_val
.get_bit(bit_pos
) ? 1 : 0;
1303 if (other_value
.str_val
.get_bit(other_value
.bit_pos
)) result
|= 2;
1304 return BITSTRING(2, &result
);
1307 BITSTRING
BITSTRING_ELEMENT::operator~() const
1309 must_bound("Unbound bitstring element operand of operator not4b.");
1310 unsigned char result
= str_val
.get_bit(bit_pos
) ? 0 : 1;
1311 return BITSTRING(1, &result
);
1314 BITSTRING
BITSTRING_ELEMENT::operator&(const BITSTRING
& other_value
) const
1316 must_bound("Left operand of operator and4b is an unbound bitstring element.");
1317 other_value
.must_bound("Right operand of operator and4b is an unbound "
1318 "bitstring value.");
1319 if (other_value
.val_ptr
->n_bits
!= 1)
1320 TTCN_error("The bitstring operands "
1321 "of operator and4b must have the same length.");
1322 unsigned char result
= str_val
.get_bit(bit_pos
) && other_value
.get_bit(0) ?
1324 return BITSTRING(1, &result
);
1327 BITSTRING
BITSTRING_ELEMENT::operator&
1328 (const BITSTRING_ELEMENT
& other_value
) const
1330 must_bound("Left operand of operator and4b is an unbound bitstring element.");
1331 other_value
.must_bound("Right operand of operator and4b is an unbound "
1332 "bitstring element.");
1333 unsigned char result
= str_val
.get_bit(bit_pos
) &&
1334 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1335 return BITSTRING(1, &result
);
1338 BITSTRING
BITSTRING_ELEMENT::operator|(const BITSTRING
& other_value
) const
1340 must_bound("Left operand of operator or4b is an unbound bitstring element.");
1341 other_value
.must_bound("Right operand of operator or4b is an unbound "
1342 "bitstring value.");
1343 if (other_value
.val_ptr
->n_bits
!= 1)
1344 TTCN_error("The bitstring operands "
1345 "of operator or4b must have the same length.");
1346 unsigned char result
= str_val
.get_bit(bit_pos
) || other_value
.get_bit(0) ?
1348 return BITSTRING(1, &result
);
1351 BITSTRING
BITSTRING_ELEMENT::operator|
1352 (const BITSTRING_ELEMENT
& other_value
) const
1354 must_bound("Left operand of operator or4b is an unbound bitstring element.");
1355 other_value
.must_bound("Right operand of operator or4b is an unbound "
1356 "bitstring element.");
1357 unsigned char result
= str_val
.get_bit(bit_pos
) ||
1358 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1359 return BITSTRING(1, &result
);
1362 BITSTRING
BITSTRING_ELEMENT::operator^(const BITSTRING
& other_value
) const
1364 must_bound("Left operand of operator xor4b is an unbound bitstring element.");
1365 other_value
.must_bound("Right operand of operator xor4b is an unbound "
1366 "bitstring value.");
1367 if (other_value
.val_ptr
->n_bits
!= 1)
1368 TTCN_error("The bitstring operands "
1369 "of operator xor4b must have the same length.");
1370 unsigned char result
= str_val
.get_bit(bit_pos
) != other_value
.get_bit(0) ?
1372 return BITSTRING(1, &result
);
1375 BITSTRING
BITSTRING_ELEMENT::operator^
1376 (const BITSTRING_ELEMENT
& other_value
) const
1378 must_bound("Left operand of operator xor4b is an unbound bitstring element.");
1379 other_value
.must_bound("Right operand of operator xor4b is an unbound "
1380 "bitstring element.");
1381 unsigned char result
= str_val
.get_bit(bit_pos
) !=
1382 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1383 return BITSTRING(1, &result
);
1386 boolean
BITSTRING_ELEMENT::get_bit() const
1388 return str_val
.get_bit(bit_pos
);
1391 void BITSTRING_ELEMENT::log() const
1393 if (bound_flag
) TTCN_Logger::log_event("'%c'B", str_val
.get_bit(bit_pos
) ?
1395 else TTCN_Logger::log_event_unbound();
1398 // bitstring template class
1400 void BITSTRING_template::clean_up()
1402 switch (template_selection
) {
1404 case COMPLEMENTED_LIST
:
1405 delete [] value_list
.list_value
;
1407 case STRING_PATTERN
:
1408 if (pattern_value
->ref_count
> 1) pattern_value
->ref_count
--;
1409 else if (pattern_value
->ref_count
== 1) Free(pattern_value
);
1410 else TTCN_error("Internal error: Invalid reference counter in a bitstring "
1416 template_selection
= UNINITIALIZED_TEMPLATE
;
1419 void BITSTRING_template::copy_template(const BITSTRING_template
& other_value
)
1421 switch (other_value
.template_selection
) {
1422 case SPECIFIC_VALUE
:
1423 single_value
= other_value
.single_value
;
1430 case COMPLEMENTED_LIST
:
1431 value_list
.n_values
= other_value
.value_list
.n_values
;
1432 value_list
.list_value
= new BITSTRING_template
[value_list
.n_values
];
1433 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1434 value_list
.list_value
[i
].copy_template(
1435 other_value
.value_list
.list_value
[i
]);
1437 case STRING_PATTERN
:
1438 pattern_value
= other_value
.pattern_value
;
1439 pattern_value
->ref_count
++;
1442 TTCN_error("Copying an uninitialized/unsupported bitstring template.");
1444 set_selection(other_value
);
1448 This is the same algorithm that match_array uses
1449 to match 'record of' types.
1450 The only differences are: how two elements are matched and
1451 how an asterisk or ? is identified in the template
1453 boolean
BITSTRING_template::match_pattern(
1454 const bitstring_pattern_struct
*string_pattern
,
1455 const BITSTRING::bitstring_struct
*string_value
)
1457 if(string_pattern
->n_elements
== 0) return string_value
->n_bits
== 0;
1459 int value_index
= 0;
1460 unsigned int template_index
= 0;
1461 int last_asterisk
= -1;
1462 int last_value_to_asterisk
= -1;
1466 switch(string_pattern
->elements_ptr
[template_index
]) {
1468 if (!(string_value
->bits_ptr
[value_index
/ 8] &
1469 (1 << (value_index
% 8))))
1474 if(last_asterisk
== -1) return FALSE
;
1475 template_index
= last_asterisk
+1;
1476 value_index
= ++last_value_to_asterisk
;
1480 if (string_value
->bits_ptr
[value_index
/ 8] & (1 << (value_index
% 8)))
1485 if(last_asterisk
== -1) return FALSE
;
1486 template_index
= last_asterisk
+1;
1487 value_index
= ++last_value_to_asterisk
;
1491 //we found a ? element, it matches anything
1496 //we found an asterisk
1497 last_asterisk
= template_index
++;
1498 last_value_to_asterisk
= value_index
;
1501 TTCN_error("Internal error: invalid element in bitstring pattern.");
1504 if(value_index
== string_value
->n_bits
1505 && template_index
== string_pattern
->n_elements
)
1508 }else if(template_index
== string_pattern
->n_elements
)
1510 if(string_pattern
->elements_ptr
[template_index
-1] == 3)
1513 } else if (last_asterisk
== -1){
1516 template_index
= last_asterisk
+1;
1517 value_index
= ++last_value_to_asterisk
;
1519 } else if(value_index
== string_value
->n_bits
)
1521 while(template_index
< string_pattern
->n_elements
&&
1522 string_pattern
->elements_ptr
[template_index
] == 3)
1525 return template_index
== string_pattern
->n_elements
;
1530 BITSTRING_template::BITSTRING_template()
1534 BITSTRING_template::BITSTRING_template(template_sel other_value
)
1535 : Restricted_Length_Template(other_value
)
1537 check_single_selection(other_value
);
1540 BITSTRING_template::BITSTRING_template(const BITSTRING
& other_value
)
1541 : Restricted_Length_Template(SPECIFIC_VALUE
), single_value(other_value
)
1545 BITSTRING_template::BITSTRING_template(const BITSTRING_ELEMENT
& other_value
)
1546 : Restricted_Length_Template(SPECIFIC_VALUE
), single_value(other_value
)
1550 BITSTRING_template::~BITSTRING_template()
1555 BITSTRING_template::BITSTRING_template(const OPTIONAL
<BITSTRING
>& other_value
)
1557 switch (other_value
.get_selection()) {
1558 case OPTIONAL_PRESENT
:
1559 set_selection(SPECIFIC_VALUE
);
1560 single_value
= (const BITSTRING
&)other_value
;
1563 set_selection(OMIT_VALUE
);
1566 TTCN_error("Creating a bitstring template from an unbound optional field.");
1570 BITSTRING_template::BITSTRING_template(unsigned int n_elements
,
1571 const unsigned char *pattern_elements
)
1572 : Restricted_Length_Template(STRING_PATTERN
)
1574 pattern_value
= (bitstring_pattern_struct
*)
1575 Malloc(sizeof(bitstring_pattern_struct
) + n_elements
- 1);
1576 pattern_value
->ref_count
= 1;
1577 pattern_value
->n_elements
= n_elements
;
1578 memcpy(pattern_value
->elements_ptr
, pattern_elements
, n_elements
);
1581 BITSTRING_template::BITSTRING_template(const BITSTRING_template
& other_value
)
1582 : Restricted_Length_Template()
1584 copy_template(other_value
);
1587 BITSTRING_template
& BITSTRING_template::operator=(template_sel other_value
)
1589 check_single_selection(other_value
);
1591 set_selection(other_value
);
1595 BITSTRING_template
& BITSTRING_template::operator=(const BITSTRING
& other_value
)
1597 other_value
.must_bound("Assignment of an unbound bitstring value to a "
1600 set_selection(SPECIFIC_VALUE
);
1601 single_value
= other_value
;
1605 BITSTRING_template
& BITSTRING_template::operator=
1606 (const BITSTRING_ELEMENT
& other_value
)
1608 other_value
.must_bound("Assignment of an unbound bitstring element to a "
1611 set_selection(SPECIFIC_VALUE
);
1612 single_value
= other_value
;
1616 BITSTRING_template
& BITSTRING_template::operator=
1617 (const OPTIONAL
<BITSTRING
>& other_value
)
1620 switch (other_value
.get_selection()) {
1621 case OPTIONAL_PRESENT
:
1622 set_selection(SPECIFIC_VALUE
);
1623 single_value
= (const BITSTRING
&)other_value
;
1626 set_selection(OMIT_VALUE
);
1629 TTCN_error("Assignment of an unbound optional field to a bitstring "
1635 BITSTRING_template
& BITSTRING_template::operator=
1636 (const BITSTRING_template
& other_value
)
1638 if (&other_value
!= this) {
1640 copy_template(other_value
);
1645 BITSTRING_ELEMENT
BITSTRING_template::operator[](int index_value
)
1647 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1648 TTCN_error("Accessing a bitstring element of a non-specific bitstring "
1650 return single_value
[index_value
];
1653 BITSTRING_ELEMENT
BITSTRING_template::operator[](const INTEGER
& index_value
)
1655 index_value
.must_bound("Indexing a bitstring template with an unbound "
1657 return (*this)[(int)index_value
];
1660 const BITSTRING_ELEMENT
BITSTRING_template::operator[](int index_value
) const
1662 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1663 TTCN_error("Accessing a bitstring element of a non-specific bitstring "
1665 return single_value
[index_value
];
1668 const BITSTRING_ELEMENT
BITSTRING_template::operator[](const INTEGER
& index_value
) const
1670 index_value
.must_bound("Indexing a bitstring template with an unbound "
1672 return (*this)[(int)index_value
];
1675 boolean
BITSTRING_template::match(const BITSTRING
& other_value
,
1676 boolean
/* legacy */) const
1678 if (!other_value
.is_bound()) return FALSE
;
1679 if (!match_length(other_value
.val_ptr
->n_bits
)) return FALSE
;
1680 switch (template_selection
) {
1681 case SPECIFIC_VALUE
:
1682 return single_value
== other_value
;
1689 case COMPLEMENTED_LIST
:
1690 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1691 if (value_list
.list_value
[i
].match(other_value
))
1692 return template_selection
== VALUE_LIST
;
1693 return template_selection
== COMPLEMENTED_LIST
;
1694 case STRING_PATTERN
:
1695 return match_pattern(pattern_value
, other_value
.val_ptr
);
1697 TTCN_error("Matching an uninitialized/unsupported bitstring template.");
1702 const BITSTRING
& BITSTRING_template::valueof() const
1704 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1705 TTCN_error("Performing a valueof or send operation on a non-specific "
1706 "bitstring template.");
1707 return single_value
;
1710 int BITSTRING_template::lengthof() const
1713 boolean has_any_or_none
;
1715 TTCN_error("Performing lengthof() operation on a bitstring template "
1716 "which has an ifpresent attribute.");
1717 switch (template_selection
)
1719 case SPECIFIC_VALUE
:
1720 min_length
= single_value
.lengthof();
1721 has_any_or_none
= FALSE
;
1724 TTCN_error("Performing lengthof() operation on a bitstring template "
1725 "containing omit value.");
1729 has_any_or_none
= TRUE
; // max. length is infinity
1733 // error if any element does not have length or the lengths differ
1734 if (value_list
.n_values
<1)
1735 TTCN_error("Internal error: "
1736 "Performing lengthof() operation on a bitstring template "
1737 "containing an empty list.");
1738 int item_length
= value_list
.list_value
[0].lengthof();
1739 for (unsigned int i
= 1; i
< value_list
.n_values
; i
++) {
1740 if (value_list
.list_value
[i
].lengthof()!=item_length
)
1741 TTCN_error("Performing lengthof() operation on a bitstring template "
1742 "containing a value list with different lengths.");
1744 min_length
= item_length
;
1745 has_any_or_none
= FALSE
;
1748 case COMPLEMENTED_LIST
:
1749 TTCN_error("Performing lengthof() operation on a bitstring template "
1750 "containing complemented list.");
1751 case STRING_PATTERN
:
1753 has_any_or_none
= FALSE
; // TRUE if * chars in the pattern
1754 for (unsigned int i
= 0; i
< pattern_value
->n_elements
; i
++)
1756 if (pattern_value
->elements_ptr
[i
] < 3) min_length
++; // case of 1, 0, ?
1757 else has_any_or_none
= TRUE
; // case of * character
1761 TTCN_error("Performing lengthof() operation on an "
1762 "uninitialized/unsupported bitstring template.");
1764 return check_section_is_single(min_length
, has_any_or_none
,
1765 "length", "a", "bitstring template");
1768 void BITSTRING_template::set_type(template_sel template_type
,
1769 unsigned int list_length
)
1771 if (template_type
!= VALUE_LIST
&& template_type
!= COMPLEMENTED_LIST
)
1772 TTCN_error("Setting an invalid list type for a bitstring template.");
1774 set_selection(template_type
);
1775 value_list
.n_values
= list_length
;
1776 value_list
.list_value
= new BITSTRING_template
[list_length
];
1779 BITSTRING_template
& BITSTRING_template::list_item(unsigned int list_index
)
1781 if (template_selection
!= VALUE_LIST
&&
1782 template_selection
!= COMPLEMENTED_LIST
)
1783 TTCN_error("Accessing a list element of a non-list bitstring template.");
1784 if (list_index
>= value_list
.n_values
)
1785 TTCN_error("Index overflow in a bitstring value list template.");
1786 return value_list
.list_value
[list_index
];
1789 static const char patterns
[] = { '0', '1', '?', '*' };
1791 void BITSTRING_template::log() const
1793 switch (template_selection
) {
1794 case SPECIFIC_VALUE
:
1797 case COMPLEMENTED_LIST
:
1798 TTCN_Logger::log_event_str("complement ");
1801 TTCN_Logger::log_char('(');
1802 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++) {
1803 if (i
> 0) TTCN_Logger::log_event_str(", ");
1804 value_list
.list_value
[i
].log();
1806 TTCN_Logger::log_char(')');
1808 case STRING_PATTERN
:
1809 TTCN_Logger::log_char('\'');
1810 for (unsigned int i
= 0; i
< pattern_value
->n_elements
; i
++) {
1811 unsigned char pattern
= pattern_value
->elements_ptr
[i
];
1812 if (pattern
< 4) TTCN_Logger::log_char(patterns
[pattern
]);
1813 else TTCN_Logger::log_event_str("<unknown>");
1815 TTCN_Logger::log_event_str("'B");
1825 void BITSTRING_template::log_match(const BITSTRING
& match_value
,
1826 boolean
/* legacy */) const
1828 if (TTCN_Logger::VERBOSITY_COMPACT
== TTCN_Logger::get_matching_verbosity()
1829 && TTCN_Logger::get_logmatch_buffer_len() != 0) {
1830 TTCN_Logger::print_logmatch_buffer();
1831 TTCN_Logger::log_event_str(" := ");
1834 TTCN_Logger::log_event_str(" with ");
1836 if (match(match_value
)) TTCN_Logger::log_event_str(" matched");
1837 else TTCN_Logger::log_event_str(" unmatched");
1840 void BITSTRING_template::set_param(Module_Param
& param
) {
1841 param
.basic_check(Module_Param::BC_TEMPLATE
|Module_Param::BC_LIST
, "bitstring template");
1842 Module_Param_Ptr mp
= ¶m
;
1843 if (param
.get_type() == Module_Param::MP_Reference
) {
1844 mp
= param
.get_referenced_param();
1846 switch (mp
->get_type()) {
1847 case Module_Param::MP_Omit
:
1850 case Module_Param::MP_Any
:
1853 case Module_Param::MP_AnyOrNone
:
1854 *this = ANY_OR_OMIT
;
1856 case Module_Param::MP_List_Template
:
1857 case Module_Param::MP_ComplementList_Template
: {
1858 BITSTRING_template temp
;
1859 temp
.set_type(mp
->get_type() == Module_Param::MP_List_Template
?
1860 VALUE_LIST
: COMPLEMENTED_LIST
, mp
->get_size());
1861 for (size_t i
=0; i
<mp
->get_size(); i
++) {
1862 temp
.list_item(i
).set_param(*mp
->get_elem(i
));
1866 case Module_Param::MP_Bitstring
:
1867 *this = BITSTRING(mp
->get_string_size(), (unsigned char*)mp
->get_string_data());
1869 case Module_Param::MP_Bitstring_Template
:
1870 *this = BITSTRING_template(mp
->get_string_size(), (unsigned char*)mp
->get_string_data());
1872 case Module_Param::MP_Expression
:
1873 if (mp
->get_expr_type() == Module_Param::EXPR_CONCATENATE
) {
1874 BITSTRING operand1
, operand2
;
1875 operand1
.set_param(*mp
->get_operand1());
1876 operand2
.set_param(*mp
->get_operand2());
1877 *this = operand1
+ operand2
;
1880 param
.expr_type_error("a bitstring");
1884 param
.type_error("bitstring template");
1886 is_ifpresent
= param
.get_ifpresent() || mp
->get_ifpresent();
1887 if (param
.get_length_restriction() != NULL
) {
1888 set_length_range(param
);
1891 set_length_range(*mp
);
1895 Module_Param
* BITSTRING_template::get_param(Module_Param_Name
& param_name
) const
1897 Module_Param
* mp
= NULL
;
1898 switch (template_selection
) {
1899 case UNINITIALIZED_TEMPLATE
:
1900 mp
= new Module_Param_Unbound();
1903 mp
= new Module_Param_Omit();
1906 mp
= new Module_Param_Any();
1909 mp
= new Module_Param_AnyOrNone();
1911 case SPECIFIC_VALUE
:
1912 mp
= single_value
.get_param(param_name
);
1915 case COMPLEMENTED_LIST
: {
1916 if (template_selection
== VALUE_LIST
) {
1917 mp
= new Module_Param_List_Template();
1920 mp
= new Module_Param_ComplementList_Template();
1922 for (size_t i
= 0; i
< value_list
.n_values
; ++i
) {
1923 mp
->add_elem(value_list
.list_value
[i
].get_param(param_name
));
1926 case STRING_PATTERN
: {
1927 unsigned char* val_cpy
= (unsigned char*)Malloc(pattern_value
->n_elements
);
1928 memcpy(val_cpy
, pattern_value
->elements_ptr
, pattern_value
->n_elements
);
1929 mp
= new Module_Param_Bitstring_Template(pattern_value
->n_elements
, val_cpy
);
1935 mp
->set_ifpresent();
1937 mp
->set_length_restriction(get_length_range());
1941 void BITSTRING_template::encode_text(Text_Buf
& text_buf
) const
1943 encode_text_restricted(text_buf
);
1944 switch (template_selection
) {
1949 case SPECIFIC_VALUE
:
1950 single_value
.encode_text(text_buf
);
1953 case COMPLEMENTED_LIST
:
1954 text_buf
.push_int(value_list
.n_values
);
1955 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1956 value_list
.list_value
[i
].encode_text(text_buf
);
1958 case STRING_PATTERN
:
1959 text_buf
.push_int(pattern_value
->n_elements
);
1960 text_buf
.push_raw(pattern_value
->n_elements
, pattern_value
->elements_ptr
);
1963 TTCN_error("Text encoder: Encoding an uninitialized/unsupported "
1964 "bitstring template.");
1968 void BITSTRING_template::decode_text(Text_Buf
& text_buf
)
1971 decode_text_restricted(text_buf
);
1972 switch (template_selection
) {
1977 case SPECIFIC_VALUE
:
1978 single_value
.decode_text(text_buf
);
1981 case COMPLEMENTED_LIST
:
1982 value_list
.n_values
= text_buf
.pull_int().get_val();
1983 value_list
.list_value
= new BITSTRING_template
[value_list
.n_values
];
1984 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1985 value_list
.list_value
[i
].decode_text(text_buf
);
1987 case STRING_PATTERN
: {
1988 unsigned int n_elements
= text_buf
.pull_int().get_val();
1989 pattern_value
= (bitstring_pattern_struct
*)
1990 Malloc(sizeof(bitstring_pattern_struct
) + n_elements
- 1);
1991 pattern_value
->ref_count
= 1;
1992 pattern_value
->n_elements
= n_elements
;
1993 text_buf
.pull_raw(n_elements
, pattern_value
->elements_ptr
);
1996 TTCN_error("Text decoder: An unknown/unsupported selection was "
1997 "received for a bitstring template.");
2001 boolean
BITSTRING_template::is_present(boolean legacy
/* = FALSE */) const
2003 if (template_selection
==UNINITIALIZED_TEMPLATE
) return FALSE
;
2004 return !match_omit(legacy
);
2007 boolean
BITSTRING_template::match_omit(boolean legacy
/* = FALSE */) const
2009 if (is_ifpresent
) return TRUE
;
2010 switch (template_selection
) {
2015 case COMPLEMENTED_LIST
:
2017 // legacy behavior: 'omit' can appear in the value/complement list
2018 for (unsigned int i
=0; i
<value_list
.n_values
; i
++)
2019 if (value_list
.list_value
[i
].match_omit())
2020 return template_selection
==VALUE_LIST
;
2021 return template_selection
==COMPLEMENTED_LIST
;
2023 // else fall through
2030 #ifndef TITAN_RUNTIME_2
2031 void BITSTRING_template::check_restriction(template_res t_res
, const char* t_name
,
2032 boolean legacy
/* = FALSE */) const
2034 if (template_selection
==UNINITIALIZED_TEMPLATE
) return;
2035 switch ((t_name
&&(t_res
==TR_VALUE
))?TR_OMIT
:t_res
) {
2037 if (!is_ifpresent
&& template_selection
==SPECIFIC_VALUE
) return;
2040 if (!is_ifpresent
&& (template_selection
==OMIT_VALUE
||
2041 template_selection
==SPECIFIC_VALUE
)) return;
2044 if (!match_omit(legacy
)) return;
2049 TTCN_error("Restriction `%s' on template of type %s violated.",
2050 get_res_name(t_res
), t_name
? t_name
: "bitstring");
2053 int BITSTRING::RAW_encode_negtest_raw(RAW_enc_tree
& p_myleaf
) const
2055 if (p_myleaf
.must_free
)
2056 Free(p_myleaf
.body
.leaf
.data_ptr
);
2057 p_myleaf
.must_free
= false;
2058 p_myleaf
.data_ptr_used
= true;
2059 p_myleaf
.body
.leaf
.data_ptr
= val_ptr
->bits_ptr
;
2060 return p_myleaf
.length
= val_ptr
->n_bits
;