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