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"
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 struct embed_values_enc_struct_t;
31 struct embed_values_dec_struct_t;
33 /** @brief Type descriptor
35 * There is one type descriptor object for each type.
36 * Descriptors for built-in types are supplied by the runtime.
37 * Descriptors for user-defined types are written by the compiler
38 * in the generated code.
40 struct TTCN_Typedescriptor_t {
41 const char * const name; /**< Name of the type, e.g INTEGER, REAL, verdicttype, etc */
42 const ASN_BERdescriptor_t * const ber; /**< Information for BER coding */
43 const TTCN_RAWdescriptor_t * const raw; /**< Information for RAW coding */
44 const TTCN_TEXTdescriptor_t * const text; /**< Information for TEXT coding */
45 const XERdescriptor_t * const xer; /**< Information for XER */
46 const TTCN_JSONdescriptor_t * const json; /**< Information for JSON coding */
47 const TTCN_Typedescriptor_t * const oftype_descr; /**< Record-of element's type descriptor */
50 * Used by classes implementing more than one ASN.1 type
51 * (OBJID and UNIVERSAL_CHARSTRING).
55 UNIVERSALSTRING, BMPSTRING, UTF8STRING,
56 TELETEXSTRING, VIDEOTEXSTRING, GRAPHICSTRING, GENERALSTRING,
61 #ifdef TITAN_RUNTIME_2
63 struct Erroneous_value_t {
65 const Base_Type * const errval; // NULL if `omit'
66 const TTCN_Typedescriptor_t* type_descr; // NULL if `omit' or raw
69 struct Erroneous_values_t {
70 const int field_index;
71 const char* field_qualifier;
72 const Erroneous_value_t * const before;
73 const Erroneous_value_t * const value;
74 const Erroneous_value_t * const after;
77 struct Erroneous_descriptor_t {
78 const int field_index;
79 const int omit_before; // -1 if none
80 const char* omit_before_qualifier; // NULL if none
81 const int omit_after; // -1 if none
82 const char* omit_after_qualifier; // NULL if none
83 const int values_size;
84 const Erroneous_values_t * const values_vec;
85 const int embedded_size;
86 const Erroneous_descriptor_t * const embedded_vec;
87 /** search in values_vec for the field with index field_idx */
88 const Erroneous_values_t* get_field_err_values(int field_idx) const;
89 /** search in embedded_vec for the field with index field_idx */
90 const Erroneous_descriptor_t* get_field_emb_descr(int field_idx) const;
91 /** if the current element of values_vec has index field_idx then returns it and increments values_idx */
92 const Erroneous_values_t* next_field_err_values(const int field_idx, int& values_idx) const;
93 /** if the current element of embedded_vec has index field_idx then returns it and increments edescr_idx */
94 const Erroneous_descriptor_t* next_field_emb_descr(const int field_idx, int& edescr_idx) const;
101 class TTCN_Type_list;
103 class Limit_Token_List;
104 #ifdef TITAN_RUNTIME_2
108 #ifdef TITAN_RUNTIME_2
109 #define VIRTUAL_IF_RUNTIME_2 virtual
111 #define VIRTUAL_IF_RUNTIME_2
115 * The base class for all types in TTCN-3 runtime environment.
117 * Uses the compiler-generated default constructor, copy constructor
118 * and assignment operator (they do nothing because the class is empty).
123 /** Whether the value is present.
124 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility!
125 * causes DTE, must be used only if the field is OPTIONAL<>
127 VIRTUAL_IF_RUNTIME_2 boolean ispresent() const;
129 VIRTUAL_IF_RUNTIME_2 void log() const;
131 /** Check whether the XML encoding of the type can begin with an XML element
132 * having the specified name and namespace.
134 * This function checks for the "starter tag" of the type. This usually
135 * means the tag should match the name in the XER descriptor,
136 * with a few notable exceptions:
138 * - If the type is untagged, then
139 * - if it's a record, all fields up to the the first non-optional field
141 * - if it's a record-of, the element is checked.
142 * - A \c universal \c charstring with ANY-ELEMENT can begin with any tag.
144 * @param name the XML element name (local name)
145 * @param uri the namespace URI of the element
146 * @param xd XER descriptor
147 * @param flavor one or more bits from XER_flavor. Only XER_EXTENDED counts
148 * @return TRUE if the XML element can begin the XER encoding of the type,
151 static boolean can_start(const char *name, const char *uri,
152 XERdescriptor_t const& xd, unsigned int flavor);
154 #ifdef TITAN_RUNTIME_2
155 virtual void set_param(Module_Param& param) = 0;
156 /** Whether the type is a sequence-of.
157 * @return \c FALSE */
158 virtual boolean is_seof() const { return FALSE; }
160 virtual void encode_text(Text_Buf& text_buf) const = 0;
161 virtual void decode_text(Text_Buf& text_buf) = 0;
163 virtual boolean is_bound() const = 0;
164 virtual boolean is_value() const { return is_bound(); }
165 virtual void clean_up() = 0;
167 /** returns if the value of this is equal to the value of the parameter. */
168 virtual boolean is_equal(const Base_Type* other_value) const = 0;
170 /** Performs: *this = *other_value.
171 * Does not take ownership of \p other_value. */
172 virtual void set_value(const Base_Type* other_value) = 0;
174 /** creates a copy of this object, returns a pointer to it. */
175 virtual Base_Type* clone() const = 0;
177 /** return reference to the type descriptor object for this type. */
178 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
180 /** @name override in class OPTIONAL<T>
182 virtual boolean is_optional() const { return FALSE; }
184 /** Is the optional value present or omit.
185 * @return \c true if present, \c false otherwise
186 * used for TTCN-3 ispresent()
188 virtual boolean is_present() const;
190 /** Access the embedded type of the optional.
192 * The implementation in Base_Type always causes a dynamic testcase error.
194 virtual Base_Type* get_opt_value();
196 /** Access the embedded type of the optional.
198 * The implementation in Base_Type always causes a dynamic testcase error.
200 virtual const Base_Type* get_opt_value() const;
202 /** Set the optional field to \c omit.
203 * The implementation in Base_Type always causes a dynamic testcase error.
205 virtual void set_to_omit();
207 /** Set the optional field to present.
208 * The implementation in Base_Type always causes a dynamic testcase error.
210 virtual void set_to_present();
213 virtual ~Base_Type() { }
217 /** Do nothing for non-structured types. */
218 VIRTUAL_IF_RUNTIME_2 void set_implicit_omit() { }
220 /** @brief Encode an instance of this object
224 @param p_td type descriptor
226 @param p_coding coding type to select BER, RAW or TEXT coding
229 VIRTUAL_IF_RUNTIME_2 void encode(const TTCN_Typedescriptor_t& p_td,
230 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const;
232 /** @brief Decode an instance of this object
236 @param p_td type descriptor
238 @param p_coding coding type to select BER, RAW or TEXT coding
241 VIRTUAL_IF_RUNTIME_2 void decode(const TTCN_Typedescriptor_t& p_td,
242 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...);
245 /** Check type descriptor for BER encoding
247 * @param p_td a TTCN type descriptor
249 * Calls TTCN_EncDec_ErrorContext::error_internal if
250 * p_td has no BER descriptor.
252 static void BER_chk_descr(const TTCN_Typedescriptor_t& p_td);
254 /** Check the BER coding
256 * If \p p_coding is either BER_ENCODE_CER or BER_ENCODE_DER,
257 * this function does nothing. Otherwise, it issues a warning
258 * and sets \p p_coding to BER_ENCODE_DER.
260 * @param[in, out] p_coding BER coding
261 * @post p_coding is either BER_ENCODE_CER or BER_ENCODE_DER
263 static void BER_encode_chk_coding(unsigned& p_coding);
265 /** Check type descriptor for XER encoding and the XER coding variant
267 * @param[in, out] p_coding XER coding
268 * @param[in] p_td TTCN type descriptor
270 * If \p p_td has no XER descriptor, calls
271 * TTCN_EncDec_ErrorContext::error_internal()
273 * If \p p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED,
274 * this function does nothing. Otherwise, it issues a warning
275 * and sets \p p_coding to XER_BASIC.
277 * @post p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED
279 static void XER_encode_chk_coding(unsigned& p_coding,
280 const TTCN_Typedescriptor_t& p_td);
282 /** Check bound and create a 0-length TLV in case unbound is ignored.
284 * @param[in] p_isbound
285 * @return a newly constructed and empty \c ASN_BER_TLV_t
286 * if p_isbound is false, or NULL if p_isbound is true
287 * @note if p_isbound is false, this function calls
288 * TTCN_EncDec_ErrorContext::error with TTCN_EncDec::ET_UNBOUND.
289 * If that error is not ignored, this function does not return at all.
291 static ASN_BER_TLV_t* BER_encode_chk_bound(boolean p_isbound);
294 static void BER_encode_putoctets_OCTETSTRING
295 (unsigned char *target,
296 unsigned int octetnum_start, unsigned int octet_count,
297 int p_nof_octets, const unsigned char *p_octets_ptr);
300 static ASN_BER_TLV_t* BER_encode_TLV_OCTETSTRING
302 int p_nof_octets, const unsigned char *p_octets_ptr);
304 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
305 const int_val_t& p_int_val);
306 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
307 const int& p_int_val);
309 static void BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
310 boolean p_isvalid, int p_value);
312 static void BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
315 static boolean BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
318 ASN_BER_TLV_t& p_target_tlv);
320 static void BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
323 ASN_BER_TLV_t& p_target_tlv,
324 boolean tlv_present);
326 static void BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
327 const ASN_BER_TLV_t& p_tlv,
329 ASN_BER_TLV_t& stripped_tlv);
332 static void BER_decode_getoctets_OCTETSTRING
333 (const unsigned char *source, size_t s_len,
334 unsigned int& octetnum_start,
335 int& p_nof_octets, unsigned char *p_octets_ptr);
338 static void BER_decode_TLV_OCTETSTRING
339 (const ASN_BER_TLV_t& p_tlv, unsigned L_form,
340 unsigned int& octetnum_start,
341 int& p_nof_octets, unsigned char *p_octets_ptr);
343 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
344 unsigned L_form, int_val_t& p_int_val);
345 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
346 unsigned L_form, int& p_int_val);
348 static boolean BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
349 const ASN_BER_TLV_t& p_tlv,
351 ASN_BER_TLV_t& p_target_tlv);
353 static boolean BER_decode_CHOICE_selection(boolean select_result,
354 const ASN_BER_TLV_t& p_tlv);
356 static void BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
357 boolean p_isvalid, int p_value);
360 VIRTUAL_IF_RUNTIME_2 ASN_BER_TLV_t* BER_encode_TLV(
361 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
362 #ifdef TITAN_RUNTIME_2
363 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
364 const TTCN_Typedescriptor_t& /*p_td*/, unsigned /*p_coding*/) const;
365 virtual ASN_BER_TLV_t* BER_encode_negtest_raw() const;
366 virtual int encode_raw(TTCN_Buffer& p_buf) const;
367 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
370 /** Examines whether this message corresponds the tags in the
371 * descriptor. If the BER descriptor contains no tags, then returns
372 * TRUE. Otherwise, examines the first (outermost) tag only. */
373 #ifdef TITAN_RUNTIME_2
378 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
379 const ASN_BER_TLV_t& p_tlv);
381 /** Returns TRUE on success, FALSE otherwise. */
382 VIRTUAL_IF_RUNTIME_2 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
383 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
385 VIRTUAL_IF_RUNTIME_2 void BER_decode_opentypes(
386 TTCN_Type_list& /*p_typelist*/, unsigned /*L_form*/) {}
388 /** Encode with RAW coding.
389 * @return the length of the encoding
390 * @param p_td type descriptor
391 * @param myleaf filled with RAW encoding data
392 * @note Basetype::RAW_encode throws an error. */
393 VIRTUAL_IF_RUNTIME_2 int RAW_encode(const TTCN_Typedescriptor_t& p_td,
394 RAW_enc_tree& myleaf) const;
395 #ifdef TITAN_RUNTIME_2
396 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
397 const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
400 /** Decode with RAW coding
402 * @param p_td type descriptor
403 * @param p_buf buffer with data to be decoded
404 * @param limit number of bits the decoder is allowed to use. At the top level
405 * this is 8x the number of bytes in the buffer.
406 * @param top_bit_ord (LSB/MSB) from TTCN_RAWdescriptor_t::top_bit_order
407 * @param no_err set to TRUE if the decoder is to return errors silently,
408 * without calling TTCN_EncDec_ErrorContext::error
409 * @param sel_field selected field indicator for CROSSTAG, or -1
410 * @param first_call default TRUE. May be FALSE for a REPEATABLE record-of
411 * inside a set, if an element has been successfully decoded.
412 * @return length of decoded field, or a negative number for error
413 * @note Basetype::RAW_decode throws an error. */
414 VIRTUAL_IF_RUNTIME_2 int RAW_decode(
415 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int limit,
416 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1,
417 boolean first_call=TRUE);
420 /** Encode with TEXT encoding.
421 * @return the length of the encoding
422 * @param p_td type descriptor
423 * @param p_buf buffer for the encoded data
424 * @note Basetype::TEXT_encode throws an error. */
425 VIRTUAL_IF_RUNTIME_2 int TEXT_encode(const TTCN_Typedescriptor_t& p_td,
426 TTCN_Buffer& p_buf) const;
428 #ifdef TITAN_RUNTIME_2
429 /** Encode with TEXT encoding negative test.
430 * @return the length of the encoding
431 * @param p_err_descr type descriptor
432 * @param p_td type descriptor
433 * @param p_buf buffer for the encoded data
434 * @note Basetype::TEXT_encode_negtest throws an error. */
435 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
436 TTCN_Buffer& p_buf) const;
440 * @return decoded length
441 * @note Basetype::TEXT_decode throws an error. */
442 VIRTUAL_IF_RUNTIME_2 int TEXT_decode(
443 const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
444 boolean no_err=FALSE, boolean first_call=TRUE);
446 /** Write the XER encoding of the current object into the buffer.
448 * @param p_td type descriptor
449 * @param p_buf buffer
450 * @param flavor one of XER_flavor values
451 * @param indent indentation level
452 * @param emb_val embed values data (only relevant for record of types)
453 * @return number of bytes written into the buffer
455 VIRTUAL_IF_RUNTIME_2 int XER_encode(const XERdescriptor_t& p_td,
456 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
458 #ifdef TITAN_RUNTIME_2
459 virtual int XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
460 const XERdescriptor_t& /*p_td*/, TTCN_Buffer& /*p_buf*/,
461 unsigned int /*flavor*/, int /*indent*/, embed_values_enc_struct_t* /*emb_val*/) const;
464 /** Decode the current object from the supplied buffer.
466 * The XML pull parser presents a forward iterator over the XML elements.
467 * @pre Upon entering this function, the current node should be the one corresponding
468 * to this object, or possibly a text or whitespace node before it.
469 * Code should begin like this:
471 * int Foo::XER_decode() {
472 * int success=1, type;
473 * for ( ; success==1; success=reader.Read() ) {
474 * int type=reader.NodeType();
475 * if (XML_READER_TYPE_ELEMENT==type) {
482 * @post Upon leaving the function, the current node should be \b past
483 * the end tag, i.e. the next start tag (or any whitespace before it).
485 * It is important not to advance the parser too far, to avoid "stealing"
486 * the content of other fields (the parser can only go forward).
488 * @param p_td type descriptor
489 * @param reader Wrapper around the XML processor
490 * @param flavor one of XER_flavor values
491 * @param emb_val embed values data (only relevant for record of types)
492 * @return number of bytes "consumed"
494 VIRTUAL_IF_RUNTIME_2 int XER_decode(const XERdescriptor_t& p_td,
495 XmlReaderWrap& reader, unsigned int flavor, embed_values_dec_struct_t* emb_val);
497 /** Return an array of namespace declarations.
499 * @param[in] p_td XER descriptor of the type.
500 * @param[out] num set to the number of strings returned.
501 * @param[out] def_ns set to @p true if a default namespace was encountered
502 * @return an array of strings or NULL. All the strings in the array
503 * and the array itself must be deallocated by the caller (if num>0).
505 * The strings are in the following format:
506 * @code " xmlns:prefix='uri'" @endcode
507 * (the space at start allows direct concatenation of the strings)
509 VIRTUAL_IF_RUNTIME_2 char ** collect_ns(const XERdescriptor_t& p_td,
510 size_t& num, bool& def_ns) const;
512 /** Copy strings from @p new_namespaces to @p collected_ns,
513 * discarding duplicates.
515 * May reallocate @p collected_ns, in which case @p num_collected
516 * is adjusted accordingly.
518 * @param collected_ns target array
519 * @param num_collected number of already collected namespaces
520 * @param new_namespaces new array
521 * @param num_new number of new namespaces
523 * @post new_namespaces has been deallocated and set to NULL.
525 static void merge_ns(char **&collected_ns, size_t& num_collected,
526 char **new_namespaces, size_t num_new);
528 typedef char** (Base_Type::*collector_fn)
529 (const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
531 /** Start writing the XML representation
533 * @param[in] p_td XER descriptor
534 * @param[out] p_buf buffer to write into
535 * @param[in,out] flavor XER variant (basic, canonical or extended),
536 * also used to pass various flags between begin_xml and its caller
537 * @param[in] indent indentation level
538 * @param[in] empty true if an empty-element tag is needed
539 * @param[in] collector namespace collector function
540 * @param[in] type_atr type identification attribute for useNil/useUnion,
541 * in the following format: "xsi:type='foo'"
542 * @return "omit_tag": zero if the type's own tag was written (not omitted
543 * e.g. due to untagged).
544 * If the XML tag was omitted, the return value is nonzero.
545 * The special value -1 is returned if the XML in the buffer
546 * was shortened by exactly one character.
548 VIRTUAL_IF_RUNTIME_2 int begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
549 unsigned int& flavor, int indent, bool empty,
550 collector_fn collector = &Base_Type::collect_ns, const char *type_atr = NULL) const;
551 /** Finish the XML representation.
553 * @param[in] p_td XER descriptor
554 * @param[out] p_buf buffer to write into
555 * @param[in] flavor XER variant (basic, canonical or extended),
556 * also used to pass various flags
557 * @param[in] indent indentation level
558 * @param[in] empty true if an empty-element tag is needed
560 VIRTUAL_IF_RUNTIME_2 void end_xml (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
561 unsigned int flavor, int indent, bool empty) const;
564 * @return encoded length
565 * @note Basetype::JSON_encode throws an error. */
566 VIRTUAL_IF_RUNTIME_2 int JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&) const;
569 * @return decoded length
570 * @note Basetype::JSON_decode throws an error. */
571 VIRTUAL_IF_RUNTIME_2 int JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean);
573 /** not a component by default (components will return true) */
574 inline boolean is_component() { return FALSE; }
578 * This class is used by BER decoding to handle
579 * ComponentRelationConstraint.
581 class TTCN_Type_list {
584 const Base_Type **types;
585 /// Copy constructor disabled
586 TTCN_Type_list(const TTCN_Type_list&);
587 /// Assignment disabled
588 TTCN_Type_list& operator=(const TTCN_Type_list&);
590 TTCN_Type_list() : n_types(0), types(NULL) {}
592 void push(const Base_Type *p_type);
593 const Base_Type* pop();
594 /** If \a pos is 0 then returns the outermost, otherwise the "pos"th
596 const Base_Type* get_nth(size_t pos) const;
599 /** @name Type descriptor objects
602 extern const TTCN_Typedescriptor_t BOOLEAN_descr_;
603 extern const TTCN_Typedescriptor_t INTEGER_descr_;
604 extern const TTCN_Typedescriptor_t FLOAT_descr_;
605 extern const TTCN_Typedescriptor_t VERDICTTYPE_descr_;
606 extern const TTCN_Typedescriptor_t OBJID_descr_;
607 extern const TTCN_Typedescriptor_t BITSTRING_descr_;
608 extern const TTCN_Typedescriptor_t HEXSTRING_descr_;
609 extern const TTCN_Typedescriptor_t OCTETSTRING_descr_;
610 extern const TTCN_Typedescriptor_t CHARSTRING_descr_;
611 extern const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_;
612 extern const TTCN_Typedescriptor_t COMPONENT_descr_;
613 extern const TTCN_Typedescriptor_t DEFAULT_descr_;
614 extern const TTCN_Typedescriptor_t ASN_NULL_descr_;
615 extern const TTCN_Typedescriptor_t ASN_ANY_descr_;
616 extern const TTCN_Typedescriptor_t EXTERNAL_descr_;
617 extern const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_;
618 extern const TTCN_Typedescriptor_t CHARACTER_STRING_descr_;
619 extern const TTCN_Typedescriptor_t ObjectDescriptor_descr_;
620 extern const TTCN_Typedescriptor_t UTF8String_descr_;
621 extern const TTCN_Typedescriptor_t ASN_ROID_descr_;
622 extern const TTCN_Typedescriptor_t NumericString_descr_;
623 extern const TTCN_Typedescriptor_t PrintableString_descr_;
624 extern const TTCN_Typedescriptor_t TeletexString_descr_;
625 extern const TTCN_Typedescriptor_t& T61String_descr_;
626 extern const TTCN_Typedescriptor_t VideotexString_descr_;
627 extern const TTCN_Typedescriptor_t IA5String_descr_;
628 extern const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_;
629 extern const TTCN_Typedescriptor_t ASN_UTCTime_descr_;
630 extern const TTCN_Typedescriptor_t GraphicString_descr_;
631 extern const TTCN_Typedescriptor_t VisibleString_descr_;
632 extern const TTCN_Typedescriptor_t& ISO646String_descr_;
633 extern const TTCN_Typedescriptor_t GeneralString_descr_;
634 extern const TTCN_Typedescriptor_t UniversalString_descr_;
635 extern const TTCN_Typedescriptor_t BMPString_descr_;
638 #ifdef TITAN_RUNTIME_2
643 /** The base class of all record-of types when using RT2
646 // Record_Of_Template can be found in Template.hh
647 class Record_Of_Type : public Base_Type, public RefdIndexInterface
649 friend class Set_Of_Template;
650 friend class Record_Of_Template;
652 friend boolean operator==(null_type null_value,
653 const Record_Of_Type& other_value);
654 struct recordof_setof_struct {
657 Base_Type **value_elements;
659 Erroneous_descriptor_t* err_descr;
661 struct refd_index_struct {
662 /** Stores the indices of elements that are referenced by 'out' and 'inout' parameters.
663 * These elements must not be deleted.*/
664 Vector<int> refd_indices;
666 /** Cached maximum value of \a refd_indices (default: -1).*/
670 static boolean compare_function(const Record_Of_Type *left_ptr, int left_index, const Record_Of_Type *right_ptr, int right_index);
671 Record_Of_Type() : val_ptr(NULL), err_descr(NULL), refd_ind_ptr(NULL) {}
672 Record_Of_Type(null_type other_value);
673 Record_Of_Type(const Record_Of_Type& other_value);
674 /// Assignment disabled
675 Record_Of_Type& operator=(const Record_Of_Type& other_value);
677 /** Returns the number of actual elements in the record of (does not count the
678 * unbound elements left at the end of the record of struct to make sure
679 * referenced elements are not deleted). */
680 int get_nof_elements() const;
682 /** Returns true if the indexed element is bound */
683 bool is_elem_bound(int index) const;
685 /** Returns the highest referenced index (uses \a max_refd_index as its cache)*/
686 int get_max_refd_index();
688 /** Returns true if the element at the given index is referenced by an 'out' or
689 * 'inout' parameter. */
690 bool is_index_refd(int index);
693 void set_val(null_type other_value);
695 boolean operator==(null_type other_value) const;
696 boolean operator!=(null_type other_value) const;
698 Base_Type* get_at(int index_value);
699 Base_Type* get_at(const INTEGER& index_value);
700 const Base_Type* get_at(int index_value) const;
701 const Base_Type* get_at(const INTEGER& index_value) const;
703 virtual boolean is_equal(const Base_Type* other_value) const;
704 virtual void set_value(const Base_Type* other_value);
706 /* following functions return rec_of or this or other_value */
707 Record_Of_Type* rotl(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
708 Record_Of_Type* rotr(int rotate_count, Record_Of_Type* rec_of) const;
709 Record_Of_Type* rotr(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
710 Record_Of_Type* concat(const Record_Of_Type* other_value, Record_Of_Type* rec_of) const;
711 /* parameter rec_of must be a newly created descendant object, these helper functions fill it */
712 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
713 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
714 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
715 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
717 void set_size(int new_size);
719 /** Implementation for isbound.
720 * @return \c true if the Record_Of itself is bound, \c false otherwise.
721 * Ignores elements. No need to be overridden. */
722 virtual boolean is_bound() const;
723 /** Implementation for isvalue.
724 * @return \c true if all elements are bound, \c false otherwise */
725 virtual boolean is_value() const;
726 virtual void clean_up();
728 virtual boolean is_seof() const { return TRUE; }
730 /** Implementation for the \c sizeof operation
731 * @return the number of elements */
733 int n_elem() const { return size_of(); }
735 /** Implementation for the lengthof operation.
736 * @return the index of the last bound element +1 */
737 int lengthof() const;
738 virtual void log() const;
739 virtual void set_param(Module_Param& param);
740 virtual void set_implicit_omit();
741 virtual void encode_text(Text_Buf& text_buf) const;
742 virtual void decode_text(Text_Buf& text_buf);
744 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
746 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
747 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
749 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
750 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;
751 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
753 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
754 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
757 * @param td type descriptor
759 * @param limit max number of bits that can be used
761 * @param no_err (not used)
763 * @param first_call default true, false for second and subsequent calls
764 * of a REPEATABLE record-of. Equivalent to "not repeated"
765 * @return number of bits used, or a negative number in case of error
767 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
768 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
769 /// raw extension bit
770 virtual int rawdec_ebv() const;
772 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
773 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
774 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
776 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
777 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
778 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
779 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned flavor, int indent, embed_values_enc_struct_t*) const;
780 /// Helper for XER_encode_negtest
781 int encode_element(int i, const XERdescriptor_t& p_td, const Erroneous_values_t* err_vals,
782 const Erroneous_descriptor_t* emb_descr,
783 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const;
784 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int, embed_values_dec_struct_t*);
785 virtual boolean isXerAttribute() const;
786 virtual boolean isXmlValueList() const;
788 /** Encodes accordingly to the JSON encoding rules.
789 * Returns the length of the encoded data. */
790 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
792 /** Decodes accordingly to the JSON encoding rules.
793 * Returns the length of the decoded data. */
794 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
796 /** @returns \c true if this is a set-of type,
797 * \c false if this is a record-of type */
798 virtual boolean is_set() 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 virtual 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 virtual 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() {}