blob: ba7063cb25e4d7ffe0567beb5d7c9e5fa0ce9034 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*
2 * A collection of data members of unspecified types.
3 */
4#ifndef ASN1_PARSER_EXPR_H
5#define ASN1_PARSER_EXPR_H
6
7/*
8 * Meta type of the ASN expression.
9 */
10typedef enum asn1p_expr_meta {
11 AMT_INVALID,
12 AMT_TYPE, /* Type1 ::= INTEGER */
13 AMT_TYPEREF, /* Type2 ::= Type1 */
Lev Walkinf15320b2004-06-03 03:38:44 +000014 AMT_VALUE, /* value1 Type1 ::= 1 */
15 AMT_VALUESET, /* ValueSet Type1 ::= { value1 } */
Lev Walkin9c2285a2006-03-09 08:49:26 +000016 AMT_OBJECT, /* object CLASS ::= {...} */
17 AMT_OBJECTCLASS, /* FUNCTION ::= CLASS {...} */
Lev Walkinf15320b2004-06-03 03:38:44 +000018 AMT_OBJECTFIELD, /* ... */
19 AMT_EXPR_META_MAX
20} asn1p_expr_meta_e;
21
22/*
23 * ASN type of the expression.
24 */
25typedef enum asn1p_expr_type {
26 /*
27 * Internal types.
28 */
29 A1TC_INVALID, /* Invalid type */
30 A1TC_REFERENCE, /* Reference to the type defined elsewhere */
31 A1TC_EXPORTVAR, /* We're exporting this definition */
32 A1TC_UNIVERVAL, /* A value of an ENUMERATED, INTEGER or BS */
Lev Walkinf15320b2004-06-03 03:38:44 +000033 A1TC_BITVECTOR, /* A plain collection of bits */
34 A1TC_OPAQUE, /* Opaque data encoded as a bitvector */
35 A1TC_EXTENSIBLE, /* An extension marker "..." */
Lev Walkin070a52d2004-08-22 03:19:54 +000036 A1TC_COMPONENTS_OF, /* COMPONENTS OF clause */
Lev Walkinf15320b2004-06-03 03:38:44 +000037 A1TC_VALUESET, /* Value set definition */
38 A1TC_CLASSDEF, /* Information Object Class */
Lev Walkinf15320b2004-06-03 03:38:44 +000039 A1TC_INSTANCE, /* Instance of Object Class */
Lev Walkin9c2285a2006-03-09 08:49:26 +000040
41 /*
42 * ASN.1 Class field types
43 */
44#define ASN_CLASSFIELD_MASK 0x10 /* Every class field type */
45 A1TC_CLASSFIELD_TFS = ASN_CLASSFIELD_MASK, /* TypeFieldSpec */
46 A1TC_CLASSFIELD_FTVFS, /* FixedTypeValueFieldSpec */
47 A1TC_CLASSFIELD_VTVFS, /* VariableTypeValueFieldSpec */
48 A1TC_CLASSFIELD_FTVSFS, /* FixedTypeValueSetFieldSpec */
49 A1TC_CLASSFIELD_VTVSFS, /* VariableTypeValueSetFieldSpec */
50 A1TC_CLASSFIELD_OFS, /* ObjectFieldSpec */
51 A1TC_CLASSFIELD_OSFS, /* ObjectSetFieldSpec */
Lev Walkin070a52d2004-08-22 03:19:54 +000052
Lev Walkinf15320b2004-06-03 03:38:44 +000053 /*
54 * ASN.1 Constructed types
55 */
Lev Walkin9c2285a2006-03-09 08:49:26 +000056#define ASN_CONSTR_MASK 0x20 /* Every constructed type */
Lev Walkinf15320b2004-06-03 03:38:44 +000057 ASN_CONSTR_SEQUENCE = ASN_CONSTR_MASK, /* SEQUENCE */
58 ASN_CONSTR_CHOICE, /* CHOICE */
59 ASN_CONSTR_SET, /* SET */
60 ASN_CONSTR_SEQUENCE_OF, /* SEQUENCE OF */
61 ASN_CONSTR_SET_OF, /* SET OF */
Lev Walkinf6853ce2017-08-11 00:50:27 -070062 ASN_CONSTR_OPEN_TYPE,
Lev Walkine422e682004-08-25 01:58:59 +000063
Lev Walkinf15320b2004-06-03 03:38:44 +000064 /*
65 * ASN.1 Basic types
66 */
Lev Walkin9c2285a2006-03-09 08:49:26 +000067#define ASN_BASIC_MASK 0x40 /* Every basic type */
Lev Walkin609ccbb2004-09-04 04:49:21 +000068 ASN_TYPE_ANY = ASN_BASIC_MASK, /* ANY (deprecated) */
69 ASN_BASIC_BOOLEAN,
Lev Walkinf15320b2004-06-03 03:38:44 +000070 ASN_BASIC_NULL,
71 ASN_BASIC_INTEGER,
72 ASN_BASIC_REAL,
73 ASN_BASIC_ENUMERATED,
74 ASN_BASIC_BIT_STRING,
75 ASN_BASIC_OCTET_STRING,
76 ASN_BASIC_OBJECT_IDENTIFIER,
77 ASN_BASIC_RELATIVE_OID,
78 ASN_BASIC_EXTERNAL,
79 ASN_BASIC_EMBEDDED_PDV,
80 ASN_BASIC_CHARACTER_STRING,
81 ASN_BASIC_UTCTime,
82 ASN_BASIC_GeneralizedTime,
Lev Walkin070a52d2004-08-22 03:19:54 +000083
Lev Walkinf15320b2004-06-03 03:38:44 +000084 /*
85 * ASN.1 String types
86 */
Lev Walkin9c2285a2006-03-09 08:49:26 +000087#define ASN_STRING_KM_MASK 0x100 /* Known multiplier */
88#define ASN_STRING_NKM_MASK 0x200 /* Not a known multiplier */
89#define ASN_STRING_MASK 0x300 /* Every restricted string type */
Lev Walkine422e682004-08-25 01:58:59 +000090 ASN_STRING_IA5String = ASN_STRING_KM_MASK,
91 ASN_STRING_PrintableString,
92 ASN_STRING_VisibleString,
93 ASN_STRING_ISO646String, /* aka VisibleString */
94 ASN_STRING_NumericString,
95 ASN_STRING_UniversalString,
96 ASN_STRING_BMPString,
97 ASN_STRING_UTF8String = ASN_STRING_NKM_MASK,
Lev Walkinf15320b2004-06-03 03:38:44 +000098 ASN_STRING_GeneralString,
99 ASN_STRING_GraphicString,
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 ASN_STRING_TeletexString,
101 ASN_STRING_T61String,
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 ASN_STRING_VideotexString,
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 ASN_STRING_ObjectDescriptor,
104 ASN_EXPR_TYPE_MAX
105} asn1p_expr_type_e;
106
107#include "asn1p_expr_str.h"
108#include "asn1p_expr2uclass.h"
109
Lev Walkin070a52d2004-08-22 03:19:54 +0000110struct asn1p_module_s; /* Forward declaration */
Lev Walkin4dcf8362017-08-07 20:10:05 -0700111struct asn1p_ioc_table_s; /* Forward declaration */
Lev Walkin070a52d2004-08-22 03:19:54 +0000112
Lev Walkinf15320b2004-06-03 03:38:44 +0000113/*
114 * A named collection of types.
115 */
116typedef struct asn1p_expr_s {
117
118 /*
119 * Human readable name.
120 */
121 char *Identifier;
122
123 /*
124 * Meta type of the expression (type, value, value set, etc).
125 */
126 asn1p_expr_meta_e meta_type;
127
128 /*
129 * ASN type of the expression.
130 */
131 asn1p_expr_type_e expr_type;
132
133 /*
Lev Walkinc8092cb2005-02-18 16:34:21 +0000134 * Referenced type, defined elsewhere.
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 * (If expr_type == A1TC_REFERENCE)
136 */
137 asn1p_ref_t *reference;
138
139 /*
140 * Constraints for the type.
141 */
142 asn1p_constraint_t *constraints;
143
144 /*
Lev Walkinf59d0752004-08-18 04:59:12 +0000145 * This field is holding the transformed constraints, with all the
146 * parent constraints taken into account.
147 */
148 asn1p_constraint_t *combined_constraints;
149
150 /*
Lev Walkina00d6b32006-03-21 03:40:38 +0000151 * Left hand side parameters for parametrized type declaration
152 * Type{Param1, Param2} ::= SEQUENCE { a Param1, b Param2 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 */
Lev Walkina00d6b32006-03-21 03:40:38 +0000154 asn1p_paramlist_t *lhs_params;
155 /*
156 * Right hand type specialization.
157 * Type2 ::= Type{Param1}
158 */
159 struct asn1p_expr_s *rhs_pspecs; /* ->members */
160 /*
161 * If lhs_params is defined, this structure represents all possible
162 * specializations of the parent expression.
163 */
164 struct {
165 struct asn1p_pspec_s {
166 struct asn1p_expr_s *rhs_pspecs;
167 struct asn1p_expr_s *my_clone;
168 } *pspec;
169 int pspecs_count; /* Number of specializations */
170 } specializations;
171 int spec_index; /* -1, or 0-based specialization index in the parent */
Lev Walkinf15320b2004-06-03 03:38:44 +0000172
173 /*
174 * The actual value (DefinedValue or inlined value).
175 */
176 asn1p_value_t *value;
177
178 /*
Lev Walkin4cd58bd2005-06-02 17:40:32 +0000179 * The WITH SYNTAX clause.
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 */
181 asn1p_wsyntx_t *with_syntax;
182
Lev Walkin4dcf8362017-08-07 20:10:05 -0700183 /* Information Object Class table, specific for a class or object set */
184 struct asn1p_ioc_table_s *ioc_table;
Lev Walkind370e9f2006-03-16 10:03:35 +0000185
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 /*
187 * A tag.
188 */
189 struct asn1p_type_tag_s {
190 enum {
191 TC_NOCLASS,
192 TC_UNIVERSAL,
193 TC_APPLICATION,
194 TC_CONTEXT_SPECIFIC,
195 TC_PRIVATE,
196 } tag_class;
197 enum {
198 TM_DEFAULT,
199 TM_IMPLICIT,
200 TM_EXPLICIT,
201 } tag_mode;
Lev Walkind21c5052004-09-29 13:18:09 +0000202 asn1c_integer_t tag_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 } tag;
204
Lev Walkin5498f2d2004-09-15 11:59:30 +0000205 struct asn1p_expr_marker_s {
206 enum asn1p_expr_marker_e {
Lev Walkinb85a8132005-08-18 13:38:19 +0000207 EM_NOMARK,
208 EM_INDIRECT = 0x01, /* 00001 Represent as pointer */
Lev Walkin70853052005-11-26 11:21:55 +0000209 EM_OMITABLE = 0x02, /* 00010 May be absent in encoding */
Lev Walkinb85a8132005-08-18 13:38:19 +0000210 EM_OPTIONAL = 0x07, /* 00111 Optional member */
211 EM_DEFAULT = 0x0F, /* 01111 default_value */
212 EM_UNRECURSE = 0x10, /* 10000 Use safe naming */
Lev Walkin5498f2d2004-09-15 11:59:30 +0000213 } flags;
214 asn1p_value_t *default_value; /* For EM_DEFAULT case */
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000215 } marker;
216 int unique; /* UNIQUE */
Bi-Ruei, Chiub9adfc52016-11-09 00:17:25 +0800217 int ref_cnt; /* reference count */
Lev Walkin6e8da2b2004-09-10 08:21:27 +0000218
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 /*
Lev Walkin21d00002005-03-04 08:48:53 +0000220 * Whether automatic tagging may be applied for subtypes.
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 */
222 int auto_tags_OK;
223
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 /*
225 * Members of the constructed type.
226 */
227 TQ_HEAD(struct asn1p_expr_s) members;
228
229 /*
230 * Next expression in the list.
231 */
232 TQ_ENTRY(struct asn1p_expr_s) next;
233
Lev Walkin1004aa92004-09-08 00:28:11 +0000234 struct asn1p_expr_s *parent_expr; /* optional */
235
236 struct asn1p_module_s *module; /* Defined in module */
237
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 /*
239 * Line number where this structure is defined in the original
240 * grammar source.
241 */
242 int _lineno;
Lev Walkin070a52d2004-08-22 03:19:54 +0000243
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 /*
245 * Marks are used for various purposes.
246 * Here are some predefined ones.
247 */
248 enum {
Lev Walkin725883b2006-10-09 12:07:58 +0000249 TM_NOMARK = 0,
250 TM_RECURSION = (1<<0), /* Used to break recursion */
251 TM_BROKEN = (1<<1), /* A warning was already issued */
252 TM_PERFROMCT = (1<<2), /* PER FROM() constraint tables emitted */
Lev Walkin5efafc52017-09-05 03:43:00 -0700253 TM_NAMECLASH = (1<<3), /* Name clash found, need to add module name to resolve */
Bi-Ruei, Chiudcc822a2017-10-19 01:06:57 +0800254 TM_NAMEGIVEN = (1<<4), /* The expression has already yielded a name */
255 TM_SKIPinUNION = (1<<5) /* Do not include this identifier in union again due to name duplication,
256 especially for OPENTYPE. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 } _mark;
258
Lev Walkin21d00002005-03-04 08:48:53 +0000259 /*
260 * Some tags used by the compiler.
261 */
262 int _anonymous_type; /* This type is unnamed */
263 int _type_unique_index; /* A per top-level-type unique index */
Lev Walkin70853052005-11-26 11:21:55 +0000264 int _type_referenced; /* This type is referenced from another place */
Lev Walkin070a52d2004-08-22 03:19:54 +0000265
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 /*
267 * Opaque data may be attached to this structure,
268 * probably by compiler.
269 */
270 void *data;
271 void (*data_free)(void *data);
272} asn1p_expr_t;
273
274
275/*
276 * Constructor and destructor.
277 */
Lev Walkina9532f42006-09-17 04:52:50 +0000278asn1p_expr_t *asn1p_expr_new(int _lineno, struct asn1p_module_s *);
Lev Walkin070a52d2004-08-22 03:19:54 +0000279asn1p_expr_t *asn1p_expr_clone(asn1p_expr_t *, int skip_extensions);
Lev Walkina00d6b32006-03-21 03:40:38 +0000280asn1p_expr_t *asn1p_expr_clone_with_resolver(asn1p_expr_t *,
281 asn1p_expr_t *(*resolver)(asn1p_expr_t *to_resolve, void *resolver_arg),
282 void *resolver_arg);
Lev Walkin1004aa92004-09-08 00:28:11 +0000283void asn1p_expr_add(asn1p_expr_t *to, asn1p_expr_t *what);
Lev Walkin0e90aa02013-03-19 16:17:13 -0700284void asn1p_expr_add_many(asn1p_expr_t *to, asn1p_expr_t *from_what);
Bi-Ruei, Chiudcc822a2017-10-19 01:06:57 +0800285asn1p_expr_t *asn1p_lookup_child(asn1p_expr_t *tc, const char *name);
Lev Walkinea6635b2017-08-06 23:23:04 -0700286int asn1p_expr_compare(const asn1p_expr_t *, const asn1p_expr_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +0000287void asn1p_expr_free(asn1p_expr_t *expr);
Lev Walkinea6635b2017-08-06 23:23:04 -0700288void asn1p_expr_set_source(asn1p_expr_t *, asn1p_module_t *, int lineno);
289
Lev Walkinc0e03b92017-08-22 01:48:23 -0700290asn1p_paramlist_t *asn1p_get_namespace(asn1p_expr_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +0000291
Lev Walkin71160962005-06-02 05:21:53 +0000292#define TAG2STRING_BUFFER_SIZE 64 /* buf should be at least this big */
Lev Walkina45518f2017-08-23 07:22:52 -0700293const char *asn1p_tag2string(const struct asn1p_type_tag_s *tag, char *opt_buf);
Lev Walkin71160962005-06-02 05:21:53 +0000294
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800295#define MODULE_NAME_OF(expr) \
296 ((!expr) ? "" : \
297 (!(expr->_mark & TM_NAMECLASH) ? "" : \
298 (!expr->module ? "" : expr->module->ModuleName))), \
299 ((!expr) ? "" : \
300 (!(expr->_mark & TM_NAMECLASH) ? "" : "_"))
301
Lev Walkinf15320b2004-06-03 03:38:44 +0000302#endif /* ASN1_PARSER_EXPR_H */