Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
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 "Boolean.hh" | |
11 | #include "../common/memory.h" | |
12 | #include "Parameters.h" | |
13 | #include "Param_Types.hh" | |
14 | #include "Error.hh" | |
15 | #include "Logger.hh" | |
16 | #include "Encdec.hh" | |
17 | #include "RAW.hh" | |
18 | #include "BER.hh" | |
19 | #include "TEXT.hh" | |
20 | #include "Charstring.hh" | |
21 | #include "XmlReader.hh" | |
22 | ||
23 | static const Token_Match boolean_true_match("^(true).*$",TRUE); | |
24 | static const Token_Match boolean_false_match("^(false).*$",TRUE); | |
25 | ||
26 | BOOLEAN::BOOLEAN() | |
27 | { | |
28 | bound_flag = FALSE; | |
29 | } | |
30 | ||
31 | BOOLEAN::BOOLEAN(boolean other_value) | |
32 | { | |
33 | bound_flag = TRUE; | |
34 | boolean_value = other_value; | |
35 | } | |
36 | ||
37 | BOOLEAN::BOOLEAN(const BOOLEAN& other_value) | |
38 | : Base_Type(other_value) | |
39 | { | |
40 | other_value.must_bound("Copying an unbound boolean value."); | |
41 | bound_flag = TRUE; | |
42 | boolean_value = other_value.boolean_value; | |
43 | } | |
44 | ||
45 | BOOLEAN& BOOLEAN::operator=(boolean other_value) | |
46 | { | |
47 | bound_flag = TRUE; | |
48 | boolean_value = other_value; | |
49 | return *this; | |
50 | } | |
51 | ||
52 | BOOLEAN& BOOLEAN::operator=(const BOOLEAN& other_value) | |
53 | { | |
54 | other_value.must_bound("Assignment of an unbound boolean value."); | |
55 | bound_flag = TRUE; | |
56 | boolean_value = other_value.boolean_value; | |
57 | return *this; | |
58 | } | |
59 | ||
60 | boolean BOOLEAN::operator!() const | |
61 | { | |
62 | must_bound("The operand of not operator is an unbound boolean value."); | |
63 | return !boolean_value; | |
64 | } | |
65 | ||
66 | boolean BOOLEAN::operator&&(boolean other_value) const | |
67 | { | |
68 | must_bound("The left operand of and operator is an unbound boolean value."); | |
69 | return boolean_value && other_value; | |
70 | } | |
71 | ||
72 | boolean BOOLEAN::operator&&(const BOOLEAN& other_value) const | |
73 | { | |
74 | must_bound("The left operand of and operator is an unbound boolean value."); | |
75 | if (!boolean_value) return FALSE; | |
76 | other_value.must_bound("The right operand of and operator is an unbound " | |
77 | "boolean value."); | |
78 | return other_value.boolean_value; | |
79 | } | |
80 | ||
81 | boolean BOOLEAN::operator^(boolean other_value) const | |
82 | { | |
83 | must_bound("The left operand of xor operator is an unbound boolean value."); | |
84 | return boolean_value != other_value; | |
85 | } | |
86 | ||
87 | boolean BOOLEAN::operator^(const BOOLEAN& other_value) const | |
88 | { | |
89 | must_bound("The left operand of xor operator is an unbound boolean value."); | |
90 | other_value.must_bound("The right operand of xor operator is an unbound " | |
91 | "boolean value."); | |
92 | return boolean_value != other_value.boolean_value; | |
93 | } | |
94 | ||
95 | boolean BOOLEAN::operator||(boolean other_value) const | |
96 | { | |
97 | must_bound("The left operand of or operator is an unbound boolean value."); | |
98 | return boolean_value || other_value; | |
99 | } | |
100 | ||
101 | boolean BOOLEAN::operator||(const BOOLEAN& other_value) const | |
102 | { | |
103 | must_bound("The left operand of or operator is an unbound boolean value."); | |
104 | if (boolean_value) return TRUE; | |
105 | other_value.must_bound("The right operand of or operator is an unbound " | |
106 | "boolean value."); | |
107 | return other_value.boolean_value; | |
108 | } | |
109 | ||
110 | boolean BOOLEAN::operator==(boolean other_value) const | |
111 | { | |
112 | must_bound("The left operand of comparison is an unbound boolean value."); | |
113 | return boolean_value == other_value; | |
114 | } | |
115 | ||
116 | boolean BOOLEAN::operator==(const BOOLEAN& other_value) const | |
117 | { | |
118 | must_bound("The left operand of comparison is an unbound boolean value."); | |
119 | other_value.must_bound("The right operand of comparison is an unbound " | |
120 | "boolean value."); | |
121 | return boolean_value == other_value.boolean_value; | |
122 | } | |
123 | ||
124 | BOOLEAN::operator boolean() const | |
125 | { | |
126 | must_bound("Using the value of an unbound boolean variable."); | |
127 | return boolean_value; | |
128 | } | |
129 | ||
130 | void BOOLEAN::clean_up() | |
131 | { | |
132 | bound_flag = FALSE; | |
133 | } | |
134 | ||
135 | void BOOLEAN::log() const | |
136 | { | |
137 | if (bound_flag) TTCN_Logger::log_event_str(boolean_value ? "true" : "false"); | |
138 | else TTCN_Logger::log_event_unbound(); | |
139 | } | |
140 | ||
141 | void BOOLEAN::encode_text(Text_Buf& text_buf) const | |
142 | { | |
143 | must_bound("Text encoder: Encoding an unbound boolean value."); | |
144 | text_buf.push_int(boolean_value ? 1 : 0); | |
145 | } | |
146 | ||
147 | void BOOLEAN::decode_text(Text_Buf& text_buf) | |
148 | { | |
149 | int int_value = text_buf.pull_int().get_val(); | |
150 | switch (int_value) { | |
151 | case 0: | |
152 | boolean_value = FALSE; | |
153 | break; | |
154 | case 1: | |
155 | boolean_value = TRUE; | |
156 | break; | |
157 | default: | |
158 | TTCN_error("Text decoder: An invalid boolean value (%d) was received.", | |
159 | int_value); | |
160 | } | |
161 | bound_flag = TRUE; | |
162 | } | |
163 | ||
164 | void BOOLEAN::set_param(Module_Param& param) { | |
165 | param.basic_check(Module_Param::BC_VALUE, "boolean value"); | |
3abe9331 | 166 | Module_Param_Ptr mp = ¶m; |
167 | if (param.get_type() == Module_Param::MP_Reference) { | |
168 | mp = param.get_referenced_param(); | |
169 | } | |
170 | if (mp->get_type()!=Module_Param::MP_Boolean) param.type_error("boolean value"); | |
970ed795 | 171 | bound_flag = TRUE; |
3abe9331 | 172 | boolean_value = mp->get_boolean(); |
173 | } | |
174 | ||
175 | Module_Param* BOOLEAN::get_param(Module_Param_Name& /* param_name */) const | |
176 | { | |
177 | if (!is_bound()) { | |
178 | return new Module_Param_Unbound(); | |
179 | } | |
180 | return new Module_Param_Boolean(boolean_value); | |
970ed795 EL |
181 | } |
182 | ||
183 | void BOOLEAN::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
184 | TTCN_EncDec::coding_t p_coding, ...) const | |
185 | { | |
186 | va_list pvar; | |
187 | va_start(pvar, p_coding); | |
188 | switch(p_coding) { | |
189 | case TTCN_EncDec::CT_BER: { | |
190 | TTCN_EncDec_ErrorContext ec("While BER-encoding type '%s': ", p_td.name); | |
191 | unsigned BER_coding=va_arg(pvar, unsigned); | |
192 | BER_encode_chk_coding(BER_coding); | |
193 | ASN_BER_TLV_t *tlv=BER_encode_TLV(p_td, BER_coding); | |
194 | tlv->put_in_buffer(p_buf); | |
195 | ASN_BER_TLV_t::destruct(tlv); | |
196 | break;} | |
197 | case TTCN_EncDec::CT_RAW: { | |
198 | TTCN_EncDec_ErrorContext ec("While RAW-encoding type '%s': ", p_td.name); | |
199 | if(!p_td.raw) | |
200 | TTCN_EncDec_ErrorContext::error_internal | |
201 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
202 | RAW_enc_tr_pos rp; | |
203 | rp.level=0; | |
204 | rp.pos=NULL; | |
205 | RAW_enc_tree root(TRUE,NULL,&rp,1,p_td.raw); | |
206 | RAW_encode(p_td, root); | |
207 | root.put_to_buf(p_buf); | |
208 | break;} | |
209 | case TTCN_EncDec::CT_TEXT: { | |
210 | TTCN_EncDec_ErrorContext ec("While TEXT-encoding type '%s': ", p_td.name); | |
211 | if(!p_td.text) | |
212 | TTCN_EncDec_ErrorContext::error_internal | |
213 | ("No TEXT descriptor available for type '%s'.", p_td.name); | |
214 | TEXT_encode(p_td,p_buf); | |
215 | break;} | |
216 | case TTCN_EncDec::CT_XER: { | |
217 | TTCN_EncDec_ErrorContext ec("While XER-encoding type '%s': ", p_td.name); | |
218 | unsigned XER_coding=va_arg(pvar, unsigned); | |
af710487 | 219 | XER_encode(*p_td.xer, p_buf, XER_coding, 0, 0); |
970ed795 EL |
220 | break;} |
221 | case TTCN_EncDec::CT_JSON: { | |
222 | TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name); | |
223 | if(!p_td.json) | |
224 | TTCN_EncDec_ErrorContext::error_internal | |
225 | ("No JSON descriptor available for type '%s'.", p_td.name); | |
226 | JSON_Tokenizer tok(va_arg(pvar, int) != 0); | |
227 | JSON_encode(p_td, tok); | |
228 | p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer()); | |
229 | break;} | |
230 | default: | |
231 | TTCN_error("Unknown coding method requested to encode type '%s'", | |
232 | p_td.name); | |
233 | } | |
234 | va_end(pvar); | |
235 | } | |
236 | ||
237 | void BOOLEAN::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
238 | TTCN_EncDec::coding_t p_coding, ...) | |
239 | { | |
240 | va_list pvar; | |
241 | va_start(pvar, p_coding); | |
242 | switch(p_coding) { | |
243 | case TTCN_EncDec::CT_BER: { | |
244 | TTCN_EncDec_ErrorContext ec("While BER-decoding type '%s': ", p_td.name); | |
245 | unsigned L_form=va_arg(pvar, unsigned); | |
246 | ASN_BER_TLV_t tlv; | |
247 | BER_decode_str2TLV(p_buf, tlv, L_form); | |
248 | BER_decode_TLV(p_td, tlv, L_form); | |
249 | if(tlv.isComplete) p_buf.increase_pos(tlv.get_len()); | |
250 | break;} | |
251 | case TTCN_EncDec::CT_RAW: { | |
252 | TTCN_EncDec_ErrorContext ec("While RAW-decoding type '%s': ", p_td.name); | |
253 | if(!p_td.raw) | |
254 | TTCN_EncDec_ErrorContext::error_internal | |
255 | ("No RAW descriptor available for type '%s'.", p_td.name); | |
256 | raw_order_t order; | |
257 | switch(p_td.raw->top_bit_order){ | |
258 | case TOP_BIT_LEFT: | |
259 | order=ORDER_LSB; | |
260 | break; | |
261 | case TOP_BIT_RIGHT: | |
262 | default: | |
263 | order=ORDER_MSB; | |
264 | } | |
265 | if(RAW_decode(p_td, p_buf, p_buf.get_len()*8, order)<0) | |
266 | ec.error(TTCN_EncDec::ET_INCOMPL_MSG, | |
267 | "Can not decode type '%s', because invalid or incomplete" | |
268 | " message was received" | |
269 | , p_td.name); | |
270 | break;} | |
271 | case TTCN_EncDec::CT_TEXT: { | |
272 | Limit_Token_List limit; | |
273 | TTCN_EncDec_ErrorContext ec("While TEXT-decoding type '%s': ", p_td.name); | |
274 | if(!p_td.text) | |
275 | TTCN_EncDec_ErrorContext::error_internal | |
276 | ("No TEXT descriptor available for type '%s'.", p_td.name); | |
277 | const unsigned char *b=p_buf.get_data(); | |
278 | if(b[p_buf.get_len()-1]!='\0'){ | |
279 | p_buf.set_pos(p_buf.get_len()); | |
280 | p_buf.put_zero(8,ORDER_LSB); | |
281 | p_buf.rewind(); | |
282 | } | |
283 | if(TEXT_decode(p_td,p_buf,limit)<0) | |
284 | ec.error(TTCN_EncDec::ET_INCOMPL_MSG, | |
285 | "Can not decode type '%s', because invalid or incomplete" | |
286 | " message was received" | |
287 | , p_td.name); | |
288 | break;} | |
289 | case TTCN_EncDec::CT_XER: { | |
af710487 | 290 | TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name); |
970ed795 EL |
291 | unsigned XER_coding=va_arg(pvar, unsigned); |
292 | XmlReaderWrap reader(p_buf); | |
293 | for (int success = reader.Read(); success==1; success=reader.Read()) { | |
294 | int type = reader.NodeType(); | |
295 | if (type==XML_READER_TYPE_ELEMENT) | |
296 | break; | |
297 | } | |
af710487 | 298 | XER_decode(*p_td.xer, reader, XER_coding, 0); |
970ed795 EL |
299 | size_t bytes = reader.ByteConsumed(); |
300 | p_buf.set_pos(bytes); | |
301 | break;} | |
302 | case TTCN_EncDec::CT_JSON: { | |
303 | TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name); | |
304 | if(!p_td.json) | |
305 | TTCN_EncDec_ErrorContext::error_internal | |
306 | ("No JSON descriptor available for type '%s'.", p_td.name); | |
307 | JSON_Tokenizer tok((const char*)p_buf.get_data(), p_buf.get_len()); | |
308 | if(JSON_decode(p_td, tok, false)<0) | |
309 | ec.error(TTCN_EncDec::ET_INCOMPL_MSG, | |
310 | "Can not decode type '%s', because invalid or incomplete" | |
311 | " message was received" | |
312 | , p_td.name); | |
313 | p_buf.set_pos(tok.get_buf_pos()); | |
314 | break;} | |
315 | default: | |
316 | TTCN_error("Unknown coding method requested to decode type '%s'", | |
317 | p_td.name); | |
318 | } | |
319 | va_end(pvar); | |
320 | } | |
321 | ||
322 | ASN_BER_TLV_t* | |
323 | BOOLEAN::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, | |
324 | unsigned p_coding) const | |
325 | { | |
326 | BER_chk_descr(p_td); | |
327 | ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound()); | |
328 | if(!new_tlv) { | |
329 | new_tlv=ASN_BER_TLV_t::construct(1, NULL); | |
330 | new_tlv->V.str.Vstr[0]=boolean_value==TRUE?0xFF:0x00; | |
331 | } | |
332 | new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding); | |
333 | return new_tlv; | |
334 | } | |
335 | ||
336 | boolean BOOLEAN::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, | |
337 | const ASN_BER_TLV_t& p_tlv, | |
338 | unsigned L_form) | |
339 | { | |
340 | bound_flag = FALSE; | |
341 | BER_chk_descr(p_td); | |
342 | ASN_BER_TLV_t stripped_tlv; | |
343 | BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv); | |
344 | TTCN_EncDec_ErrorContext ec("While decoding BOOLEAN type: "); | |
345 | stripped_tlv.chk_constructed_flag(FALSE); | |
346 | if (!stripped_tlv.isComplete) return FALSE; | |
347 | if(stripped_tlv.V.str.Vlen!=1) | |
348 | ec.error(TTCN_EncDec::ET_INVAL_MSG, | |
349 | "Length of V-part is %lu (instead of 1).", | |
350 | (unsigned long) stripped_tlv.V.str.Vlen); | |
351 | if(stripped_tlv.V.str.Vlen>=1) { | |
352 | switch(stripped_tlv.V.str.Vstr[0]) { | |
353 | case 0x00: | |
354 | boolean_value=FALSE; | |
355 | break; | |
356 | default: | |
357 | /* warning? */ | |
358 | case 0xFF: | |
359 | boolean_value=TRUE; | |
360 | break; | |
361 | } // switch | |
362 | bound_flag = TRUE; | |
363 | return TRUE; | |
364 | } else return FALSE; | |
365 | } | |
366 | ||
367 | int BOOLEAN::TEXT_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff, | |
368 | Limit_Token_List&, boolean no_err, boolean /*first_call*/) | |
369 | { | |
370 | ||
371 | int decoded_length = 0; | |
372 | int str_len = 0; | |
373 | if (p_td.text->begin_decode) { | |
374 | int tl; | |
375 | if ((tl = p_td.text->begin_decode->match_begin(buff)) < 0) { | |
376 | if (no_err) return -1; | |
377 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR, | |
378 | "The specified token '%s' not found for '%s': ", | |
379 | (const char*) *(p_td.text->begin_decode), p_td.name); | |
380 | return 0; | |
381 | } | |
382 | decoded_length += tl; | |
383 | buff.increase_pos(tl); | |
384 | } | |
385 | if (buff.get_read_len() < 1 && no_err) return -TTCN_EncDec::ET_LEN_ERR; | |
386 | ||
387 | boolean found = FALSE; | |
388 | ||
389 | if ( p_td.text->val.bool_values | |
390 | && p_td.text->val.bool_values->true_decode_token) { | |
391 | int tl; | |
392 | if ((tl = p_td.text->val.bool_values->true_decode_token->match_begin(buff)) > -1) { | |
393 | str_len = tl; | |
394 | found = TRUE; | |
395 | boolean_value = TRUE; | |
396 | } | |
397 | } | |
398 | else { | |
399 | int tl; | |
400 | if ((tl = boolean_true_match.match_begin(buff)) >= 0) { | |
401 | str_len = tl; | |
402 | found = TRUE; | |
403 | boolean_value = TRUE; | |
404 | } | |
405 | } | |
406 | ||
407 | if (!found) { | |
408 | if ( p_td.text->val.bool_values | |
409 | && p_td.text->val.bool_values->false_decode_token) { | |
410 | int tl; | |
411 | if ((tl = p_td.text->val.bool_values->false_decode_token->match_begin(buff)) > -1) { | |
412 | str_len = tl; | |
413 | found = TRUE; | |
414 | boolean_value = FALSE; | |
415 | } | |
416 | } | |
417 | else { | |
418 | int tl; | |
419 | if ((tl = boolean_false_match.match_begin(buff)) >= 0) { | |
420 | str_len = tl; | |
421 | found = TRUE; | |
422 | boolean_value = FALSE; | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | if (found) { | |
428 | decoded_length += str_len; | |
429 | buff.increase_pos(str_len); | |
430 | } | |
431 | else { | |
432 | if (no_err) return -1; | |
433 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR, | |
434 | "No boolean token found for '%s': ", p_td.name); | |
435 | return decoded_length; | |
436 | } | |
437 | ||
438 | if (p_td.text->end_decode) { | |
439 | int tl; | |
440 | if ((tl = p_td.text->end_decode->match_begin(buff)) < 0) { | |
441 | if (no_err) return -1; | |
442 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR, | |
443 | "The specified token '%s' not found for '%s': ", | |
444 | (const char*) *(p_td.text->end_decode), p_td.name); | |
445 | return 0; | |
446 | } | |
447 | decoded_length += tl; | |
448 | buff.increase_pos(tl); | |
449 | } | |
450 | bound_flag = TRUE; | |
451 | return decoded_length; | |
452 | } | |
453 | ||
454 | ||
455 | int BOOLEAN::TEXT_encode(const TTCN_Typedescriptor_t& p_td, | |
456 | TTCN_Buffer& buff) const{ | |
457 | int encoded_length=0; | |
458 | if(p_td.text->begin_encode){ | |
459 | buff.put_cs(*p_td.text->begin_encode); | |
460 | encoded_length+=p_td.text->begin_encode->lengthof(); | |
461 | } | |
462 | if(!is_bound()) { | |
463 | TTCN_EncDec_ErrorContext::error | |
464 | (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value."); | |
465 | if(p_td.text->end_encode){ | |
466 | buff.put_cs(*p_td.text->end_encode); | |
467 | encoded_length+=p_td.text->end_encode->lengthof(); | |
468 | } | |
469 | return encoded_length; | |
470 | } | |
471 | ||
472 | if(p_td.text->val.bool_values==NULL){ | |
473 | if(boolean_value){ | |
474 | buff.put_s(4,(const unsigned char*)"true"); | |
475 | encoded_length+=4; | |
476 | } | |
477 | else { | |
478 | buff.put_s(5,(const unsigned char*)"false"); | |
479 | encoded_length+=5; | |
480 | } | |
481 | } else { | |
482 | if(boolean_value){ | |
483 | if(p_td.text->val.bool_values->true_encode_token){ | |
484 | buff.put_cs(*p_td.text->val.bool_values->true_encode_token); | |
485 | encoded_length+=p_td.text-> | |
486 | val.bool_values->true_encode_token->lengthof(); | |
487 | } else { | |
488 | buff.put_s(4,(const unsigned char*)"true"); | |
489 | encoded_length+=4; | |
490 | } | |
491 | } | |
492 | else { | |
493 | if(p_td.text->val.bool_values->false_encode_token){ | |
494 | buff.put_cs(*p_td.text->val.bool_values->false_encode_token); | |
495 | encoded_length+=p_td.text-> | |
496 | val.bool_values->false_encode_token->lengthof(); | |
497 | } else { | |
498 | buff.put_s(5,(const unsigned char*)"false"); | |
499 | encoded_length+=5; | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
504 | if(p_td.text->end_encode){ | |
505 | buff.put_cs(*p_td.text->end_encode); | |
506 | encoded_length+=p_td.text->end_encode->lengthof(); | |
507 | } | |
508 | return encoded_length; | |
509 | } | |
510 | ||
511 | int BOOLEAN::RAW_encode(const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const | |
512 | { | |
513 | unsigned char *bc; | |
514 | int loc_length = p_td.raw->fieldlength ? p_td.raw->fieldlength : 1; | |
515 | int length = (loc_length + 7) / 8; | |
516 | unsigned char tmp; | |
517 | if (!is_bound()) { | |
518 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
519 | "Encoding an unbound value."); | |
520 | tmp = '\0'; | |
521 | } | |
522 | else tmp = boolean_value ? 0xFF : 0x00; | |
523 | // myleaf.ext_bit=EXT_BIT_NO; | |
524 | if (myleaf.must_free) Free(myleaf.body.leaf.data_ptr); | |
525 | if (length > RAW_INT_ENC_LENGTH) { | |
526 | myleaf.body.leaf.data_ptr = bc = (unsigned char*)Malloc(length*sizeof(*bc)); | |
527 | myleaf.must_free = TRUE; | |
528 | myleaf.data_ptr_used = TRUE; | |
529 | } | |
530 | else bc = myleaf.body.leaf.data_array; | |
531 | ||
532 | memset(bc, tmp, length * sizeof(*bc)); | |
533 | return myleaf.length = loc_length; | |
534 | } | |
535 | ||
536 | int BOOLEAN::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff, | |
537 | int limit, raw_order_t top_bit_ord, boolean no_err, int /*sel_field*/, | |
538 | boolean /*first_call*/) | |
539 | { | |
540 | bound_flag = FALSE; | |
541 | int prepaddlength = buff.increase_pos_padd(p_td.raw->prepadding); | |
542 | limit -= prepaddlength; | |
543 | int decode_length = p_td.raw->fieldlength > 0 ? p_td.raw->fieldlength : 1; | |
544 | if (decode_length > limit) { | |
545 | if (no_err) return -TTCN_EncDec::ET_LEN_ERR; | |
546 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR, | |
547 | "There is not enough bits in the buffer to decode type %s (needed: %d, " | |
548 | "found: %d).", p_td.name, decode_length, limit); | |
549 | decode_length = limit; | |
550 | } | |
551 | int nof_unread_bits = buff.unread_len_bit(); | |
552 | if (decode_length > nof_unread_bits) { | |
553 | if (no_err) return -TTCN_EncDec::ET_INCOMPL_MSG; | |
554 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG, | |
555 | "There is not enough bits in the buffer to decode type %s (needed: %d, " | |
556 | "found: %d).", p_td.name, decode_length, nof_unread_bits); | |
557 | decode_length = nof_unread_bits; | |
558 | } | |
559 | if (decode_length < 0) return -1; | |
560 | else if (decode_length == 0) boolean_value = FALSE; | |
561 | else { | |
562 | RAW_coding_par cp; | |
563 | boolean orders = FALSE; | |
564 | if (p_td.raw->bitorderinoctet == ORDER_MSB) orders = TRUE; | |
565 | if (p_td.raw->bitorderinfield == ORDER_MSB) orders = !orders; | |
566 | cp.bitorder = orders ? ORDER_MSB : ORDER_LSB; | |
567 | orders = FALSE; | |
568 | if (p_td.raw->byteorder == ORDER_MSB) orders = TRUE; | |
569 | if (p_td.raw->bitorderinfield == ORDER_MSB) orders = !orders; | |
570 | cp.byteorder = orders ? ORDER_MSB : ORDER_LSB; | |
571 | cp.fieldorder = p_td.raw->fieldorder; | |
572 | cp.hexorder = ORDER_LSB; | |
573 | int length = (decode_length + 7) / 8; | |
574 | unsigned char *data = (unsigned char*)Malloc(length*sizeof(unsigned char)); | |
575 | buff.get_b((size_t)decode_length, data, cp, top_bit_ord); | |
576 | if(decode_length % 8){ | |
577 | data[length - 1] &= BitMaskTable[decode_length % 8]; | |
578 | } | |
579 | unsigned char ch = '\0'; | |
580 | for (int a = 0; a < length; a++) ch |= data[a]; | |
581 | Free(data); | |
582 | boolean_value = ch != '\0'; | |
583 | } | |
584 | bound_flag = TRUE; | |
585 | decode_length += buff.increase_pos_padd(p_td.raw->padding); | |
586 | return decode_length + prepaddlength; | |
587 | } | |
588 | ||
589 | int BOOLEAN::XER_encode(const XERdescriptor_t& p_td, | |
af710487 | 590 | TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const |
970ed795 EL |
591 | { |
592 | if(!is_bound()) { | |
593 | TTCN_EncDec_ErrorContext::error | |
594 | (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound boolean value."); | |
595 | } | |
596 | int encoded_length=(int)p_buf.get_len(); | |
597 | ||
598 | int exer = is_exer(flavor); | |
599 | ||
600 | flavor |= (SIMPLE_TYPE | BXER_EMPTY_ELEM); | |
601 | if (begin_xml(p_td, p_buf, flavor, indent, false) == -1) --encoded_length; | |
602 | ||
603 | if (exer) { | |
604 | if (p_td.xer_bits & XER_TEXT) { | |
605 | p_buf.put_c(boolean_value ? '1' : '0'); | |
606 | } | |
607 | else { | |
608 | if (boolean_value) p_buf.put_s(4, (cbyte*)"true"); | |
609 | else p_buf.put_s(5, (cbyte*)"false"); | |
610 | } | |
611 | } | |
612 | else { | |
613 | if (boolean_value) p_buf.put_s(7, (cbyte*)"<true/>"); | |
614 | else p_buf.put_s(8, (cbyte*)"<false/>"); | |
615 | } | |
616 | ||
617 | end_xml(p_td, p_buf, flavor, indent, false); | |
618 | ||
619 | return (int)p_buf.get_len() - encoded_length; | |
620 | } | |
621 | ||
622 | int BOOLEAN::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, | |
af710487 | 623 | unsigned int flavor, embed_values_dec_struct_t*) |
970ed795 EL |
624 | { |
625 | const boolean exer = is_exer(flavor); | |
626 | int XMLValueList = !exer && is_record_of(flavor); | |
627 | const boolean notag = (exer && (p_td.xer_bits & (UNTAGGED))) || | |
628 | is_exerlist(flavor) || XMLValueList; | |
629 | int depth = -1, success, type; | |
630 | const char *value = 0; | |
631 | ||
632 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { | |
633 | verify_name(reader, p_td, exer); | |
634 | value = (const char *)reader.Value(); | |
635 | } | |
636 | else { | |
637 | for (success = reader.Ok(); success == 1; success = reader.Read()) { | |
638 | type = reader.NodeType(); | |
639 | if (!notag && depth == -1) { | |
640 | if (XML_READER_TYPE_ELEMENT == type) { | |
641 | verify_name(reader, p_td, exer); | |
642 | depth = reader.Depth(); | |
643 | ||
644 | if (exer && (p_td.dfeValue != 0) && reader.IsEmptyElement()) { | |
645 | *this = *static_cast<const BOOLEAN*>(p_td.dfeValue); | |
646 | (void)reader.Read(); | |
647 | goto fini; | |
648 | } | |
649 | continue; | |
650 | } // if type | |
651 | } | |
652 | else { // found the enclosing tag already | |
653 | if (!exer && XML_READER_TYPE_ELEMENT == type) { | |
654 | // this must be EmptyElement Boolean | |
655 | if (!reader.IsEmptyElement()) TTCN_EncDec_ErrorContext::error( | |
656 | TTCN_EncDec::ET_INVAL_MSG, "Boolean must be empty element"); | |
657 | value = (const char*)reader.LocalName(); | |
658 | } | |
659 | else if (XML_READER_TYPE_TEXT == type) { | |
660 | // TextBoolean | |
661 | value = (const char*)reader.Value(); | |
662 | } | |
663 | ||
664 | // Must not modify the buffer when attempting to find the selected alternative for USE-UNION | |
665 | if (!exer || !(flavor & EXIT_ON_ERROR)) reader.Read(); | |
666 | break; | |
667 | } // if depth | |
668 | } // next read | |
669 | } // if not attribute | |
670 | ||
671 | if (value != 0 && *value != 0) { | |
672 | // extract the data | |
673 | if (value[1]=='\0' && (*value & 0x3E) == '0') | |
674 | { | |
675 | bound_flag = true; | |
676 | boolean_value = *value == '1'; | |
677 | } | |
678 | else if (!strcmp(value, "true")) { | |
679 | boolean_value = true; | |
680 | bound_flag = true; | |
681 | } | |
682 | else if (!strcmp(value, "false")) { | |
683 | boolean_value = false; | |
684 | bound_flag = true; | |
685 | } | |
686 | ||
687 | } | |
688 | ||
689 | if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) { // I am an attribute | |
690 | // Let the caller do reader.AdvanceAttribute(); | |
691 | } | |
692 | else if (!notag) { | |
693 | for (success = reader.Ok(); success == 1; success = reader.Read()) { | |
694 | type = reader.NodeType(); | |
695 | if (XML_READER_TYPE_END_ELEMENT == type) { | |
696 | verify_end(reader, p_td, depth, exer); | |
697 | reader.Read(); // one last time | |
698 | break; | |
699 | } | |
700 | } // next | |
701 | } | |
702 | fini: | |
703 | return 1; | |
704 | } | |
705 | ||
706 | int BOOLEAN::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok) const | |
707 | { | |
708 | if (!is_bound()) { | |
709 | TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND, | |
710 | "Encoding an unbound boolean value."); | |
711 | return -1; | |
712 | } | |
713 | return p_tok.put_next_token((boolean_value) ? JSON_TOKEN_LITERAL_TRUE : JSON_TOKEN_LITERAL_FALSE, NULL); | |
714 | } | |
715 | ||
716 | int BOOLEAN::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent) | |
717 | { | |
718 | json_token_t token = JSON_TOKEN_NONE; | |
719 | int dec_len = 0; | |
720 | if (p_td.json->default_value && 0 == p_tok.get_buffer_length()) { | |
721 | // No JSON data in the buffer -> use default value | |
722 | if (strcmp(p_td.json->default_value, "true") == 0) { | |
723 | token = JSON_TOKEN_LITERAL_TRUE; | |
724 | } | |
725 | else { | |
726 | token = JSON_TOKEN_LITERAL_FALSE; | |
727 | } | |
728 | } else { | |
729 | dec_len = p_tok.get_next_token(&token, NULL, NULL); | |
730 | } | |
731 | if (JSON_TOKEN_ERROR == token) { | |
732 | JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, ""); | |
733 | return JSON_ERROR_FATAL; | |
734 | } | |
735 | else if (JSON_TOKEN_LITERAL_TRUE == token) { | |
736 | bound_flag = true; | |
737 | boolean_value = true; | |
738 | } | |
739 | else if (JSON_TOKEN_LITERAL_FALSE == token) { | |
740 | bound_flag = true; | |
741 | boolean_value = false; | |
742 | } | |
743 | else { | |
744 | bound_flag = false; | |
745 | return JSON_ERROR_INVALID_TOKEN; | |
746 | } | |
747 | return dec_len; | |
748 | } | |
749 | ||
750 | ||
751 | boolean operator&&(boolean bool_value, const BOOLEAN& other_value) | |
752 | { | |
753 | if (!bool_value) return FALSE; | |
754 | other_value.must_bound("The right operand of and operator is an unbound " | |
755 | "boolean value."); | |
756 | return other_value.boolean_value; | |
757 | } | |
758 | ||
759 | boolean operator^(boolean bool_value, const BOOLEAN& other_value) | |
760 | { | |
761 | other_value.must_bound("The right operand of xor operator is an unbound " | |
762 | "boolean value."); | |
763 | return bool_value != other_value.boolean_value; | |
764 | } | |
765 | ||
766 | boolean operator||(boolean bool_value, const BOOLEAN& other_value) | |
767 | { | |
768 | if (bool_value) return TRUE; | |
769 | other_value.must_bound("The right operand of or operator is an unbound " | |
770 | "boolean value."); | |
771 | return other_value.boolean_value; | |
772 | } | |
773 | ||
774 | boolean operator==(boolean bool_value, const BOOLEAN& other_value) | |
775 | { | |
776 | other_value.must_bound("The right operand of comparison is an unbound " | |
777 | "boolean value."); | |
778 | return bool_value == other_value.boolean_value; | |
779 | } | |
780 | ||
781 | void BOOLEAN_template::clean_up() | |
782 | { | |
783 | if (template_selection == VALUE_LIST || | |
784 | template_selection == COMPLEMENTED_LIST) | |
785 | delete [] value_list.list_value; | |
786 | template_selection = UNINITIALIZED_TEMPLATE; | |
787 | } | |
788 | ||
789 | void BOOLEAN_template::copy_template(const BOOLEAN_template& other_value) | |
790 | { | |
791 | switch (other_value.template_selection) { | |
792 | case SPECIFIC_VALUE: | |
793 | single_value = other_value.single_value; | |
794 | break; | |
795 | case OMIT_VALUE: | |
796 | case ANY_VALUE: | |
797 | case ANY_OR_OMIT: | |
798 | break; | |
799 | case VALUE_LIST: | |
800 | case COMPLEMENTED_LIST: | |
801 | value_list.n_values = other_value.value_list.n_values; | |
802 | value_list.list_value = new BOOLEAN_template[value_list.n_values]; | |
803 | for (unsigned int i = 0; i < value_list.n_values; i++) | |
804 | value_list.list_value[i].copy_template( | |
805 | other_value.value_list.list_value[i]); | |
806 | break; | |
807 | default: | |
808 | TTCN_error("Copying an uninitialized/unsupported boolean template."); | |
809 | } | |
810 | set_selection(other_value); | |
811 | } | |
812 | ||
813 | BOOLEAN_template::BOOLEAN_template() | |
814 | { | |
815 | ||
816 | } | |
817 | ||
818 | BOOLEAN_template::BOOLEAN_template(template_sel other_value) | |
819 | : Base_Template(other_value) | |
820 | { | |
821 | check_single_selection(other_value); | |
822 | } | |
823 | ||
824 | BOOLEAN_template::BOOLEAN_template(boolean other_value) | |
825 | : Base_Template(SPECIFIC_VALUE) | |
826 | { | |
827 | single_value = other_value; | |
828 | } | |
829 | ||
830 | BOOLEAN_template::BOOLEAN_template(const BOOLEAN& other_value) | |
831 | : Base_Template(SPECIFIC_VALUE) | |
832 | { | |
833 | other_value.must_bound("Creating a template from an unbound integer value."); | |
834 | single_value = other_value.boolean_value; | |
835 | } | |
836 | ||
837 | BOOLEAN_template::BOOLEAN_template(const OPTIONAL<BOOLEAN>& other_value) | |
838 | { | |
839 | switch (other_value.get_selection()) { | |
840 | case OPTIONAL_PRESENT: | |
841 | set_selection(SPECIFIC_VALUE); | |
842 | single_value = (boolean)(const BOOLEAN&)other_value; | |
843 | break; | |
844 | case OPTIONAL_OMIT: | |
845 | set_selection(OMIT_VALUE); | |
846 | break; | |
847 | default: | |
848 | TTCN_error("Creating a boolean template from an unbound optional field."); | |
849 | } | |
850 | } | |
851 | ||
852 | BOOLEAN_template::BOOLEAN_template(const BOOLEAN_template& other_value) | |
853 | : Base_Template() | |
854 | { | |
855 | copy_template(other_value); | |
856 | } | |
857 | ||
858 | BOOLEAN_template::~BOOLEAN_template() | |
859 | { | |
860 | clean_up(); | |
861 | } | |
862 | ||
863 | BOOLEAN_template& BOOLEAN_template::operator=(template_sel other_value) | |
864 | { | |
865 | check_single_selection(other_value); | |
866 | clean_up(); | |
867 | set_selection(other_value); | |
868 | return *this; | |
869 | } | |
870 | ||
871 | BOOLEAN_template& BOOLEAN_template::operator=(boolean other_value) | |
872 | { | |
873 | clean_up(); | |
874 | set_selection(SPECIFIC_VALUE); | |
875 | single_value = other_value; | |
876 | return *this; | |
877 | } | |
878 | ||
879 | BOOLEAN_template& BOOLEAN_template::operator=(const BOOLEAN& other_value) | |
880 | { | |
881 | other_value.must_bound("Assignment of an unbound boolean value to a " | |
882 | "template."); | |
883 | clean_up(); | |
884 | set_selection(SPECIFIC_VALUE); | |
885 | single_value = other_value.boolean_value; | |
886 | return *this; | |
887 | } | |
888 | ||
889 | BOOLEAN_template& BOOLEAN_template::operator= | |
890 | (const OPTIONAL<BOOLEAN>& other_value) | |
891 | { | |
892 | clean_up(); | |
893 | switch (other_value.get_selection()) { | |
894 | case OPTIONAL_PRESENT: | |
895 | set_selection(SPECIFIC_VALUE); | |
896 | single_value = (boolean)(const BOOLEAN&)other_value; | |
897 | break; | |
898 | case OPTIONAL_OMIT: | |
899 | set_selection(OMIT_VALUE); | |
900 | break; | |
901 | default: | |
902 | TTCN_error("Assignment of an unbound optional field to a boolean " | |
903 | "template."); | |
904 | } | |
905 | return *this; | |
906 | } | |
907 | ||
908 | BOOLEAN_template& BOOLEAN_template::operator= | |
909 | (const BOOLEAN_template& other_value) | |
910 | { | |
911 | if (&other_value != this) { | |
912 | clean_up(); | |
913 | copy_template(other_value); | |
914 | } | |
915 | return *this; | |
916 | } | |
917 | ||
3abe9331 | 918 | boolean BOOLEAN_template::match(boolean other_value, |
919 | boolean /* legacy */) const | |
970ed795 EL |
920 | { |
921 | switch (template_selection) { | |
922 | case SPECIFIC_VALUE: | |
923 | return single_value == other_value; | |
924 | case OMIT_VALUE: | |
925 | return FALSE; | |
926 | case ANY_VALUE: | |
927 | case ANY_OR_OMIT: | |
928 | return TRUE; | |
929 | case VALUE_LIST: | |
930 | case COMPLEMENTED_LIST: | |
931 | for (unsigned int i = 0; i < value_list.n_values; i++) | |
932 | if (value_list.list_value[i].match(other_value)) | |
933 | return template_selection == VALUE_LIST; | |
934 | return template_selection == COMPLEMENTED_LIST; | |
935 | default: | |
936 | TTCN_error("Matching with an uninitialized/unsupported boolean template."); | |
937 | } | |
938 | return FALSE; | |
939 | } | |
940 | ||
3abe9331 | 941 | boolean BOOLEAN_template::match(const BOOLEAN& other_value, |
942 | boolean /* legacy */) const | |
970ed795 EL |
943 | { |
944 | if (!other_value.is_bound()) return FALSE; | |
945 | return match(other_value.boolean_value); | |
946 | } | |
947 | ||
948 | boolean BOOLEAN_template::valueof() const | |
949 | { | |
950 | if (template_selection != SPECIFIC_VALUE || is_ifpresent) | |
951 | TTCN_error("Performing valueof or " | |
952 | "send operation on a non-specific boolean template."); | |
953 | return single_value; | |
954 | } | |
955 | ||
956 | void BOOLEAN_template::set_type(template_sel template_type, | |
957 | unsigned int list_length) | |
958 | { | |
959 | if (template_type != VALUE_LIST && template_type != COMPLEMENTED_LIST) | |
960 | TTCN_error("Setting an invalid list type for a boolean template."); | |
961 | clean_up(); | |
962 | set_selection(template_type); | |
963 | value_list.n_values = list_length; | |
964 | value_list.list_value = new BOOLEAN_template[list_length]; | |
965 | } | |
966 | ||
967 | BOOLEAN_template& BOOLEAN_template::list_item(unsigned int list_index) | |
968 | { | |
969 | if (template_selection != VALUE_LIST && | |
970 | template_selection != COMPLEMENTED_LIST) | |
971 | TTCN_error("Accessing a list element of a non-list boolean template."); | |
972 | if (list_index >= value_list.n_values) | |
973 | TTCN_error("Index overflow in a boolean value list template."); | |
974 | return value_list.list_value[list_index]; | |
975 | } | |
976 | ||
977 | void BOOLEAN_template::log() const | |
978 | { | |
979 | switch (template_selection) { | |
980 | case SPECIFIC_VALUE: | |
981 | TTCN_Logger::log_event_str(single_value ? "true" : "false"); | |
982 | break; | |
983 | case COMPLEMENTED_LIST: | |
984 | TTCN_Logger::log_event_str("complement "); | |
985 | // no break | |
986 | case VALUE_LIST: | |
987 | TTCN_Logger::log_char('('); | |
988 | for (unsigned int i = 0; i < value_list.n_values; i++) { | |
989 | if (i > 0) TTCN_Logger::log_event_str(", "); | |
990 | value_list.list_value[i].log(); | |
991 | } | |
992 | TTCN_Logger::log_char(')'); | |
993 | break; | |
994 | default: | |
995 | log_generic(); | |
996 | break; | |
997 | } | |
998 | log_ifpresent(); | |
999 | } | |
1000 | ||
3abe9331 | 1001 | void BOOLEAN_template::log_match(const BOOLEAN& match_value, |
1002 | boolean /* legacy */) const | |
970ed795 EL |
1003 | { |
1004 | if (TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity() | |
1005 | && TTCN_Logger::get_logmatch_buffer_len() != 0) { | |
1006 | TTCN_Logger::print_logmatch_buffer(); | |
1007 | TTCN_Logger::log_event_str(" := "); | |
1008 | } | |
1009 | match_value.log(); | |
1010 | TTCN_Logger::log_event_str(" with "); | |
1011 | log(); | |
1012 | if (match(match_value)) TTCN_Logger::log_event_str(" matched"); | |
1013 | else TTCN_Logger::log_event_str(" unmatched"); | |
1014 | } | |
1015 | ||
1016 | void BOOLEAN_template::set_param(Module_Param& param) { | |
1017 | param.basic_check(Module_Param::BC_TEMPLATE, "boolean template"); | |
3abe9331 | 1018 | Module_Param_Ptr mp = ¶m; |
1019 | if (param.get_type() == Module_Param::MP_Reference) { | |
1020 | mp = param.get_referenced_param(); | |
1021 | } | |
1022 | switch (mp->get_type()) { | |
970ed795 EL |
1023 | case Module_Param::MP_Omit: |
1024 | *this = OMIT_VALUE; | |
1025 | break; | |
1026 | case Module_Param::MP_Any: | |
1027 | *this = ANY_VALUE; | |
1028 | break; | |
1029 | case Module_Param::MP_AnyOrNone: | |
1030 | *this = ANY_OR_OMIT; | |
1031 | break; | |
1032 | case Module_Param::MP_List_Template: | |
3abe9331 | 1033 | case Module_Param::MP_ComplementList_Template: { |
1034 | BOOLEAN_template temp; | |
1035 | temp.set_type(mp->get_type() == Module_Param::MP_List_Template ? | |
1036 | VALUE_LIST : COMPLEMENTED_LIST, mp->get_size()); | |
1037 | for (size_t i=0; i<mp->get_size(); i++) { | |
1038 | temp.list_item(i).set_param(*mp->get_elem(i)); | |
970ed795 | 1039 | } |
3abe9331 | 1040 | *this = temp; |
1041 | break; } | |
970ed795 | 1042 | case Module_Param::MP_Boolean: |
3abe9331 | 1043 | *this = mp->get_boolean(); |
970ed795 EL |
1044 | break; |
1045 | default: | |
1046 | param.type_error("boolean template"); | |
1047 | } | |
3abe9331 | 1048 | is_ifpresent = param.get_ifpresent() || mp->get_ifpresent(); |
1049 | } | |
1050 | ||
1051 | Module_Param* BOOLEAN_template::get_param(Module_Param_Name& param_name) const | |
1052 | { | |
1053 | Module_Param* mp = NULL; | |
1054 | switch (template_selection) { | |
1055 | case UNINITIALIZED_TEMPLATE: | |
1056 | mp = new Module_Param_Unbound(); | |
1057 | break; | |
1058 | case OMIT_VALUE: | |
1059 | mp = new Module_Param_Omit(); | |
1060 | break; | |
1061 | case ANY_VALUE: | |
1062 | mp = new Module_Param_Any(); | |
1063 | break; | |
1064 | case ANY_OR_OMIT: | |
1065 | mp = new Module_Param_AnyOrNone(); | |
1066 | break; | |
1067 | case SPECIFIC_VALUE: | |
1068 | mp = new Module_Param_Boolean(single_value); | |
1069 | break; | |
1070 | case VALUE_LIST: | |
1071 | case COMPLEMENTED_LIST: { | |
1072 | if (template_selection == VALUE_LIST) { | |
1073 | mp = new Module_Param_List_Template(); | |
1074 | } | |
1075 | else { | |
1076 | mp = new Module_Param_ComplementList_Template(); | |
1077 | } | |
1078 | for (size_t i = 0; i < value_list.n_values; ++i) { | |
1079 | mp->add_elem(value_list.list_value[i].get_param(param_name)); | |
1080 | } | |
1081 | break; } | |
1082 | default: | |
1083 | break; | |
1084 | } | |
1085 | if (is_ifpresent) { | |
1086 | mp->set_ifpresent(); | |
1087 | } | |
1088 | return mp; | |
970ed795 EL |
1089 | } |
1090 | ||
1091 | void BOOLEAN_template::encode_text(Text_Buf& text_buf) const | |
1092 | { | |
1093 | encode_text_base(text_buf); | |
1094 | switch (template_selection) { | |
1095 | case OMIT_VALUE: | |
1096 | case ANY_VALUE: | |
1097 | case ANY_OR_OMIT: | |
1098 | break; | |
1099 | case SPECIFIC_VALUE: | |
1100 | text_buf.push_int(single_value ? 1 : 0); | |
1101 | break; | |
1102 | case VALUE_LIST: | |
1103 | case COMPLEMENTED_LIST: | |
1104 | text_buf.push_int(value_list.n_values); | |
1105 | for (unsigned int i = 0; i < value_list.n_values; i++) | |
1106 | value_list.list_value[i].encode_text(text_buf); | |
1107 | break; | |
1108 | default: | |
1109 | TTCN_error("Text encoder: Encoding an uninitialized/unsupported boolean " | |
1110 | "template."); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | void BOOLEAN_template::decode_text(Text_Buf& text_buf) | |
1115 | { | |
1116 | clean_up(); | |
1117 | decode_text_base(text_buf); | |
1118 | switch (template_selection) { | |
1119 | case OMIT_VALUE: | |
1120 | case ANY_VALUE: | |
1121 | case ANY_OR_OMIT: | |
1122 | break; | |
1123 | case SPECIFIC_VALUE: { | |
1124 | int int_value = text_buf.pull_int().get_val(); | |
1125 | switch (int_value) { | |
1126 | case 0: | |
1127 | single_value = FALSE; | |
1128 | break; | |
1129 | case 1: | |
1130 | single_value = TRUE; | |
1131 | break; | |
1132 | default: | |
1133 | TTCN_error("Text decoder: An invalid boolean value (%d) was received for " | |
1134 | "a template.", int_value); | |
1135 | } | |
1136 | break; } | |
1137 | case VALUE_LIST: | |
1138 | case COMPLEMENTED_LIST: | |
1139 | value_list.n_values = text_buf.pull_int().get_val(); | |
1140 | value_list.list_value = new BOOLEAN_template[value_list.n_values]; | |
1141 | for (unsigned int i = 0; i < value_list.n_values; i++) | |
1142 | value_list.list_value[i].decode_text(text_buf); | |
1143 | break; | |
1144 | default: | |
1145 | TTCN_error("Text decoder: An unknown/unsupported selection was " | |
1146 | "received for a boolean template."); | |
1147 | } | |
1148 | } | |
1149 | ||
3abe9331 | 1150 | boolean BOOLEAN_template::is_present(boolean legacy /* = FALSE */) const |
970ed795 EL |
1151 | { |
1152 | if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE; | |
3abe9331 | 1153 | return !match_omit(legacy); |
970ed795 EL |
1154 | } |
1155 | ||
3abe9331 | 1156 | boolean BOOLEAN_template::match_omit(boolean legacy /* = FALSE */) const |
970ed795 EL |
1157 | { |
1158 | if (is_ifpresent) return TRUE; | |
1159 | switch (template_selection) { | |
1160 | case OMIT_VALUE: | |
1161 | case ANY_OR_OMIT: | |
1162 | return TRUE; | |
1163 | case VALUE_LIST: | |
1164 | case COMPLEMENTED_LIST: | |
3abe9331 | 1165 | if (legacy) { |
1166 | // legacy behavior: 'omit' can appear in the value/complement list | |
1167 | for (unsigned int i=0; i<value_list.n_values; i++) | |
1168 | if (value_list.list_value[i].match_omit()) | |
1169 | return template_selection==VALUE_LIST; | |
1170 | return template_selection==COMPLEMENTED_LIST; | |
1171 | } | |
1172 | // else fall through | |
970ed795 EL |
1173 | default: |
1174 | return FALSE; | |
1175 | } | |
1176 | return FALSE; | |
1177 | } | |
1178 | ||
1179 | #ifndef TITAN_RUNTIME_2 | |
3abe9331 | 1180 | void BOOLEAN_template::check_restriction(template_res t_res, const char* t_name, |
1181 | boolean legacy /* = FALSE */) const | |
970ed795 EL |
1182 | { |
1183 | if (template_selection==UNINITIALIZED_TEMPLATE) return; | |
1184 | switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) { | |
1185 | case TR_VALUE: | |
1186 | if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return; | |
1187 | break; | |
1188 | case TR_OMIT: | |
1189 | if (!is_ifpresent && (template_selection==OMIT_VALUE || | |
1190 | template_selection==SPECIFIC_VALUE)) return; | |
1191 | break; | |
1192 | case TR_PRESENT: | |
3abe9331 | 1193 | if (!match_omit(legacy)) return; |
970ed795 EL |
1194 | break; |
1195 | default: | |
1196 | return; | |
1197 | } | |
1198 | TTCN_error("Restriction `%s' on template of type %s violated.", | |
1199 | get_res_name(t_res), t_name ? t_name : "boolean"); | |
1200 | } | |
1201 | #endif |