blob: 93ed24d388140c54cdd94c8ab29219875302552f [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
vlm39ba4c42004-09-22 16:06:28 +000014struct asn1_TYPE_descriptor_s; /* Forward declaration */
15struct asn1_TYPE_member_s; /* Forward declaration */
16
17/*
18 * Type of the return value of the encoding functions (der_encode, xer_encode).
19 */
20typedef struct asn_enc_rval_s {
21 /*
22 * Number of bytes encoded.
23 * -1 indicates failure to encode the structure.
24 * In this case, the members below this one are meaningful.
25 */
26 ssize_t encoded;
27
28 /*
29 * Members meaningful when (encoded == -1), for post mortem analysis.
30 */
31
32 /* Type which cannot be encoded */
33 struct asn1_TYPE_descriptor_s *failed_type;
34
35 /* Pointer to the structure of that type */
36 void *structure_ptr;
37} asn_enc_rval_t;
38#define _ASN_ENCODE_FAILED do { \
39 asn_enc_rval_t __er = { -1, td, sptr }; \
40 return __er; \
41} while(0)
42
vlmfa67ddc2004-06-03 03:38:44 +000043#include <ber_tlv_length.h>
44#include <ber_tlv_tag.h>
45#include <ber_decoder.h>
46#include <der_encoder.h>
vlm39ba4c42004-09-22 16:06:28 +000047#include <xer_encoder.h>
vlmfa67ddc2004-06-03 03:38:44 +000048#include <constraints.h>
49
vlmfa67ddc2004-06-03 03:38:44 +000050/*
51 * Free the structure according to its specification.
52 * If (free_contents_only) is set, the wrapper structure itself (struct_ptr)
53 * will not be freed. (It may be useful in case the structure is allocated
54 * statically or arranged on the stack, yet its elements are allocated
55 * dynamically.)
56 */
57typedef void (asn_struct_free_f)(
58 struct asn1_TYPE_descriptor_s *type_descriptor,
59 void *struct_ptr, int free_contents_only);
60
61/*
62 * Print the structure according to its specification.
63 */
64typedef int (asn_struct_print_f)(
65 struct asn1_TYPE_descriptor_s *type_descriptor,
66 const void *struct_ptr,
67 int level, /* Indentation level */
68 asn_app_consume_bytes_f *callback, void *app_key);
69
70/*
71 * Return the outmost tag of the type.
72 * If the type is untagged CHOICE, the dynamic operation is performed.
73 * NOTE: This function pointer type is only useful internally.
74 * Do not use it in your application.
75 */
76typedef ber_tlv_tag_t (asn_outmost_tag_f)(
77 struct asn1_TYPE_descriptor_s *type_descriptor,
78 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);
vlme413c122004-08-20 13:23:42 +000079/* The instance of the above function type; used internally. */
vlmfa67ddc2004-06-03 03:38:44 +000080asn_outmost_tag_f asn1_TYPE_outmost_tag;
81
82
83/*
84 * The definitive description of the destination language's structure.
85 */
86typedef struct asn1_TYPE_descriptor_s {
87 char *name; /* A name of the ASN.1 type */
88
89 /*
90 * Generalized functions for dealing with the specific type.
91 * May be directly invoked by applications.
92 */
vlm39ba4c42004-09-22 16:06:28 +000093 asn_struct_free_f *free_struct; /* Free the structure */
94 asn_struct_print_f *print_struct; /* Human readable output */
vlmfa67ddc2004-06-03 03:38:44 +000095 asn_constr_check_f *check_constraints; /* Constraints validator */
96 ber_type_decoder_f *ber_decoder; /* Free-form BER decoder */
97 der_type_encoder_f *der_encoder; /* Canonical DER encoder */
vlm39ba4c42004-09-22 16:06:28 +000098 int (*xer_decoder);/* PLACEHOLDER */ /* Free-form XER decoder */
99 xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
vlmfa67ddc2004-06-03 03:38:44 +0000100
101 /*
102 * Functions used internally. Should not be used by applications.
103 */
104 asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
105
106 /*
vlm72425de2004-09-13 08:31:01 +0000107 * Tags that are expected to occur.
vlmfa67ddc2004-06-03 03:38:44 +0000108 */
vlm72425de2004-09-13 08:31:01 +0000109 ber_tlv_tag_t *tags; /* Effective tags sequence for this type */
vlmfa67ddc2004-06-03 03:38:44 +0000110 int tags_count; /* Number of tags which are expected */
vlm72425de2004-09-13 08:31:01 +0000111 ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
112 int all_tags_count; /* Number of tags */
113
vlmfa67ddc2004-06-03 03:38:44 +0000114 /*
vlme413c122004-08-20 13:23:42 +0000115 * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).
116 */
117 struct asn1_TYPE_member_s *elements;
118 int elements_count;
119
120 /*
vlmfa67ddc2004-06-03 03:38:44 +0000121 * Additional information describing the type, used by appropriate
122 * functions above.
123 */
124 void *specifics;
125} asn1_TYPE_descriptor_t;
126
127/*
vlme413c122004-08-20 13:23:42 +0000128 * An element of the constructed type, i.e. SEQUENCE, SET, CHOICE.
129 */
vlm72425de2004-09-13 08:31:01 +0000130 enum asn1_TYPE_flags_e {
131 ATF_NOFLAGS,
132 ATF_POINTER = 0x01, /* Represented by the pointer */
vlm6678cb12004-09-26 13:10:40 +0000133 ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */
vlm72425de2004-09-13 08:31:01 +0000134 };
vlme413c122004-08-20 13:23:42 +0000135typedef struct asn1_TYPE_member_s {
vlm72425de2004-09-13 08:31:01 +0000136 enum asn1_TYPE_flags_e flags; /* Element's presentation flags */
vlmddd5a7d2004-09-10 09:18:20 +0000137 int optional; /* Following optional members, including current */
vlme413c122004-08-20 13:23:42 +0000138 int memb_offset; /* Offset of the element */
139 ber_tlv_tag_t tag; /* Outmost (most immediate) tag */
140 int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */
141 asn1_TYPE_descriptor_t *type; /* Member type descriptor */
142 asn_constr_check_f *memb_constraints; /* Constraints validator */
143 char *name; /* ASN.1 identifier of the element */
144} asn1_TYPE_member_t;
145
146/*
vlm31473082004-06-06 07:20:02 +0000147 * BER tag to element number mapping.
148 */
149typedef struct asn1_TYPE_tag2member_s {
150 ber_tlv_tag_t el_tag; /* Outmost tag of the member */
151 int el_no; /* Index of the associated member, base 0 */
vlmc8aeab42004-06-14 13:09:45 +0000152 int toff_first; /* First occurence of the el_tag, relative */
153 int toff_last; /* Last occurence of the el_tag, relatvie */
vlm31473082004-06-06 07:20:02 +0000154} asn1_TYPE_tag2member_t;
155
156
vlm31473082004-06-06 07:20:02 +0000157/*
vlmfa67ddc2004-06-03 03:38:44 +0000158 * This function is a wrapper around (td)->print_struct, which prints out
159 * the contents of the target language's structure (struct_ptr) into the
160 * file pointer (stream) in human readable form.
161 * RETURN VALUES:
162 * 0: The structure is printed.
163 * -1: Problem dumping the structure.
vlm39ba4c42004-09-22 16:06:28 +0000164 * (See also xer_fprint() in xer_encoder.h)
vlmfa67ddc2004-06-03 03:38:44 +0000165 */
166int asn_fprint(FILE *stream, /* Destination stream descriptor */
167 asn1_TYPE_descriptor_t *td, /* ASN.1 type descriptor */
168 const void *struct_ptr); /* Structure to be printed */
169
170#endif /* _CONSTR_TYPE_H_ */