blob: 28e8e2cdde696260e7dbbf1e30d725107c0e6baf [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005/*
6 * This file contains the declaration structure called "ASN.1 Type Definition",
7 * which holds all information necessary for encoding and decoding routines.
8 * This structure even contains pointer to these encoding and decoding routines
9 * for each defined ASN.1 type.
10 */
11#ifndef _CONSTR_TYPE_H
12#define _CONSTR_TYPE_H
Lev Walkinf15320b2004-06-03 03:38:44 +000013
14#include <asn_types.h> /* System-dependent types */
Lev Walkina9cc46e2004-09-22 16:06:28 +000015
16struct asn1_TYPE_descriptor_s; /* Forward declaration */
17struct asn1_TYPE_member_s; /* Forward declaration */
18
19/*
20 * Type of the return value of the encoding functions (der_encode, xer_encode).
21 */
22typedef struct asn_enc_rval_s {
23 /*
24 * Number of bytes encoded.
25 * -1 indicates failure to encode the structure.
26 * In this case, the members below this one are meaningful.
27 */
28 ssize_t encoded;
29
30 /*
31 * Members meaningful when (encoded == -1), for post mortem analysis.
32 */
33
34 /* Type which cannot be encoded */
35 struct asn1_TYPE_descriptor_s *failed_type;
36
37 /* Pointer to the structure of that type */
38 void *structure_ptr;
39} asn_enc_rval_t;
40#define _ASN_ENCODE_FAILED do { \
41 asn_enc_rval_t __er = { -1, td, sptr }; \
42 return __er; \
43} while(0)
44
Lev Walkinf15320b2004-06-03 03:38:44 +000045#include <ber_tlv_length.h>
46#include <ber_tlv_tag.h>
47#include <ber_decoder.h>
48#include <der_encoder.h>
Lev Walkina9cc46e2004-09-22 16:06:28 +000049#include <xer_encoder.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000050#include <constraints.h>
51
Lev Walkinf15320b2004-06-03 03:38:44 +000052/*
53 * Free the structure according to its specification.
54 * If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
55 * will not be freed. (It may be useful in case the structure is allocated
56 * statically or arranged on the stack, yet its elements are allocated
57 * dynamically.)
58 */
59typedef void (asn_struct_free_f)(
60 struct asn1_TYPE_descriptor_s *type_descriptor,
61 void *struct_ptr, int free_contents_only);
62
63/*
64 * Print the structure according to its specification.
65 */
66typedef int (asn_struct_print_f)(
67 struct asn1_TYPE_descriptor_s *type_descriptor,
68 const void *struct_ptr,
69 int level, /* Indentation level */
70 asn_app_consume_bytes_f *callback, void *app_key);
71
72/*
73 * Return the outmost tag of the type.
74 * If the type is untagged CHOICE, the dynamic operation is performed.
75 * NOTE: This function pointer type is only useful internally.
76 * Do not use it in your application.
77 */
78typedef ber_tlv_tag_t (asn_outmost_tag_f)(
79 struct asn1_TYPE_descriptor_s *type_descriptor,
80 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
Lev Walkin449f8322004-08-20 13:23:42 +000081/* The instance of the above function type; used internally. */
Lev Walkinf15320b2004-06-03 03:38:44 +000082asn_outmost_tag_f asn1_TYPE_outmost_tag;
83
84
85/*
86 * The definitive description of the destination language's structure.
87 */
88typedef struct asn1_TYPE_descriptor_s {
89 char *name; /* A name of the ASN.1 type */
90
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 */
98 ber_type_decoder_f *ber_decoder; /* Free-form BER decoder */
99 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000100 int (*xer_decoder);/* PLACEHOLDER */ /* Free-form XER decoder */
101 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +0000102
103 /*
104 * Functions used internally. Should not be used by applications.
105 */
106 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
107
108 /*
Lev Walkin188ed2c2004-09-13 08:31:01 +0000109 * Tags that are expected to occur.
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000111 ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 int tags_count; /* Number of tags which are expected */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000113 ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
114 int all_tags_count; /* Number of tags */
115
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 int last_tag_form; /* Acceptable form of the tag (prim, constr) */
117
118 /*
Lev Walkin449f8322004-08-20 13:23:42 +0000119 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
120 */
121 struct asn1_TYPE_member_s *elements;
122 int elements_count;
123
124 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 * Additional information describing the type, used by appropriate
126 * functions above.
127 */
128 void *specifics;
129} asn1_TYPE_descriptor_t;
130
131/*
Lev Walkin449f8322004-08-20 13:23:42 +0000132 * An element of the constructed type, i.e. SEQUENCE, SET, CHOICE.
133 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000134 enum asn1_TYPE_flags_e {
135 ATF_NOFLAGS,
136 ATF_POINTER = 0x01, /* Represented by the pointer */
137 ATF_OPEN_TYPE = 0x02, /* ANY type, without meaningful tag */
138 };
Lev Walkin449f8322004-08-20 13:23:42 +0000139typedef struct asn1_TYPE_member_s {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000140 enum asn1_TYPE_flags_e flags; /* Element's presentation flags */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000141 int optional; /* Following optional members, including current */
Lev Walkin449f8322004-08-20 13:23:42 +0000142 int memb_offset; /* Offset of the element */
143 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
144 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
145 asn1_TYPE_descriptor_t *type; /* Member type descriptor */
146 asn_constr_check_f *memb_constraints; /* Constraints validator */
147 char *name; /* ASN.1 identifier of the element */
148} asn1_TYPE_member_t;
149
150/*
Lev Walkin4c36e302004-06-06 07:20:02 +0000151 * BER tag to element number mapping.
152 */
153typedef struct asn1_TYPE_tag2member_s {
154 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
155 int el_no; /* Index of the associated member, base 0 */
Lev Walkin38abe792004-06-14 13:09:45 +0000156 int toff_first; /* First occurence of the el_tag, relative */
157 int toff_last; /* Last occurence of the el_tag, relatvie */
Lev Walkin4c36e302004-06-06 07:20:02 +0000158} asn1_TYPE_tag2member_t;
159
160
Lev Walkin4c36e302004-06-06 07:20:02 +0000161/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 * This function is a wrapper around (td)->print_struct, which prints out
163 * the contents of the target language's structure (struct_ptr) into the
164 * file pointer (stream) in human readable form.
165 * RETURN VALUES:
166 * 0: The structure is printed.
167 * -1: Problem dumping the structure.
Lev Walkina9cc46e2004-09-22 16:06:28 +0000168 * (See also xer_fprint() in xer_encoder.h)
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 */
170int asn_fprint(FILE *stream, /* Destination stream descriptor */
171 asn1_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
172 const void *struct_ptr); /* Structure to be printed */
173
174#endif /* _CONSTR_TYPE_H_ */