Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / ASN1c / constr_TYPE.h
1 /*
2  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 /*
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.
10  */
11 #ifndef _CONSTR_TYPE_H_
12 #define _CONSTR_TYPE_H_
13
14 #include <ber_tlv_length.h>
15 #include <ber_tlv_tag.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 struct asn_TYPE_descriptor_s;   /* Forward declaration */
22 struct asn_TYPE_member_s;       /* Forward declaration */
23
24 /*
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.
28  */
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 */
35 } asn_struct_ctx_t;
36
37 #include <ber_decoder.h>        /* Basic Encoding Rules decoder */
38 #include <der_encoder.h>        /* Distinguished Encoding Rules encoder */
39 #include <xer_decoder.h>        /* Decoder of XER (XML, text) */
40 #include <xer_encoder.h>        /* Encoder into XER (XML, text) */
41 #include <per_decoder.h>        /* Packet Encoding Rules decoder */
42 #include <per_encoder.h>        /* Packet Encoding Rules encoder */
43 #include <constraints.h>        /* Subtype constraints support */
44 #include <asn_random_fill.h>    /* Random structures support */
45
46 #ifdef  ASN_DISABLE_OER_SUPPORT
47 typedef void (oer_type_decoder_f)(void);
48 typedef void (oer_type_encoder_f)(void);
49 typedef void asn_oer_constraints_t;
50 #else
51 #include <oer_decoder.h>        /* Octet Encoding Rules encoder */
52 #include <oer_encoder.h>        /* Octet Encoding Rules encoder */
53 #endif
54
55 /*
56  * Free the structure according to its specification.
57  * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead.
58  * Do not use directly.
59  */
60 enum asn_struct_free_method {
61     ASFM_FREE_EVERYTHING,   /* free(struct_ptr) and underlying members */
62     ASFM_FREE_UNDERLYING,   /* free underlying members */
63     ASFM_FREE_UNDERLYING_AND_RESET   /* FREE_UNDERLYING + memset(0) */
64 };
65 typedef void (asn_struct_free_f)(
66                 const struct asn_TYPE_descriptor_s *type_descriptor,
67                 void *struct_ptr, enum asn_struct_free_method);
68
69 /*
70  * Free the structure including freeing the memory pointed to by ptr itself.
71  */
72 #define ASN_STRUCT_FREE(asn_DEF, ptr) \
73     (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING)
74
75 /*
76  * Free the memory used by the members of the structure without freeing the
77  * the structure pointer itself.
78  * ZERO-OUT the structure to the safe clean state.
79  * (Retaining the pointer may be useful in case the structure is allocated
80  *  statically or arranged on the stack, yet its elements are dynamic.)
81  */
82 #define ASN_STRUCT_RESET(asn_DEF, ptr) \
83     (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET)
84
85 /*
86  * Free memory used by the members of the structure without freeing
87  * the structure pointer itself.
88  * (Retaining the pointer may be useful in case the structure is allocated
89  *  statically or arranged on the stack, yet its elements are dynamic.)
90  * AVOID using it in the application code;
91  * Use a safer ASN_STRUCT_RESET() instead.
92  */
93 #define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
94     (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING)
95
96 /*
97  * Print the structure according to its specification.
98  */
99 typedef int(asn_struct_print_f)(
100     const struct asn_TYPE_descriptor_s *type_descriptor,
101     const void *struct_ptr,
102     int level, /* Indentation level */
103     asn_app_consume_bytes_f *callback, void *app_key);
104
105 /*
106  * Compare two structs between each other.
107  * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater",
108  * and =0 if "equal to", for some type-specific, stable definition of
109  * "smaller", "greater" and "equal to".
110  */
111 typedef int (asn_struct_compare_f)(
112                 const struct asn_TYPE_descriptor_s *type_descriptor,
113                 const void *struct_A,
114                 const void *struct_B);
115
116 /*
117  * Return the outmost tag of the type.
118  * If the type is untagged CHOICE, the dynamic operation is performed.
119  * NOTE: This function pointer type is only useful internally.
120  * Do not use it in your application.
121  */
122 typedef ber_tlv_tag_t (asn_outmost_tag_f)(
123                 const struct asn_TYPE_descriptor_s *type_descriptor,
124                 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
125 /* The instance of the above function type; used internally. */
126 asn_outmost_tag_f asn_TYPE_outmost_tag;
127
128 /*
129  * Fetch the desired type of the Open Type based on the
130  * Information Object Set driven constraints.
131  */
132 typedef struct asn_type_selector_result_s {
133     const struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */
134     unsigned presence_index; /* Associated choice variant. */
135 } asn_type_selector_result_t;
136 typedef asn_type_selector_result_t(asn_type_selector_f)(
137     const struct asn_TYPE_descriptor_s *parent_type_descriptor,
138     const void *parent_structure_ptr);
139
140 /*
141  * Generalized functions for dealing with the speciic type.
142  * May be directly invoked by applications.
143  */
144 typedef struct asn_TYPE_operation_s {
145     asn_struct_free_f *free_struct;     /* Free the structure */
146     asn_struct_print_f *print_struct;   /* Human readable output */
147     asn_struct_compare_f *compare_struct; /* Compare two structures */
148     ber_type_decoder_f *ber_decoder;      /* Generic BER decoder */
149     der_type_encoder_f *der_encoder;      /* Canonical DER encoder */
150     xer_type_decoder_f *xer_decoder;      /* Generic XER decoder */
151     xer_type_encoder_f *xer_encoder;      /* [Canonical] XER encoder */
152     oer_type_decoder_f *oer_decoder;      /* Generic OER decoder */
153     oer_type_encoder_f *oer_encoder;      /* Canonical OER encoder */
154     per_type_decoder_f *uper_decoder;     /* Unaligned PER decoder */
155     per_type_encoder_f *uper_encoder;     /* Unaligned PER encoder */
156     per_type_decoder_f *aper_decoder;     /* Aligned PER decoder */
157     per_type_encoder_f *aper_encoder;     /* Aligned PER encoder */
158     asn_random_fill_f *random_fill;       /* Initialize with a random value */
159     asn_outmost_tag_f *outmost_tag;       /* <optional, internal> */
160 } asn_TYPE_operation_t;
161
162 /*
163  * A constraints tuple specifying both the OER and PER constraints.
164  */
165 typedef struct asn_encoding_constraints_s {
166     const struct asn_oer_constraints_s *oer_constraints;
167     const struct asn_per_constraints_s *per_constraints;
168     asn_constr_check_f *general_constraints;
169 } asn_encoding_constraints_t;
170
171 /*
172  * The definitive description of the destination language's structure.
173  */
174 typedef struct asn_TYPE_descriptor_s {
175     const char *name;       /* A name of the ASN.1 type. "" in some cases. */
176     const char *xml_tag;    /* Name used in XML tag */
177
178     /*
179      * Generalized functions for dealing with the specific type.
180      * May be directly invoked by applications.
181      */
182     asn_TYPE_operation_t *op;
183
184     /***********************************************************************
185      * Internally useful members. Not to be used by applications directly. *
186      **********************************************************************/
187
188     /*
189      * Tags that are expected to occur.
190      */
191     const ber_tlv_tag_t *tags;      /* Effective tags sequence for this type */
192     unsigned tags_count;            /* Number of tags which are expected */
193     const ber_tlv_tag_t *all_tags;  /* Every tag for BER/containment */
194     unsigned all_tags_count;        /* Number of tags */
195
196     /* OER, PER, and general constraints */
197     asn_encoding_constraints_t encoding_constraints;
198
199     /*
200      * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
201      */
202     struct asn_TYPE_member_s *elements;
203     unsigned elements_count;
204
205     /*
206      * Additional information describing the type, used by appropriate
207      * functions above.
208      */
209     const void *specifics;
210 } asn_TYPE_descriptor_t;
211
212 /*
213  * This type describes an element of the constructed type,
214  * i.e. SEQUENCE, SET, CHOICE, etc.
215  */
216   enum asn_TYPE_flags_e {
217     ATF_NOFLAGS,
218     ATF_POINTER = 0x01,   /* Represented by the pointer */
219     ATF_OPEN_TYPE = 0x02, /* Open Type */
220     ATF_ANY_TYPE = 0x04   /* ANY type (deprecated!) */
221   };
222 typedef struct asn_TYPE_member_s {
223     enum asn_TYPE_flags_e flags; /* Element's presentation flags */
224     unsigned optional;      /* Following optional members, including current */
225     unsigned memb_offset;   /* Offset of the element */
226     ber_tlv_tag_t tag;      /* Outmost (most immediate) tag */
227     int tag_mode;           /* IMPLICIT/no/EXPLICIT tag at current level */
228     asn_TYPE_descriptor_t *type;            /* Member type descriptor */
229     asn_type_selector_f *type_selector;     /* IoS runtime type selector */
230     asn_encoding_constraints_t encoding_constraints;
231     int (*default_value_cmp)(const void *sptr); /* Compare DEFAULT <value> */
232     int (*default_value_set)(void **sptr);      /* Set DEFAULT <value> */
233     const char *name; /* ASN.1 identifier of the element */
234 } asn_TYPE_member_t;
235
236 /*
237  * BER tag to element number mapping.
238  */
239 typedef struct asn_TYPE_tag2member_s {
240     ber_tlv_tag_t el_tag;   /* Outmost tag of the member */
241     unsigned el_no;         /* Index of the associated member, base 0 */
242     int toff_first;         /* First occurence of the el_tag, relative */
243     int toff_last;          /* Last occurence of the el_tag, relative */
244 } asn_TYPE_tag2member_t;
245
246 /*
247  * This function prints out the contents of the target language's structure
248  * (struct_ptr) into the file pointer (stream) in human readable form.
249  * RETURN VALUES:
250  *       0: The structure is printed.
251  *      -1: Problem dumping the structure.
252  * (See also xer_fprint() in xer_encoder.h)
253  */
254 int asn_fprint(FILE *stream, /* Destination stream descriptor */
255                const asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
256                const void *struct_ptr);         /* Structure to be printed */
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif  /* _CONSTR_TYPE_H_ */