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 ///////////////////////////////////////////////////////////////////////////////
13 #include <stddef.h> // for size_t
14 #include <string.h> // strncmp for the inline function
19 #ifdef TITAN_RUNTIME_2
21 class Erroneous_descriptor_t;
23 namespace PreGenRecordOf {
24 class PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING;
25 class PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING__OPTIMIZED;
30 /** @defgroup XER XER codec
33 * @brief ASN.1 XML Encoding Rules, ITU-T Rec X.693 and amd1
36 /** XER flags for various uses.
38 * Low values specify the XML encoding variant (Basic, Canonical, Extended)
39 * Other bits have dual uses:
40 * - set in XERdescriptor_t::xer_bits, according to XML encoding attributes
41 * - passed in as additional flags in the \c flavor parameter, usually
42 * to XER_encode. These are used when encoding attributes in a parent type
43 * influence the encoding of its components (e.g. EMBED-VALUES on a record
44 * change the encoding of all components).
47 XER_BASIC = 1U << 0, /**< Basic XER with indentation */
48 XER_CANONICAL = 1U << 1, /**< Canonical XER, no indentation */
49 XER_EXTENDED = 1U << 2, /**< Extended XER */
50 DEF_NS_PRESENT = 1U << 3, // 0x08
51 DEF_NS_SQUASHED = 1U << 4, // 0x10
52 XER_MASK = 0x1FU, /**< All the "real" XER flavors plus DEF_NS */
54 /* Additional flags, for the parent to pass information to its children
55 * (when the parent affects the child, e.g. LIST) */
56 XER_ESCAPE_ENTITIES = 1U << 5, /**< Escape according to X.680/2002, 11.15.8,
57 used internally by UNIVERSAL_CHARSTRING. */
58 XER_RECOF = 1U << 6, /**< Generating code for the contained type
59 of a record-of/set-of. Only affects BOOLEAN, CHOICE, ENUMERATED and NULL
60 (see Table 5 in X.680 (11/2008) clause 26.5) */
62 /* More flags for XERdescriptor_t::xer_bits */
63 ANY_ATTRIBUTES = 1U << 7, // 0xooo80
64 ANY_ELEMENT = 1U << 8, // 0xoo100
65 XER_ATTRIBUTE = 1U << 9, // 0xoo200
66 BASE_64 = 1U << 10, // 0xoo400
67 XER_DECIMAL = 1U << 11, // 0xoo800
68 // DEFAULT-FOR-EMPTY has its own field
69 EMBED_VALUES = 1U << 12, // 0xo1000
70 /** LIST encoding instruction for record-of/set-of. */
71 XER_LIST = 1U << 13, // 0xo2000
72 // NAME is stored in the descriptor
73 // NAMESPACE is folded into the name
74 XER_TEXT = 1U << 14, // 0xo4000
75 UNTAGGED = 1U << 15, // 0xo8000
76 USE_NIL = 1U << 16, // 0x10000
77 USE_NUMBER = 1U << 17, // 0x20000
78 USE_ORDER = 1U << 18, // 0x40000
79 USE_QNAME = 1U << 19, // 0x80000
80 USE_TYPE_ATTR = 1U << 20, // 0x100000, either USE-TYPE or USE-UNION
81 HAS_1UNTAGGED = 1U << 21, // 0x200000 member, and it's character-encodable
82 // another hint to pass down to the children:
83 PARENT_CLOSED = 1U << 22, // 0x400000
84 FORM_UNQUALIFIED=1U << 23, // 0X800000 (qualified is more frequent)
85 XER_TOPLEVEL = 1U << 24, //0X1000000 (toplevel, for decoding)
86 SIMPLE_TYPE = 1U << 25, /*0X2000000 always encode on one line:
87 <foo>content</foo>, never <foo>\ncontent\n</foo> */
88 BXER_EMPTY_ELEM= 1U << 26, /*0X4000000 boolean and enum encode themselves
89 as empty elements in BXER only. This also influences them in record-of */
90 ANY_FROM = 1U << 27, // 0x8000000 anyElement from ... or anyAttributes from ...
91 ANY_EXCEPT = 1U << 28, // 0x10000000 anyElement except ... or anyAttributes except ...
92 EXIT_ON_ERROR = 1U << 29, /* 0x20000000 clean up and exit instead of throwing
93 a decoding error, used on alternatives of a union with USE-UNION */
94 XER_OPTIONAL = 1U << 30, // 0x40000000 is an optional field of a record or set
95 BLOCKED = 1U << 31 // 0x80000000 either ABSTRACT or BLOCK
98 /** WHITESPACE actions.
99 * Note that WHITESPACE_COLLAPSE includes the effect of WHITESPACE_REPLACE
100 * and the code relies on WHITESPACE_COLLAPSE having the higher value. */
101 enum XER_whitespace_action {
107 /// Check that \p f has the canonical flavor.
108 inline bool is_canonical(unsigned int f)
110 return (f & XER_CANONICAL) != 0;
113 inline bool is_exer(unsigned int f)
115 return (f & XER_EXTENDED) != 0;
118 /** Is this a member of a SEQUENCE OF
120 * @param f XER flavor
121 * @return \c true if \p contains \c XER_RECOF, \c false otherwise
123 inline bool is_record_of(unsigned int f)
125 return (f & XER_RECOF) != 0;
130 * This is now hijacked to mean "the enclosing type told us to omit our tag".
131 * Hence the check for USE-NIL too.
133 * @param f XER flavor
134 * @return \c true if \c XER_EXTENDED and either \c XER_LIST or \c USE_NIL is set.
136 inline bool is_exerlist(unsigned int f)
138 return (f & XER_EXTENDED) && ((f & (XER_LIST|USE_NIL|USE_TYPE_ATTR)) != 0);
141 /** Descriptor for XER encoding/decoding during runtime.
143 * This structure contains XER enc/dec information for the runtime.
145 * There is an instance of this struct for most TTCN3/ASN1 types.
146 * Because TITAN generates type aliases (typedefs) when one type references
147 * another (e.g. "type integer i1" results in "typedef INTEGER i1"),
148 * this struct holds information to distinguish them during encoding.
150 * Only those encoding instructions need to be recorded which can apply to
151 * scalar types (e.g. BOOLEAN, REAL, etc., usually implemented by classes in core/)
152 * because the same code needs to handle all varieties.
154 * - ANY-ELEMENT : UFT8String
155 * - BASE64 : OCTET STRING, open type, restricted character string
157 * - NAME : anything (this is already present as \c name)
159 * - TEXT : INTEGER, enum
160 * - USE-NUMBER : enum
161 * - WHITESPACE : restricted character string
163 * ANY-ATTRIBUTE, EMBED-VALUES, LIST, USE-TYPE, USE-UNION apply to sequence/choice types;
164 * their effect will be resolved by the compiler.
166 * Instances of this type are written by the compiler into the generated code,
167 * one for each type. For a TTCN3 type foo_bar, there will be a class
168 * foo__bar and a XERdescriptor_t instance named foo__bar_xer_.
170 * Each built-in type has a descriptor (e.g. INTEGER_xer_) in the runtime.
172 * The \a name field contains the closing tag including a newline, e.g.
173 * \c "</INTEGER>\n". This allows for a more efficient output of the tags,
174 * minimizing the number of one-character inserts into the buffer.
176 * The start tag is written as an 'open angle bracket' character,
177 * followed by the \a name field without its first two characters (\c "</" ).
179 * In case of the canonical encoding (\c CXER ) there is no indenting,
180 * so the final newline is omitted by reducing the length by one.
184 * int Foo::XER_encode(const XERdescriptor_t& p_td,
185 * TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const {
186 * int canon = is_canonical(flavor);
187 * if (!canon) do_indent(p_buf, indent);
188 * // output the start tag
190 * p_buf.put_s((size_t)p_td.namelen-2-canon, (const unsigned char*)p_td.name+2);
191 * // this is not right if Foo has attributes :(
192 * // we'll need to reduce namelen further (or just get rid of this hackery altogether)
194 * // output actual content
197 * // output the closing tag
198 * if (!canon) do_indent(p_buf, indent);
199 * p_buf.put_s((size_t)p_td.namelen-canon, (const unsigned char*)p_td.name);
206 * int Foo::XER_encode(const XERdescriptor_t& p_td,
207 * TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const {
208 * int canon = is_canonical(flavor);
209 * if (!canon) do_indent(p_buf, indent);
210 * // output an empty element tag
212 * p_buf.put_s((size_t)p_td.namelen-4, (const unsigned char*)p_td.name+2);
213 * p_buf.put_s(3 - canon, (const unsigned char*)"/>\n");
217 * @note We don't generate the XML prolog. This is required for Canonical XER
218 * (X.693 9.1.1) and permitted for Basic-XER (8.2.1).
220 * @note X.693 amd1 (EXER) 10.3.5 states: If an "ExtendedXMLValue" is empty,
221 * and its associated tags have not been removed by the use of an UNTAGGED
222 * encoding instruction, then the associated preceding and following tags
223 * <b>can (as an encoder's option)</b> be replaced with
224 * an XML empty-element tag (see ITU-T Rec. X.680 | ISO/IEC 8824-1, 16.8).
225 * This is called the associated empty-element tag.
227 * @note X.693 (XER) 9.1.4 states: (for Canonical XER)
228 * If the XML value notation permits the use of an XML empty-element tag
229 * (see ITU-T Rec. X.680 |ISO/IEC 8824-1, 15.5 and 16.8),
230 * then this empty-element tag @b shall be used.
232 * @note After editing XERdescriptor_t, make sure to change XER_STRUCT2 here
233 * and generate_code_xerdescriptor() in Type.cc.
235 struct XERdescriptor_t
237 /** (closing) Tag name, including a newline.
238 * First is for basic and canonical XER, second for EXER */
239 const char *names[2];
240 /** Length of closing tag string (strlen of names[i]) */
241 const unsigned short namelens[2];
242 /** Various EXER flags */
243 const unsigned long xer_bits;
244 /** Whitespace handling */
245 const XER_whitespace_action whitespace;
246 /** value to compare for DEFAULT-FOR-EMPTY */
247 const Base_Type* dfeValue;
248 /** The module to which the type belongs. May be NULL in a descriptor
249 * for a built-in type, e.g. in INTEGER_xer_ */
250 const TTCN_Module* my_module;
251 /** Index into the module's namespace list.
252 * -1 means no namespace.
253 * >=+0 and FORM_UNQUALIFIED means that there IS a namespace,
254 * it just doesn't show up in the XML (but libxml2 will return it). */
257 /** Number of namespace URIs*/
258 const unsigned short nof_ns_uris;
260 /** List of namespace URIs
261 * In case of "anyElement" variants this list contains the valid ("anyElement from ...")
262 * or invalid ("anyElement except ...") namespace URIs.
263 * The unqualified namespace is marked by an empty string ("").*/
264 const char** ns_uris;
266 /** Points to the element type's XER descriptor in case of 'record of' and 'set of' types */
267 const XERdescriptor_t* oftype_descr;
270 /** Information related to the embedded values in XML encoding
272 * Used when a record/set with the EMBED-VALUES coding instruction contains
273 * one or more record of/set of fields. */
274 struct embed_values_enc_struct_t
276 #ifdef TITAN_RUNTIME_2
277 /** Stores the array of embedded values */
278 const Record_Of_Type* embval_array;
279 /** Stores the erroneous descriptor of the embedded values field (for negative tests) */
280 const Erroneous_descriptor_t* embval_err;
281 /** Error value index for the embedded values (for negative tests) */
282 int embval_err_val_idx;
283 /** Erroneous descriptor index for the embedded values (for negative tests) */
284 int embval_err_descr_idx;
286 /** Stores the array of embedded values (regular record-of) */
287 const PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING* embval_array_reg;
288 /** Stores the array of embedded values (optimized record-of) */
289 const PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING__OPTIMIZED* embval_array_opt;
291 /** Stores the index of the next embedded value to be read */
295 /** Information related to the embedded values in XML decoding
297 * Used when a record/set with the EMBED-VALUES coding instruction contains
298 * one or more record of/set of fields. */
299 struct embed_values_dec_struct_t
301 #ifdef TITAN_RUNTIME_2
302 /** Stores the array of embedded values */
303 Record_Of_Type* embval_array;
305 /** Stores the array of embedded values (regular record-of) */
306 PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING* embval_array_reg;
307 /** Stores the array of embedded values (optimized record-of) */
308 PreGenRecordOf::PREGEN__RECORD__OF__UNIVERSAL__CHARSTRING__OPTIMIZED* embval_array_opt;
310 /** Stores the number of embedded values that are currently in the array,
311 * and the index where the next one should be inserted */
315 /** Check the name of an XML node against a XER type descriptor.
317 * @param name the (local, unqualified) name of the XML element
318 * @param p_td the type descriptor
319 * @param exer \c true if Extended XER decoding, \c false for Basic and Canonical XER
320 * @return \c true if \p name corresponds to the type descriptor, \c false otherwise.
322 inline bool check_name(const char *name, const XERdescriptor_t& p_td, int exer)
324 return strncmp(name, p_td.names[exer], p_td.namelens[exer]-2) == 0
325 && name[p_td.namelens[exer]-2] == '\0';
328 /** Verify the namespace of an XML node against a XER type descriptor.
330 * @pre EXER decoding is in progress
332 * @param ns_uri the URI of the current node
333 * @param p_td the type descriptor
334 * @return \c true if \p ns_uri is NULL and the type has no namespace
335 * or it's the default namespace.
336 * @return \c true if \p ns_uri is not NULL and it matches the one referenced
338 * @return \c false otherwise.
340 bool check_namespace(const char *ns_uri, const XERdescriptor_t& p_td);
342 /** Check that the current element matches the XER descriptor
344 * Calls TTCN_EncDec_ErrorContext::error() if it doesn't.
346 * @param reader XML reader
347 * @param p_td XER descriptor
348 * @param exer 0 for Basic/Canonical XER, 1 for EXER
349 * @return the name of the current element
351 const char* verify_name(XmlReaderWrap& reader, const XERdescriptor_t& p_td, int exer);
353 /** Check the end tag
355 * Calls verify_name(), then compares \a depth with the current XML depth
356 * and calls TTCN_EncDec_ErrorContext::error() if they don't match.
358 * @param reader XML reader
359 * @param p_td XER descriptor
360 * @param depth XML tag depth (0 for top-level element)
361 * @param exer 0 for Basic/Canonical XER, 1 for EXER
363 void verify_end(XmlReaderWrap& reader, const XERdescriptor_t& p_td, const int depth, int exer);
367 /** Output the namespace prefix
369 * The namespace prefix is determined by the XER descriptor (@a my_module
370 * and @a ns_index fields). It is not written if p_td.xer_bits has
373 * @param p_td XER descriptor
374 * @param p_buf buffer to write into
376 * @pre the caller should check that E-XER encoding is in effect.
378 void write_ns_prefix(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf);
380 /** Return the namespace referred to by a prefix
382 * Finds the namespace specified by \a prefix in the module's namespace table
383 * and returns its URI. Returns NULL if the namespace is not found.
385 * @param prefix namespace prefix to be found
386 * @param p_td XER descriptor (contains the module to search in)
388 const char* get_ns_uri_from_prefix(const char *prefix, const XERdescriptor_t& p_td);
390 /** Output the beginning of an XML attribute.
392 * Writes a space, the attribute name (from \p p_td), and the string "='".
393 * @post the buffer is ready to receive the actual value
395 * @param p_td XER descriptor (contains the attribute name)
396 * @param p_buf buffer to write into
398 inline void begin_attribute(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf)
401 write_ns_prefix(p_td, p_buf);
402 p_buf.put_s((size_t)p_td.namelens[1]-2, (const unsigned char*)p_td.names[1]);
403 p_buf.put_s((size_t)2, (const unsigned char*)"='");
408 * @param buf buffer to write into.
409 * @param level indent level
411 * Writes the appropriate amount of indentation into \p buf.
413 * Indentation is currently done with with tabs.
415 int do_indent(TTCN_Buffer& buf, int level);
417 /** Ensures that the anyElement or anyAttribute field respects its namespace
419 * In case of "anyElement from ..." or "anyAttributes from ..." the namespace
420 * needs to be in the specified list.
421 * In case of "anyElement except ..." or "anyAttributes except ..." it cannot
422 * match any of the namespaces from the list.
423 * An invalid namespace causes a dynamic test case error.
425 * @param p_td type descriptor of the field in question, contains the list of
426 * valid or invalid namespaces
427 * @param p_xmlns constains the namespace in question
429 void check_namespace_restrictions(const XERdescriptor_t& p_td, const char* p_xmlns);
432 #ifdef DEFINE_XER_STRUCT
433 # define XER_STRUCT2(type_name,xmlname) \
434 extern const XERdescriptor_t type_name##_xer_ = { \
435 { xmlname ">\n", xmlname ">\n" }, \
436 { 2+sizeof(xmlname)-1, 2+sizeof(xmlname)-1 }, \
437 0UL, WHITESPACE_PRESERVE, NULL, NULL, 0, 0, NULL, NULL }
438 // The compiler should fold the two identical strings into one
440 # define XER_STRUCT_COPY(cpy,original) \
441 const XERdescriptor_t& cpy##_xer_ = original##_xer_
443 /** Declare a XER structure.
444 * @param type_name the name of a Titan runtime class
445 * @param xmlname the XML tag name
447 # define XER_STRUCT2(type_name,xmlname) extern const XERdescriptor_t type_name##_xer_
448 # define XER_STRUCT_COPY(cpy,original) extern const XERdescriptor_t& cpy##_xer_
451 /** Declare a XER structure where the name of the type matches the tag */
452 # define XER_STRUCT(name) XER_STRUCT2(name, #name)
454 /* XER descriptors for built-in types.
455 * The XML tag names are defined in Table 4, referenced by clause
456 * 11.25.2 (X.680/2002) or 12.36.2 (X.680/2008) */
458 // Types shared between ASN.1 and TTCN-3
459 XER_STRUCT2(BITSTRING, "BIT_STRING");
460 XER_STRUCT (BOOLEAN);
461 XER_STRUCT (CHARSTRING);
462 XER_STRUCT2(FLOAT, "REAL");
463 XER_STRUCT (INTEGER);
464 XER_STRUCT2(OBJID, "OBJECT_IDENTIFIER");
465 XER_STRUCT2(OCTETSTRING, "OCTET_STRING");
466 XER_STRUCT (UNIVERSAL_CHARSTRING);
468 XER_STRUCT(RELATIVE_OID);
472 XER_STRUCT2(EMBEDDED_PDV, "SEQUENCE");
473 XER_STRUCT2(EMBEDDED_PDV_identification, "identification");
474 XER_STRUCT2(EMBEDDED_PDV_identification_sxs, "syntaxes");
475 XER_STRUCT2(EMBEDDED_PDV_identification_sxs_abs, "abstract");
476 XER_STRUCT2(EMBEDDED_PDV_identification_sxs_xfr, "transfer");
477 XER_STRUCT2(EMBEDDED_PDV_identification_sx , "syntax");
478 XER_STRUCT2(EMBEDDED_PDV_identification_pci, "presentation-context-id");
479 XER_STRUCT2(EMBEDDED_PDV_identification_cn , "context-negotiation");
480 XER_STRUCT2(EMBEDDED_PDV_identification_cn_pci , "presentation-context-id");
481 XER_STRUCT2(EMBEDDED_PDV_identification_cn_tsx , "transfer-syntax");
482 XER_STRUCT2(EMBEDDED_PDV_identification_ts , "transfer-syntax");
483 XER_STRUCT2(EMBEDDED_PDV_identification_fix, "fixed");
484 XER_STRUCT2(EMBEDDED_PDV_data_value_descriptor, "data-value-descriptor");
485 XER_STRUCT2(EMBEDDED_PDV_data_value, "data-value");
488 XER_STRUCT2(EXTERNAL, "SEQUENCE");
489 XER_STRUCT2(EXTERNAL_direct_reference , "direct-reference");
490 XER_STRUCT2(EXTERNAL_indirect_reference, "indirect-reference");
491 XER_STRUCT2(EXTERNAL_data_value_descriptor, "data-value-descriptor");
492 XER_STRUCT2(EXTERNAL_encoding, "encoding");
493 XER_STRUCT2(EXTERNAL_encoding_singleASN , "single-ASN1-type");
494 XER_STRUCT2(EXTERNAL_encoding_octet_aligned, "octet-aligned");
495 XER_STRUCT2(EXTERNAL_encoding_arbitrary , "arbitrary");
497 // The big, scary ASN.1 unrestricted character string
498 XER_STRUCT2(CHARACTER_STRING, "SEQUENCE");
499 XER_STRUCT_COPY(CHARACTER_STRING_identification, EMBEDDED_PDV_identification);
500 XER_STRUCT_COPY(CHARACTER_STRING_identification_sxs, EMBEDDED_PDV_identification_sxs);
501 XER_STRUCT_COPY(CHARACTER_STRING_identification_sxs_abs, EMBEDDED_PDV_identification_sxs_abs);
502 XER_STRUCT_COPY(CHARACTER_STRING_identification_sxs_xfr, EMBEDDED_PDV_identification_sxs_xfr);
503 XER_STRUCT_COPY(CHARACTER_STRING_identification_sx , EMBEDDED_PDV_identification_sx);
504 XER_STRUCT_COPY(CHARACTER_STRING_identification_pci, EMBEDDED_PDV_identification_pci);
505 XER_STRUCT_COPY(CHARACTER_STRING_identification_cn , EMBEDDED_PDV_identification_cn);
506 XER_STRUCT_COPY(CHARACTER_STRING_identification_cn_pci , EMBEDDED_PDV_identification_cn_pci);
507 XER_STRUCT_COPY(CHARACTER_STRING_identification_cn_tsx , EMBEDDED_PDV_identification_cn_tsx);
508 XER_STRUCT_COPY(CHARACTER_STRING_identification_ts , EMBEDDED_PDV_identification_ts);
509 XER_STRUCT_COPY(CHARACTER_STRING_identification_fix, EMBEDDED_PDV_identification_fix);
510 // this one is used for decoding only (only to check that it's absent)
511 XER_STRUCT2(CHARACTER_STRING_data_value_descriptor, "data-value-descriptor");
512 // this can not be folded with EMBEDDED-PDV
513 XER_STRUCT2(CHARACTER_STRING_data_value, "string-value");
515 // ASN.1 restricted character strings
516 XER_STRUCT(GeneralString);
517 XER_STRUCT(NumericString);
518 XER_STRUCT(UTF8String);
519 XER_STRUCT(PrintableString);
520 XER_STRUCT(UniversalString);
522 XER_STRUCT(BMPString);
523 XER_STRUCT(GraphicString);
524 XER_STRUCT(IA5String);
525 XER_STRUCT(TeletexString);
526 XER_STRUCT(VideotexString);
527 XER_STRUCT(VisibleString);
529 XER_STRUCT2(ASN_NULL, "NULL");
530 XER_STRUCT2(ASN_ROID, "RELATIVE_OID");
531 XER_STRUCT (ASN_ANY); // obsoleted by 2002
534 XER_STRUCT2(HEXSTRING, "hexstring");
535 XER_STRUCT2(VERDICTTYPE, "verdicttype");