1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
19 * Szabo, Janos Zoltan – initial implementation
23 ******************************************************************************/
30 #include "JSON_Tokenizer.hh"
32 #ifdef TITAN_RUNTIME_2
33 #include "Struct_of.hh"
36 #include "RefdIndex.hh"
39 struct ASN_BERdescriptor_t;
41 struct TTCN_RAWdescriptor_t;
42 struct TTCN_TEXTdescriptor_t;
43 struct XERdescriptor_t;
44 struct TTCN_JSONdescriptor_t;
47 class Module_Param_Name;
48 struct embed_values_enc_struct_t;
49 struct embed_values_dec_struct_t;
51 /** @brief Type descriptor
53 * There is one type descriptor object for each type.
54 * Descriptors for built-in types are supplied by the runtime.
55 * Descriptors for user-defined types are written by the compiler
56 * in the generated code.
58 struct TTCN_Typedescriptor_t {
59 const char * const name; /**< Name of the type, e.g INTEGER, REAL, verdicttype, etc */
60 const ASN_BERdescriptor_t * const ber; /**< Information for BER coding */
61 const TTCN_RAWdescriptor_t * const raw; /**< Information for RAW coding */
62 const TTCN_TEXTdescriptor_t * const text; /**< Information for TEXT coding */
63 const XERdescriptor_t * const xer; /**< Information for XER */
64 const TTCN_JSONdescriptor_t * const json; /**< Information for JSON coding */
65 const TTCN_Typedescriptor_t * const oftype_descr; /**< Record-of element's type descriptor */
68 * Used by classes implementing more than one ASN.1 type
69 * (OBJID and UNIVERSAL_CHARSTRING).
73 UNIVERSALSTRING, BMPSTRING, UTF8STRING,
74 TELETEXSTRING, VIDEOTEXSTRING, GRAPHICSTRING, GENERALSTRING,
79 #ifdef TITAN_RUNTIME_2
81 struct Erroneous_value_t {
83 const Base_Type * const errval; // NULL if `omit'
84 const TTCN_Typedescriptor_t* type_descr; // NULL if `omit' or raw
87 struct Erroneous_values_t {
88 const int field_index;
89 const char* field_qualifier;
90 const Erroneous_value_t * const before;
91 const Erroneous_value_t * const value;
92 const Erroneous_value_t * const after;
95 struct Erroneous_descriptor_t {
96 const int field_index;
97 const int omit_before; // -1 if none
98 const char* omit_before_qualifier; // NULL if none
99 const int omit_after; // -1 if none
100 const char* omit_after_qualifier; // NULL if none
101 const int values_size;
102 const Erroneous_values_t * const values_vec;
103 const int embedded_size;
104 const Erroneous_descriptor_t * const embedded_vec;
105 /** search in values_vec for the field with index field_idx */
106 const Erroneous_values_t* get_field_err_values(int field_idx) const;
107 /** search in embedded_vec for the field with index field_idx */
108 const Erroneous_descriptor_t* get_field_emb_descr(int field_idx) const;
109 /** if the current element of values_vec has index field_idx then returns it and increments values_idx */
110 const Erroneous_values_t* next_field_err_values(const int field_idx, int& values_idx) const;
111 /** if the current element of embedded_vec has index field_idx then returns it and increments edescr_idx */
112 const Erroneous_descriptor_t* next_field_emb_descr(const int field_idx, int& edescr_idx) const;
119 class TTCN_Type_list;
121 class Limit_Token_List;
122 #ifdef TITAN_RUNTIME_2
126 #ifdef TITAN_RUNTIME_2
127 #define VIRTUAL_IF_RUNTIME_2 virtual
129 #define VIRTUAL_IF_RUNTIME_2
133 * The base class for all types in TTCN-3 runtime environment.
135 * Uses the compiler-generated default constructor, copy constructor
136 * and assignment operator (they do nothing because the class is empty).
141 /** Whether the value is present.
142 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility!
143 * causes DTE, must be used only if the field is OPTIONAL<>
145 VIRTUAL_IF_RUNTIME_2 boolean ispresent() const;
147 VIRTUAL_IF_RUNTIME_2 void log() const;
149 /** Check whether the XML encoding of the type can begin with an XML element
150 * having the specified name and namespace.
152 * This function checks for the "starter tag" of the type. This usually
153 * means the tag should match the name in the XER descriptor,
154 * with a few notable exceptions:
156 * - If the type is untagged, then
157 * - if it's a record, all fields up to the the first non-optional field
159 * - if it's a record-of, the element is checked.
160 * - A \c universal \c charstring with ANY-ELEMENT can begin with any tag.
162 * @param name the XML element name (local name)
163 * @param uri the namespace URI of the element
164 * @param xd XER descriptor
165 * @param flavor one or more bits from XER_flavor. Only XER_EXTENDED counts
166 * @return TRUE if the XML element can begin the XER encoding of the type,
169 static boolean can_start(const char *name, const char *uri,
170 XERdescriptor_t const& xd, unsigned int flavor);
172 #ifdef TITAN_RUNTIME_2
173 /** Initialize this object (or one of its fields/elements) with a
174 * module parameter value. The module parameter may contain references to
175 * other module parameters or module parameter expressions, which are processed
176 * by this method to calculated the final result.
177 * @param param module parameter value (its ID specifies which object is to be set) */
178 virtual void set_param(Module_Param& param) = 0;
179 /** Create a module parameter value equivalent to this object (or one of its
181 * @param param_name module parameter ID, specifies which object to convert */
182 virtual Module_Param* get_param(Module_Param_Name& param_name) const = 0;
183 /** Whether the type is a sequence-of.
184 * @return \c FALSE */
185 virtual boolean is_seof() const { return FALSE; }
187 virtual void encode_text(Text_Buf& text_buf) const = 0;
188 virtual void decode_text(Text_Buf& text_buf) = 0;
190 virtual boolean is_bound() const = 0;
191 virtual boolean is_value() const { return is_bound(); }
192 virtual void clean_up() = 0;
194 /** returns if the value of this is equal to the value of the parameter. */
195 virtual boolean is_equal(const Base_Type* other_value) const = 0;
197 /** Performs: *this = *other_value.
198 * Does not take ownership of \p other_value. */
199 virtual void set_value(const Base_Type* other_value) = 0;
201 /** creates a copy of this object, returns a pointer to it. */
202 virtual Base_Type* clone() const = 0;
204 /** return reference to the type descriptor object for this type. */
205 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
207 /** @name override in class OPTIONAL<T>
209 virtual boolean is_optional() const { return FALSE; }
211 /** Is the optional value present or omit.
212 * @return \c true if present, \c false otherwise
213 * used for TTCN-3 ispresent()
215 virtual boolean is_present() const;
217 /** Access the embedded type of the optional.
219 * The implementation in Base_Type always causes a dynamic testcase error.
221 virtual Base_Type* get_opt_value();
223 /** Access the embedded type of the optional.
225 * The implementation in Base_Type always causes a dynamic testcase error.
227 virtual const Base_Type* get_opt_value() const;
229 /** Set the optional field to \c omit.
230 * The implementation in Base_Type always causes a dynamic testcase error.
232 virtual void set_to_omit();
234 /** Set the optional field to present.
235 * The implementation in Base_Type always causes a dynamic testcase error.
237 virtual void set_to_present();
240 virtual ~Base_Type() { }
244 /** Do nothing for non-structured types. */
245 VIRTUAL_IF_RUNTIME_2 void set_implicit_omit() { }
247 /** @brief Encode an instance of this object
251 @param p_td type descriptor
253 @param p_coding coding type to select BER, RAW or TEXT coding
256 VIRTUAL_IF_RUNTIME_2 void encode(const TTCN_Typedescriptor_t& p_td,
257 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const;
259 /** @brief Decode an instance of this object
263 @param p_td type descriptor
265 @param p_coding coding type to select BER, RAW or TEXT coding
268 VIRTUAL_IF_RUNTIME_2 void decode(const TTCN_Typedescriptor_t& p_td,
269 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...);
272 /** Check type descriptor for BER encoding
274 * @param p_td a TTCN type descriptor
276 * Calls TTCN_EncDec_ErrorContext::error_internal if
277 * p_td has no BER descriptor.
279 static void BER_chk_descr(const TTCN_Typedescriptor_t& p_td);
281 /** Check the BER coding
283 * If \p p_coding is either BER_ENCODE_CER or BER_ENCODE_DER,
284 * this function does nothing. Otherwise, it issues a warning
285 * and sets \p p_coding to BER_ENCODE_DER.
287 * @param[in, out] p_coding BER coding
288 * @post p_coding is either BER_ENCODE_CER or BER_ENCODE_DER
290 static void BER_encode_chk_coding(unsigned& p_coding);
292 /** Check type descriptor for XER encoding and the XER coding variant
294 * @param[in, out] p_coding XER coding
295 * @param[in] p_td TTCN type descriptor
297 * If \p p_td has no XER descriptor, calls
298 * TTCN_EncDec_ErrorContext::error_internal()
300 * If \p p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED,
301 * this function does nothing. Otherwise, it issues a warning
302 * and sets \p p_coding to XER_BASIC.
304 * @post p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED
306 static void XER_encode_chk_coding(unsigned& p_coding,
307 const TTCN_Typedescriptor_t& p_td);
309 /** Check bound and create a 0-length TLV in case unbound is ignored.
311 * @param[in] p_isbound
312 * @return a newly constructed and empty \c ASN_BER_TLV_t
313 * if p_isbound is false, or NULL if p_isbound is true
314 * @note if p_isbound is false, this function calls
315 * TTCN_EncDec_ErrorContext::error with TTCN_EncDec::ET_UNBOUND.
316 * If that error is not ignored, this function does not return at all.
318 static ASN_BER_TLV_t* BER_encode_chk_bound(boolean p_isbound);
321 static void BER_encode_putoctets_OCTETSTRING
322 (unsigned char *target,
323 unsigned int octetnum_start, unsigned int octet_count,
324 int p_nof_octets, const unsigned char *p_octets_ptr);
327 static ASN_BER_TLV_t* BER_encode_TLV_OCTETSTRING
329 int p_nof_octets, const unsigned char *p_octets_ptr);
331 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
332 const int_val_t& p_int_val);
333 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
334 const int& p_int_val);
336 static void BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
337 boolean p_isvalid, int p_value);
339 static void BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
342 static boolean BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
345 ASN_BER_TLV_t& p_target_tlv);
347 static void BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
350 ASN_BER_TLV_t& p_target_tlv,
351 boolean tlv_present);
353 static void BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
354 const ASN_BER_TLV_t& p_tlv,
356 ASN_BER_TLV_t& stripped_tlv);
359 static void BER_decode_getoctets_OCTETSTRING
360 (const unsigned char *source, size_t s_len,
361 unsigned int& octetnum_start,
362 int& p_nof_octets, unsigned char *p_octets_ptr);
365 static void BER_decode_TLV_OCTETSTRING
366 (const ASN_BER_TLV_t& p_tlv, unsigned L_form,
367 unsigned int& octetnum_start,
368 int& p_nof_octets, unsigned char *p_octets_ptr);
370 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
371 unsigned L_form, int_val_t& p_int_val);
372 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
373 unsigned L_form, int& p_int_val);
375 static boolean BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
376 const ASN_BER_TLV_t& p_tlv,
378 ASN_BER_TLV_t& p_target_tlv);
380 static boolean BER_decode_CHOICE_selection(boolean select_result,
381 const ASN_BER_TLV_t& p_tlv);
383 static void BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
384 boolean p_isvalid, int p_value);
387 VIRTUAL_IF_RUNTIME_2 ASN_BER_TLV_t* BER_encode_TLV(
388 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
389 #ifdef TITAN_RUNTIME_2
390 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
391 const TTCN_Typedescriptor_t& /*p_td*/, unsigned /*p_coding*/) const;
392 virtual ASN_BER_TLV_t* BER_encode_negtest_raw() const;
393 virtual int encode_raw(TTCN_Buffer& p_buf) const;
394 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
395 virtual int JSON_encode_negtest_raw(JSON_Tokenizer&) const;
398 /** Examines whether this message corresponds the tags in the
399 * descriptor. If the BER descriptor contains no tags, then returns
400 * TRUE. Otherwise, examines the first (outermost) tag only. */
401 #ifdef TITAN_RUNTIME_2
406 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
407 const ASN_BER_TLV_t& p_tlv);
409 /** Returns TRUE on success, FALSE otherwise. */
410 VIRTUAL_IF_RUNTIME_2 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
411 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
413 VIRTUAL_IF_RUNTIME_2 void BER_decode_opentypes(
414 TTCN_Type_list& /*p_typelist*/, unsigned /*L_form*/) {}
416 /** Encode with RAW coding.
417 * @return the length of the encoding
418 * @param p_td type descriptor
419 * @param myleaf filled with RAW encoding data
420 * @note Basetype::RAW_encode throws an error. */
421 VIRTUAL_IF_RUNTIME_2 int RAW_encode(const TTCN_Typedescriptor_t& p_td,
422 RAW_enc_tree& myleaf) const;
423 #ifdef TITAN_RUNTIME_2
424 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
425 const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
428 /** Decode with RAW coding
430 * @param p_td type descriptor
431 * @param p_buf buffer with data to be decoded
432 * @param limit number of bits the decoder is allowed to use. At the top level
433 * this is 8x the number of bytes in the buffer.
434 * @param top_bit_ord (LSB/MSB) from TTCN_RAWdescriptor_t::top_bit_order
435 * @param no_err set to TRUE if the decoder is to return errors silently,
436 * without calling TTCN_EncDec_ErrorContext::error
437 * @param sel_field selected field indicator for CROSSTAG, or -1
438 * @param first_call default TRUE. May be FALSE for a REPEATABLE record-of
439 * inside a set, if an element has been successfully decoded.
440 * @return length of decoded field, or a negative number for error
441 * @note Basetype::RAW_decode throws an error. */
442 VIRTUAL_IF_RUNTIME_2 int RAW_decode(
443 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int limit,
444 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1,
445 boolean first_call=TRUE);
448 /** Encode with TEXT encoding.
449 * @return the length of the encoding
450 * @param p_td type descriptor
451 * @param p_buf buffer for the encoded data
452 * @note Basetype::TEXT_encode throws an error. */
453 VIRTUAL_IF_RUNTIME_2 int TEXT_encode(const TTCN_Typedescriptor_t& p_td,
454 TTCN_Buffer& p_buf) const;
456 #ifdef TITAN_RUNTIME_2
457 /** Encode with TEXT encoding negative test.
458 * @return the length of the encoding
459 * @param p_err_descr type descriptor
460 * @param p_td type descriptor
461 * @param p_buf buffer for the encoded data
462 * @note Basetype::TEXT_encode_negtest throws an error. */
463 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
464 TTCN_Buffer& p_buf) const;
468 * @return decoded length
469 * @note Basetype::TEXT_decode throws an error. */
470 VIRTUAL_IF_RUNTIME_2 int TEXT_decode(
471 const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
472 boolean no_err=FALSE, boolean first_call=TRUE);
474 /** Write the XER encoding of the current object into the buffer.
476 * @param p_td type descriptor
477 * @param p_buf buffer
478 * @param flavor one of XER_flavor values
479 * @param indent indentation level
480 * @param emb_val embed values data (only relevant for record of types)
481 * @return number of bytes written into the buffer
483 VIRTUAL_IF_RUNTIME_2 int XER_encode(const XERdescriptor_t& p_td,
484 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
486 #ifdef TITAN_RUNTIME_2
487 virtual int XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
488 const XERdescriptor_t& /*p_td*/, TTCN_Buffer& /*p_buf*/,
489 unsigned int /*flavor*/, int /*indent*/, embed_values_enc_struct_t* /*emb_val*/) const;
492 /** Decode the current object from the supplied buffer.
494 * The XML pull parser presents a forward iterator over the XML elements.
495 * @pre Upon entering this function, the current node should be the one corresponding
496 * to this object, or possibly a text or whitespace node before it.
497 * Code should begin like this:
499 * int Foo::XER_decode() {
500 * int success=1, type;
501 * for ( ; success==1; success=reader.Read() ) {
502 * int type=reader.NodeType();
503 * if (XML_READER_TYPE_ELEMENT==type) {
510 * @post Upon leaving the function, the current node should be \b past
511 * the end tag, i.e. the next start tag (or any whitespace before it).
513 * It is important not to advance the parser too far, to avoid "stealing"
514 * the content of other fields (the parser can only go forward).
516 * @param p_td type descriptor
517 * @param reader Wrapper around the XML processor
518 * @param flavor one of XER_flavor values
519 * @param flavor2 one of XER_flavor2 values
520 * @param emb_val embed values data (only relevant for record of types)
521 * @return number of bytes "consumed"
523 VIRTUAL_IF_RUNTIME_2 int XER_decode(const XERdescriptor_t& p_td,
524 XmlReaderWrap& reader, unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val);
526 /** Return an array of namespace declarations.
528 * @param[in] p_td XER descriptor of the type.
529 * @param[out] num set to the number of strings returned.
530 * @param[out] def_ns set to @p true if a default namespace was encountered
531 * @return an array of strings or NULL. All the strings in the array
532 * and the array itself must be deallocated by the caller (if num>0).
534 * The strings are in the following format:
535 * @code " xmlns:prefix='uri'" @endcode
536 * (the space at start allows direct concatenation of the strings)
538 VIRTUAL_IF_RUNTIME_2 char ** collect_ns(const XERdescriptor_t& p_td,
539 size_t& num, bool& def_ns) const;
541 /** Copy strings from @p new_namespaces to @p collected_ns,
542 * discarding duplicates.
544 * May reallocate @p collected_ns, in which case @p num_collected
545 * is adjusted accordingly.
547 * @param collected_ns target array
548 * @param num_collected number of already collected namespaces
549 * @param new_namespaces new array
550 * @param num_new number of new namespaces
552 * @post new_namespaces has been deallocated and set to NULL.
554 static void merge_ns(char **&collected_ns, size_t& num_collected,
555 char **new_namespaces, size_t num_new);
557 typedef char** (Base_Type::*collector_fn)
558 (const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
560 /** Start writing the XML representation
562 * @param[in] p_td XER descriptor
563 * @param[out] p_buf buffer to write into
564 * @param[in,out] flavor XER variant (basic, canonical or extended),
565 * also used to pass various flags between begin_xml and its caller
566 * @param[in] indent indentation level
567 * @param[in] empty true if an empty-element tag is needed
568 * @param[in] collector namespace collector function
569 * @param[in] type_atr type identification attribute for useNil/useUnion,
570 * in the following format: "xsi:type='foo'"
571 * @return "omit_tag": zero if the type's own tag was written (not omitted
572 * e.g. due to untagged).
573 * If the XML tag was omitted, the return value is nonzero.
574 * The special value -1 is returned if the XML in the buffer
575 * was shortened by exactly one character.
577 VIRTUAL_IF_RUNTIME_2 int begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
578 unsigned int& flavor, int indent, bool empty,
579 collector_fn collector = &Base_Type::collect_ns, const char *type_atr = NULL) const;
580 /** Finish the XML representation.
582 * @param[in] p_td XER descriptor
583 * @param[out] p_buf buffer to write into
584 * @param[in] flavor XER variant (basic, canonical or extended),
585 * also used to pass various flags
586 * @param[in] indent indentation level
587 * @param[in] empty true if an empty-element tag is needed
589 VIRTUAL_IF_RUNTIME_2 void end_xml (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
590 unsigned int flavor, int indent, bool empty) const;
593 * @return encoded length
594 * @note Basetype::JSON_encode throws an error. */
595 VIRTUAL_IF_RUNTIME_2 int JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&) const;
597 #ifdef TITAN_RUNTIME_2
598 /** Encode with JSON encoding negative test.
599 * @return the length of the encoding
600 * @param p_err_descr erroneous type descriptor
601 * @param p_td type descriptor
602 * @param p_tok JSON tokenizer for the encoded data
603 * @note Basetype::JSON_encode_negtest throws an error. */
604 virtual int JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
605 JSON_Tokenizer& p_tok) const;
609 * @return decoded length
610 * @note Basetype::JSON_decode throws an error. */
611 VIRTUAL_IF_RUNTIME_2 int JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean);
613 /** not a component by default (components will return true) */
614 inline boolean is_component() { return FALSE; }
618 * This class is used by BER decoding to handle
619 * ComponentRelationConstraint.
621 class TTCN_Type_list {
624 const Base_Type **types;
625 /// Copy constructor disabled
626 TTCN_Type_list(const TTCN_Type_list&);
627 /// Assignment disabled
628 TTCN_Type_list& operator=(const TTCN_Type_list&);
630 TTCN_Type_list() : n_types(0), types(NULL) {}
632 void push(const Base_Type *p_type);
633 const Base_Type* pop();
634 /** If \a pos is 0 then returns the outermost, otherwise the "pos"th
636 const Base_Type* get_nth(size_t pos) const;
639 /** @name Type descriptor objects
642 extern const TTCN_Typedescriptor_t BOOLEAN_descr_;
643 extern const TTCN_Typedescriptor_t INTEGER_descr_;
644 extern const TTCN_Typedescriptor_t FLOAT_descr_;
645 extern const TTCN_Typedescriptor_t VERDICTTYPE_descr_;
646 extern const TTCN_Typedescriptor_t OBJID_descr_;
647 extern const TTCN_Typedescriptor_t BITSTRING_descr_;
648 extern const TTCN_Typedescriptor_t HEXSTRING_descr_;
649 extern const TTCN_Typedescriptor_t OCTETSTRING_descr_;
650 extern const TTCN_Typedescriptor_t CHARSTRING_descr_;
651 extern const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_;
652 extern const TTCN_Typedescriptor_t COMPONENT_descr_;
653 extern const TTCN_Typedescriptor_t DEFAULT_descr_;
654 extern const TTCN_Typedescriptor_t ASN_NULL_descr_;
655 extern const TTCN_Typedescriptor_t ASN_ANY_descr_;
656 extern const TTCN_Typedescriptor_t EXTERNAL_descr_;
657 extern const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_;
658 extern const TTCN_Typedescriptor_t CHARACTER_STRING_descr_;
659 extern const TTCN_Typedescriptor_t ObjectDescriptor_descr_;
660 extern const TTCN_Typedescriptor_t UTF8String_descr_;
661 extern const TTCN_Typedescriptor_t ASN_ROID_descr_;
662 extern const TTCN_Typedescriptor_t NumericString_descr_;
663 extern const TTCN_Typedescriptor_t PrintableString_descr_;
664 extern const TTCN_Typedescriptor_t TeletexString_descr_;
665 extern const TTCN_Typedescriptor_t& T61String_descr_;
666 extern const TTCN_Typedescriptor_t VideotexString_descr_;
667 extern const TTCN_Typedescriptor_t IA5String_descr_;
668 extern const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_;
669 extern const TTCN_Typedescriptor_t ASN_UTCTime_descr_;
670 extern const TTCN_Typedescriptor_t GraphicString_descr_;
671 extern const TTCN_Typedescriptor_t VisibleString_descr_;
672 extern const TTCN_Typedescriptor_t& ISO646String_descr_;
673 extern const TTCN_Typedescriptor_t GeneralString_descr_;
674 extern const TTCN_Typedescriptor_t UniversalString_descr_;
675 extern const TTCN_Typedescriptor_t BMPString_descr_;
678 #ifdef TITAN_RUNTIME_2
683 /** The base class of all record-of types when using RT2
686 // Record_Of_Template can be found in Template.hh
687 class Record_Of_Type : public Base_Type, public RefdIndexInterface
689 friend class Set_Of_Template;
690 friend class Record_Of_Template;
692 friend boolean operator==(null_type null_value,
693 const Record_Of_Type& other_value);
694 struct recordof_setof_struct {
697 Base_Type **value_elements;
699 Erroneous_descriptor_t* err_descr;
701 struct refd_index_struct {
702 /** Stores the indices of elements that are referenced by 'out' and 'inout' parameters.
703 * These elements must not be deleted.*/
704 Vector<int> refd_indices;
706 /** Cached maximum value of \a refd_indices (default: -1).*/
710 static boolean compare_function(const Record_Of_Type *left_ptr, int left_index, const Record_Of_Type *right_ptr, int right_index);
711 Record_Of_Type() : val_ptr(NULL), err_descr(NULL), refd_ind_ptr(NULL) {}
712 Record_Of_Type(null_type other_value);
713 Record_Of_Type(const Record_Of_Type& other_value);
714 /// Assignment disabled
715 Record_Of_Type& operator=(const Record_Of_Type& other_value);
717 /** Returns the number of actual elements in the record of (does not count the
718 * unbound elements left at the end of the record of struct to make sure
719 * referenced elements are not deleted). */
720 int get_nof_elements() const;
722 /** Returns true if the indexed element is bound */
723 bool is_elem_bound(int index) const;
725 /** Returns the highest referenced index (uses \a max_refd_index as its cache)*/
726 int get_max_refd_index();
728 /** Returns true if the element at the given index is referenced by an 'out' or
729 * 'inout' parameter. */
730 bool is_index_refd(int index);
733 void set_val(null_type other_value);
735 boolean operator==(null_type other_value) const;
736 boolean operator!=(null_type other_value) const;
738 Base_Type* get_at(int index_value);
739 Base_Type* get_at(const INTEGER& index_value);
740 const Base_Type* get_at(int index_value) const;
741 const Base_Type* get_at(const INTEGER& index_value) const;
743 virtual boolean is_equal(const Base_Type* other_value) const;
744 virtual void set_value(const Base_Type* other_value);
746 /* following functions return rec_of or this or other_value */
747 Record_Of_Type* rotl(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
748 Record_Of_Type* rotr(int rotate_count, Record_Of_Type* rec_of) const;
749 Record_Of_Type* rotr(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
750 Record_Of_Type* concat(const Record_Of_Type* other_value, Record_Of_Type* rec_of) const;
751 /* parameter rec_of must be a newly created descendant object, these helper functions fill it */
752 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
753 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
754 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
755 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
757 void set_size(int new_size);
759 /** Implementation for isbound.
760 * @return \c true if the Record_Of itself is bound, \c false otherwise.
761 * Ignores elements. No need to be overridden. */
762 virtual boolean is_bound() const;
763 /** Implementation for isvalue.
764 * @return \c true if all elements are bound, \c false otherwise */
765 virtual boolean is_value() const;
766 virtual void clean_up();
768 virtual boolean is_seof() const { return TRUE; }
770 /** Implementation for the \c sizeof operation
771 * @return the number of elements */
773 int n_elem() const { return size_of(); }
775 /** Implementation for the lengthof operation.
776 * @return the index of the last bound element +1 */
777 int lengthof() const;
778 virtual void log() const;
779 virtual void set_param(Module_Param& param);
780 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
781 virtual void set_implicit_omit();
782 virtual void encode_text(Text_Buf& text_buf) const;
783 virtual void decode_text(Text_Buf& text_buf);
785 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
787 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
788 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
790 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
791 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
792 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
794 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
795 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
798 * @param td type descriptor
800 * @param limit max number of bits that can be used
802 * @param no_err (not used)
804 * @param first_call default true, false for second and subsequent calls
805 * of a REPEATABLE record-of. Equivalent to "not repeated"
806 * @return number of bits used, or a negative number in case of error
808 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
809 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
810 /// raw extension bit
811 virtual int rawdec_ebv() const;
813 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
814 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
815 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
817 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
818 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
819 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
820 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned flavor, int indent, embed_values_enc_struct_t*) const;
821 /// Helper for XER_encode_negtest
822 int encode_element(int i, const XERdescriptor_t& p_td, const Erroneous_values_t* err_vals,
823 const Erroneous_descriptor_t* emb_descr,
824 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
825 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int, unsigned int, embed_values_dec_struct_t*);
826 virtual boolean isXerAttribute() const;
827 virtual boolean isXmlValueList() const;
829 /** Encodes accordingly to the JSON encoding rules.
830 * Returns the length of the encoded data. */
831 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
833 /** Negative testing for the JSON encoder
834 * Encodes this value according to the JSON encoding rules, but with the
835 * modifications (errors) specified in the erroneous descriptor parameter. */
836 int JSON_encode_negtest(const Erroneous_descriptor_t*, const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
838 /** Decodes accordingly to the JSON encoding rules.
839 * Returns the length of the decoded data. */
840 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
842 /** @returns \c true if this is a set-of type,
843 * \c false if this is a record-of type */
844 virtual boolean is_set() const = 0;
845 /** creates an instance of the record's element class, using the default constructor */
846 virtual Base_Type* create_elem() const = 0;
848 /** Constant unbound element - return this instead of NULL in constant functions */
849 virtual const Base_Type* get_unbound_elem() const = 0;
851 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
852 virtual boolean can_start_v(const char *name, const char *prefix,
853 XERdescriptor_t const& xd, unsigned int flavor) = 0;
855 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
856 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
858 /** Indicates that the element at the given index is referenced by an 'out' or
859 * 'inout' parameter and must not be deleted.
860 * Used just before the actual function call that references the element. */
861 virtual void add_refd_index(int index);
863 /** Indicates that the element at the given index is no longer referenced by
864 * an 'out' or 'inout' parameter.
865 * Used immediately after the actual function call that referenced the element. */
866 virtual void remove_refd_index(int index);
869 extern boolean operator==(null_type null_value,
870 const Record_Of_Type& other_value);
871 extern boolean operator!=(null_type null_value,
872 const Record_Of_Type& other_value);
874 ////////////////////////////////////////////////////////////////////////////////
876 /** The base class of all record types when using RT2 */
877 // Record_Template can be found in Template.hh
878 class Record_Type : public Base_Type {
880 struct default_struct {
882 const Base_Type* value;
884 Erroneous_descriptor_t* err_descr;
886 Record_Type() : err_descr(NULL) {}
889 /** get pointer to a field */
890 virtual Base_Type* get_at(int index_value) = 0;
891 virtual const Base_Type* get_at(int index_value) const = 0;
894 /** get the index to a field based on its name and namespace URI, or -1 */
895 int get_index_byname(const char *name, const char *uri) const;
897 /** get number of fields */
898 virtual int get_count() const = 0;
899 /** number of optional fields */
900 virtual int optional_count() const { return 0; }
902 /** virtual functions inherited from Base_Type */
903 boolean is_equal(const Base_Type* other_value) const;
904 void set_value(const Base_Type* other_value);
906 /** @returns \c true if this is a set type,
907 * \c false if this is a record type */
908 virtual boolean is_set() const = 0;
910 /** return the name of a field */
911 virtual const char* fld_name(int field_index) const = 0;
913 /** return the descriptor of a field */
914 virtual const TTCN_Typedescriptor_t* fld_descr(int field_index) const = 0;
916 /** return int array which contains the indexes of optional fields
917 and a -1 value appended to the end, or return NULL if no optional fields,
918 generated classes override if there are optional fields */
919 virtual const int* get_optional_indexes() const { return NULL; }
920 /** default values and indexes, if no default fields then returns NULL,
921 the last default_struct.index is -1 */
922 virtual const default_struct* get_default_indexes() const { return NULL; }
924 /** override if it's TRUE for the specific type (if sdef->opentype_outermost) */
925 virtual boolean is_opentype_outermost() const { return FALSE; }
927 virtual boolean default_as_optional() const { return FALSE; }
929 virtual boolean is_bound() const;
930 virtual boolean is_value() const;
931 virtual void clean_up();
932 virtual void log() const;
933 virtual void set_param(Module_Param& param);
934 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
935 virtual void set_implicit_omit();
938 virtual void encode_text(Text_Buf& text_buf) const;
939 virtual void decode_text(Text_Buf& text_buf);
941 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
942 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
944 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
945 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
946 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
947 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
948 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
950 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
951 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *, const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
952 virtual int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t, boolean no_err = FALSE, int sel_field = -1, boolean first_call = TRUE);
953 // Helper functions for RAW enc/dec, shall be overridden in descendants if the default behavior is not enough.
954 virtual boolean raw_has_ext_bit() const { return FALSE; }
956 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
957 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
958 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
960 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
961 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
962 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
963 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
964 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
965 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
966 unsigned int, unsigned int, embed_values_dec_struct_t*);
968 /// Methods overridden in the derived (generated) class
969 virtual int get_xer_num_attr() const { return 0; /* default */ }
970 virtual const XERdescriptor_t* xer_descr(int field_index) const;
972 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
973 virtual boolean can_start_v(const char *name, const char *prefix,
974 XERdescriptor_t const& xd, unsigned int flavor) = 0;
976 /** Encodes accordingly to the JSON encoding rules.
977 * Returns the length of the encoded data. */
978 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
980 /** Negative testing for the JSON encoder
981 * Encodes this value according to the JSON encoding rules, but with the
982 * modifications (errors) specified in the erroneous descriptor parameter. */
983 int JSON_encode_negtest(const Erroneous_descriptor_t*, const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
985 /** Decodes accordingly to the JSON encoding rules.
986 * Returns the length of the decoded data. */
987 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
989 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
990 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
992 /// Helper for XER_encode_negtest
993 int encode_field(int i, const Erroneous_values_t* err_vals, const Erroneous_descriptor_t* emb_descr,
994 TTCN_Buffer& p_buf, unsigned int sub_flavor, int indent, embed_values_enc_struct_t* emb_val) const;
997 ////////////////////////////////////////////////////////////////////////////////
998 class Empty_Record_Type : public Base_Type {
1002 Empty_Record_Type();
1003 Empty_Record_Type(const Empty_Record_Type& other_value);
1006 boolean operator==(null_type other_value) const;
1007 inline boolean operator!=(null_type other_value) const { return !(*this == other_value); }
1009 /** virtual functions inherited from Base_Type */
1010 virtual boolean is_equal(const Base_Type* other_value) const;
1011 virtual void set_value(const Base_Type* other_value);
1013 /** @returns \c true if this is a set type,
1014 * \c false if this is a record type */
1015 virtual boolean is_set() const = 0;
1017 inline void set_null() { bound_flag = TRUE; }
1018 virtual boolean is_bound() const { return bound_flag; }
1019 virtual void clean_up() { bound_flag = FALSE; }
1020 virtual void log() const;
1021 virtual void set_param(Module_Param& param);
1022 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
1024 int size_of() const { return 0; }
1025 virtual void encode_text(Text_Buf& text_buf) const;
1026 virtual void decode_text(Text_Buf& text_buf);
1028 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
1029 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
1031 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
1032 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
1034 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
1035 virtual int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
1037 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
1038 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
1040 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
1041 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
1042 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1043 unsigned int, unsigned int, embed_values_dec_struct_t*);
1045 /** Encodes accordingly to the JSON encoding rules.
1046 * Returns the length of the encoded data. */
1047 virtual int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
1049 /** Decodes accordingly to the JSON encoding rules.
1050 * Returns the length of the decoded data. */
1051 virtual int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
1054 struct Enum_Type : public Base_Type {
1055 virtual int as_int() const = 0;
1056 virtual void from_int(int) = 0;
1059 extern boolean operator==(null_type null_value, const Empty_Record_Type& other_value);
1060 extern boolean operator!=(null_type null_value, const Empty_Record_Type& other_value);
1062 #undef VIRTUAL_IF_RUNTIME_2
1065 template <class EXPR_TYPE>
1068 bool expr_evaluated;
1069 EXPR_TYPE expr_cache;
1070 virtual void eval_expr() {}
1072 Lazy_Param(): expr_evaluated(false) {}
1073 enum evaluated_state_t { EXPR_EVALED };
1074 Lazy_Param(evaluated_state_t /*p_es*/, EXPR_TYPE p_cache): expr_evaluated(true), expr_cache(p_cache) {}
1075 operator EXPR_TYPE&() {
1076 if (!expr_evaluated) { eval_expr(); expr_evaluated=true; }
1079 virtual ~Lazy_Param() {}
1081 if (!expr_evaluated) {
1082 TTCN_Logger::log_event_str("<not evaluated>");