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