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