blob: a125562c37d4dd83e526bdad3f3627f00f6490c0 [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
84
85/*
Lev Walkin52559a22004-09-24 20:58:37 +000086 * Generic type of an application-defined callback to return various
87 * types of data to the application.
88 * EXPECTED RETURN VALUES:
89 * -1: Failed to consume bytes. Abort the mission.
90 * Non-negative return values indicate success, and ignored.
91 */
Lev Walkin6d46bc32017-09-12 21:34:00 -070092typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
93 void *application_specific_key);
94
95
96/*
97 * A generic encoder for any supported transfer syntax.
98 * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
99 * RETURN VALUES:
100 * The negative (.encoded) field of the return values is accompanied with the
101 * following error codes (errno):
102 * EINVAL: Incorrect parameters to the function, such as NULLs.
103 * ENOENT: Encoding transfer syntax is not defined (for this type).
104 * EBADF: The structure has invalid form or content constraint failed.
105 * EIO: The (callback) has returned negative value during encoding.
106 */
107asn_enc_rval_t asn_encode(
108 const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
109 enum asn_transfer_syntax,
110 struct asn_TYPE_descriptor_s *type_to_encode,
111 void *structure_to_encode,
112 asn_app_consume_bytes_f *callback, void *callback_key);
113
114
Lev Walkine8bbe932017-09-12 23:06:52 -0700115/*
116 * A generic decoder for any supported transfer syntax.
117 */
118asn_dec_rval_t asn_decode(
119 const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
Lev Walkin20696a42017-10-17 21:27:33 -0700120 const struct asn_TYPE_descriptor_s *type_to_decode,
Lev Walkine8bbe932017-09-12 23:06:52 -0700121 void **structure_ptr, /* Pointer to a target structure's pointer */
122 const void *buffer, /* Data to be decoded */
123 size_t size /* Size of that buffer */
Lev Walkin20696a42017-10-17 21:27:33 -0700124);
Lev Walkine8bbe932017-09-12 23:06:52 -0700125
Lev Walkin52559a22004-09-24 20:58:37 +0000126
Lev Walkin1eded352006-07-13 11:19:01 +0000127/*
128 * A callback of this type is called whenever constraint validation fails
129 * on some ASN.1 type. See "constraints.h" for more details on constraint
130 * validation.
131 * This callback specifies a descriptor of the ASN.1 type which failed
132 * the constraint check, as well as human readable message on what
133 * particular constraint has failed.
134 */
135typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
Lev Walkin20696a42017-10-17 21:27:33 -0700136 const struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
Lev Walkin1eded352006-07-13 11:19:01 +0000137 const void *structure_which_failed_ptr,
Lev Walkin3f995632017-09-26 18:27:32 -0700138 const char *error_message_format, ...) CC_PRINTFLIKE(4, 5);
Lev Walkin52559a22004-09-24 20:58:37 +0000139
Lev Walkin6d46bc32017-09-12 21:34:00 -0700140
Lev Walkin21b41ac2005-07-24 09:03:44 +0000141#ifdef __cplusplus
142}
143#endif
144
Lev Walkin1eded352006-07-13 11:19:01 +0000145#include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
146
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700147#endif /* ASN_APPLICATION_H */