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