blob: 79bd0a26815b08a93f00d6d9c1ee16b50be38740 [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 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000011#ifndef _CONSTR_TYPE_H_
12#define _CONSTR_TYPE_H_
Lev Walkinf15320b2004-06-03 03:38:44 +000013
Lev Walkin1cbd2222004-09-29 13:24:10 +000014#include <ber_tlv_length.h>
15#include <ber_tlv_tag.h>
16
17struct asn_TYPE_descriptor_s; /* Forward declaration */
18struct asn_TYPE_member_s; /* Forward declaration */
19
20/*
21 * This type provides the context information for various ASN.1 routines,
22 * primarily ones doing decoding. A member _asn_ctx of this type must be
23 * included into certain target language's structures, such as compound types.
24 */
25typedef struct asn_struct_ctx_s {
26 int phase; /* Decoding phase */
27 int step; /* Elementary step of a phase */
28 ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
29 void *ptr; /* Decoder-specific stuff (stack elements) */
30} asn_struct_ctx_t;
31
Lev Walkindc06f6b2004-10-20 15:50:55 +000032#include <ber_decoder.h> /* Basic Encoding Rules decoder */
33#include <der_encoder.h> /* Distinguished Encoding Rules encoder */
34#include <xer_decoder.h> /* Decoder of XER (XML, text) */
35#include <xer_encoder.h> /* Encoder into XER (XML, text) */
36#include <constraints.h> /* Subtype constraints support */
Lev Walkinf15320b2004-06-03 03:38:44 +000037
Lev Walkinf15320b2004-06-03 03:38:44 +000038/*
39 * Free the structure according to its specification.
40 * If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
41 * will not be freed. (It may be useful in case the structure is allocated
42 * statically or arranged on the stack, yet its elements are allocated
43 * dynamically.)
44 */
45typedef void (asn_struct_free_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000046 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000047 void *struct_ptr, int free_contents_only);
48
49/*
50 * Print the structure according to its specification.
51 */
52typedef int (asn_struct_print_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000053 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000054 const void *struct_ptr,
55 int level, /* Indentation level */
56 asn_app_consume_bytes_f *callback, void *app_key);
57
58/*
59 * Return the outmost tag of the type.
60 * If the type is untagged CHOICE, the dynamic operation is performed.
61 * NOTE: This function pointer type is only useful internally.
62 * Do not use it in your application.
63 */
64typedef ber_tlv_tag_t (asn_outmost_tag_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000065 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000066 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
Lev Walkin449f8322004-08-20 13:23:42 +000067/* The instance of the above function type; used internally. */
Lev Walkin1cbd2222004-09-29 13:24:10 +000068asn_outmost_tag_f asn_TYPE_outmost_tag;
Lev Walkinf15320b2004-06-03 03:38:44 +000069
70
71/*
72 * The definitive description of the destination language's structure.
73 */
Lev Walkin1cbd2222004-09-29 13:24:10 +000074typedef struct asn_TYPE_descriptor_s {
Lev Walkindc06f6b2004-10-20 15:50:55 +000075 char *name; /* A name of the ASN.1 type. "" in some cases. */
76 char *xml_tag; /* Name used in XML tag */
Lev Walkinf15320b2004-06-03 03:38:44 +000077
78 /*
79 * Generalized functions for dealing with the specific type.
80 * May be directly invoked by applications.
81 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000082 asn_struct_free_f *free_struct; /* Free the structure */
83 asn_struct_print_f *print_struct; /* Human readable output */
Lev Walkinf15320b2004-06-03 03:38:44 +000084 asn_constr_check_f *check_constraints; /* Constraints validator */
Lev Walkindc06f6b2004-10-20 15:50:55 +000085 ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000086 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
Lev Walkindc06f6b2004-10-20 15:50:55 +000087 xer_type_decoder_f *xer_decoder; /* Generic XER decoder */
Lev Walkina9cc46e2004-09-22 16:06:28 +000088 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000089
Lev Walkinbbd93252004-10-12 05:57:23 +000090 /***********************************************************************
91 * Internally useful members. Not to be used by applications directly. *
92 **********************************************************************/
Lev Walkinf15320b2004-06-03 03:38:44 +000093
94 /*
Lev Walkin188ed2c2004-09-13 08:31:01 +000095 * Tags that are expected to occur.
Lev Walkinf15320b2004-06-03 03:38:44 +000096 */
Lev Walkinbbd93252004-10-12 05:57:23 +000097 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
Lev Walkin188ed2c2004-09-13 08:31:01 +000098 ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
Lev Walkinf15320b2004-06-03 03:38:44 +000099 int tags_count; /* Number of tags which are expected */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000100 ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
101 int all_tags_count; /* Number of tags */
102
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 /*
Lev Walkin449f8322004-08-20 13:23:42 +0000104 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
105 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000106 struct asn_TYPE_member_s *elements;
Lev Walkin449f8322004-08-20 13:23:42 +0000107 int elements_count;
108
109 /*
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 * Additional information describing the type, used by appropriate
111 * functions above.
112 */
113 void *specifics;
Lev Walkin1cbd2222004-09-29 13:24:10 +0000114} asn_TYPE_descriptor_t;
Lev Walkinf15320b2004-06-03 03:38:44 +0000115
116/*
Lev Walkin1cbd2222004-09-29 13:24:10 +0000117 * This type describes an element of the constructed type,
118 * i.e. SEQUENCE, SET, CHOICE, etc.
Lev Walkin449f8322004-08-20 13:23:42 +0000119 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000120 enum asn_TYPE_flags_e {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000121 ATF_NOFLAGS,
122 ATF_POINTER = 0x01, /* Represented by the pointer */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000123 ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000124 };
Lev Walkin1cbd2222004-09-29 13:24:10 +0000125typedef struct asn_TYPE_member_s {
126 enum asn_TYPE_flags_e flags; /* Element's presentation flags */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000127 int optional; /* Following optional members, including current */
Lev Walkin449f8322004-08-20 13:23:42 +0000128 int memb_offset; /* Offset of the element */
129 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
130 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000131 asn_TYPE_descriptor_t *type; /* Member type descriptor */
Lev Walkin449f8322004-08-20 13:23:42 +0000132 asn_constr_check_f *memb_constraints; /* Constraints validator */
133 char *name; /* ASN.1 identifier of the element */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000134} asn_TYPE_member_t;
Lev Walkin449f8322004-08-20 13:23:42 +0000135
136/*
Lev Walkin4c36e302004-06-06 07:20:02 +0000137 * BER tag to element number mapping.
138 */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000139typedef struct asn_TYPE_tag2member_s {
Lev Walkin4c36e302004-06-06 07:20:02 +0000140 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
141 int el_no; /* Index of the associated member, base 0 */
Lev Walkin38abe792004-06-14 13:09:45 +0000142 int toff_first; /* First occurence of the el_tag, relative */
143 int toff_last; /* Last occurence of the el_tag, relatvie */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000144} asn_TYPE_tag2member_t;
Lev Walkin4c36e302004-06-06 07:20:02 +0000145
Lev Walkin4c36e302004-06-06 07:20:02 +0000146/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 * This function is a wrapper around (td)->print_struct, which prints out
148 * the contents of the target language's structure (struct_ptr) into the
149 * file pointer (stream) in human readable form.
150 * RETURN VALUES:
151 * 0: The structure is printed.
152 * -1: Problem dumping the structure.
Lev Walkina9cc46e2004-09-22 16:06:28 +0000153 * (See also xer_fprint() in xer_encoder.h)
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 */
155int asn_fprint(FILE *stream, /* Destination stream descriptor */
Lev Walkin1cbd2222004-09-29 13:24:10 +0000156 asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 const void *struct_ptr); /* Structure to be printed */
158
159#endif /* _CONSTR_TYPE_H_ */