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 generatede 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,
56 * XER: XML Encoding Rules.
57 * CANONICAL-XER is a more strict variant of BASIC-XER.
64 * A generic encoder for any supported transfer syntax.
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.
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);
85 * A variant of asn_encode_to_buffer() with automatically allocated buffer.
87 * On success, returns a newly allocated (.buffer) containing the whole message.
88 * The message size is returned in (.result.encoded).
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).
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);
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.
114 typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
115 void *application_specific_key);
119 * A generic encoder for any supported transfer syntax.
120 * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
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.
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);
138 * A generic decoder for any supported transfer syntax.
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 */
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
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.
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);
167 #include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
169 #endif /* ASN_APPLICATION_H */