blob: acc91ab7fad07b3376d1069fad32c24e4daf5d0b [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,
Lev Walkin20696a42017-10-17 21:27:33 -070081 const struct asn_TYPE_descriptor_s *type_to_encode,
82 const void *structure_to_encode, void *buffer, size_t buffer_size);
Lev Walkin6d46bc32017-09-12 21:34:00 -070083
Lev Walkin1fc5edb2017-11-06 01:58:49 -080084/*
85 * A variant of asn_encode_to_buffer() with automatically allocated buffer.
86 * RETURN VALUES:
87 * On success, returns a newly allocated (.buffer) containing the whole message.
88 * The message size is returned in (.result.encoded).
89 * On failure:
90 * (.buffer) is NULL,
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).
95 */
96typedef 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;
100asn_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);
105
Lev Walkin6d46bc32017-09-12 21:34:00 -0700106
107/*
Lev Walkin52559a22004-09-24 20:58:37 +0000108 * 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.
113 */
Lev Walkin6d46bc32017-09-12 21:34:00 -0700114typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
115 void *application_specific_key);
116
117
118/*
119 * A generic encoder for any supported transfer syntax.
120 * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
121 * RETURN VALUES:
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.
128 */
129asn_enc_rval_t asn_encode(
130 const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
131 enum asn_transfer_syntax,
132 struct asn_TYPE_descriptor_s *type_to_encode,
133 void *structure_to_encode,
134 asn_app_consume_bytes_f *callback, void *callback_key);
135
136
Lev Walkine8bbe932017-09-12 23:06:52 -0700137/*
138 * A generic decoder for any supported transfer syntax.
139 */
140asn_dec_rval_t asn_decode(
141 const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
Lev Walkin20696a42017-10-17 21:27:33 -0700142 const struct asn_TYPE_descriptor_s *type_to_decode,
Lev Walkine8bbe932017-09-12 23:06:52 -0700143 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 */
Lev Walkin20696a42017-10-17 21:27:33 -0700146);
Lev Walkine8bbe932017-09-12 23:06:52 -0700147
Lev Walkin52559a22004-09-24 20:58:37 +0000148
Lev Walkin1eded352006-07-13 11:19:01 +0000149/*
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
152 * validation.
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.
156 */
157typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
Lev Walkin20696a42017-10-17 21:27:33 -0700158 const struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
Lev Walkin1eded352006-07-13 11:19:01 +0000159 const void *structure_which_failed_ptr,
Lev Walkin3f995632017-09-26 18:27:32 -0700160 const char *error_message_format, ...) CC_PRINTFLIKE(4, 5);
Lev Walkin52559a22004-09-24 20:58:37 +0000161
Lev Walkin6d46bc32017-09-12 21:34:00 -0700162
Lev Walkin21b41ac2005-07-24 09:03:44 +0000163#ifdef __cplusplus
164}
165#endif
166
Lev Walkin1eded352006-07-13 11:19:01 +0000167#include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
168
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700169#endif /* ASN_APPLICATION_H */