3 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
7 * Application-level ASN.1 callbacks.
9 #ifndef ASN_APPLICATION_H
10 #define ASN_APPLICATION_H
12 #include "asn_system.h" /* for platform-dependent types */
13 #include "asn_codecs.h" /* for ASN.1 codecs specifics */
20 * A selection of ASN.1 Transfer Syntaxes to use with generalized
21 * encoders and decoders declared further in this .h file.
23 enum asn_transfer_syntax {
24 /* Avoid appearance of a default transfer syntax. */
26 /* Plaintext output (not conforming to any standard), for debugging. */
27 ATS_NONSTANDARD_PLAINTEXT,
28 /* Returns a randomly generated structure. */
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.
39 ATS_CER, /* Only decoding is supported */
42 * OER: Octet Encoding Rules.
43 * CANONICAL-OER is a more strict variant of BASIC-OER.
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).
53 ATS_UNALIGNED_BASIC_PER,
54 ATS_UNALIGNED_CANONICAL_PER,
55 ATS_ALIGNED_BASIC_PER,
56 ATS_ALIGNED_CANONICAL_PER,
59 * XER: XML Encoding Rules.
60 * CANONICAL-XER is a more strict variant of BASIC-XER.
67 * A generic encoder for any supported transfer syntax.
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.
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);
88 * A variant of asn_encode_to_buffer() with automatically allocated buffer.
90 * On success, returns a newly allocated (.buffer) containing the whole message.
91 * The message size is returned in (.result.encoded).
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).
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);
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.
117 typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
118 void *application_specific_key);
122 * A generic encoder for any supported transfer syntax.
123 * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
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.
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);
141 * A generic decoder for any supported transfer syntax.
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 */
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
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.
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);
170 #include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
172 #endif /* ASN_APPLICATION_H */