blob: d75a2cef537cfcacb23adf038bafce0399c72790 [file] [log] [blame]
Lev Walkin52559a22004-09-24 20:58:37 +00001/*-
Lev Walkin07906c72017-09-13 01:36:33 -07002 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkin52559a22004-09-24 20:58:37 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
Lev Walkin1eded352006-07-13 11:19:01 +00006 * Application-level ASN.1 callbacks.
Lev Walkin52559a22004-09-24 20:58:37 +00007 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07008#ifndef ASN_APPLICATION_H
9#define ASN_APPLICATION_H
Lev Walkin52559a22004-09-24 20:58:37 +000010
Lev Walkin1eded352006-07-13 11:19:01 +000011#include "asn_system.h" /* for platform-dependent types */
12#include "asn_codecs.h" /* for ASN.1 codecs specifics */
Lev Walkin52559a22004-09-24 20:58:37 +000013
Lev Walkin21b41ac2005-07-24 09:03:44 +000014#ifdef __cplusplus
15extern "C" {
16#endif
17
Lev Walkin52559a22004-09-24 20:58:37 +000018/*
Lev Walkin6d46bc32017-09-12 21:34:00 -070019 * A selection of ASN.1 Transfer Syntaxes to use with generalized
Lev Walkin07906c72017-09-13 01:36:33 -070020 * encoders and decoders declared further in this .h file.
Lev Walkin6d46bc32017-09-12 21:34:00 -070021 */
22enum asn_transfer_syntax {
23 /* Avoid appearance of a default transfer syntax. */
24 ATS_INVALID = 0,
Lev Walkina5972be2017-09-29 23:15:58 -070025 /* Plaintext output (not conforming to any standard), for debugging. */
Lev Walkin6d46bc32017-09-12 21:34:00 -070026 ATS_NONSTANDARD_PLAINTEXT,
Lev Walkina5972be2017-09-29 23:15:58 -070027 /* Returns a randomly generatede structure. */
28 ATS_RANDOM,
Lev Walkin6d46bc32017-09-12 21:34:00 -070029 /*
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,
Lev Walkindfbff612017-09-13 21:07:00 +000060 ATS_CANONICAL_XER
Lev Walkin6d46bc32017-09-12 21:34:00 -070061};
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 */
78asn_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 struct asn_TYPE_descriptor_s *type_to_encode,
82 void *structure_to_encode,
83 void *buffer, size_t buffer_size);
84
85
86/*
Lev Walkin52559a22004-09-24 20:58:37 +000087 * Generic type of an application-defined callback to return various
88 * types of data to the application.
89 * EXPECTED RETURN VALUES:
90 * -1: Failed to consume bytes. Abort the mission.
91 * Non-negative return values indicate success, and ignored.
92 */
Lev Walkin6d46bc32017-09-12 21:34:00 -070093typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
94 void *application_specific_key);
95
96
97/*
98 * A generic encoder for any supported transfer syntax.
99 * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
100 * RETURN VALUES:
101 * The negative (.encoded) field of the return values is accompanied with the
102 * following error codes (errno):
103 * EINVAL: Incorrect parameters to the function, such as NULLs.
104 * ENOENT: Encoding transfer syntax is not defined (for this type).
105 * EBADF: The structure has invalid form or content constraint failed.
106 * EIO: The (callback) has returned negative value during encoding.
107 */
108asn_enc_rval_t asn_encode(
109 const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
110 enum asn_transfer_syntax,
111 struct asn_TYPE_descriptor_s *type_to_encode,
112 void *structure_to_encode,
113 asn_app_consume_bytes_f *callback, void *callback_key);
114
115
Lev Walkine8bbe932017-09-12 23:06:52 -0700116/*
117 * A generic decoder for any supported transfer syntax.
118 */
119asn_dec_rval_t asn_decode(
120 const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
121 struct asn_TYPE_descriptor_s *type_to_decode,
122 void **structure_ptr, /* Pointer to a target structure's pointer */
123 const void *buffer, /* Data to be decoded */
124 size_t size /* Size of that buffer */
125 );
126
Lev Walkin52559a22004-09-24 20:58:37 +0000127
Lev Walkin1eded352006-07-13 11:19:01 +0000128/*
129 * A callback of this type is called whenever constraint validation fails
130 * on some ASN.1 type. See "constraints.h" for more details on constraint
131 * validation.
132 * This callback specifies a descriptor of the ASN.1 type which failed
133 * the constraint check, as well as human readable message on what
134 * particular constraint has failed.
135 */
136typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
137 struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
138 const void *structure_which_failed_ptr,
Lev Walkin3f995632017-09-26 18:27:32 -0700139 const char *error_message_format, ...) CC_PRINTFLIKE(4, 5);
Lev Walkin52559a22004-09-24 20:58:37 +0000140
Lev Walkin6d46bc32017-09-12 21:34:00 -0700141
Lev Walkin21b41ac2005-07-24 09:03:44 +0000142#ifdef __cplusplus
143}
144#endif
145
Lev Walkin1eded352006-07-13 11:19:01 +0000146#include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
147
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700148#endif /* ASN_APPLICATION_H */