1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
14 #include "JSON_Tokenizer.hh"
16 #ifdef TITAN_RUNTIME_2
17 #include "Struct_of.hh"
21 struct ASN_BERdescriptor_t;
23 struct TTCN_RAWdescriptor_t;
24 struct TTCN_TEXTdescriptor_t;
25 struct XERdescriptor_t;
26 struct TTCN_JSONdescriptor_t;
29 struct embed_values_enc_struct_t;
30 struct embed_values_dec_struct_t;
32 /** @brief Type descriptor
34 * There is one type descriptor object for each type.
35 * Descriptors for built-in types are supplied by the runtime.
36 * Descriptors for user-defined types are written by the compiler
37 * in the generated code.
39 struct TTCN_Typedescriptor_t {
40 const char * const name; /**< Name of the type, e.g INTEGER, REAL, verdicttype, etc */
41 const ASN_BERdescriptor_t * const ber; /**< Information for BER coding */
42 const TTCN_RAWdescriptor_t * const raw; /**< Information for RAW coding */
43 const TTCN_TEXTdescriptor_t * const text; /**< Information for TEXT coding */
44 const XERdescriptor_t * const xer; /**< Information for XER */
45 const TTCN_JSONdescriptor_t * const json; /**< Information for JSON coding */
48 * Used by classes implementing more than one ASN.1 type
49 * (OBJID and UNIVERSAL_CHARSTRING).
53 UNIVERSALSTRING, BMPSTRING, UTF8STRING,
54 TELETEXSTRING, VIDEOTEXSTRING, GRAPHICSTRING, GENERALSTRING,
59 #ifdef TITAN_RUNTIME_2
61 struct Erroneous_value_t {
63 const Base_Type * const errval; // NULL if `omit'
64 const TTCN_Typedescriptor_t* type_descr; // NULL if `omit' or raw
67 struct Erroneous_values_t {
68 const int field_index;
69 const char* field_qualifier;
70 const Erroneous_value_t * const before;
71 const Erroneous_value_t * const value;
72 const Erroneous_value_t * const after;
75 struct Erroneous_descriptor_t {
76 const int field_index;
77 const int omit_before; // -1 if none
78 const char* omit_before_qualifier; // NULL if none
79 const int omit_after; // -1 if none
80 const char* omit_after_qualifier; // NULL if none
81 const int values_size;
82 const Erroneous_values_t * const values_vec;
83 const int embedded_size;
84 const Erroneous_descriptor_t * const embedded_vec;
85 /** search in values_vec for the field with index field_idx */
86 const Erroneous_values_t* get_field_err_values(int field_idx) const;
87 /** search in embedded_vec for the field with index field_idx */
88 const Erroneous_descriptor_t* get_field_emb_descr(int field_idx) const;
89 /** if the current element of values_vec has index field_idx then returns it and increments values_idx */
90 const Erroneous_values_t* next_field_err_values(const int field_idx, int& values_idx) const;
91 /** if the current element of embedded_vec has index field_idx then returns it and increments edescr_idx */
92 const Erroneous_descriptor_t* next_field_emb_descr(const int field_idx, int& edescr_idx) const;
101 class Limit_Token_List;
102 #ifdef TITAN_RUNTIME_2
106 #ifdef TITAN_RUNTIME_2
107 #define VIRTUAL_IF_RUNTIME_2 virtual
109 #define VIRTUAL_IF_RUNTIME_2
113 * The base class for all types in TTCN-3 runtime environment.
115 * Uses the compiler-generated default constructor, copy constructor
116 * and assignment operator (they do nothing because the class is empty).
121 /** Whether the value is present.
122 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility!
123 * causes DTE, must be used only if the field is OPTIONAL<>
125 VIRTUAL_IF_RUNTIME_2 boolean ispresent() const;
127 VIRTUAL_IF_RUNTIME_2 void log() const;
129 /** Check whether the XML encoding of the type can begin with an XML element
130 * having the specified name and namespace.
132 * This function checks for the "starter tag" of the type. This usually
133 * means the tag should match the name in the XER descriptor,
134 * with a few notable exceptions:
136 * - If the type is untagged, then
137 * - if it's a record, all fields up to the the first non-optional field
139 * - if it's a record-of, the element is checked.
140 * - A \c universal \c charstring with ANY-ELEMENT can begin with any tag.
142 * @param name the XML element name (local name)
143 * @param uri the namespace URI of the element
144 * @param xd XER descriptor
145 * @param flavor one or more bits from XER_flavor. Only XER_EXTENDED counts
146 * @return TRUE if the XML element can begin the XER encoding of the type,
149 static boolean can_start(const char *name, const char *uri,
150 XERdescriptor_t const& xd, unsigned int flavor);
152 #ifdef TITAN_RUNTIME_2
153 virtual void set_param(Module_Param& param) = 0;
154 /** Whether the type is a sequence-of.
155 * @return \c FALSE */
156 virtual boolean is_seof() const { return FALSE; }
158 virtual void encode_text(Text_Buf& text_buf) const = 0;
159 virtual void decode_text(Text_Buf& text_buf) = 0;
161 virtual boolean is_bound() const = 0;
162 virtual boolean is_value() const { return is_bound(); }
163 virtual void clean_up() = 0;
165 /** returns if the value of this is equal to the value of the parameter. */
166 virtual boolean is_equal(const Base_Type* other_value) const = 0;
168 /** Performs: *this = *other_value.
169 * Does not take ownership of \p other_value. */
170 virtual void set_value(const Base_Type* other_value) = 0;
172 /** creates a copy of this object, returns a pointer to it. */
173 virtual Base_Type* clone() const = 0;
175 /** return reference to the type descriptor object for this type. */
176 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
178 /** @name override in class OPTIONAL<T>
180 virtual boolean is_optional() const { return FALSE; }
182 /** Is the optional value present or omit.
183 * @return \c true if present, \c false otherwise
184 * used for TTCN-3 ispresent()
186 virtual boolean is_present() const;
188 /** Access the embedded type of the optional.
190 * The implementation in Base_Type always causes a dynamic testcase error.
192 virtual Base_Type* get_opt_value();
194 /** Access the embedded type of the optional.
196 * The implementation in Base_Type always causes a dynamic testcase error.
198 virtual const Base_Type* get_opt_value() const;
200 /** Set the optional field to \c omit.
201 * The implementation in Base_Type always causes a dynamic testcase error.
203 virtual void set_to_omit();
205 /** Set the optional field to present.
206 * The implementation in Base_Type always causes a dynamic testcase error.
208 virtual void set_to_present();
211 virtual ~Base_Type() { }
215 /** Do nothing for non-structured types. */
216 VIRTUAL_IF_RUNTIME_2 void set_implicit_omit() { }
218 /** @brief Encode an instance of this object
222 @param p_td type descriptor
224 @param p_coding coding type to select BER, RAW or TEXT coding
227 VIRTUAL_IF_RUNTIME_2 void encode(const TTCN_Typedescriptor_t& p_td,
228 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const;
230 /** @brief Decode 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 decode(const TTCN_Typedescriptor_t& p_td,
240 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...);
243 /** Check type descriptor for BER encoding
245 * @param p_td a TTCN type descriptor
247 * Calls TTCN_EncDec_ErrorContext::error_internal if
248 * p_td has no BER descriptor.
250 static void BER_chk_descr(const TTCN_Typedescriptor_t& p_td);
252 /** Check the BER coding
254 * If \p p_coding is either BER_ENCODE_CER or BER_ENCODE_DER,
255 * this function does nothing. Otherwise, it issues a warning
256 * and sets \p p_coding to BER_ENCODE_DER.
258 * @param[in, out] p_coding BER coding
259 * @post p_coding is either BER_ENCODE_CER or BER_ENCODE_DER
261 static void BER_encode_chk_coding(unsigned& p_coding);
263 /** Check type descriptor for XER encoding and the XER coding variant
265 * @param[in, out] p_coding XER coding
266 * @param[in] p_td TTCN type descriptor
268 * If \p p_td has no XER descriptor, calls
269 * TTCN_EncDec_ErrorContext::error_internal()
271 * If \p p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED,
272 * this function does nothing. Otherwise, it issues a warning
273 * and sets \p p_coding to XER_BASIC.
275 * @post p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED
277 static void XER_encode_chk_coding(unsigned& p_coding,
278 const TTCN_Typedescriptor_t& p_td);
280 /** Check bound and create a 0-length TLV in case unbound is ignored.
282 * @param[in] p_isbound
283 * @return a newly constructed and empty \c ASN_BER_TLV_t
284 * if p_isbound is false, or NULL if p_isbound is true
285 * @note if p_isbound is false, this function calls
286 * TTCN_EncDec_ErrorContext::error with TTCN_EncDec::ET_UNBOUND.
287 * If that error is not ignored, this function does not return at all.
289 static ASN_BER_TLV_t* BER_encode_chk_bound(boolean p_isbound);
292 static void BER_encode_putoctets_OCTETSTRING
293 (unsigned char *target,
294 unsigned int octetnum_start, unsigned int octet_count,
295 int p_nof_octets, const unsigned char *p_octets_ptr);
298 static ASN_BER_TLV_t* BER_encode_TLV_OCTETSTRING
300 int p_nof_octets, const unsigned char *p_octets_ptr);
302 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
303 const int_val_t& p_int_val);
304 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
305 const int& p_int_val);
307 static void BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
308 boolean p_isvalid, int p_value);
310 static void BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
313 static boolean BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
316 ASN_BER_TLV_t& p_target_tlv);
318 static void BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
321 ASN_BER_TLV_t& p_target_tlv,
322 boolean tlv_present);
324 static void BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
325 const ASN_BER_TLV_t& p_tlv,
327 ASN_BER_TLV_t& stripped_tlv);
330 static void BER_decode_getoctets_OCTETSTRING
331 (const unsigned char *source, size_t s_len,
332 unsigned int& octetnum_start,
333 int& p_nof_octets, unsigned char *p_octets_ptr);
336 static void BER_decode_TLV_OCTETSTRING
337 (const ASN_BER_TLV_t& p_tlv, unsigned L_form,
338 unsigned int& octetnum_start,
339 int& p_nof_octets, unsigned char *p_octets_ptr);
341 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
342 unsigned L_form, int_val_t& p_int_val);
343 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
344 unsigned L_form, int& p_int_val);
346 static boolean BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
347 const ASN_BER_TLV_t& p_tlv,
349 ASN_BER_TLV_t& p_target_tlv);
351 static boolean BER_decode_CHOICE_selection(boolean select_result,
352 const ASN_BER_TLV_t& p_tlv);
354 static void BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
355 boolean p_isvalid, int p_value);
358 VIRTUAL_IF_RUNTIME_2 ASN_BER_TLV_t* BER_encode_TLV(
359 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
360 #ifdef TITAN_RUNTIME_2
361 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
362 const TTCN_Typedescriptor_t& /*p_td*/, unsigned /*p_coding*/) const;
363 virtual ASN_BER_TLV_t* BER_encode_negtest_raw() const;
364 virtual int encode_raw(TTCN_Buffer& p_buf) const;
365 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
368 /** Examines whether this message corresponds the tags in the
369 * descriptor. If the BER descriptor contains no tags, then returns
370 * TRUE. Otherwise, examines the first (outermost) tag only. */
371 #ifdef TITAN_RUNTIME_2
376 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
377 const ASN_BER_TLV_t& p_tlv);
379 /** Returns TRUE on success, FALSE otherwise. */
380 VIRTUAL_IF_RUNTIME_2 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
381 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
383 VIRTUAL_IF_RUNTIME_2 void BER_decode_opentypes(
384 TTCN_Type_list& /*p_typelist*/, unsigned /*L_form*/) {}
386 /** Encode with RAW coding.
387 * @return the length of the encoding
388 * @param p_td type descriptor
389 * @param myleaf filled with RAW encoding data
390 * @note Basetype::RAW_encode throws an error. */
391 VIRTUAL_IF_RUNTIME_2 int RAW_encode(const TTCN_Typedescriptor_t& p_td,
392 RAW_enc_tree& myleaf) const;
393 #ifdef TITAN_RUNTIME_2
394 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
395 const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
398 /** Decode with RAW coding
400 * @param p_td type descriptor
401 * @param p_buf buffer with data to be decoded
402 * @param limit number of bits the decoder is allowed to use. At the top level
403 * this is 8x the number of bytes in the buffer.
404 * @param top_bit_ord (LSB/MSB) from TTCN_RAWdescriptor_t::top_bit_order
405 * @param no_err set to TRUE if the decoder is to return errors silently,
406 * without calling TTCN_EncDec_ErrorContext::error
407 * @param sel_field selected field indicator for CROSSTAG, or -1
408 * @param first_call default TRUE. May be FALSE for a REPEATABLE record-of
409 * inside a set, if an element has been successfully decoded.
410 * @return length of decoded field, or a negative number for error
411 * @note Basetype::RAW_decode throws an error. */
412 VIRTUAL_IF_RUNTIME_2 int RAW_decode(
413 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int limit,
414 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1,
415 boolean first_call=TRUE);
418 /** Encode with TEXT encoding.
419 * @return the length of the encoding
420 * @param p_td type descriptor
421 * @param p_buf buffer for the encoded data
422 * @note Basetype::TEXT_encode throws an error. */
423 VIRTUAL_IF_RUNTIME_2 int TEXT_encode(const TTCN_Typedescriptor_t& p_td,
424 TTCN_Buffer& p_buf) const;
426 #ifdef TITAN_RUNTIME_2
427 /** Encode with TEXT encoding negative test.
428 * @return the length of the encoding
429 * @param p_err_descr type descriptor
430 * @param p_td type descriptor
431 * @param p_buf buffer for the encoded data
432 * @note Basetype::TEXT_encode_negtest throws an error. */
433 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
434 TTCN_Buffer& p_buf) const;
438 * @return decoded length
439 * @note Basetype::TEXT_decode throws an error. */
440 VIRTUAL_IF_RUNTIME_2 int TEXT_decode(
441 const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
442 boolean no_err=FALSE, boolean first_call=TRUE);
444 /** Write the XER encoding of the current object into the buffer.
446 * @param p_td type descriptor
447 * @param p_buf buffer
448 * @param flavor one of XER_flavor values
449 * @param indent indentation level
450 * @param emb_val embed values data (only relevant for record of types)
451 * @return number of bytes written into the buffer
453 VIRTUAL_IF_RUNTIME_2 int XER_encode(const XERdescriptor_t& p_td,
454 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
456 #ifdef TITAN_RUNTIME_2
457 virtual int XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
458 const XERdescriptor_t& /*p_td*/, TTCN_Buffer& /*p_buf*/,
459 unsigned int /*flavor*/, int /*indent*/, embed_values_enc_struct_t* /*emb_val*/) const;
462 /** Decode the current object from the supplied buffer.
464 * The XML pull parser presents a forward iterator over the XML elements.
465 * @pre Upon entering this function, the current node should be the one corresponding
466 * to this object, or possibly a text or whitespace node before it.
467 * Code should begin like this:
469 * int Foo::XER_decode() {
470 * int success=1, type;
471 * for ( ; success==1; success=reader.Read() ) {
472 * int type=reader.NodeType();
473 * if (XML_READER_TYPE_ELEMENT==type) {
480 * @post Upon leaving the function, the current node should be \b past
481 * the end tag, i.e. the next start tag (or any whitespace before it).
483 * It is important not to advance the parser too far, to avoid "stealing"
484 * the content of other fields (the parser can only go forward).
486 * @param p_td type descriptor
487 * @param reader Wrapper around the XML processor
488 * @param flavor one of XER_flavor values
489 * @param emb_val embed values data (only relevant for record of types)
490 * @return number of bytes "consumed"
492 VIRTUAL_IF_RUNTIME_2 int XER_decode(const XERdescriptor_t& p_td,
493 XmlReaderWrap& reader, unsigned int flavor, embed_values_dec_struct_t* emb_val);
495 /** Return an array of namespace declarations.
497 * @param[in] p_td XER descriptor of the type.
498 * @param[out] num set to the number of strings returned.
499 * @param[out] def_ns set to @p true if a default namespace was encountered
500 * @return an array of strings or NULL. All the strings in the array
501 * and the array itself must be deallocated by the caller (if num>0).
503 * The strings are in the following format:
504 * @code " xmlns:prefix='uri'" @endcode
505 * (the space at start allows direct concatenation of the strings)
507 VIRTUAL_IF_RUNTIME_2 char ** collect_ns(const XERdescriptor_t& p_td,
508 size_t& num, bool& def_ns) const;
510 /** Copy strings from @p new_namespaces to @p collected_ns,
511 * discarding duplicates.
513 * May reallocate @p collected_ns, in which case @p num_collected
514 * is adjusted accordingly.
516 * @param collected_ns target array
517 * @param num_collected number of already collected namespaces
518 * @param new_namespaces new array
519 * @param num_new number of new namespaces
521 * @post new_namespaces has been deallocated and set to NULL.
523 static void merge_ns(char **&collected_ns, size_t& num_collected,
524 char **new_namespaces, size_t num_new);
526 typedef char** (Base_Type::*collector_fn)
527 (const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
529 /** Start writing the XML representation
531 * @param[in] p_td XER descriptor
532 * @param[out] p_buf buffer to write into
533 * @param[in,out] flavor XER variant (basic, canonical or extended),
534 * also used to pass various flags between begin_xml and its caller
535 * @param[in] indent indentation level
536 * @param[in] empty true if an empty-element tag is needed
537 * @param[in] collector namespace collector function
538 * @param[in] type_atr type identification attribute for useNil/useUnion,
539 * in the following format: "xsi:type='foo'"
540 * @return "omit_tag": zero if the type's own tag was written (not omitted
541 * e.g. due to untagged).
542 * If the XML tag was omitted, the return value is nonzero.
543 * The special value -1 is returned if the XML in the buffer
544 * was shortened by exactly one character.
546 VIRTUAL_IF_RUNTIME_2 int begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
547 unsigned int& flavor, int indent, bool empty,
548 collector_fn collector = &Base_Type::collect_ns, const char *type_atr = NULL) const;
549 /** Finish the XML representation.
551 * @param[in] p_td XER descriptor
552 * @param[out] p_buf buffer to write into
553 * @param[in] flavor XER variant (basic, canonical or extended),
554 * also used to pass various flags
555 * @param[in] indent indentation level
556 * @param[in] empty true if an empty-element tag is needed
558 VIRTUAL_IF_RUNTIME_2 void end_xml (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
559 unsigned int flavor, int indent, bool empty) const;
562 * @return encoded length
563 * @note Basetype::JSON_encode throws an error. */
564 VIRTUAL_IF_RUNTIME_2 int JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&) const;
567 * @return decoded length
568 * @note Basetype::JSON_decode throws an error. */
569 VIRTUAL_IF_RUNTIME_2 int JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean);
571 /** not a component by default (components will return true) */
572 inline boolean is_component() { return FALSE; }
576 * This class is used by BER decoding to handle
577 * ComponentRelationConstraint.
579 class TTCN_Type_list {
582 const Base_Type **types;
583 /// Copy constructor disabled
584 TTCN_Type_list(const TTCN_Type_list&);
585 /// Assignment disabled
586 TTCN_Type_list& operator=(const TTCN_Type_list&);
588 TTCN_Type_list() : n_types(0), types(NULL) {}
590 void push(const Base_Type *p_type);
591 const Base_Type* pop();
592 /** If \a pos is 0 then returns the outermost, otherwise the "pos"th
594 const Base_Type* get_nth(size_t pos) const;
597 /** @name Type descriptor objects
600 extern const TTCN_Typedescriptor_t BOOLEAN_descr_;
601 extern const TTCN_Typedescriptor_t INTEGER_descr_;
602 extern const TTCN_Typedescriptor_t FLOAT_descr_;
603 extern const TTCN_Typedescriptor_t VERDICTTYPE_descr_;
604 extern const TTCN_Typedescriptor_t OBJID_descr_;
605 extern const TTCN_Typedescriptor_t BITSTRING_descr_;
606 extern const TTCN_Typedescriptor_t HEXSTRING_descr_;
607 extern const TTCN_Typedescriptor_t OCTETSTRING_descr_;
608 extern const TTCN_Typedescriptor_t CHARSTRING_descr_;
609 extern const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_;
610 extern const TTCN_Typedescriptor_t COMPONENT_descr_;
611 extern const TTCN_Typedescriptor_t DEFAULT_descr_;
612 extern const TTCN_Typedescriptor_t ASN_NULL_descr_;
613 extern const TTCN_Typedescriptor_t ASN_ANY_descr_;
614 extern const TTCN_Typedescriptor_t EXTERNAL_descr_;
615 extern const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_;
616 extern const TTCN_Typedescriptor_t CHARACTER_STRING_descr_;
617 extern const TTCN_Typedescriptor_t ObjectDescriptor_descr_;
618 extern const TTCN_Typedescriptor_t UTF8String_descr_;
619 extern const TTCN_Typedescriptor_t ASN_ROID_descr_;
620 extern const TTCN_Typedescriptor_t NumericString_descr_;
621 extern const TTCN_Typedescriptor_t PrintableString_descr_;
622 extern const TTCN_Typedescriptor_t TeletexString_descr_;
623 extern const TTCN_Typedescriptor_t& T61String_descr_;
624 extern const TTCN_Typedescriptor_t VideotexString_descr_;
625 extern const TTCN_Typedescriptor_t IA5String_descr_;
626 extern const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_;
627 extern const TTCN_Typedescriptor_t ASN_UTCTime_descr_;
628 extern const TTCN_Typedescriptor_t GraphicString_descr_;
629 extern const TTCN_Typedescriptor_t VisibleString_descr_;
630 extern const TTCN_Typedescriptor_t& ISO646String_descr_;
631 extern const TTCN_Typedescriptor_t GeneralString_descr_;
632 extern const TTCN_Typedescriptor_t UniversalString_descr_;
633 extern const TTCN_Typedescriptor_t BMPString_descr_;
636 #ifdef TITAN_RUNTIME_2
641 /** The base class of all record-of types when using RT2
644 // Record_Of_Template can be found in Template.hh
645 class Record_Of_Type : public Base_Type
647 friend class Set_Of_Template;
648 friend class Record_Of_Template;
650 friend boolean operator==(null_type null_value,
651 const Record_Of_Type& other_value);
652 struct recordof_setof_struct {
655 Base_Type **value_elements;
657 Erroneous_descriptor_t* err_descr;
659 struct refd_index_struct {
660 /** Stores the indices of elements that are referenced by 'out' and 'inout' parameters.
661 * These elements must not be deleted.*/
662 Vector<int> refd_indices;
664 /** Cached maximum value of \a refd_indices (default: -1).*/
668 static boolean compare_function(const Record_Of_Type *left_ptr, int left_index, const Record_Of_Type *right_ptr, int right_index);
669 Record_Of_Type() : val_ptr(NULL), err_descr(NULL), refd_ind_ptr(NULL) {}
670 Record_Of_Type(null_type other_value);
671 Record_Of_Type(const Record_Of_Type& other_value);
672 /// Assignment disabled
673 Record_Of_Type& operator=(const Record_Of_Type& other_value);
675 /** Returns the number of actual elements in the record of (does not count the
676 * unbound elements left at the end of the record of struct to make sure
677 * referenced elements are not deleted). */
678 int get_nof_elements() const;
680 /** Returns true if the indexed element is bound */
681 bool is_elem_bound(int index) const;
683 /** Returns the highest referenced index (uses \a max_refd_index as its cache)*/
684 int get_max_refd_index();
686 /** Returns true if the element at the given index is referenced by an 'out' or
687 * 'inout' parameter. */
688 bool is_index_refd(int index);
691 void set_val(null_type other_value);
693 boolean operator==(null_type other_value) const;
694 boolean operator!=(null_type other_value) const;
696 Base_Type* get_at(int index_value);
697 Base_Type* get_at(const INTEGER& index_value);
698 const Base_Type* get_at(int index_value) const;
699 const Base_Type* get_at(const INTEGER& index_value) const;
701 virtual boolean is_equal(const Base_Type* other_value) const;
702 virtual void set_value(const Base_Type* other_value);
704 /* following functions return rec_of or this or other_value */
705 Record_Of_Type* rotl(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
706 Record_Of_Type* rotr(int rotate_count, Record_Of_Type* rec_of) const;
707 Record_Of_Type* rotr(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
708 Record_Of_Type* concat(const Record_Of_Type* other_value, Record_Of_Type* rec_of) const;
709 /* parameter rec_of must be a newly created descendant object, these helper functions fill it */
710 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
711 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
712 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
713 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
715 void set_size(int new_size);
717 /** Implementation for isbound.
718 * @return \c true if the Record_Of itself is bound, \c false otherwise.
719 * Ignores elements. No need to be overridden. */
720 virtual boolean is_bound() const;
721 /** Implementation for isvalue.
722 * @return \c true if all elements are bound, \c false otherwise */
723 virtual boolean is_value() const;
724 virtual void clean_up();
726 virtual boolean is_seof() const { return TRUE; }
728 /** Implementation for the \c sizeof operation
729 * @return the number of elements */
731 int n_elem() const { return size_of(); }
733 /** Implementation for the lengthof operation.
734 * @return the index of the last bound element +1 */
735 int lengthof() const;
736 virtual void log() const;
737 virtual void set_param(Module_Param& param);
738 virtual void set_implicit_omit();
739 virtual void encode_text(Text_Buf& text_buf) const;
740 virtual void decode_text(Text_Buf& text_buf);
742 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
744 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
745 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
747 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
748 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;
749 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
751 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
752 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
755 * @param td type descriptor
757 * @param limit max number of bits that can be used
759 * @param no_err (not used)
761 * @param first_call default true, false for second and subsequent calls
762 * of a REPEATABLE record-of. Equivalent to "not repeated"
763 * @return number of bits used, or a negative number in case of error
765 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
766 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
767 /// raw extension bit
768 virtual int rawdec_ebv() const;
770 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
771 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
772 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
774 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
775 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
776 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
777 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned flavor, int indent, embed_values_enc_struct_t*) const;
778 /// Helper for XER_encode_negtest
779 int encode_element(int i, const Erroneous_values_t* err_vals,
780 const Erroneous_descriptor_t* emb_descr,
781 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
782 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int, embed_values_dec_struct_t*);
783 virtual boolean isXerAttribute() const;
784 virtual boolean isXmlValueList() const;
786 /** Encodes accordingly to the JSON encoding rules.
787 * Returns the length of the encoded data. */
788 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
790 /** Decodes accordingly to the JSON encoding rules.
791 * Returns the length of the decoded data. */
792 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
794 /** @returns \c true if this is a set-of type,
795 * \c false if this is a record-of type */
796 virtual boolean is_set() const = 0;
797 /** return the type descriptor of the element */
798 virtual const TTCN_Typedescriptor_t* get_elem_descr() const = 0;
799 /** creates an instance of the record's element class, using the default constructor */
800 virtual Base_Type* create_elem() const = 0;
802 /** Constant unbound element - return this instead of NULL in constant functions */
803 virtual const Base_Type* get_unbound_elem() const = 0;
805 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
806 virtual boolean can_start_v(const char *name, const char *prefix,
807 XERdescriptor_t const& xd, unsigned int flavor) = 0;
809 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
810 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
812 /** Indicates that the element at the given index is referenced by an 'out' or
813 * 'inout' parameter and must not be deleted.
814 * Used just before the actual function call that references the element. */
815 void add_refd_index(int index);
817 /** Indicates that the element at the given index is no longer referenced by
818 * an 'out' or 'inout' parameter.
819 * Used immediately after the actual function call that referenced the element. */
820 void remove_refd_index(int index);
823 extern boolean operator==(null_type null_value,
824 const Record_Of_Type& other_value);
825 extern boolean operator!=(null_type null_value,
826 const Record_Of_Type& other_value);
828 ////////////////////////////////////////////////////////////////////////////////
830 /** The base class of all record types when using RT2 */
831 // Record_Template can be found in Template.hh
832 class Record_Type : public Base_Type {
834 struct default_struct {
836 const Base_Type* value;
838 Erroneous_descriptor_t* err_descr;
841 Record_Type() : err_descr(NULL), bound_flag(false) {}
844 /** get pointer to a field */
845 virtual Base_Type* get_at(int index_value) = 0;
846 virtual const Base_Type* get_at(int index_value) const = 0;
849 /** get the index to a field based on its name and namespace URI, or -1 */
850 int get_index_byname(const char *name, const char *uri) const;
852 /** get number of fields */
853 virtual int get_count() const = 0;
854 /** number of optional fields */
855 virtual int optional_count() const { return 0; }
857 /** virtual functions inherited from Base_Type */
858 boolean is_equal(const Base_Type* other_value) const;
859 void set_value(const Base_Type* other_value);
861 /** @returns \c true if this is a set type,
862 * \c false if this is a record type */
863 virtual boolean is_set() const = 0;
865 /** return the name of a field */
866 virtual const char* fld_name(int field_index) const = 0;
868 /** return the descriptor of a field */
869 virtual const TTCN_Typedescriptor_t* fld_descr(int field_index) const = 0;
871 /** return int array which contains the indexes of optional fields
872 and a -1 value appended to the end, or return NULL if no optional fields,
873 generated classes override if there are optional fields */
874 virtual const int* get_optional_indexes() const { return NULL; }
875 /** default values and indexes, if no default fields then returns NULL,
876 the last default_struct.index is -1 */
877 virtual const default_struct* get_default_indexes() const { return NULL; }
879 /** override if it's TRUE for the specific type (if sdef->opentype_outermost) */
880 virtual boolean is_opentype_outermost() const { return FALSE; }
882 virtual boolean default_as_optional() const { return FALSE; }
884 virtual boolean is_bound() const;
885 virtual boolean is_value() const;
886 virtual void clean_up();
887 virtual void log() const;
888 virtual void set_param(Module_Param& param);
889 virtual void set_implicit_omit();
892 virtual void encode_text(Text_Buf& text_buf) const;
893 virtual void decode_text(Text_Buf& text_buf);
895 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
896 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
898 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
899 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
900 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
901 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
902 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
904 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
905 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *, const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
906 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);
907 // Helper functions for RAW enc/dec, shall be overridden in descendants if the default behavior is not enough.
908 virtual boolean raw_has_ext_bit() const { return FALSE; }
910 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
911 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
912 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
914 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
915 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
916 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
917 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
918 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
919 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
920 unsigned int, embed_values_dec_struct_t*);
922 /// Methods overridden in the derived (generated) class
923 virtual int get_xer_num_attr() const { return 0; /* default */ }
924 virtual const XERdescriptor_t* xer_descr(int field_index) const;
926 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
927 virtual boolean can_start_v(const char *name, const char *prefix,
928 XERdescriptor_t const& xd, unsigned int flavor) = 0;
930 /** Encodes accordingly to the JSON encoding rules.
931 * Returns the length of the encoded data. */
932 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
934 /** Decodes accordingly to the JSON encoding rules.
935 * Returns the length of the decoded data. */
936 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
938 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
939 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
941 /// Helper for XER_encode_negtest
942 int encode_field(int i, const Erroneous_values_t* err_vals, const Erroneous_descriptor_t* emb_descr,
943 TTCN_Buffer& p_buf, unsigned int sub_flavor, int indent, embed_values_enc_struct_t* emb_val) const;
946 ////////////////////////////////////////////////////////////////////////////////
947 class Empty_Record_Type : public Base_Type {
952 Empty_Record_Type(const Empty_Record_Type& other_value);
955 boolean operator==(null_type other_value) const;
956 inline boolean operator!=(null_type other_value) const { return !(*this == other_value); }
958 /** virtual functions inherited from Base_Type */
959 virtual boolean is_equal(const Base_Type* other_value) const;
960 virtual void set_value(const Base_Type* other_value);
962 /** @returns \c true if this is a set type,
963 * \c false if this is a record type */
964 virtual boolean is_set() const = 0;
966 inline void set_null() { bound_flag = TRUE; }
967 virtual boolean is_bound() const { return bound_flag; }
968 virtual void clean_up() { bound_flag = FALSE; }
969 virtual void log() const;
970 virtual void set_param(Module_Param& param);
972 int size_of() const { return 0; }
973 virtual void encode_text(Text_Buf& text_buf) const;
974 virtual void decode_text(Text_Buf& text_buf);
976 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
977 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
979 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
980 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
982 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
983 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);
985 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
986 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
988 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
989 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
990 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
991 unsigned int, embed_values_dec_struct_t*);
993 /** Encodes accordingly to the JSON encoding rules.
994 * Returns the length of the encoded data. */
995 virtual int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
997 /** Decodes accordingly to the JSON encoding rules.
998 * Returns the length of the decoded data. */
999 virtual int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
1002 struct Enum_Type : public Base_Type {
1003 virtual int as_int() const = 0;
1004 virtual void from_int(int) = 0;
1007 extern boolean operator==(null_type null_value, const Empty_Record_Type& other_value);
1008 extern boolean operator!=(null_type null_value, const Empty_Record_Type& other_value);
1010 #undef VIRTUAL_IF_RUNTIME_2
1013 template <class EXPR_TYPE>
1016 bool expr_evaluated;
1017 EXPR_TYPE expr_cache;
1018 virtual void eval_expr() {}
1020 Lazy_Param(): expr_evaluated(false) {}
1021 enum evaluated_state_t { EXPR_EVALED };
1022 Lazy_Param(evaluated_state_t /*p_es*/, EXPR_TYPE p_cache): expr_evaluated(true), expr_cache(p_cache) {}
1023 operator EXPR_TYPE&() {
1024 if (!expr_evaluated) { eval_expr(); expr_evaluated=true; }
1027 virtual ~Lazy_Param() {}