c4af0e582a9370837c08154218bd70df0167504b
[ric-plt/resource-status-manager.git] / RSM / asn1codec / e2ap_engine / asn_application.h
1
2 /*-
3  * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 /*
7  * Application-level ASN.1 callbacks.
8  */
9 #ifndef ASN_APPLICATION_H
10 #define ASN_APPLICATION_H
11
12 #include "asn_system.h"         /* for platform-dependent types */
13 #include "asn_codecs.h"         /* for ASN.1 codecs specifics */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /*
20  * A selection of ASN.1 Transfer Syntaxes to use with generalized
21  * encoders and decoders declared further in this .h file.
22  */
23 enum asn_transfer_syntax {
24     /* Avoid appearance of a default transfer syntax. */
25     ATS_INVALID = 0,
26     /* Plaintext output (not conforming to any standard), for debugging. */
27     ATS_NONSTANDARD_PLAINTEXT,
28     /* Returns a randomly generated structure. */
29     ATS_RANDOM,
30     /*
31      * X.690:
32      * BER: Basic Encoding Rules.
33      * DER: Distinguished Encoding Rules.
34      * CER: Canonical Encoding Rules.
35      * DER and CER are more strict variants of BER.
36      */
37     ATS_BER,
38     ATS_DER,
39     ATS_CER, /* Only decoding is supported */
40     /*
41      * X.696:
42      * OER: Octet Encoding Rules.
43      * CANONICAL-OER is a more strict variant of BASIC-OER.
44      */
45     ATS_BASIC_OER,
46     ATS_CANONICAL_OER,
47     /*
48      * X.691:
49      * PER: Packed Encoding Rules.
50      * CANONICAL-PER is a more strict variant of BASIC-PER.
51      * NOTE: Produces or consumes a complete encoding (X.691 (08/2015) #11.1).
52      */
53     ATS_UNALIGNED_BASIC_PER,
54     ATS_UNALIGNED_CANONICAL_PER,
55     ATS_ALIGNED_BASIC_PER,
56     ATS_ALIGNED_CANONICAL_PER,
57     /*
58      * X.693:
59      * XER: XML Encoding Rules.
60      * CANONICAL-XER is a more strict variant of BASIC-XER.
61      */
62     ATS_BASIC_XER,
63     ATS_CANONICAL_XER
64 };
65
66 /*
67  * A generic encoder for any supported transfer syntax.
68  * RETURN VALUES:
69  * The (.encoded) field of the return value is REDEFINED to mean the following:
70  * >=0: The computed size of the encoded data. Can exceed the (buffer_size).
71  *  -1: Error encoding the structure. See the error code in (errno):
72  *      EINVAL: Incorrect parameters to the function, such as NULLs.
73  *      ENOENT: Encoding transfer syntax is not defined (for this type).
74  *      EBADF:  The structure has invalid form or content constraint failed.
75  *      The (.failed_type) and (.structure_ptr) MIGHT be set to the appropriate
76  *      values at the place of failure, if at all possible.
77  * WARNING: The (.encoded) field of the return value can exceed the buffer_size.
78  * This is similar to snprintf(3) contract which might return values
79  * greater than the buffer size.
80  */
81 asn_enc_rval_t asn_encode_to_buffer(
82     const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
83     enum asn_transfer_syntax,
84     const struct asn_TYPE_descriptor_s *type_to_encode,
85     const void *structure_to_encode, void *buffer, size_t buffer_size);
86
87 /*
88  * A variant of asn_encode_to_buffer() with automatically allocated buffer.
89  * RETURN VALUES:
90  * On success, returns a newly allocated (.buffer) containing the whole message.
91  * The message size is returned in (.result.encoded).
92  * On failure:
93  *  (.buffer) is NULL,
94  *  (.result.encoded) as in asn_encode_to_buffer(),
95  *  The errno codes as in asn_encode_to_buffer(), plus the following:
96  *      ENOMEM: Memory allocation failed due to system or internal limits.
97  * The user is responsible for freeing the (.buffer).
98  */
99 typedef struct asn_encode_to_new_buffer_result_s {
100     void *buffer;   /* NULL if failed to encode. */
101     asn_enc_rval_t result;
102 } asn_encode_to_new_buffer_result_t;
103 asn_encode_to_new_buffer_result_t asn_encode_to_new_buffer(
104     const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
105     enum asn_transfer_syntax,
106     const struct asn_TYPE_descriptor_s *type_to_encode,
107     const void *structure_to_encode);
108
109
110 /*
111  * Generic type of an application-defined callback to return various
112  * types of data to the application.
113  * EXPECTED RETURN VALUES:
114  *  -1: Failed to consume bytes. Abort the mission.
115  * Non-negative return values indicate success, and ignored.
116  */
117 typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
118                                      void *application_specific_key);
119
120
121 /*
122  * A generic encoder for any supported transfer syntax.
123  * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
124  * RETURN VALUES:
125  * The negative (.encoded) field of the return values is accompanied with the
126  * following error codes (errno):
127  *      EINVAL: Incorrect parameters to the function, such as NULLs.
128  *      ENOENT: Encoding transfer syntax is not defined (for this type).
129  *      EBADF:  The structure has invalid form or content constraint failed.
130  *      EIO:    The (callback) has returned negative value during encoding.
131  */
132 asn_enc_rval_t asn_encode(
133     const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
134     enum asn_transfer_syntax,
135     const struct asn_TYPE_descriptor_s *type_to_encode,
136     const void *structure_to_encode,
137     asn_app_consume_bytes_f *callback, void *callback_key);
138
139
140 /*
141  * A generic decoder for any supported transfer syntax.
142  */
143 asn_dec_rval_t asn_decode(
144     const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
145     const struct asn_TYPE_descriptor_s *type_to_decode,
146     void **structure_ptr, /* Pointer to a target structure's pointer */
147     const void *buffer,   /* Data to be decoded */
148     size_t size           /* Size of that buffer */
149 );
150
151
152 /*
153  * A callback of this type is called whenever constraint validation fails
154  * on some ASN.1 type. See "constraints.h" for more details on constraint
155  * validation.
156  * This callback specifies a descriptor of the ASN.1 type which failed
157  * the constraint check, as well as human readable message on what
158  * particular constraint has failed.
159  */
160 typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
161         const struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
162         const void *structure_which_failed_ptr,
163         const char *error_message_format, ...) CC_PRINTFLIKE(4, 5);
164
165
166 #ifdef __cplusplus
167 }
168 #endif
169
170 #include "constr_TYPE.h"        /* for asn_TYPE_descriptor_t */
171
172 #endif  /* ASN_APPLICATION_H */