blob: 7810349f146b578a616f442aab9149ec1c20e274 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin523de9e2006-08-18 01:34:18 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkin59b176e2005-11-26 11:25:14 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006/*
7 * This file contains the declaration structure called "ASN.1 Type Definition",
8 * which holds all information necessary for encoding and decoding routines.
9 * This structure even contains pointer to these encoding and decoding routines
10 * for each defined ASN.1 type.
11 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000012#ifndef _CONSTR_TYPE_H_
13#define _CONSTR_TYPE_H_
Lev Walkinf15320b2004-06-03 03:38:44 +000014
Lev Walkin1cbd2222004-09-29 13:24:10 +000015#include <ber_tlv_length.h>
16#include <ber_tlv_tag.h>
17
Lev Walkin21b41ac2005-07-24 09:03:44 +000018#ifdef __cplusplus
19extern "C" {
20#endif
21
Lev Walkin1cbd2222004-09-29 13:24:10 +000022struct asn_TYPE_descriptor_s; /* Forward declaration */
23struct asn_TYPE_member_s; /* Forward declaration */
24
25/*
26 * This type provides the context information for various ASN.1 routines,
27 * primarily ones doing decoding. A member _asn_ctx of this type must be
28 * included into certain target language's structures, such as compound types.
29 */
30typedef struct asn_struct_ctx_s {
Lev Walkin1e443962005-02-18 18:06:36 +000031 short phase; /* Decoding phase */
32 short step; /* Elementary step of a phase */
33 int context; /* Other context information */
Lev Walkin1cbd2222004-09-29 13:24:10 +000034 void *ptr; /* Decoder-specific stuff (stack elements) */
Lev Walkin1e443962005-02-18 18:06:36 +000035 ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
Lev Walkin1cbd2222004-09-29 13:24:10 +000036} asn_struct_ctx_t;
37
Lev Walkindc06f6b2004-10-20 15:50:55 +000038#include <ber_decoder.h> /* Basic Encoding Rules decoder */
39#include <der_encoder.h> /* Distinguished Encoding Rules encoder */
40#include <xer_decoder.h> /* Decoder of XER (XML, text) */
41#include <xer_encoder.h> /* Encoder into XER (XML, text) */
Lev Walkin59b176e2005-11-26 11:25:14 +000042#include <per_decoder.h> /* Packet Encoding Rules decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000043#include <per_encoder.h> /* Packet Encoding Rules encoder */
Lev Walkin69033802017-08-25 12:15:58 -070044#include <constraints.h> /* Subtype constraints support */
Lev Walkina5972be2017-09-29 23:15:58 -070045#include <asn_random_fill.h> /* Random structures support */
Lev Walkin69033802017-08-25 12:15:58 -070046
47#ifdef ASN_DISABLE_OER_SUPPORT
Lev Walkin049a2d72017-08-27 18:34:02 -070048typedef void (oer_type_decoder_f)();
49typedef void (oer_type_encoder_f)();
Lev Walkinb5cdc5d2017-09-15 21:57:46 -070050typedef void asn_oer_constraints_t;
Lev Walkin69033802017-08-25 12:15:58 -070051#else
Lev Walkincc159472017-07-06 08:26:36 -070052#include <oer_decoder.h> /* Octet Encoding Rules encoder */
53#include <oer_encoder.h> /* Octet Encoding Rules encoder */
Lev Walkin69033802017-08-25 12:15:58 -070054#endif
Lev Walkinf15320b2004-06-03 03:38:44 +000055
Lev Walkinf15320b2004-06-03 03:38:44 +000056/*
57 * Free the structure according to its specification.
Lev Walkin8d99d7b2017-08-25 01:06:00 -070058 * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead.
59 * Do not use directly.
Lev Walkinf15320b2004-06-03 03:38:44 +000060 */
Lev Walkin8d99d7b2017-08-25 01:06:00 -070061enum asn_struct_free_method {
62 ASFM_FREE_EVERYTHING, /* free(struct_ptr) and underlying members */
63 ASFM_FREE_UNDERLYING, /* free underlying members */
64 ASFM_FREE_UNDERLYING_AND_RESET /* FREE_UNDERLYING + memset(0) */
65};
Lev Walkinf15320b2004-06-03 03:38:44 +000066typedef void (asn_struct_free_f)(
Lev Walkinf6853ce2017-08-11 00:50:27 -070067 const struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkin8d99d7b2017-08-25 01:06:00 -070068 void *struct_ptr, enum asn_struct_free_method);
69
70/*
71 * Free the structure including freeing the memory pointed to by ptr itself.
72 */
73#define ASN_STRUCT_FREE(asn_DEF, ptr) \
74 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING)
75
76/*
77 * Free the memory used by the members of the structure without freeing the
78 * the structure pointer itself.
79 * ZERO-OUT the structure to the safe clean state.
80 * (Retaining the pointer may be useful in case the structure is allocated
81 * statically or arranged on the stack, yet its elements are dynamic.)
82 */
83#define ASN_STRUCT_RESET(asn_DEF, ptr) \
84 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET)
85
86/*
87 * Free memory used by the members of the structure without freeing
88 * the structure pointer itself.
89 * (Retaining the pointer may be useful in case the structure is allocated
90 * statically or arranged on the stack, yet its elements are dynamic.)
91 * AVOID using it in the application code;
92 * Use a safer ASN_STRUCT_RESET() instead.
93 */
94#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
95 (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING)
Lev Walkinf15320b2004-06-03 03:38:44 +000096
97/*
98 * Print the structure according to its specification.
99 */
100typedef int (asn_struct_print_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +0000101 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 const void *struct_ptr,
103 int level, /* Indentation level */
104 asn_app_consume_bytes_f *callback, void *app_key);
105
106/*
Lev Walkincd2f48e2017-08-10 02:14:59 -0700107 * Compare two structs between each other.
108 * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater",
109 * and =0 if "equal to", for some type-specific, stable definition of
110 * "smaller", "greater" and "equal to".
111 */
112typedef int (asn_struct_compare_f)(
113 const struct asn_TYPE_descriptor_s *type_descriptor,
114 const void *struct_A,
115 const void *struct_B);
116
117/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 * Return the outmost tag of the type.
119 * If the type is untagged CHOICE, the dynamic operation is performed.
120 * NOTE: This function pointer type is only useful internally.
121 * Do not use it in your application.
122 */
123typedef ber_tlv_tag_t (asn_outmost_tag_f)(
Wim Lewis14e6b162014-07-23 16:06:01 -0700124 const struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
Lev Walkin449f8322004-08-20 13:23:42 +0000126/* The instance of the above function type; used internally. */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000127asn_outmost_tag_f asn_TYPE_outmost_tag;
Lev Walkinf15320b2004-06-03 03:38:44 +0000128
Lev Walkin9de6cd82017-08-10 05:47:46 -0700129/*
130 * Fetch the desired type of the Open Type based on the
131 * Information Object Set driven constraints.
132 */
Lev Walkinf6853ce2017-08-11 00:50:27 -0700133typedef struct asn_type_selector_result_s {
134 struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */
135 unsigned presence_index; /* Associated choice variant. */
136} asn_type_selector_result_t;
137typedef asn_type_selector_result_t(asn_type_selector_f)(
Lev Walkin9de6cd82017-08-10 05:47:46 -0700138 const struct asn_TYPE_descriptor_s *parent_type_descriptor,
139 const void *parent_structure_ptr);
140
Lev Walkinf15320b2004-06-03 03:38:44 +0000141/*
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800142 * Generalized functions for dealing with the speciic type.
143 * May be directly invoked by applications.
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800145typedef struct asn_TYPE_operation_s {
Lev Walkina5972be2017-09-29 23:15:58 -0700146 asn_struct_free_f *free_struct; /* Free the structure */
147 asn_struct_print_f *print_struct; /* Human readable output */
148 asn_struct_compare_f *compare_struct; /* Compare two structures */
149 ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
150 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
151 xer_type_decoder_f *xer_decoder; /* Generic XER decoder */
152 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
153 oer_type_decoder_f *oer_decoder; /* Generic OER decoder */
154 oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */
155 per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */
156 per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */
157 asn_random_fill_f *random_fill; /* Initialize with a random value */
158 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800159} asn_TYPE_operation_t;
160
161/*
Lev Walkina5972be2017-09-29 23:15:58 -0700162 * A constraints tuple specifying both the OER and PER constraints.
163 */
164typedef struct asn_encoding_constraints_s {
165 const struct asn_oer_constraints_s *oer_constraints;
166 const struct asn_per_constraints_s *per_constraints;
167 asn_constr_check_f *general_constraints;
168} asn_encoding_constraints_t;
169
170/*
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800171 * The definitive description of the destination language's structure.
172 */
173typedef struct asn_TYPE_descriptor_s {
Lev Walkina5972be2017-09-29 23:15:58 -0700174 const char *name; /* A name of the ASN.1 type. "" in some cases. */
175 const char *xml_tag; /* Name used in XML tag */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800176
Lev Walkina5972be2017-09-29 23:15:58 -0700177 /*
178 * Generalized functions for dealing with the specific type.
179 * May be directly invoked by applications.
180 */
181 asn_TYPE_operation_t *op;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182
Lev Walkina5972be2017-09-29 23:15:58 -0700183 /***********************************************************************
184 * Internally useful members. Not to be used by applications directly. *
185 **********************************************************************/
Lev Walkinf15320b2004-06-03 03:38:44 +0000186
Lev Walkina5972be2017-09-29 23:15:58 -0700187 /*
188 * Tags that are expected to occur.
189 */
190 const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
191 unsigned tags_count; /* Number of tags which are expected */
192 const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */
193 unsigned all_tags_count; /* Number of tags */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000194
Lev Walkina5972be2017-09-29 23:15:58 -0700195 /* OER, PER, and general constraints */
196 asn_encoding_constraints_t encoding_constraints;
Lev Walkin59b176e2005-11-26 11:25:14 +0000197
Lev Walkina5972be2017-09-29 23:15:58 -0700198 /*
199 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
200 */
201 struct asn_TYPE_member_s *elements;
202 unsigned elements_count;
Lev Walkin449f8322004-08-20 13:23:42 +0000203
Lev Walkina5972be2017-09-29 23:15:58 -0700204 /*
205 * Additional information describing the type, used by appropriate
206 * functions above.
207 */
208 const void *specifics;
Lev Walkin1cbd2222004-09-29 13:24:10 +0000209} asn_TYPE_descriptor_t;
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
211/*
Lev Walkin1cbd2222004-09-29 13:24:10 +0000212 * This type describes an element of the constructed type,
213 * i.e. SEQUENCE, SET, CHOICE, etc.
Lev Walkin449f8322004-08-20 13:23:42 +0000214 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000215 enum asn_TYPE_flags_e {
Lev Walkin14fd3e52017-08-27 01:38:45 -0700216 ATF_NOFLAGS,
217 ATF_POINTER = 0x01, /* Represented by the pointer */
218 ATF_OPEN_TYPE = 0x02, /* Open Type */
219 ATF_ANY_TYPE = 0x04 /* ANY type (deprecated!) */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000220 };
Lev Walkin1cbd2222004-09-29 13:24:10 +0000221typedef struct asn_TYPE_member_s {
Lev Walkin494fb702017-08-07 20:07:00 -0700222 enum asn_TYPE_flags_e flags; /* Element's presentation flags */
223 unsigned optional; /* Following optional members, including current */
224 unsigned memb_offset; /* Offset of the element */
225 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
226 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
227 asn_TYPE_descriptor_t *type; /* Member type descriptor */
Lev Walkinf6853ce2017-08-11 00:50:27 -0700228 asn_type_selector_f *type_selector; /* IoS runtime type selector */
Lev Walkina5972be2017-09-29 23:15:58 -0700229 asn_encoding_constraints_t encoding_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -0700230 int (*default_value)(int setval, void **sptr); /* DEFAULT <value> */
231 const char *name; /* ASN.1 identifier of the element */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000232} asn_TYPE_member_t;
Lev Walkin449f8322004-08-20 13:23:42 +0000233
234/*
Lev Walkin4c36e302004-06-06 07:20:02 +0000235 * BER tag to element number mapping.
236 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000237typedef struct asn_TYPE_tag2member_s {
Lev Walkin494fb702017-08-07 20:07:00 -0700238 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
239 unsigned el_no; /* Index of the associated member, base 0 */
Lev Walkind3fbff92017-08-27 12:39:30 -0700240 int toff_first; /* First occurence of the el_tag, relative */
241 int toff_last; /* Last occurence of the el_tag, relative */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000242} asn_TYPE_tag2member_t;
Lev Walkin4c36e302004-06-06 07:20:02 +0000243
Lev Walkin4c36e302004-06-06 07:20:02 +0000244/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 * This function is a wrapper around (td)->print_struct, which prints out
246 * the contents of the target language's structure (struct_ptr) into the
247 * file pointer (stream) in human readable form.
248 * RETURN VALUES:
249 * 0: The structure is printed.
250 * -1: Problem dumping the structure.
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251 * (See also xer_fprint() in xer_encoder.h)
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 */
253int asn_fprint(FILE *stream, /* Destination stream descriptor */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000254 asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 const void *struct_ptr); /* Structure to be printed */
256
Lev Walkin21b41ac2005-07-24 09:03:44 +0000257#ifdef __cplusplus
258}
259#endif
260
Lev Walkinf15320b2004-06-03 03:38:44 +0000261#endif /* _CONSTR_TYPE_H_ */