1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 ///////////////////////////////////////////////////////////////////////////////
14 #include "JSON_Tokenizer.hh"
15 #ifdef TITAN_RUNTIME_2
16 #include "Struct_of.hh"
19 #include "RefdIndex.hh"
22 struct ASN_BERdescriptor_t;
24 struct TTCN_RAWdescriptor_t;
25 struct TTCN_TEXTdescriptor_t;
26 struct XERdescriptor_t;
27 struct TTCN_JSONdescriptor_t;
30 class Module_Param_Name;
31 struct embed_values_enc_struct_t;
32 struct embed_values_dec_struct_t;
34 /** @brief Type descriptor
36 * There is one type descriptor object for each type.
37 * Descriptors for built-in types are supplied by the runtime.
38 * Descriptors for user-defined types are written by the compiler
39 * in the generated code.
41 struct TTCN_Typedescriptor_t {
42 const char * const name; /**< Name of the type, e.g INTEGER, REAL, verdicttype, etc */
43 const ASN_BERdescriptor_t * const ber; /**< Information for BER coding */
44 const TTCN_RAWdescriptor_t * const raw; /**< Information for RAW coding */
45 const TTCN_TEXTdescriptor_t * const text; /**< Information for TEXT coding */
46 const XERdescriptor_t * const xer; /**< Information for XER */
47 const TTCN_JSONdescriptor_t * const json; /**< Information for JSON coding */
48 const TTCN_Typedescriptor_t * const oftype_descr; /**< Record-of element's type descriptor */
51 * Used by classes implementing more than one ASN.1 type
52 * (OBJID and UNIVERSAL_CHARSTRING).
56 UNIVERSALSTRING, BMPSTRING, UTF8STRING,
57 TELETEXSTRING, VIDEOTEXSTRING, GRAPHICSTRING, GENERALSTRING,
62 #ifdef TITAN_RUNTIME_2
64 struct Erroneous_value_t {
66 const Base_Type * const errval; // NULL if `omit'
67 const TTCN_Typedescriptor_t* type_descr; // NULL if `omit' or raw
70 struct Erroneous_values_t {
71 const int field_index;
72 const char* field_qualifier;
73 const Erroneous_value_t * const before;
74 const Erroneous_value_t * const value;
75 const Erroneous_value_t * const after;
78 struct Erroneous_descriptor_t {
79 const int field_index;
80 const int omit_before; // -1 if none
81 const char* omit_before_qualifier; // NULL if none
82 const int omit_after; // -1 if none
83 const char* omit_after_qualifier; // NULL if none
84 const int values_size;
85 const Erroneous_values_t * const values_vec;
86 const int embedded_size;
87 const Erroneous_descriptor_t * const embedded_vec;
88 /** search in values_vec for the field with index field_idx */
89 const Erroneous_values_t* get_field_err_values(int field_idx) const;
90 /** search in embedded_vec for the field with index field_idx */
91 const Erroneous_descriptor_t* get_field_emb_descr(int field_idx) const;
92 /** if the current element of values_vec has index field_idx then returns it and increments values_idx */
93 const Erroneous_values_t* next_field_err_values(const int field_idx, int& values_idx) const;
94 /** if the current element of embedded_vec has index field_idx then returns it and increments edescr_idx */
95 const Erroneous_descriptor_t* next_field_emb_descr(const int field_idx, int& edescr_idx) const;
102 class TTCN_Type_list;
104 class Limit_Token_List;
105 #ifdef TITAN_RUNTIME_2
109 #ifdef TITAN_RUNTIME_2
110 #define VIRTUAL_IF_RUNTIME_2 virtual
112 #define VIRTUAL_IF_RUNTIME_2
116 * The base class for all types in TTCN-3 runtime environment.
118 * Uses the compiler-generated default constructor, copy constructor
119 * and assignment operator (they do nothing because the class is empty).
124 /** Whether the value is present.
125 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility!
126 * causes DTE, must be used only if the field is OPTIONAL<>
128 VIRTUAL_IF_RUNTIME_2 boolean ispresent() const;
130 VIRTUAL_IF_RUNTIME_2 void log() const;
132 /** Check whether the XML encoding of the type can begin with an XML element
133 * having the specified name and namespace.
135 * This function checks for the "starter tag" of the type. This usually
136 * means the tag should match the name in the XER descriptor,
137 * with a few notable exceptions:
139 * - If the type is untagged, then
140 * - if it's a record, all fields up to the the first non-optional field
142 * - if it's a record-of, the element is checked.
143 * - A \c universal \c charstring with ANY-ELEMENT can begin with any tag.
145 * @param name the XML element name (local name)
146 * @param uri the namespace URI of the element
147 * @param xd XER descriptor
148 * @param flavor one or more bits from XER_flavor. Only XER_EXTENDED counts
149 * @return TRUE if the XML element can begin the XER encoding of the type,
152 static boolean can_start(const char *name, const char *uri,
153 XERdescriptor_t const& xd, unsigned int flavor);
155 #ifdef TITAN_RUNTIME_2
156 /** Initialize this object (or one of its fields/elements) with a
157 * module parameter value. The module parameter may contain references to
158 * other module parameters or module parameter expressions, which are processed
159 * by this method to calculated the final result.
160 * @param param module parameter value (its ID specifies which object is to be set) */
161 virtual void set_param(Module_Param& param) = 0;
162 /** Create a module parameter value equivalent to this object (or one of its
164 * @param param_name module parameter ID, specifies which object to convert */
165 virtual Module_Param* get_param(Module_Param_Name& param_name) const = 0;
166 /** Whether the type is a sequence-of.
167 * @return \c FALSE */
168 virtual boolean is_seof() const { return FALSE; }
170 virtual void encode_text(Text_Buf& text_buf) const = 0;
171 virtual void decode_text(Text_Buf& text_buf) = 0;
173 virtual boolean is_bound() const = 0;
174 virtual boolean is_value() const { return is_bound(); }
175 virtual void clean_up() = 0;
177 /** returns if the value of this is equal to the value of the parameter. */
178 virtual boolean is_equal(const Base_Type* other_value) const = 0;
180 /** Performs: *this = *other_value.
181 * Does not take ownership of \p other_value. */
182 virtual void set_value(const Base_Type* other_value) = 0;
184 /** creates a copy of this object, returns a pointer to it. */
185 virtual Base_Type* clone() const = 0;
187 /** return reference to the type descriptor object for this type. */
188 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
190 /** @name override in class OPTIONAL<T>
192 virtual boolean is_optional() const { return FALSE; }
194 /** Is the optional value present or omit.
195 * @return \c true if present, \c false otherwise
196 * used for TTCN-3 ispresent()
198 virtual boolean is_present() const;
200 /** Access the embedded type of the optional.
202 * The implementation in Base_Type always causes a dynamic testcase error.
204 virtual Base_Type* get_opt_value();
206 /** Access the embedded type of the optional.
208 * The implementation in Base_Type always causes a dynamic testcase error.
210 virtual const Base_Type* get_opt_value() const;
212 /** Set the optional field to \c omit.
213 * The implementation in Base_Type always causes a dynamic testcase error.
215 virtual void set_to_omit();
217 /** Set the optional field to present.
218 * The implementation in Base_Type always causes a dynamic testcase error.
220 virtual void set_to_present();
223 virtual ~Base_Type() { }
227 /** Do nothing for non-structured types. */
228 VIRTUAL_IF_RUNTIME_2 void set_implicit_omit() { }
230 /** @brief Encode an instance of this object
234 @param p_td type descriptor
236 @param p_coding coding type to select BER, RAW or TEXT coding
239 VIRTUAL_IF_RUNTIME_2 void encode(const TTCN_Typedescriptor_t& p_td,
240 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const;
242 /** @brief Decode an instance of this object
246 @param p_td type descriptor
248 @param p_coding coding type to select BER, RAW or TEXT coding
251 VIRTUAL_IF_RUNTIME_2 void decode(const TTCN_Typedescriptor_t& p_td,
252 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...);
255 /** Check type descriptor for BER encoding
257 * @param p_td a TTCN type descriptor
259 * Calls TTCN_EncDec_ErrorContext::error_internal if
260 * p_td has no BER descriptor.
262 static void BER_chk_descr(const TTCN_Typedescriptor_t& p_td);
264 /** Check the BER coding
266 * If \p p_coding is either BER_ENCODE_CER or BER_ENCODE_DER,
267 * this function does nothing. Otherwise, it issues a warning
268 * and sets \p p_coding to BER_ENCODE_DER.
270 * @param[in, out] p_coding BER coding
271 * @post p_coding is either BER_ENCODE_CER or BER_ENCODE_DER
273 static void BER_encode_chk_coding(unsigned& p_coding);
275 /** Check type descriptor for XER encoding and the XER coding variant
277 * @param[in, out] p_coding XER coding
278 * @param[in] p_td TTCN type descriptor
280 * If \p p_td has no XER descriptor, calls
281 * TTCN_EncDec_ErrorContext::error_internal()
283 * If \p p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED,
284 * this function does nothing. Otherwise, it issues a warning
285 * and sets \p p_coding to XER_BASIC.
287 * @post p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED
289 static void XER_encode_chk_coding(unsigned& p_coding,
290 const TTCN_Typedescriptor_t& p_td);
292 /** Check bound and create a 0-length TLV in case unbound is ignored.
294 * @param[in] p_isbound
295 * @return a newly constructed and empty \c ASN_BER_TLV_t
296 * if p_isbound is false, or NULL if p_isbound is true
297 * @note if p_isbound is false, this function calls
298 * TTCN_EncDec_ErrorContext::error with TTCN_EncDec::ET_UNBOUND.
299 * If that error is not ignored, this function does not return at all.
301 static ASN_BER_TLV_t* BER_encode_chk_bound(boolean p_isbound);
304 static void BER_encode_putoctets_OCTETSTRING
305 (unsigned char *target,
306 unsigned int octetnum_start, unsigned int octet_count,
307 int p_nof_octets, const unsigned char *p_octets_ptr);
310 static ASN_BER_TLV_t* BER_encode_TLV_OCTETSTRING
312 int p_nof_octets, const unsigned char *p_octets_ptr);
314 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
315 const int_val_t& p_int_val);
316 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
317 const int& p_int_val);
319 static void BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
320 boolean p_isvalid, int p_value);
322 static void BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
325 static boolean BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
328 ASN_BER_TLV_t& p_target_tlv);
330 static void BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
333 ASN_BER_TLV_t& p_target_tlv,
334 boolean tlv_present);
336 static void BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
337 const ASN_BER_TLV_t& p_tlv,
339 ASN_BER_TLV_t& stripped_tlv);
342 static void BER_decode_getoctets_OCTETSTRING
343 (const unsigned char *source, size_t s_len,
344 unsigned int& octetnum_start,
345 int& p_nof_octets, unsigned char *p_octets_ptr);
348 static void BER_decode_TLV_OCTETSTRING
349 (const ASN_BER_TLV_t& p_tlv, unsigned L_form,
350 unsigned int& octetnum_start,
351 int& p_nof_octets, unsigned char *p_octets_ptr);
353 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
354 unsigned L_form, int_val_t& p_int_val);
355 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
356 unsigned L_form, int& p_int_val);
358 static boolean BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
359 const ASN_BER_TLV_t& p_tlv,
361 ASN_BER_TLV_t& p_target_tlv);
363 static boolean BER_decode_CHOICE_selection(boolean select_result,
364 const ASN_BER_TLV_t& p_tlv);
366 static void BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
367 boolean p_isvalid, int p_value);
370 VIRTUAL_IF_RUNTIME_2 ASN_BER_TLV_t* BER_encode_TLV(
371 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
372 #ifdef TITAN_RUNTIME_2
373 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
374 const TTCN_Typedescriptor_t& /*p_td*/, unsigned /*p_coding*/) const;
375 virtual ASN_BER_TLV_t* BER_encode_negtest_raw() const;
376 virtual int encode_raw(TTCN_Buffer& p_buf) const;
377 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
380 /** Examines whether this message corresponds the tags in the
381 * descriptor. If the BER descriptor contains no tags, then returns
382 * TRUE. Otherwise, examines the first (outermost) tag only. */
383 #ifdef TITAN_RUNTIME_2
388 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
389 const ASN_BER_TLV_t& p_tlv);
391 /** Returns TRUE on success, FALSE otherwise. */
392 VIRTUAL_IF_RUNTIME_2 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
393 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
395 VIRTUAL_IF_RUNTIME_2 void BER_decode_opentypes(
396 TTCN_Type_list& /*p_typelist*/, unsigned /*L_form*/) {}
398 /** Encode with RAW coding.
399 * @return the length of the encoding
400 * @param p_td type descriptor
401 * @param myleaf filled with RAW encoding data
402 * @note Basetype::RAW_encode throws an error. */
403 VIRTUAL_IF_RUNTIME_2 int RAW_encode(const TTCN_Typedescriptor_t& p_td,
404 RAW_enc_tree& myleaf) const;
405 #ifdef TITAN_RUNTIME_2
406 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
407 const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
410 /** Decode with RAW coding
412 * @param p_td type descriptor
413 * @param p_buf buffer with data to be decoded
414 * @param limit number of bits the decoder is allowed to use. At the top level
415 * this is 8x the number of bytes in the buffer.
416 * @param top_bit_ord (LSB/MSB) from TTCN_RAWdescriptor_t::top_bit_order
417 * @param no_err set to TRUE if the decoder is to return errors silently,
418 * without calling TTCN_EncDec_ErrorContext::error
419 * @param sel_field selected field indicator for CROSSTAG, or -1
420 * @param first_call default TRUE. May be FALSE for a REPEATABLE record-of
421 * inside a set, if an element has been successfully decoded.
422 * @return length of decoded field, or a negative number for error
423 * @note Basetype::RAW_decode throws an error. */
424 VIRTUAL_IF_RUNTIME_2 int RAW_decode(
425 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int limit,
426 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1,
427 boolean first_call=TRUE);
430 /** Encode with TEXT encoding.
431 * @return the length of the encoding
432 * @param p_td type descriptor
433 * @param p_buf buffer for the encoded data
434 * @note Basetype::TEXT_encode throws an error. */
435 VIRTUAL_IF_RUNTIME_2 int TEXT_encode(const TTCN_Typedescriptor_t& p_td,
436 TTCN_Buffer& p_buf) const;
438 #ifdef TITAN_RUNTIME_2
439 /** Encode with TEXT encoding negative test.
440 * @return the length of the encoding
441 * @param p_err_descr type descriptor
442 * @param p_td type descriptor
443 * @param p_buf buffer for the encoded data
444 * @note Basetype::TEXT_encode_negtest throws an error. */
445 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
446 TTCN_Buffer& p_buf) const;
450 * @return decoded length
451 * @note Basetype::TEXT_decode throws an error. */
452 VIRTUAL_IF_RUNTIME_2 int TEXT_decode(
453 const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
454 boolean no_err=FALSE, boolean first_call=TRUE);
456 /** Write the XER encoding of the current object into the buffer.
458 * @param p_td type descriptor
459 * @param p_buf buffer
460 * @param flavor one of XER_flavor values
461 * @param indent indentation level
462 * @param emb_val embed values data (only relevant for record of types)
463 * @return number of bytes written into the buffer
465 VIRTUAL_IF_RUNTIME_2 int XER_encode(const XERdescriptor_t& p_td,
466 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
468 #ifdef TITAN_RUNTIME_2
469 virtual int XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
470 const XERdescriptor_t& /*p_td*/, TTCN_Buffer& /*p_buf*/,
471 unsigned int /*flavor*/, int /*indent*/, embed_values_enc_struct_t* /*emb_val*/) const;
474 /** Decode the current object from the supplied buffer.
476 * The XML pull parser presents a forward iterator over the XML elements.
477 * @pre Upon entering this function, the current node should be the one corresponding
478 * to this object, or possibly a text or whitespace node before it.
479 * Code should begin like this:
481 * int Foo::XER_decode() {
482 * int success=1, type;
483 * for ( ; success==1; success=reader.Read() ) {
484 * int type=reader.NodeType();
485 * if (XML_READER_TYPE_ELEMENT==type) {
492 * @post Upon leaving the function, the current node should be \b past
493 * the end tag, i.e. the next start tag (or any whitespace before it).
495 * It is important not to advance the parser too far, to avoid "stealing"
496 * the content of other fields (the parser can only go forward).
498 * @param p_td type descriptor
499 * @param reader Wrapper around the XML processor
500 * @param flavor one of XER_flavor values
501 * @param emb_val embed values data (only relevant for record of types)
502 * @return number of bytes "consumed"
504 VIRTUAL_IF_RUNTIME_2 int XER_decode(const XERdescriptor_t& p_td,
505 XmlReaderWrap& reader, unsigned int flavor, embed_values_dec_struct_t* emb_val);
507 /** Return an array of namespace declarations.
509 * @param[in] p_td XER descriptor of the type.
510 * @param[out] num set to the number of strings returned.
511 * @param[out] def_ns set to @p true if a default namespace was encountered
512 * @return an array of strings or NULL. All the strings in the array
513 * and the array itself must be deallocated by the caller (if num>0).
515 * The strings are in the following format:
516 * @code " xmlns:prefix='uri'" @endcode
517 * (the space at start allows direct concatenation of the strings)
519 VIRTUAL_IF_RUNTIME_2 char ** collect_ns(const XERdescriptor_t& p_td,
520 size_t& num, bool& def_ns) const;
522 /** Copy strings from @p new_namespaces to @p collected_ns,
523 * discarding duplicates.
525 * May reallocate @p collected_ns, in which case @p num_collected
526 * is adjusted accordingly.
528 * @param collected_ns target array
529 * @param num_collected number of already collected namespaces
530 * @param new_namespaces new array
531 * @param num_new number of new namespaces
533 * @post new_namespaces has been deallocated and set to NULL.
535 static void merge_ns(char **&collected_ns, size_t& num_collected,
536 char **new_namespaces, size_t num_new);
538 typedef char** (Base_Type::*collector_fn)
539 (const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
541 /** Start writing the XML representation
543 * @param[in] p_td XER descriptor
544 * @param[out] p_buf buffer to write into
545 * @param[in,out] flavor XER variant (basic, canonical or extended),
546 * also used to pass various flags between begin_xml and its caller
547 * @param[in] indent indentation level
548 * @param[in] empty true if an empty-element tag is needed
549 * @param[in] collector namespace collector function
550 * @param[in] type_atr type identification attribute for useNil/useUnion,
551 * in the following format: "xsi:type='foo'"
552 * @return "omit_tag": zero if the type's own tag was written (not omitted
553 * e.g. due to untagged).
554 * If the XML tag was omitted, the return value is nonzero.
555 * The special value -1 is returned if the XML in the buffer
556 * was shortened by exactly one character.
558 VIRTUAL_IF_RUNTIME_2 int begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
559 unsigned int& flavor, int indent, bool empty,
560 collector_fn collector = &Base_Type::collect_ns, const char *type_atr = NULL) const;
561 /** Finish the XML representation.
563 * @param[in] p_td XER descriptor
564 * @param[out] p_buf buffer to write into
565 * @param[in] flavor XER variant (basic, canonical or extended),
566 * also used to pass various flags
567 * @param[in] indent indentation level
568 * @param[in] empty true if an empty-element tag is needed
570 VIRTUAL_IF_RUNTIME_2 void end_xml (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
571 unsigned int flavor, int indent, bool empty) const;
574 * @return encoded length
575 * @note Basetype::JSON_encode throws an error. */
576 VIRTUAL_IF_RUNTIME_2 int JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&) const;
579 * @return decoded length
580 * @note Basetype::JSON_decode throws an error. */
581 VIRTUAL_IF_RUNTIME_2 int JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean);
583 /** not a component by default (components will return true) */
584 inline boolean is_component() { return FALSE; }
588 * This class is used by BER decoding to handle
589 * ComponentRelationConstraint.
591 class TTCN_Type_list {
594 const Base_Type **types;
595 /// Copy constructor disabled
596 TTCN_Type_list(const TTCN_Type_list&);
597 /// Assignment disabled
598 TTCN_Type_list& operator=(const TTCN_Type_list&);
600 TTCN_Type_list() : n_types(0), types(NULL) {}
602 void push(const Base_Type *p_type);
603 const Base_Type* pop();
604 /** If \a pos is 0 then returns the outermost, otherwise the "pos"th
606 const Base_Type* get_nth(size_t pos) const;
609 /** @name Type descriptor objects
612 extern const TTCN_Typedescriptor_t BOOLEAN_descr_;
613 extern const TTCN_Typedescriptor_t INTEGER_descr_;
614 extern const TTCN_Typedescriptor_t FLOAT_descr_;
615 extern const TTCN_Typedescriptor_t VERDICTTYPE_descr_;
616 extern const TTCN_Typedescriptor_t OBJID_descr_;
617 extern const TTCN_Typedescriptor_t BITSTRING_descr_;
618 extern const TTCN_Typedescriptor_t HEXSTRING_descr_;
619 extern const TTCN_Typedescriptor_t OCTETSTRING_descr_;
620 extern const TTCN_Typedescriptor_t CHARSTRING_descr_;
621 extern const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_;
622 extern const TTCN_Typedescriptor_t COMPONENT_descr_;
623 extern const TTCN_Typedescriptor_t DEFAULT_descr_;
624 extern const TTCN_Typedescriptor_t ASN_NULL_descr_;
625 extern const TTCN_Typedescriptor_t ASN_ANY_descr_;
626 extern const TTCN_Typedescriptor_t EXTERNAL_descr_;
627 extern const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_;
628 extern const TTCN_Typedescriptor_t CHARACTER_STRING_descr_;
629 extern const TTCN_Typedescriptor_t ObjectDescriptor_descr_;
630 extern const TTCN_Typedescriptor_t UTF8String_descr_;
631 extern const TTCN_Typedescriptor_t ASN_ROID_descr_;
632 extern const TTCN_Typedescriptor_t NumericString_descr_;
633 extern const TTCN_Typedescriptor_t PrintableString_descr_;
634 extern const TTCN_Typedescriptor_t TeletexString_descr_;
635 extern const TTCN_Typedescriptor_t& T61String_descr_;
636 extern const TTCN_Typedescriptor_t VideotexString_descr_;
637 extern const TTCN_Typedescriptor_t IA5String_descr_;
638 extern const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_;
639 extern const TTCN_Typedescriptor_t ASN_UTCTime_descr_;
640 extern const TTCN_Typedescriptor_t GraphicString_descr_;
641 extern const TTCN_Typedescriptor_t VisibleString_descr_;
642 extern const TTCN_Typedescriptor_t& ISO646String_descr_;
643 extern const TTCN_Typedescriptor_t GeneralString_descr_;
644 extern const TTCN_Typedescriptor_t UniversalString_descr_;
645 extern const TTCN_Typedescriptor_t BMPString_descr_;
648 #ifdef TITAN_RUNTIME_2
653 /** The base class of all record-of types when using RT2
656 // Record_Of_Template can be found in Template.hh
657 class Record_Of_Type : public Base_Type, public RefdIndexInterface
659 friend class Set_Of_Template;
660 friend class Record_Of_Template;
662 friend boolean operator==(null_type null_value,
663 const Record_Of_Type& other_value);
664 struct recordof_setof_struct {
667 Base_Type **value_elements;
669 Erroneous_descriptor_t* err_descr;
671 struct refd_index_struct {
672 /** Stores the indices of elements that are referenced by 'out' and 'inout' parameters.
673 * These elements must not be deleted.*/
674 Vector<int> refd_indices;
676 /** Cached maximum value of \a refd_indices (default: -1).*/
680 static boolean compare_function(const Record_Of_Type *left_ptr, int left_index, const Record_Of_Type *right_ptr, int right_index);
681 Record_Of_Type() : val_ptr(NULL), err_descr(NULL), refd_ind_ptr(NULL) {}
682 Record_Of_Type(null_type other_value);
683 Record_Of_Type(const Record_Of_Type& other_value);
684 /// Assignment disabled
685 Record_Of_Type& operator=(const Record_Of_Type& other_value);
687 /** Returns the number of actual elements in the record of (does not count the
688 * unbound elements left at the end of the record of struct to make sure
689 * referenced elements are not deleted). */
690 int get_nof_elements() const;
692 /** Returns true if the indexed element is bound */
693 bool is_elem_bound(int index) const;
695 /** Returns the highest referenced index (uses \a max_refd_index as its cache)*/
696 int get_max_refd_index();
698 /** Returns true if the element at the given index is referenced by an 'out' or
699 * 'inout' parameter. */
700 bool is_index_refd(int index);
703 void set_val(null_type other_value);
705 boolean operator==(null_type other_value) const;
706 boolean operator!=(null_type other_value) const;
708 Base_Type* get_at(int index_value);
709 Base_Type* get_at(const INTEGER& index_value);
710 const Base_Type* get_at(int index_value) const;
711 const Base_Type* get_at(const INTEGER& index_value) const;
713 virtual boolean is_equal(const Base_Type* other_value) const;
714 virtual void set_value(const Base_Type* other_value);
716 /* following functions return rec_of or this or other_value */
717 Record_Of_Type* rotl(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
718 Record_Of_Type* rotr(int rotate_count, Record_Of_Type* rec_of) const;
719 Record_Of_Type* rotr(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
720 Record_Of_Type* concat(const Record_Of_Type* other_value, Record_Of_Type* rec_of) const;
721 /* parameter rec_of must be a newly created descendant object, these helper functions fill it */
722 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
723 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
724 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
725 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
727 void set_size(int new_size);
729 /** Implementation for isbound.
730 * @return \c true if the Record_Of itself is bound, \c false otherwise.
731 * Ignores elements. No need to be overridden. */
732 virtual boolean is_bound() const;
733 /** Implementation for isvalue.
734 * @return \c true if all elements are bound, \c false otherwise */
735 virtual boolean is_value() const;
736 virtual void clean_up();
738 virtual boolean is_seof() const { return TRUE; }
740 /** Implementation for the \c sizeof operation
741 * @return the number of elements */
743 int n_elem() const { return size_of(); }
745 /** Implementation for the lengthof operation.
746 * @return the index of the last bound element +1 */
747 int lengthof() const;
748 virtual void log() const;
749 virtual void set_param(Module_Param& param);
750 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
751 virtual void set_implicit_omit();
752 virtual void encode_text(Text_Buf& text_buf) const;
753 virtual void decode_text(Text_Buf& text_buf);
755 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
757 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
758 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
760 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
761 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;
762 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
764 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
765 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
768 * @param td type descriptor
770 * @param limit max number of bits that can be used
772 * @param no_err (not used)
774 * @param first_call default true, false for second and subsequent calls
775 * of a REPEATABLE record-of. Equivalent to "not repeated"
776 * @return number of bits used, or a negative number in case of error
778 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
779 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
780 /// raw extension bit
781 virtual int rawdec_ebv() const;
783 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
784 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
785 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
787 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
788 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
789 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
790 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned flavor, int indent, embed_values_enc_struct_t*) const;
791 /// Helper for XER_encode_negtest
792 int encode_element(int i, const XERdescriptor_t& p_td, const Erroneous_values_t* err_vals,
793 const Erroneous_descriptor_t* emb_descr,
794 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
795 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int, embed_values_dec_struct_t*);
796 virtual boolean isXerAttribute() const;
797 virtual boolean isXmlValueList() const;
799 /** Encodes accordingly to the JSON encoding rules.
800 * Returns the length of the encoded data. */
801 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
803 /** Decodes accordingly to the JSON encoding rules.
804 * Returns the length of the decoded data. */
805 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
807 /** @returns \c true if this is a set-of type,
808 * \c false if this is a record-of type */
809 virtual boolean is_set() const = 0;
810 /** creates an instance of the record's element class, using the default constructor */
811 virtual Base_Type* create_elem() const = 0;
813 /** Constant unbound element - return this instead of NULL in constant functions */
814 virtual const Base_Type* get_unbound_elem() const = 0;
816 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
817 virtual boolean can_start_v(const char *name, const char *prefix,
818 XERdescriptor_t const& xd, unsigned int flavor) = 0;
820 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
821 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
823 /** Indicates that the element at the given index is referenced by an 'out' or
824 * 'inout' parameter and must not be deleted.
825 * Used just before the actual function call that references the element. */
826 virtual void add_refd_index(int index);
828 /** Indicates that the element at the given index is no longer referenced by
829 * an 'out' or 'inout' parameter.
830 * Used immediately after the actual function call that referenced the element. */
831 virtual void remove_refd_index(int index);
834 extern boolean operator==(null_type null_value,
835 const Record_Of_Type& other_value);
836 extern boolean operator!=(null_type null_value,
837 const Record_Of_Type& other_value);
839 ////////////////////////////////////////////////////////////////////////////////
841 /** The base class of all record types when using RT2 */
842 // Record_Template can be found in Template.hh
843 class Record_Type : public Base_Type {
845 struct default_struct {
847 const Base_Type* value;
849 Erroneous_descriptor_t* err_descr;
852 Record_Type() : err_descr(NULL), bound_flag(false) {}
855 /** get pointer to a field */
856 virtual Base_Type* get_at(int index_value) = 0;
857 virtual const Base_Type* get_at(int index_value) const = 0;
860 /** get the index to a field based on its name and namespace URI, or -1 */
861 int get_index_byname(const char *name, const char *uri) const;
863 /** get number of fields */
864 virtual int get_count() const = 0;
865 /** number of optional fields */
866 virtual int optional_count() const { return 0; }
868 /** virtual functions inherited from Base_Type */
869 boolean is_equal(const Base_Type* other_value) const;
870 void set_value(const Base_Type* other_value);
872 /** @returns \c true if this is a set type,
873 * \c false if this is a record type */
874 virtual boolean is_set() const = 0;
876 /** return the name of a field */
877 virtual const char* fld_name(int field_index) const = 0;
879 /** return the descriptor of a field */
880 virtual const TTCN_Typedescriptor_t* fld_descr(int field_index) const = 0;
882 /** return int array which contains the indexes of optional fields
883 and a -1 value appended to the end, or return NULL if no optional fields,
884 generated classes override if there are optional fields */
885 virtual const int* get_optional_indexes() const { return NULL; }
886 /** default values and indexes, if no default fields then returns NULL,
887 the last default_struct.index is -1 */
888 virtual const default_struct* get_default_indexes() const { return NULL; }
890 /** override if it's TRUE for the specific type (if sdef->opentype_outermost) */
891 virtual boolean is_opentype_outermost() const { return FALSE; }
893 virtual boolean default_as_optional() const { return FALSE; }
895 virtual boolean is_bound() const;
896 virtual boolean is_value() const;
897 virtual void clean_up();
898 virtual void log() const;
899 virtual void set_param(Module_Param& param);
900 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
901 virtual void set_implicit_omit();
904 virtual void encode_text(Text_Buf& text_buf) const;
905 virtual void decode_text(Text_Buf& text_buf);
907 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
908 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
910 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
911 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
912 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
913 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
914 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
916 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
917 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *, const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
918 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);
919 // Helper functions for RAW enc/dec, shall be overridden in descendants if the default behavior is not enough.
920 virtual boolean raw_has_ext_bit() const { return FALSE; }
922 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
923 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
924 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
926 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
927 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
928 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
929 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
930 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
931 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
932 unsigned int, embed_values_dec_struct_t*);
934 /// Methods overridden in the derived (generated) class
935 virtual int get_xer_num_attr() const { return 0; /* default */ }
936 virtual const XERdescriptor_t* xer_descr(int field_index) const;
938 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
939 virtual boolean can_start_v(const char *name, const char *prefix,
940 XERdescriptor_t const& xd, unsigned int flavor) = 0;
942 /** Encodes accordingly to the JSON encoding rules.
943 * Returns the length of the encoded data. */
944 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
946 /** Decodes accordingly to the JSON encoding rules.
947 * Returns the length of the decoded data. */
948 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
950 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
951 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
953 /// Helper for XER_encode_negtest
954 int encode_field(int i, const Erroneous_values_t* err_vals, const Erroneous_descriptor_t* emb_descr,
955 TTCN_Buffer& p_buf, unsigned int sub_flavor, int indent, embed_values_enc_struct_t* emb_val) const;
958 ////////////////////////////////////////////////////////////////////////////////
959 class Empty_Record_Type : public Base_Type {
964 Empty_Record_Type(const Empty_Record_Type& other_value);
967 boolean operator==(null_type other_value) const;
968 inline boolean operator!=(null_type other_value) const { return !(*this == other_value); }
970 /** virtual functions inherited from Base_Type */
971 virtual boolean is_equal(const Base_Type* other_value) const;
972 virtual void set_value(const Base_Type* other_value);
974 /** @returns \c true if this is a set type,
975 * \c false if this is a record type */
976 virtual boolean is_set() const = 0;
978 inline void set_null() { bound_flag = TRUE; }
979 virtual boolean is_bound() const { return bound_flag; }
980 virtual void clean_up() { bound_flag = FALSE; }
981 virtual void log() const;
982 virtual void set_param(Module_Param& param);
983 virtual Module_Param* get_param(Module_Param_Name& param_name) const;
985 int size_of() const { return 0; }
986 virtual void encode_text(Text_Buf& text_buf) const;
987 virtual void decode_text(Text_Buf& text_buf);
989 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
990 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
992 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
993 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
995 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
996 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);
998 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
999 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
1001 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
1002 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
1003 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1004 unsigned int, embed_values_dec_struct_t*);
1006 /** Encodes accordingly to the JSON encoding rules.
1007 * Returns the length of the encoded data. */
1008 virtual int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
1010 /** Decodes accordingly to the JSON encoding rules.
1011 * Returns the length of the decoded data. */
1012 virtual int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
1015 struct Enum_Type : public Base_Type {
1016 virtual int as_int() const = 0;
1017 virtual void from_int(int) = 0;
1020 extern boolean operator==(null_type null_value, const Empty_Record_Type& other_value);
1021 extern boolean operator!=(null_type null_value, const Empty_Record_Type& other_value);
1023 #undef VIRTUAL_IF_RUNTIME_2
1026 template <class EXPR_TYPE>
1029 bool expr_evaluated;
1030 EXPR_TYPE expr_cache;
1031 virtual void eval_expr() {}
1033 Lazy_Param(): expr_evaluated(false) {}
1034 enum evaluated_state_t { EXPR_EVALED };
1035 Lazy_Param(evaluated_state_t /*p_es*/, EXPR_TYPE p_cache): expr_evaluated(true), expr_cache(p_cache) {}
1036 operator EXPR_TYPE&() {
1037 if (!expr_evaluated) { eval_expr(); expr_evaluated=true; }
1040 virtual ~Lazy_Param() {}