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 ///////////////////////////////////////////////////////////////////////////////
8 #include "../common/memory.h"
13 void def_encdec(const char *p_classname
,
14 char **p_classdef
, char **p_classsrc
,
15 boolean ber
, boolean raw
, boolean text
, boolean xer
,
16 boolean json
, boolean is_leaf
)
23 "void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&,"
24 " TTCN_EncDec::coding_t, ...) const;\n"
25 "void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&,"
26 " TTCN_EncDec::coding_t, ...);\n");
29 "ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,"
30 " unsigned p_coding) const;\n"
31 "boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const"
32 " ASN_BER_TLV_t& p_tlv, unsigned L_form);\n"
36 "int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;\n"
37 "int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&,"
38 " int, raw_order_t, boolean no_err=FALSE,"
39 "int sel_field=-1, boolean first_call=TRUE);\n"
43 "int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;\n"
44 "int TEXT_decode(const TTCN_Typedescriptor_t&,"
45 "TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE,"
46 "boolean first_call=TRUE);\n"
48 if (xer
) /* XERSTUFF encdec function headers */
51 "// written by %s in " __FILE__
" at %d\n"
53 "int XER_encode(const XERdescriptor_t&, TTCN_Buffer&, unsigned int, int, "
54 "embed_values_enc_struct_t*) const;\n"
55 "int XER_decode(const XERdescriptor_t&, XmlReaderWrap&, unsigned int, "
56 "unsigned int, embed_values_dec_struct_t*);\n"
57 "static boolean can_start(const char *name, const char *uri, "
58 "XERdescriptor_t const& xd, unsigned int);\n"
61 , __FUNCTION__
, __LINE__
64 "boolean can_start_v(const char *name, const char *uri, "
65 "XERdescriptor_t const& xd, unsigned int);\n" : ""
69 "int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;\n"
70 "int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);\n");
74 "void %s::encode(const TTCN_Typedescriptor_t& p_td,"
75 " TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const\n"
78 " va_start(pvar, p_coding);\n"
79 " switch(p_coding) {\n"
80 " case TTCN_EncDec::CT_BER: {\n"
81 " TTCN_EncDec_ErrorContext ec(\"While BER-encoding type"
82 " '%%s': \", p_td.name);\n"
83 " unsigned BER_coding=va_arg(pvar, unsigned);\n"
84 " BER_encode_chk_coding(BER_coding);\n"
85 " ASN_BER_TLV_t *tlv=BER_encode_TLV(p_td, BER_coding);\n"
86 " tlv->put_in_buffer(p_buf);\n"
87 " ASN_BER_TLV_t::destruct(tlv);\n"
89 " case TTCN_EncDec::CT_RAW: {\n"
90 " TTCN_EncDec_ErrorContext ec(\"While RAW-encoding type"
91 " '%%s': \", p_td.name);\n"
93 " TTCN_EncDec_ErrorContext::error_internal\n"
94 " (\"No RAW descriptor available for type '%%s'.\", p_td.name);\n"
95 " RAW_enc_tr_pos rp;\n"
98 " RAW_enc_tree root(%s, NULL, &rp, 1, p_td.raw);\n"
99 " RAW_encode(p_td, root);\n"
100 " root.put_to_buf(p_buf);\n"
102 " case TTCN_EncDec::CT_TEXT: {\n"
103 " TTCN_EncDec_ErrorContext ec("
104 "\"While TEXT-encoding type '%%s': \", p_td.name);\n"
106 " TTCN_EncDec_ErrorContext::error_internal\n"
107 " (\"No TEXT descriptor available for type '%%s'.\", p_td.name);\n"
108 " TEXT_encode(p_td,p_buf);\n"
110 /* XERSTUFF encoder */
111 " case TTCN_EncDec::CT_XER: {\n"
112 " TTCN_EncDec_ErrorContext ec("
113 "\"While XER-encoding type '%%s': \", p_td.name);\n"
114 " unsigned XER_coding=va_arg(pvar, unsigned);\n"
115 " XER_encode_chk_coding(XER_coding, p_td);\n"
116 /* Do not use %s_xer_ here. It supplies the XER descriptor of oldtype
117 * even if encoding newtype for:
118 * <ttcn>type newtype oldtype;</ttcn> */
119 " XER_encode(*(p_td.xer),p_buf, XER_coding, 0, 0);\n"
120 " p_buf.put_c('\\n');\n" /* make sure it has a newline */
122 " case TTCN_EncDec::CT_JSON: {\n"
123 " TTCN_EncDec_ErrorContext ec("
124 "\"While JSON-encoding type '%%s': \", p_td.name);\n"
126 " TTCN_EncDec_ErrorContext::error_internal\n"
127 " (\"No JSON descriptor available for type '%%s'.\", p_td.name);\n"
128 " JSON_Tokenizer tok(va_arg(pvar, int) != 0);\n"
129 " JSON_encode(p_td, tok);\n"
130 " p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());\n"
133 " TTCN_error(\"Unknown coding method requested to encode"
134 " type '%%s'\", p_td.name);\n"
139 , p_classname
, is_leaf
?"TRUE":"FALSE"
144 "// written by %s in " __FILE__
" at %d\n"
146 "void %s::decode(const TTCN_Typedescriptor_t& p_td,"
147 " TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...)\n"
150 " va_start(pvar, p_coding);\n"
151 " switch(p_coding) {\n"
152 " case TTCN_EncDec::CT_BER: {\n"
153 " TTCN_EncDec_ErrorContext ec(\"While BER-decoding type"
154 " '%%s': \", p_td.name);\n"
155 " unsigned L_form=va_arg(pvar, unsigned);\n"
156 " ASN_BER_TLV_t tlv;\n"
157 " BER_decode_str2TLV(p_buf, tlv, L_form);\n"
158 " BER_decode_TLV(p_td, tlv, L_form);\n"
159 " if(tlv.isComplete) p_buf.increase_pos(tlv.get_len());\n"
161 " case TTCN_EncDec::CT_RAW: {\n"
162 " TTCN_EncDec_ErrorContext ec(\"While RAW-decoding"
163 " type '%%s': \", p_td.name);\n"
165 " TTCN_EncDec_ErrorContext::error_internal\n"
166 " (\"No RAW descriptor available for type '%%s'.\", p_td.name);\n"
167 " raw_order_t r_order;\n"
168 " switch(p_td.raw->top_bit_order) {\n"
169 " case TOP_BIT_LEFT:\n"
170 " r_order=ORDER_LSB;\n"
172 " case TOP_BIT_RIGHT:\n"
174 " r_order=ORDER_MSB;\n"
176 " int rawr = RAW_decode(p_td, p_buf, p_buf.get_len()*8, r_order);\n"
177 " if(rawr<0) switch (-rawr) {\n"
178 " case TTCN_EncDec::ET_INCOMPL_MSG:\n"
179 " case TTCN_EncDec::ET_LEN_ERR:\n"
180 " ec.error((TTCN_EncDec::error_type_t)-rawr, "
181 "\"Can not decode type '%%s', because incomplete message was received\", "
184 " case 1:\n" /* from the generic -1 return value */
186 " ec.error(TTCN_EncDec::ET_INVAL_MSG, "
187 "\"Can not decode type '%%s', because invalid "
188 "message was received\", p_td.name);\n"
192 " case TTCN_EncDec::CT_TEXT: {\n"
193 " Limit_Token_List limit;\n"
194 " TTCN_EncDec_ErrorContext ec(\"While TEXT-decoding type '%%s': \","
197 " TTCN_EncDec_ErrorContext::error_internal\n"
198 " (\"No TEXT descriptor available for type '%%s'.\", p_td.name);\n"
199 " const unsigned char *b_data=p_buf.get_data();\n"
200 " if(b_data[p_buf.get_len()-1]!='\\0'){\n"
201 " p_buf.set_pos(p_buf.get_len());\n"
202 " p_buf.put_zero(8,ORDER_LSB);\n"
205 " if(TEXT_decode(p_td,p_buf,limit)<0)\n"
206 " ec.error(TTCN_EncDec::ET_INCOMPL_MSG,"
207 "\"Can not decode type '%%s', because invalid or incomplete"
208 " message was received\", p_td.name);\n"
210 /* XERSTUFF decoder */
211 " case TTCN_EncDec::CT_XER: {\n"
212 " TTCN_EncDec_ErrorContext ec("
213 "\"While XER-decoding type '%%s': \", p_td.name);\n"
214 " unsigned XER_coding=va_arg(pvar, unsigned);\n"
215 " XER_encode_chk_coding(XER_coding, p_td);\n"
216 " XmlReaderWrap reader(p_buf);\n"
217 " for (int rd_ok=reader.Read(); rd_ok==1; rd_ok=reader.Read()) {\n"
218 " if (reader.NodeType() == XML_READER_TYPE_ELEMENT) break;\n"
220 " XER_decode(*(p_td.xer), reader, XER_coding | XER_TOPLEVEL, XER_NONE, 0);\n"
221 " size_t bytes = reader.ByteConsumed();\n"
222 " p_buf.set_pos(bytes);\n"
224 " case TTCN_EncDec::CT_JSON: {\n"
225 " TTCN_EncDec_ErrorContext ec(\"While JSON-decoding type '%%s': \","
228 " TTCN_EncDec_ErrorContext::error_internal\n"
229 " (\"No JSON descriptor available for type '%%s'.\", p_td.name);\n"
230 " JSON_Tokenizer tok((const char*)p_buf.get_data(), p_buf.get_len());\n"
231 " if(JSON_decode(p_td, tok, false)<0)\n"
232 " ec.error(TTCN_EncDec::ET_INCOMPL_MSG,"
233 "\"Can not decode type '%%s', because invalid or incomplete"
234 " message was received\", p_td.name);\n"
235 " p_buf.set_pos(tok.get_buf_pos());\n"
238 " TTCN_error(\"Unknown coding method requested to decode"
239 " type '%%s'\", p_td.name);\n"
244 , __FUNCTION__
, __LINE__
248 *p_classdef
=mputstr(*p_classdef
, def
);
250 *p_classsrc
=mputstr(*p_classsrc
, src
);
254 char *genRawFieldChecker(char *src
, const rawAST_coding_taglist
*taglist
,
258 for (i
= 0; i
< taglist
->nElements
; i
++) {
259 rawAST_coding_field_list
*fields
= taglist
->fields
+ i
;
260 char *field_name
= NULL
;
261 boolean first_expr
= TRUE
;
263 if (i
> 0) src
= mputstr(src
, is_equal
? " || " : " && ");
264 for (j
= 0; j
< fields
->nElements
; j
++) {
265 rawAST_coding_fields
*field
= fields
->fields
+ j
;
267 /* this is the first field reference */
268 if (field
->fieldtype
== UNION_FIELD
)
269 field_name
= mputprintf(field_name
,"(*field_%s)",field
->nthfieldname
);
271 field_name
= mputprintf(field_name
,"field_%s", field
->nthfieldname
);
274 /* this is not the first field reference */
275 if (field
->fieldtype
== UNION_FIELD
) {
276 /* checking for the right selection within the union */
278 if (taglist
->nElements
> 1) src
= mputc(src
, '(');
281 else src
= mputstr(src
, is_equal
? " && " : " || ");
282 src
= mputprintf(src
, "%s.get_selection() %s %s%s%s", field_name
,
283 is_equal
? "==" : "!=", fields
->fields
[j
- 1].type
,
284 "::ALT_", field
->nthfieldname
);
286 /* appending the current field name to the field reference */
287 field_name
= mputprintf(field_name
, ".%s()", field
->nthfieldname
);
289 if (j
< fields
->nElements
- 1 && field
->fieldtype
== OPTIONAL_FIELD
) {
290 /* this is not the last field in the chain and it is optional */
292 if (taglist
->nElements
> 1) src
= mputc(src
, '(');
295 else src
= mputstr(src
, is_equal
? " && " : " || ");
296 /* check for the presence */
297 if (!is_equal
) src
= mputc(src
, '!');
298 src
= mputprintf(src
, "%s.ispresent()", field_name
);
299 /* add an extra () to the field reference */
300 field_name
= mputstr(field_name
, "()");
303 if (!first_expr
) src
= mputstr(src
, is_equal
? " && " : " || ");
304 /* compare the referred field with the given value */
305 src
= mputprintf(src
, "%s %s %s", field_name
, is_equal
? "==" : "!=",
307 if (!first_expr
&& taglist
->nElements
> 1) src
= mputc(src
, ')');
313 char *genRawTagChecker(char *src
, const rawAST_coding_taglist
*taglist
)
316 rawAST_coding_field_list temp_field
;
317 src
= mputstr(src
, " RAW_enc_tree* temp_leaf;\n");
318 for (temp_tag
= 0; temp_tag
< taglist
->nElements
; temp_tag
++) {
319 temp_field
= taglist
->fields
[temp_tag
];
320 src
= mputprintf(src
, " {\n"
321 " RAW_enc_tr_pos pr_pos%d;\n"
322 " pr_pos%d.level=myleaf.curr_pos.level+%d;\n"
323 " int new_pos%d[]={", temp_tag
, temp_tag
, temp_field
.nElements
, temp_tag
);
324 for (l
= 0; l
< temp_field
.nElements
; l
++) {
325 src
= mputprintf(src
, "%s%d", l
? "," : "", temp_field
.fields
[l
].nthfield
);
327 src
= mputprintf(src
, "};\n"
328 " pr_pos%d.pos=init_new_tree_pos(myleaf.curr_pos,%d,new_pos%d);\n"
329 " temp_leaf = myleaf.get_node(pr_pos%d);\n"
330 " if(temp_leaf != NULL){\n", temp_tag
, temp_field
.nElements
, temp_tag
, temp_tag
);
331 if (temp_field
.value
[0] != ' ') {
332 src
= mputprintf(src
, " %s new_val = %s;\n"
333 " new_val.RAW_encode(%s_descr_,*temp_leaf);\n",
334 temp_field
.fields
[temp_field
.nElements
- 1].type
, temp_field
.value
,
335 temp_field
.fields
[temp_field
.nElements
- 1].typedescr
);
338 src
= mputprintf(src
, " %s.RAW_encode(%s_descr_,*temp_leaf);\n",
339 temp_field
.value
, temp_field
.fields
[temp_field
.nElements
- 1].typedescr
);
341 src
= mputstr(src
, " } else");
343 src
= mputstr(src
, " {\n"
344 " TTCN_EncDec_ErrorContext::error\n"
345 " (TTCN_EncDec::ET_OMITTED_TAG, \"Encoding a tagged, but omitted"
348 for (temp_tag
= taglist
->nElements
- 1; temp_tag
>= 0 ; temp_tag
--) {
349 src
= mputprintf(src
, " free_tree_pos(pr_pos%d.pos);\n"