1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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
7 ///////////////////////////////////////////////////////////////////////////////
10 #include "Bitstring.hh"
11 #include "../common/memory.h"
13 #include "String_struct.hh"
14 #include "Parameters.h"
15 #include "Param_Types.hh"
20 #include "../common/dbgnew.hh"
22 // bitstring value class
24 /** The amount of memory needed for a bitstring containing n bits. */
25 #define MEMORY_SIZE(n) (sizeof(bitstring_struct) - sizeof(int) + ((n) + 7) / 8)
27 void BITSTRING::init_struct(int n_bits
)
31 TTCN_error("Initializing a bitstring with a negative length.");
32 } else if (n_bits
== 0) {
33 /** This will represent the empty strings so they won't need allocated
34 * memory, this delays the memory allocation until it is really needed.
36 static bitstring_struct empty_string
= { 1, 0, "" };
37 val_ptr
= &empty_string
;
38 empty_string
.ref_count
++;
40 val_ptr
= (bitstring_struct
*)Malloc(MEMORY_SIZE(n_bits
));
41 val_ptr
->ref_count
= 1;
42 val_ptr
->n_bits
= n_bits
;
46 boolean
BITSTRING::get_bit(int bit_index
) const
48 return val_ptr
->bits_ptr
[bit_index
/ 8] & (1 << (bit_index
% 8));
51 void BITSTRING::set_bit(int bit_index
, boolean new_value
)
53 unsigned char mask
= 1 << (bit_index
% 8);
54 if (new_value
) val_ptr
->bits_ptr
[bit_index
/ 8] |= mask
;
55 else val_ptr
->bits_ptr
[bit_index
/ 8] &= ~mask
;
58 void BITSTRING::copy_value()
60 if (val_ptr
== NULL
|| val_ptr
->n_bits
<= 0)
61 TTCN_error("Internal error: Invalid internal data structure when copying "
62 "the memory area of a bitstring value.");
63 if (val_ptr
->ref_count
> 1) {
64 bitstring_struct
*old_ptr
= val_ptr
;
66 init_struct(old_ptr
->n_bits
);
67 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (old_ptr
->n_bits
+ 7) / 8);
71 void BITSTRING::clear_unused_bits() const
73 int n_bits
= val_ptr
->n_bits
;
74 if (n_bits
% 8 != 0) val_ptr
->bits_ptr
[(n_bits
- 1) / 8] &=
75 (unsigned char)'\377' >> (7 - (n_bits
- 1) % 8);
78 BITSTRING::BITSTRING(int n_bits
)
83 BITSTRING::BITSTRING()
88 BITSTRING::BITSTRING(int n_bits
, const unsigned char *bits_ptr
)
91 memcpy(val_ptr
->bits_ptr
, bits_ptr
, (n_bits
+ 7) / 8);
95 BITSTRING::BITSTRING(const BITSTRING
& other_value
)
96 : Base_Type(other_value
)
98 other_value
.must_bound("Copying an unbound bitstring value.");
99 val_ptr
= other_value
.val_ptr
;
100 val_ptr
->ref_count
++;
103 BITSTRING::BITSTRING(const BITSTRING_ELEMENT
& other_value
)
105 other_value
.must_bound("Copying an unbound bitstring element.");
107 val_ptr
->bits_ptr
[0] = other_value
.get_bit() ? 1 : 0;
110 BITSTRING::~BITSTRING()
115 void BITSTRING::clean_up()
117 if (val_ptr
!= NULL
) {
118 if (val_ptr
->ref_count
> 1) val_ptr
->ref_count
--;
119 else if (val_ptr
->ref_count
== 1) Free(val_ptr
);
120 else TTCN_error("Internal error: Invalid reference counter in a bitstring "
126 BITSTRING
& BITSTRING::operator=(const BITSTRING
& other_value
)
128 other_value
.must_bound("Assignment of an unbound bitstring value.");
129 if (&other_value
!= this) {
131 val_ptr
= other_value
.val_ptr
;
132 val_ptr
->ref_count
++;
137 BITSTRING
& BITSTRING::operator=(const BITSTRING_ELEMENT
& other_value
)
139 other_value
.must_bound("Assignment of an unbound bitstring element to a "
141 boolean bit_value
= other_value
.get_bit();
144 val_ptr
->bits_ptr
[0] = bit_value
? 1 : 0;
148 boolean
BITSTRING::operator==(const BITSTRING
& other_value
) const
150 must_bound("Unbound left operand of bitstring comparison.");
151 other_value
.must_bound("Unbound right operand of bitstring comparison.");
152 int n_bits
= val_ptr
->n_bits
;
153 if (n_bits
!= other_value
.val_ptr
->n_bits
) return FALSE
;
154 if (n_bits
== 0) return TRUE
;
156 other_value
.clear_unused_bits();
157 return !memcmp(val_ptr
->bits_ptr
, other_value
.val_ptr
->bits_ptr
,
161 boolean
BITSTRING::operator==(const BITSTRING_ELEMENT
& other_value
) const
163 must_bound("Unbound left operand of bitstring comparison.");
164 other_value
.must_bound("Unbound right operand of bitstring element "
166 if (val_ptr
->n_bits
!= 1) return FALSE
;
167 return get_bit(0) == other_value
.get_bit();
170 BITSTRING
BITSTRING::operator+(const BITSTRING
& other_value
) const
172 must_bound("Unbound left operand of bitstring concatenation.");
173 other_value
.must_bound("Unbound right operand of bitstring concatenation.");
175 int left_n_bits
= val_ptr
->n_bits
;
176 if (left_n_bits
== 0) return other_value
;
178 int right_n_bits
= other_value
.val_ptr
->n_bits
;
179 if (right_n_bits
== 0) return *this;
181 // the length of result
182 int n_bits
= left_n_bits
+ right_n_bits
;
184 // the number of bytes used
185 int left_n_bytes
= (left_n_bits
+ 7) / 8;
186 int right_n_bytes
= (right_n_bits
+ 7) / 8;
188 // the number of bits used in the last incomplete octet of the left operand
189 int left_empty_bits
= left_n_bits
% 8;
192 BITSTRING
ret_val(n_bits
);
194 // pointers to the data areas
195 const unsigned char *left_ptr
= val_ptr
->bits_ptr
;
196 const unsigned char *right_ptr
= other_value
.val_ptr
->bits_ptr
;
197 unsigned char *dest_ptr
= ret_val
.val_ptr
->bits_ptr
;
199 // copying the left fragment into the result
200 memcpy(dest_ptr
, left_ptr
, left_n_bytes
);
202 if (left_empty_bits
!= 0) {
203 // non-trivial case: the length of left fragment is not a multiply of 8
204 // the bytes used in the result
205 int n_bytes
= (n_bits
+ 7) / 8;
206 // placing the bytes from the right fragment until the result is filled
207 for (int i
= left_n_bytes
; i
< n_bytes
; i
++) {
208 unsigned char right_byte
= right_ptr
[i
- left_n_bytes
];
209 // finish filling the previous byte
210 dest_ptr
[i
- 1] |= right_byte
<< left_empty_bits
;
211 // start filling the actual byte
212 dest_ptr
[i
] = right_byte
>> (8 - left_empty_bits
);
214 if (left_n_bytes
+ right_n_bytes
> n_bytes
) {
215 // if the result data area is shorter than the two operands together
216 // the last bits of right fragment were not placed into the result
217 // in the previous for loop
218 dest_ptr
[n_bytes
- 1] |= right_ptr
[right_n_bytes
- 1] << left_empty_bits
;
221 // trivial case: just append the bytes of the right fragment
222 memcpy(dest_ptr
+ left_n_bytes
, right_ptr
, right_n_bytes
);
224 ret_val
.clear_unused_bits();
228 BITSTRING
BITSTRING::operator+(const BITSTRING_ELEMENT
& other_value
) const
230 must_bound("Unbound left operand of bitstring concatenation.");
231 other_value
.must_bound("Unbound right operand of bitstring element "
234 int n_bits
= val_ptr
->n_bits
;
235 BITSTRING
ret_val(n_bits
+ 1);
236 memcpy(ret_val
.val_ptr
->bits_ptr
, val_ptr
->bits_ptr
, (n_bits
+ 7) / 8);
237 ret_val
.set_bit(n_bits
, other_value
.get_bit());
241 BITSTRING
BITSTRING::operator~() const
243 must_bound("Unbound bitstring operand of operator not4b.");
244 int n_bytes
= (val_ptr
->n_bits
+ 7) / 8;
245 if (n_bytes
== 0) return *this;
246 BITSTRING
ret_val(val_ptr
->n_bits
);
247 for (int i
= 0; i
< n_bytes
; i
++)
248 ret_val
.val_ptr
->bits_ptr
[i
] = ~val_ptr
->bits_ptr
[i
];
249 ret_val
.clear_unused_bits();
253 BITSTRING
BITSTRING::operator&(const BITSTRING
& other_value
) const
255 must_bound("Left operand of operator and4b is an unbound bitstring value.");
256 other_value
.must_bound("Right operand of operator and4b is an unbound "
258 int n_bits
= val_ptr
->n_bits
;
259 if (n_bits
!= other_value
.val_ptr
->n_bits
)
260 TTCN_error("The bitstring operands of operator and4b must have the "
262 if (n_bits
== 0) return *this;
263 BITSTRING
ret_val(n_bits
);
264 int n_bytes
= (n_bits
+ 7) / 8;
265 for (int i
= 0; i
< n_bytes
; i
++)
266 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] &
267 other_value
.val_ptr
->bits_ptr
[i
];
268 ret_val
.clear_unused_bits();
272 BITSTRING
BITSTRING::operator&(const BITSTRING_ELEMENT
& other_value
) const
274 must_bound("Left operand of operator and4b is an unbound bitstring value.");
275 other_value
.must_bound("Right operand of operator and4b is an unbound "
276 "bitstring element.");
277 if (val_ptr
->n_bits
!= 1)
278 TTCN_error("The bitstring operands of "
279 "operator and4b must have the same length.");
280 unsigned char result
= get_bit(0) && other_value
.get_bit() ? 1 : 0;
281 return BITSTRING(1, &result
);
284 BITSTRING
BITSTRING::operator|(const BITSTRING
& other_value
) const
286 must_bound("Left operand of operator or4b is an unbound bitstring value.");
287 other_value
.must_bound("Right operand of operator or4b is an unbound "
289 int n_bits
= val_ptr
->n_bits
;
290 if (n_bits
!= other_value
.val_ptr
->n_bits
)
291 TTCN_error("The bitstring operands of operator or4b must have the "
293 if (n_bits
== 0) return *this;
294 BITSTRING
ret_val(n_bits
);
295 int n_bytes
= (n_bits
+ 7) / 8;
296 for (int i
= 0; i
< n_bytes
; i
++)
297 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] |
298 other_value
.val_ptr
->bits_ptr
[i
];
299 ret_val
.clear_unused_bits();
303 BITSTRING
BITSTRING::operator|(const BITSTRING_ELEMENT
& other_value
) const
305 must_bound("Left operand of operator or4b is an unbound bitstring value.");
306 other_value
.must_bound("Right operand of operator or4b is an unbound "
307 "bitstring element.");
308 if (val_ptr
->n_bits
!= 1)
309 TTCN_error("The bitstring operands of "
310 "operator or4b must have the same length.");
311 unsigned char result
= get_bit(0) || other_value
.get_bit() ? 1 : 0;
312 return BITSTRING(1, &result
);
315 BITSTRING
BITSTRING::operator^(const BITSTRING
& other_value
) const
317 must_bound("Left operand of operator xor4b is an unbound bitstring value.");
318 other_value
.must_bound("Right operand of operator xor4b is an unbound "
320 int n_bits
= val_ptr
->n_bits
;
321 if (n_bits
!= other_value
.val_ptr
->n_bits
)
322 TTCN_error("The bitstring operands of operator xor4b must have the "
324 if (n_bits
== 0) return *this;
325 BITSTRING
ret_val(n_bits
);
326 int n_bytes
= (n_bits
+ 7) / 8;
327 for (int i
= 0; i
< n_bytes
; i
++)
328 ret_val
.val_ptr
->bits_ptr
[i
] = val_ptr
->bits_ptr
[i
] ^
329 other_value
.val_ptr
->bits_ptr
[i
];
330 ret_val
.clear_unused_bits();
334 BITSTRING
BITSTRING::operator^(const BITSTRING_ELEMENT
& other_value
) const
336 must_bound("Left operand of operator xor4b is an unbound bitstring value.");
337 other_value
.must_bound("Right operand of operator xor4b is an unbound "
338 "bitstring element.");
339 if (val_ptr
->n_bits
!= 1)
340 TTCN_error("The bitstring operands of "
341 "operator xor4b must have the same length.");
342 unsigned char result
= get_bit(0) != other_value
.get_bit() ? 1 : 0;
343 return BITSTRING(1, &result
);
346 BITSTRING
BITSTRING::operator<<(int shift_count
) const
348 must_bound("Unbound bitstring operand of shift left operator.");
349 if (shift_count
> 0) {
350 int n_bits
= val_ptr
->n_bits
;
351 if (n_bits
== 0) return *this;
352 BITSTRING
ret_val(n_bits
);
353 int n_bytes
= (n_bits
+ 7) / 8;
355 if (shift_count
> n_bits
) shift_count
= n_bits
;
356 int shift_bytes
= shift_count
/ 8,
357 shift_bits
= shift_count
% 8;
358 if (shift_bits
!= 0) {
360 for ( ; byte_count
< n_bytes
- shift_bytes
- 1; byte_count
++) {
361 ret_val
.val_ptr
->bits_ptr
[byte_count
] =
362 (val_ptr
->bits_ptr
[byte_count
+ shift_bytes
] >> shift_bits
)|
363 (val_ptr
->bits_ptr
[byte_count
+ shift_bytes
+ 1] <<
366 ret_val
.val_ptr
->bits_ptr
[n_bytes
- shift_bytes
- 1] =
367 val_ptr
->bits_ptr
[n_bytes
- 1] >> shift_bits
;
369 memcpy(ret_val
.val_ptr
->bits_ptr
, val_ptr
->bits_ptr
+ shift_bytes
,
370 n_bytes
- shift_bytes
);
372 memset(ret_val
.val_ptr
->bits_ptr
+ n_bytes
- shift_bytes
, 0,
374 ret_val
.clear_unused_bits();
376 } else if (shift_count
== 0) return *this;
377 else return *this >> (-shift_count
);
380 BITSTRING
BITSTRING::operator<<(const INTEGER
& shift_count
) const
382 shift_count
.must_bound("Unbound right operand of bitstring shift left "
384 return *this << (int)shift_count
;
387 BITSTRING
BITSTRING::operator>>(int shift_count
) const
389 must_bound("Unbound bitstring operand of shift right operator.");
390 if (shift_count
> 0) {
391 int n_bits
= val_ptr
->n_bits
;
392 if (n_bits
== 0) return *this;
393 BITSTRING
ret_val(n_bits
);
394 int n_bytes
= (n_bits
+ 7) / 8;
396 if (shift_count
> n_bits
) shift_count
= n_bits
;
397 int shift_bytes
= shift_count
/ 8, shift_bits
= shift_count
% 8;
398 memset(ret_val
.val_ptr
->bits_ptr
, 0, shift_bytes
);
399 if (shift_bits
!= 0) {
400 ret_val
.val_ptr
->bits_ptr
[shift_bytes
] =
401 val_ptr
->bits_ptr
[0] << shift_bits
;
402 for (int byte_count
= shift_bytes
+ 1; byte_count
< n_bytes
; byte_count
++)
404 ret_val
.val_ptr
->bits_ptr
[byte_count
] =
405 (val_ptr
->bits_ptr
[byte_count
- shift_bytes
- 1] >> (8 - shift_bits
))
406 | (val_ptr
->bits_ptr
[byte_count
- shift_bytes
] << shift_bits
);
409 memcpy(ret_val
.val_ptr
->bits_ptr
+ shift_bytes
, val_ptr
->bits_ptr
,
410 n_bytes
- shift_bytes
);
412 ret_val
.clear_unused_bits();
414 } else if (shift_count
== 0) return *this;
415 else return *this << (-shift_count
);
418 BITSTRING
BITSTRING::operator>>(const INTEGER
& shift_count
) const
420 shift_count
.must_bound("Unbound right operand of bitstring shift right "
422 return *this >> (int)shift_count
;
425 BITSTRING
BITSTRING::operator<<=(int rotate_count
) const
427 must_bound("Unbound bistring operand of rotate left operator.");
428 int n_bits
= val_ptr
->n_bits
;
429 if (n_bits
== 0) return *this;
430 if (rotate_count
>= 0) {
431 rotate_count
%= n_bits
;
432 if (rotate_count
== 0) return *this;
433 else return (*this << rotate_count
) |
434 (*this >> (n_bits
- rotate_count
));
435 } else return *this >>= (-rotate_count
);
438 BITSTRING
BITSTRING::operator<<=(const INTEGER
& rotate_count
) const
440 rotate_count
.must_bound("Unbound right operand of bitstring rotate left "
442 return *this <<= (int)rotate_count
;
445 BITSTRING
BITSTRING::operator>>=(int rotate_count
) const
447 must_bound("Unbound bistring operand of rotate right operator.");
448 int n_bits
= val_ptr
->n_bits
;
449 if (n_bits
== 0) return *this;
450 if (rotate_count
>= 0) {
451 rotate_count
%= n_bits
;
452 if (rotate_count
== 0) return *this;
453 else return (*this >> rotate_count
) |
454 (*this << (n_bits
- rotate_count
));
455 } else return *this <<= (-rotate_count
);
458 BITSTRING
BITSTRING::operator>>=(const INTEGER
& rotate_count
) const
460 rotate_count
.must_bound("Unbound right operand of bitstring rotate right "
462 return *this >>= (int)rotate_count
;
465 BITSTRING_ELEMENT
BITSTRING::operator[](int index_value
)
467 if (val_ptr
== NULL
&& index_value
== 0) {
470 return BITSTRING_ELEMENT(FALSE
, *this, 0);
472 must_bound("Accessing an element of an unbound bitstring value.");
473 if (index_value
< 0) TTCN_error("Accessing an bitstring element using "
474 "a negative index (%d).", index_value
);
475 int n_bits
= val_ptr
->n_bits
;
476 if (index_value
> n_bits
) TTCN_error("Index overflow when accessing a "
477 "bitstring element: The index is %d, but the string has only %d bits.",
478 index_value
, n_bits
);
479 if (index_value
== n_bits
) {
480 if (val_ptr
->ref_count
== 1) {
481 if (n_bits
% 8 == 0) val_ptr
= (bitstring_struct
*)
482 Realloc(val_ptr
, MEMORY_SIZE(n_bits
+ 1));
485 bitstring_struct
*old_ptr
= val_ptr
;
486 old_ptr
->ref_count
--;
487 init_struct(n_bits
+ 1);
488 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (n_bits
+ 7) / 8);
491 return BITSTRING_ELEMENT(FALSE
, *this, index_value
);
492 } else return BITSTRING_ELEMENT(TRUE
, *this, index_value
);
496 BITSTRING_ELEMENT
BITSTRING::operator[](const INTEGER
& index_value
)
498 index_value
.must_bound("Indexing a bitstring value with an unbound integer "
500 return (*this)[(int)index_value
];
503 const BITSTRING_ELEMENT
BITSTRING::operator[](int index_value
) const
505 must_bound("Accessing an element of an unbound bitstring value.");
506 if (index_value
< 0) TTCN_error("Accessing an bitstring element using a "
507 "negative index (%d).", index_value
);
508 if (index_value
>= val_ptr
->n_bits
) TTCN_error("Index overflow when "
509 "accessing a bitstring element: The index is %d, but the string has only "
510 "%d bits.", index_value
, val_ptr
->n_bits
);
511 return BITSTRING_ELEMENT(TRUE
, const_cast<BITSTRING
&>(*this), index_value
);
514 const BITSTRING_ELEMENT
BITSTRING::operator[](const INTEGER
& index_value
) const
516 index_value
.must_bound("Indexing a bitstring value with an unbound integer "
518 return (*this)[(int)index_value
];
521 int BITSTRING::lengthof() const
523 must_bound("Getting the length of an unbound bitstring value.");
524 return val_ptr
->n_bits
;
527 BITSTRING::operator const unsigned char*() const
529 must_bound("Casting an unbound bitstring value to const unsigned char*.");
530 return val_ptr
->bits_ptr
;
533 void BITSTRING::log() const
535 if (val_ptr
!= NULL
) {
536 TTCN_Logger::log_char('\'');
537 for (int bit_count
= 0; bit_count
< val_ptr
->n_bits
; bit_count
++)
538 TTCN_Logger::log_char(get_bit(bit_count
) ? '1' : '0');
539 TTCN_Logger::log_event_str("'B");
540 } else TTCN_Logger::log_event_unbound();
543 void BITSTRING::set_param(Module_Param
& param
) {
544 param
.basic_check(Module_Param::BC_VALUE
|Module_Param::BC_LIST
, "bitstring value");
545 if (param
.get_type()!=Module_Param::MP_Bitstring
) param
.type_error("bitstring value");
546 switch (param
.get_operation_type()) {
547 case Module_Param::OT_ASSIGN
:
549 init_struct(param
.get_string_size());
550 memcpy(val_ptr
->bits_ptr
, param
.get_string_data(), (val_ptr
->n_bits
+ 7) / 8);
553 case Module_Param::OT_CONCAT
:
555 *this = *this + BITSTRING(param
.get_string_size(), (unsigned char*)param
.get_string_data());
557 *this = BITSTRING(param
.get_string_size(), (unsigned char*)param
.get_string_data());
561 TTCN_error("Internal error: BITSTRING::set_param()");
565 void BITSTRING::encode_text(Text_Buf
& text_buf
) const
567 must_bound("Text encoder: Encoding an unbound bitstring value.");
568 int n_bits
= val_ptr
->n_bits
;
569 text_buf
.push_int(n_bits
);
570 if (n_bits
> 0) text_buf
.push_raw((n_bits
+ 7) / 8, val_ptr
->bits_ptr
);
573 void BITSTRING::decode_text(Text_Buf
& text_buf
)
575 int n_bits
= text_buf
.pull_int().get_val();
577 TTCN_error("Text decoder: Invalid length was received for a bitstring.");
581 text_buf
.pull_raw((n_bits
+ 7) / 8, val_ptr
->bits_ptr
);
586 void BITSTRING::encode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
587 TTCN_EncDec::coding_t p_coding
, ...) const
590 va_start(pvar
, p_coding
);
592 case TTCN_EncDec::CT_BER
: {
593 TTCN_EncDec_ErrorContext
ec("While BER-encoding type '%s': ", p_td
.name
);
594 unsigned BER_coding
=va_arg(pvar
, unsigned);
595 BER_encode_chk_coding(BER_coding
);
596 ASN_BER_TLV_t
*tlv
=BER_encode_TLV(p_td
, BER_coding
);
597 tlv
->put_in_buffer(p_buf
);
598 ASN_BER_TLV_t::destruct(tlv
);
600 case TTCN_EncDec::CT_RAW
: {
601 TTCN_EncDec_ErrorContext
ec("While RAW-encoding type '%s': ", p_td
.name
);
603 TTCN_EncDec_ErrorContext::error_internal
604 ("No RAW descriptor available for type '%s'.", p_td
.name
);
608 RAW_enc_tree
root(true,NULL
,&rp
,1,p_td
.raw
);
609 RAW_encode(p_td
, root
);
610 root
.put_to_buf(p_buf
);
612 case TTCN_EncDec::CT_XER
: {
613 TTCN_EncDec_ErrorContext
ec("While XER-encoding type '%s': ", p_td
.name
);
614 unsigned XER_coding
=va_arg(pvar
, unsigned);
615 XER_encode(*p_td
.xer
, p_buf
, XER_coding
, 0, 0);
617 case TTCN_EncDec::CT_JSON
: {
618 TTCN_EncDec_ErrorContext
ec("While JSON-encoding type '%s': ", p_td
.name
);
620 TTCN_EncDec_ErrorContext::error_internal
621 ("No JSON descriptor available for type '%s'.", p_td
.name
);
622 JSON_Tokenizer
tok(va_arg(pvar
, int) != 0);
623 JSON_encode(p_td
, tok
);
624 p_buf
.put_s(tok
.get_buffer_length(), (const unsigned char*)tok
.get_buffer());
627 TTCN_error("Unknown coding method requested to encode type '%s'",
633 void BITSTRING::decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
634 TTCN_EncDec::coding_t p_coding
, ...)
637 va_start(pvar
, p_coding
);
639 case TTCN_EncDec::CT_BER
: {
640 TTCN_EncDec_ErrorContext
ec("While BER-decoding type '%s': ", p_td
.name
);
641 unsigned L_form
=va_arg(pvar
, unsigned);
643 BER_decode_str2TLV(p_buf
, tlv
, L_form
);
644 BER_decode_TLV(p_td
, tlv
, L_form
);
645 if(tlv
.isComplete
) p_buf
.increase_pos(tlv
.get_len());
647 case TTCN_EncDec::CT_RAW
: {
648 TTCN_EncDec_ErrorContext
ec("While RAW-decoding type '%s': ", p_td
.name
);
650 TTCN_EncDec_ErrorContext::error_internal
651 ("No RAW descriptor available for type '%s'.", p_td
.name
);
653 switch(p_td
.raw
->top_bit_order
){
661 if(RAW_decode(p_td
, p_buf
, p_buf
.get_len()*8, order
)<0)
662 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
663 "Can not decode type '%s', because invalid or incomplete"
664 " message was received"
667 case TTCN_EncDec::CT_XER
: {
668 TTCN_EncDec_ErrorContext
ec("While XER-decoding type '%s': ", p_td
.name
);
669 unsigned XER_coding
=va_arg(pvar
, unsigned);
670 XmlReaderWrap
reader(p_buf
);
671 int success
= reader
.Read();
672 for (; success
==1; success
=reader
.Read()) {
673 int type
= reader
.NodeType();
674 if (type
==XML_READER_TYPE_ELEMENT
)
677 XER_decode(*p_td
.xer
, reader
, XER_coding
, 0);
678 size_t bytes
= reader
.ByteConsumed();
679 p_buf
.set_pos(bytes
);
681 case TTCN_EncDec::CT_JSON
: {
682 TTCN_EncDec_ErrorContext
ec("While JSON-decoding type '%s': ", p_td
.name
);
684 TTCN_EncDec_ErrorContext::error_internal
685 ("No JSON descriptor available for type '%s'.", p_td
.name
);
686 JSON_Tokenizer
tok((const char*)p_buf
.get_data(), p_buf
.get_len());
687 if(JSON_decode(p_td
, tok
, false)<0)
688 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
689 "Can not decode type '%s', because invalid or incomplete"
690 " message was received"
692 p_buf
.set_pos(tok
.get_buf_pos());
695 TTCN_error("Unknown coding method requested to decode type '%s'",
701 void BITSTRING::BER_encode_putbits(unsigned char *target
,
702 unsigned int bitnum_start
,
703 unsigned int bit_count
) const
705 unsigned int nof_bits
, nof_octets
, i
, j
;
708 nof_bits
=val_ptr
->n_bits
;
709 if(bitnum_start
>nof_bits
710 || bitnum_start
+bit_count
>nof_bits
)
711 TTCN_EncDec_ErrorContext::error_internal
712 ("In BITSTRING::BER_encode_putbits(): Index overflow.");
713 nof_octets
=(bit_count
+7)/8;
718 target
[0]=(unsigned char)(nof_octets
*8-bit_count
);
719 for(i
=0; i
<nof_octets
-1; i
++) {
723 if(get_bit(bitnum_start
+8*i
+j
)) c
|=0x01;
731 if(get_bit(bitnum_start
+8*i
+j
)) c
|=0x01;
737 BITSTRING::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
738 unsigned p_coding
) const
741 ASN_BER_TLV_t
*new_tlv
=BER_encode_chk_bound(is_bound());
743 unsigned char *V_ptr
;
745 unsigned int nof_bits
=val_ptr
->n_bits
;
746 unsigned int nof_octets
=(nof_bits
+7)/8;
747 unsigned int nof_fragments
=0;
748 if(p_coding
==BER_ENCODE_CER
) {
749 nof_fragments
=(nof_octets
+998)/999;
750 if(!nof_fragments
) nof_fragments
=1;
752 else /*if(coding==BER_ENCODE_DER)*/ {
756 boolean is_constructed
=nof_fragments
>1;
757 if(!is_constructed
) {
759 V_ptr
=(unsigned char*)Malloc(V_len
);
760 BER_encode_putbits(V_ptr
, 0, nof_bits
);
761 new_tlv
=ASN_BER_TLV_t::construct(V_len
, V_ptr
);
763 else { // is constructed
764 ASN_BER_TLV_t
*tmp_tlv
=NULL
;
765 new_tlv
=ASN_BER_TLV_t::construct(NULL
);
766 unsigned int rest_octets
=nof_octets
-(nof_fragments
-1)*999;
768 V_len=(nof_fragments-1)*1004;
769 V_len+=rest_octets<128?2:4;
770 V_len+=rest_octets+1;
771 V_ptr=(unsigned char*)Malloc(V_len);
774 unsigned int nof_bits_curr
=8*999;
775 for(unsigned int i
=0; i
<nof_fragments
; i
++) {
776 if(i
==nof_fragments
-1) {
778 nof_bits_curr
=nof_bits
-i
*8*999;
780 V_ptr
=(unsigned char*)Malloc(V_len
+1); // because of unused bits-octet
787 BER_encode_putbits(V_ptr
, i
*8*999, nof_bits_curr
);
791 tmp_tlv
=ASN_BER_TLV_t::construct(V_len
+1, V_ptr
);
792 tmp_tlv
=ASN_BER_V2TLV(tmp_tlv
, BITSTRING_descr_
, p_coding
);
793 new_tlv
->add_TLV(tmp_tlv
);
797 if(rest_octets<128) {
798 V_ptr[1]=(rest_octets+1) & '\x7F';
803 V_ptr[2]=((rest_octets+1)/256) & 0xFF;
804 V_ptr[3]=(rest_octets+1) & 0xFF;
807 BER_encode_putbits(V_ptr, i*8*999, nof_bits-i*8*999);
811 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
815 void BITSTRING::BER_decode_getbits(const unsigned char *source
,
816 size_t s_len
, unsigned int& bitnum_start
)
821 TTCN_EncDec_ErrorContext::error
822 (TTCN_EncDec::ET_INVAL_MSG
, "Length of V-part of bitstring"
826 unsigned int nof_octets
=s_len
-1;
827 unsigned int nof_restbits
=8-source
[0];
830 TTCN_EncDec_ErrorContext::error
831 (TTCN_EncDec::ET_INVAL_MSG
,
832 "If the bitstring is empty,"
833 " the initial octet shall be 0, not %u [see X.690 clause 8.6.2.3].",
838 TTCN_EncDec_ErrorContext::error
839 (TTCN_EncDec::ET_INVAL_MSG
, "The number of unused bits in bitstring"
840 " cannot be %u (should be less than 8) [see X.690 clause 8.6.2.2].",
844 // And what about overflow? :)
845 i
= (nof_octets
- 1) * 8 + nof_restbits
;
847 if (val_ptr
->ref_count
> 1) {
848 bitstring_struct
*old_ptr
= val_ptr
;
849 old_ptr
->ref_count
--;
850 init_struct(bitnum_start
+ i
);
851 memcpy(val_ptr
->bits_ptr
, old_ptr
->bits_ptr
, (old_ptr
->n_bits
+ 7) / 8);
853 if ((bitnum_start
+ i
+ 7) / 8 > ((unsigned int)val_ptr
->n_bits
+ 7) / 8)
854 val_ptr
= (bitstring_struct
*)Realloc(val_ptr
,
855 MEMORY_SIZE(bitnum_start
+ i
));
856 val_ptr
->n_bits
= bitnum_start
+ i
;
859 for(i
=0; i
<nof_octets
-1; i
++) {
862 set_bit(bitnum_start
+8*i
+j
, c
& 0x80?TRUE
:FALSE
);
867 for(j
=0; j
<nof_restbits
; j
++) {
868 set_bit(bitnum_start
+8*i
+j
, c
& 0x80?TRUE
:FALSE
);
871 bitnum_start
+=(nof_octets
-1)*8+nof_restbits
;
874 void BITSTRING::BER_decode_TLV_(const ASN_BER_TLV_t
& p_tlv
, unsigned L_form
,
875 unsigned int& bitnum_start
)
877 if(!p_tlv
.isConstructed
) {
878 if (p_tlv
.isComplete
|| p_tlv
.V
.str
.Vlen
> 0)
879 BER_decode_getbits(p_tlv
.V
.str
.Vstr
, p_tlv
.V
.str
.Vlen
, bitnum_start
);
881 else { // is constructed
886 if(!ASN_BER_str2TLV(p_tlv
.V
.str
.Vlen
-V_pos
, p_tlv
.V
.str
.Vstr
+V_pos
,
888 TTCN_EncDec_ErrorContext::error
889 (TTCN_EncDec::ET_INCOMPL_MSG
,
890 "Incomplete TLV in a constructed BITSTRING TLV.");
893 if(!p_tlv
.isLenDefinite
&& tlv2
.tagnumber
==0
894 && tlv2
.tagclass
==ASN_TAG_UNIV
)
895 doit
=FALSE
; // End-of-contents
897 ASN_BER_TLV_t stripped_tlv
;
898 BER_decode_strip_tags(BITSTRING_ber_
, tlv2
, L_form
, stripped_tlv
);
899 BER_decode_TLV_(tlv2
, L_form
, bitnum_start
);
900 V_pos
+=tlv2
.get_len();
901 if(V_pos
>=p_tlv
.V
.str
.Vlen
) doit
=FALSE
;
904 } // else / is constructed
907 boolean
BITSTRING::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
908 const ASN_BER_TLV_t
& p_tlv
,
913 ASN_BER_TLV_t stripped_tlv
;
914 BER_decode_strip_tags(*p_td
.ber
, p_tlv
, L_form
, stripped_tlv
);
915 TTCN_EncDec_ErrorContext
ec("While decoding BITSTRING type: ");
917 unsigned int bitnum_start
= 0;
918 BER_decode_TLV_(stripped_tlv
, L_form
, bitnum_start
);
922 int BITSTRING::RAW_encode(const TTCN_Typedescriptor_t
& p_td
, RAW_enc_tree
& myleaf
) const
925 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
926 "Encoding an unbound value.");
928 int bl
= val_ptr
->n_bits
;
929 int align_length
= p_td
.raw
->fieldlength
? p_td
.raw
->fieldlength
- bl
: 0;
930 if ((bl
+ align_length
) < val_ptr
->n_bits
) {
931 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
932 "There is no sufficient bits to encode '%s': ", p_td
.name
);
933 bl
= p_td
.raw
->fieldlength
;
936 // myleaf.ext_bit=EXT_BIT_NO;
937 if (myleaf
.must_free
) Free(myleaf
.body
.leaf
.data_ptr
);
938 myleaf
.must_free
= false;
939 myleaf
.data_ptr_used
= true;
940 myleaf
.body
.leaf
.data_ptr
= val_ptr
->bits_ptr
;
942 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= true;
943 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
944 myleaf
.coding_par
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
946 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= true;
947 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
948 myleaf
.coding_par
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
950 if (p_td
.raw
->endianness
== ORDER_MSB
) myleaf
.align
= align_length
;
951 else myleaf
.align
= -align_length
;
953 return myleaf
.length
= bl
+ align_length
;
956 int BITSTRING::RAW_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
957 int limit
, raw_order_t top_bit_ord
, boolean no_err
, int /*sel_field*/,
958 boolean
/*first_call*/)
960 int prepaddlength
= buff
.increase_pos_padd(p_td
.raw
->prepadding
);
961 limit
-= prepaddlength
;
962 int decode_length
= p_td
.raw
->fieldlength
== 0
963 ? limit
: p_td
.raw
->fieldlength
;
964 if ( p_td
.raw
->fieldlength
> limit
965 || p_td
.raw
->fieldlength
> (int) buff
.unread_len_bit()) {
966 if (no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
967 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
968 "There is not enough bits in the buffer to decode type %s.", p_td
.name
);
969 decode_length
= limit
> (int) buff
.unread_len_bit()
970 ? buff
.unread_len_bit() : limit
;
973 init_struct(decode_length
);
976 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= true;
977 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
978 cp
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
980 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= true;
981 if (p_td
.raw
->bitorderinfield
== ORDER_LSB
) orders
= !orders
;
982 cp
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
983 cp
.fieldorder
= p_td
.raw
->fieldorder
;
984 cp
.hexorder
= ORDER_LSB
;
985 buff
.get_b((size_t) decode_length
, val_ptr
->bits_ptr
, cp
, top_bit_ord
);
986 if (p_td
.raw
->length_restrition
!= -1) {
987 val_ptr
->n_bits
= p_td
.raw
->length_restrition
;
988 if (p_td
.raw
->endianness
== ORDER_LSB
) {
989 if ((decode_length
- val_ptr
->n_bits
) % 8) {
990 int bound
= (decode_length
- val_ptr
->n_bits
) % 8;
991 int maxindex
= (decode_length
- 1) / 8;
992 for (int a
= 0, b
= (decode_length
- val_ptr
->n_bits
- 1) / 8;
993 a
< (val_ptr
->n_bits
+ 7) / 8; a
++, b
++) {
994 val_ptr
->bits_ptr
[a
] = val_ptr
->bits_ptr
[b
] >> bound
;
996 val_ptr
->bits_ptr
[a
] = val_ptr
->bits_ptr
[b
+ 1] << (8 - bound
);
1000 else memmove(val_ptr
->bits_ptr
,
1001 val_ptr
->bits_ptr
+ (decode_length
- val_ptr
->n_bits
) / 8,
1002 val_ptr
->n_bits
/ 8 * sizeof(unsigned char));
1005 decode_length
+= buff
.increase_pos_padd(p_td
.raw
->padding
);
1006 clear_unused_bits();
1007 return decode_length
+ prepaddlength
;
1010 int BITSTRING::XER_encode(const XERdescriptor_t
& p_td
,
1011 TTCN_Buffer
& p_buf
, unsigned int flavor
, int indent
, embed_values_enc_struct_t
*) const
1014 TTCN_EncDec_ErrorContext::error
1015 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound bitstring value.");
1018 int encoded_length
=(int)p_buf
.get_len();
1019 int empty_element
= val_ptr
==NULL
|| val_ptr
->n_bits
== 0;
1020 flavor
|= SIMPLE_TYPE
;
1021 flavor
&= ~XER_RECOF
; // bitstring doesn't care
1023 begin_xml(p_td
, p_buf
, flavor
, indent
, empty_element
);
1025 if (!empty_element
) {
1026 for (int bit_count
= 0; bit_count
< val_ptr
->n_bits
; bit_count
++)
1027 p_buf
.put_c(get_bit(bit_count
) ? '1' : '0');
1030 end_xml(p_td
, p_buf
, flavor
, indent
, empty_element
);
1031 return (int)p_buf
.get_len() - encoded_length
;
1034 int BITSTRING::XER_decode(const XERdescriptor_t
& p_td
, XmlReaderWrap
& reader
,
1035 unsigned int flavor
, embed_values_dec_struct_t
*)
1037 int exer
= is_exer(flavor
);
1038 int success
= reader
.Ok(), depth
= -1, type
;
1039 boolean own_tag
= !is_exerlist(flavor
) && !(exer
&& (p_td
.xer_bits
& UNTAGGED
));
1041 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
1042 const char * name
= verify_name(reader
, p_td
, exer
);
1046 for (; success
== 1; success
= reader
.Read()) {
1047 type
= reader
.NodeType();
1048 if (XML_READER_TYPE_ELEMENT
== type
) {
1049 verify_name(reader
, p_td
, exer
);
1050 depth
= reader
.Depth();
1051 if (reader
.IsEmptyElement()) {
1057 else if (XML_READER_TYPE_TEXT
== type
&& depth
!= -1) break;
1058 else if (XML_READER_TYPE_END_ELEMENT
== type
) {
1059 // End tag without intervening #text == empty content
1060 verify_end(reader
, p_td
, depth
, exer
);
1068 type
= reader
.NodeType();
1069 if (success
== 1 && (XML_READER_TYPE_TEXT
== type
|| XML_READER_TYPE_ATTRIBUTE
== type
)) {
1070 const char* value
= (const char *)reader
.Value();
1071 size_t num_bits
= strlen(value
);
1072 init_struct(num_bits
);
1073 for (size_t i
= 0; i
< num_bits
; ++i
) {
1074 if (value
[i
] < '0' || value
[i
] > '1') {
1075 if (exer
&& (flavor
& EXIT_ON_ERROR
)) {
1079 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG
,
1080 "The bitstring value may only contain ones and zeros.");
1083 set_bit(i
, value
[i
] - '0');
1087 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
1088 // Let the caller do reader.AdvanceAttribute();
1091 for (success
= reader
.Read(); success
== 1; success
= reader
.Read()) {
1092 type
= reader
.NodeType();
1093 if (XML_READER_TYPE_END_ELEMENT
== type
) {
1094 verify_end(reader
, p_td
, depth
, exer
);
1095 reader
.Read(); // one last time
1103 int BITSTRING::JSON_encode(const TTCN_Typedescriptor_t
&, JSON_Tokenizer
& p_tok
) const
1106 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
1107 "Encoding an unbound bitstring value.");
1111 char* tmp_str
= (char*)Malloc(val_ptr
->n_bits
+ 3);
1113 tmp_str
[val_ptr
->n_bits
+ 1] = '\"';
1114 for (int i
= 0; i
< val_ptr
->n_bits
; ++i
) {
1115 tmp_str
[i
+ 1] = get_bit(i
) ? '1' : '0';
1117 tmp_str
[val_ptr
->n_bits
+ 2] = 0;
1118 int enc_len
= p_tok
.put_next_token(JSON_TOKEN_STRING
, tmp_str
);
1123 int BITSTRING::JSON_decode(const TTCN_Typedescriptor_t
& p_td
, JSON_Tokenizer
& p_tok
, boolean p_silent
)
1125 json_token_t token
= JSON_TOKEN_NONE
;
1127 size_t value_len
= 0;
1128 boolean error
= false;
1130 boolean use_default
= p_td
.json
->default_value
&& 0 == p_tok
.get_buffer_length();
1132 // No JSON data in the buffer -> use default value
1133 value
= (char*)p_td
.json
->default_value
;
1134 value_len
= strlen(value
);
1136 dec_len
= p_tok
.get_next_token(&token
, &value
, &value_len
);
1138 if (JSON_TOKEN_ERROR
== token
) {
1139 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_BAD_TOKEN_ERROR
, "");
1140 return JSON_ERROR_FATAL
;
1142 else if (JSON_TOKEN_STRING
== token
|| use_default
) {
1143 if (use_default
|| (value_len
> 2 && value
[0] == '\"' && value
[value_len
- 1] == '\"')) {
1145 // The default value doesn't have quotes around it
1149 init_struct(value_len
);
1150 for (size_t i
= 0; i
< value_len
; ++i
) {
1151 if ('0' <= value
[i
] && '1' >= value
[i
]) {
1152 set_bit(i
, value
[i
] - '0');
1162 return JSON_ERROR_INVALID_TOKEN
;
1166 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_FORMAT_ERROR
, "string", "bitstring");
1170 return JSON_ERROR_FATAL
;
1176 // bitstring element class
1178 BITSTRING_ELEMENT::BITSTRING_ELEMENT(boolean par_bound_flag
,
1179 BITSTRING
& par_str_val
, int par_bit_pos
)
1180 : bound_flag(par_bound_flag
), str_val(par_str_val
), bit_pos(par_bit_pos
)
1184 BITSTRING_ELEMENT
& BITSTRING_ELEMENT::operator=(const BITSTRING
& other_value
)
1186 other_value
.must_bound("Assignment of an unbound bitstring value.");
1187 if(other_value
.val_ptr
->n_bits
!= 1)
1188 TTCN_error("Assignment of a bitstring "
1189 "value with length other than 1 to a bitstring element.");
1191 str_val
.copy_value();
1192 str_val
.set_bit(bit_pos
, other_value
.get_bit(0));
1196 BITSTRING_ELEMENT
& BITSTRING_ELEMENT::operator=
1197 (const BITSTRING_ELEMENT
& other_value
)
1199 other_value
.must_bound("Assignment of an unbound bitstring element.");
1201 str_val
.copy_value();
1202 str_val
.set_bit(bit_pos
, other_value
.str_val
.get_bit(other_value
.bit_pos
));
1206 boolean
BITSTRING_ELEMENT::operator==(const BITSTRING
& other_value
) const
1208 must_bound("Unbound left operand of bitstring element comparison.");
1209 other_value
.must_bound("Unbound right operand of bitstring comparison.");
1210 if(other_value
.val_ptr
->n_bits
!= 1) return FALSE
;
1211 return str_val
.get_bit(bit_pos
) == other_value
.get_bit(0);
1214 boolean
BITSTRING_ELEMENT::operator==
1215 (const BITSTRING_ELEMENT
& other_value
) const
1217 must_bound("Unbound left operand of bitstring element comparison.");
1218 other_value
.must_bound("Unbound right operand of bitstring element "
1220 return str_val
.get_bit(bit_pos
) ==
1221 other_value
.str_val
.get_bit(other_value
.bit_pos
);
1224 BITSTRING
BITSTRING_ELEMENT::operator+(const BITSTRING
& other_value
) const
1226 must_bound("Unbound left operand of bitstring element concatenation.");
1227 other_value
.must_bound("Unbound right operand of bitstring concatenation.");
1228 int n_bits
= other_value
.val_ptr
->n_bits
;
1229 BITSTRING
ret_val(n_bits
+ 1);
1230 ret_val
.val_ptr
->bits_ptr
[0] = str_val
.get_bit(bit_pos
) ? 1 : 0;
1231 int n_bytes
= (n_bits
+ 7) / 8;
1232 for (int byte_count
= 0; byte_count
< n_bytes
; byte_count
++) {
1233 ret_val
.val_ptr
->bits_ptr
[byte_count
] |=
1234 other_value
.val_ptr
->bits_ptr
[byte_count
] << 1;
1235 if (n_bits
> byte_count
* 8 + 7)
1236 ret_val
.val_ptr
->bits_ptr
[byte_count
+ 1] =
1237 other_value
.val_ptr
->bits_ptr
[byte_count
] >> 7;
1239 ret_val
.clear_unused_bits();
1243 BITSTRING
BITSTRING_ELEMENT::operator+(const BITSTRING_ELEMENT
& other_value
)
1246 must_bound("Unbound left operand of bitstring element concatenation.");
1247 other_value
.must_bound("Unbound right operand of bitstring element "
1249 unsigned char result
= str_val
.get_bit(bit_pos
) ? 1 : 0;
1250 if (other_value
.str_val
.get_bit(other_value
.bit_pos
)) result
|= 2;
1251 return BITSTRING(2, &result
);
1254 BITSTRING
BITSTRING_ELEMENT::operator~() const
1256 must_bound("Unbound bitstring element operand of operator not4b.");
1257 unsigned char result
= str_val
.get_bit(bit_pos
) ? 0 : 1;
1258 return BITSTRING(1, &result
);
1261 BITSTRING
BITSTRING_ELEMENT::operator&(const BITSTRING
& other_value
) const
1263 must_bound("Left operand of operator and4b is an unbound bitstring element.");
1264 other_value
.must_bound("Right operand of operator and4b is an unbound "
1265 "bitstring value.");
1266 if (other_value
.val_ptr
->n_bits
!= 1)
1267 TTCN_error("The bitstring operands "
1268 "of operator and4b must have the same length.");
1269 unsigned char result
= str_val
.get_bit(bit_pos
) && other_value
.get_bit(0) ?
1271 return BITSTRING(1, &result
);
1274 BITSTRING
BITSTRING_ELEMENT::operator&
1275 (const BITSTRING_ELEMENT
& other_value
) const
1277 must_bound("Left operand of operator and4b is an unbound bitstring element.");
1278 other_value
.must_bound("Right operand of operator and4b is an unbound "
1279 "bitstring element.");
1280 unsigned char result
= str_val
.get_bit(bit_pos
) &&
1281 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1282 return BITSTRING(1, &result
);
1285 BITSTRING
BITSTRING_ELEMENT::operator|(const BITSTRING
& other_value
) const
1287 must_bound("Left operand of operator or4b is an unbound bitstring element.");
1288 other_value
.must_bound("Right operand of operator or4b is an unbound "
1289 "bitstring value.");
1290 if (other_value
.val_ptr
->n_bits
!= 1)
1291 TTCN_error("The bitstring operands "
1292 "of operator or4b must have the same length.");
1293 unsigned char result
= str_val
.get_bit(bit_pos
) || other_value
.get_bit(0) ?
1295 return BITSTRING(1, &result
);
1298 BITSTRING
BITSTRING_ELEMENT::operator|
1299 (const BITSTRING_ELEMENT
& other_value
) const
1301 must_bound("Left operand of operator or4b is an unbound bitstring element.");
1302 other_value
.must_bound("Right operand of operator or4b is an unbound "
1303 "bitstring element.");
1304 unsigned char result
= str_val
.get_bit(bit_pos
) ||
1305 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1306 return BITSTRING(1, &result
);
1309 BITSTRING
BITSTRING_ELEMENT::operator^(const BITSTRING
& other_value
) const
1311 must_bound("Left operand of operator xor4b is an unbound bitstring element.");
1312 other_value
.must_bound("Right operand of operator xor4b is an unbound "
1313 "bitstring value.");
1314 if (other_value
.val_ptr
->n_bits
!= 1)
1315 TTCN_error("The bitstring operands "
1316 "of operator xor4b must have the same length.");
1317 unsigned char result
= str_val
.get_bit(bit_pos
) != other_value
.get_bit(0) ?
1319 return BITSTRING(1, &result
);
1322 BITSTRING
BITSTRING_ELEMENT::operator^
1323 (const BITSTRING_ELEMENT
& other_value
) const
1325 must_bound("Left operand of operator xor4b is an unbound bitstring element.");
1326 other_value
.must_bound("Right operand of operator xor4b is an unbound "
1327 "bitstring element.");
1328 unsigned char result
= str_val
.get_bit(bit_pos
) !=
1329 other_value
.str_val
.get_bit(other_value
.bit_pos
) ? 1 : 0;
1330 return BITSTRING(1, &result
);
1333 boolean
BITSTRING_ELEMENT::get_bit() const
1335 return str_val
.get_bit(bit_pos
);
1338 void BITSTRING_ELEMENT::log() const
1340 if (bound_flag
) TTCN_Logger::log_event("'%c'B", str_val
.get_bit(bit_pos
) ?
1342 else TTCN_Logger::log_event_unbound();
1345 // bitstring template class
1347 void BITSTRING_template::clean_up()
1349 switch (template_selection
) {
1351 case COMPLEMENTED_LIST
:
1352 delete [] value_list
.list_value
;
1354 case STRING_PATTERN
:
1355 if (pattern_value
->ref_count
> 1) pattern_value
->ref_count
--;
1356 else if (pattern_value
->ref_count
== 1) Free(pattern_value
);
1357 else TTCN_error("Internal error: Invalid reference counter in a bitstring "
1363 template_selection
= UNINITIALIZED_TEMPLATE
;
1366 void BITSTRING_template::copy_template(const BITSTRING_template
& other_value
)
1368 switch (other_value
.template_selection
) {
1369 case SPECIFIC_VALUE
:
1370 single_value
= other_value
.single_value
;
1377 case COMPLEMENTED_LIST
:
1378 value_list
.n_values
= other_value
.value_list
.n_values
;
1379 value_list
.list_value
= new BITSTRING_template
[value_list
.n_values
];
1380 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1381 value_list
.list_value
[i
].copy_template(
1382 other_value
.value_list
.list_value
[i
]);
1384 case STRING_PATTERN
:
1385 pattern_value
= other_value
.pattern_value
;
1386 pattern_value
->ref_count
++;
1389 TTCN_error("Copying an uninitialized/unsupported bitstring template.");
1391 set_selection(other_value
);
1395 This is the same algorithm that match_array uses
1396 to match 'record of' types.
1397 The only differences are: how two elements are matched and
1398 how an asterisk or ? is identified in the template
1400 boolean
BITSTRING_template::match_pattern(
1401 const bitstring_pattern_struct
*string_pattern
,
1402 const BITSTRING::bitstring_struct
*string_value
)
1404 if(string_pattern
->n_elements
== 0) return string_value
->n_bits
== 0;
1406 int value_index
= 0;
1407 unsigned int template_index
= 0;
1408 int last_asterisk
= -1;
1409 int last_value_to_asterisk
= -1;
1413 switch(string_pattern
->elements_ptr
[template_index
]) {
1415 if (!(string_value
->bits_ptr
[value_index
/ 8] &
1416 (1 << (value_index
% 8))))
1421 if(last_asterisk
== -1) return FALSE
;
1422 template_index
= last_asterisk
+1;
1423 value_index
= ++last_value_to_asterisk
;
1427 if (string_value
->bits_ptr
[value_index
/ 8] & (1 << (value_index
% 8)))
1432 if(last_asterisk
== -1) return FALSE
;
1433 template_index
= last_asterisk
+1;
1434 value_index
= ++last_value_to_asterisk
;
1438 //we found a ? element, it matches anything
1443 //we found an asterisk
1444 last_asterisk
= template_index
++;
1445 last_value_to_asterisk
= value_index
;
1448 TTCN_error("Internal error: invalid element in bitstring pattern.");
1451 if(value_index
== string_value
->n_bits
1452 && template_index
== string_pattern
->n_elements
)
1455 }else if(template_index
== string_pattern
->n_elements
)
1457 if(string_pattern
->elements_ptr
[template_index
-1] == 3)
1460 } else if (last_asterisk
== -1){
1463 template_index
= last_asterisk
+1;
1464 value_index
= ++last_value_to_asterisk
;
1466 } else if(value_index
== string_value
->n_bits
)
1468 while(template_index
< string_pattern
->n_elements
&&
1469 string_pattern
->elements_ptr
[template_index
] == 3)
1472 return template_index
== string_pattern
->n_elements
;
1477 BITSTRING_template::BITSTRING_template()
1481 BITSTRING_template::BITSTRING_template(template_sel other_value
)
1482 : Restricted_Length_Template(other_value
)
1484 check_single_selection(other_value
);
1487 BITSTRING_template::BITSTRING_template(const BITSTRING
& other_value
)
1488 : Restricted_Length_Template(SPECIFIC_VALUE
), single_value(other_value
)
1492 BITSTRING_template::BITSTRING_template(const BITSTRING_ELEMENT
& other_value
)
1493 : Restricted_Length_Template(SPECIFIC_VALUE
), single_value(other_value
)
1497 BITSTRING_template::~BITSTRING_template()
1502 BITSTRING_template::BITSTRING_template(const OPTIONAL
<BITSTRING
>& other_value
)
1504 switch (other_value
.get_selection()) {
1505 case OPTIONAL_PRESENT
:
1506 set_selection(SPECIFIC_VALUE
);
1507 single_value
= (const BITSTRING
&)other_value
;
1510 set_selection(OMIT_VALUE
);
1513 TTCN_error("Creating a bitstring template from an unbound optional field.");
1517 BITSTRING_template::BITSTRING_template(unsigned int n_elements
,
1518 const unsigned char *pattern_elements
)
1519 : Restricted_Length_Template(STRING_PATTERN
)
1521 pattern_value
= (bitstring_pattern_struct
*)
1522 Malloc(sizeof(bitstring_pattern_struct
) + n_elements
- 1);
1523 pattern_value
->ref_count
= 1;
1524 pattern_value
->n_elements
= n_elements
;
1525 memcpy(pattern_value
->elements_ptr
, pattern_elements
, n_elements
);
1528 BITSTRING_template::BITSTRING_template(const BITSTRING_template
& other_value
)
1529 : Restricted_Length_Template()
1531 copy_template(other_value
);
1534 BITSTRING_template
& BITSTRING_template::operator=(template_sel other_value
)
1536 check_single_selection(other_value
);
1538 set_selection(other_value
);
1542 BITSTRING_template
& BITSTRING_template::operator=(const BITSTRING
& other_value
)
1544 other_value
.must_bound("Assignment of an unbound bitstring value to a "
1547 set_selection(SPECIFIC_VALUE
);
1548 single_value
= other_value
;
1552 BITSTRING_template
& BITSTRING_template::operator=
1553 (const BITSTRING_ELEMENT
& other_value
)
1555 other_value
.must_bound("Assignment of an unbound bitstring element to a "
1558 set_selection(SPECIFIC_VALUE
);
1559 single_value
= other_value
;
1563 BITSTRING_template
& BITSTRING_template::operator=
1564 (const OPTIONAL
<BITSTRING
>& other_value
)
1567 switch (other_value
.get_selection()) {
1568 case OPTIONAL_PRESENT
:
1569 set_selection(SPECIFIC_VALUE
);
1570 single_value
= (const BITSTRING
&)other_value
;
1573 set_selection(OMIT_VALUE
);
1576 TTCN_error("Assignment of an unbound optional field to a bitstring "
1582 BITSTRING_template
& BITSTRING_template::operator=
1583 (const BITSTRING_template
& other_value
)
1585 if (&other_value
!= this) {
1587 copy_template(other_value
);
1592 BITSTRING_ELEMENT
BITSTRING_template::operator[](int index_value
)
1594 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1595 TTCN_error("Accessing a bitstring element of a non-specific bitstring "
1597 return single_value
[index_value
];
1600 BITSTRING_ELEMENT
BITSTRING_template::operator[](const INTEGER
& index_value
)
1602 index_value
.must_bound("Indexing a bitstring template with an unbound "
1604 return (*this)[(int)index_value
];
1607 const BITSTRING_ELEMENT
BITSTRING_template::operator[](int index_value
) const
1609 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1610 TTCN_error("Accessing a bitstring element of a non-specific bitstring "
1612 return single_value
[index_value
];
1615 const BITSTRING_ELEMENT
BITSTRING_template::operator[](const INTEGER
& index_value
) const
1617 index_value
.must_bound("Indexing a bitstring template with an unbound "
1619 return (*this)[(int)index_value
];
1622 boolean
BITSTRING_template::match(const BITSTRING
& other_value
) const
1624 if (!other_value
.is_bound()) return FALSE
;
1625 if (!match_length(other_value
.val_ptr
->n_bits
)) return FALSE
;
1626 switch (template_selection
) {
1627 case SPECIFIC_VALUE
:
1628 return single_value
== other_value
;
1635 case COMPLEMENTED_LIST
:
1636 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1637 if (value_list
.list_value
[i
].match(other_value
))
1638 return template_selection
== VALUE_LIST
;
1639 return template_selection
== COMPLEMENTED_LIST
;
1640 case STRING_PATTERN
:
1641 return match_pattern(pattern_value
, other_value
.val_ptr
);
1643 TTCN_error("Matching an uninitialized/unsupported bitstring template.");
1648 const BITSTRING
& BITSTRING_template::valueof() const
1650 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
1651 TTCN_error("Performing a valueof or send operation on a non-specific "
1652 "bitstring template.");
1653 return single_value
;
1656 int BITSTRING_template::lengthof() const
1659 boolean has_any_or_none
;
1661 TTCN_error("Performing lengthof() operation on a bitstring template "
1662 "which has an ifpresent attribute.");
1663 switch (template_selection
)
1665 case SPECIFIC_VALUE
:
1666 min_length
= single_value
.lengthof();
1667 has_any_or_none
= FALSE
;
1670 TTCN_error("Performing lengthof() operation on a bitstring template "
1671 "containing omit value.");
1675 has_any_or_none
= TRUE
; // max. length is infinity
1679 // error if any element does not have length or the lengths differ
1680 if (value_list
.n_values
<1)
1681 TTCN_error("Internal error: "
1682 "Performing lengthof() operation on a bitstring template "
1683 "containing an empty list.");
1684 int item_length
= value_list
.list_value
[0].lengthof();
1685 for (unsigned int i
= 1; i
< value_list
.n_values
; i
++) {
1686 if (value_list
.list_value
[i
].lengthof()!=item_length
)
1687 TTCN_error("Performing lengthof() operation on a bitstring template "
1688 "containing a value list with different lengths.");
1690 min_length
= item_length
;
1691 has_any_or_none
= FALSE
;
1694 case COMPLEMENTED_LIST
:
1695 TTCN_error("Performing lengthof() operation on a bitstring template "
1696 "containing complemented list.");
1697 case STRING_PATTERN
:
1699 has_any_or_none
= FALSE
; // TRUE if * chars in the pattern
1700 for (unsigned int i
= 0; i
< pattern_value
->n_elements
; i
++)
1702 if (pattern_value
->elements_ptr
[i
] < 3) min_length
++; // case of 1, 0, ?
1703 else has_any_or_none
= TRUE
; // case of * character
1707 TTCN_error("Performing lengthof() operation on an "
1708 "uninitialized/unsupported bitstring template.");
1710 return check_section_is_single(min_length
, has_any_or_none
,
1711 "length", "a", "bitstring template");
1714 void BITSTRING_template::set_type(template_sel template_type
,
1715 unsigned int list_length
)
1717 if (template_type
!= VALUE_LIST
&& template_type
!= COMPLEMENTED_LIST
)
1718 TTCN_error("Setting an invalid list type for a bitstring template.");
1720 set_selection(template_type
);
1721 value_list
.n_values
= list_length
;
1722 value_list
.list_value
= new BITSTRING_template
[list_length
];
1725 BITSTRING_template
& BITSTRING_template::list_item(unsigned int list_index
)
1727 if (template_selection
!= VALUE_LIST
&&
1728 template_selection
!= COMPLEMENTED_LIST
)
1729 TTCN_error("Accessing a list element of a non-list bitstring template.");
1730 if (list_index
>= value_list
.n_values
)
1731 TTCN_error("Index overflow in a bitstring value list template.");
1732 return value_list
.list_value
[list_index
];
1735 static const char patterns
[] = { '0', '1', '?', '*' };
1737 void BITSTRING_template::log() const
1739 switch (template_selection
) {
1740 case SPECIFIC_VALUE
:
1743 case COMPLEMENTED_LIST
:
1744 TTCN_Logger::log_event_str("complement ");
1747 TTCN_Logger::log_char('(');
1748 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++) {
1749 if (i
> 0) TTCN_Logger::log_event_str(", ");
1750 value_list
.list_value
[i
].log();
1752 TTCN_Logger::log_char(')');
1754 case STRING_PATTERN
:
1755 TTCN_Logger::log_char('\'');
1756 for (unsigned int i
= 0; i
< pattern_value
->n_elements
; i
++) {
1757 unsigned char pattern
= pattern_value
->elements_ptr
[i
];
1758 if (pattern
< 4) TTCN_Logger::log_char(patterns
[pattern
]);
1759 else TTCN_Logger::log_event_str("<unknown>");
1761 TTCN_Logger::log_event_str("'B");
1771 void BITSTRING_template::log_match(const BITSTRING
& match_value
) const
1773 if (TTCN_Logger::VERBOSITY_COMPACT
== TTCN_Logger::get_matching_verbosity()
1774 && TTCN_Logger::get_logmatch_buffer_len() != 0) {
1775 TTCN_Logger::print_logmatch_buffer();
1776 TTCN_Logger::log_event_str(" := ");
1779 TTCN_Logger::log_event_str(" with ");
1781 if (match(match_value
)) TTCN_Logger::log_event_str(" matched");
1782 else TTCN_Logger::log_event_str(" unmatched");
1785 void BITSTRING_template::set_param(Module_Param
& param
) {
1786 param
.basic_check(Module_Param::BC_TEMPLATE
|Module_Param::BC_LIST
, "bitstring template");
1787 switch (param
.get_type()) {
1788 case Module_Param::MP_Omit
:
1791 case Module_Param::MP_Any
:
1794 case Module_Param::MP_AnyOrNone
:
1795 *this = ANY_OR_OMIT
;
1797 case Module_Param::MP_List_Template
:
1798 case Module_Param::MP_ComplementList_Template
:
1799 set_type(param
.get_type()==Module_Param::MP_List_Template
? VALUE_LIST
: COMPLEMENTED_LIST
, param
.get_size());
1800 for (size_t i
=0; i
<param
.get_size(); i
++) {
1801 list_item(i
).set_param(*param
.get_elem(i
));
1804 case Module_Param::MP_Bitstring
:
1805 *this = BITSTRING(param
.get_string_size(), (unsigned char*)param
.get_string_data());
1807 case Module_Param::MP_Bitstring_Template
:
1808 *this = BITSTRING_template(param
.get_string_size(), (unsigned char*)param
.get_string_data());
1811 param
.type_error("bitstring template");
1813 is_ifpresent
= param
.get_ifpresent();
1814 set_length_range(param
);
1817 void BITSTRING_template::encode_text(Text_Buf
& text_buf
) const
1819 encode_text_restricted(text_buf
);
1820 switch (template_selection
) {
1825 case SPECIFIC_VALUE
:
1826 single_value
.encode_text(text_buf
);
1829 case COMPLEMENTED_LIST
:
1830 text_buf
.push_int(value_list
.n_values
);
1831 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1832 value_list
.list_value
[i
].encode_text(text_buf
);
1834 case STRING_PATTERN
:
1835 text_buf
.push_int(pattern_value
->n_elements
);
1836 text_buf
.push_raw(pattern_value
->n_elements
, pattern_value
->elements_ptr
);
1839 TTCN_error("Text encoder: Encoding an uninitialized/unsupported "
1840 "bitstring template.");
1844 void BITSTRING_template::decode_text(Text_Buf
& text_buf
)
1847 decode_text_restricted(text_buf
);
1848 switch (template_selection
) {
1853 case SPECIFIC_VALUE
:
1854 single_value
.decode_text(text_buf
);
1857 case COMPLEMENTED_LIST
:
1858 value_list
.n_values
= text_buf
.pull_int().get_val();
1859 value_list
.list_value
= new BITSTRING_template
[value_list
.n_values
];
1860 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1861 value_list
.list_value
[i
].decode_text(text_buf
);
1863 case STRING_PATTERN
: {
1864 unsigned int n_elements
= text_buf
.pull_int().get_val();
1865 pattern_value
= (bitstring_pattern_struct
*)
1866 Malloc(sizeof(bitstring_pattern_struct
) + n_elements
- 1);
1867 pattern_value
->ref_count
= 1;
1868 pattern_value
->n_elements
= n_elements
;
1869 text_buf
.pull_raw(n_elements
, pattern_value
->elements_ptr
);
1872 TTCN_error("Text decoder: An unknown/unsupported selection was "
1873 "received for a bitstring template.");
1877 boolean
BITSTRING_template::is_present() const
1879 if (template_selection
==UNINITIALIZED_TEMPLATE
) return FALSE
;
1880 return !match_omit();
1883 boolean
BITSTRING_template::match_omit() const
1885 if (is_ifpresent
) return TRUE
;
1886 switch (template_selection
) {
1891 case COMPLEMENTED_LIST
:
1892 for (unsigned int i
=0; i
<value_list
.n_values
; i
++)
1893 if (value_list
.list_value
[i
].match_omit())
1894 return template_selection
==VALUE_LIST
;
1895 return template_selection
==COMPLEMENTED_LIST
;
1902 #ifndef TITAN_RUNTIME_2
1903 void BITSTRING_template::check_restriction(template_res t_res
, const char* t_name
) const
1905 if (template_selection
==UNINITIALIZED_TEMPLATE
) return;
1906 switch ((t_name
&&(t_res
==TR_VALUE
))?TR_OMIT
:t_res
) {
1908 if (!is_ifpresent
&& template_selection
==SPECIFIC_VALUE
) return;
1911 if (!is_ifpresent
&& (template_selection
==OMIT_VALUE
||
1912 template_selection
==SPECIFIC_VALUE
)) return;
1915 if (!match_omit()) return;
1920 TTCN_error("Restriction `%s' on template of type %s violated.",
1921 get_res_name(t_res
), t_name
? t_name
: "bitstring");
1924 int BITSTRING::RAW_encode_negtest_raw(RAW_enc_tree
& p_myleaf
) const
1926 if (p_myleaf
.must_free
)
1927 Free(p_myleaf
.body
.leaf
.data_ptr
);
1928 p_myleaf
.must_free
= false;
1929 p_myleaf
.data_ptr_used
= true;
1930 p_myleaf
.body
.leaf
.data_ptr
= val_ptr
->bits_ptr
;
1931 return p_myleaf
.length
= val_ptr
->n_bits
;