vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | /* |
| 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 |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 14 | #include <ber_tlv_length.h> |
| 15 | #include <ber_tlv_tag.h> |
| 16 | |
| 17 | struct asn_TYPE_descriptor_s; /* Forward declaration */ |
| 18 | struct 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 | */ |
| 25 | typedef 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 | */ |
| 39 | typedef 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; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Type of the return value of the encoding functions (der_encode, xer_encode). |
| 56 | */ |
| 57 | typedef 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 */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 70 | struct asn_TYPE_descriptor_s *failed_type; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 71 | |
| 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | #include <ber_decoder.h> |
| 81 | #include <der_encoder.h> |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 82 | #include <xer_encoder.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | #include <constraints.h> |
| 84 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | /* |
| 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 | */ |
| 92 | typedef void (asn_struct_free_f)( |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 93 | struct asn_TYPE_descriptor_s *type_descriptor, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | void *struct_ptr, int free_contents_only); |
| 95 | |
| 96 | /* |
| 97 | * Print the structure according to its specification. |
| 98 | */ |
| 99 | typedef int (asn_struct_print_f)( |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 100 | struct asn_TYPE_descriptor_s *type_descriptor, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | 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 | */ |
| 111 | typedef ber_tlv_tag_t (asn_outmost_tag_f)( |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 112 | struct asn_TYPE_descriptor_s *type_descriptor, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 114 | /* The instance of the above function type; used internally. */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 115 | asn_outmost_tag_f asn_TYPE_outmost_tag; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | /* |
| 119 | * The definitive description of the destination language's structure. |
| 120 | */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 121 | typedef struct asn_TYPE_descriptor_s { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 128 | asn_struct_free_f *free_struct; /* Free the structure */ |
| 129 | asn_struct_print_f *print_struct; /* Human readable output */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 130 | 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 */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 133 | int (*xer_decoder);/* PLACEHOLDER */ /* Free-form XER decoder */ |
| 134 | xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Functions used internally. Should not be used by applications. |
| 138 | */ |
| 139 | asn_outmost_tag_f *outmost_tag; /* <optional, internal> */ |
| 140 | |
| 141 | /* |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 142 | * Tags that are expected to occur. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 144 | ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | int tags_count; /* Number of tags which are expected */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 146 | ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */ |
| 147 | int all_tags_count; /* Number of tags */ |
| 148 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | /* |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 150 | * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). |
| 151 | */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 152 | struct asn_TYPE_member_s *elements; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 153 | int elements_count; |
| 154 | |
| 155 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | * Additional information describing the type, used by appropriate |
| 157 | * functions above. |
| 158 | */ |
| 159 | void *specifics; |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 160 | } asn_TYPE_descriptor_t; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 163 | * This type describes an element of the constructed type, |
| 164 | * i.e. SEQUENCE, SET, CHOICE, etc. |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 165 | */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 166 | enum asn_TYPE_flags_e { |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 167 | ATF_NOFLAGS, |
| 168 | ATF_POINTER = 0x01, /* Represented by the pointer */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 169 | ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 170 | }; |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 171 | typedef struct asn_TYPE_member_s { |
| 172 | enum asn_TYPE_flags_e flags; /* Element's presentation flags */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 173 | int optional; /* Following optional members, including current */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 174 | 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 */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 177 | asn_TYPE_descriptor_t *type; /* Member type descriptor */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 178 | asn_constr_check_f *memb_constraints; /* Constraints validator */ |
| 179 | char *name; /* ASN.1 identifier of the element */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 180 | } asn_TYPE_member_t; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 181 | |
| 182 | /* |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 183 | * BER tag to element number mapping. |
| 184 | */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 185 | typedef struct asn_TYPE_tag2member_s { |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 186 | ber_tlv_tag_t el_tag; /* Outmost tag of the member */ |
| 187 | int el_no; /* Index of the associated member, base 0 */ |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 188 | int toff_first; /* First occurence of the el_tag, relative */ |
| 189 | int toff_last; /* Last occurence of the el_tag, relatvie */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 190 | } asn_TYPE_tag2member_t; |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 191 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 192 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | * 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. |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 199 | * (See also xer_fprint() in xer_encoder.h) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | */ |
| 201 | int asn_fprint(FILE *stream, /* Destination stream descriptor */ |
vlm | 574b081 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 202 | asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 203 | const void *struct_ptr); /* Structure to be printed */ |
| 204 | |
| 205 | #endif /* _CONSTR_TYPE_H_ */ |