2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
6 * This file contains the declaration structure called "ASN.1 Type Definition",
7 * which holds all information necessary for encoding and decoding routines.
8 * This structure even contains pointer to these encoding and decoding routines
9 * for each defined ASN.1 type.
11 #ifndef _CONSTR_TYPE_H_
12 #define _CONSTR_TYPE_H_
14 #include <ber_tlv_length.h>
15 #include <ber_tlv_tag.h>
21 struct asn_TYPE_descriptor_s; /* Forward declaration */
22 struct asn_TYPE_member_s; /* Forward declaration */
25 * This type provides the context information for various ASN.1 routines,
26 * primarily ones doing decoding. A member _asn_ctx of this type must be
27 * included into certain target language's structures, such as compound types.
29 typedef struct asn_struct_ctx_s {
30 short phase; /* Decoding phase */
31 short step; /* Elementary step of a phase */
32 int context; /* Other context information */
33 void *ptr; /* Decoder-specific stuff (stack elements) */
34 ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
37 #if !defined(ASN_DISABLE_BER_SUPPORT)
38 #include <ber_decoder.h> /* Basic Encoding Rules decoder */
39 #include <der_encoder.h> /* Distinguished Encoding Rules encoder */
41 typedef void (ber_type_decoder_f)(void);
42 typedef void (der_type_encoder_f)(void);
43 #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
45 #if !defined(ASN_DISABLE_XER_SUPPORT)
46 #include <xer_decoder.h> /* Decoder of XER (XML, text) */
47 #include <xer_encoder.h> /* Encoder into XER (XML, text) */
49 typedef void (xer_type_decoder_f)(void);
50 typedef void (xer_type_encoder_f)(void);
51 #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
53 #if !defined(ASN_DISABLE_JER_SUPPORT)
54 #include <jer_encoder.h> /* Encoder into JER (JSON, text) */
56 typedef void (jer_type_encoder_f)(void);
57 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
59 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
60 #include <per_decoder.h> /* Packet Encoding Rules decoder */
61 #include <per_encoder.h> /* Packet Encoding Rules encoder */
63 typedef void (per_type_decoder_f)(void);
64 typedef void (per_type_encoder_f)(void);
65 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
67 #include <constraints.h> /* Subtype constraints support */
69 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
70 #include <asn_random_fill.h> /* Random structures support */
72 typedef void (asn_random_fill_f)(void);
73 #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
75 #if !defined(ASN_DISABLE_OER_SUPPORT)
76 #include <oer_decoder.h> /* Octet Encoding Rules encoder */
77 #include <oer_encoder.h> /* Octet Encoding Rules encoder */
79 typedef void (oer_type_decoder_f)(void);
80 typedef void (oer_type_encoder_f)(void);
81 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
84 * Free the structure according to its specification.
85 * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead.
86 * Do not use directly.
88 enum asn_struct_free_method {
89 ASFM_FREE_EVERYTHING, /* free(struct_ptr) and underlying members */
90 ASFM_FREE_UNDERLYING, /* free underlying members */
91 ASFM_FREE_UNDERLYING_AND_RESET /* FREE_UNDERLYING + memset(0) */
93 typedef void (asn_struct_free_f)(
94 const struct asn_TYPE_descriptor_s *type_descriptor,
95 void *struct_ptr, enum asn_struct_free_method);
98 * Free the structure including freeing the memory pointed to by ptr itself.
100 #define ASN_STRUCT_FREE(asn_DEF, ptr) \
101 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING)
104 * Free the memory used by the members of the structure without freeing the
105 * the structure pointer itself.
106 * ZERO-OUT the structure to the safe clean state.
107 * (Retaining the pointer may be useful in case the structure is allocated
108 * statically or arranged on the stack, yet its elements are dynamic.)
110 #define ASN_STRUCT_RESET(asn_DEF, ptr) \
111 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET)
114 * Free memory used by the members of the structure without freeing
115 * the structure pointer itself.
116 * (Retaining the pointer may be useful in case the structure is allocated
117 * statically or arranged on the stack, yet its elements are dynamic.)
118 * AVOID using it in the application code;
119 * Use a safer ASN_STRUCT_RESET() instead.
121 #define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
122 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING)
125 * Print the structure according to its specification.
127 typedef int(asn_struct_print_f)(
128 const struct asn_TYPE_descriptor_s *type_descriptor,
129 const void *struct_ptr,
130 int level, /* Indentation level */
131 asn_app_consume_bytes_f *callback, void *app_key);
134 * Compare two structs between each other.
135 * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater",
136 * and =0 if "equal to", for some type-specific, stable definition of
137 * "smaller", "greater" and "equal to".
139 typedef int (asn_struct_compare_f)(
140 const struct asn_TYPE_descriptor_s *type_descriptor,
141 const void *struct_A,
142 const void *struct_B);
145 * Return the outmost tag of the type.
146 * If the type is untagged CHOICE, the dynamic operation is performed.
147 * NOTE: This function pointer type is only useful internally.
148 * Do not use it in your application.
150 typedef ber_tlv_tag_t (asn_outmost_tag_f)(
151 const struct asn_TYPE_descriptor_s *type_descriptor,
152 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
153 /* The instance of the above function type; used internally. */
154 asn_outmost_tag_f asn_TYPE_outmost_tag;
157 * Fetch the desired type of the Open Type based on the
158 * Information Object Set driven constraints.
160 typedef struct asn_type_selector_result_s {
161 const struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */
162 unsigned presence_index; /* Associated choice variant. */
163 } asn_type_selector_result_t;
164 typedef asn_type_selector_result_t(asn_type_selector_f)(
165 const struct asn_TYPE_descriptor_s *parent_type_descriptor,
166 const void *parent_structure_ptr);
169 * Generalized functions for dealing with the speciic type.
170 * May be directly invoked by applications.
172 typedef struct asn_TYPE_operation_s {
173 asn_struct_free_f *free_struct; /* Free the structure */
174 asn_struct_print_f *print_struct; /* Human readable output */
175 asn_struct_compare_f *compare_struct; /* Compare two structures */
176 ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
177 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
178 xer_type_decoder_f *xer_decoder; /* Generic XER decoder */
179 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
180 jer_type_encoder_f *jer_encoder; /* Generic JER encoder */
181 oer_type_decoder_f *oer_decoder; /* Generic OER decoder */
182 oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */
183 per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */
184 per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */
185 per_type_decoder_f *aper_decoder; /* Aligned PER decoder */
186 per_type_encoder_f *aper_encoder; /* Aligned PER encoder */
187 asn_random_fill_f *random_fill; /* Initialize with a random value */
188 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
189 } asn_TYPE_operation_t;
192 * A constraints tuple specifying both the OER and PER constraints.
194 typedef struct asn_encoding_constraints_s {
195 #if !defined(ASN_DISABLE_OER_SUPPORT)
196 const struct asn_oer_constraints_s *oer_constraints;
197 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
198 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
199 const struct asn_per_constraints_s *per_constraints;
200 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
201 asn_constr_check_f *general_constraints;
202 } asn_encoding_constraints_t;
205 * The definitive description of the destination language's structure.
207 typedef struct asn_TYPE_descriptor_s {
208 const char *name; /* A name of the ASN.1 type. "" in some cases. */
209 const char *xml_tag; /* Name used in XML tag */
212 * Generalized functions for dealing with the specific type.
213 * May be directly invoked by applications.
215 asn_TYPE_operation_t *op;
217 /***********************************************************************
218 * Internally useful members. Not to be used by applications directly. *
219 **********************************************************************/
222 * Tags that are expected to occur.
224 const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
225 unsigned tags_count; /* Number of tags which are expected */
226 const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */
227 unsigned all_tags_count; /* Number of tags */
229 /* OER, PER, and general constraints */
230 asn_encoding_constraints_t encoding_constraints;
233 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
235 struct asn_TYPE_member_s *elements;
236 unsigned elements_count;
239 * Additional information describing the type, used by appropriate
242 const void *specifics;
243 } asn_TYPE_descriptor_t;
246 * This type describes an element of the constructed type,
247 * i.e. SEQUENCE, SET, CHOICE, etc.
249 enum asn_TYPE_flags_e {
251 ATF_POINTER = 0x01, /* Represented by the pointer */
252 ATF_OPEN_TYPE = 0x02, /* Open Type */
253 ATF_ANY_TYPE = 0x04 /* ANY type (deprecated!) */
255 typedef struct asn_TYPE_member_s {
256 enum asn_TYPE_flags_e flags; /* Element's presentation flags */
257 unsigned optional; /* Following optional members, including current */
258 unsigned memb_offset; /* Offset of the element */
259 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
260 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
261 asn_TYPE_descriptor_t *type; /* Member type descriptor */
262 asn_type_selector_f *type_selector; /* IoS runtime type selector */
263 asn_encoding_constraints_t encoding_constraints;
264 int (*default_value_cmp)(const void *sptr); /* Compare DEFAULT <value> */
265 int (*default_value_set)(void **sptr); /* Set DEFAULT <value> */
266 const char *name; /* ASN.1 identifier of the element */
270 * BER tag to element number mapping.
272 typedef struct asn_TYPE_tag2member_s {
273 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
274 unsigned el_no; /* Index of the associated member, base 0 */
275 int toff_first; /* First occurrence of the el_tag, relative */
276 int toff_last; /* Last occurrence of the el_tag, relative */
277 } asn_TYPE_tag2member_t;
280 * This function prints out the contents of the target language's structure
281 * (struct_ptr) into the file pointer (stream) in human readable form.
283 * 0: The structure is printed.
284 * -1: Problem dumping the structure.
285 * (See also xer_fprint() in xer_encoder.h)
287 int asn_fprint(FILE *stream, /* Destination stream descriptor */
288 const asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
289 const void *struct_ptr); /* Structure to be printed */
295 #endif /* _CONSTR_TYPE_H_ */