Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a9cc46e | 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 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 11 | #ifndef _CONSTR_TYPE_H_ |
| 12 | #define _CONSTR_TYPE_H_ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | |
Lev Walkin | 1cbd222 | 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 { |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 26 | short phase; /* Decoding phase */ |
| 27 | short step; /* Elementary step of a phase */ |
| 28 | int context; /* Other context information */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 29 | void *ptr; /* Decoder-specific stuff (stack elements) */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 30 | ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 31 | } asn_struct_ctx_t; |
| 32 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 33 | #include <ber_decoder.h> /* Basic Encoding Rules decoder */ |
| 34 | #include <der_encoder.h> /* Distinguished Encoding Rules encoder */ |
| 35 | #include <xer_decoder.h> /* Decoder of XER (XML, text) */ |
| 36 | #include <xer_encoder.h> /* Encoder into XER (XML, text) */ |
| 37 | #include <constraints.h> /* Subtype constraints support */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | /* |
| 40 | * Free the structure according to its specification. |
| 41 | * If (free_contents_only) is set, the wrapper structure itself (struct_ptr) |
| 42 | * will not be freed. (It may be useful in case the structure is allocated |
| 43 | * statically or arranged on the stack, yet its elements are allocated |
| 44 | * dynamically.) |
| 45 | */ |
| 46 | typedef void (asn_struct_free_f)( |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 47 | struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | void *struct_ptr, int free_contents_only); |
| 49 | |
| 50 | /* |
| 51 | * Print the structure according to its specification. |
| 52 | */ |
| 53 | typedef int (asn_struct_print_f)( |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 54 | struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | const void *struct_ptr, |
| 56 | int level, /* Indentation level */ |
| 57 | asn_app_consume_bytes_f *callback, void *app_key); |
| 58 | |
| 59 | /* |
| 60 | * Return the outmost tag of the type. |
| 61 | * If the type is untagged CHOICE, the dynamic operation is performed. |
| 62 | * NOTE: This function pointer type is only useful internally. |
| 63 | * Do not use it in your application. |
| 64 | */ |
| 65 | typedef ber_tlv_tag_t (asn_outmost_tag_f)( |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 66 | struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 68 | /* The instance of the above function type; used internally. */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 69 | asn_outmost_tag_f asn_TYPE_outmost_tag; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | /* |
| 73 | * The definitive description of the destination language's structure. |
| 74 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 75 | typedef struct asn_TYPE_descriptor_s { |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 76 | char *name; /* A name of the ASN.1 type. "" in some cases. */ |
| 77 | char *xml_tag; /* Name used in XML tag */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Generalized functions for dealing with the specific type. |
| 81 | * May be directly invoked by applications. |
| 82 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 83 | asn_struct_free_f *free_struct; /* Free the structure */ |
| 84 | asn_struct_print_f *print_struct; /* Human readable output */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | asn_constr_check_f *check_constraints; /* Constraints validator */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 86 | ber_type_decoder_f *ber_decoder; /* Generic BER decoder */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | der_type_encoder_f *der_encoder; /* Canonical DER encoder */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 88 | xer_type_decoder_f *xer_decoder; /* Generic XER decoder */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 89 | xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 91 | /*********************************************************************** |
| 92 | * Internally useful members. Not to be used by applications directly. * |
| 93 | **********************************************************************/ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 96 | * Tags that are expected to occur. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 98 | asn_outmost_tag_f *outmost_tag; /* <optional, internal> */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 99 | ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | int tags_count; /* Number of tags which are expected */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 101 | ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */ |
| 102 | int all_tags_count; /* Number of tags */ |
| 103 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | /* |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 105 | * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). |
| 106 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 107 | struct asn_TYPE_member_s *elements; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 108 | int elements_count; |
| 109 | |
| 110 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | * Additional information describing the type, used by appropriate |
| 112 | * functions above. |
| 113 | */ |
| 114 | void *specifics; |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 115 | } asn_TYPE_descriptor_t; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 118 | * This type describes an element of the constructed type, |
| 119 | * i.e. SEQUENCE, SET, CHOICE, etc. |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 120 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 121 | enum asn_TYPE_flags_e { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 122 | ATF_NOFLAGS, |
| 123 | ATF_POINTER = 0x01, /* Represented by the pointer */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 124 | ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 125 | }; |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 126 | typedef struct asn_TYPE_member_s { |
| 127 | enum asn_TYPE_flags_e flags; /* Element's presentation flags */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 128 | int optional; /* Following optional members, including current */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 129 | int memb_offset; /* Offset of the element */ |
| 130 | ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ |
| 131 | int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 132 | asn_TYPE_descriptor_t *type; /* Member type descriptor */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 133 | asn_constr_check_f *memb_constraints; /* Constraints validator */ |
| 134 | char *name; /* ASN.1 identifier of the element */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 135 | } asn_TYPE_member_t; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 138 | * BER tag to element number mapping. |
| 139 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 140 | typedef struct asn_TYPE_tag2member_s { |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 141 | ber_tlv_tag_t el_tag; /* Outmost tag of the member */ |
| 142 | int el_no; /* Index of the associated member, base 0 */ |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 143 | int toff_first; /* First occurence of the el_tag, relative */ |
| 144 | int toff_last; /* Last occurence of the el_tag, relatvie */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 145 | } asn_TYPE_tag2member_t; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 146 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 147 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | * This function is a wrapper around (td)->print_struct, which prints out |
| 149 | * the contents of the target language's structure (struct_ptr) into the |
| 150 | * file pointer (stream) in human readable form. |
| 151 | * RETURN VALUES: |
| 152 | * 0: The structure is printed. |
| 153 | * -1: Problem dumping the structure. |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 154 | * (See also xer_fprint() in xer_encoder.h) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | */ |
| 156 | int asn_fprint(FILE *stream, /* Destination stream descriptor */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 157 | asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | const void *struct_ptr); /* Structure to be printed */ |
| 159 | |
| 160 | #endif /* _CONSTR_TYPE_H_ */ |