blob: b1d4a1a928f07a99eff51b611987281f9c03ac13 [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 Walkincc159472017-07-06 08:26:36 -070044#include <oer_decoder.h> /* Octet Encoding Rules encoder */
45#include <oer_encoder.h> /* Octet Encoding Rules encoder */
Lev Walkindc06f6b2004-10-20 15:50:55 +000046#include <constraints.h> /* Subtype constraints support */
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkinf15320b2004-06-03 03:38:44 +000048/*
49 * Free the structure according to its specification.
50 * If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
51 * will not be freed. (It may be useful in case the structure is allocated
52 * statically or arranged on the stack, yet its elements are allocated
53 * dynamically.)
54 */
55typedef void (asn_struct_free_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000056 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000057 void *struct_ptr, int free_contents_only);
Lev Walkinadcb5862006-03-17 02:11:12 +000058#define ASN_STRUCT_FREE(asn_DEF, ptr) (asn_DEF).free_struct(&(asn_DEF),ptr,0)
59#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \
60 (asn_DEF).free_struct(&(asn_DEF),ptr,1)
Lev Walkinf15320b2004-06-03 03:38:44 +000061
62/*
63 * Print the structure according to its specification.
64 */
65typedef int (asn_struct_print_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000066 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000067 const void *struct_ptr,
68 int level, /* Indentation level */
69 asn_app_consume_bytes_f *callback, void *app_key);
70
71/*
72 * Return the outmost tag of the type.
73 * If the type is untagged CHOICE, the dynamic operation is performed.
74 * NOTE: This function pointer type is only useful internally.
75 * Do not use it in your application.
76 */
77typedef ber_tlv_tag_t (asn_outmost_tag_f)(
Wim Lewis14e6b162014-07-23 16:06:01 -070078 const struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000079 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
Lev Walkin449f8322004-08-20 13:23:42 +000080/* The instance of the above function type; used internally. */
Lev Walkin1cbd2222004-09-29 13:24:10 +000081asn_outmost_tag_f asn_TYPE_outmost_tag;
Lev Walkinf15320b2004-06-03 03:38:44 +000082
83
84/*
85 * The definitive description of the destination language's structure.
86 */
Lev Walkin1cbd2222004-09-29 13:24:10 +000087typedef struct asn_TYPE_descriptor_s {
Wim Lewisfb6344e2014-07-28 12:16:01 -070088 const char *name; /* A name of the ASN.1 type. "" in some cases. */
89 const char *xml_tag; /* Name used in XML tag */
Lev Walkinf15320b2004-06-03 03:38:44 +000090
91 /*
92 * Generalized functions for dealing with the specific type.
93 * May be directly invoked by applications.
94 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000095 asn_struct_free_f *free_struct; /* Free the structure */
96 asn_struct_print_f *print_struct; /* Human readable output */
Lev Walkinf15320b2004-06-03 03:38:44 +000097 asn_constr_check_f *check_constraints; /* Constraints validator */
Lev Walkindc06f6b2004-10-20 15:50:55 +000098 ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000099 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000100 xer_type_decoder_f *xer_decoder; /* Generic XER decoder */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
Lev Walkincc159472017-07-06 08:26:36 -0700102 oer_type_decoder_f *oer_decoder; /* Generic OER decoder */
103 oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */
Lev Walkinb33425f2017-07-14 14:59:52 +0400104 per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */
105 per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +0000106
Lev Walkinbbd93252004-10-12 05:57:23 +0000107 /***********************************************************************
108 * Internally useful members. Not to be used by applications directly. *
109 **********************************************************************/
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
111 /*
Lev Walkin188ed2c2004-09-13 08:31:01 +0000112 * Tags that are expected to occur.
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000114 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700115 const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
116 int tags_count; /* Number of tags which are expected */
117 const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */
118 int all_tags_count; /* Number of tags */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000119
Lev Walkin76780762017-07-07 10:07:30 -0700120 asn_oer_constraints_t *oer_constraints; /* OER constraints */
121 asn_per_constraints_t *per_constraints; /* PER constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +0000122
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 /*
Lev Walkin449f8322004-08-20 13:23:42 +0000124 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
125 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000126 struct asn_TYPE_member_s *elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000127 int elements_count;
128
129 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 * Additional information describing the type, used by appropriate
131 * functions above.
132 */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700133 const void *specifics;
Lev Walkin1cbd2222004-09-29 13:24:10 +0000134} asn_TYPE_descriptor_t;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135
136/*
Lev Walkin1cbd2222004-09-29 13:24:10 +0000137 * This type describes an element of the constructed type,
138 * i.e. SEQUENCE, SET, CHOICE, etc.
Lev Walkin449f8322004-08-20 13:23:42 +0000139 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000140 enum asn_TYPE_flags_e {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000141 ATF_NOFLAGS,
142 ATF_POINTER = 0x01, /* Represented by the pointer */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000143 ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000144 };
Lev Walkin1cbd2222004-09-29 13:24:10 +0000145typedef struct asn_TYPE_member_s {
146 enum asn_TYPE_flags_e flags; /* Element's presentation flags */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000147 int optional; /* Following optional members, including current */
Lev Walkin449f8322004-08-20 13:23:42 +0000148 int memb_offset; /* Offset of the element */
149 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
150 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000151 asn_TYPE_descriptor_t *type; /* Member type descriptor */
Lev Walkin449f8322004-08-20 13:23:42 +0000152 asn_constr_check_f *memb_constraints; /* Constraints validator */
Lev Walkinb33425f2017-07-14 14:59:52 +0400153 asn_oer_constraints_t *oer_constraints; /* OER compiled constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +0000154 asn_per_constraints_t *per_constraints; /* PER compiled constraints */
Lev Walkin523de9e2006-08-18 01:34:18 +0000155 int (*default_value)(int setval, void **sptr); /* DEFAULT <value> */
Wim Lewisfb6344e2014-07-28 12:16:01 -0700156 const char *name; /* ASN.1 identifier of the element */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000157} asn_TYPE_member_t;
Lev Walkin449f8322004-08-20 13:23:42 +0000158
159/*
Lev Walkin4c36e302004-06-06 07:20:02 +0000160 * BER tag to element number mapping.
161 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000162typedef struct asn_TYPE_tag2member_s {
Lev Walkin4c36e302004-06-06 07:20:02 +0000163 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
164 int el_no; /* Index of the associated member, base 0 */
Lev Walkin38abe792004-06-14 13:09:45 +0000165 int toff_first; /* First occurence of the el_tag, relative */
166 int toff_last; /* Last occurence of the el_tag, relatvie */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000167} asn_TYPE_tag2member_t;
Lev Walkin4c36e302004-06-06 07:20:02 +0000168
Lev Walkin4c36e302004-06-06 07:20:02 +0000169/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 * This function is a wrapper around (td)->print_struct, which prints out
171 * the contents of the target language's structure (struct_ptr) into the
172 * file pointer (stream) in human readable form.
173 * RETURN VALUES:
174 * 0: The structure is printed.
175 * -1: Problem dumping the structure.
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 * (See also xer_fprint() in xer_encoder.h)
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 */
178int asn_fprint(FILE *stream, /* Destination stream descriptor */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000179 asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 const void *struct_ptr); /* Structure to be printed */
181
Lev Walkin21b41ac2005-07-24 09:03:44 +0000182#ifdef __cplusplus
183}
184#endif
185
Lev Walkinf15320b2004-06-03 03:38:44 +0000186#endif /* _CONSTR_TYPE_H_ */