Commit | Line | Data |
---|---|---|
d44e3c4f | 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 | |
7 | * | |
8 | * Contributors: | |
9 | * Baji, Laszlo | |
10 | * Balasko, Jeno | |
11 | * Baranyi, Botond | |
12 | * Beres, Szabolcs | |
13 | * Delic, Adam | |
14 | * Forstner, Matyas | |
15 | * Kovacs, Ferenc | |
16 | * Raduly, Csaba | |
17 | * Szabados, Kristof | |
18 | * Szabo, Bence Janos | |
19 | * Szabo, Janos Zoltan – initial implementation | |
20 | * Szalai, Gabor | |
21 | * Tatarka, Gabor | |
22 | * | |
23 | ******************************************************************************/ | |
970ed795 EL |
24 | #include <string.h> |
25 | ||
26 | #include "Bitstring.hh" | |
27 | #include "../common/memory.h" | |
28 | #include "Integer.hh" | |
29 | #include "String_struct.hh" | |
30 | #include "Parameters.h" | |
31 | #include "Param_Types.hh" | |
32 | #include "Error.hh" | |
33 | #include "Logger.hh" | |
34 | #include "Encdec.hh" | |
35 | ||
36 | #include "../common/dbgnew.hh" | |
37 | ||
38 | // bitstring value class | |
39 | ||
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) | |
42 | ||
43 | void BITSTRING::init_struct(int n_bits) | |
44 | { | |
45 | if (n_bits < 0) { | |
46 | val_ptr = NULL; | |
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. | |
51 | */ | |
52 | static bitstring_struct empty_string = { 1, 0, "" }; | |
53 | val_ptr = &empty_string; | |
54 | empty_string.ref_count++; | |
55 | } else { | |
56 | val_ptr = (bitstring_struct*)Malloc(MEMORY_SIZE(n_bits)); | |
57 | val_ptr->ref_count = 1; | |
58 | val_ptr->n_bits = n_bits; | |
59 | } | |
60 | } | |
61 | ||
62 | boolean BITSTRING::get_bit(int bit_index) const | |
63 | { | |
64 | return val_ptr->bits_ptr[bit_index / 8] & (1 << (bit_index % 8)); | |
65 | } | |
66 | ||
67 | void BITSTRING::set_bit(int bit_index, boolean new_value) | |
68 | { | |
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; | |
72 | } | |
73 | ||
74 | void BITSTRING::copy_value() | |
75 | { | |
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; | |
81 | old_ptr->ref_count--; | |
82 | init_struct(old_ptr->n_bits); | |
83 | memcpy(val_ptr->bits_ptr, old_ptr->bits_ptr, (old_ptr->n_bits + 7) / 8); | |
84 | } | |
85 | } | |
86 | ||
87 | void BITSTRING::clear_unused_bits() const | |
88 | { | |
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); | |
92 | } | |
93 | ||
94 | BITSTRING::BITSTRING(int n_bits) | |
95 | { | |
96 | init_struct(n_bits); | |
97 | } | |
98 | ||
99 | BITSTRING::BITSTRING() | |
100 | { | |
101 | val_ptr = NULL; | |
102 | } | |
103 | ||
104 | BITSTRING::BITSTRING(int n_bits, const unsigned char *bits_ptr) | |
105 | { | |
106 | init_struct(n_bits); | |
107 | memcpy(val_ptr->bits_ptr, bits_ptr, (n_bits + 7) / 8); | |
108 | clear_unused_bits(); | |
109 | } | |
110 | ||
111 | BITSTRING::BITSTRING(const BITSTRING& other_value) | |
112 | : Base_Type(other_value) | |
113 | { | |
114 | other_value.must_bound("Copying an unbound bitstring value."); | |
115 | val_ptr = other_value.val_ptr; | |
116 | val_ptr->ref_count++; | |
117 | } | |
118 | ||
119 | BITSTRING::BITSTRING(const BITSTRING_ELEMENT& other_value) | |
120 | { | |
121 | other_value.must_bound("Copying an unbound bitstring element."); | |
122 | init_struct(1); | |
123 | val_ptr->bits_ptr[0] = other_value.get_bit() ? 1 : 0; | |
124 | } | |
125 | ||
126 | BITSTRING::~BITSTRING() | |
127 | { | |
128 | clean_up(); | |
129 | } | |
130 | ||
131 | void BITSTRING::clean_up() | |
132 | { | |
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 " | |
137 | "value."); | |
138 | val_ptr = NULL; | |
139 | } | |
140 | } | |
141 | ||
142 | BITSTRING& BITSTRING::operator=(const BITSTRING& other_value) | |
143 | { | |
144 | other_value.must_bound("Assignment of an unbound bitstring value."); | |
145 | if (&other_value != this) { | |
146 | clean_up(); | |
147 | val_ptr = other_value.val_ptr; | |
148 | val_ptr->ref_count++; | |
149 | } | |
150 | return *this; | |
151 | } | |
152 | ||
153 | BITSTRING& BITSTRING::operator=(const BITSTRING_ELEMENT& other_value) | |
154 | { | |
155 | other_value.must_bound("Assignment of an unbound bitstring element to a " | |
156 | "bitstring."); | |
157 | boolean bit_value = other_value.get_bit(); | |
158 | clean_up(); | |
159 | init_struct(1); | |
160 | val_ptr->bits_ptr[0] = bit_value ? 1 : 0; | |
161 | return *this; | |
162 | } | |
163 | ||
164 | boolean BITSTRING::operator==(const BITSTRING& other_value) const | |
165 | { | |
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; | |
171 | clear_unused_bits(); | |
172 | other_value.clear_unused_bits(); | |
173 | return !memcmp(val_ptr->bits_ptr, other_value.val_ptr->bits_ptr, | |
174 | (n_bits + 7) / 8); | |
175 | } | |
176 | ||
177 | boolean BITSTRING::operator==(const BITSTRING_ELEMENT& other_value) const | |
178 | { | |
179 | must_bound("Unbound left operand of bitstring comparison."); | |
180 | other_value.must_bound("Unbound right operand of bitstring element " | |
181 | "comparison."); | |
182 | if (val_ptr->n_bits != 1) return FALSE; | |
183 | return get_bit(0) == other_value.get_bit(); | |
184 | } | |
185 | ||
186 | BITSTRING BITSTRING::operator+(const BITSTRING& other_value) const | |
187 | { | |
188 | must_bound("Unbound left operand of bitstring concatenation."); | |
189 | other_value.must_bound("Unbound right operand of bitstring concatenation."); | |
190 | ||
191 | int left_n_bits = val_ptr->n_bits; | |
192 | if (left_n_bits == 0) return other_value; | |
193 | ||
194 | int right_n_bits = other_value.val_ptr->n_bits; | |
195 | if (right_n_bits == 0) return *this; | |
196 | ||
197 | // the length of result | |
198 | int n_bits = left_n_bits + right_n_bits; | |
199 | ||
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; | |
203 | ||
204 | // the number of bits used in the last incomplete octet of the left operand | |
205 | int left_empty_bits = left_n_bits % 8; | |
206 | ||
207 | // the result | |
208 | BITSTRING ret_val(n_bits); | |
209 | ||
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; | |
214 | ||
215 | // copying the left fragment into the result | |
216 | memcpy(dest_ptr, left_ptr, left_n_bytes); | |
217 | ||
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); | |
229 | } | |
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; | |
235 | } | |
236 | } else { | |
237 | // trivial case: just append the bytes of the right fragment | |
238 | memcpy(dest_ptr + left_n_bytes, right_ptr, right_n_bytes); | |
239 | } | |
240 | ret_val.clear_unused_bits(); | |
241 | return ret_val; | |
242 | } | |
243 | ||
244 | BITSTRING BITSTRING::operator+(const BITSTRING_ELEMENT& other_value) const | |
245 | { | |
246 | must_bound("Unbound left operand of bitstring concatenation."); | |
247 | other_value.must_bound("Unbound right operand of bitstring element " | |
248 | "concatenation."); | |
249 | ||
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()); | |
254 | return ret_val; | |
255 | } | |
256 | ||
257 | BITSTRING BITSTRING::operator~() const | |
258 | { | |
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(); | |
266 | return ret_val; | |
267 | } | |
268 | ||
269 | BITSTRING BITSTRING::operator&(const BITSTRING& other_value) const | |
270 | { | |
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 " | |
273 | "bitstring value."); | |
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 " | |
277 | "same length."); | |
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(); | |
285 | return ret_val; | |
286 | } | |
287 | ||
288 | BITSTRING BITSTRING::operator&(const BITSTRING_ELEMENT& other_value) const | |
289 | { | |
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); | |
298 | } | |
299 | ||
300 | BITSTRING BITSTRING::operator|(const BITSTRING& other_value) const | |
301 | { | |
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 " | |
304 | "bitstring value."); | |
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 " | |
308 | "same length."); | |
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(); | |
316 | return ret_val; | |
317 | } | |
318 | ||
319 | BITSTRING BITSTRING::operator|(const BITSTRING_ELEMENT& other_value) const | |
320 | { | |
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); | |
329 | } | |
330 | ||
331 | BITSTRING BITSTRING::operator^(const BITSTRING& other_value) const | |
332 | { | |
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 " | |
335 | "bitstring value."); | |
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 " | |
339 | "same length."); | |
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(); | |
347 | return ret_val; | |
348 | } | |
349 | ||
350 | BITSTRING BITSTRING::operator^(const BITSTRING_ELEMENT& other_value) const | |
351 | { | |
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); | |
360 | } | |
361 | ||
362 | BITSTRING BITSTRING::operator<<(int shift_count) const | |
363 | { | |
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; | |
370 | clear_unused_bits(); | |
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) { | |
375 | int byte_count = 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] << | |
380 | (8 - shift_bits)); | |
381 | } | |
382 | ret_val.val_ptr->bits_ptr[n_bytes - shift_bytes - 1] = | |
383 | val_ptr->bits_ptr[n_bytes - 1] >> shift_bits; | |
384 | } else { | |
385 | memcpy(ret_val.val_ptr->bits_ptr, val_ptr->bits_ptr + shift_bytes, | |
386 | n_bytes - shift_bytes); | |
387 | } | |
388 | memset(ret_val.val_ptr->bits_ptr + n_bytes - shift_bytes, 0, | |
389 | shift_bytes); | |
390 | ret_val.clear_unused_bits(); | |
391 | return ret_val; | |
392 | } else if (shift_count == 0) return *this; | |
393 | else return *this >> (-shift_count); | |
394 | } | |
395 | ||
396 | BITSTRING BITSTRING::operator<<(const INTEGER& shift_count) const | |
397 | { | |
398 | shift_count.must_bound("Unbound right operand of bitstring shift left " | |
399 | "operator."); | |
400 | return *this << (int)shift_count; | |
401 | } | |
402 | ||
403 | BITSTRING BITSTRING::operator>>(int shift_count) const | |
404 | { | |
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; | |
411 | clear_unused_bits(); | |
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++) | |
419 | { | |
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); | |
423 | } | |
424 | } else { | |
425 | memcpy(ret_val.val_ptr->bits_ptr + shift_bytes, val_ptr->bits_ptr, | |
426 | n_bytes - shift_bytes); | |
427 | } | |
428 | ret_val.clear_unused_bits(); | |
429 | return ret_val; | |
430 | } else if (shift_count == 0) return *this; | |
431 | else return *this << (-shift_count); | |
432 | } | |
433 | ||
434 | BITSTRING BITSTRING::operator>>(const INTEGER& shift_count) const | |
435 | { | |
436 | shift_count.must_bound("Unbound right operand of bitstring shift right " | |
437 | "operator."); | |
438 | return *this >> (int)shift_count; | |
439 | } | |
440 | ||
441 | BITSTRING BITSTRING::operator<<=(int rotate_count) const | |
442 | { | |
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); | |
452 | } | |
453 | ||
454 | BITSTRING BITSTRING::operator<<=(const INTEGER& rotate_count) const | |
455 | { | |
456 | rotate_count.must_bound("Unbound right operand of bitstring rotate left " | |
457 | "operator."); | |
458 | return *this <<= (int)rotate_count; | |
459 | } | |
460 | ||
461 | BITSTRING BITSTRING::operator>>=(int rotate_count) const | |
462 | { | |
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); | |
472 | } | |
473 | ||
474 | BITSTRING BITSTRING::operator>>=(const INTEGER& rotate_count) const | |
475 | { | |
476 | rotate_count.must_bound("Unbound right operand of bitstring rotate right " | |
477 | "operator."); | |
478 | return *this >>= (int)rotate_count; | |
479 | } | |
480 | ||
481 | BITSTRING_ELEMENT BITSTRING::operator[](int index_value) | |
482 | { | |
483 | if (val_ptr == NULL && index_value == 0) { | |
484 | init_struct(1); | |
485 | clear_unused_bits(); | |
486 | return BITSTRING_ELEMENT(FALSE, *this, 0); | |
487 | } else { | |
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)); | |
499 | val_ptr->n_bits++; | |
500 | } else { | |
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); | |
505 | } | |
506 | clear_unused_bits(); | |
507 | return BITSTRING_ELEMENT(FALSE, *this, index_value); | |
508 | } else return BITSTRING_ELEMENT(TRUE, *this, index_value); | |
509 | } | |
510 | } | |
511 | ||
512 | BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value) | |
513 | { | |
514 | index_value.must_bound("Indexing a bitstring value with an unbound integer " | |
515 | "value."); | |
516 | return (*this)[(int)index_value]; | |
517 | } | |
518 | ||
519 | const BITSTRING_ELEMENT BITSTRING::operator[](int index_value) const | |
520 | { | |
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); | |
528 | } | |
529 | ||
530 | const BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value) const | |
531 | { | |
532 | index_value.must_bound("Indexing a bitstring value with an unbound integer " | |
533 | "value."); | |
534 | return (*this)[(int)index_value]; | |
535 | } | |
536 | ||
537 | int BITSTRING::lengthof() const | |
538 | { | |
539 | must_bound("Getting the length of an unbound bitstring value."); | |
540 | return val_ptr->n_bits; | |
541 | } | |
542 | ||
543 | BITSTRING::operator const unsigned char*() const | |
544 | { | |
545 | must_bound("Casting an unbound bitstring value to const unsigned char*."); | |
546 | return val_ptr->bits_ptr; | |
547 | } | |
548 | ||
549 | void BITSTRING::log() const | |
550 | { | |
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(); | |
557 | } | |
558 | ||
559 | void BITSTRING::set_param(Module_Param& param) { | |
560 | param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, "bitstring value"); | |
3abe9331 | 561 | Module_Param_Ptr mp = ¶m; |
562 | if (param.get_type() == Module_Param::MP_Reference) { | |
563 | mp = param.get_referenced_param(); | |
564 | } | |
565 | switch (mp->get_type()) { | |
566 | case Module_Param::MP_Bitstring: | |
567 | switch (param.get_operation_type()) { | |
568 | case Module_Param::OT_ASSIGN: | |
569 | clean_up(); | |
570 | init_struct(mp->get_string_size()); | |
571 | memcpy(val_ptr->bits_ptr, mp->get_string_data(), (val_ptr->n_bits + 7) / 8); | |
572 | clear_unused_bits(); | |
573 | break; | |
574 | case Module_Param::OT_CONCAT: | |
575 | if (is_bound()) { | |
576 | *this = *this + BITSTRING(mp->get_string_size(), (unsigned char*)mp->get_string_data()); | |
577 | } else { | |
578 | *this = BITSTRING(mp->get_string_size(), (unsigned char*)mp->get_string_data()); | |
579 | } | |
580 | break; | |
581 | default: | |
582 | TTCN_error("Internal error: BITSTRING::set_param()"); | |
583 | } | |
970ed795 | 584 | break; |
3abe9331 | 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; | |
592 | } | |
593 | else { | |
594 | *this = operand1 + operand2; | |
595 | } | |
596 | } | |
597 | else { | |
598 | param.expr_type_error("a bitstring"); | |
970ed795 EL |
599 | } |
600 | break; | |
601 | default: | |
3abe9331 | 602 | param.type_error("bitstring value"); |
603 | break; | |
970ed795 EL |
604 | } |
605 | } | |
606 | ||
3abe9331 | 607 | Module_Param* BITSTRING::get_param(Module_Param_Name& /* param_name */) const |
608 | { | |
609 | if (!is_bound()) { | |
610 | return new Module_Param_Unbound(); | |
611 | } | |
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); | |
616 | } | |
617 | ||
970ed795 EL |
618 | void BITSTRING::encode_text(Text_Buf& text_buf) const |
619 | { | |
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); | |
624 | } | |
625 | ||
626 | void BITSTRING::decode_text(Text_Buf& text_buf) | |
627 | { | |
628 | int n_bits = text_buf.pull_int().get_val(); | |
629 | if (n_bits < 0) | |
630 | TTCN_error("Text decoder: Invalid length was received for a bitstring."); | |
631 | clean_up(); | |
632 | init_struct(n_bits); | |
633 | if (n_bits > 0) { | |
634 | text_buf.pull_raw((n_bits + 7) / 8, val_ptr->bits_ptr); | |
635 | clear_unused_bits(); | |
636 | } | |
637 | } | |
638 | ||
639 | void BITSTRING::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
640 | TTCN_EncDec::coding_t p_coding, ...) const | |
641 | { | |
642 | va_list pvar; | |
643 | va_start(pvar, p_coding); | |
644 | switch(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); | |
652 | break;} | |
653 | case TTCN_EncDec::CT_RAW: { | |
654 | TTCN_EncDec_ErrorContext ec("While RAW-encoding type '%s': ", p_td.name); | |
655 | if(!p_td.raw) | |
656 | TTCN_EncDec_ErrorContext::error_internal | |
657 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
658 | RAW_enc_tr_pos rp; | |
659 | rp.level=0; | |
660 | rp.pos=NULL; | |
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); | |
664 | break;} | |
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); | |
af710487 | 668 | XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0); |
970ed795 EL |
669 | break;} |
670 | case TTCN_EncDec::CT_JSON: { | |
af710487 | 671 | TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name); |
970ed795 EL |
672 | if(!p_td.json) |
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()); | |
678 | break;} | |
679 | default: | |
680 | TTCN_error("Unknown coding method requested to encode type '%s'", | |
681 | p_td.name); | |
682 | } | |
683 | va_end(pvar); | |
684 | } | |
685 | ||
686 | void BITSTRING::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
687 | TTCN_EncDec::coding_t p_coding, ...) | |
688 | { | |
689 | va_list pvar; | |
690 | va_start(pvar, p_coding); | |
691 | switch(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); | |
695 | ASN_BER_TLV_t tlv; | |
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()); | |
699 | break;} | |
700 | case TTCN_EncDec::CT_RAW: { | |
701 | TTCN_EncDec_ErrorContext ec("While RAW-decoding type '%s': ", p_td.name); | |
702 | if(!p_td.raw) | |
703 | TTCN_EncDec_ErrorContext::error_internal | |
704 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
705 | raw_order_t order; | |
706 | switch(p_td.raw->top_bit_order){ | |
707 | case TOP_BIT_LEFT: | |
708 | order=ORDER_LSB; | |
709 | break; | |
710 | case TOP_BIT_RIGHT: | |
711 | default: | |
712 | order=ORDER_MSB; | |
713 | } | |
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" | |
718 | , p_td.name); | |
719 | break;} | |
720 | case TTCN_EncDec::CT_XER: { | |
af710487 | 721 | TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name); |
970ed795 EL |
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) | |
728 | break; | |
729 | } | |
feade998 | 730 | XER_decode(*p_td.xer, reader, XER_coding, XER_NONE, 0); |
970ed795 EL |
731 | size_t bytes = reader.ByteConsumed(); |
732 | p_buf.set_pos(bytes); | |
733 | break;} | |
734 | case TTCN_EncDec::CT_JSON: { | |
735 | TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name); | |
736 | if(!p_td.json) | |
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" | |
744 | , p_td.name); | |
745 | p_buf.set_pos(tok.get_buf_pos()); | |
746 | break;} | |
747 | default: | |
748 | TTCN_error("Unknown coding method requested to decode type '%s'", | |
749 | p_td.name); | |
750 | } | |
751 | va_end(pvar); | |
752 | } | |
753 | ||
754 | void BITSTRING::BER_encode_putbits(unsigned char *target, | |
755 | unsigned int bitnum_start, | |
756 | unsigned int bit_count) const | |
757 | { | |
758 | unsigned int nof_bits, nof_octets, i, j; | |
759 | unsigned char c; | |
760 | ||
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; | |
767 | if(!nof_octets) { | |
768 | target[0]=0x00; | |
769 | return; | |
770 | } | |
771 | target[0]=(unsigned char)(nof_octets*8-bit_count); | |
772 | for(i=0; i<nof_octets-1; i++) { | |
773 | c=0; | |
774 | for(j=0; j<8; j++) { | |
775 | c<<=1; | |
776 | if(get_bit(bitnum_start+8*i+j)) c|=0x01; | |
777 | } | |
778 | target[1+i]=c; | |
779 | } // for | |
780 | c=0; | |
781 | for(j=0; j<8; j++) { | |
782 | c<<=1; | |
783 | if(8*i+j<bit_count) | |
784 | if(get_bit(bitnum_start+8*i+j)) c|=0x01; | |
785 | } | |
786 | target[1+i]=c; | |
787 | } | |
788 | ||
789 | ASN_BER_TLV_t* | |
790 | BITSTRING::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, | |
791 | unsigned p_coding) const | |
792 | { | |
793 | BER_chk_descr(p_td); | |
794 | ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound()); | |
795 | if(!new_tlv) { | |
796 | unsigned char *V_ptr; | |
797 | size_t V_len; | |
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; | |
804 | } | |
805 | else /*if(coding==BER_ENCODE_DER)*/ { | |
806 | nof_fragments=1; | |
807 | } | |
808 | ||
809 | boolean is_constructed=nof_fragments>1; | |
810 | if(!is_constructed) { | |
811 | V_len=nof_octets+1; | |
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); | |
815 | } | |
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; | |
820 | /* | |
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); | |
825 | */ | |
826 | V_len=999; | |
827 | unsigned int nof_bits_curr=8*999; | |
828 | for(unsigned int i=0; i<nof_fragments; i++) { | |
829 | if(i==nof_fragments-1) { | |
830 | V_len=rest_octets; | |
831 | nof_bits_curr=nof_bits-i*8*999; | |
832 | } | |
833 | V_ptr=(unsigned char*)Malloc(V_len+1); // because of unused bits-octet | |
834 | /* | |
835 | V_ptr[0]=0x03; | |
836 | V_ptr[1]=0x82; | |
837 | V_ptr[2]=0x03; | |
838 | V_ptr[3]=0xE8; | |
839 | */ | |
840 | BER_encode_putbits(V_ptr, i*8*999, nof_bits_curr); | |
841 | /* | |
842 | V_ptr+=1004; | |
843 | */ | |
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); | |
847 | } | |
848 | /* | |
849 | V_ptr[0]=0x03; | |
850 | if(rest_octets<128) { | |
851 | V_ptr[1]=(rest_octets+1) & '\x7F'; | |
852 | V_ptr+=2; | |
853 | } | |
854 | else { | |
855 | V_ptr[1]=0x82; | |
856 | V_ptr[2]=((rest_octets+1)/256) & 0xFF; | |
857 | V_ptr[3]=(rest_octets+1) & 0xFF; | |
858 | V_ptr+=4; | |
859 | } | |
860 | BER_encode_putbits(V_ptr, i*8*999, nof_bits-i*8*999); | |
861 | */ | |
862 | } | |
863 | } | |
864 | new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding); | |
865 | return new_tlv; | |
866 | } | |
867 | ||
868 | void BITSTRING::BER_decode_getbits(const unsigned char *source, | |
869 | size_t s_len, unsigned int& bitnum_start) | |
870 | { | |
871 | unsigned int i, j; | |
872 | unsigned char c; | |
873 | if(s_len<1) { | |
874 | TTCN_EncDec_ErrorContext::error | |
875 | (TTCN_EncDec::ET_INVAL_MSG, "Length of V-part of bitstring" | |
876 | " cannot be 0."); | |
877 | return; | |
878 | } | |
879 | unsigned int nof_octets=s_len-1; | |
880 | unsigned int nof_restbits=8-source[0]; | |
881 | if(nof_octets==0) { | |
882 | if(nof_restbits!=8) | |
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].", | |
887 | source[0]); | |
888 | return; | |
889 | } | |
890 | if(source[0]>7) { | |
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].", | |
894 | source[0]); | |
895 | nof_restbits=1; | |
896 | } | |
897 | // And what about overflow? :) | |
898 | i = (nof_octets - 1) * 8 + nof_restbits; | |
899 | if (i > 0) { | |
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); | |
905 | } else { | |
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; | |
910 | } | |
911 | } | |
912 | for(i=0; i<nof_octets-1; i++) { | |
913 | c=source[1+i]; | |
914 | for(j=0; j<8; j++) { | |
915 | set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE); | |
916 | c<<=1; | |
917 | } | |
918 | } | |
919 | c=source[1+i]; | |
920 | for(j=0; j<nof_restbits; j++) { | |
921 | set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE); | |
922 | c<<=1; | |
923 | } | |
924 | bitnum_start+=(nof_octets-1)*8+nof_restbits; | |
925 | } | |
926 | ||
927 | void BITSTRING::BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form, | |
928 | unsigned int& bitnum_start) | |
929 | { | |
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); | |
933 | } | |
934 | else { // is constructed | |
935 | ASN_BER_TLV_t tlv2; | |
936 | size_t V_pos=0; | |
937 | boolean doit=TRUE; | |
938 | while(doit) { | |
939 | if(!ASN_BER_str2TLV(p_tlv.V.str.Vlen-V_pos, p_tlv.V.str.Vstr+V_pos, | |
940 | tlv2, L_form)) { | |
941 | TTCN_EncDec_ErrorContext::error | |
942 | (TTCN_EncDec::ET_INCOMPL_MSG, | |
943 | "Incomplete TLV in a constructed BITSTRING TLV."); | |
944 | return; | |
945 | } | |
946 | if(!p_tlv.isLenDefinite && tlv2.tagnumber==0 | |
947 | && tlv2.tagclass==ASN_TAG_UNIV) | |
948 | doit=FALSE; // End-of-contents | |
949 | if(doit) { | |
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; | |
955 | } | |
956 | } // while(doit) | |
957 | } // else / is constructed | |
958 | } | |
959 | ||
960 | boolean BITSTRING::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, | |
961 | const ASN_BER_TLV_t& p_tlv, | |
962 | unsigned L_form) | |
963 | { | |
964 | clean_up(); | |
965 | BER_chk_descr(p_td); | |
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: "); | |
969 | init_struct(0); | |
970 | unsigned int bitnum_start = 0; | |
971 | BER_decode_TLV_(stripped_tlv, L_form, bitnum_start); | |
972 | return TRUE; | |
973 | } | |
974 | ||
975 | int BITSTRING::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const | |
976 | { | |
977 | if (!is_bound()) { | |
978 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
979 | "Encoding an unbound value."); | |
980 | } | |
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; | |
987 | align_length = 0; | |
988 | } | |
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; | |
994 | bool orders = false; | |
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; | |
998 | orders = false; | |
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; | |
1002 | ||
1003 | if (p_td.raw->endianness == ORDER_MSB) myleaf.align = align_length; | |
1004 | else myleaf.align = -align_length; | |
1005 | ||
1006 | return myleaf.length = bl + align_length; | |
1007 | } | |
1008 | ||
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*/) | |
1012 | { | |
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; | |
1024 | } | |
1025 | clean_up(); | |
1026 | init_struct(decode_length); | |
1027 | RAW_coding_par cp; | |
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; | |
1032 | orders = false; | |
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; | |
1048 | if (b < maxindex) { | |
1049 | val_ptr->bits_ptr[a] = val_ptr->bits_ptr[b + 1] << (8 - bound); | |
1050 | } | |
1051 | } | |
1052 | } | |
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)); | |
1056 | } | |
1057 | } | |
1058 | decode_length += buff.increase_pos_padd(p_td.raw->padding); | |
1059 | clear_unused_bits(); | |
1060 | return decode_length + prepaddlength; | |
1061 | } | |
1062 | ||
1063 | int BITSTRING::XER_encode(const XERdescriptor_t& p_td, | |
af710487 | 1064 | TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const |
970ed795 EL |
1065 | { |
1066 | if(!is_bound()) { | |
1067 | TTCN_EncDec_ErrorContext::error | |
1068 | (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound bitstring value."); | |
1069 | } | |
1070 | ||
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 | |
1075 | ||
1076 | begin_xml(p_td, p_buf, flavor, indent, empty_element); | |
1077 | ||
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'); | |
1081 | } | |
1082 | ||
1083 | end_xml(p_td, p_buf, flavor, indent, empty_element); | |
1084 | return (int)p_buf.get_len() - encoded_length; | |
1085 | } | |
1086 | ||
1087 | int BITSTRING::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, | |
feade998 | 1088 | unsigned int flavor, unsigned int /*flavor2*/, embed_values_dec_struct_t*) |
970ed795 EL |
1089 | { |
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)); | |
1093 | ||
1094 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { | |
1095 | const char * name = verify_name(reader, p_td, exer); | |
1096 | (void)name; | |
1097 | } | |
1098 | else if (own_tag) { | |
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()) { | |
1105 | init_struct(0); | |
1106 | reader.Read(); | |
1107 | return 1; | |
1108 | } | |
1109 | } | |
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); | |
1114 | init_struct(0); | |
1115 | reader.Read(); | |
1116 | return 1; | |
1117 | } | |
1118 | } | |
1119 | } | |
1120 | ||
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)) { | |
1129 | clean_up(); | |
1130 | return -1; | |
1131 | } else { | |
1132 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG, | |
1133 | "The bitstring value may only contain ones and zeros."); | |
1134 | } | |
1135 | } | |
1136 | set_bit(i, value[i] - '0'); | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { | |
1141 | // Let the caller do reader.AdvanceAttribute(); | |
1142 | } | |
1143 | else if (own_tag) { | |
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 | |
1149 | break; | |
1150 | } | |
1151 | } | |
1152 | } | |
1153 | return 1; | |
1154 | } | |
1155 | ||
1156 | int BITSTRING::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok) const | |
1157 | { | |
1158 | if (!is_bound()) { | |
1159 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
1160 | "Encoding an unbound bitstring value."); | |
1161 | return -1; | |
1162 | } | |
1163 | ||
1164 | char* tmp_str = (char*)Malloc(val_ptr->n_bits + 3); | |
1165 | tmp_str[0] = '\"'; | |
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'; | |
1169 | } | |
1170 | tmp_str[val_ptr->n_bits + 2] = 0; | |
1171 | int enc_len = p_tok.put_next_token(JSON_TOKEN_STRING, tmp_str); | |
1172 | Free(tmp_str); | |
1173 | return enc_len; | |
1174 | } | |
1175 | ||
1176 | int BITSTRING::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent) | |
1177 | { | |
1178 | json_token_t token = JSON_TOKEN_NONE; | |
1179 | char* value = 0; | |
1180 | size_t value_len = 0; | |
1181 | boolean error = false; | |
1182 | int dec_len = 0; | |
1183 | boolean use_default = p_td.json->default_value && 0 == p_tok.get_buffer_length(); | |
1184 | if (use_default) { | |
1185 | // No JSON data in the buffer -> use default value | |
1186 | value = (char*)p_td.json->default_value; | |
1187 | value_len = strlen(value); | |
1188 | } else { | |
1189 | dec_len = p_tok.get_next_token(&token, &value, &value_len); | |
1190 | } | |
1191 | if (JSON_TOKEN_ERROR == token) { | |
1192 | JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, ""); | |
1193 | return JSON_ERROR_FATAL; | |
1194 | } | |
1195 | else if (JSON_TOKEN_STRING == token || use_default) { | |
1196 | if (use_default || (value_len > 2 && value[0] == '\"' && value[value_len - 1] == '\"')) { | |
1197 | if (!use_default) { | |
1198 | // The default value doesn't have quotes around it | |
1199 | value_len -= 2; | |
1200 | ++value; | |
1201 | } | |
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'); | |
1206 | } else { | |
1207 | error = true; | |
1208 | break; | |
1209 | } | |
1210 | } | |
1211 | } else { | |
1212 | error = true; | |
1213 | } | |
1214 | } else { | |
1215 | return JSON_ERROR_INVALID_TOKEN; | |
1216 | } | |
1217 | ||
1218 | if (error) { | |
1219 | JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FORMAT_ERROR, "string", "bitstring"); | |
1220 | if (p_silent) { | |
1221 | clean_up(); | |
1222 | } | |
1223 | return JSON_ERROR_FATAL; | |
1224 | } | |
1225 | return dec_len; | |
1226 | } | |
1227 | ||
1228 | ||
1229 | // bitstring element class | |
1230 | ||
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) | |
1234 | { | |
1235 | } | |
1236 | ||
1237 | BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator=(const BITSTRING& other_value) | |
1238 | { | |
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."); | |
1243 | bound_flag = TRUE; | |
1244 | str_val.copy_value(); | |
1245 | str_val.set_bit(bit_pos, other_value.get_bit(0)); | |
1246 | return *this; | |
1247 | } | |
1248 | ||
1249 | BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator= | |
1250 | (const BITSTRING_ELEMENT& other_value) | |
1251 | { | |
1252 | other_value.must_bound("Assignment of an unbound bitstring element."); | |
1253 | bound_flag = TRUE; | |
1254 | str_val.copy_value(); | |
1255 | str_val.set_bit(bit_pos, other_value.str_val.get_bit(other_value.bit_pos)); | |
1256 | return *this; | |
1257 | } | |
1258 | ||
1259 | boolean BITSTRING_ELEMENT::operator==(const BITSTRING& other_value) const | |
1260 | { | |
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); | |
1265 | } | |
1266 | ||
1267 | boolean BITSTRING_ELEMENT::operator== | |
1268 | (const BITSTRING_ELEMENT& other_value) const | |
1269 | { | |
1270 | must_bound("Unbound left operand of bitstring element comparison."); | |
1271 | other_value.must_bound("Unbound right operand of bitstring element " | |
1272 | "comparison."); | |
1273 | return str_val.get_bit(bit_pos) == | |
1274 | other_value.str_val.get_bit(other_value.bit_pos); | |
1275 | } | |
1276 | ||
1277 | BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING& other_value) const | |
1278 | { | |
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; | |
1291 | } | |
1292 | ret_val.clear_unused_bits(); | |
1293 | return ret_val; | |
1294 | } | |
1295 | ||
1296 | BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING_ELEMENT& other_value) | |
1297 | const | |
1298 | { | |
1299 | must_bound("Unbound left operand of bitstring element concatenation."); | |
1300 | other_value.must_bound("Unbound right operand of bitstring element " | |
1301 | "concatenation."); | |
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); | |
1305 | } | |
1306 | ||
1307 | BITSTRING BITSTRING_ELEMENT::operator~() const | |
1308 | { | |
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); | |
1312 | } | |
1313 | ||
1314 | BITSTRING BITSTRING_ELEMENT::operator&(const BITSTRING& other_value) const | |
1315 | { | |
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) ? | |
1323 | 1 : 0; | |
1324 | return BITSTRING(1, &result); | |
1325 | } | |
1326 | ||
1327 | BITSTRING BITSTRING_ELEMENT::operator& | |
1328 | (const BITSTRING_ELEMENT& other_value) const | |
1329 | { | |
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); | |
1336 | } | |
1337 | ||
1338 | BITSTRING BITSTRING_ELEMENT::operator|(const BITSTRING& other_value) const | |
1339 | { | |
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) ? | |
1347 | 1 : 0; | |
1348 | return BITSTRING(1, &result); | |
1349 | } | |
1350 | ||
1351 | BITSTRING BITSTRING_ELEMENT::operator| | |
1352 | (const BITSTRING_ELEMENT& other_value) const | |
1353 | { | |
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); | |
1360 | } | |
1361 | ||
1362 | BITSTRING BITSTRING_ELEMENT::operator^(const BITSTRING& other_value) const | |
1363 | { | |
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) ? | |
1371 | 1 : 0; | |
1372 | return BITSTRING(1, &result); | |
1373 | } | |
1374 | ||
1375 | BITSTRING BITSTRING_ELEMENT::operator^ | |
1376 | (const BITSTRING_ELEMENT& other_value) const | |
1377 | { | |
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); | |
1384 | } | |
1385 | ||
1386 | boolean BITSTRING_ELEMENT::get_bit() const | |
1387 | { | |
1388 | return str_val.get_bit(bit_pos); | |
1389 | } | |
1390 | ||
1391 | void BITSTRING_ELEMENT::log() const | |
1392 | { | |
1393 | if (bound_flag) TTCN_Logger::log_event("'%c'B", str_val.get_bit(bit_pos) ? | |
1394 | '1' : '0'); | |
1395 | else TTCN_Logger::log_event_unbound(); | |
1396 | } | |
1397 | ||
1398 | // bitstring template class | |
1399 | ||
1400 | void BITSTRING_template::clean_up() | |
1401 | { | |
1402 | switch (template_selection) { | |
1403 | case VALUE_LIST: | |
1404 | case COMPLEMENTED_LIST: | |
1405 | delete [] value_list.list_value; | |
1406 | break; | |
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 " | |
1411 | "pattern."); | |
1412 | break; | |
1413 | default: | |
1414 | break; | |
1415 | } | |
1416 | template_selection = UNINITIALIZED_TEMPLATE; | |
1417 | } | |
1418 | ||
1419 | void BITSTRING_template::copy_template(const BITSTRING_template& other_value) | |
1420 | { | |
1421 | switch (other_value.template_selection) { | |
1422 | case SPECIFIC_VALUE: | |
1423 | single_value = other_value.single_value; | |
1424 | break; | |
1425 | case OMIT_VALUE: | |
1426 | case ANY_VALUE: | |
1427 | case ANY_OR_OMIT: | |
1428 | break; | |
1429 | case VALUE_LIST: | |
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]); | |
1436 | break; | |
1437 | case STRING_PATTERN: | |
1438 | pattern_value = other_value.pattern_value; | |
1439 | pattern_value->ref_count++; | |
1440 | break; | |
1441 | default: | |
1442 | TTCN_error("Copying an uninitialized/unsupported bitstring template."); | |
1443 | } | |
1444 | set_selection(other_value); | |
1445 | } | |
1446 | ||
1447 | /* | |
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 | |
1452 | */ | |
1453 | boolean BITSTRING_template::match_pattern( | |
1454 | const bitstring_pattern_struct *string_pattern, | |
1455 | const BITSTRING::bitstring_struct *string_value) | |
1456 | { | |
1457 | if(string_pattern->n_elements == 0) return string_value->n_bits == 0; | |
1458 | ||
1459 | int value_index = 0; | |
1460 | unsigned int template_index = 0; | |
1461 | int last_asterisk = -1; | |
1462 | int last_value_to_asterisk = -1; | |
1463 | ||
1464 | for(;;) | |
1465 | { | |
1466 | switch(string_pattern->elements_ptr[template_index]) { | |
1467 | case 0: | |
1468 | if (!(string_value->bits_ptr[value_index / 8] & | |
1469 | (1 << (value_index % 8)))) | |
1470 | { | |
1471 | value_index++; | |
1472 | template_index++; | |
1473 | }else{ | |
1474 | if(last_asterisk == -1) return FALSE; | |
1475 | template_index = last_asterisk +1; | |
1476 | value_index = ++last_value_to_asterisk; | |
1477 | } | |
1478 | break; | |
1479 | case 1: | |
1480 | if (string_value->bits_ptr[value_index / 8] & (1 << (value_index % 8))) | |
1481 | { | |
1482 | value_index++; | |
1483 | template_index++; | |
1484 | }else { | |
1485 | if(last_asterisk == -1) return FALSE; | |
1486 | template_index = last_asterisk +1; | |
1487 | value_index = ++last_value_to_asterisk; | |
1488 | } | |
1489 | break; | |
1490 | case 2: | |
1491 | //we found a ? element, it matches anything | |
1492 | value_index++; | |
1493 | template_index++; | |
1494 | break; | |
1495 | case 3: | |
1496 | //we found an asterisk | |
1497 | last_asterisk = template_index++; | |
1498 | last_value_to_asterisk = value_index; | |
1499 | break; | |
1500 | default: | |
1501 | TTCN_error("Internal error: invalid element in bitstring pattern."); | |
1502 | } | |
1503 | ||
1504 | if(value_index == string_value->n_bits | |
1505 | && template_index == string_pattern->n_elements) | |
1506 | { | |
1507 | return TRUE; | |
1508 | }else if(template_index == string_pattern->n_elements) | |
1509 | { | |
1510 | if(string_pattern->elements_ptr[template_index-1] == 3) | |
1511 | { | |
1512 | return TRUE; | |
1513 | } else if (last_asterisk == -1){ | |
1514 | return FALSE; | |
1515 | } else{ | |
1516 | template_index = last_asterisk+1; | |
1517 | value_index = ++last_value_to_asterisk; | |
1518 | } | |
1519 | } else if(value_index == string_value->n_bits) | |
1520 | { | |
1521 | while(template_index < string_pattern->n_elements && | |
1522 | string_pattern->elements_ptr[template_index] == 3) | |
1523 | template_index++; | |
1524 | ||
1525 | return template_index == string_pattern->n_elements; | |
1526 | } | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | BITSTRING_template::BITSTRING_template() | |
1531 | { | |
1532 | } | |
1533 | ||
1534 | BITSTRING_template::BITSTRING_template(template_sel other_value) | |
1535 | : Restricted_Length_Template(other_value) | |
1536 | { | |
1537 | check_single_selection(other_value); | |
1538 | } | |
1539 | ||
1540 | BITSTRING_template::BITSTRING_template(const BITSTRING& other_value) | |
1541 | : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value) | |
1542 | { | |
1543 | } | |
1544 | ||
1545 | BITSTRING_template::BITSTRING_template(const BITSTRING_ELEMENT& other_value) | |
1546 | : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value) | |
1547 | { | |
1548 | } | |
1549 | ||
1550 | BITSTRING_template::~BITSTRING_template() | |
1551 | { | |
1552 | clean_up(); | |
1553 | } | |
1554 | ||
1555 | BITSTRING_template::BITSTRING_template(const OPTIONAL<BITSTRING>& other_value) | |
1556 | { | |
1557 | switch (other_value.get_selection()) { | |
1558 | case OPTIONAL_PRESENT: | |
1559 | set_selection(SPECIFIC_VALUE); | |
1560 | single_value = (const BITSTRING&)other_value; | |
1561 | break; | |
1562 | case OPTIONAL_OMIT: | |
1563 | set_selection(OMIT_VALUE); | |
1564 | break; | |
1565 | default: | |
1566 | TTCN_error("Creating a bitstring template from an unbound optional field."); | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | BITSTRING_template::BITSTRING_template(unsigned int n_elements, | |
1571 | const unsigned char *pattern_elements) | |
1572 | : Restricted_Length_Template(STRING_PATTERN) | |
1573 | { | |
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); | |
1579 | } | |
1580 | ||
1581 | BITSTRING_template::BITSTRING_template(const BITSTRING_template& other_value) | |
1582 | : Restricted_Length_Template() | |
1583 | { | |
1584 | copy_template(other_value); | |
1585 | } | |
1586 | ||
1587 | BITSTRING_template& BITSTRING_template::operator=(template_sel other_value) | |
1588 | { | |
1589 | check_single_selection(other_value); | |
1590 | clean_up(); | |
1591 | set_selection(other_value); | |
1592 | return *this; | |
1593 | } | |
1594 | ||
1595 | BITSTRING_template& BITSTRING_template::operator=(const BITSTRING& other_value) | |
1596 | { | |
1597 | other_value.must_bound("Assignment of an unbound bitstring value to a " | |
1598 | "template."); | |
1599 | clean_up(); | |
1600 | set_selection(SPECIFIC_VALUE); | |
1601 | single_value = other_value; | |
1602 | return *this; | |
1603 | } | |
1604 | ||
1605 | BITSTRING_template& BITSTRING_template::operator= | |
1606 | (const BITSTRING_ELEMENT& other_value) | |
1607 | { | |
1608 | other_value.must_bound("Assignment of an unbound bitstring element to a " | |
1609 | "template."); | |
1610 | clean_up(); | |
1611 | set_selection(SPECIFIC_VALUE); | |
1612 | single_value = other_value; | |
1613 | return *this; | |
1614 | } | |
1615 | ||
1616 | BITSTRING_template& BITSTRING_template::operator= | |
1617 | (const OPTIONAL<BITSTRING>& other_value) | |
1618 | { | |
1619 | clean_up(); | |
1620 | switch (other_value.get_selection()) { | |
1621 | case OPTIONAL_PRESENT: | |
1622 | set_selection(SPECIFIC_VALUE); | |
1623 | single_value = (const BITSTRING&)other_value; | |
1624 | break; | |
1625 | case OPTIONAL_OMIT: | |
1626 | set_selection(OMIT_VALUE); | |
1627 | break; | |
1628 | default: | |
1629 | TTCN_error("Assignment of an unbound optional field to a bitstring " | |
1630 | "template."); | |
1631 | } | |
1632 | return *this; | |
1633 | } | |
1634 | ||
1635 | BITSTRING_template& BITSTRING_template::operator= | |
1636 | (const BITSTRING_template& other_value) | |
1637 | { | |
1638 | if (&other_value != this) { | |
1639 | clean_up(); | |
1640 | copy_template(other_value); | |
1641 | } | |
1642 | return *this; | |
1643 | } | |
1644 | ||
1645 | BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value) | |
1646 | { | |
1647 | if (template_selection != SPECIFIC_VALUE || is_ifpresent) | |
1648 | TTCN_error("Accessing a bitstring element of a non-specific bitstring " | |
1649 | "template."); | |
1650 | return single_value[index_value]; | |
1651 | } | |
1652 | ||
1653 | BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value) | |
1654 | { | |
1655 | index_value.must_bound("Indexing a bitstring template with an unbound " | |
1656 | "integer value."); | |
1657 | return (*this)[(int)index_value]; | |
1658 | } | |
1659 | ||
1660 | const BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value) const | |
1661 | { | |
1662 | if (template_selection != SPECIFIC_VALUE || is_ifpresent) | |
1663 | TTCN_error("Accessing a bitstring element of a non-specific bitstring " | |
1664 | "template."); | |
1665 | return single_value[index_value]; | |
1666 | } | |
1667 | ||
1668 | const BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value) const | |
1669 | { | |
1670 | index_value.must_bound("Indexing a bitstring template with an unbound " | |
1671 | "integer value."); | |
1672 | return (*this)[(int)index_value]; | |
1673 | } | |
1674 | ||
3abe9331 | 1675 | boolean BITSTRING_template::match(const BITSTRING& other_value, |
1676 | boolean /* legacy */) const | |
970ed795 EL |
1677 | { |
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; | |
1683 | case OMIT_VALUE: | |
1684 | return FALSE; | |
1685 | case ANY_VALUE: | |
1686 | case ANY_OR_OMIT: | |
1687 | return TRUE; | |
1688 | case VALUE_LIST: | |
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); | |
1696 | default: | |
1697 | TTCN_error("Matching an uninitialized/unsupported bitstring template."); | |
1698 | } | |
1699 | return FALSE; | |
1700 | } | |
1701 | ||
1702 | const BITSTRING& BITSTRING_template::valueof() const | |
1703 | { | |
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; | |
1708 | } | |
1709 | ||
1710 | int BITSTRING_template::lengthof() const | |
1711 | { | |
1712 | int min_length; | |
1713 | boolean has_any_or_none; | |
1714 | if (is_ifpresent) | |
1715 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1716 | "which has an ifpresent attribute."); | |
1717 | switch (template_selection) | |
1718 | { | |
1719 | case SPECIFIC_VALUE: | |
1720 | min_length = single_value.lengthof(); | |
1721 | has_any_or_none = FALSE; | |
1722 | break; | |
1723 | case OMIT_VALUE: | |
1724 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1725 | "containing omit value."); | |
1726 | case ANY_VALUE: | |
1727 | case ANY_OR_OMIT: | |
1728 | min_length = 0; | |
1729 | has_any_or_none = TRUE; // max. length is infinity | |
1730 | break; | |
1731 | case VALUE_LIST: | |
1732 | { | |
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."); | |
1743 | } | |
1744 | min_length = item_length; | |
1745 | has_any_or_none = FALSE; | |
1746 | break; | |
1747 | } | |
1748 | case COMPLEMENTED_LIST: | |
1749 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1750 | "containing complemented list."); | |
1751 | case STRING_PATTERN: | |
1752 | min_length = 0; | |
1753 | has_any_or_none = FALSE; // TRUE if * chars in the pattern | |
1754 | for (unsigned int i = 0; i < pattern_value->n_elements; i++) | |
1755 | { | |
1756 | if (pattern_value->elements_ptr[i] < 3) min_length++; // case of 1, 0, ? | |
1757 | else has_any_or_none = TRUE; // case of * character | |
1758 | } | |
1759 | break; | |
1760 | default: | |
1761 | TTCN_error("Performing lengthof() operation on an " | |
1762 | "uninitialized/unsupported bitstring template."); | |
1763 | } | |
1764 | return check_section_is_single(min_length, has_any_or_none, | |
1765 | "length", "a", "bitstring template"); | |
1766 | } | |
1767 | ||
1768 | void BITSTRING_template::set_type(template_sel template_type, | |
1769 | unsigned int list_length) | |
1770 | { | |
1771 | if (template_type != VALUE_LIST && template_type != COMPLEMENTED_LIST) | |
1772 | TTCN_error("Setting an invalid list type for a bitstring template."); | |
1773 | clean_up(); | |
1774 | set_selection(template_type); | |
1775 | value_list.n_values = list_length; | |
1776 | value_list.list_value = new BITSTRING_template[list_length]; | |
1777 | } | |
1778 | ||
1779 | BITSTRING_template& BITSTRING_template::list_item(unsigned int list_index) | |
1780 | { | |
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]; | |
1787 | } | |
1788 | ||
1789 | static const char patterns[] = { '0', '1', '?', '*' }; | |
1790 | ||
1791 | void BITSTRING_template::log() const | |
1792 | { | |
1793 | switch (template_selection) { | |
1794 | case SPECIFIC_VALUE: | |
1795 | single_value.log(); | |
1796 | break; | |
1797 | case COMPLEMENTED_LIST: | |
1798 | TTCN_Logger::log_event_str("complement "); | |
1799 | // no break | |
1800 | case VALUE_LIST: | |
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(); | |
1805 | } | |
1806 | TTCN_Logger::log_char(')'); | |
1807 | break; | |
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>"); | |
1814 | } | |
1815 | TTCN_Logger::log_event_str("'B"); | |
1816 | break; | |
1817 | default: | |
1818 | log_generic(); | |
1819 | break; | |
1820 | } | |
1821 | log_restricted(); | |
1822 | log_ifpresent(); | |
1823 | } | |
1824 | ||
3abe9331 | 1825 | void BITSTRING_template::log_match(const BITSTRING& match_value, |
1826 | boolean /* legacy */) const | |
970ed795 EL |
1827 | { |
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(" := "); | |
1832 | } | |
1833 | match_value.log(); | |
1834 | TTCN_Logger::log_event_str(" with "); | |
1835 | log(); | |
1836 | if (match(match_value)) TTCN_Logger::log_event_str(" matched"); | |
1837 | else TTCN_Logger::log_event_str(" unmatched"); | |
1838 | } | |
1839 | ||
1840 | void BITSTRING_template::set_param(Module_Param& param) { | |
1841 | param.basic_check(Module_Param::BC_TEMPLATE|Module_Param::BC_LIST, "bitstring template"); | |
3abe9331 | 1842 | Module_Param_Ptr mp = ¶m; |
1843 | if (param.get_type() == Module_Param::MP_Reference) { | |
1844 | mp = param.get_referenced_param(); | |
1845 | } | |
1846 | switch (mp->get_type()) { | |
970ed795 EL |
1847 | case Module_Param::MP_Omit: |
1848 | *this = OMIT_VALUE; | |
1849 | break; | |
1850 | case Module_Param::MP_Any: | |
1851 | *this = ANY_VALUE; | |
1852 | break; | |
1853 | case Module_Param::MP_AnyOrNone: | |
1854 | *this = ANY_OR_OMIT; | |
1855 | break; | |
1856 | case Module_Param::MP_List_Template: | |
3abe9331 | 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)); | |
970ed795 | 1863 | } |
3abe9331 | 1864 | *this = temp; |
1865 | break; } | |
970ed795 | 1866 | case Module_Param::MP_Bitstring: |
3abe9331 | 1867 | *this = BITSTRING(mp->get_string_size(), (unsigned char*)mp->get_string_data()); |
970ed795 EL |
1868 | break; |
1869 | case Module_Param::MP_Bitstring_Template: | |
3abe9331 | 1870 | *this = BITSTRING_template(mp->get_string_size(), (unsigned char*)mp->get_string_data()); |
1871 | break; | |
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; | |
1878 | } | |
1879 | else { | |
1880 | param.expr_type_error("a bitstring"); | |
1881 | } | |
970ed795 EL |
1882 | break; |
1883 | default: | |
1884 | param.type_error("bitstring template"); | |
1885 | } | |
3abe9331 | 1886 | is_ifpresent = param.get_ifpresent() || mp->get_ifpresent(); |
1887 | if (param.get_length_restriction() != NULL) { | |
1888 | set_length_range(param); | |
1889 | } | |
1890 | else { | |
1891 | set_length_range(*mp); | |
1892 | } | |
1893 | } | |
1894 | ||
1895 | Module_Param* BITSTRING_template::get_param(Module_Param_Name& param_name) const | |
1896 | { | |
1897 | Module_Param* mp = NULL; | |
1898 | switch (template_selection) { | |
1899 | case UNINITIALIZED_TEMPLATE: | |
1900 | mp = new Module_Param_Unbound(); | |
1901 | break; | |
1902 | case OMIT_VALUE: | |
1903 | mp = new Module_Param_Omit(); | |
1904 | break; | |
1905 | case ANY_VALUE: | |
1906 | mp = new Module_Param_Any(); | |
1907 | break; | |
1908 | case ANY_OR_OMIT: | |
1909 | mp = new Module_Param_AnyOrNone(); | |
1910 | break; | |
1911 | case SPECIFIC_VALUE: | |
1912 | mp = single_value.get_param(param_name); | |
1913 | break; | |
1914 | case VALUE_LIST: | |
1915 | case COMPLEMENTED_LIST: { | |
1916 | if (template_selection == VALUE_LIST) { | |
1917 | mp = new Module_Param_List_Template(); | |
1918 | } | |
1919 | else { | |
1920 | mp = new Module_Param_ComplementList_Template(); | |
1921 | } | |
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)); | |
1924 | } | |
1925 | break; } | |
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); | |
1930 | break; } | |
1931 | default: | |
1932 | break; | |
1933 | } | |
1934 | if (is_ifpresent) { | |
1935 | mp->set_ifpresent(); | |
1936 | } | |
1937 | mp->set_length_restriction(get_length_range()); | |
1938 | return mp; | |
970ed795 EL |
1939 | } |
1940 | ||
1941 | void BITSTRING_template::encode_text(Text_Buf& text_buf) const | |
1942 | { | |
1943 | encode_text_restricted(text_buf); | |
1944 | switch (template_selection) { | |
1945 | case OMIT_VALUE: | |
1946 | case ANY_VALUE: | |
1947 | case ANY_OR_OMIT: | |
1948 | break; | |
1949 | case SPECIFIC_VALUE: | |
1950 | single_value.encode_text(text_buf); | |
1951 | break; | |
1952 | case VALUE_LIST: | |
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); | |
1957 | break; | |
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); | |
1961 | break; | |
1962 | default: | |
1963 | TTCN_error("Text encoder: Encoding an uninitialized/unsupported " | |
1964 | "bitstring template."); | |
1965 | } | |
1966 | } | |
1967 | ||
1968 | void BITSTRING_template::decode_text(Text_Buf& text_buf) | |
1969 | { | |
1970 | clean_up(); | |
1971 | decode_text_restricted(text_buf); | |
1972 | switch (template_selection) { | |
1973 | case OMIT_VALUE: | |
1974 | case ANY_VALUE: | |
1975 | case ANY_OR_OMIT: | |
1976 | break; | |
1977 | case SPECIFIC_VALUE: | |
1978 | single_value.decode_text(text_buf); | |
1979 | break; | |
1980 | case VALUE_LIST: | |
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); | |
1986 | break; | |
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); | |
1994 | break;} | |
1995 | default: | |
1996 | TTCN_error("Text decoder: An unknown/unsupported selection was " | |
1997 | "received for a bitstring template."); | |
1998 | } | |
1999 | } | |
2000 | ||
3abe9331 | 2001 | boolean BITSTRING_template::is_present(boolean legacy /* = FALSE */) const |
970ed795 EL |
2002 | { |
2003 | if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE; | |
3abe9331 | 2004 | return !match_omit(legacy); |
970ed795 EL |
2005 | } |
2006 | ||
3abe9331 | 2007 | boolean BITSTRING_template::match_omit(boolean legacy /* = FALSE */) const |
970ed795 EL |
2008 | { |
2009 | if (is_ifpresent) return TRUE; | |
2010 | switch (template_selection) { | |
2011 | case OMIT_VALUE: | |
2012 | case ANY_OR_OMIT: | |
2013 | return TRUE; | |
2014 | case VALUE_LIST: | |
2015 | case COMPLEMENTED_LIST: | |
3abe9331 | 2016 | if (legacy) { |
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; | |
2022 | } | |
2023 | // else fall through | |
970ed795 EL |
2024 | default: |
2025 | return FALSE; | |
2026 | } | |
2027 | return FALSE; | |
2028 | } | |
2029 | ||
2030 | #ifndef TITAN_RUNTIME_2 | |
3abe9331 | 2031 | void BITSTRING_template::check_restriction(template_res t_res, const char* t_name, |
2032 | boolean legacy /* = FALSE */) const | |
970ed795 EL |
2033 | { |
2034 | if (template_selection==UNINITIALIZED_TEMPLATE) return; | |
2035 | switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) { | |
2036 | case TR_VALUE: | |
2037 | if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return; | |
2038 | break; | |
2039 | case TR_OMIT: | |
2040 | if (!is_ifpresent && (template_selection==OMIT_VALUE || | |
2041 | template_selection==SPECIFIC_VALUE)) return; | |
2042 | break; | |
2043 | case TR_PRESENT: | |
3abe9331 | 2044 | if (!match_omit(legacy)) return; |
970ed795 EL |
2045 | break; |
2046 | default: | |
2047 | return; | |
2048 | } | |
2049 | TTCN_error("Restriction `%s' on template of type %s violated.", | |
2050 | get_res_name(t_res), t_name ? t_name : "bitstring"); | |
2051 | } | |
2052 | #else | |
2053 | int BITSTRING::RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const | |
2054 | { | |
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; | |
2061 | } | |
2062 | #endif |