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 | */ |
| 11 | #ifndef _CONSTR_TYPE_H |
| 12 | #define _CONSTR_TYPE_H |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | |
| 14 | #include <asn_types.h> /* System-dependent types */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame^] | 15 | |
| 16 | struct asn1_TYPE_descriptor_s; /* Forward declaration */ |
| 17 | struct asn1_TYPE_member_s; /* Forward declaration */ |
| 18 | |
| 19 | /* |
| 20 | * Type of the return value of the encoding functions (der_encode, xer_encode). |
| 21 | */ |
| 22 | typedef struct asn_enc_rval_s { |
| 23 | /* |
| 24 | * Number of bytes encoded. |
| 25 | * -1 indicates failure to encode the structure. |
| 26 | * In this case, the members below this one are meaningful. |
| 27 | */ |
| 28 | ssize_t encoded; |
| 29 | |
| 30 | /* |
| 31 | * Members meaningful when (encoded == -1), for post mortem analysis. |
| 32 | */ |
| 33 | |
| 34 | /* Type which cannot be encoded */ |
| 35 | struct asn1_TYPE_descriptor_s *failed_type; |
| 36 | |
| 37 | /* Pointer to the structure of that type */ |
| 38 | void *structure_ptr; |
| 39 | } asn_enc_rval_t; |
| 40 | #define _ASN_ENCODE_FAILED do { \ |
| 41 | asn_enc_rval_t __er = { -1, td, sptr }; \ |
| 42 | return __er; \ |
| 43 | } while(0) |
| 44 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | #include <ber_tlv_length.h> |
| 46 | #include <ber_tlv_tag.h> |
| 47 | #include <ber_decoder.h> |
| 48 | #include <der_encoder.h> |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame^] | 49 | #include <xer_encoder.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | #include <constraints.h> |
| 51 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 52 | /* |
| 53 | * Free the structure according to its specification. |
| 54 | * If (free_contents_only) is set, the wrapper structure itself (struct_ptr) |
| 55 | * will not be freed. (It may be useful in case the structure is allocated |
| 56 | * statically or arranged on the stack, yet its elements are allocated |
| 57 | * dynamically.) |
| 58 | */ |
| 59 | typedef void (asn_struct_free_f)( |
| 60 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 61 | void *struct_ptr, int free_contents_only); |
| 62 | |
| 63 | /* |
| 64 | * Print the structure according to its specification. |
| 65 | */ |
| 66 | typedef int (asn_struct_print_f)( |
| 67 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 68 | const void *struct_ptr, |
| 69 | int level, /* Indentation level */ |
| 70 | asn_app_consume_bytes_f *callback, void *app_key); |
| 71 | |
| 72 | /* |
| 73 | * Return the outmost tag of the type. |
| 74 | * If the type is untagged CHOICE, the dynamic operation is performed. |
| 75 | * NOTE: This function pointer type is only useful internally. |
| 76 | * Do not use it in your application. |
| 77 | */ |
| 78 | typedef ber_tlv_tag_t (asn_outmost_tag_f)( |
| 79 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 80 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 81 | /* The instance of the above function type; used internally. */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | asn_outmost_tag_f asn1_TYPE_outmost_tag; |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * The definitive description of the destination language's structure. |
| 87 | */ |
| 88 | typedef struct asn1_TYPE_descriptor_s { |
| 89 | char *name; /* A name of the ASN.1 type */ |
| 90 | |
| 91 | /* |
| 92 | * Generalized functions for dealing with the specific type. |
| 93 | * May be directly invoked by applications. |
| 94 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame^] | 95 | asn_struct_free_f *free_struct; /* Free the structure */ |
| 96 | asn_struct_print_f *print_struct; /* Human readable output */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | asn_constr_check_f *check_constraints; /* Constraints validator */ |
| 98 | ber_type_decoder_f *ber_decoder; /* Free-form BER decoder */ |
| 99 | der_type_encoder_f *der_encoder; /* Canonical DER encoder */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame^] | 100 | int (*xer_decoder);/* PLACEHOLDER */ /* Free-form XER decoder */ |
| 101 | xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Functions used internally. Should not be used by applications. |
| 105 | */ |
| 106 | asn_outmost_tag_f *outmost_tag; /* <optional, internal> */ |
| 107 | |
| 108 | /* |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 109 | * Tags that are expected to occur. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 111 | ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | int tags_count; /* Number of tags which are expected */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 113 | ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */ |
| 114 | int all_tags_count; /* Number of tags */ |
| 115 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | int last_tag_form; /* Acceptable form of the tag (prim, constr) */ |
| 117 | |
| 118 | /* |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 119 | * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). |
| 120 | */ |
| 121 | struct asn1_TYPE_member_s *elements; |
| 122 | int elements_count; |
| 123 | |
| 124 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | * Additional information describing the type, used by appropriate |
| 126 | * functions above. |
| 127 | */ |
| 128 | void *specifics; |
| 129 | } asn1_TYPE_descriptor_t; |
| 130 | |
| 131 | /* |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 132 | * An element of the constructed type, i.e. SEQUENCE, SET, CHOICE. |
| 133 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 134 | enum asn1_TYPE_flags_e { |
| 135 | ATF_NOFLAGS, |
| 136 | ATF_POINTER = 0x01, /* Represented by the pointer */ |
| 137 | ATF_OPEN_TYPE = 0x02, /* ANY type, without meaningful tag */ |
| 138 | }; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 139 | typedef struct asn1_TYPE_member_s { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 140 | enum asn1_TYPE_flags_e flags; /* Element's presentation flags */ |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 141 | int optional; /* Following optional members, including current */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 142 | int memb_offset; /* Offset of the element */ |
| 143 | ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ |
| 144 | int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ |
| 145 | asn1_TYPE_descriptor_t *type; /* Member type descriptor */ |
| 146 | asn_constr_check_f *memb_constraints; /* Constraints validator */ |
| 147 | char *name; /* ASN.1 identifier of the element */ |
| 148 | } asn1_TYPE_member_t; |
| 149 | |
| 150 | /* |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 151 | * BER tag to element number mapping. |
| 152 | */ |
| 153 | typedef struct asn1_TYPE_tag2member_s { |
| 154 | ber_tlv_tag_t el_tag; /* Outmost tag of the member */ |
| 155 | int el_no; /* Index of the associated member, base 0 */ |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 156 | int toff_first; /* First occurence of the el_tag, relative */ |
| 157 | int toff_last; /* Last occurence of the el_tag, relatvie */ |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 158 | } asn1_TYPE_tag2member_t; |
| 159 | |
| 160 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 161 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | * This function is a wrapper around (td)->print_struct, which prints out |
| 163 | * the contents of the target language's structure (struct_ptr) into the |
| 164 | * file pointer (stream) in human readable form. |
| 165 | * RETURN VALUES: |
| 166 | * 0: The structure is printed. |
| 167 | * -1: Problem dumping the structure. |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame^] | 168 | * (See also xer_fprint() in xer_encoder.h) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | */ |
| 170 | int asn_fprint(FILE *stream, /* Destination stream descriptor */ |
| 171 | asn1_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ |
| 172 | const void *struct_ptr); /* Structure to be printed */ |
| 173 | |
| 174 | #endif /* _CONSTR_TYPE_H_ */ |