blob: f5f94e9cfe559ed39ec063bd473a9f2a1f6b1104 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*
2 * Don't look into this file. First, because it's a mess, and second, because
3 * it's a brain of the compiler, and you don't wanna mess with brains do you? ;)
4 */
5#include "asn1c_internal.h"
6#include "asn1c_C.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +00007#include "asn1c_constraint.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00008#include "asn1c_out.h"
9#include "asn1c_misc.h"
Lev Walkinad0d6372017-08-08 02:02:42 -070010#include "asn1c_ioc.h"
Lev Walkin59b176e2005-11-26 11:25:14 +000011#include <asn1fix_crange.h> /* constraint groker from libasn1fix */
12#include <asn1fix_export.h> /* other exportables from libasn1fix */
Lev Walkinda997b12017-08-04 01:38:41 -070013#include <asn1parser.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000014
Lev Walkinfd171ef2004-06-06 07:20:17 +000015typedef struct tag2el_s {
16 struct asn1p_type_tag_s el_tag;
17 int el_no;
Lev Walkin38abe792004-06-14 13:09:45 +000018 int toff_first;
19 int toff_last;
Lev Walkinfd171ef2004-06-06 07:20:17 +000020 asn1p_expr_t *from_expr;
21} tag2el_t;
22
Lev Walkin33d5d3c2004-10-03 09:13:30 +000023typedef enum fte {
24 FTE_ALLTAGS,
25 FTE_CANONICAL_XER,
26} fte_e;
27static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
28static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
Lev Walkinfd171ef2004-06-06 07:20:17 +000029
Lev Walkin0f5d74c2005-08-14 15:03:31 +000030enum onc_flags {
31 ONC_noflags = 0x00,
32 ONC_avoid_keywords = 0x01,
33 ONC_force_compound_name = 0x02,
34};
35static int out_name_chain(arg_t *arg, enum onc_flags);
Lev Walkinf15320b2004-06-03 03:38:44 +000036static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
37static int asn1c_lang_C_type_SET_def(arg_t *arg);
38static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
39static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
Lev Walkinc3e29402004-09-10 06:07:18 +000040static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
Lev Walkin59b176e2005-11-26 11:25:14 +000041static int compute_extensions_start(asn1p_expr_t *expr);
Lev Walkinda9a3b82005-08-16 17:00:21 +000042static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr);
Lev Walkina9cc46e2004-09-22 16:06:28 +000043static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin59964be2004-08-25 02:03:12 +000044static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
Lev Walkinb9e98cb2017-08-02 12:53:44 -070045static int emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range);
46static int emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range);
47static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int juscountvalues, const char *type);
Lev Walkin98eabc12017-07-19 08:51:11 +040048static int emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx);
Lev Walkin4b5dae32006-10-09 12:27:44 +000049static int emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx);
Lev Walkin59964be2004-08-25 02:03:12 +000050static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin33d5d3c2004-10-03 09:13:30 +000051static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
Lev Walkinc8285712005-03-04 22:18:20 +000052static int emit_include_dependencies(arg_t *arg);
Lev Walkinb85a8132005-08-18 13:38:19 +000053static asn1p_expr_t *terminal_structable(arg_t *arg, asn1p_expr_t *expr);
54static int expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr);
55static int asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key);
Lev Walkin59b176e2005-11-26 11:25:14 +000056static asn1p_expr_type_e expr_get_type(arg_t *arg, asn1p_expr_t *expr);
57static int try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out);
58static int *compute_canonical_members_order(arg_t *arg, int el_count);
Lev Walkinf15320b2004-06-03 03:38:44 +000059
Lev Walkin188ed2c2004-09-13 08:31:01 +000060enum tvm_compat {
61 _TVM_SAME = 0, /* tags and all_tags are same */
62 _TVM_SUBSET = 1, /* tags are subset of all_tags */
63 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
64};
65static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
66
Lev Walkin59964be2004-08-25 02:03:12 +000067enum etd_spec {
68 ETD_NO_SPECIFICS,
69 ETD_HAS_SPECIFICS
70};
Lev Walkin8de2ab22004-09-26 13:11:31 +000071static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec);
Lev Walkin59964be2004-08-25 02:03:12 +000072
Lev Walkin59004fa2004-08-20 13:37:01 +000073#define C99_MODE (!(arg->flags & A1C_NO_C99))
Lev Walkinf15320b2004-06-03 03:38:44 +000074#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
Lev Walkindd32b592004-09-06 08:07:29 +000075#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
Lev Walkinf15320b2004-06-03 03:38:44 +000076
77#define PCTX_DEF INDENTED( \
78 OUT("\n"); \
79 OUT("/* Context for parsing across buffer boundaries */\n"); \
Lev Walkin05363a72004-09-29 13:16:40 +000080 OUT("asn_struct_ctx_t _asn_ctx;\n"));
Lev Walkinf15320b2004-06-03 03:38:44 +000081
Lev Walkinc8285712005-03-04 22:18:20 +000082
Lev Walkin3dcaafa2004-08-11 05:21:32 +000083#define DEPENDENCIES do { \
Lev Walkinc8285712005-03-04 22:18:20 +000084 emit_include_dependencies(arg); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000085 if(expr->expr_type == ASN_CONSTR_SET_OF) \
Lev Walkin22b5ed42006-09-13 02:51:20 +000086 GEN_INCLUDE_STD("asn_SET_OF"); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000087 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
Lev Walkin22b5ed42006-09-13 02:51:20 +000088 GEN_INCLUDE_STD("asn_SEQUENCE_OF"); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000089} while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000090
Lev Walkincaf0d5a2005-03-04 23:48:19 +000091/* MKID_safe() without checking for reserved keywords */
Lev Walkina00d6b32006-03-21 03:40:38 +000092#define MKID(expr) (asn1c_make_identifier(0, expr, 0))
93#define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0))
Lev Walkinf15320b2004-06-03 03:38:44 +000094
95int
Lev Walkinc78cbfb2004-09-14 12:47:45 +000096asn1c_lang_C_type_REAL(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +000097 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
98}
99
Lev Walkine0b56e02005-02-25 12:10:27 +0000100struct value2enum {
101 asn1c_integer_t value;
102 const char *name;
103 int idx;
104};
105static int compar_enumMap_byName(const void *ap, const void *bp) {
106 const struct value2enum *a = (const struct value2enum *)ap;
107 const struct value2enum *b = (const struct value2enum *)bp;
108 return strcmp(a->name, b->name);
109}
110static int compar_enumMap_byValue(const void *ap, const void *bp) {
111 const struct value2enum *a = (const struct value2enum *)ap;
112 const struct value2enum *b = (const struct value2enum *)bp;
113 if(a->value < b->value)
114 return -1;
115 else if(a->value == b->value)
116 return 0;
117 return 1;
118}
119
Lev Walkinf15320b2004-06-03 03:38:44 +0000120int
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000121asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 asn1p_expr_t *expr = arg->expr;
123 asn1p_expr_t *v;
Lev Walkine0b56e02005-02-25 12:10:27 +0000124 int el_count = expr_elements_count(arg, expr);
125 struct value2enum *v2e;
Lev Walkin59b176e2005-11-26 11:25:14 +0000126 int map_extensions = (expr->expr_type == ASN_BASIC_INTEGER);
Lev Walkin414b0782005-08-13 23:30:24 +0000127 int eidx;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800128 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000129
Lev Walkine0b56e02005-02-25 12:10:27 +0000130 v2e = alloca((el_count + 1) * sizeof(*v2e));
Lev Walkinf15320b2004-06-03 03:38:44 +0000131
Lev Walkine0b56e02005-02-25 12:10:27 +0000132 /*
Lev Walkin414b0782005-08-13 23:30:24 +0000133 * For all ENUMERATED types and for those INTEGER types which
134 * have identifiers, print out an enumeration table.
Lev Walkine0b56e02005-02-25 12:10:27 +0000135 */
Lev Walkin414b0782005-08-13 23:30:24 +0000136 if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) {
137 eidx = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000138 REDIR(OT_DEPS);
Lev Walkin21d00002005-03-04 08:48:53 +0000139 OUT("typedef enum ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000140 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000141 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 TQ_FOR(v, &(expr->members), next) {
143 switch(v->expr_type) {
144 case A1TC_UNIVERVAL:
Lev Walkin21d00002005-03-04 08:48:53 +0000145 OUT("\t");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000146 out_name_chain(arg, ONC_noflags);
Lev Walkina00d6b32006-03-21 03:40:38 +0000147 OUT("_%s", MKID(v));
Lev Walkinda997b12017-08-04 01:38:41 -0700148 OUT("\t= %s%s\n",
149 asn1p_itoa(v->value->value.v_integer),
Lev Walkin21d00002005-03-04 08:48:53 +0000150 (eidx+1 < el_count) ? "," : "");
Lev Walkine0b56e02005-02-25 12:10:27 +0000151 v2e[eidx].name = v->Identifier;
152 v2e[eidx].value = v->value->value.v_integer;
153 eidx++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000155 case A1TC_EXTENSIBLE:
156 OUT("\t/*\n");
157 OUT("\t * Enumeration is extensible\n");
158 OUT("\t */\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000159 if(!map_extensions)
160 map_extensions = eidx + 1;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000161 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 default:
163 return -1;
164 }
165 }
Lev Walkin171487e2006-03-21 07:25:18 +0000166 OUT("} e_");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000167 out_name_chain(arg, ONC_noflags);
Lev Walkin171487e2006-03-21 07:25:18 +0000168 OUT(";\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000169 assert(eidx == el_count);
Lev Walkin414b0782005-08-13 23:30:24 +0000170 }
171
172 /*
173 * For all ENUMERATED types print out a mapping table
174 * between identifiers and associated values.
175 * This is prohibited for INTEGER types by by X.693:8.3.4.
176 */
177 if(expr->expr_type == ASN_BASIC_ENUMERATED) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000178
179 /*
180 * Generate a enumerationName<->value map for XER codec.
181 */
182 REDIR(OT_STAT_DEFS);
183
Wim Lewisfb6344e2014-07-28 12:16:01 -0700184 OUT("static const asn_INTEGER_enum_map_t asn_MAP_%s_value2enum_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000185 MKID(expr), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000186 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
187 for(eidx = 0; eidx < el_count; eidx++) {
188 v2e[eidx].idx = eidx;
Lev Walkinda997b12017-08-04 01:38:41 -0700189 OUT("\t{ %s,\t%ld,\t\"%s\" }%s\n",
190 asn1p_itoa(v2e[eidx].value),
Lev Walkine0b56e02005-02-25 12:10:27 +0000191 (long)strlen(v2e[eidx].name), v2e[eidx].name,
192 (eidx + 1 < el_count) ? "," : "");
193 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000194 if(map_extensions)
Lev Walkine0b56e02005-02-25 12:10:27 +0000195 OUT("\t/* This list is extensible */\n");
196 OUT("};\n");
197
Wim Lewisfb6344e2014-07-28 12:16:01 -0700198 OUT("static const unsigned int asn_MAP_%s_enum2value_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000199 MKID(expr), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000200 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
201 for(eidx = 0; eidx < el_count; eidx++) {
Lev Walkinda997b12017-08-04 01:38:41 -0700202 OUT("\t%d%s\t/* %s(%s) */\n",
Lev Walkine0b56e02005-02-25 12:10:27 +0000203 v2e[eidx].idx,
204 (eidx + 1 < el_count) ? "," : "",
Lev Walkinda997b12017-08-04 01:38:41 -0700205 v2e[eidx].name, asn1p_itoa(v2e[eidx].value));
Lev Walkine0b56e02005-02-25 12:10:27 +0000206 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000207 if(map_extensions)
Lev Walkine0b56e02005-02-25 12:10:27 +0000208 OUT("\t/* This list is extensible */\n");
209 OUT("};\n");
210
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800211 if(!(expr->_type_referenced)) OUT("static ");
212 OUT("const asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000213 MKID(expr), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000214 INDENT(+1);
Lev Walkin59b176e2005-11-26 11:25:14 +0000215 OUT("asn_MAP_%s_value2enum_%d,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000216 "/* \"tag\" => N; sorted by tag */\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000217 MKID(expr),
Lev Walkin21d00002005-03-04 08:48:53 +0000218 expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +0000219 OUT("asn_MAP_%s_enum2value_%d,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000220 "/* N => \"tag\"; sorted by N */\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000221 MKID(expr),
Lev Walkin21d00002005-03-04 08:48:53 +0000222 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000223 OUT("%d,\t/* Number of elements in the maps */\n",
224 el_count);
Lev Walkin59b176e2005-11-26 11:25:14 +0000225 if(map_extensions) {
226 OUT("%d,\t/* Extensions before this member */\n",
227 map_extensions);
228 } else {
229 OUT("0,\t/* Enumeration is not extensible */\n");
230 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000231 if(expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8bb57a22007-12-03 13:41:36 +0000232 OUT("1,\t/* Strict enumeration */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000233 else
Lev Walkin8bb57a22007-12-03 13:41:36 +0000234 OUT("0,\n");
235 OUT("0,\t/* Native long size */\n");
236 OUT("0\n");
237 INDENT(-1);
238 OUT("};\n");
239 }
240
241 if(expr->expr_type == ASN_BASIC_INTEGER
242 && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) {
243 REDIR(OT_STAT_DEFS);
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800244 if(!(expr->_type_referenced)) OUT("static ");
245 OUT("const asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000246 MKID(expr), expr->_type_unique_index);
247 INDENT(+1);
248 OUT("0,\t");
249 OUT("0,\t");
250 OUT("0,\t");
251 OUT("0,\t");
252 OUT("0,\n");
253 OUT("0,\t/* Native long size */\n");
254 OUT("1\t/* Unsigned representation */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000255 INDENT(-1);
256 OUT("};\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 }
258
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800259 REDIR(saved_target);
260
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
262}
263
264int
Lev Walkinb02a8832005-08-13 23:51:47 +0000265asn1c_lang_C_type_BIT_STRING(arg_t *arg) {
266 asn1p_expr_t *expr = arg->expr;
267 asn1p_expr_t *v;
268 int el_count = expr_elements_count(arg, expr);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800269 int saved_target = arg->target->target;
Lev Walkinb02a8832005-08-13 23:51:47 +0000270
271 if(el_count) {
Lev Walkin082cadc2005-08-14 02:18:27 +0000272 int eidx = 0;
Lev Walkinb02a8832005-08-13 23:51:47 +0000273 REDIR(OT_DEPS);
274 OUT("typedef enum ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000275 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkinb02a8832005-08-13 23:51:47 +0000276 OUT(" {\n");
277 TQ_FOR(v, &(expr->members), next) {
Lev Walkin082cadc2005-08-14 02:18:27 +0000278 if(v->expr_type != A1TC_UNIVERVAL) {
Lev Walkinb02a8832005-08-13 23:51:47 +0000279 OUT("/* Unexpected BIT STRING element: %s */\n",
280 v->Identifier);
Lev Walkin082cadc2005-08-14 02:18:27 +0000281 continue;
Lev Walkinb02a8832005-08-13 23:51:47 +0000282 }
Lev Walkin0f8d4a42006-08-26 03:29:15 +0000283 eidx++;
Lev Walkin082cadc2005-08-14 02:18:27 +0000284 OUT("\t");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000285 out_name_chain(arg, ONC_noflags);
Lev Walkina00d6b32006-03-21 03:40:38 +0000286 OUT("_%s", MKID(v));
Lev Walkinda997b12017-08-04 01:38:41 -0700287 OUT("\t= %s%s\n",
288 asn1p_itoa(v->value->value.v_integer),
Lev Walkin082cadc2005-08-14 02:18:27 +0000289 (eidx < el_count) ? "," : "");
Lev Walkinb02a8832005-08-13 23:51:47 +0000290 }
Lev Walkin171487e2006-03-21 07:25:18 +0000291 OUT("} e_");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000292 out_name_chain(arg, ONC_noflags);
Lev Walkin171487e2006-03-21 07:25:18 +0000293 OUT(";\n");
Lev Walkinb02a8832005-08-13 23:51:47 +0000294 assert(eidx == el_count);
295 }
296
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800297 REDIR(saved_target);
298
Lev Walkinb02a8832005-08-13 23:51:47 +0000299 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
300}
301
302int
Lev Walkinf15320b2004-06-03 03:38:44 +0000303asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
304 asn1p_expr_t *expr = arg->expr;
305 asn1p_expr_t *v;
306 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800307 int saved_target = arg->target->target;
Lev Walkinad0d6372017-08-08 02:02:42 -0700308 asn1c_ioc_table_and_objset_t ioc_tao;
Lev Walkinf15320b2004-06-03 03:38:44 +0000309
310 DEPENDENCIES;
311
Lev Walkinad0d6372017-08-08 02:02:42 -0700312 ioc_tao = asn1c_get_ioc_table(arg);
313 if(ioc_tao.ioct) {
314 if(emit_ioc_table(arg, expr, ioc_tao)) {
315 return -1;
316 }
317 } else if(ioc_tao.fatal_error) {
Lev Walkinea6635b2017-08-06 23:23:04 -0700318 return -1;
319 }
320
Lev Walkinf15320b2004-06-03 03:38:44 +0000321 if(arg->embed) {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800322
323 /* Use _anonymous_type field to indicate it's called from
324 * asn1c_lang_C_type_SEx_OF() */
325 if (expr->_anonymous_type) {
326 REDIR(OT_FWD_DEFS);
327 OUT("typedef ");
328 }
Lev Walkin21d00002005-03-04 08:48:53 +0000329 OUT("struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000330 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000331 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000332 } else {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800333 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 OUT("typedef struct %s {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000335 MKID_safe(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 }
337
338 TQ_FOR(v, &(expr->members), next) {
Lev Walkinda9a3b82005-08-16 17:00:21 +0000339 if(v->expr_type == A1TC_EXTENSIBLE)
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 if(comp_mode < 3) comp_mode++;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000341 if(comp_mode == 1)
Lev Walkin59b176e2005-11-26 11:25:14 +0000342 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
343 try_inline_default(arg, v, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000344 EMBED(v);
345 }
346
347 PCTX_DEF;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800348
349 if (arg->embed && expr->_anonymous_type) {
350 OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":"");
351 out_name_chain(arg, ONC_avoid_keywords);
352 OUT("%s;\n", arg->embed ? "" : "_t");
353
354 REDIR(saved_target);
355
356 OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":"");
357 out_name_chain(arg, ONC_avoid_keywords);
358 } else {
359 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000360 expr->_anonymous_type ? "" :
361 arg->embed
Lev Walkina00d6b32006-03-21 03:40:38 +0000362 ? MKID_safe(expr)
363 : MKID(expr),
Lev Walkin08079b02004-08-22 03:25:24 +0000364 arg->embed ? "" : "_t");
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800365 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000366
367 return asn1c_lang_C_type_SEQUENCE_def(arg);
368}
369
370static int
371asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
372 asn1p_expr_t *expr = arg->expr;
373 asn1p_expr_t *v;
374 int elements; /* Number of elements */
Lev Walkin33af5c32007-06-23 20:05:49 +0000375 int ext_start = -2;
376 int ext_stop = -2;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000377 tag2el_t *tag2el = NULL;
378 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000379 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000380 int all_tags_count;
381 enum tvm_compat tv_mode;
Lev Walkin59b176e2005-11-26 11:25:14 +0000382 int roms_count; /* Root optional members */
383 int aoms_count; /* Additions optional members */
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800384 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000385
Lev Walkinfd171ef2004-06-06 07:20:17 +0000386 /*
387 * Fetch every inner tag from the tag to elements map.
388 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000389 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000390 if(tag2el) free(tag2el);
391 return -1;
392 }
393
Lev Walkin22b5ed42006-09-13 02:51:20 +0000394 GEN_INCLUDE_STD("constr_SEQUENCE");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000395 if(!arg->embed)
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800396 GEN_DECLARE("SEQUENCE", expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000397
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000398 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000399
400 /*
Lev Walkin59b176e2005-11-26 11:25:14 +0000401 * Print out the table according to which parsing is performed.
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000403 if(expr_elements_count(arg, expr)) {
404 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000405
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800406 if(!(expr->_type_referenced)) OUT("static ");
407 OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000408 MKID(expr), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000409
410 elements = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000411 roms_count = 0;
412 aoms_count = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000413 INDENTED(TQ_FOR(v, &(expr->members), next) {
414 if(v->expr_type == A1TC_EXTENSIBLE) {
415 if((++comp_mode) == 1)
416 ext_start = elements - 1;
417 else
418 ext_stop = elements - 1;
419 continue;
420 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000421 if(v->marker.flags & EM_OMITABLE)
422 comp_mode == 1 ? ++aoms_count : ++roms_count;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000423 emit_member_table(arg, v);
Lev Walkinda9a3b82005-08-16 17:00:21 +0000424 elements++;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000425 });
426 OUT("};\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000427
Lev Walkin3c5d9412017-07-24 01:49:24 +0400428 if((roms_count + aoms_count) && (arg->flags & (A1C_GEN_OER|A1C_GEN_PER))) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000429 int elm = 0;
430 int comma = 0;
431 comp_mode = 0;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700432 OUT("static const int asn_MAP_%s_oms_%d[] = {",
Lev Walkina00d6b32006-03-21 03:40:38 +0000433 MKID(expr),
Lev Walkin59b176e2005-11-26 11:25:14 +0000434 expr->_type_unique_index);
435 TQ_FOR(v, &(expr->members), next) {
436 if(v->expr_type == A1TC_EXTENSIBLE) {
437 ++comp_mode;
438 continue;
439 }
440 if((v->marker.flags & EM_OMITABLE)
441 && comp_mode != 1) {
442 if(!comma) comma++;
443 else OUT(",");
444 OUT(" %d", elm);
445 }
446 ++elm;
447 }
448 elm = 0;
449 comp_mode = 0;
450 TQ_FOR(v, &(expr->members), next) {
451 if(v->expr_type == A1TC_EXTENSIBLE) {
452 ++comp_mode;
453 continue;
454 }
455 if((v->marker.flags & EM_OMITABLE)
456 && comp_mode == 1) {
457 if(!comma) comma++;
458 else OUT(",");
459 OUT(" %d", elm);
460 }
461 ++elm;
462 }
463 OUT(" };\n");
Lev Walkinea6635b2017-08-06 23:23:04 -0700464 if(roms_count > 65536) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000465 FATAL("Too many optional elements in %s "
466 "at line %d!",
467 arg->expr->Identifier,
468 arg->expr->_lineno);
Lev Walkinea6635b2017-08-06 23:23:04 -0700469 return -1;
470 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000471 } else {
472 roms_count = 0;
473 aoms_count = 0;
474 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000475 } else {
476 elements = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000477 roms_count = 0;
478 aoms_count = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000479 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000480
Lev Walkin27ea3802004-06-28 21:13:46 +0000481 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000482 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000483 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000484 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000485
Lev Walkinfd171ef2004-06-06 07:20:17 +0000486 /*
487 * Tags to elements map.
488 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000489 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000490
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800491 if(!(expr->_type_referenced)) OUT("static ");
492 OUT("asn_SEQUENCE_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000493 MKID(expr), expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +0000494 INDENT(+1);
495 OUT("sizeof(struct ");
496 out_name_chain(arg, ONC_avoid_keywords); OUT("),\n");
497 OUT("offsetof(struct ");
498 out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n");
Lev Walkin21d00002005-03-04 08:48:53 +0000499
Lev Walkin59b176e2005-11-26 11:25:14 +0000500 if(tag2el_count) {
501 OUT("asn_MAP_%s_tag2el_%d,\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000502 MKID(expr),
Lev Walkin59b176e2005-11-26 11:25:14 +0000503 expr->_type_unique_index);
504 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
505 } else {
506 OUT("0,\t/* No top level tags */\n");
507 OUT("0,\t/* No tags in the map */\n");
508 }
509 if(roms_count + aoms_count) {
510 OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000511 MKID(expr), expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +0000512 OUT("%d, %d,\t/* Root/Additions */\n", roms_count, aoms_count);
513 } else {
514 OUT("0, 0, 0,\t/* Optional elements (not needed) */\n");
515 }
516 OUT("%d,\t/* Start extensions */\n",
Lev Walkin33af5c32007-06-23 20:05:49 +0000517 ext_start<0 ? -1 : ext_start);
Lev Walkin59b176e2005-11-26 11:25:14 +0000518 OUT("%d\t/* Stop extensions */\n",
Lev Walkin33af5c32007-06-23 20:05:49 +0000519 (ext_stop<ext_start)?elements+1:(ext_stop<0?-1:ext_stop));
Lev Walkin59b176e2005-11-26 11:25:14 +0000520 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000522
523 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000524 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000525 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000526 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000527 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800529 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +0000530
Bi-Ruei, Chiu12021b52016-11-04 11:59:45 +0800531 if(tag2el) free(tag2el);
532
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000534} /* _SEQUENCE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000535
536int
Lev Walkinf15320b2004-06-03 03:38:44 +0000537asn1c_lang_C_type_SET(arg_t *arg) {
538 asn1p_expr_t *expr = arg->expr;
539 asn1p_expr_t *v;
540 long mcount;
541 char *id;
542 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800543 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000544
545 DEPENDENCIES;
546
547 REDIR(OT_DEPS);
548
549 OUT("\n");
550 OUT("/*\n");
551 OUT(" * Method of determining the components presence\n");
552 OUT(" */\n");
553 mcount = 0;
Lev Walkin21d00002005-03-04 08:48:53 +0000554 OUT("typedef enum ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000555 out_name_chain(arg, ONC_noflags);
Lev Walkin21d00002005-03-04 08:48:53 +0000556 OUT("_PR {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 TQ_FOR(v, &(expr->members), next) {
558 if(v->expr_type == A1TC_EXTENSIBLE) continue;
559 INDENTED(
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000560 out_name_chain(arg, ONC_noflags);
Lev Walkin801fabc2005-01-28 12:18:50 +0000561 OUT("_PR_");
Lev Walkina00d6b32006-03-21 03:40:38 +0000562 id = MKID(v);
Lev Walkinf15320b2004-06-03 03:38:44 +0000563 OUT("%s,\t/* Member %s is present */\n",
564 id, id)
565 );
566 mcount++;
567 }
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000568 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000569
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800570 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
572 if(arg->embed) {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800573 if (expr->_anonymous_type) {
574 REDIR(OT_FWD_DEFS);
575 OUT("typedef ");
576 }
Lev Walkin21d00002005-03-04 08:48:53 +0000577 OUT("struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000578 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000579 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 } else {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800581 REDIR(OT_TYPE_DECLS);
Lev Walkin59b176e2005-11-26 11:25:14 +0000582 OUT("typedef struct %s {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000583 MKID_safe(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000584 }
585
586 TQ_FOR(v, &(expr->members), next) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000587 if(v->expr_type == A1TC_EXTENSIBLE)
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 if(comp_mode < 3) comp_mode++;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000589 if(comp_mode == 1)
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
591 try_inline_default(arg, v, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000592 EMBED(v);
593 }
594
595 INDENTED(
Lev Walkina00d6b32006-03-21 03:40:38 +0000596 id = MKID(expr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000597 OUT("\n");
598 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
599 id, id);
600 OUT("unsigned int _presence_map\n");
601 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
602 );
603
604 PCTX_DEF;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800605
606 if (arg->embed && expr->_anonymous_type) {
607 OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":"");
608 out_name_chain(arg, ONC_avoid_keywords);
609 OUT("%s;\n", arg->embed ? "" : "_t");
610
611 REDIR(saved_target);
612
613 OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":"");
614 out_name_chain(arg, ONC_avoid_keywords);
615 } else {
616 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
617 expr->_anonymous_type ? "" : MKID_safe(expr),
618 arg->embed ? "" : "_t");
619 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000620
621 return asn1c_lang_C_type_SET_def(arg);
622}
623
Lev Walkinf15320b2004-06-03 03:38:44 +0000624static int
625asn1c_lang_C_type_SET_def(arg_t *arg) {
626 asn1p_expr_t *expr = arg->expr;
627 asn1p_expr_t *v;
628 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000629 tag2el_t *tag2el = NULL;
630 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000631 tag2el_t *tag2el_cxer = NULL;
632 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000633 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000634 int all_tags_count;
635 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000636 char *p;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800637 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000638
639 /*
640 * Fetch every inner tag from the tag to elements map.
641 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000642 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 if(tag2el) free(tag2el);
644 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000646 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
647 if(tag2el) free(tag2el);
648 if(tag2el_cxer) free(tag2el_cxer);
649 return -1;
650 }
651 if(tag2el_cxer_count == tag2el_count
652 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
653 free(tag2el_cxer);
654 tag2el_cxer = 0;
655 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000656
Lev Walkin22b5ed42006-09-13 02:51:20 +0000657 GEN_INCLUDE_STD("constr_SET");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000658 if(!arg->embed)
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800659 GEN_DECLARE("SET", expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000660
661 REDIR(OT_STAT_DEFS);
662
Lev Walkinf15320b2004-06-03 03:38:44 +0000663 /*
Lev Walkin59b176e2005-11-26 11:25:14 +0000664 * Print out the table according to which parsing is performed.
Lev Walkinf15320b2004-06-03 03:38:44 +0000665 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000666 if(expr_elements_count(arg, expr)) {
667 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000668
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800669 if(!(expr->_type_referenced)) OUT("static ");
670 OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000671 MKID(expr), expr->_type_unique_index);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800672
Lev Walkina9cc46e2004-09-22 16:06:28 +0000673 elements = 0;
674 INDENTED(TQ_FOR(v, &(expr->members), next) {
675 if(v->expr_type == A1TC_EXTENSIBLE) {
676 if(comp_mode < 3) comp_mode++;
677 } else {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000678 emit_member_table(arg, v);
Lev Walkinda9a3b82005-08-16 17:00:21 +0000679 elements++;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000680 }
681 });
682 OUT("};\n");
683 } else {
684 elements = 0;
685 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000686
Lev Walkin27ea3802004-06-28 21:13:46 +0000687 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000688 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000689 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000690 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000691
692 /*
693 * Tags to elements map.
694 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000695 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
696 if(tag2el_cxer)
697 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000698
699 /*
700 * Emit a map of mandatory elements.
701 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700702 OUT("static const uint8_t asn_MAP_%s_mmap_%d",
Lev Walkina00d6b32006-03-21 03:40:38 +0000703 MKID(expr), expr->_type_unique_index);
704 p = MKID_safe(expr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000705 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000706 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000707 INDENTED(
708 if(elements) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000709 int el = 0;
710 TQ_FOR(v, &(expr->members), next) {
711 if(v->expr_type == A1TC_EXTENSIBLE) continue;
Lev Walkinac760bf2005-10-05 08:59:03 +0000712 if(el) {
713 if((el % 8) == 0)
714 OUT(",\n");
715 else
716 OUT(" | ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000717 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000718 OUT("(%d << %d)",
Lev Walkinb85a8132005-08-18 13:38:19 +0000719 (v->marker.flags & EM_OMITABLE) != EM_OMITABLE,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000720 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000721 el++;
722 }
723 } else {
724 OUT("0");
725 }
726 );
727 OUT("\n");
728 OUT("};\n");
729
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800730 if(!(expr->_type_referenced)) OUT("static \n");
731 OUT("asn_SET_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000732 MKID(expr), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000733 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000734 OUT("sizeof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000735 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000736 OUT("),\n");
737 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000738 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000739 OUT(", _asn_ctx),\n");
740 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000741 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000742 OUT(", _presence_map),\n");
Lev Walkina00d6b32006-03-21 03:40:38 +0000743 p = MKID(expr);
Lev Walkin59b176e2005-11-26 11:25:14 +0000744 OUT("asn_MAP_%s_tag2el_%d,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000745 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000746 if(tag2el_cxer)
Lev Walkin59b176e2005-11-26 11:25:14 +0000747 OUT("asn_MAP_%s_tag2el_cxer_%d,\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000748 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000749 else
Lev Walkin59b176e2005-11-26 11:25:14 +0000750 OUT("asn_MAP_%s_tag2el_%d,\t/* Same as above */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000751 p, expr->_type_unique_index);
752 OUT("%d,\t/* Count of tags in the CXER map */\n",
753 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000754 OUT("%d,\t/* Whether extensible */\n",
Lev Walkin59b176e2005-11-26 11:25:14 +0000755 compute_extensions_start(expr) == -1 ? 0 : 1);
Lev Walkin46ef3f32017-07-06 08:47:53 -0700756 OUT("(const unsigned int *)asn_MAP_%s_mmap_%d\t/* Mandatory elements map */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000757 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000758 );
759 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000760
761 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000762 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000763 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000764 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000765 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000766
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800767 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +0000768
Bi-Ruei, Chiu12021b52016-11-04 11:59:45 +0800769 if (tag2el) free(tag2el);
770 if (tag2el_cxer) free(tag2el_cxer);
771
Lev Walkinf15320b2004-06-03 03:38:44 +0000772 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000773} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000774
775int
Lev Walkin08079b02004-08-22 03:25:24 +0000776asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000777 asn1p_expr_t *expr = arg->expr;
Lev Walkinc8285712005-03-04 22:18:20 +0000778 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800779 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000780
781 DEPENDENCIES;
782
783 if(arg->embed) {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800784 if (expr->_anonymous_type) {
785 REDIR(OT_FWD_DEFS);
786 OUT("typedef ");
787 }
Lev Walkin21d00002005-03-04 08:48:53 +0000788 OUT("struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000789 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000790 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000791 } else {
Lev Walkina00d6b32006-03-21 03:40:38 +0000792 OUT("typedef struct %s {\n", MKID_safe(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000793 }
794
Lev Walkin08079b02004-08-22 03:25:24 +0000795 INDENT(+1);
796 OUT("A_%s_OF(",
797 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
798 ? "SET" : "SEQUENCE");
Lev Walkinda9a3b82005-08-16 17:00:21 +0000799
800 /*
801 * README README
802 * The implementation of the A_SET_OF() macro is already indirect.
803 */
804 memb->marker.flags |= EM_INDIRECT;
805
Lev Walkine0b56e02005-02-25 12:10:27 +0000806 if(memb->expr_type & ASN_CONSTR_MASK
807 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000808 || (0 /* -- prohibited by X.693:8.3.4 */
809 && memb->expr_type == ASN_BASIC_INTEGER))
810 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000811 arg_t tmp;
812 asn1p_expr_t tmp_memb;
813 arg->embed++;
814 tmp = *arg;
815 tmp.expr = &tmp_memb;
816 tmp_memb = *memb;
Lev Walkinda9a3b82005-08-16 17:00:21 +0000817 tmp_memb.marker.flags &= ~EM_INDIRECT;
Lev Walkin08079b02004-08-22 03:25:24 +0000818 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000819 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000820 tmp_memb.Identifier = "Member";
821 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000822 tmp_memb.Identifier = strdup(
823 asn1c_make_identifier(0,
Lev Walkina00d6b32006-03-21 03:40:38 +0000824 expr, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000825 assert(tmp_memb.Identifier);
826 }
Lev Walkin08079b02004-08-22 03:25:24 +0000827 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000828 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000829 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000830 arg->embed--;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800831 assert(arg->target->target == OT_TYPE_DECLS ||
832 arg->target->target == OT_FWD_DEFS);
Lev Walkin08079b02004-08-22 03:25:24 +0000833 } else {
Lev Walkinfe7f6ec2005-03-04 22:38:22 +0000834 OUT("%s", asn1c_type_name(arg, memb,
835 (memb->marker.flags & EM_UNRECURSE)
836 ? TNF_RSAFE : TNF_CTYPE));
Lev Walkinf15320b2004-06-03 03:38:44 +0000837 }
Lev Walkinda9a3b82005-08-16 17:00:21 +0000838 /* README README (above) */
839 if(0 && (memb->marker.flags & EM_INDIRECT))
840 OUT(" *");
Lev Walkin08079b02004-08-22 03:25:24 +0000841 OUT(") list;\n");
842 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000843
844 PCTX_DEF;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800845
846 if (arg->embed && expr->_anonymous_type) {
847 OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":"");
848 out_name_chain(arg, ONC_avoid_keywords);
849 OUT("%s;\n", arg->embed ? "" : "_t");
850
851 REDIR(saved_target);
852
853 OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":"");
854 out_name_chain(arg, ONC_avoid_keywords);
855 } else {
856 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
857 expr->_anonymous_type ? "" : MKID_safe(expr),
858 arg->embed ? "" : "_t");
859 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000860
861 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000862 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000863 */
Lev Walkin08079b02004-08-22 03:25:24 +0000864 return asn1c_lang_C_type_SEx_OF_def(arg,
865 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000866}
867
868static int
869asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
870 asn1p_expr_t *expr = arg->expr;
871 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000872 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000873 int all_tags_count;
874 enum tvm_compat tv_mode;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800875 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000876
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000877 /*
Lev Walkin59b176e2005-11-26 11:25:14 +0000878 * Print out the table according to which parsing is performed.
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000879 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000880 if(seq_of) {
Lev Walkin22b5ed42006-09-13 02:51:20 +0000881 GEN_INCLUDE_STD("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000882 } else {
Lev Walkin22b5ed42006-09-13 02:51:20 +0000883 GEN_INCLUDE_STD("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000884 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000885 if(!arg->embed)
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800886 GEN_DECLARE("SET_OF", expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000887
888 REDIR(OT_STAT_DEFS);
889
890 /*
Lev Walkin59b176e2005-11-26 11:25:14 +0000891 * Print out the table according to which parsing is performed.
Lev Walkinf15320b2004-06-03 03:38:44 +0000892 */
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800893 if(!(expr->_type_referenced)) OUT("static ");
894 OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000895 MKID(expr), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000896 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000897 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000898 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000899 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000900 assert(v->Identifier);
901 }
902 v->_anonymous_type = 1;
903 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000904 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000905 arg->embed--;
Bi-Ruei, Chiu9b780832016-11-07 22:19:10 +0800906 free(v->Identifier);
907 v->Identifier = (char *)NULL;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000908 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000909 OUT("};\n");
910
Lev Walkin27ea3802004-06-28 21:13:46 +0000911 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000912 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000913 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000914 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000915
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +0800916 if(!(expr->_type_referenced)) OUT("static ");
917 OUT("asn_SET_OF_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000918 MKID(expr), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000919 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000920 OUT("sizeof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000921 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000922 OUT("),\n");
923 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000924 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +0000925 OUT(", _asn_ctx),\n");
Lev Walkind1bfea62005-11-08 03:06:16 +0000926 {
927 int as_xvl = expr_as_xmlvaluelist(arg, v);
928 OUT("%d,\t/* XER encoding is %s */\n",
929 as_xvl,
930 as_xvl ? "XMLValueList" : "XMLDelimitedItemList");
931 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000932 );
933 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000934
935 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000936 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000937 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000938 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000939 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000940
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800941 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +0000942
943 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000944} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000945
946int
947asn1c_lang_C_type_CHOICE(arg_t *arg) {
948 asn1p_expr_t *expr = arg->expr;
949 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000950 char *id;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800951 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +0000952
953 DEPENDENCIES;
954
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000955 REDIR(OT_DEPS);
956
Lev Walkin21d00002005-03-04 08:48:53 +0000957 OUT("typedef enum ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000958 out_name_chain(arg, ONC_noflags);
Lev Walkin21d00002005-03-04 08:48:53 +0000959 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000960 INDENTED(
Lev Walkin11380fb2006-08-18 01:38:40 +0000961 int skipComma = 1;
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000962 out_name_chain(arg, ONC_noflags);
Lev Walkin801fabc2005-01-28 12:18:50 +0000963 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000964 TQ_FOR(v, &(expr->members), next) {
Lev Walkin11380fb2006-08-18 01:38:40 +0000965 if(skipComma) skipComma = 0;
johvik792216e2017-04-11 10:42:21 +0200966 else if (v->expr_type == A1TC_EXTENSIBLE && !TQ_NEXT(v, next)) OUT("\n");
Lev Walkin1dc85292006-08-18 01:32:26 +0000967 else OUT(",\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000968 if(v->expr_type == A1TC_EXTENSIBLE) {
969 OUT("/* Extensions may appear below */\n");
Lev Walkin11380fb2006-08-18 01:38:40 +0000970 skipComma = 1;
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000971 continue;
972 }
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000973 out_name_chain(arg, ONC_noflags);
Lev Walkina00d6b32006-03-21 03:40:38 +0000974 id = MKID(v);
Lev Walkin1dc85292006-08-18 01:32:26 +0000975 OUT("_PR_%s", id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000976 }
Lev Walkin1dc85292006-08-18 01:32:26 +0000977 OUT("\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000978 );
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000979 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000980
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800981 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +0000982
983 if(arg->embed) {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800984 if (expr->_anonymous_type) {
985 REDIR(OT_FWD_DEFS);
986 OUT("typedef ");
987 }
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000988 OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000989 } else {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800990 REDIR(OT_TYPE_DECLS);
Lev Walkina00d6b32006-03-21 03:40:38 +0000991 OUT("typedef struct %s {\n", MKID_safe(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000992 }
993
994 INDENTED(
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000995 out_name_chain(arg, ONC_noflags);
Lev Walkin801fabc2005-01-28 12:18:50 +0000996 OUT("_PR present;\n");
Lev Walkin0f5d74c2005-08-14 15:03:31 +0000997 OUT("union ");
998 if(UNNAMED_UNIONS == 0) {
999 out_name_chain(arg, ONC_force_compound_name);
1000 OUT("_u ");
1001 }
1002 OUT("{\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001003 TQ_FOR(v, &(expr->members), next) {
1004 EMBED(v);
1005 }
1006 if(UNNAMED_UNIONS) OUT("};\n");
1007 else OUT("} choice;\n");
1008 );
1009
1010 PCTX_DEF;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001011
1012 if (arg->embed && expr->_anonymous_type) {
1013 OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":"");
1014 out_name_chain(arg, ONC_avoid_keywords);
1015 OUT("%s;\n", arg->embed ? "" : "_t");
1016
1017 REDIR(saved_target);
1018
1019 OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":"");
1020 out_name_chain(arg, ONC_avoid_keywords);
1021 } else {
1022 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
1023 expr->_anonymous_type ? "" :
1024 arg->embed
1025 ? MKID_safe(expr)
1026 : MKID(expr),
1027 arg->embed ? "" : "_t");
1028 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001029
1030 return asn1c_lang_C_type_CHOICE_def(arg);
1031}
1032
1033static int
1034asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
1035 asn1p_expr_t *expr = arg->expr;
1036 asn1p_expr_t *v;
1037 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +00001038 tag2el_t *tag2el = NULL;
1039 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +00001040 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001041 int all_tags_count;
1042 enum tvm_compat tv_mode;
Lev Walkin59b176e2005-11-26 11:25:14 +00001043 int *cmap = 0;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001044 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +00001045
1046 /*
1047 * Fetch every inner tag from the tag to elements map.
1048 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001049 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001050 if(tag2el) free(tag2el);
1051 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001052 }
1053
Lev Walkin22b5ed42006-09-13 02:51:20 +00001054 GEN_INCLUDE_STD("constr_CHOICE");
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001055 if(!arg->embed)
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001056 GEN_DECLARE("CHOICE", expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +00001057
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001058 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +00001059
1060 /*
Lev Walkin59b176e2005-11-26 11:25:14 +00001061 * Print out the table according to which parsing is performed.
Lev Walkinf15320b2004-06-03 03:38:44 +00001062 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001063 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001064
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001065 if(!(expr->_type_referenced)) OUT("static ");
1066 OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001067 MKID(expr), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001068
1069 elements = 0;
1070 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +00001071 if(v->expr_type == A1TC_EXTENSIBLE)
1072 continue;
Lev Walkin659c63b2005-01-17 11:40:49 +00001073 emit_member_table(arg, v);
Lev Walkinda9a3b82005-08-16 17:00:21 +00001074 elements++;
Lev Walkina9cc46e2004-09-22 16:06:28 +00001075 });
1076 OUT("};\n");
1077 } else {
1078 elements = 0;
1079 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001080
Lev Walkin59b176e2005-11-26 11:25:14 +00001081 /* Create a canonical elements map */
1082 if(elements && (arg->flags & A1C_GEN_PER)) {
1083 int i;
1084 cmap = compute_canonical_members_order(arg, elements);
1085 if(cmap) {
Wim Lewisfb6344e2014-07-28 12:16:01 -07001086 OUT("static const int asn_MAP_%s_cmap_%d[] = {",
Lev Walkina00d6b32006-03-21 03:40:38 +00001087 MKID(expr),
Lev Walkin59b176e2005-11-26 11:25:14 +00001088 expr->_type_unique_index);
1089 for(i = 0; i < elements; i++) {
1090 if(i) OUT(",");
1091 OUT(" %d", cmap[i]);
1092 }
1093 OUT(" };\n");
1094 free(cmap);
1095 }
1096 }
Lev Walkin64399722004-08-11 07:17:22 +00001097
Lev Walkinf15320b2004-06-03 03:38:44 +00001098 if(arg->embed) {
1099 /*
1100 * Our parent structure has already taken this into account.
1101 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001102 tv_mode = _TVM_SAME;
1103 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001104 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001105 tv_mode = emit_tags_vectors(arg, expr,
1106 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +00001107 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001108
1109 /*
1110 * Tags to elements map.
1111 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001112 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +00001113
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001114 if(!(expr->_type_referenced)) OUT("static ");
1115 OUT("asn_CHOICE_specifics_t asn_SPC_%s_specs_%d = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001116 MKID(expr), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001117 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +00001118 OUT("sizeof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +00001119 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +00001120 OUT("),\n");
1121 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +00001122 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +00001123 OUT(", _asn_ctx),\n");
1124 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +00001125 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +00001126 OUT(", present),\n");
1127 OUT("sizeof(((struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +00001128 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +00001129 OUT(" *)0)->present),\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00001130 OUT("asn_MAP_%s_tag2el_%d,\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001131 MKID(expr), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001132 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin59b176e2005-11-26 11:25:14 +00001133 if(C99_MODE) OUT(".canonical_order = ");
1134 if(cmap) OUT("asn_MAP_%s_cmap_%d,\t/* Canonically sorted */\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001135 MKID(expr), expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00001136 else OUT("0,\n");
1137 if(C99_MODE) OUT(".ext_start = ");
1138 OUT("%d\t/* Extensions start */\n",
1139 compute_extensions_start(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +00001140 );
1141 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001142
1143 /*
Lev Walkin05363a72004-09-29 13:16:40 +00001144 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +00001145 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001146 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +00001147 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +00001148
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001149 REDIR(saved_target);
Lev Walkinf15320b2004-06-03 03:38:44 +00001150
Bi-Ruei, Chiu12021b52016-11-04 11:59:45 +08001151 if (tag2el) free(tag2el);
1152
Lev Walkinf15320b2004-06-03 03:38:44 +00001153 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +00001154} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +00001155
1156int
1157asn1c_lang_C_type_REFERENCE(arg_t *arg) {
1158 asn1p_ref_t *ref;
1159
1160 ref = arg->expr->reference;
1161 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +00001162 asn1p_expr_t *extract;
1163 arg_t tmp;
1164 int ret;
1165
Lev Walkina9cc46e2004-09-22 16:06:28 +00001166 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkina00d6b32006-03-21 03:40:38 +00001167 arg->expr, arg->expr->rhs_pspecs, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +00001168 if(extract == NULL)
1169 return -1;
1170
Lev Walkin08079b02004-08-22 03:25:24 +00001171 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +00001172 if(extract) {
Lev Walkind8b83642016-03-14 02:00:27 -07001173 free(extract->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +00001174 extract->Identifier = strdup(arg->expr->Identifier);
1175 if(extract->Identifier == NULL) {
1176 asn1p_expr_free(extract);
1177 return -1;
1178 }
1179 } else {
1180 return -1;
1181 }
1182
1183 tmp = *arg;
1184 tmp.asn = arg->asn;
Lev Walkinf15320b2004-06-03 03:38:44 +00001185 tmp.expr = extract;
1186
1187 ret = arg->default_cb(&tmp);
1188
1189 asn1p_expr_free(extract);
1190
1191 return ret;
1192 }
1193
1194
1195 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
1196}
1197
1198int
1199asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
1200 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +00001201 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001202 int all_tags_count;
1203 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +00001204 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +00001205 char *p;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001206 int saved_target = arg->target->target;
Lev Walkinf15320b2004-06-03 03:38:44 +00001207
1208 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +00001209 enum tnfmt tnfmt = TNF_CTYPE;
1210
1211 /*
1212 * If this is an optional compound type,
1213 * refer it using "struct X" convention,
1214 * as it may recursively include the current structure.
1215 */
Lev Walkinc8285712005-03-04 22:18:20 +00001216 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
Lev Walkinb85a8132005-08-18 13:38:19 +00001217 if(terminal_structable(arg, expr)) {
Lev Walkinc8285712005-03-04 22:18:20 +00001218 tnfmt = TNF_RSAFE;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001219 if(saved_target != OT_FWD_DECLS) {
1220 REDIR(OT_FWD_DECLS);
1221 OUT("%s;\n",
1222 asn1c_type_name(arg, arg->expr, tnfmt));
1223 }
1224 REDIR(saved_target);
Lev Walkin05363a72004-09-29 13:16:40 +00001225 }
1226 }
1227
Lev Walkinf15320b2004-06-03 03:38:44 +00001228
Lev Walkinc8285712005-03-04 22:18:20 +00001229 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkine0b56e02005-02-25 12:10:27 +00001230 if(!expr->_anonymous_type) {
Lev Walkinc8285712005-03-04 22:18:20 +00001231 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
Lev Walkina00d6b32006-03-21 03:40:38 +00001232 OUT("%s", MKID_safe(expr));
Lev Walkin59b176e2005-11-26 11:25:14 +00001233 if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT))
1234 == (EM_DEFAULT & ~EM_INDIRECT))
Lev Walkine0b56e02005-02-25 12:10:27 +00001235 OUT("\t/* DEFAULT %s */",
1236 asn1f_printable_value(
1237 expr->marker.default_value));
Lev Walkin59b176e2005-11-26 11:25:14 +00001238 else if((expr->marker.flags & EM_OPTIONAL)
1239 == EM_OPTIONAL)
Lev Walkine0b56e02005-02-25 12:10:27 +00001240 OUT("\t/* OPTIONAL */");
1241 }
1242
1243 } else {
1244 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
Lev Walkin59004fa2004-08-20 13:37:01 +00001245
1246 REDIR(OT_TYPE_DECLS);
Lev Walkine0b56e02005-02-25 12:10:27 +00001247
1248 OUT("typedef %s\t",
Lev Walkinc8285712005-03-04 22:18:20 +00001249 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
Lev Walkine0b56e02005-02-25 12:10:27 +00001250 OUT("%s%s_t",
Lev Walkinc8285712005-03-04 22:18:20 +00001251 (expr->marker.flags & EM_INDIRECT)?"*":" ",
Lev Walkina00d6b32006-03-21 03:40:38 +00001252 MKID(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +00001253 }
1254
Lev Walkine0b56e02005-02-25 12:10:27 +00001255 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001256 || (0 /* -- prohibited by X.693:8.3.4 */
1257 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkin8bb57a22007-12-03 13:41:36 +00001258 && expr_elements_count(arg, expr))
1259 || (expr->expr_type == ASN_BASIC_INTEGER
1260 && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN)
1261 )
Lev Walkine0b56e02005-02-25 12:10:27 +00001262 etd_spec = ETD_HAS_SPECIFICS;
1263 else
1264 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +00001265
Lev Walkin8de2ab22004-09-26 13:11:31 +00001266 /*
1267 * If this type just blindly refers the other type, alias it.
1268 * Type1 ::= Type2
1269 */
Lev Walkine0b56e02005-02-25 12:10:27 +00001270 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001271 REDIR(saved_target);
Lev Walkine0b56e02005-02-25 12:10:27 +00001272 return 0;
1273 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001274 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +00001275 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
Lev Walkin6938d042005-03-04 23:23:50 +00001276 && etd_spec == ETD_NO_SPECIFICS
1277 && 0 /* This shortcut is incompatible with XER */
1278 ) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001279 char *type_name;
1280 REDIR(OT_FUNC_DECLS);
1281 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1282 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001283 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkina00d6b32006-03-21 03:40:38 +00001284 OUT("#define\tasn_DEF_%s\t", MKID(expr));
Lev Walkin8de2ab22004-09-26 13:11:31 +00001285 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +00001286 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001287 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +00001288 OUT("\t// (Use -fall-defs-global to expose) */");
1289 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001290 REDIR(OT_CODE);
1291 OUT("/* This type is equivalent to %s */\n", type_name);
1292 OUT("\n");
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001293 REDIR(saved_target);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001294 return 0;
1295 }
1296
Lev Walkin59004fa2004-08-20 13:37:01 +00001297 REDIR(OT_CODE);
1298
Lev Walkinf15320b2004-06-03 03:38:44 +00001299 /*
1300 * Constraint checking.
1301 */
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001302 if(!(arg->flags & A1C_NO_CONSTRAINTS) && expr->combined_constraints) {
Lev Walkina00d6b32006-03-21 03:40:38 +00001303 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001304 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001305 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001306 OUT("%s", p);
1307 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1308 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001309 INDENT(+1);
Lev Walkin1eded352006-07-13 11:19:01 +00001310 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001311 OUT("\n");
Lev Walkin725883b2006-10-09 12:07:58 +00001312 DEBUG("expr constraint checking code for %s", p);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001313 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1314 OUT("/* Replace with underlying type checker */\n");
1315 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001316 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001317 asn1c_type_name(arg, expr, TNF_SAFE));
1318 OUT("return td->check_constraints"
Lev Walkin1eded352006-07-13 11:19:01 +00001319 "(td, sptr, ctfailcb, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001320 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001321 INDENT(-1);
1322 OUT("}\n");
1323 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001324 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001325
Lev Walkin725883b2006-10-09 12:07:58 +00001326 REDIR(OT_STAT_DEFS);
1327
1328 /*
1329 * Print out asn_DEF_<type>_[all_]tags[] vectors.
1330 */
1331 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
1332 DEBUG("emit tag vectors for %s %d, %d, %d", expr->Identifier,
1333 tv_mode, tags_count, all_tags_count);
1334
1335 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1336 0, etd_spec);
1337
1338 REDIR(OT_CODE);
1339
Lev Walkinf15320b2004-06-03 03:38:44 +00001340 /*
1341 * Emit suicidal functions.
1342 */
1343
Lev Walkinf15320b2004-06-03 03:38:44 +00001344 /*
1345 * This function replaces certain fields from the definition
1346 * of a type with the corresponding fields from the basic type
1347 * (from which the current type is inherited).
1348 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001349 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001350 OUT(" * This type is implemented using %s,\n",
1351 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001352 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001353 OUT(" */\n");
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001354
1355#if 0 /* remove unnecessary functions */
Lev Walkinf15320b2004-06-03 03:38:44 +00001356 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001357 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001358 MKID(expr), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001359 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001360 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001361 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001362 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001363 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1364 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
Lev Walkin6169b8d2013-12-07 11:02:44 -08001365 OUT("td->check_constraints = asn_DEF_%s.check_constraints;\n", type_name);
Lev Walkin05363a72004-09-29 13:16:40 +00001366 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1367 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1368 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1369 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin59b176e2005-11-26 11:25:14 +00001370 OUT("td->uper_decoder = asn_DEF_%s.uper_decoder;\n", type_name);
Lev Walkin1dc85292006-08-18 01:32:26 +00001371 OUT("td->uper_encoder = asn_DEF_%s.uper_encoder;\n", type_name);
Lev Walkinba68c912017-07-06 07:52:39 -07001372 OUT("td->oer_decoder = asn_DEF_%s.oer_decoder;\n", type_name);
1373 OUT("td->oer_encoder = asn_DEF_%s.oer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001374 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001375 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001376 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1377 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1378 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1379 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001380 OUT("/* End of these lines */\n");
1381 }
Lev Walkinb33425f2017-07-14 14:59:52 +04001382 OUT("if(!td->oer_constraints)\n");
1383 OUT("\ttd->oer_constraints = asn_DEF_%s.oer_constraints;\n",
1384 type_name);
Lev Walkin59b176e2005-11-26 11:25:14 +00001385 OUT("if(!td->per_constraints)\n");
1386 OUT("\ttd->per_constraints = asn_DEF_%s.per_constraints;\n",
1387 type_name);
Lev Walkin05363a72004-09-29 13:16:40 +00001388 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1389 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001390 if(etd_spec != ETD_NO_SPECIFICS) {
1391 INDENT(-1);
Lev Walkin7ef83a42005-03-29 19:04:24 +00001392 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001393 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001394 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1395 if(etd_spec == ETD_NO_SPECIFICS) {
1396 INDENT(-1);
1397 OUT("\n");
1398 } else {
1399 OUT("\t// Defined explicitly */\n");
1400 }
1401 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001402 OUT("}\n");
1403 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001404
Lev Walkina00d6b32006-03-21 03:40:38 +00001405 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001406 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001407 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001408 OUT("%s", p);
1409 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1410 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001411 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001412 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001413 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1414 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001415 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001416 );
1417 OUT("}\n");
1418 OUT("\n");
1419
Lev Walkina00d6b32006-03-21 03:40:38 +00001420 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001421 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001422 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001423 OUT("%s", p);
1424 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1425 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001426 INDENTED(
1427 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001428 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1429 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001430 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1431 );
1432 OUT("}\n");
1433 OUT("\n");
1434
Lev Walkina00d6b32006-03-21 03:40:38 +00001435 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001436 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001437 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001438 OUT("%s", p);
1439 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1440 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001441 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001442 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001443 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1444 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001445 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001446 );
1447 OUT("}\n");
1448 OUT("\n");
1449
Lev Walkina00d6b32006-03-21 03:40:38 +00001450 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001451 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001452 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001453 OUT("%s", p);
1454 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1455 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001456 INDENTED(
1457 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1458 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001459 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1460 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001461 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1462 );
1463 OUT("}\n");
1464 OUT("\n");
1465
Lev Walkina00d6b32006-03-21 03:40:38 +00001466 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001467 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001468 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001469 OUT("%s", p);
1470 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1471 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001472 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001473 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001474 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1475 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001476 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1477 );
1478 OUT("}\n");
1479 OUT("\n");
1480
Lev Walkina00d6b32006-03-21 03:40:38 +00001481 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001482 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001483 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001484 OUT("%s", p);
1485 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1486 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001487 INDENTED(
1488 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1489 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001490 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1491 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001492 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001493 );
1494 OUT("}\n");
1495 OUT("\n");
1496
Lev Walkinb33425f2017-07-14 14:59:52 +04001497 if(arg->flags & A1C_GEN_OER) {
1498 p = MKID(expr);
1499
1500 if(HIDE_INNER_DEFS) OUT("static ");
1501 OUT("asn_dec_rval_t\n");
1502 OUT("%s", p);
1503 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1504 OUT("_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
1505 INDENTED(
1506 OUT("\tasn_oer_constraints_t *constraints, void **structure, const void *buffer, size_t size) {\n");
1507 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1508 p, expr->_type_unique_index);
1509 OUT("return td->oer_decoder(opt_codec_ctx, td, constraints, structure, buffer, size);\n");
1510 );
1511 OUT("}\n");
1512 OUT("\n");
1513
1514 p = MKID(expr);
1515 if(HIDE_INNER_DEFS) OUT("static ");
1516 OUT("asn_enc_rval_t\n");
1517 OUT("%s", p);
1518 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1519 OUT("_encode_oer(asn_TYPE_descriptor_t *td,\n");
1520 INDENTED(
1521 OUT("\tasn_oer_constraints_t *constraints,\n");
1522 OUT("\tvoid *structure, asn_app_consume_bytes_f *cb, void *app_key) {\n");
1523 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1524 p, expr->_type_unique_index);
1525 OUT("return td->uper_encoder(td, constraints, structure, cb, app_key);\n");
1526 );
1527 OUT("}\n");
1528 OUT("\n");
1529 }
1530
Lev Walkin59b176e2005-11-26 11:25:14 +00001531 if(arg->flags & A1C_GEN_PER) {
Lev Walkina00d6b32006-03-21 03:40:38 +00001532 p = MKID(expr);
Lev Walkin1dc85292006-08-18 01:32:26 +00001533
Lev Walkin59b176e2005-11-26 11:25:14 +00001534 if(HIDE_INNER_DEFS) OUT("static ");
1535 OUT("asn_dec_rval_t\n");
1536 OUT("%s", p);
1537 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1538 OUT("_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
1539 INDENTED(
1540 OUT("\tasn_per_constraints_t *constraints, void **structure, asn_per_data_t *per_data) {\n");
1541 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1542 p, expr->_type_unique_index);
1543 OUT("return td->uper_decoder(opt_codec_ctx, td, constraints, structure, per_data);\n");
1544 );
1545 OUT("}\n");
1546 OUT("\n");
Lev Walkin1dc85292006-08-18 01:32:26 +00001547
1548 p = MKID(expr);
1549 if(HIDE_INNER_DEFS) OUT("static ");
1550 OUT("asn_enc_rval_t\n");
1551 OUT("%s", p);
1552 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1553 OUT("_encode_uper(asn_TYPE_descriptor_t *td,\n");
1554 INDENTED(
1555 OUT("\tasn_per_constraints_t *constraints,\n");
1556 OUT("\tvoid *structure, asn_per_outp_t *per_out) {\n");
1557 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1558 p, expr->_type_unique_index);
1559 OUT("return td->uper_encoder(td, constraints, structure, per_out);\n");
1560 );
1561 OUT("}\n");
1562 OUT("\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00001563 }
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001564#endif
Lev Walkinf15320b2004-06-03 03:38:44 +00001565 REDIR(OT_FUNC_DECLS);
1566
Lev Walkina00d6b32006-03-21 03:40:38 +00001567 p = MKID(expr);
Lev Walkine0b56e02005-02-25 12:10:27 +00001568 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001569 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1570 "\t// (Use -fall-defs-global to expose) */\n",
1571 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001572 } else {
1573 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08001574 if (etd_spec == ETD_HAS_SPECIFICS) {
1575 if((expr->expr_type == ASN_BASIC_ENUMERATED) ||
1576 (expr->expr_type == ASN_BASIC_INTEGER)) {
1577 if(expr->_type_referenced) {
1578 OUT("extern asn_INTEGER_specifics_t "
1579 "asn_SPC_%s_specs_%d;\n", p, expr->_type_unique_index);
1580 }
1581 } else {
1582 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
1583
1584 OUT("extern asn_%s_specifics_t ", asn1c_type_name(arg, terminal, TNF_SAFE));
1585 OUT("asn_SPC_%s_specs_%d;\n", MKID(expr), expr->_type_unique_index);
1586 }
1587 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001588 OUT("asn_struct_free_f %s_free;\n", p);
1589 OUT("asn_struct_print_f %s_print;\n", p);
1590 OUT("asn_constr_check_f %s_constraint;\n", p);
1591 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1592 OUT("der_type_encoder_f %s_encode_der;\n", p);
1593 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1594 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
Lev Walkinb33425f2017-07-14 14:59:52 +04001595 if(arg->flags & A1C_GEN_OER) {
1596 OUT("oer_type_decoder_f %s_decode_oer;\n", p);
1597 OUT("oer_type_encoder_f %s_encode_oer;\n", p);
1598 }
Lev Walkin1dc85292006-08-18 01:32:26 +00001599 if(arg->flags & A1C_GEN_PER) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001600 OUT("per_type_decoder_f %s_decode_uper;\n", p);
Lev Walkin1dc85292006-08-18 01:32:26 +00001601 OUT("per_type_encoder_f %s_encode_uper;\n", p);
1602 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001603 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001604
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08001605 REDIR(saved_target);
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001606
Lev Walkinf15320b2004-06-03 03:38:44 +00001607 return 0;
1608}
1609
1610int
1611asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1612
1613 OUT("/*\n");
1614 OUT(" * This type is extensible,\n");
1615 OUT(" * possible extensions are below.\n");
1616 OUT(" */\n");
1617
1618 return 0;
1619}
1620
Lev Walkin59b176e2005-11-26 11:25:14 +00001621static int
1622compute_extensions_start(asn1p_expr_t *expr) {
Lev Walkin35631482004-07-01 00:52:50 +00001623 asn1p_expr_t *v;
Lev Walkin59b176e2005-11-26 11:25:14 +00001624 int eidx = 0;
Lev Walkin35631482004-07-01 00:52:50 +00001625 TQ_FOR(v, &(expr->members), next) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001626 if(v->expr_type == A1TC_EXTENSIBLE)
1627 return eidx;
1628 eidx++;
Lev Walkin35631482004-07-01 00:52:50 +00001629 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001630 return -1;
Lev Walkin35631482004-07-01 00:52:50 +00001631}
1632
Lev Walkinf15320b2004-06-03 03:38:44 +00001633static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001634_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001635
1636 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001637 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001638 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1639 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1640 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1641 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1642 case TC_NOCLASS:
1643 break;
1644 }
Lev Walkinda997b12017-08-04 01:38:41 -07001645 OUT(" | (%s << 2))", asn1p_itoa(tag->tag_value));
Lev Walkinf15320b2004-06-03 03:38:44 +00001646
1647 return 0;
1648}
1649
Lev Walkinfd171ef2004-06-06 07:20:17 +00001650
1651static int
1652_tag2el_cmp(const void *ap, const void *bp) {
1653 const tag2el_t *a = ap;
1654 const tag2el_t *b = bp;
1655 const struct asn1p_type_tag_s *ta = &a->el_tag;
1656 const struct asn1p_type_tag_s *tb = &b->el_tag;
1657
1658 if(ta->tag_class == tb->tag_class) {
1659 if(ta->tag_value == tb->tag_value) {
1660 /*
1661 * Sort by their respective positions.
1662 */
1663 if(a->el_no < b->el_no)
1664 return -1;
1665 else if(a->el_no > b->el_no)
1666 return 1;
1667 return 0;
1668 } else if(ta->tag_value < tb->tag_value)
1669 return -1;
1670 else
1671 return 1;
1672 } else if(ta->tag_class < tb->tag_class) {
1673 return -1;
1674 } else {
1675 return 1;
1676 }
1677}
1678
Lev Walkinf15320b2004-06-03 03:38:44 +00001679/*
1680 * For constructed types, number of external tags may be greater than
1681 * number of elements in the type because of CHOICE type.
1682 * T ::= SET { -- Three possible tags:
1683 * a INTEGER, -- One tag is here...
1684 * b Choice1 -- ... and two more tags are there.
1685 * }
1686 * Choice1 ::= CHOICE {
1687 * s1 IA5String,
1688 * s2 ObjectDescriptor
1689 * }
1690 */
1691static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001692_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001693 asn1p_expr_t *expr = arg->expr;
1694 arg_t tmparg = *arg;
1695 asn1p_expr_t *v;
1696 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001697 int original_count = *count;
1698 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001699
1700 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001701 if(v->expr_type == A1TC_EXTENSIBLE) {
1702 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001703 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001704 * only for the root part.
1705 */
1706 if(flags == FTE_CANONICAL_XER
1707 && sort_until == -1)
1708 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001709 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001710 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001711
1712 tmparg.expr = v;
1713
1714 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001715 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001716 return -1;
1717 }
1718
1719 element++;
1720 }
1721
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001722
1723 if(flags == FTE_CANONICAL_XER) {
1724 if(sort_until == -1) sort_until = *count;
1725 qsort((*tag2el) + original_count,
1726 sort_until - original_count,
1727 sizeof(**tag2el), _tag2el_cmp);
1728 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1729 && (sort_until - original_count) >= 1) {
1730 /* Only take in account the root component */
1731 *count = original_count + 1;
1732 }
1733 } else {
1734 /*
1735 * Sort the map according to canonical order of their
1736 * tags and element numbers.
1737 */
1738 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1739 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001740
Lev Walkin38abe792004-06-14 13:09:45 +00001741 /*
1742 * Initialize .toff_{first|last} members.
1743 */
1744 if(*count) {
1745 struct asn1p_type_tag_s *cur_tag = 0;
1746 tag2el_t *cur = *tag2el;
1747 tag2el_t *end = cur + *count;
1748 int occur, i;
1749 for(occur = 0; cur < end; cur++) {
1750 if(cur_tag == 0
1751 || cur_tag->tag_value != cur->el_tag.tag_value
1752 || cur_tag->tag_class != cur->el_tag.tag_class) {
1753 cur_tag = &cur->el_tag;
1754 occur = 0;
1755 } else {
1756 occur++;
1757 }
1758 cur->toff_first = -occur;
1759 for(i = 0; i >= -occur; i--)
1760 cur[i].toff_last = -i;
1761 }
1762 }
1763
Lev Walkinf15320b2004-06-03 03:38:44 +00001764 return 0;
1765}
1766
1767static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001768_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001769 struct asn1p_type_tag_s tag;
1770 int ret;
1771
1772 assert(el_no >= 0);
1773
Lev Walkina9cc46e2004-09-22 16:06:28 +00001774 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
Lev Walkin59b176e2005-11-26 11:25:14 +00001775 arg->expr, &tag, AFT_IMAGINARY_ANY);
Lev Walkinf15320b2004-06-03 03:38:44 +00001776 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001777 tag2el_t *te;
1778 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001779 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001780
Lev Walkinac7e2292004-09-05 10:42:33 +00001781 if(tag.tag_value == -1) {
1782 /*
1783 * This is an untagged ANY type,
1784 * proceed without adding a tag
1785 */
1786 return 0;
1787 }
1788
Lev Walkin201943e2004-09-04 04:42:29 +00001789 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001790 if(p) *tag2el = p;
1791 else return -1;
1792
Lev Walkin725883b2006-10-09 12:07:58 +00001793 if(0) DEBUG("Found tag for %s: %ld",
Lev Walkinf15320b2004-06-03 03:38:44 +00001794 arg->expr->Identifier,
1795 (long)tag.tag_value);
1796
Lev Walkin201943e2004-09-04 04:42:29 +00001797 te = &((*tag2el)[*count]);
1798 te->el_tag = tag;
1799 te->el_no = el_no;
1800 te->from_expr = arg->expr;
1801 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001802 return 0;
1803 }
1804
1805 DEBUG("Searching tag in complex expression %s:%x at line %d",
1806 arg->expr->Identifier,
1807 arg->expr->expr_type,
1808 arg->expr->_lineno);
1809
1810 /*
1811 * Iterate over members of CHOICE type.
1812 */
1813 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001814 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001815 }
1816
1817 if(arg->expr->expr_type == A1TC_REFERENCE) {
1818 arg_t tmp = *arg;
1819 asn1p_expr_t *expr;
Lev Walkinb85a8132005-08-18 13:38:19 +00001820 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001821 arg->expr->reference);
1822 if(expr) {
1823 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001824 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001825 } else {
1826 FATAL("Cannot dereference %s at line %d",
1827 arg->expr->Identifier,
1828 arg->expr->_lineno);
1829 return -1;
1830 }
1831 }
1832
1833 DEBUG("No tag for %s at line %d",
1834 arg->expr->Identifier,
1835 arg->expr->_lineno);
1836
1837 return -1;
1838}
1839
1840static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001841emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001842 asn1p_expr_t *expr = arg->expr;
Lev Walkinf3b29b12005-06-02 05:21:37 +00001843 int i;
1844
1845 if(!tag2el_count) return 0; /* No top level tags */
Lev Walkinfd171ef2004-06-06 07:20:17 +00001846
Wim Lewisfb6344e2014-07-28 12:16:01 -07001847 OUT("static const asn_TYPE_tag2member_t asn_MAP_%s_tag2el%s_%d[] = {\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00001848 MKID(expr), opt_modifier?opt_modifier:"",
Lev Walkin59b176e2005-11-26 11:25:14 +00001849 expr->_type_unique_index);
Lev Walkinf3b29b12005-06-02 05:21:37 +00001850 for(i = 0; i < tag2el_count; i++) {
1851 OUT(" { ");
1852 _print_tag(arg, &tag2el[i].el_tag);
1853 OUT(", ");
1854 OUT("%d, ", tag2el[i].el_no);
1855 OUT("%d, ", tag2el[i].toff_first);
1856 OUT("%d ", tag2el[i].toff_last);
Lev Walkin4062b012013-10-11 14:29:38 -07001857 OUT("}%s /* %s",
Lev Walkinf3b29b12005-06-02 05:21:37 +00001858 (i + 1 < tag2el_count) ? "," : "",
Lev Walkin4062b012013-10-11 14:29:38 -07001859 tag2el[i].from_expr->Identifier);
1860 if(arg->flags & A1C_LINE_REFS)
1861 OUT("at %d", tag2el[i].from_expr->_lineno);
1862 OUT(" */\n");
Lev Walkinfd171ef2004-06-06 07:20:17 +00001863 }
1864 OUT("};\n");
1865
Lev Walkinf3b29b12005-06-02 05:21:37 +00001866 return 0;
Lev Walkinfd171ef2004-06-06 07:20:17 +00001867}
1868
Lev Walkin188ed2c2004-09-13 08:31:01 +00001869static enum tvm_compat
1870emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1871 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1872 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001873 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001874 int all_tags_count = 0;
1875 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001876 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001877
Lev Walkin6d1220a2004-09-14 14:10:10 +00001878 /* Cleanup before proceeding. */
1879 *tags_count_r = 0;
1880 *all_tags_count_r = 0;
1881
Lev Walkin906654e2004-09-10 15:49:15 +00001882 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001883 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin725883b2006-10-09 12:07:58 +00001884 if(tags_count < 0) {
1885 DEBUG("fail to fetch tags for %s", expr->Identifier);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001886 return -1;
Lev Walkin725883b2006-10-09 12:07:58 +00001887 }
Lev Walkin64399722004-08-11 07:17:22 +00001888
Lev Walkin188ed2c2004-09-13 08:31:01 +00001889 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001890 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001891 &all_tags, AFT_FULL_COLLECT);
1892 if(all_tags_count < 0) {
Lev Walkind8b83642016-03-14 02:00:27 -07001893 free(tags);
Lev Walkin725883b2006-10-09 12:07:58 +00001894 DEBUG("fail to fetch tags chain for %s", expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001895 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001896 }
Lev Walkin906654e2004-09-10 15:49:15 +00001897
Lev Walkin188ed2c2004-09-13 08:31:01 +00001898 assert(tags_count <= all_tags_count);
1899 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001900
Lev Walkin188ed2c2004-09-13 08:31:01 +00001901 if(tags_count <= all_tags_count) {
1902 for(i = 0; i < tags_count; i++) {
1903 if(tags[i].tag_value != all_tags[i].tag_value
1904 || tags[i].tag_class != all_tags[i].tag_class) {
1905 tv_mode = _TVM_DIFFERENT;
1906 break;
1907 }
1908 }
1909 if(i == tags_count && tags_count < all_tags_count)
1910 tv_mode = _TVM_SUBSET;
1911 } else {
1912 tv_mode = _TVM_DIFFERENT;
1913 }
1914
1915#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Wim Lewisfb6344e2014-07-28 12:16:01 -07001916 OUT("static const ber_tlv_tag_t asn_DEF_%s%s_tags_%d[] = {\n",\
Lev Walkina00d6b32006-03-21 03:40:38 +00001917 MKID(expr), name, \
Lev Walkin59b176e2005-11-26 11:25:14 +00001918 expr->_type_unique_index); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001919 INDENT(+1); \
1920 /* Print the array of collected tags */ \
1921 for(i = 0; i < tags_count; i++) { \
1922 if(i) OUT(",\n"); \
1923 _print_tag(arg, &tags[i]); \
1924 } \
1925 OUT("\n"); \
1926 INDENT(-1); \
1927 OUT("};\n"); \
1928 } while(0)
1929
1930 if(tags_count) {
1931 if(tv_mode == _TVM_SUBSET)
1932 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1933 else
1934 EMIT_TAGS_TABLE("", tags, tags_count);
1935 }
1936
1937 if(all_tags_count) {
1938 if(tv_mode == _TVM_DIFFERENT)
1939 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1940 }
1941
Lev Walkind8b83642016-03-14 02:00:27 -07001942 free(tags);
1943 free(all_tags);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001944
1945 *tags_count_r = tags_count;
1946 *all_tags_count_r = all_tags_count;
1947
1948 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001949}
Lev Walkin59004fa2004-08-20 13:37:01 +00001950
1951static int
Lev Walkin59964be2004-08-25 02:03:12 +00001952expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001953 asn1p_expr_t *topmost_parent;
1954 asn1p_expr_t *v;
1955 int elements = 0;
1956
Lev Walkina9cc46e2004-09-22 16:06:28 +00001957 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001958 if(!topmost_parent) return 0;
1959
Lev Walkine0b56e02005-02-25 12:10:27 +00001960 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
Lev Walkin1b3a1352016-01-10 13:15:02 -08001961 && !(topmost_parent->expr_type == ASN_BASIC_INTEGER)
Lev Walkin0f262452016-01-10 13:33:08 -08001962 && !(topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
1963 && !(topmost_parent->expr_type == ASN_BASIC_BIT_STRING))
Lev Walkin59004fa2004-08-20 13:37:01 +00001964 return 0;
1965
1966 TQ_FOR(v, &(topmost_parent->members), next) {
1967 if(v->expr_type != A1TC_EXTENSIBLE)
1968 elements++;
1969 }
1970
1971 return elements;
1972}
1973
Lev Walkin59b176e2005-11-26 11:25:14 +00001974static asn1p_expr_type_e
1975expr_get_type(arg_t *arg, asn1p_expr_t *expr) {
1976 asn1p_expr_t *terminal;
1977 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
1978 if(terminal) return terminal->expr_type;
1979 return A1TC_INVALID;
1980}
1981
Lev Walkin725883b2006-10-09 12:07:58 +00001982static asn1c_integer_t
1983PER_FROM_alphabet_characters(asn1cnst_range_t *range) {
1984 asn1c_integer_t numchars = 0;
1985 if(range->el_count) {
1986 int i;
1987 for(i = 0; i < range->el_count; i++)
1988 numchars
1989 += PER_FROM_alphabet_characters(range->elements[i]);
1990 } else {
1991 assert(range->left.type == ARE_VALUE);
1992 assert(range->right.type == ARE_VALUE);
1993 numchars = 1 + (range->right.value - range->left.value);
1994 }
1995 return numchars;
1996}
1997
Lev Walkin4118ccf2017-08-02 10:37:31 -07001998static void
1999emit_single_member_OER_constraint_comment(arg_t *arg, asn1cnst_range_t *range, char *type) {
2000
2001 /*
2002 * Print some courtesy debug information.
2003 */
2004 if(range
2005 && (range->left.type == ARE_VALUE || range->right.type == ARE_VALUE)) {
2006 OUT("\t/* ");
2007 if(type) OUT("(%s", type);
2008 OUT("(");
2009 if(range->left.type == ARE_VALUE)
Lev Walkinda997b12017-08-04 01:38:41 -07002010 OUT("%s", asn1p_itoa(range->left.value));
Lev Walkin4118ccf2017-08-02 10:37:31 -07002011 else
2012 OUT("MIN");
2013 OUT("..");
2014 if(range->right.type == ARE_VALUE)
Lev Walkinda997b12017-08-04 01:38:41 -07002015 OUT("%s", asn1p_itoa(range->right.value));
Lev Walkin4118ccf2017-08-02 10:37:31 -07002016 else
2017 OUT("MAX");
2018 if(range->extensible) OUT(",...");
2019 if(type) OUT(")");
2020 OUT(") */");
2021 }
2022}
2023
2024static int
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002025emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range) {
Lev Walkin4118ccf2017-08-02 10:37:31 -07002026 if(!range) {
2027 /* oer_support.h: asn_oer_constraint_s */
2028 OUT("{ 0, 0 }");
2029 return 0;
2030 }
2031
2032 if(range->incompatible || range->not_OER_visible) {
2033 OUT("{ 0, 0 }");
2034 } else if(range->left.type == ARE_VALUE &&
2035 range->right.type == ARE_VALUE) {
2036 asn1c_integer_t lb = range->left.value;
2037 asn1c_integer_t ub = range->right.value;
2038 unsigned width = 0;
2039 unsigned positive = 0;
2040
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002041
Lev Walkin4118ccf2017-08-02 10:37:31 -07002042 if(lb >= 0) {
2043 /* X.969 08/2015 10.2(a) */
2044 if(ub <= 255) {
2045 width = 1;
2046 } else if(ub <= 65535) {
2047 width = 2;
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002048 } else if((unsigned long long)ub <= 4294967295UL) {
Lev Walkin4118ccf2017-08-02 10:37:31 -07002049 width = 4;
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002050 } else if((unsigned long long)ub <= 18446744073709551615ULL) {
Lev Walkin4118ccf2017-08-02 10:37:31 -07002051 width = 8;
2052 }
2053 positive = 1;
2054 } else {
2055 positive = 0;
2056 /* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */
2057 if(lb >= -128 && ub <= 127) {
2058 width = 1;
2059 } else if(lb >= -32768 && ub <= 32767) {
2060 width = 2;
2061 } else if(lb >= -2147483648L && ub <= 2147483647L) {
2062 width = 4;
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002063 } else if(lb >= (-9223372036854775807LL-1)
Lev Walkin4118ccf2017-08-02 10:37:31 -07002064 && ub <= 9223372036854775807LL) {
2065 width = 8;
2066 }
2067 }
2068 OUT("{ %u, %u }", width, positive);
2069 } else {
2070 OUT("{ 0, 0 }");
2071 }
2072
2073 return 0;
2074}
2075
2076static int
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002077emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range) {
Lev Walkin4118ccf2017-08-02 10:37:31 -07002078 if(!range) {
2079 /* oer_support.h: asn_oer_constraint_s */
2080 OUT("-1");
2081 return 0;
2082 }
2083
2084 if(range->incompatible || range->not_OER_visible) {
2085 OUT("-1");
2086 } else {
2087 if(range->left.type == ARE_VALUE && range->right.type == ARE_VALUE
2088 && range->left.value == range->right.value
2089 && range->left.value >= 0) {
Lev Walkinda997b12017-08-04 01:38:41 -07002090 OUT("%s", asn1p_itoa(range->left.value));
Lev Walkin4118ccf2017-08-02 10:37:31 -07002091 } else {
2092 OUT("-1");
2093 }
2094 }
2095
2096 return 0;
2097}
2098
Lev Walkin59b176e2005-11-26 11:25:14 +00002099static int
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002100emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alphabetsize, const char *type) {
Lev Walkin4118ccf2017-08-02 10:37:31 -07002101 if(!range || range->incompatible || range->not_PER_visible) {
2102 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
Lev Walkin59b176e2005-11-26 11:25:14 +00002103 return 0;
Lev Walkin4118ccf2017-08-02 10:37:31 -07002104 }
Lev Walkin59b176e2005-11-26 11:25:14 +00002105
Lev Walkin4118ccf2017-08-02 10:37:31 -07002106 if(range->left.type == ARE_VALUE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002107 if(range->right.type == ARE_VALUE) {
2108 asn1c_integer_t cover = 1;
2109 asn1c_integer_t r = 1 + range->right.value
2110 - range->left.value;
Lev Walkinc46b7cb2006-08-18 02:27:55 +00002111 size_t rbits; /* Value range bits */
2112 ssize_t ebits; /* Value effective range bits */
Lev Walkin59b176e2005-11-26 11:25:14 +00002113
2114 if(range->empty_constraint)
2115 r = 0;
2116
Lev Walkin725883b2006-10-09 12:07:58 +00002117 if(alphabetsize) {
2118 /* X.691: 27.5.2 */
2119 r = PER_FROM_alphabet_characters(range);
2120 }
2121
Lev Walkin59b176e2005-11-26 11:25:14 +00002122 /* Compute real constraint */
2123 for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) {
2124 if(r <= cover)
2125 break;
2126 cover *= 2; /* Can't do shifting */
2127 if(cover < 0) {
2128 FATAL("Constraint at line %d too wide "
2129 "for %d-bits integer type",
2130 arg->expr->_lineno,
2131 sizeof(r) * 8);
2132 rbits = sizeof(r);
2133 break;
2134 }
2135 }
2136
Lev Walkin3a4689a2006-11-24 11:20:27 +00002137 if(alphabetsize) {
2138 ebits = rbits;
2139 } else {
Lev Walkin59b176e2005-11-26 11:25:14 +00002140 /* X.691, #10.9.4.1 */
2141 for(ebits = 0; ebits <= 16; ebits++)
2142 if(r <= 1 << ebits) break;
2143 if(ebits == 17
2144 || range->right.value >= 65536)
2145 ebits = -1;
Lev Walkin3a4689a2006-11-24 11:20:27 +00002146 if(0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002147 /* X.691, #10.5.7.1 */
2148 for(ebits = 0; ebits <= 8; ebits++)
2149 if(r <= 1 << ebits) break;
2150 if(ebits == 9) {
2151 if(r <= 65536)
2152 ebits = 16;
2153 else
2154 ebits = -1;
2155 }
2156 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00002157 }
Lev Walkin725883b2006-10-09 12:07:58 +00002158
Lev Walkin59b176e2005-11-26 11:25:14 +00002159 OUT("{ APC_CONSTRAINED%s,%s% d, % d, ",
2160 range->extensible
2161 ? " | APC_EXTENSIBLE" : "",
2162 range->extensible ? " " : "\t", rbits, ebits);
Lev Walkin3a4689a2006-11-24 11:20:27 +00002163
2164 if(alphabetsize) {
2165 asn1c_integer_t lv = range->left.value;
2166 asn1c_integer_t rv = range->right.value;
2167 int gcmt = 0;
2168 if(lv > 0x7fffffff) { lv = 0x7fffffff; gcmt++; }
2169 if(rv > 0x7fffffff) { rv = 0x7fffffff; gcmt++; }
2170 if(gcmt) {
Lev Walkin63b41262007-11-06 01:48:46 +00002171 OINTS(lv); OUT(", "); OINTS(rv); OUT(" }");
Lev Walkin3a4689a2006-11-24 11:20:27 +00002172 goto pcmt;
2173 }
2174 }
Lev Walkin59b176e2005-11-26 11:25:14 +00002175 } else {
2176 if(range->extensible) {
2177 OUT("{ APC_SEMI_CONSTRAINED | APC_EXTENSIBLE, "
2178 "-1, ");
2179 } else {
2180 OUT("{ APC_SEMI_CONSTRAINED,\t-1, -1, ");
2181 }
2182 }
Lev Walkin63b41262007-11-06 01:48:46 +00002183 OINTS(range->left.value); OUT(", ");
2184 OINTS(range->right.value); OUT(" }");
Lev Walkin59b176e2005-11-26 11:25:14 +00002185 } else {
2186 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
2187 }
2188
Lev Walkin3a4689a2006-11-24 11:20:27 +00002189 pcmt:
2190
Lev Walkin59b176e2005-11-26 11:25:14 +00002191 /*
2192 * Print some courtesy debug information.
2193 */
2194 if(range->left.type == ARE_VALUE
2195 || range->right.type == ARE_VALUE) {
2196 OUT("\t/* ");
2197 if(type) OUT("(%s", type);
2198 OUT("(");
2199 if(range->left.type == ARE_VALUE)
Lev Walkinda997b12017-08-04 01:38:41 -07002200 OUT("%s", asn1p_itoa(range->left.value));
Lev Walkin59b176e2005-11-26 11:25:14 +00002201 else
2202 OUT("MIN");
2203 OUT("..");
2204 if(range->right.type == ARE_VALUE)
Lev Walkinda997b12017-08-04 01:38:41 -07002205 OUT("%s", asn1p_itoa(range->right.value));
Lev Walkin59b176e2005-11-26 11:25:14 +00002206 else
2207 OUT("MAX");
2208 if(range->extensible) OUT(",...");
2209 if(type) OUT(")");
2210 OUT(") */");
2211 }
2212
2213 return 0;
2214}
2215
2216static int
Lev Walkin98eabc12017-07-19 08:51:11 +04002217emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) {
2218 int save_target = arg->target->target;
2219 asn1cnst_range_t *range;
2220 asn1p_expr_type_e etype;
2221
2222 etype = expr_get_type(arg, expr);
2223
2224 if((arg->flags & A1C_GEN_OER)
2225 && (expr->constraints || etype == ASN_BASIC_ENUMERATED
2226 || etype == ASN_CONSTR_CHOICE)) {
2227 /* Fall through */
2228 } else {
2229 return 0;
2230 }
2231
2232 REDIR(OT_CTDEFS);
2233
2234 OUT("static asn_oer_constraints_t "
2235 "asn_OER_%s_%s_constr_%d GCC_NOTUSED = {\n",
2236 pfx, MKID(expr), expr->_type_unique_index);
2237
2238 INDENT(+1);
2239
Lev Walkin4118ccf2017-08-02 10:37:31 -07002240 /* .value{.width,.positive} */
Lev Walkina28cbb92017-07-31 20:20:17 -07002241 range = asn1constraint_compute_OER_range(expr->Identifier, etype,
2242 expr->combined_constraints,
Lev Walkin98eabc12017-07-19 08:51:11 +04002243 ACT_EL_RANGE, 0, 0, 0);
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002244 if(emit_single_member_OER_constraint_value(arg, range)) {
Lev Walkin98eabc12017-07-19 08:51:11 +04002245 return -1;
2246 }
Lev Walkin4118ccf2017-08-02 10:37:31 -07002247 emit_single_member_OER_constraint_comment(arg, range, 0);
Lev Walkin98eabc12017-07-19 08:51:11 +04002248 asn1constraint_range_free(range);
2249
2250 OUT(",\n");
2251
Lev Walkin4118ccf2017-08-02 10:37:31 -07002252 /* .size */
Lev Walkina28cbb92017-07-31 20:20:17 -07002253 range = asn1constraint_compute_OER_range(expr->Identifier, etype,
2254 expr->combined_constraints,
Lev Walkin98eabc12017-07-19 08:51:11 +04002255 ACT_CT_SIZE, 0, 0, 0);
Lev Walkinb9e98cb2017-08-02 12:53:44 -07002256 if(emit_single_member_OER_constraint_size(arg, range)) {
Lev Walkin98eabc12017-07-19 08:51:11 +04002257 return -1;
2258 }
Lev Walkin4118ccf2017-08-02 10:37:31 -07002259 emit_single_member_OER_constraint_comment(arg, range, "SIZE");
Lev Walkin98eabc12017-07-19 08:51:11 +04002260 asn1constraint_range_free(range);
2261
2262 INDENT(-1);
2263
2264 OUT("};\n");
2265
2266 REDIR(save_target);
2267
2268 return 0;
2269}
2270
2271static int
Lev Walkin4b5dae32006-10-09 12:27:44 +00002272emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) {
Lev Walkin725883b2006-10-09 12:07:58 +00002273 int save_target = arg->target->target;
Lev Walkin59b176e2005-11-26 11:25:14 +00002274 asn1cnst_range_t *range;
2275 asn1p_expr_type_e etype;
2276
Lev Walkin07aaa342006-10-09 12:52:15 +00002277 etype = expr_get_type(arg, expr);
2278
Lev Walkin725883b2006-10-09 12:07:58 +00002279 if((arg->flags & A1C_GEN_PER)
2280 && (expr->constraints
Lev Walkin07aaa342006-10-09 12:52:15 +00002281 || etype == ASN_BASIC_ENUMERATED
2282 || etype == ASN_CONSTR_CHOICE)
Lev Walkin725883b2006-10-09 12:07:58 +00002283 ) {
2284 /* Fall through */
2285 } else {
2286 return 0;
2287 }
2288
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002289 if(expr->_type_referenced) {
2290 REDIR(OT_FUNC_DECLS);
2291
2292 OUT("extern asn_per_constraints_t "
2293 "asn_PER_%s_%s_constr_%d;\n",
2294 pfx, MKID(expr), expr->_type_unique_index);
2295 }
2296
Lev Walkin725883b2006-10-09 12:07:58 +00002297 REDIR(OT_CTDEFS);
2298
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002299 if(!(expr->_type_referenced)) OUT("static ");
2300 OUT("asn_per_constraints_t "
Lev Walkin1715b322013-03-28 04:02:13 -07002301 "asn_PER_%s_%s_constr_%d GCC_NOTUSED = {\n",
Lev Walkin4b5dae32006-10-09 12:27:44 +00002302 pfx, MKID(expr), expr->_type_unique_index);
Lev Walkin725883b2006-10-09 12:07:58 +00002303
Lev Walkin59b176e2005-11-26 11:25:14 +00002304 INDENT(+1);
2305
2306 /*
2307 * ENUMERATED and CHOICE are special.
2308 */
2309 if(etype == ASN_BASIC_ENUMERATED
2310 || etype == ASN_CONSTR_CHOICE) {
2311 asn1cnst_range_t tmprng;
2312 asn1p_expr_t *v;
2313 int extensible = 0;
2314 int eidx = -1;
2315
2316 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
2317 assert(expr);
2318
2319 TQ_FOR(v, &(expr->members), next) {
2320 if(v->expr_type == A1TC_EXTENSIBLE) {
2321 extensible++;
2322 break;
2323 }
2324 eidx++;
2325 }
2326
2327 memset(&tmprng, 0, sizeof (tmprng));
2328 tmprng.extensible = extensible;
2329 if(eidx < 0) tmprng.empty_constraint = 1;
2330 tmprng.left.type = ARE_VALUE;
2331 tmprng.left.value = 0;
2332 tmprng.right.type = ARE_VALUE;
2333 tmprng.right.value = eidx < 0 ? 0 : eidx;
Lev Walkin725883b2006-10-09 12:07:58 +00002334 if(emit_single_member_PER_constraint(arg, &tmprng, 0, 0))
Lev Walkin59b176e2005-11-26 11:25:14 +00002335 return -1;
Lev Walkin729eb862006-09-21 01:50:13 +00002336 } else if(etype & ASN_STRING_KM_MASK) {
Lev Walkina28cbb92017-07-31 20:20:17 -07002337 range = asn1constraint_compute_PER_range(expr->Identifier, etype,
Lev Walkin729eb862006-09-21 01:50:13 +00002338 expr->combined_constraints, ACT_CT_FROM,
2339 0, 0, 0);
Lev Walkin725883b2006-10-09 12:07:58 +00002340 DEBUG("Emitting FROM constraint for %s", expr->Identifier);
Lev Walkin3a4689a2006-11-24 11:20:27 +00002341
2342 if((range->left.type == ARE_MIN && range->right.type == ARE_MAX)
2343 || range->not_PER_visible) {
2344 switch(etype) {
2345 case ASN_STRING_BMPString:
2346 range->left.type = ARE_VALUE;
2347 range->left.value = 0;
2348 range->right.type = ARE_VALUE;
2349 range->right.value = 65535;
2350 range->not_PER_visible = 0;
2351 range->extensible = 0;
2352 break;
2353 case ASN_STRING_UniversalString:
2354 OUT("{ APC_CONSTRAINED,\t32, 32,"
2355 " 0, 2147483647 }"
2356 " /* special case 1 */\n");
2357 goto avoid;
2358 default:
2359 break;
2360 }
2361 }
Lev Walkin725883b2006-10-09 12:07:58 +00002362 if(emit_single_member_PER_constraint(arg, range, 1, 0))
Lev Walkin729eb862006-09-21 01:50:13 +00002363 return -1;
Lev Walkin3a4689a2006-11-24 11:20:27 +00002364 avoid:
Lev Walkin729eb862006-09-21 01:50:13 +00002365 asn1constraint_range_free(range);
Lev Walkin59b176e2005-11-26 11:25:14 +00002366 } else {
Lev Walkina28cbb92017-07-31 20:20:17 -07002367 range = asn1constraint_compute_PER_range(expr->Identifier, etype,
Lev Walkin59b176e2005-11-26 11:25:14 +00002368 expr->combined_constraints, ACT_EL_RANGE,
2369 0, 0, 0);
Lev Walkin725883b2006-10-09 12:07:58 +00002370 if(emit_single_member_PER_constraint(arg, range, 0, 0))
Lev Walkin59b176e2005-11-26 11:25:14 +00002371 return -1;
2372 asn1constraint_range_free(range);
2373 }
2374 OUT(",\n");
2375
Lev Walkina28cbb92017-07-31 20:20:17 -07002376 range = asn1constraint_compute_PER_range(expr->Identifier, etype,
Lev Walkin59b176e2005-11-26 11:25:14 +00002377 expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0);
Lev Walkin725883b2006-10-09 12:07:58 +00002378 if(emit_single_member_PER_constraint(arg, range, 0, "SIZE"))
Lev Walkin59b176e2005-11-26 11:25:14 +00002379 return -1;
2380 asn1constraint_range_free(range);
Lev Walkin725883b2006-10-09 12:07:58 +00002381 OUT(",\n");
2382
2383 if((etype & ASN_STRING_KM_MASK) && (expr->_mark & TM_PERFROMCT)) {
2384 int old_target = arg->target->target;
2385 REDIR(OT_CODE);
2386
2387 OUT("static int asn_PER_MAP_%s_%d_v2c(unsigned int value) {\n",
2388 MKID(expr), expr->_type_unique_index);
2389 OUT("\tif(value >= sizeof(permitted_alphabet_table_%d)/"
2390 "sizeof(permitted_alphabet_table_%d[0]))\n",
2391 expr->_type_unique_index,
2392 expr->_type_unique_index);
2393 OUT("\t\treturn -1;\n");
2394 OUT("\treturn permitted_alphabet_table_%d[value] - 1;\n",
2395 expr->_type_unique_index);
2396 OUT("}\n");
2397
2398 OUT("static int asn_PER_MAP_%s_%d_c2v(unsigned int code) {\n",
2399 MKID(expr), expr->_type_unique_index);
2400 OUT("\tif(code >= sizeof(permitted_alphabet_code2value_%d)/"
2401 "sizeof(permitted_alphabet_code2value_%d[0]))\n",
2402 expr->_type_unique_index,
2403 expr->_type_unique_index);
2404 OUT("\t\treturn -1;\n");
2405 OUT("\treturn permitted_alphabet_code2value_%d[code];\n",
2406 expr->_type_unique_index);
2407 OUT("}\n");
2408
2409 REDIR(old_target);
2410
2411 OUT("asn_PER_MAP_%s_%d_v2c,\t/* Value to PER code map */\n",
2412 MKID(expr), expr->_type_unique_index);
2413 OUT("asn_PER_MAP_%s_%d_c2v\t/* PER code to value map */\n",
2414 MKID(expr), expr->_type_unique_index);
2415 } else if(etype & ASN_STRING_KM_MASK) {
2416 DEBUG("No PER value map necessary for %s", MKID(expr));
2417 OUT("0, 0\t/* No PER character map necessary */\n");
2418 } else {
2419 OUT("0, 0\t/* No PER value map */\n");
2420 }
Lev Walkin59b176e2005-11-26 11:25:14 +00002421
2422 INDENT(-1);
2423
Lev Walkin725883b2006-10-09 12:07:58 +00002424 OUT("};\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002425
Lev Walkin725883b2006-10-09 12:07:58 +00002426 REDIR(save_target);
Lev Walkin59b176e2005-11-26 11:25:14 +00002427
2428 return 0;
2429}
2430
2431static int
Lev Walkin8032f7a2007-06-27 01:54:57 +00002432safe_string(const uint8_t *buf, int size) {
2433 const uint8_t *end = buf + size;
2434 for(; buf < end; buf++) {
2435 int ch = *buf;
2436 if((ch < 0x20 || ch > 0x7e) || ch == '"')
2437 return 0;
2438 }
2439 return 1;
2440}
2441
2442static void
2443emit_default_value(arg_t *arg, asn1p_value_t *v) {
2444
2445 OUT("static uint8_t defv[] = ");
2446 assert(v->type == ATV_STRING);
2447
2448 if(safe_string(v->value.string.buf, v->value.string.size)) {
2449 OUT("\"%s\";\n", v->value.string.buf);
2450 } else {
2451 uint8_t *b = v->value.string.buf;
2452 uint8_t *e = v->value.string.size + b;
2453 OUT("{ ");
2454 for(;b < e; b++)
2455 OUT("0x%02x, ", *b);
2456 OUT("0 };\n");
2457 }
2458}
2459
2460static int
Lev Walkin59b176e2005-11-26 11:25:14 +00002461try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
2462 int save_target = arg->target->target;
2463 asn1p_expr_type_e etype = expr_get_type(arg, expr);
2464 int fits_long = 0;
2465
2466 switch(etype) {
2467 case ASN_BASIC_BOOLEAN:
2468 fits_long = 1;
2469 case ASN_BASIC_INTEGER:
2470 case ASN_BASIC_ENUMERATED:
2471 if(expr->marker.default_value == NULL
2472 || expr->marker.default_value->type != ATV_INTEGER)
2473 break;
2474 if(!fits_long)
2475 fits_long = asn1c_type_fits_long(arg, expr)!=FL_NOTFIT;
2476 if(fits_long && !expr->marker.default_value->value.v_integer)
2477 expr->marker.flags &= ~EM_INDIRECT;
Lev Walkin8032f7a2007-06-27 01:54:57 +00002478 if(!out) {
Lev Walkinda997b12017-08-04 01:38:41 -07002479 OUT("asn_DFL_%d_set_%s,",
Lev Walkin8032f7a2007-06-27 01:54:57 +00002480 expr->_type_unique_index,
Lev Walkinda997b12017-08-04 01:38:41 -07002481 asn1p_itoa(expr->marker.default_value->value.v_integer));
2482 OUT("\t/* DEFAULT %s */\n",
2483 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin8032f7a2007-06-27 01:54:57 +00002484 return 1;
2485 }
Lev Walkin59b176e2005-11-26 11:25:14 +00002486 REDIR(OT_STAT_DEFS);
Lev Walkinda997b12017-08-04 01:38:41 -07002487 OUT("static int asn_DFL_%d_set_%s(int set_value, void **sptr) {\n",
Lev Walkin59b176e2005-11-26 11:25:14 +00002488 expr->_type_unique_index,
Lev Walkinda997b12017-08-04 01:38:41 -07002489 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin59b176e2005-11-26 11:25:14 +00002490 INDENT(+1);
2491 OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
2492 OUT("\n");
2493 OUT("if(!st) {\n");
Lev Walkin1dc85292006-08-18 01:32:26 +00002494 OUT("\tif(!set_value) return -1;\t/* Not a default value */\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002495 OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n");
2496 OUT("\tif(!st) return -1;\n");
2497 OUT("}\n");
2498 OUT("\n");
Lev Walkin1dc85292006-08-18 01:32:26 +00002499 OUT("if(set_value) {\n");
2500 INDENT(+1);
Lev Walkinda997b12017-08-04 01:38:41 -07002501 OUT("/* Install default value %s */\n",
2502 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin59b176e2005-11-26 11:25:14 +00002503 if(fits_long) {
Lev Walkin63b41262007-11-06 01:48:46 +00002504 OUT("*st = ");
2505 OINT(expr->marker.default_value->value.v_integer);
2506 OUT(";\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002507 OUT("return 0;\n");
2508 } else {
Lev Walkin63b41262007-11-06 01:48:46 +00002509 OUT("return asn_long2INTEGER(st, ");
2510 OINT(expr->marker.default_value->value.v_integer);
2511 OUT(");\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002512 }
2513 INDENT(-1);
Lev Walkin1dc85292006-08-18 01:32:26 +00002514 OUT("} else {\n");
2515 INDENT(+1);
Lev Walkinda997b12017-08-04 01:38:41 -07002516 OUT("/* Test default value %s */\n",
2517 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin1dc85292006-08-18 01:32:26 +00002518 if(fits_long) {
Lev Walkinda997b12017-08-04 01:38:41 -07002519 OUT("return (*st == %s);\n",
2520 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin1dc85292006-08-18 01:32:26 +00002521 } else {
2522 OUT("long value;\n");
2523 OUT("if(asn_INTEGER2long(st, &value))\n");
2524 OUT("\treturn -1;\n");
Lev Walkinda997b12017-08-04 01:38:41 -07002525 OUT("return (value == %s);\n",
2526 asn1p_itoa(expr->marker.default_value->value.v_integer));
Lev Walkin1dc85292006-08-18 01:32:26 +00002527 }
2528 INDENT(-1);
2529 OUT("}\n");
2530 INDENT(-1);
Lev Walkin59b176e2005-11-26 11:25:14 +00002531 OUT("}\n");
2532 REDIR(save_target);
2533 return 1;
2534 case ASN_BASIC_NULL:
2535 //expr->marker.flags &= ~EM_INDIRECT;
2536 return 0;
2537 default:
Lev Walkin8032f7a2007-06-27 01:54:57 +00002538 if(etype & ASN_STRING_KM_MASK) {
2539 if(expr->marker.default_value == NULL
2540 || expr->marker.default_value->type != ATV_STRING)
2541 break;
2542 if(!out) {
2543 OUT("asn_DFL_%d_set,\t/* DEFAULT \"%s\" */\n",
2544 expr->_type_unique_index,
2545 expr->marker.default_value->value.string.buf);
2546 return 1;
2547 }
2548 REDIR(OT_STAT_DEFS);
2549 OUT("static int asn_DFL_%d_set(int set_value, void **sptr) {\n", expr->_type_unique_index);
2550 INDENT(+1);
2551 emit_default_value(arg, expr->marker.default_value);
2552 OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
2553 OUT("\n");
2554 OUT("if(!st) {\n");
2555 OUT("\tif(!set_value) return -1;\t/* Not a default value */\n");
2556 OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n");
2557 OUT("\tif(!st) return -1;\n");
2558 OUT("}\n");
2559 OUT("\n");
2560 OUT("if(set_value) {\n");
2561 INDENT(+1);
2562 OUT("uint8_t *ptr = MALLOC(sizeof(defv));\n");
2563 OUT("if(!ptr) return -1;\n");
2564 OUT("memcpy(ptr, &defv, sizeof(defv));\n");
2565 OUT("FREEMEM(st->buf);\n");
2566 OUT("st->buf = ptr;\n");
2567 OUT("st->size = sizeof(defv) - 1;\n");
2568 OUT("return 0;\n");
2569 INDENT(-1);
2570 OUT("} else {\n");
2571 INDENT(+1);
2572 OUT("if(st->size != (sizeof(defv) - 1)\n");
2573 OUT("|| memcmp(st->buf, &defv, sizeof(defv) - 1))\n");
2574 OUT("\treturn 0;\n");
2575 OUT("return 1;\n");
2576 INDENT(-1);
2577 OUT("}\n"); OUT("\n");
2578 INDENT(-1);
2579 OUT("}\n");
2580 REDIR(save_target);
2581 return 1;
2582 }
2583 break;
Lev Walkin59b176e2005-11-26 11:25:14 +00002584 }
2585 return 0;
2586}
2587
Lev Walkin59004fa2004-08-20 13:37:01 +00002588static int
Lev Walkin59964be2004-08-25 02:03:12 +00002589emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00002590 int save_target;
2591 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00002592 struct asn1p_type_tag_s outmost_tag_s;
2593 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00002594 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00002595 char *p;
2596
Lev Walkinb9189732004-09-10 09:37:12 +00002597 if(asn1f_fetch_outmost_tag(arg->asn,
Lev Walkin59b176e2005-11-26 11:25:14 +00002598 expr->module, expr, &outmost_tag_s,
2599 AFT_IMAGINARY_ANY)) {
Lev Walkinb9189732004-09-10 09:37:12 +00002600 outmost_tag = 0;
2601 } else {
2602 outmost_tag = &outmost_tag_s;
2603 }
2604
Lev Walkin59004fa2004-08-20 13:37:01 +00002605 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00002606
2607 if(outmost_tag && outmost_tag->tag_value == -1)
2608 OUT("ATF_OPEN_TYPE | ");
Lev Walkinc8285712005-03-04 22:18:20 +00002609 OUT("%s, ",
2610 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
Lev Walkinb85a8132005-08-18 13:38:19 +00002611 if((expr->marker.flags & EM_OMITABLE) == EM_OMITABLE) {
Lev Walkin59004fa2004-08-20 13:37:01 +00002612 asn1p_expr_t *tv;
2613 int opts = 0;
Lev Walkinb85a8132005-08-18 13:38:19 +00002614 for(tv = expr;
2615 tv && (tv->marker.flags & EM_OMITABLE) == EM_OMITABLE;
Lev Walkin59004fa2004-08-20 13:37:01 +00002616 tv = TQ_NEXT(tv, next), opts++) {
2617 if(tv->expr_type == A1TC_EXTENSIBLE)
2618 opts--;
2619 }
2620 OUT("%d, ", opts);
2621 } else {
2622 OUT("0, ");
2623 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00002624 if(expr->_anonymous_type) {
2625 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
2626 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
2627 OUT("0,\n");
2628 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00002629 OUT("offsetof(struct ");
Lev Walkin0f5d74c2005-08-14 15:03:31 +00002630 out_name_chain(arg, ONC_avoid_keywords);
Lev Walkin21d00002005-03-04 08:48:53 +00002631 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00002632 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
2633 && (!UNNAMED_UNIONS)) OUT("choice.");
Lev Walkina00d6b32006-03-21 03:40:38 +00002634 OUT("%s),\n", MKID_safe(expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00002635 }
2636 INDENT(+1);
2637 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00002638 if(outmost_tag) {
2639 if(outmost_tag->tag_value == -1)
2640 OUT("-1 /* Ambiguous tag (ANY?) */");
2641 else
2642 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00002643 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00002644 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00002645 }
Lev Walkinb9189732004-09-10 09:37:12 +00002646
Lev Walkin59004fa2004-08-20 13:37:01 +00002647 OUT(",\n");
2648 if(C99_MODE) OUT(".tag_mode = ");
Lev Walkin9ab21b82006-10-19 02:46:01 +00002649 if((!(expr->expr_type & ASN_CONSTR_MASK)
2650 || expr->expr_type == ASN_CONSTR_CHOICE)
2651 && expr->tag.tag_class) {
Lev Walkin59004fa2004-08-20 13:37:01 +00002652 if(expr->tag.tag_mode == TM_IMPLICIT)
2653 OUT("-1,\t/* IMPLICIT tag at current level */\n");
2654 else
2655 OUT("+1,\t/* EXPLICIT tag at current level */\n");
2656 } else {
2657 OUT("0,\n");
2658 }
Lev Walkine0b56e02005-02-25 12:10:27 +00002659
2660 complex_contents =
2661 (expr->expr_type & ASN_CONSTR_MASK)
2662 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00002663 || (0 /* -- prohibited by X.693:8.3.4 */
2664 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkin8bb57a22007-12-03 13:41:36 +00002665 && expr_elements_count(arg, expr))
2666 || (expr->expr_type == ASN_BASIC_INTEGER
2667 && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN);
Lev Walkin59004fa2004-08-20 13:37:01 +00002668 if(C99_MODE) OUT(".type = ");
Lev Walkinae7bf3f2005-08-29 10:31:14 +00002669 OUT("&asn_DEF_");
Lev Walkin21d00002005-03-04 08:48:53 +00002670 if(complex_contents) {
Lev Walkina00d6b32006-03-21 03:40:38 +00002671 OUT("%s", MKID(expr));
Lev Walkin21d00002005-03-04 08:48:53 +00002672 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
2673 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00002674 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00002675 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00002676 }
Lev Walkin21d00002005-03-04 08:48:53 +00002677 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00002678 if(C99_MODE) OUT(".memb_constraints = ");
2679 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00002680 if(arg->flags & A1C_NO_CONSTRAINTS) {
2681 OUT("0,\t/* No check because of -fno-constraints */\n");
2682 } else {
Lev Walkina00d6b32006-03-21 03:40:38 +00002683 char *id = MKID(expr);
Lev Walkin8de2ab22004-09-26 13:11:31 +00002684 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00002685 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00002686 id = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin59b176e2005-11-26 11:25:14 +00002687 OUT("memb_%s_constraint_%d,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00002688 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00002689 }
Lev Walkin59004fa2004-08-20 13:37:01 +00002690 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00002691 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00002692 }
Lev Walkinb33425f2017-07-14 14:59:52 +04002693 if(C99_MODE) OUT(".oer_constraints = ");
2694 if(arg->flags & A1C_GEN_OER) {
2695 if(expr->constraints) {
2696 OUT("&asn_OER_memb_%s_constr_%d,\n",
2697 MKID(expr),
2698 expr->_type_unique_index);
2699 } else {
2700 OUT("0,\t/* No OER visible constraints */\n");
2701 }
2702 } else {
2703 OUT("0,\t/* OER is not compiled, use -gen-OER */\n");
2704 }
Lev Walkin59b176e2005-11-26 11:25:14 +00002705 if(C99_MODE) OUT(".per_constraints = ");
2706 if(arg->flags & A1C_GEN_PER) {
Lev Walkin07aaa342006-10-09 12:52:15 +00002707 if(expr->constraints) {
Lev Walkin4b5dae32006-10-09 12:27:44 +00002708 OUT("&asn_PER_memb_%s_constr_%d,\n",
Lev Walkina00d6b32006-03-21 03:40:38 +00002709 MKID(expr),
Lev Walkin59b176e2005-11-26 11:25:14 +00002710 expr->_type_unique_index);
2711 } else {
2712 OUT("0,\t/* No PER visible constraints */\n");
2713 }
Lev Walkin152a91e2005-02-14 20:41:29 +00002714 } else {
Lev Walkin59b176e2005-11-26 11:25:14 +00002715 OUT("0,\t/* PER is not compiled, use -gen-PER */\n");
2716 }
2717 if(C99_MODE) OUT(".default_value = ");
2718 if(try_inline_default(arg, expr, 0)) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002719 } else {
2720 OUT("0,\n");
2721 }
2722 if(C99_MODE) OUT(".name = ");
2723 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) {
2724 OUT("\"\"\n");
2725 } else {
2726 OUT("\"%s\"\n", expr->Identifier);
Lev Walkin152a91e2005-02-14 20:41:29 +00002727 }
Lev Walkin59004fa2004-08-20 13:37:01 +00002728 OUT("},\n");
2729 INDENT(-1);
2730
Lev Walkin8de2ab22004-09-26 13:11:31 +00002731 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00002732 return 0;
2733
2734 save_target = arg->target->target;
2735 REDIR(OT_CODE);
2736
Lev Walkin152a91e2005-02-14 20:41:29 +00002737 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00002738 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00002739 else
Lev Walkina00d6b32006-03-21 03:40:38 +00002740 p = MKID(expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00002741 OUT("static int\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002742 OUT("memb_%s_constraint_%d(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
Lev Walkin59004fa2004-08-20 13:37:01 +00002743 INDENT(+1);
Lev Walkin1eded352006-07-13 11:19:01 +00002744 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00002745 tmp_arg = *arg;
2746 tmp_arg.expr = expr;
Lev Walkin725883b2006-10-09 12:07:58 +00002747 DEBUG("member constraint checking code for %s", p);
Lev Walkin59004fa2004-08-20 13:37:01 +00002748 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00002749 OUT("return td->check_constraints"
Lev Walkin1eded352006-07-13 11:19:01 +00002750 "(td, sptr, ctfailcb, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00002751 }
2752 INDENT(-1);
2753 OUT("}\n");
2754 OUT("\n");
2755
Lev Walkin98eabc12017-07-19 08:51:11 +04002756 if(emit_member_OER_constraints(arg, expr, "memb"))
2757 return -1;
2758
Lev Walkin4b5dae32006-10-09 12:27:44 +00002759 if(emit_member_PER_constraints(arg, expr, "memb"))
Lev Walkin725883b2006-10-09 12:07:58 +00002760 return -1;
2761
Lev Walkin59004fa2004-08-20 13:37:01 +00002762 REDIR(save_target);
2763
2764 return 0;
2765}
Lev Walkin59964be2004-08-25 02:03:12 +00002766
Lev Walkindc06f6b2004-10-20 15:50:55 +00002767/*
2768 * Generate "asn_DEF_XXX" type definition.
2769 */
Lev Walkin59964be2004-08-25 02:03:12 +00002770static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00002771emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec spec) {
Lev Walkin8c2ca0b2006-09-17 06:31:08 +00002772 asn1p_expr_t *terminal;
Lev Walkin21d00002005-03-04 08:48:53 +00002773 int using_type_name = 0;
Lev Walkina00d6b32006-03-21 03:40:38 +00002774 char *p = MKID(expr);
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002775 char *p2 = (char *)0;
Lev Walkin59b176e2005-11-26 11:25:14 +00002776
Lev Walkin8c2ca0b2006-09-17 06:31:08 +00002777 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
2778
Lev Walkin01582a72017-07-26 18:47:08 -07002779 if(emit_member_OER_constraints(arg, expr, "type"))
2780 return -1;
2781
Lev Walkin4b5dae32006-10-09 12:27:44 +00002782 if(emit_member_PER_constraints(arg, expr, "type"))
Lev Walkin725883b2006-10-09 12:07:58 +00002783 return -1;
Lev Walkin59964be2004-08-25 02:03:12 +00002784
Lev Walkindd32b592004-09-06 08:07:29 +00002785 if(HIDE_INNER_DEFS)
2786 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkin59b176e2005-11-26 11:25:14 +00002787 OUT("asn_TYPE_descriptor_t asn_DEF_%s", p);
Lev Walkin21d00002005-03-04 08:48:53 +00002788 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
2789 OUT(" = {\n");
Lev Walkin188ed2c2004-09-13 08:31:01 +00002790 INDENT(+1);
Lev Walkin59b176e2005-11-26 11:25:14 +00002791
2792 if(expr->_anonymous_type) {
2793 p = ASN_EXPR_TYPE2STR(expr->expr_type);
2794 OUT("\"%s\",\n", p?p:"");
Lev Walkina00d6b32006-03-21 03:40:38 +00002795 OUT("\"%s\",\n",
2796 p ? asn1c_make_identifier(AMI_CHECK_RESERVED,
2797 0, p, 0) : "");
Lev Walkin59b176e2005-11-26 11:25:14 +00002798 } else {
2799 OUT("\"%s\",\n", expr->Identifier);
2800 OUT("\"%s\",\n", expr->Identifier);
2801 }
Lev Walkin59964be2004-08-25 02:03:12 +00002802
2803 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00002804 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00002805 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
Lev Walkin59b176e2005-11-26 11:25:14 +00002806 } else {
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002807 if (expr->expr_type == A1TC_REFERENCE) {
2808 p2 = strdup(asn1c_type_name(arg, terminal, TNF_SAFE));
2809 } else {
2810 p2 = strdup(asn1c_type_name(arg, expr, TNF_SAFE));
2811 }
Lev Walkina00d6b32006-03-21 03:40:38 +00002812 p = MKID(expr);
Lev Walkin59964be2004-08-25 02:03:12 +00002813 }
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002814 if (!p2)
2815 p2 = strdup(p);
Lev Walkin59964be2004-08-25 02:03:12 +00002816
Lev Walkin21d00002005-03-04 08:48:53 +00002817#define FUNCREF(foo) do { \
2818 OUT("%s", p); \
2819 if(HIDE_INNER_DEFS && !using_type_name) \
2820 OUT("_%d", expr->_type_unique_index); \
2821 OUT("_" #foo ",\n"); \
2822} while(0)
2823
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002824#define FUNCREF2(foo) \
2825do { \
2826 OUT("%s", p2); \
2827 OUT("_" #foo ",\n"); \
2828} while(0)
2829
2830 FUNCREF2(free);
2831 FUNCREF2(print);
Lev Walkincd2f48e2017-08-10 02:14:59 -07002832 FUNCREF2(compare);
mattipeee0b99ab2017-08-04 11:44:04 +01002833 if (arg->flags & A1C_NO_CONSTRAINTS)
2834 OUT("0,\t/* No check because of -fno-constraints */\n");
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002835 else
mattipeee0b99ab2017-08-04 11:44:04 +01002836 {
2837 if (!expr->combined_constraints)
2838 FUNCREF2(constraint);
2839 else
2840 FUNCREF(constraint);
2841 }
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002842 FUNCREF2(decode_ber);
2843 FUNCREF2(encode_der);
2844 FUNCREF2(decode_xer);
2845 FUNCREF2(encode_xer);
Lev Walkinb33425f2017-07-14 14:59:52 +04002846
2847 if(arg->flags & A1C_GEN_OER) {
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002848 FUNCREF2(decode_oer);
2849 FUNCREF2(encode_oer);
Lev Walkinb33425f2017-07-14 14:59:52 +04002850 } else {
2851 OUT("0, 0,\t/* No OER support, "
2852 "use \"-gen-OER\" to enable */\n");
2853 }
Lev Walkin1dc85292006-08-18 01:32:26 +00002854 if(arg->flags & A1C_GEN_PER) {
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002855 FUNCREF2(decode_uper);
2856 FUNCREF2(encode_uper);
Lev Walkin1dc85292006-08-18 01:32:26 +00002857 } else {
2858 OUT("0, 0,\t/* No PER support, "
2859 "use \"-gen-PER\" to enable */\n");
2860 }
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002861 if (p2) free(p2);
Lev Walkin59964be2004-08-25 02:03:12 +00002862
Lev Walkin8c2ca0b2006-09-17 06:31:08 +00002863 if(!terminal || terminal->expr_type == ASN_CONSTR_CHOICE) {
2864 //if(expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin59964be2004-08-25 02:03:12 +00002865 OUT("CHOICE_outmost_tag,\n");
2866 } else {
2867 OUT("0,\t/* Use generic outmost tag fetcher */\n");
2868 }
2869
Lev Walkina00d6b32006-03-21 03:40:38 +00002870 p = MKID(expr);
Lev Walkin59964be2004-08-25 02:03:12 +00002871 if(tags_count) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002872 OUT("asn_DEF_%s_tags_%d,\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002873 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002874 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002875 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002876 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0])",
Lev Walkin21d00002005-03-04 08:48:53 +00002877 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00002878 if(tv_mode == _TVM_SUBSET
2879 && tags_count != all_tags_count)
2880 OUT(" - %d", all_tags_count - tags_count);
2881 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00002882 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00002883 OUT("0,\t/* No effective tags (pointer) */\n");
2884 OUT("0,\t/* No effective tags (count) */\n");
2885 }
2886
2887 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002888 OUT("asn_DEF_%s_all_tags_%d,\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002889 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002890 OUT("sizeof(asn_DEF_%s_all_tags_%d)\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002891 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002892 OUT("\t/sizeof(asn_DEF_%s_all_tags_%d[0]), /* %d */\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002893 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00002894 } else if(all_tags_count) {
Lev Walkin59b176e2005-11-26 11:25:14 +00002895 OUT("asn_DEF_%s_tags_%d,\t/* Same as above */\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002896 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002897 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002898 p, expr->_type_unique_index);
Lev Walkin59b176e2005-11-26 11:25:14 +00002899 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0]), /* %d */\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002900 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00002901 } else {
2902 OUT("0,\t/* No tags (pointer) */\n");
2903 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00002904 }
2905
Lev Walkinb33425f2017-07-14 14:59:52 +04002906 if(arg->flags & A1C_GEN_OER) {
2907 if(expr->constraints
2908 || expr->expr_type == ASN_BASIC_ENUMERATED
2909 || expr->expr_type == ASN_CONSTR_CHOICE) {
2910 OUT("&asn_OER_type_%s_constr_%d,\n",
2911 p, expr->_type_unique_index);
2912 } else {
2913 OUT("0,\t/* No OER visible constraints */\n");
2914 }
2915 } else {
2916 OUT("0,\t/* No OER visible constraints */\n");
2917 }
2918
Lev Walkin59b176e2005-11-26 11:25:14 +00002919 if(arg->flags & A1C_GEN_PER) {
2920 if(expr->constraints
2921 || expr->expr_type == ASN_BASIC_ENUMERATED
2922 || expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin4b5dae32006-10-09 12:27:44 +00002923 OUT("&asn_PER_type_%s_constr_%d,\n",
Lev Walkin59b176e2005-11-26 11:25:14 +00002924 p, expr->_type_unique_index);
2925 } else {
2926 OUT("0,\t/* No PER visible constraints */\n");
2927 }
2928 } else {
2929 OUT("0,\t/* No PER visible constraints */\n");
2930 }
2931
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002932 if(elements_count ||
2933 ((expr->expr_type == A1TC_REFERENCE) &&
2934 (terminal->expr_type & ASN_CONSTR_MASK) &&
2935 expr_elements_count(arg, terminal))) {
2936
2937 if (expr->expr_type == A1TC_REFERENCE) {
2938 OUT("asn_MBR_%s_%d,\n", MKID(terminal), terminal->_type_unique_index);
2939
2940 if(terminal->expr_type == ASN_CONSTR_SEQUENCE_OF
2941 || terminal->expr_type == ASN_CONSTR_SET_OF) {
2942 OUT("%d,\t/* Single element */\n",
2943 expr_elements_count(arg, terminal));
2944 assert(expr_elements_count(arg, terminal) == 1);
2945 } else {
2946 OUT("%d,\t/* Elements count */\n",
2947 expr_elements_count(arg, terminal));
2948 }
Lev Walkin59964be2004-08-25 02:03:12 +00002949 } else {
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002950 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
2951
2952 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
2953 || expr->expr_type == ASN_CONSTR_SET_OF) {
2954 OUT("%d,\t/* Single element */\n",
2955 elements_count);
2956 assert(elements_count == 1);
2957 } else {
2958 OUT("%d,\t/* Elements count */\n",
2959 elements_count);
2960 }
Lev Walkin59964be2004-08-25 02:03:12 +00002961 }
2962 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00002963 if(expr_elements_count(arg, expr))
2964 OUT("0, 0,\t/* Defined elsewhere */\n");
2965 else
2966 OUT("0, 0,\t/* No members */\n");
2967 }
2968
2969 switch(spec) {
2970 case ETD_NO_SPECIFICS:
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +08002971 if ((expr->expr_type == A1TC_REFERENCE) &&
2972 ((terminal->expr_type & ASN_CONSTR_MASK) ||
2973 (terminal->expr_type == ASN_BASIC_ENUMERATED) ||
2974 ((terminal->expr_type == ASN_BASIC_INTEGER) &&
2975 (asn1c_type_fits_long(arg, terminal) == FL_FITS_UNSIGN)))) {
2976 OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n", MKID(terminal), terminal->_type_unique_index);
2977 } else if ((expr->expr_type == ASN_TYPE_ANY) ||
2978 (expr->expr_type == ASN_BASIC_BIT_STRING) ||
2979 (expr->expr_type == ASN_STRING_BMPString) ||
2980 (expr->expr_type == ASN_BASIC_OCTET_STRING) ||
2981 (expr->expr_type == ASN_STRING_UniversalString)) {
2982 OUT("&asn_SPC_%s_specs\t/* Additional specs */\n", asn1c_type_name(arg, expr, TNF_SAFE));
2983 } else if ((expr->expr_type == A1TC_REFERENCE) &&
2984 ((terminal->expr_type == ASN_TYPE_ANY) ||
2985 (terminal->expr_type == ASN_BASIC_BIT_STRING) ||
2986 (terminal->expr_type == ASN_STRING_BMPString) ||
2987 (terminal->expr_type == ASN_BASIC_OCTET_STRING) ||
2988 (terminal->expr_type == ASN_STRING_UniversalString))) {
2989 OUT("&asn_SPC_%s_specs\t/* Additional specs */\n", asn1c_type_name(arg, terminal, TNF_SAFE));
2990 } else {
2991 OUT("0\t/* No specifics */\n");
2992 }
Lev Walkin59964be2004-08-25 02:03:12 +00002993 break;
2994 case ETD_HAS_SPECIFICS:
Lev Walkin59b176e2005-11-26 11:25:14 +00002995 OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n",
Lev Walkin21d00002005-03-04 08:48:53 +00002996 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00002997 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00002998 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00002999 OUT("};\n");
3000 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00003001
3002 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00003003}
Lev Walkincc93b0f2004-09-10 09:18:20 +00003004
Lev Walkina9cc46e2004-09-22 16:06:28 +00003005static int
3006expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkind1bfea62005-11-08 03:06:16 +00003007 /*
3008 * X.680, 25.5, Table 5
3009 */
Lev Walkin59b176e2005-11-26 11:25:14 +00003010 switch(expr_get_type(arg, expr)) {
Lev Walkina9cc46e2004-09-22 16:06:28 +00003011 case ASN_BASIC_BOOLEAN:
3012 case ASN_BASIC_ENUMERATED:
3013 case ASN_BASIC_NULL:
3014 return 1;
Lev Walkind1bfea62005-11-08 03:06:16 +00003015 case ASN_CONSTR_CHOICE:
3016 return 2;
Lev Walkina9cc46e2004-09-22 16:06:28 +00003017 default:
3018 return 0;
3019 }
3020}
Lev Walkin801fabc2005-01-28 12:18:50 +00003021
3022static int
Lev Walkin0f5d74c2005-08-14 15:03:31 +00003023out_name_chain(arg_t *arg, enum onc_flags onc_flags) {
Lev Walkin801fabc2005-01-28 12:18:50 +00003024 asn1p_expr_t *expr = arg->expr;
3025 char *id;
3026
Lev Walkin0f5d74c2005-08-14 15:03:31 +00003027 if((arg->flags & A1C_COMPOUND_NAMES
3028 || onc_flags & ONC_force_compound_name)
Lev Walkin21d00002005-03-04 08:48:53 +00003029 && ((expr->expr_type & ASN_CONSTR_MASK)
3030 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkinb02a8832005-08-13 23:51:47 +00003031 || ((expr->expr_type == ASN_BASIC_INTEGER
3032 || expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkin21d00002005-03-04 08:48:53 +00003033 && expr_elements_count(arg, expr))
3034 )
Bi-Ruei, Chiu94f0b642017-06-29 01:34:23 +08003035 && expr->parent_expr) {
3036
Lev Walkin801fabc2005-01-28 12:18:50 +00003037 arg_t tmparg = *arg;
3038
3039 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00003040 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
3041
Lev Walkin0f5d74c2005-08-14 15:03:31 +00003042 out_name_chain(&tmparg, onc_flags);
Lev Walkin801fabc2005-01-28 12:18:50 +00003043
Bi-Ruei, Chiu94f0b642017-06-29 01:34:23 +08003044 if(expr->parent_expr->Identifier) OUT("__"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00003045
Lev Walkin801fabc2005-01-28 12:18:50 +00003046 /* Fall through */
3047 }
3048
Lev Walkin0f5d74c2005-08-14 15:03:31 +00003049 if(onc_flags & ONC_avoid_keywords)
Lev Walkina00d6b32006-03-21 03:40:38 +00003050 id = MKID_safe(expr);
Lev Walkin801fabc2005-01-28 12:18:50 +00003051 else
Lev Walkina00d6b32006-03-21 03:40:38 +00003052 id = MKID(expr);
Lev Walkin801fabc2005-01-28 12:18:50 +00003053 OUT("%s", id);
3054
3055 return 0;
3056}
Lev Walkinb85a8132005-08-18 13:38:19 +00003057
3058static int
3059emit_include_dependencies(arg_t *arg) {
3060 asn1p_expr_t *expr = arg->expr;
3061 asn1p_expr_t *memb;
3062
3063 /* Avoid recursive definitions. */
3064 TQ_FOR(memb, &(expr->members), next) {
3065 expr_break_recursion(arg, memb);
3066 }
3067
3068 TQ_FOR(memb, &(expr->members), next) {
3069
3070 if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
3071 if(terminal_structable(arg, memb)) {
3072 int saved_target = arg->target->target;
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +08003073 if(saved_target != OT_FWD_DECLS) {
3074 REDIR(OT_FWD_DECLS);
3075 OUT("%s;\n",
3076 asn1c_type_name(arg, memb, TNF_RSAFE));
3077 }
Lev Walkinb85a8132005-08-18 13:38:19 +00003078 REDIR(saved_target);
3079 }
3080 }
3081
3082 if((!(memb->expr_type & ASN_CONSTR_MASK)
3083 && memb->expr_type > ASN_CONSTR_MASK)
3084 || memb->meta_type == AMT_TYPEREF) {
3085 if(memb->marker.flags & EM_UNRECURSE) {
3086 GEN_POSTINCLUDE(asn1c_type_name(arg,
3087 memb, TNF_INCLUDE));
3088 } else {
3089 GEN_INCLUDE(asn1c_type_name(arg,
3090 memb, TNF_INCLUDE));
3091 }
3092 }
3093 }
3094
3095 return 0;
3096}
3097
3098/*
3099 * Check if it is better to make this type indirectly accessed via
3100 * a pointer.
3101 * This may be the case for the following recursive definition:
3102 * Type ::= CHOICE { member Type };
3103 */
3104static int
3105expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkinb85a8132005-08-18 13:38:19 +00003106 int ret;
3107
3108 if(expr->marker.flags & EM_UNRECURSE)
3109 return 1; /* Already broken */
3110
Lev Walkinb85a8132005-08-18 13:38:19 +00003111 /* -findirect-choice compiles members of CHOICE as indirect pointers */
3112 if((arg->flags & A1C_INDIRECT_CHOICE)
3113 && arg->expr->expr_type == ASN_CONSTR_CHOICE
Lev Walkin59b176e2005-11-26 11:25:14 +00003114 && (expr_get_type(arg, expr) & ASN_CONSTR_MASK)
Lev Walkinb85a8132005-08-18 13:38:19 +00003115 ) {
3116 /* Break cross-reference */
3117 expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE;
3118 return 1;
3119 }
3120
3121 if((expr->marker.flags & EM_INDIRECT)
3122 || arg->expr->expr_type == ASN_CONSTR_SET_OF
3123 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
3124 if(terminal_structable(arg, expr)) {
3125 expr->marker.flags |= EM_UNRECURSE;
3126
3127 if(arg->expr->expr_type == ASN_CONSTR_SET_OF
3128 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
3129 /* Don't put EM_INDIRECT even if recursion */
3130 return 1;
3131 }
3132
3133 /* Fall through */
3134 }
3135 }
3136
3137 /* Look for recursive back-references */
3138 ret = expr_defined_recursively(arg, expr);
3139 switch(ret) {
3140 case 2: /* Explicitly break the recursion */
3141 case 1: /* Use safer typing */
3142 expr->marker.flags |= EM_INDIRECT;
3143 expr->marker.flags |= EM_UNRECURSE;
3144 break;
3145 }
3146
3147 return 0;
3148}
3149
3150/*
3151 * Check if the type can be represented using simple `struct TYPE`.
3152 */
3153static asn1p_expr_t *
3154terminal_structable(arg_t *arg, asn1p_expr_t *expr) {
3155 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
3156 if(terminal
3157 && !terminal->parent_expr
3158 && (terminal->expr_type & ASN_CONSTR_MASK)) {
3159 return terminal;
3160 }
3161 return 0;
3162}
3163
3164static int
3165asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key) {
3166 arg_t tmp = *arg;
3167 int maxret = 0;
3168 int ret;
3169
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +08003170 if(expr->_mark & TM_RECURSION) return 0;
Lev Walkinb85a8132005-08-18 13:38:19 +00003171 expr->_mark |= TM_RECURSION;
3172
3173 /* Invoke callback for every type going into recursion */
3174 tmp.expr = expr;
3175 maxret = callback(&tmp, key);
3176 if(maxret <= 1) {
3177 /*
3178 * Recursively invoke myself and the callbacks.
3179 */
3180 TQ_FOR(tmp.expr, &(expr->members), next) {
3181 ret = asn1c_recurse(&tmp, tmp.expr, callback, key);
3182 if(ret > maxret)
3183 maxret = ret;
3184 if(maxret > 1) break;
3185 }
3186 }
3187
3188 expr->_mark &= ~TM_RECURSION;
3189 return maxret;
3190}
3191
3192static int
3193check_is_refer_to(arg_t *arg, void *key) {
3194 asn1p_expr_t *terminal = terminal_structable(arg, arg->expr);
3195 if(terminal == key) {
3196 if(arg->expr->marker.flags & EM_INDIRECT)
3197 return 1; /* This is almost safe indirection */
3198 return 2;
3199 } else if(terminal) {
3200 /* This might be N-step circular loop. Dive into it. */
3201 return asn1c_recurse(arg, terminal, check_is_refer_to, key);
3202 }
3203 return 0;
3204}
3205
3206/*
3207 * Check if the possibly inner expression defined recursively.
3208 */
3209static int
3210expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr) {
3211 asn1p_expr_t *terminal;
3212 asn1p_expr_t *topmost;
3213
3214 /* If expression is top-level, there's no way it can be recursive. */
3215 if(expr->parent_expr == 0) return 0;
3216 if(expr->expr_type != A1TC_REFERENCE)
3217 return 0; /* Basic types are never recursive */
3218
3219 terminal = terminal_structable(arg, expr);
3220 if(!terminal) return 0; /* Terminal cannot be indirected */
3221
3222 /* Search for the parent container for the given expression */
3223 topmost = expr;
3224 while(topmost->parent_expr)
3225 topmost = topmost->parent_expr;
3226
3227 /* Look inside the terminal type if it mentions the parent expression */
3228 return asn1c_recurse(arg, terminal, check_is_refer_to, topmost);
3229}
Lev Walkin59b176e2005-11-26 11:25:14 +00003230
3231struct canonical_map_element {
3232 int eidx;
3233 asn1p_expr_t *expr;
3234};
3235static int compar_cameo(const void *ap, const void *bp);
3236static arg_t *cameo_arg;
3237static int *
3238compute_canonical_members_order(arg_t *arg, int el_count) {
3239 struct canonical_map_element *cmap;
3240 int *rmap;
3241 asn1p_expr_t *v;
3242 int eidx = 0;
3243 int ext_start = -1;
3244 int nextmax = -1;
3245 int already_sorted = 1;
3246
3247 cmap = calloc(el_count, sizeof *cmap);
3248 assert(cmap);
3249
3250 TQ_FOR(v, &(arg->expr->members), next) {
3251 if(v->expr_type != A1TC_EXTENSIBLE) {
3252 cmap[eidx].eidx = eidx;
3253 cmap[eidx].expr = v;
3254 eidx++;
3255 } else if(ext_start == -1)
3256 ext_start = eidx;
3257 }
3258
3259 cameo_arg = arg;
3260 if(ext_start == -1) {
3261 /* Sort the whole thing */
3262 qsort(cmap, el_count, sizeof(*cmap), compar_cameo);
3263 } else {
3264 /* Sort root and extensions independently */
3265 qsort(cmap, ext_start, sizeof(*cmap), compar_cameo);
3266 qsort(cmap + ext_start, el_count - ext_start,
3267 sizeof(*cmap), compar_cameo);
3268 }
3269
3270 /* move data back to a simpler map */
3271 rmap = calloc(el_count, sizeof *rmap);
3272 assert(rmap);
3273 for(eidx = 0; eidx < el_count; eidx++) {
3274 rmap[eidx] = cmap[eidx].eidx;
3275 if(rmap[eidx] <= nextmax)
3276 already_sorted = 0;
3277 else
3278 nextmax = rmap[eidx];
3279 }
3280 free(cmap);
3281
3282 if(already_sorted) { free(rmap); rmap = 0; }
3283 return rmap;
3284}
3285static int compar_cameo(const void *ap, const void *bp) {
3286 const struct canonical_map_element *a = (const void *)ap;
3287 const struct canonical_map_element *b = (const void *)bp;
3288 struct asn1p_type_tag_s atag, btag;
3289 arg_t *arg = cameo_arg;
3290
3291 if(asn1f_fetch_outmost_tag(arg->asn, a->expr->module, a->expr,
3292 &atag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
3293 return 1;
3294
3295 if(asn1f_fetch_outmost_tag(arg->asn, b->expr->module, b->expr,
3296 &btag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
3297 return -1;
3298
3299 if(atag.tag_class < btag.tag_class)
3300 return -1;
3301 if(atag.tag_class > btag.tag_class)
3302 return 1;
3303 if(atag.tag_value < btag.tag_value)
3304 return -1;
3305 if(atag.tag_value > btag.tag_value)
3306 return 1;
3307 return 0;
3308
3309}