Commit | Line | Data |
---|---|---|
970ed795 EL |
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 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #include <string.h> | |
9 | ||
10 | #include "Bitstring.hh" | |
11 | #include "../common/memory.h" | |
12 | #include "Integer.hh" | |
13 | #include "String_struct.hh" | |
14 | #include "Parameters.h" | |
15 | #include "Param_Types.hh" | |
16 | #include "Error.hh" | |
17 | #include "Logger.hh" | |
18 | #include "Encdec.hh" | |
19 | ||
20 | #include "../common/dbgnew.hh" | |
21 | ||
22 | // bitstring value class | |
23 | ||
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) | |
26 | ||
27 | void BITSTRING::init_struct(int n_bits) | |
28 | { | |
29 | if (n_bits < 0) { | |
30 | val_ptr = NULL; | |
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. | |
35 | */ | |
36 | static bitstring_struct empty_string = { 1, 0, "" }; | |
37 | val_ptr = &empty_string; | |
38 | empty_string.ref_count++; | |
39 | } else { | |
40 | val_ptr = (bitstring_struct*)Malloc(MEMORY_SIZE(n_bits)); | |
41 | val_ptr->ref_count = 1; | |
42 | val_ptr->n_bits = n_bits; | |
43 | } | |
44 | } | |
45 | ||
46 | boolean BITSTRING::get_bit(int bit_index) const | |
47 | { | |
48 | return val_ptr->bits_ptr[bit_index / 8] & (1 << (bit_index % 8)); | |
49 | } | |
50 | ||
51 | void BITSTRING::set_bit(int bit_index, boolean new_value) | |
52 | { | |
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; | |
56 | } | |
57 | ||
58 | void BITSTRING::copy_value() | |
59 | { | |
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; | |
65 | old_ptr->ref_count--; | |
66 | init_struct(old_ptr->n_bits); | |
67 | memcpy(val_ptr->bits_ptr, old_ptr->bits_ptr, (old_ptr->n_bits + 7) / 8); | |
68 | } | |
69 | } | |
70 | ||
71 | void BITSTRING::clear_unused_bits() const | |
72 | { | |
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); | |
76 | } | |
77 | ||
78 | BITSTRING::BITSTRING(int n_bits) | |
79 | { | |
80 | init_struct(n_bits); | |
81 | } | |
82 | ||
83 | BITSTRING::BITSTRING() | |
84 | { | |
85 | val_ptr = NULL; | |
86 | } | |
87 | ||
88 | BITSTRING::BITSTRING(int n_bits, const unsigned char *bits_ptr) | |
89 | { | |
90 | init_struct(n_bits); | |
91 | memcpy(val_ptr->bits_ptr, bits_ptr, (n_bits + 7) / 8); | |
92 | clear_unused_bits(); | |
93 | } | |
94 | ||
95 | BITSTRING::BITSTRING(const BITSTRING& other_value) | |
96 | : Base_Type(other_value) | |
97 | { | |
98 | other_value.must_bound("Copying an unbound bitstring value."); | |
99 | val_ptr = other_value.val_ptr; | |
100 | val_ptr->ref_count++; | |
101 | } | |
102 | ||
103 | BITSTRING::BITSTRING(const BITSTRING_ELEMENT& other_value) | |
104 | { | |
105 | other_value.must_bound("Copying an unbound bitstring element."); | |
106 | init_struct(1); | |
107 | val_ptr->bits_ptr[0] = other_value.get_bit() ? 1 : 0; | |
108 | } | |
109 | ||
110 | BITSTRING::~BITSTRING() | |
111 | { | |
112 | clean_up(); | |
113 | } | |
114 | ||
115 | void BITSTRING::clean_up() | |
116 | { | |
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 " | |
121 | "value."); | |
122 | val_ptr = NULL; | |
123 | } | |
124 | } | |
125 | ||
126 | BITSTRING& BITSTRING::operator=(const BITSTRING& other_value) | |
127 | { | |
128 | other_value.must_bound("Assignment of an unbound bitstring value."); | |
129 | if (&other_value != this) { | |
130 | clean_up(); | |
131 | val_ptr = other_value.val_ptr; | |
132 | val_ptr->ref_count++; | |
133 | } | |
134 | return *this; | |
135 | } | |
136 | ||
137 | BITSTRING& BITSTRING::operator=(const BITSTRING_ELEMENT& other_value) | |
138 | { | |
139 | other_value.must_bound("Assignment of an unbound bitstring element to a " | |
140 | "bitstring."); | |
141 | boolean bit_value = other_value.get_bit(); | |
142 | clean_up(); | |
143 | init_struct(1); | |
144 | val_ptr->bits_ptr[0] = bit_value ? 1 : 0; | |
145 | return *this; | |
146 | } | |
147 | ||
148 | boolean BITSTRING::operator==(const BITSTRING& other_value) const | |
149 | { | |
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; | |
155 | clear_unused_bits(); | |
156 | other_value.clear_unused_bits(); | |
157 | return !memcmp(val_ptr->bits_ptr, other_value.val_ptr->bits_ptr, | |
158 | (n_bits + 7) / 8); | |
159 | } | |
160 | ||
161 | boolean BITSTRING::operator==(const BITSTRING_ELEMENT& other_value) const | |
162 | { | |
163 | must_bound("Unbound left operand of bitstring comparison."); | |
164 | other_value.must_bound("Unbound right operand of bitstring element " | |
165 | "comparison."); | |
166 | if (val_ptr->n_bits != 1) return FALSE; | |
167 | return get_bit(0) == other_value.get_bit(); | |
168 | } | |
169 | ||
170 | BITSTRING BITSTRING::operator+(const BITSTRING& other_value) const | |
171 | { | |
172 | must_bound("Unbound left operand of bitstring concatenation."); | |
173 | other_value.must_bound("Unbound right operand of bitstring concatenation."); | |
174 | ||
175 | int left_n_bits = val_ptr->n_bits; | |
176 | if (left_n_bits == 0) return other_value; | |
177 | ||
178 | int right_n_bits = other_value.val_ptr->n_bits; | |
179 | if (right_n_bits == 0) return *this; | |
180 | ||
181 | // the length of result | |
182 | int n_bits = left_n_bits + right_n_bits; | |
183 | ||
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; | |
187 | ||
188 | // the number of bits used in the last incomplete octet of the left operand | |
189 | int left_empty_bits = left_n_bits % 8; | |
190 | ||
191 | // the result | |
192 | BITSTRING ret_val(n_bits); | |
193 | ||
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; | |
198 | ||
199 | // copying the left fragment into the result | |
200 | memcpy(dest_ptr, left_ptr, left_n_bytes); | |
201 | ||
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); | |
213 | } | |
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; | |
219 | } | |
220 | } else { | |
221 | // trivial case: just append the bytes of the right fragment | |
222 | memcpy(dest_ptr + left_n_bytes, right_ptr, right_n_bytes); | |
223 | } | |
224 | ret_val.clear_unused_bits(); | |
225 | return ret_val; | |
226 | } | |
227 | ||
228 | BITSTRING BITSTRING::operator+(const BITSTRING_ELEMENT& other_value) const | |
229 | { | |
230 | must_bound("Unbound left operand of bitstring concatenation."); | |
231 | other_value.must_bound("Unbound right operand of bitstring element " | |
232 | "concatenation."); | |
233 | ||
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()); | |
238 | return ret_val; | |
239 | } | |
240 | ||
241 | BITSTRING BITSTRING::operator~() const | |
242 | { | |
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(); | |
250 | return ret_val; | |
251 | } | |
252 | ||
253 | BITSTRING BITSTRING::operator&(const BITSTRING& other_value) const | |
254 | { | |
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 " | |
257 | "bitstring value."); | |
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 " | |
261 | "same length."); | |
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(); | |
269 | return ret_val; | |
270 | } | |
271 | ||
272 | BITSTRING BITSTRING::operator&(const BITSTRING_ELEMENT& other_value) const | |
273 | { | |
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); | |
282 | } | |
283 | ||
284 | BITSTRING BITSTRING::operator|(const BITSTRING& other_value) const | |
285 | { | |
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 " | |
288 | "bitstring value."); | |
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 " | |
292 | "same length."); | |
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(); | |
300 | return ret_val; | |
301 | } | |
302 | ||
303 | BITSTRING BITSTRING::operator|(const BITSTRING_ELEMENT& other_value) const | |
304 | { | |
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); | |
313 | } | |
314 | ||
315 | BITSTRING BITSTRING::operator^(const BITSTRING& other_value) const | |
316 | { | |
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 " | |
319 | "bitstring value."); | |
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 " | |
323 | "same length."); | |
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(); | |
331 | return ret_val; | |
332 | } | |
333 | ||
334 | BITSTRING BITSTRING::operator^(const BITSTRING_ELEMENT& other_value) const | |
335 | { | |
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); | |
344 | } | |
345 | ||
346 | BITSTRING BITSTRING::operator<<(int shift_count) const | |
347 | { | |
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; | |
354 | clear_unused_bits(); | |
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) { | |
359 | int byte_count = 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] << | |
364 | (8 - shift_bits)); | |
365 | } | |
366 | ret_val.val_ptr->bits_ptr[n_bytes - shift_bytes - 1] = | |
367 | val_ptr->bits_ptr[n_bytes - 1] >> shift_bits; | |
368 | } else { | |
369 | memcpy(ret_val.val_ptr->bits_ptr, val_ptr->bits_ptr + shift_bytes, | |
370 | n_bytes - shift_bytes); | |
371 | } | |
372 | memset(ret_val.val_ptr->bits_ptr + n_bytes - shift_bytes, 0, | |
373 | shift_bytes); | |
374 | ret_val.clear_unused_bits(); | |
375 | return ret_val; | |
376 | } else if (shift_count == 0) return *this; | |
377 | else return *this >> (-shift_count); | |
378 | } | |
379 | ||
380 | BITSTRING BITSTRING::operator<<(const INTEGER& shift_count) const | |
381 | { | |
382 | shift_count.must_bound("Unbound right operand of bitstring shift left " | |
383 | "operator."); | |
384 | return *this << (int)shift_count; | |
385 | } | |
386 | ||
387 | BITSTRING BITSTRING::operator>>(int shift_count) const | |
388 | { | |
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; | |
395 | clear_unused_bits(); | |
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++) | |
403 | { | |
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); | |
407 | } | |
408 | } else { | |
409 | memcpy(ret_val.val_ptr->bits_ptr + shift_bytes, val_ptr->bits_ptr, | |
410 | n_bytes - shift_bytes); | |
411 | } | |
412 | ret_val.clear_unused_bits(); | |
413 | return ret_val; | |
414 | } else if (shift_count == 0) return *this; | |
415 | else return *this << (-shift_count); | |
416 | } | |
417 | ||
418 | BITSTRING BITSTRING::operator>>(const INTEGER& shift_count) const | |
419 | { | |
420 | shift_count.must_bound("Unbound right operand of bitstring shift right " | |
421 | "operator."); | |
422 | return *this >> (int)shift_count; | |
423 | } | |
424 | ||
425 | BITSTRING BITSTRING::operator<<=(int rotate_count) const | |
426 | { | |
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); | |
436 | } | |
437 | ||
438 | BITSTRING BITSTRING::operator<<=(const INTEGER& rotate_count) const | |
439 | { | |
440 | rotate_count.must_bound("Unbound right operand of bitstring rotate left " | |
441 | "operator."); | |
442 | return *this <<= (int)rotate_count; | |
443 | } | |
444 | ||
445 | BITSTRING BITSTRING::operator>>=(int rotate_count) const | |
446 | { | |
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); | |
456 | } | |
457 | ||
458 | BITSTRING BITSTRING::operator>>=(const INTEGER& rotate_count) const | |
459 | { | |
460 | rotate_count.must_bound("Unbound right operand of bitstring rotate right " | |
461 | "operator."); | |
462 | return *this >>= (int)rotate_count; | |
463 | } | |
464 | ||
465 | BITSTRING_ELEMENT BITSTRING::operator[](int index_value) | |
466 | { | |
467 | if (val_ptr == NULL && index_value == 0) { | |
468 | init_struct(1); | |
469 | clear_unused_bits(); | |
470 | return BITSTRING_ELEMENT(FALSE, *this, 0); | |
471 | } else { | |
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)); | |
483 | val_ptr->n_bits++; | |
484 | } else { | |
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); | |
489 | } | |
490 | clear_unused_bits(); | |
491 | return BITSTRING_ELEMENT(FALSE, *this, index_value); | |
492 | } else return BITSTRING_ELEMENT(TRUE, *this, index_value); | |
493 | } | |
494 | } | |
495 | ||
496 | BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value) | |
497 | { | |
498 | index_value.must_bound("Indexing a bitstring value with an unbound integer " | |
499 | "value."); | |
500 | return (*this)[(int)index_value]; | |
501 | } | |
502 | ||
503 | const BITSTRING_ELEMENT BITSTRING::operator[](int index_value) const | |
504 | { | |
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); | |
512 | } | |
513 | ||
514 | const BITSTRING_ELEMENT BITSTRING::operator[](const INTEGER& index_value) const | |
515 | { | |
516 | index_value.must_bound("Indexing a bitstring value with an unbound integer " | |
517 | "value."); | |
518 | return (*this)[(int)index_value]; | |
519 | } | |
520 | ||
521 | int BITSTRING::lengthof() const | |
522 | { | |
523 | must_bound("Getting the length of an unbound bitstring value."); | |
524 | return val_ptr->n_bits; | |
525 | } | |
526 | ||
527 | BITSTRING::operator const unsigned char*() const | |
528 | { | |
529 | must_bound("Casting an unbound bitstring value to const unsigned char*."); | |
530 | return val_ptr->bits_ptr; | |
531 | } | |
532 | ||
533 | void BITSTRING::log() const | |
534 | { | |
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(); | |
541 | } | |
542 | ||
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: | |
548 | clean_up(); | |
549 | init_struct(param.get_string_size()); | |
550 | memcpy(val_ptr->bits_ptr, param.get_string_data(), (val_ptr->n_bits + 7) / 8); | |
551 | clear_unused_bits(); | |
552 | break; | |
553 | case Module_Param::OT_CONCAT: | |
554 | if (is_bound()) { | |
555 | *this = *this + BITSTRING(param.get_string_size(), (unsigned char*)param.get_string_data()); | |
556 | } else { | |
557 | *this = BITSTRING(param.get_string_size(), (unsigned char*)param.get_string_data()); | |
558 | } | |
559 | break; | |
560 | default: | |
561 | TTCN_error("Internal error: BITSTRING::set_param()"); | |
562 | } | |
563 | } | |
564 | ||
565 | void BITSTRING::encode_text(Text_Buf& text_buf) const | |
566 | { | |
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); | |
571 | } | |
572 | ||
573 | void BITSTRING::decode_text(Text_Buf& text_buf) | |
574 | { | |
575 | int n_bits = text_buf.pull_int().get_val(); | |
576 | if (n_bits < 0) | |
577 | TTCN_error("Text decoder: Invalid length was received for a bitstring."); | |
578 | clean_up(); | |
579 | init_struct(n_bits); | |
580 | if (n_bits > 0) { | |
581 | text_buf.pull_raw((n_bits + 7) / 8, val_ptr->bits_ptr); | |
582 | clear_unused_bits(); | |
583 | } | |
584 | } | |
585 | ||
586 | void BITSTRING::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
587 | TTCN_EncDec::coding_t p_coding, ...) const | |
588 | { | |
589 | va_list pvar; | |
590 | va_start(pvar, p_coding); | |
591 | switch(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); | |
599 | break;} | |
600 | case TTCN_EncDec::CT_RAW: { | |
601 | TTCN_EncDec_ErrorContext ec("While RAW-encoding type '%s': ", p_td.name); | |
602 | if(!p_td.raw) | |
603 | TTCN_EncDec_ErrorContext::error_internal | |
604 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
605 | RAW_enc_tr_pos rp; | |
606 | rp.level=0; | |
607 | rp.pos=NULL; | |
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); | |
611 | break;} | |
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); | |
af710487 | 615 | XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0); |
970ed795 EL |
616 | break;} |
617 | case TTCN_EncDec::CT_JSON: { | |
af710487 | 618 | TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name); |
970ed795 EL |
619 | if(!p_td.json) |
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()); | |
625 | break;} | |
626 | default: | |
627 | TTCN_error("Unknown coding method requested to encode type '%s'", | |
628 | p_td.name); | |
629 | } | |
630 | va_end(pvar); | |
631 | } | |
632 | ||
633 | void BITSTRING::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
634 | TTCN_EncDec::coding_t p_coding, ...) | |
635 | { | |
636 | va_list pvar; | |
637 | va_start(pvar, p_coding); | |
638 | switch(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); | |
642 | ASN_BER_TLV_t tlv; | |
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()); | |
646 | break;} | |
647 | case TTCN_EncDec::CT_RAW: { | |
648 | TTCN_EncDec_ErrorContext ec("While RAW-decoding type '%s': ", p_td.name); | |
649 | if(!p_td.raw) | |
650 | TTCN_EncDec_ErrorContext::error_internal | |
651 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
652 | raw_order_t order; | |
653 | switch(p_td.raw->top_bit_order){ | |
654 | case TOP_BIT_LEFT: | |
655 | order=ORDER_LSB; | |
656 | break; | |
657 | case TOP_BIT_RIGHT: | |
658 | default: | |
659 | order=ORDER_MSB; | |
660 | } | |
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" | |
665 | , p_td.name); | |
666 | break;} | |
667 | case TTCN_EncDec::CT_XER: { | |
af710487 | 668 | TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name); |
970ed795 EL |
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) | |
675 | break; | |
676 | } | |
af710487 | 677 | XER_decode(*p_td.xer, reader, XER_coding, 0); |
970ed795 EL |
678 | size_t bytes = reader.ByteConsumed(); |
679 | p_buf.set_pos(bytes); | |
680 | break;} | |
681 | case TTCN_EncDec::CT_JSON: { | |
682 | TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name); | |
683 | if(!p_td.json) | |
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" | |
691 | , p_td.name); | |
692 | p_buf.set_pos(tok.get_buf_pos()); | |
693 | break;} | |
694 | default: | |
695 | TTCN_error("Unknown coding method requested to decode type '%s'", | |
696 | p_td.name); | |
697 | } | |
698 | va_end(pvar); | |
699 | } | |
700 | ||
701 | void BITSTRING::BER_encode_putbits(unsigned char *target, | |
702 | unsigned int bitnum_start, | |
703 | unsigned int bit_count) const | |
704 | { | |
705 | unsigned int nof_bits, nof_octets, i, j; | |
706 | unsigned char c; | |
707 | ||
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; | |
714 | if(!nof_octets) { | |
715 | target[0]=0x00; | |
716 | return; | |
717 | } | |
718 | target[0]=(unsigned char)(nof_octets*8-bit_count); | |
719 | for(i=0; i<nof_octets-1; i++) { | |
720 | c=0; | |
721 | for(j=0; j<8; j++) { | |
722 | c<<=1; | |
723 | if(get_bit(bitnum_start+8*i+j)) c|=0x01; | |
724 | } | |
725 | target[1+i]=c; | |
726 | } // for | |
727 | c=0; | |
728 | for(j=0; j<8; j++) { | |
729 | c<<=1; | |
730 | if(8*i+j<bit_count) | |
731 | if(get_bit(bitnum_start+8*i+j)) c|=0x01; | |
732 | } | |
733 | target[1+i]=c; | |
734 | } | |
735 | ||
736 | ASN_BER_TLV_t* | |
737 | BITSTRING::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, | |
738 | unsigned p_coding) const | |
739 | { | |
740 | BER_chk_descr(p_td); | |
741 | ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound()); | |
742 | if(!new_tlv) { | |
743 | unsigned char *V_ptr; | |
744 | size_t V_len; | |
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; | |
751 | } | |
752 | else /*if(coding==BER_ENCODE_DER)*/ { | |
753 | nof_fragments=1; | |
754 | } | |
755 | ||
756 | boolean is_constructed=nof_fragments>1; | |
757 | if(!is_constructed) { | |
758 | V_len=nof_octets+1; | |
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); | |
762 | } | |
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; | |
767 | /* | |
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); | |
772 | */ | |
773 | V_len=999; | |
774 | unsigned int nof_bits_curr=8*999; | |
775 | for(unsigned int i=0; i<nof_fragments; i++) { | |
776 | if(i==nof_fragments-1) { | |
777 | V_len=rest_octets; | |
778 | nof_bits_curr=nof_bits-i*8*999; | |
779 | } | |
780 | V_ptr=(unsigned char*)Malloc(V_len+1); // because of unused bits-octet | |
781 | /* | |
782 | V_ptr[0]=0x03; | |
783 | V_ptr[1]=0x82; | |
784 | V_ptr[2]=0x03; | |
785 | V_ptr[3]=0xE8; | |
786 | */ | |
787 | BER_encode_putbits(V_ptr, i*8*999, nof_bits_curr); | |
788 | /* | |
789 | V_ptr+=1004; | |
790 | */ | |
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); | |
794 | } | |
795 | /* | |
796 | V_ptr[0]=0x03; | |
797 | if(rest_octets<128) { | |
798 | V_ptr[1]=(rest_octets+1) & '\x7F'; | |
799 | V_ptr+=2; | |
800 | } | |
801 | else { | |
802 | V_ptr[1]=0x82; | |
803 | V_ptr[2]=((rest_octets+1)/256) & 0xFF; | |
804 | V_ptr[3]=(rest_octets+1) & 0xFF; | |
805 | V_ptr+=4; | |
806 | } | |
807 | BER_encode_putbits(V_ptr, i*8*999, nof_bits-i*8*999); | |
808 | */ | |
809 | } | |
810 | } | |
811 | new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding); | |
812 | return new_tlv; | |
813 | } | |
814 | ||
815 | void BITSTRING::BER_decode_getbits(const unsigned char *source, | |
816 | size_t s_len, unsigned int& bitnum_start) | |
817 | { | |
818 | unsigned int i, j; | |
819 | unsigned char c; | |
820 | if(s_len<1) { | |
821 | TTCN_EncDec_ErrorContext::error | |
822 | (TTCN_EncDec::ET_INVAL_MSG, "Length of V-part of bitstring" | |
823 | " cannot be 0."); | |
824 | return; | |
825 | } | |
826 | unsigned int nof_octets=s_len-1; | |
827 | unsigned int nof_restbits=8-source[0]; | |
828 | if(nof_octets==0) { | |
829 | if(nof_restbits!=8) | |
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].", | |
834 | source[0]); | |
835 | return; | |
836 | } | |
837 | if(source[0]>7) { | |
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].", | |
841 | source[0]); | |
842 | nof_restbits=1; | |
843 | } | |
844 | // And what about overflow? :) | |
845 | i = (nof_octets - 1) * 8 + nof_restbits; | |
846 | if (i > 0) { | |
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); | |
852 | } else { | |
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; | |
857 | } | |
858 | } | |
859 | for(i=0; i<nof_octets-1; i++) { | |
860 | c=source[1+i]; | |
861 | for(j=0; j<8; j++) { | |
862 | set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE); | |
863 | c<<=1; | |
864 | } | |
865 | } | |
866 | c=source[1+i]; | |
867 | for(j=0; j<nof_restbits; j++) { | |
868 | set_bit(bitnum_start+8*i+j, c & 0x80?TRUE:FALSE); | |
869 | c<<=1; | |
870 | } | |
871 | bitnum_start+=(nof_octets-1)*8+nof_restbits; | |
872 | } | |
873 | ||
874 | void BITSTRING::BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form, | |
875 | unsigned int& bitnum_start) | |
876 | { | |
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); | |
880 | } | |
881 | else { // is constructed | |
882 | ASN_BER_TLV_t tlv2; | |
883 | size_t V_pos=0; | |
884 | boolean doit=TRUE; | |
885 | while(doit) { | |
886 | if(!ASN_BER_str2TLV(p_tlv.V.str.Vlen-V_pos, p_tlv.V.str.Vstr+V_pos, | |
887 | tlv2, L_form)) { | |
888 | TTCN_EncDec_ErrorContext::error | |
889 | (TTCN_EncDec::ET_INCOMPL_MSG, | |
890 | "Incomplete TLV in a constructed BITSTRING TLV."); | |
891 | return; | |
892 | } | |
893 | if(!p_tlv.isLenDefinite && tlv2.tagnumber==0 | |
894 | && tlv2.tagclass==ASN_TAG_UNIV) | |
895 | doit=FALSE; // End-of-contents | |
896 | if(doit) { | |
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; | |
902 | } | |
903 | } // while(doit) | |
904 | } // else / is constructed | |
905 | } | |
906 | ||
907 | boolean BITSTRING::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, | |
908 | const ASN_BER_TLV_t& p_tlv, | |
909 | unsigned L_form) | |
910 | { | |
911 | clean_up(); | |
912 | BER_chk_descr(p_td); | |
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: "); | |
916 | init_struct(0); | |
917 | unsigned int bitnum_start = 0; | |
918 | BER_decode_TLV_(stripped_tlv, L_form, bitnum_start); | |
919 | return TRUE; | |
920 | } | |
921 | ||
922 | int BITSTRING::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const | |
923 | { | |
924 | if (!is_bound()) { | |
925 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
926 | "Encoding an unbound value."); | |
927 | } | |
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; | |
934 | align_length = 0; | |
935 | } | |
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; | |
941 | bool orders = false; | |
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; | |
945 | orders = false; | |
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; | |
949 | ||
950 | if (p_td.raw->endianness == ORDER_MSB) myleaf.align = align_length; | |
951 | else myleaf.align = -align_length; | |
952 | ||
953 | return myleaf.length = bl + align_length; | |
954 | } | |
955 | ||
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*/) | |
959 | { | |
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; | |
971 | } | |
972 | clean_up(); | |
973 | init_struct(decode_length); | |
974 | RAW_coding_par cp; | |
975 | bool orders = false; | |
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; | |
979 | orders = false; | |
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; | |
995 | if (b < maxindex) { | |
996 | val_ptr->bits_ptr[a] = val_ptr->bits_ptr[b + 1] << (8 - bound); | |
997 | } | |
998 | } | |
999 | } | |
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)); | |
1003 | } | |
1004 | } | |
1005 | decode_length += buff.increase_pos_padd(p_td.raw->padding); | |
1006 | clear_unused_bits(); | |
1007 | return decode_length + prepaddlength; | |
1008 | } | |
1009 | ||
1010 | int BITSTRING::XER_encode(const XERdescriptor_t& p_td, | |
af710487 | 1011 | TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const |
970ed795 EL |
1012 | { |
1013 | if(!is_bound()) { | |
1014 | TTCN_EncDec_ErrorContext::error | |
1015 | (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound bitstring value."); | |
1016 | } | |
1017 | ||
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 | |
1022 | ||
1023 | begin_xml(p_td, p_buf, flavor, indent, empty_element); | |
1024 | ||
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'); | |
1028 | } | |
1029 | ||
1030 | end_xml(p_td, p_buf, flavor, indent, empty_element); | |
1031 | return (int)p_buf.get_len() - encoded_length; | |
1032 | } | |
1033 | ||
1034 | int BITSTRING::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, | |
af710487 | 1035 | unsigned int flavor, embed_values_dec_struct_t*) |
970ed795 EL |
1036 | { |
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)); | |
1040 | ||
1041 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { | |
1042 | const char * name = verify_name(reader, p_td, exer); | |
1043 | (void)name; | |
1044 | } | |
1045 | else if (own_tag) { | |
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()) { | |
1052 | init_struct(0); | |
1053 | reader.Read(); | |
1054 | return 1; | |
1055 | } | |
1056 | } | |
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); | |
1061 | init_struct(0); | |
1062 | reader.Read(); | |
1063 | return 1; | |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | ||
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)) { | |
1076 | clean_up(); | |
1077 | return -1; | |
1078 | } else { | |
1079 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG, | |
1080 | "The bitstring value may only contain ones and zeros."); | |
1081 | } | |
1082 | } | |
1083 | set_bit(i, value[i] - '0'); | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { | |
1088 | // Let the caller do reader.AdvanceAttribute(); | |
1089 | } | |
1090 | else if (own_tag) { | |
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 | |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | return 1; | |
1101 | } | |
1102 | ||
1103 | int BITSTRING::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok) const | |
1104 | { | |
1105 | if (!is_bound()) { | |
1106 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
1107 | "Encoding an unbound bitstring value."); | |
1108 | return -1; | |
1109 | } | |
1110 | ||
1111 | char* tmp_str = (char*)Malloc(val_ptr->n_bits + 3); | |
1112 | tmp_str[0] = '\"'; | |
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'; | |
1116 | } | |
1117 | tmp_str[val_ptr->n_bits + 2] = 0; | |
1118 | int enc_len = p_tok.put_next_token(JSON_TOKEN_STRING, tmp_str); | |
1119 | Free(tmp_str); | |
1120 | return enc_len; | |
1121 | } | |
1122 | ||
1123 | int BITSTRING::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent) | |
1124 | { | |
1125 | json_token_t token = JSON_TOKEN_NONE; | |
1126 | char* value = 0; | |
1127 | size_t value_len = 0; | |
1128 | boolean error = false; | |
1129 | int dec_len = 0; | |
1130 | boolean use_default = p_td.json->default_value && 0 == p_tok.get_buffer_length(); | |
1131 | if (use_default) { | |
1132 | // No JSON data in the buffer -> use default value | |
1133 | value = (char*)p_td.json->default_value; | |
1134 | value_len = strlen(value); | |
1135 | } else { | |
1136 | dec_len = p_tok.get_next_token(&token, &value, &value_len); | |
1137 | } | |
1138 | if (JSON_TOKEN_ERROR == token) { | |
1139 | JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, ""); | |
1140 | return JSON_ERROR_FATAL; | |
1141 | } | |
1142 | else if (JSON_TOKEN_STRING == token || use_default) { | |
1143 | if (use_default || (value_len > 2 && value[0] == '\"' && value[value_len - 1] == '\"')) { | |
1144 | if (!use_default) { | |
1145 | // The default value doesn't have quotes around it | |
1146 | value_len -= 2; | |
1147 | ++value; | |
1148 | } | |
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'); | |
1153 | } else { | |
1154 | error = true; | |
1155 | break; | |
1156 | } | |
1157 | } | |
1158 | } else { | |
1159 | error = true; | |
1160 | } | |
1161 | } else { | |
1162 | return JSON_ERROR_INVALID_TOKEN; | |
1163 | } | |
1164 | ||
1165 | if (error) { | |
1166 | JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_FORMAT_ERROR, "string", "bitstring"); | |
1167 | if (p_silent) { | |
1168 | clean_up(); | |
1169 | } | |
1170 | return JSON_ERROR_FATAL; | |
1171 | } | |
1172 | return dec_len; | |
1173 | } | |
1174 | ||
1175 | ||
1176 | // bitstring element class | |
1177 | ||
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) | |
1181 | { | |
1182 | } | |
1183 | ||
1184 | BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator=(const BITSTRING& other_value) | |
1185 | { | |
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."); | |
1190 | bound_flag = TRUE; | |
1191 | str_val.copy_value(); | |
1192 | str_val.set_bit(bit_pos, other_value.get_bit(0)); | |
1193 | return *this; | |
1194 | } | |
1195 | ||
1196 | BITSTRING_ELEMENT& BITSTRING_ELEMENT::operator= | |
1197 | (const BITSTRING_ELEMENT& other_value) | |
1198 | { | |
1199 | other_value.must_bound("Assignment of an unbound bitstring element."); | |
1200 | bound_flag = TRUE; | |
1201 | str_val.copy_value(); | |
1202 | str_val.set_bit(bit_pos, other_value.str_val.get_bit(other_value.bit_pos)); | |
1203 | return *this; | |
1204 | } | |
1205 | ||
1206 | boolean BITSTRING_ELEMENT::operator==(const BITSTRING& other_value) const | |
1207 | { | |
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); | |
1212 | } | |
1213 | ||
1214 | boolean BITSTRING_ELEMENT::operator== | |
1215 | (const BITSTRING_ELEMENT& other_value) const | |
1216 | { | |
1217 | must_bound("Unbound left operand of bitstring element comparison."); | |
1218 | other_value.must_bound("Unbound right operand of bitstring element " | |
1219 | "comparison."); | |
1220 | return str_val.get_bit(bit_pos) == | |
1221 | other_value.str_val.get_bit(other_value.bit_pos); | |
1222 | } | |
1223 | ||
1224 | BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING& other_value) const | |
1225 | { | |
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; | |
1238 | } | |
1239 | ret_val.clear_unused_bits(); | |
1240 | return ret_val; | |
1241 | } | |
1242 | ||
1243 | BITSTRING BITSTRING_ELEMENT::operator+(const BITSTRING_ELEMENT& other_value) | |
1244 | const | |
1245 | { | |
1246 | must_bound("Unbound left operand of bitstring element concatenation."); | |
1247 | other_value.must_bound("Unbound right operand of bitstring element " | |
1248 | "concatenation."); | |
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); | |
1252 | } | |
1253 | ||
1254 | BITSTRING BITSTRING_ELEMENT::operator~() const | |
1255 | { | |
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); | |
1259 | } | |
1260 | ||
1261 | BITSTRING BITSTRING_ELEMENT::operator&(const BITSTRING& other_value) const | |
1262 | { | |
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) ? | |
1270 | 1 : 0; | |
1271 | return BITSTRING(1, &result); | |
1272 | } | |
1273 | ||
1274 | BITSTRING BITSTRING_ELEMENT::operator& | |
1275 | (const BITSTRING_ELEMENT& other_value) const | |
1276 | { | |
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); | |
1283 | } | |
1284 | ||
1285 | BITSTRING BITSTRING_ELEMENT::operator|(const BITSTRING& other_value) const | |
1286 | { | |
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) ? | |
1294 | 1 : 0; | |
1295 | return BITSTRING(1, &result); | |
1296 | } | |
1297 | ||
1298 | BITSTRING BITSTRING_ELEMENT::operator| | |
1299 | (const BITSTRING_ELEMENT& other_value) const | |
1300 | { | |
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); | |
1307 | } | |
1308 | ||
1309 | BITSTRING BITSTRING_ELEMENT::operator^(const BITSTRING& other_value) const | |
1310 | { | |
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) ? | |
1318 | 1 : 0; | |
1319 | return BITSTRING(1, &result); | |
1320 | } | |
1321 | ||
1322 | BITSTRING BITSTRING_ELEMENT::operator^ | |
1323 | (const BITSTRING_ELEMENT& other_value) const | |
1324 | { | |
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); | |
1331 | } | |
1332 | ||
1333 | boolean BITSTRING_ELEMENT::get_bit() const | |
1334 | { | |
1335 | return str_val.get_bit(bit_pos); | |
1336 | } | |
1337 | ||
1338 | void BITSTRING_ELEMENT::log() const | |
1339 | { | |
1340 | if (bound_flag) TTCN_Logger::log_event("'%c'B", str_val.get_bit(bit_pos) ? | |
1341 | '1' : '0'); | |
1342 | else TTCN_Logger::log_event_unbound(); | |
1343 | } | |
1344 | ||
1345 | // bitstring template class | |
1346 | ||
1347 | void BITSTRING_template::clean_up() | |
1348 | { | |
1349 | switch (template_selection) { | |
1350 | case VALUE_LIST: | |
1351 | case COMPLEMENTED_LIST: | |
1352 | delete [] value_list.list_value; | |
1353 | break; | |
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 " | |
1358 | "pattern."); | |
1359 | break; | |
1360 | default: | |
1361 | break; | |
1362 | } | |
1363 | template_selection = UNINITIALIZED_TEMPLATE; | |
1364 | } | |
1365 | ||
1366 | void BITSTRING_template::copy_template(const BITSTRING_template& other_value) | |
1367 | { | |
1368 | switch (other_value.template_selection) { | |
1369 | case SPECIFIC_VALUE: | |
1370 | single_value = other_value.single_value; | |
1371 | break; | |
1372 | case OMIT_VALUE: | |
1373 | case ANY_VALUE: | |
1374 | case ANY_OR_OMIT: | |
1375 | break; | |
1376 | case VALUE_LIST: | |
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]); | |
1383 | break; | |
1384 | case STRING_PATTERN: | |
1385 | pattern_value = other_value.pattern_value; | |
1386 | pattern_value->ref_count++; | |
1387 | break; | |
1388 | default: | |
1389 | TTCN_error("Copying an uninitialized/unsupported bitstring template."); | |
1390 | } | |
1391 | set_selection(other_value); | |
1392 | } | |
1393 | ||
1394 | /* | |
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 | |
1399 | */ | |
1400 | boolean BITSTRING_template::match_pattern( | |
1401 | const bitstring_pattern_struct *string_pattern, | |
1402 | const BITSTRING::bitstring_struct *string_value) | |
1403 | { | |
1404 | if(string_pattern->n_elements == 0) return string_value->n_bits == 0; | |
1405 | ||
1406 | int value_index = 0; | |
1407 | unsigned int template_index = 0; | |
1408 | int last_asterisk = -1; | |
1409 | int last_value_to_asterisk = -1; | |
1410 | ||
1411 | for(;;) | |
1412 | { | |
1413 | switch(string_pattern->elements_ptr[template_index]) { | |
1414 | case 0: | |
1415 | if (!(string_value->bits_ptr[value_index / 8] & | |
1416 | (1 << (value_index % 8)))) | |
1417 | { | |
1418 | value_index++; | |
1419 | template_index++; | |
1420 | }else{ | |
1421 | if(last_asterisk == -1) return FALSE; | |
1422 | template_index = last_asterisk +1; | |
1423 | value_index = ++last_value_to_asterisk; | |
1424 | } | |
1425 | break; | |
1426 | case 1: | |
1427 | if (string_value->bits_ptr[value_index / 8] & (1 << (value_index % 8))) | |
1428 | { | |
1429 | value_index++; | |
1430 | template_index++; | |
1431 | }else { | |
1432 | if(last_asterisk == -1) return FALSE; | |
1433 | template_index = last_asterisk +1; | |
1434 | value_index = ++last_value_to_asterisk; | |
1435 | } | |
1436 | break; | |
1437 | case 2: | |
1438 | //we found a ? element, it matches anything | |
1439 | value_index++; | |
1440 | template_index++; | |
1441 | break; | |
1442 | case 3: | |
1443 | //we found an asterisk | |
1444 | last_asterisk = template_index++; | |
1445 | last_value_to_asterisk = value_index; | |
1446 | break; | |
1447 | default: | |
1448 | TTCN_error("Internal error: invalid element in bitstring pattern."); | |
1449 | } | |
1450 | ||
1451 | if(value_index == string_value->n_bits | |
1452 | && template_index == string_pattern->n_elements) | |
1453 | { | |
1454 | return TRUE; | |
1455 | }else if(template_index == string_pattern->n_elements) | |
1456 | { | |
1457 | if(string_pattern->elements_ptr[template_index-1] == 3) | |
1458 | { | |
1459 | return TRUE; | |
1460 | } else if (last_asterisk == -1){ | |
1461 | return FALSE; | |
1462 | } else{ | |
1463 | template_index = last_asterisk+1; | |
1464 | value_index = ++last_value_to_asterisk; | |
1465 | } | |
1466 | } else if(value_index == string_value->n_bits) | |
1467 | { | |
1468 | while(template_index < string_pattern->n_elements && | |
1469 | string_pattern->elements_ptr[template_index] == 3) | |
1470 | template_index++; | |
1471 | ||
1472 | return template_index == string_pattern->n_elements; | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | BITSTRING_template::BITSTRING_template() | |
1478 | { | |
1479 | } | |
1480 | ||
1481 | BITSTRING_template::BITSTRING_template(template_sel other_value) | |
1482 | : Restricted_Length_Template(other_value) | |
1483 | { | |
1484 | check_single_selection(other_value); | |
1485 | } | |
1486 | ||
1487 | BITSTRING_template::BITSTRING_template(const BITSTRING& other_value) | |
1488 | : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value) | |
1489 | { | |
1490 | } | |
1491 | ||
1492 | BITSTRING_template::BITSTRING_template(const BITSTRING_ELEMENT& other_value) | |
1493 | : Restricted_Length_Template(SPECIFIC_VALUE), single_value(other_value) | |
1494 | { | |
1495 | } | |
1496 | ||
1497 | BITSTRING_template::~BITSTRING_template() | |
1498 | { | |
1499 | clean_up(); | |
1500 | } | |
1501 | ||
1502 | BITSTRING_template::BITSTRING_template(const OPTIONAL<BITSTRING>& other_value) | |
1503 | { | |
1504 | switch (other_value.get_selection()) { | |
1505 | case OPTIONAL_PRESENT: | |
1506 | set_selection(SPECIFIC_VALUE); | |
1507 | single_value = (const BITSTRING&)other_value; | |
1508 | break; | |
1509 | case OPTIONAL_OMIT: | |
1510 | set_selection(OMIT_VALUE); | |
1511 | break; | |
1512 | default: | |
1513 | TTCN_error("Creating a bitstring template from an unbound optional field."); | |
1514 | } | |
1515 | } | |
1516 | ||
1517 | BITSTRING_template::BITSTRING_template(unsigned int n_elements, | |
1518 | const unsigned char *pattern_elements) | |
1519 | : Restricted_Length_Template(STRING_PATTERN) | |
1520 | { | |
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); | |
1526 | } | |
1527 | ||
1528 | BITSTRING_template::BITSTRING_template(const BITSTRING_template& other_value) | |
1529 | : Restricted_Length_Template() | |
1530 | { | |
1531 | copy_template(other_value); | |
1532 | } | |
1533 | ||
1534 | BITSTRING_template& BITSTRING_template::operator=(template_sel other_value) | |
1535 | { | |
1536 | check_single_selection(other_value); | |
1537 | clean_up(); | |
1538 | set_selection(other_value); | |
1539 | return *this; | |
1540 | } | |
1541 | ||
1542 | BITSTRING_template& BITSTRING_template::operator=(const BITSTRING& other_value) | |
1543 | { | |
1544 | other_value.must_bound("Assignment of an unbound bitstring value to a " | |
1545 | "template."); | |
1546 | clean_up(); | |
1547 | set_selection(SPECIFIC_VALUE); | |
1548 | single_value = other_value; | |
1549 | return *this; | |
1550 | } | |
1551 | ||
1552 | BITSTRING_template& BITSTRING_template::operator= | |
1553 | (const BITSTRING_ELEMENT& other_value) | |
1554 | { | |
1555 | other_value.must_bound("Assignment of an unbound bitstring element to a " | |
1556 | "template."); | |
1557 | clean_up(); | |
1558 | set_selection(SPECIFIC_VALUE); | |
1559 | single_value = other_value; | |
1560 | return *this; | |
1561 | } | |
1562 | ||
1563 | BITSTRING_template& BITSTRING_template::operator= | |
1564 | (const OPTIONAL<BITSTRING>& other_value) | |
1565 | { | |
1566 | clean_up(); | |
1567 | switch (other_value.get_selection()) { | |
1568 | case OPTIONAL_PRESENT: | |
1569 | set_selection(SPECIFIC_VALUE); | |
1570 | single_value = (const BITSTRING&)other_value; | |
1571 | break; | |
1572 | case OPTIONAL_OMIT: | |
1573 | set_selection(OMIT_VALUE); | |
1574 | break; | |
1575 | default: | |
1576 | TTCN_error("Assignment of an unbound optional field to a bitstring " | |
1577 | "template."); | |
1578 | } | |
1579 | return *this; | |
1580 | } | |
1581 | ||
1582 | BITSTRING_template& BITSTRING_template::operator= | |
1583 | (const BITSTRING_template& other_value) | |
1584 | { | |
1585 | if (&other_value != this) { | |
1586 | clean_up(); | |
1587 | copy_template(other_value); | |
1588 | } | |
1589 | return *this; | |
1590 | } | |
1591 | ||
1592 | BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value) | |
1593 | { | |
1594 | if (template_selection != SPECIFIC_VALUE || is_ifpresent) | |
1595 | TTCN_error("Accessing a bitstring element of a non-specific bitstring " | |
1596 | "template."); | |
1597 | return single_value[index_value]; | |
1598 | } | |
1599 | ||
1600 | BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value) | |
1601 | { | |
1602 | index_value.must_bound("Indexing a bitstring template with an unbound " | |
1603 | "integer value."); | |
1604 | return (*this)[(int)index_value]; | |
1605 | } | |
1606 | ||
1607 | const BITSTRING_ELEMENT BITSTRING_template::operator[](int index_value) const | |
1608 | { | |
1609 | if (template_selection != SPECIFIC_VALUE || is_ifpresent) | |
1610 | TTCN_error("Accessing a bitstring element of a non-specific bitstring " | |
1611 | "template."); | |
1612 | return single_value[index_value]; | |
1613 | } | |
1614 | ||
1615 | const BITSTRING_ELEMENT BITSTRING_template::operator[](const INTEGER& index_value) const | |
1616 | { | |
1617 | index_value.must_bound("Indexing a bitstring template with an unbound " | |
1618 | "integer value."); | |
1619 | return (*this)[(int)index_value]; | |
1620 | } | |
1621 | ||
1622 | boolean BITSTRING_template::match(const BITSTRING& other_value) const | |
1623 | { | |
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; | |
1629 | case OMIT_VALUE: | |
1630 | return FALSE; | |
1631 | case ANY_VALUE: | |
1632 | case ANY_OR_OMIT: | |
1633 | return TRUE; | |
1634 | case VALUE_LIST: | |
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); | |
1642 | default: | |
1643 | TTCN_error("Matching an uninitialized/unsupported bitstring template."); | |
1644 | } | |
1645 | return FALSE; | |
1646 | } | |
1647 | ||
1648 | const BITSTRING& BITSTRING_template::valueof() const | |
1649 | { | |
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; | |
1654 | } | |
1655 | ||
1656 | int BITSTRING_template::lengthof() const | |
1657 | { | |
1658 | int min_length; | |
1659 | boolean has_any_or_none; | |
1660 | if (is_ifpresent) | |
1661 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1662 | "which has an ifpresent attribute."); | |
1663 | switch (template_selection) | |
1664 | { | |
1665 | case SPECIFIC_VALUE: | |
1666 | min_length = single_value.lengthof(); | |
1667 | has_any_or_none = FALSE; | |
1668 | break; | |
1669 | case OMIT_VALUE: | |
1670 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1671 | "containing omit value."); | |
1672 | case ANY_VALUE: | |
1673 | case ANY_OR_OMIT: | |
1674 | min_length = 0; | |
1675 | has_any_or_none = TRUE; // max. length is infinity | |
1676 | break; | |
1677 | case VALUE_LIST: | |
1678 | { | |
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."); | |
1689 | } | |
1690 | min_length = item_length; | |
1691 | has_any_or_none = FALSE; | |
1692 | break; | |
1693 | } | |
1694 | case COMPLEMENTED_LIST: | |
1695 | TTCN_error("Performing lengthof() operation on a bitstring template " | |
1696 | "containing complemented list."); | |
1697 | case STRING_PATTERN: | |
1698 | min_length = 0; | |
1699 | has_any_or_none = FALSE; // TRUE if * chars in the pattern | |
1700 | for (unsigned int i = 0; i < pattern_value->n_elements; i++) | |
1701 | { | |
1702 | if (pattern_value->elements_ptr[i] < 3) min_length++; // case of 1, 0, ? | |
1703 | else has_any_or_none = TRUE; // case of * character | |
1704 | } | |
1705 | break; | |
1706 | default: | |
1707 | TTCN_error("Performing lengthof() operation on an " | |
1708 | "uninitialized/unsupported bitstring template."); | |
1709 | } | |
1710 | return check_section_is_single(min_length, has_any_or_none, | |
1711 | "length", "a", "bitstring template"); | |
1712 | } | |
1713 | ||
1714 | void BITSTRING_template::set_type(template_sel template_type, | |
1715 | unsigned int list_length) | |
1716 | { | |
1717 | if (template_type != VALUE_LIST && template_type != COMPLEMENTED_LIST) | |
1718 | TTCN_error("Setting an invalid list type for a bitstring template."); | |
1719 | clean_up(); | |
1720 | set_selection(template_type); | |
1721 | value_list.n_values = list_length; | |
1722 | value_list.list_value = new BITSTRING_template[list_length]; | |
1723 | } | |
1724 | ||
1725 | BITSTRING_template& BITSTRING_template::list_item(unsigned int list_index) | |
1726 | { | |
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]; | |
1733 | } | |
1734 | ||
1735 | static const char patterns[] = { '0', '1', '?', '*' }; | |
1736 | ||
1737 | void BITSTRING_template::log() const | |
1738 | { | |
1739 | switch (template_selection) { | |
1740 | case SPECIFIC_VALUE: | |
1741 | single_value.log(); | |
1742 | break; | |
1743 | case COMPLEMENTED_LIST: | |
1744 | TTCN_Logger::log_event_str("complement "); | |
1745 | // no break | |
1746 | case VALUE_LIST: | |
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(); | |
1751 | } | |
1752 | TTCN_Logger::log_char(')'); | |
1753 | break; | |
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>"); | |
1760 | } | |
1761 | TTCN_Logger::log_event_str("'B"); | |
1762 | break; | |
1763 | default: | |
1764 | log_generic(); | |
1765 | break; | |
1766 | } | |
1767 | log_restricted(); | |
1768 | log_ifpresent(); | |
1769 | } | |
1770 | ||
1771 | void BITSTRING_template::log_match(const BITSTRING& match_value) const | |
1772 | { | |
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(" := "); | |
1777 | } | |
1778 | match_value.log(); | |
1779 | TTCN_Logger::log_event_str(" with "); | |
1780 | log(); | |
1781 | if (match(match_value)) TTCN_Logger::log_event_str(" matched"); | |
1782 | else TTCN_Logger::log_event_str(" unmatched"); | |
1783 | } | |
1784 | ||
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: | |
1789 | *this = OMIT_VALUE; | |
1790 | break; | |
1791 | case Module_Param::MP_Any: | |
1792 | *this = ANY_VALUE; | |
1793 | break; | |
1794 | case Module_Param::MP_AnyOrNone: | |
1795 | *this = ANY_OR_OMIT; | |
1796 | break; | |
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)); | |
1802 | } | |
1803 | break; | |
1804 | case Module_Param::MP_Bitstring: | |
1805 | *this = BITSTRING(param.get_string_size(), (unsigned char*)param.get_string_data()); | |
1806 | break; | |
1807 | case Module_Param::MP_Bitstring_Template: | |
1808 | *this = BITSTRING_template(param.get_string_size(), (unsigned char*)param.get_string_data()); | |
1809 | break; | |
1810 | default: | |
1811 | param.type_error("bitstring template"); | |
1812 | } | |
1813 | is_ifpresent = param.get_ifpresent(); | |
1814 | set_length_range(param); | |
1815 | } | |
1816 | ||
1817 | void BITSTRING_template::encode_text(Text_Buf& text_buf) const | |
1818 | { | |
1819 | encode_text_restricted(text_buf); | |
1820 | switch (template_selection) { | |
1821 | case OMIT_VALUE: | |
1822 | case ANY_VALUE: | |
1823 | case ANY_OR_OMIT: | |
1824 | break; | |
1825 | case SPECIFIC_VALUE: | |
1826 | single_value.encode_text(text_buf); | |
1827 | break; | |
1828 | case VALUE_LIST: | |
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); | |
1833 | break; | |
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); | |
1837 | break; | |
1838 | default: | |
1839 | TTCN_error("Text encoder: Encoding an uninitialized/unsupported " | |
1840 | "bitstring template."); | |
1841 | } | |
1842 | } | |
1843 | ||
1844 | void BITSTRING_template::decode_text(Text_Buf& text_buf) | |
1845 | { | |
1846 | clean_up(); | |
1847 | decode_text_restricted(text_buf); | |
1848 | switch (template_selection) { | |
1849 | case OMIT_VALUE: | |
1850 | case ANY_VALUE: | |
1851 | case ANY_OR_OMIT: | |
1852 | break; | |
1853 | case SPECIFIC_VALUE: | |
1854 | single_value.decode_text(text_buf); | |
1855 | break; | |
1856 | case VALUE_LIST: | |
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); | |
1862 | break; | |
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); | |
1870 | break;} | |
1871 | default: | |
1872 | TTCN_error("Text decoder: An unknown/unsupported selection was " | |
1873 | "received for a bitstring template."); | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | boolean BITSTRING_template::is_present() const | |
1878 | { | |
1879 | if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE; | |
1880 | return !match_omit(); | |
1881 | } | |
1882 | ||
1883 | boolean BITSTRING_template::match_omit() const | |
1884 | { | |
1885 | if (is_ifpresent) return TRUE; | |
1886 | switch (template_selection) { | |
1887 | case OMIT_VALUE: | |
1888 | case ANY_OR_OMIT: | |
1889 | return TRUE; | |
1890 | case VALUE_LIST: | |
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; | |
1896 | default: | |
1897 | return FALSE; | |
1898 | } | |
1899 | return FALSE; | |
1900 | } | |
1901 | ||
1902 | #ifndef TITAN_RUNTIME_2 | |
1903 | void BITSTRING_template::check_restriction(template_res t_res, const char* t_name) const | |
1904 | { | |
1905 | if (template_selection==UNINITIALIZED_TEMPLATE) return; | |
1906 | switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) { | |
1907 | case TR_VALUE: | |
1908 | if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return; | |
1909 | break; | |
1910 | case TR_OMIT: | |
1911 | if (!is_ifpresent && (template_selection==OMIT_VALUE || | |
1912 | template_selection==SPECIFIC_VALUE)) return; | |
1913 | break; | |
1914 | case TR_PRESENT: | |
1915 | if (!match_omit()) return; | |
1916 | break; | |
1917 | default: | |
1918 | return; | |
1919 | } | |
1920 | TTCN_error("Restriction `%s' on template of type %s violated.", | |
1921 | get_res_name(t_res), t_name ? t_name : "bitstring"); | |
1922 | } | |
1923 | #else | |
1924 | int BITSTRING::RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const | |
1925 | { | |
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; | |
1932 | } | |
1933 | #endif |