blob: 937ef10e694d08e13e969b7bbaa606b5c7acef49 [file] [log] [blame]
vlmfa67ddc2004-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 */
vlm39ba4c42004-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
vlmfa67ddc2004-06-03 03:38:44 +000013
vlm574b0812004-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
32/*
33 * This structure defines a context that may be passed to every ASN.1 encoder
34 * or decoder function.
35 * WARNING: if max_stack_size member is set, and you are calling the
36 * function pointers of the asn_TYPE_descriptor_t directly,
37 * this structure must be ALLOCATED ON THE STACK!
38 */
39typedef struct asn_codec_ctx_s {
40 /*
41 * Limit the decoder routines to use no (much) more stack than a given
42 * number of bytes. Most of decoders are stack-based, and this
43 * would protect against stack overflows if the number of nested
44 * encodings is high.
45 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
46 * and are safe from this kind of overflow.
47 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
48 * this variable. Be careful in multithreaded environments, as the
49 * stack size is rather limited.
50 */
51 size_t max_stack_size; /* 0 disables stack bounds checking */
52} asn_codec_ctx_t;
vlm39ba4c42004-09-22 16:06:28 +000053
54/*
55 * Type of the return value of the encoding functions (der_encode, xer_encode).
56 */
57typedef struct asn_enc_rval_s {
58 /*
59 * Number of bytes encoded.
60 * -1 indicates failure to encode the structure.
61 * In this case, the members below this one are meaningful.
62 */
63 ssize_t encoded;
64
65 /*
66 * Members meaningful when (encoded == -1), for post mortem analysis.
67 */
68
69 /* Type which cannot be encoded */
vlm574b0812004-09-29 13:24:10 +000070 struct asn_TYPE_descriptor_s *failed_type;
vlm39ba4c42004-09-22 16:06:28 +000071
72 /* Pointer to the structure of that type */
73 void *structure_ptr;
74} asn_enc_rval_t;
75#define _ASN_ENCODE_FAILED do { \
76 asn_enc_rval_t __er = { -1, td, sptr }; \
77 return __er; \
78} while(0)
79
vlmfa67ddc2004-06-03 03:38:44 +000080#include <ber_decoder.h>
81#include <der_encoder.h>
vlm39ba4c42004-09-22 16:06:28 +000082#include <xer_encoder.h>
vlmfa67ddc2004-06-03 03:38:44 +000083#include <constraints.h>
84
vlmfa67ddc2004-06-03 03:38:44 +000085/*
86 * Free the structure according to its specification.
87 * If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
88 * will not be freed. (It may be useful in case the structure is allocated
89 * statically or arranged on the stack, yet its elements are allocated
90 * dynamically.)
91 */
92typedef void (asn_struct_free_f)(
vlm574b0812004-09-29 13:24:10 +000093 struct asn_TYPE_descriptor_s *type_descriptor,
vlmfa67ddc2004-06-03 03:38:44 +000094 void *struct_ptr, int free_contents_only);
95
96/*
97 * Print the structure according to its specification.
98 */
99typedef int (asn_struct_print_f)(
vlm574b0812004-09-29 13:24:10 +0000100 struct asn_TYPE_descriptor_s *type_descriptor,
vlmfa67ddc2004-06-03 03:38:44 +0000101 const void *struct_ptr,
102 int level, /* Indentation level */
103 asn_app_consume_bytes_f *callback, void *app_key);
104
105/*
106 * Return the outmost tag of the type.
107 * If the type is untagged CHOICE, the dynamic operation is performed.
108 * NOTE: This function pointer type is only useful internally.
109 * Do not use it in your application.
110 */
111typedef ber_tlv_tag_t (asn_outmost_tag_f)(
vlm574b0812004-09-29 13:24:10 +0000112 struct asn_TYPE_descriptor_s *type_descriptor,
vlmfa67ddc2004-06-03 03:38:44 +0000113 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
vlme413c122004-08-20 13:23:42 +0000114/* The instance of the above function type; used internally. */
vlm574b0812004-09-29 13:24:10 +0000115asn_outmost_tag_f asn_TYPE_outmost_tag;
vlmfa67ddc2004-06-03 03:38:44 +0000116
117
118/*
119 * The definitive description of the destination language's structure.
120 */
vlm574b0812004-09-29 13:24:10 +0000121typedef struct asn_TYPE_descriptor_s {
vlmfa67ddc2004-06-03 03:38:44 +0000122 char *name; /* A name of the ASN.1 type */
123
124 /*
125 * Generalized functions for dealing with the specific type.
126 * May be directly invoked by applications.
127 */
vlm39ba4c42004-09-22 16:06:28 +0000128 asn_struct_free_f *free_struct; /* Free the structure */
129 asn_struct_print_f *print_struct; /* Human readable output */
vlmfa67ddc2004-06-03 03:38:44 +0000130 asn_constr_check_f *check_constraints; /* Constraints validator */
131 ber_type_decoder_f *ber_decoder; /* Free-form BER decoder */
132 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
vlm39ba4c42004-09-22 16:06:28 +0000133 int (*xer_decoder);/* PLACEHOLDER */ /* Free-form XER decoder */
134 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
vlmfa67ddc2004-06-03 03:38:44 +0000135
136 /*
137 * Functions used internally. Should not be used by applications.
138 */
139 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
140
141 /*
vlm72425de2004-09-13 08:31:01 +0000142 * Tags that are expected to occur.
vlmfa67ddc2004-06-03 03:38:44 +0000143 */
vlm72425de2004-09-13 08:31:01 +0000144 ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
vlmfa67ddc2004-06-03 03:38:44 +0000145 int tags_count; /* Number of tags which are expected */
vlm72425de2004-09-13 08:31:01 +0000146 ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
147 int all_tags_count; /* Number of tags */
148
vlmfa67ddc2004-06-03 03:38:44 +0000149 /*
vlme413c122004-08-20 13:23:42 +0000150 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
151 */
vlm574b0812004-09-29 13:24:10 +0000152 struct asn_TYPE_member_s *elements;
vlme413c122004-08-20 13:23:42 +0000153 int elements_count;
154
155 /*
vlmfa67ddc2004-06-03 03:38:44 +0000156 * Additional information describing the type, used by appropriate
157 * functions above.
158 */
159 void *specifics;
vlm574b0812004-09-29 13:24:10 +0000160} asn_TYPE_descriptor_t;
vlmfa67ddc2004-06-03 03:38:44 +0000161
162/*
vlm574b0812004-09-29 13:24:10 +0000163 * This type describes an element of the constructed type,
164 * i.e. SEQUENCE, SET, CHOICE, etc.
vlme413c122004-08-20 13:23:42 +0000165 */
vlm574b0812004-09-29 13:24:10 +0000166 enum asn_TYPE_flags_e {
vlm72425de2004-09-13 08:31:01 +0000167 ATF_NOFLAGS,
168 ATF_POINTER = 0x01, /* Represented by the pointer */
vlm6678cb12004-09-26 13:10:40 +0000169 ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */
vlm72425de2004-09-13 08:31:01 +0000170 };
vlm574b0812004-09-29 13:24:10 +0000171typedef struct asn_TYPE_member_s {
172 enum asn_TYPE_flags_e flags; /* Element's presentation flags */
vlmddd5a7d2004-09-10 09:18:20 +0000173 int optional; /* Following optional members, including current */
vlme413c122004-08-20 13:23:42 +0000174 int memb_offset; /* Offset of the element */
175 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
176 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
vlm574b0812004-09-29 13:24:10 +0000177 asn_TYPE_descriptor_t *type; /* Member type descriptor */
vlme413c122004-08-20 13:23:42 +0000178 asn_constr_check_f *memb_constraints; /* Constraints validator */
179 char *name; /* ASN.1 identifier of the element */
vlm574b0812004-09-29 13:24:10 +0000180} asn_TYPE_member_t;
vlme413c122004-08-20 13:23:42 +0000181
182/*
vlm31473082004-06-06 07:20:02 +0000183 * BER tag to element number mapping.
184 */
vlm574b0812004-09-29 13:24:10 +0000185typedef struct asn_TYPE_tag2member_s {
vlm31473082004-06-06 07:20:02 +0000186 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
187 int el_no; /* Index of the associated member, base 0 */
vlmc8aeab42004-06-14 13:09:45 +0000188 int toff_first; /* First occurence of the el_tag, relative */
189 int toff_last; /* Last occurence of the el_tag, relatvie */
vlm574b0812004-09-29 13:24:10 +0000190} asn_TYPE_tag2member_t;
vlm31473082004-06-06 07:20:02 +0000191
vlm31473082004-06-06 07:20:02 +0000192/*
vlmfa67ddc2004-06-03 03:38:44 +0000193 * This function is a wrapper around (td)->print_struct, which prints out
194 * the contents of the target language's structure (struct_ptr) into the
195 * file pointer (stream) in human readable form.
196 * RETURN VALUES:
197 * 0: The structure is printed.
198 * -1: Problem dumping the structure.
vlm39ba4c42004-09-22 16:06:28 +0000199 * (See also xer_fprint() in xer_encoder.h)
vlmfa67ddc2004-06-03 03:38:44 +0000200 */
201int asn_fprint(FILE *stream, /* Destination stream descriptor */
vlm574b0812004-09-29 13:24:10 +0000202 asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
vlmfa67ddc2004-06-03 03:38:44 +0000203 const void *struct_ptr); /* Structure to be printed */
204
205#endif /* _CONSTR_TYPE_H_ */