Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | /* |
| 7 | * This file contains the declaration structure called "ASN.1 Type Definition", |
| 8 | * which holds all information necessary for encoding and decoding routines. |
| 9 | * This structure even contains pointer to these encoding and decoding routines |
| 10 | * for each defined ASN.1 type. |
| 11 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 12 | #ifndef _CONSTR_TYPE_H_ |
| 13 | #define _CONSTR_TYPE_H_ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 15 | #include <ber_tlv_length.h> |
| 16 | #include <ber_tlv_tag.h> |
| 17 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 22 | struct asn_TYPE_descriptor_s; /* Forward declaration */ |
| 23 | struct asn_TYPE_member_s; /* Forward declaration */ |
| 24 | |
| 25 | /* |
| 26 | * This type provides the context information for various ASN.1 routines, |
| 27 | * primarily ones doing decoding. A member _asn_ctx of this type must be |
| 28 | * included into certain target language's structures, such as compound types. |
| 29 | */ |
| 30 | typedef struct asn_struct_ctx_s { |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 31 | short phase; /* Decoding phase */ |
| 32 | short step; /* Elementary step of a phase */ |
| 33 | int context; /* Other context information */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 34 | void *ptr; /* Decoder-specific stuff (stack elements) */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 35 | ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 36 | } asn_struct_ctx_t; |
| 37 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 38 | #include <ber_decoder.h> /* Basic Encoding Rules decoder */ |
| 39 | #include <der_encoder.h> /* Distinguished Encoding Rules encoder */ |
| 40 | #include <xer_decoder.h> /* Decoder of XER (XML, text) */ |
| 41 | #include <xer_encoder.h> /* Encoder into XER (XML, text) */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 42 | #include <per_decoder.h> /* Packet Encoding Rules decoder */ |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 43 | #include <per_encoder.h> /* Packet Encoding Rules encoder */ |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 44 | #include <constraints.h> /* Subtype constraints support */ |
| 45 | |
| 46 | #ifdef ASN_DISABLE_OER_SUPPORT |
Lev Walkin | 049a2d7 | 2017-08-27 18:34:02 -0700 | [diff] [blame] | 47 | typedef void (oer_type_decoder_f)(); |
| 48 | typedef void (oer_type_encoder_f)(); |
Lev Walkin | b5cdc5d | 2017-09-15 21:57:46 -0700 | [diff] [blame] | 49 | typedef void asn_oer_constraints_t; |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 50 | #else |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 51 | #include <oer_decoder.h> /* Octet Encoding Rules encoder */ |
| 52 | #include <oer_encoder.h> /* Octet Encoding Rules encoder */ |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 53 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Free the structure according to its specification. |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 57 | * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead. |
| 58 | * Do not use directly. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | */ |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 60 | enum asn_struct_free_method { |
| 61 | ASFM_FREE_EVERYTHING, /* free(struct_ptr) and underlying members */ |
| 62 | ASFM_FREE_UNDERLYING, /* free underlying members */ |
| 63 | ASFM_FREE_UNDERLYING_AND_RESET /* FREE_UNDERLYING + memset(0) */ |
| 64 | }; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | typedef void (asn_struct_free_f)( |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 66 | const struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 67 | void *struct_ptr, enum asn_struct_free_method); |
| 68 | |
| 69 | /* |
| 70 | * Free the structure including freeing the memory pointed to by ptr itself. |
| 71 | */ |
| 72 | #define ASN_STRUCT_FREE(asn_DEF, ptr) \ |
| 73 | (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING) |
| 74 | |
| 75 | /* |
| 76 | * Free the memory used by the members of the structure without freeing the |
| 77 | * the structure pointer itself. |
| 78 | * ZERO-OUT the structure to the safe clean state. |
| 79 | * (Retaining the pointer may be useful in case the structure is allocated |
| 80 | * statically or arranged on the stack, yet its elements are dynamic.) |
| 81 | */ |
| 82 | #define ASN_STRUCT_RESET(asn_DEF, ptr) \ |
| 83 | (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET) |
| 84 | |
| 85 | /* |
| 86 | * Free memory used by the members of the structure without freeing |
| 87 | * the structure pointer itself. |
| 88 | * (Retaining the pointer may be useful in case the structure is allocated |
| 89 | * statically or arranged on the stack, yet its elements are dynamic.) |
| 90 | * AVOID using it in the application code; |
| 91 | * Use a safer ASN_STRUCT_RESET() instead. |
| 92 | */ |
| 93 | #define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \ |
| 94 | (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Print the structure according to its specification. |
| 98 | */ |
| 99 | typedef int (asn_struct_print_f)( |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 100 | struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | f15320b | 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 | /* |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 106 | * Compare two structs between each other. |
| 107 | * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater", |
| 108 | * and =0 if "equal to", for some type-specific, stable definition of |
| 109 | * "smaller", "greater" and "equal to". |
| 110 | */ |
| 111 | typedef int (asn_struct_compare_f)( |
| 112 | const struct asn_TYPE_descriptor_s *type_descriptor, |
| 113 | const void *struct_A, |
| 114 | const void *struct_B); |
| 115 | |
| 116 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | * Return the outmost tag of the type. |
| 118 | * If the type is untagged CHOICE, the dynamic operation is performed. |
| 119 | * NOTE: This function pointer type is only useful internally. |
| 120 | * Do not use it in your application. |
| 121 | */ |
| 122 | typedef ber_tlv_tag_t (asn_outmost_tag_f)( |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 123 | const struct asn_TYPE_descriptor_s *type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 125 | /* The instance of the above function type; used internally. */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 126 | asn_outmost_tag_f asn_TYPE_outmost_tag; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Fetch the desired type of the Open Type based on the |
| 130 | * Information Object Set driven constraints. |
| 131 | */ |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 132 | typedef struct asn_type_selector_result_s { |
| 133 | struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */ |
| 134 | unsigned presence_index; /* Associated choice variant. */ |
| 135 | } asn_type_selector_result_t; |
| 136 | typedef asn_type_selector_result_t(asn_type_selector_f)( |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 137 | const struct asn_TYPE_descriptor_s *parent_type_descriptor, |
| 138 | const void *parent_structure_ptr); |
| 139 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | /* |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 141 | * Generalized functions for dealing with the speciic type. |
| 142 | * May be directly invoked by applications. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 144 | typedef struct asn_TYPE_operation_s { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 145 | asn_struct_free_f *free_struct; /* Free the structure */ |
| 146 | asn_struct_print_f *print_struct; /* Human readable output */ |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 147 | asn_struct_compare_f *compare_struct; /* Compare two structures */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 148 | ber_type_decoder_f *ber_decoder; /* Generic BER decoder */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | der_type_encoder_f *der_encoder; /* Canonical DER encoder */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 150 | xer_type_decoder_f *xer_decoder; /* Generic XER decoder */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 151 | xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 152 | oer_type_decoder_f *oer_decoder; /* Generic OER decoder */ |
| 153 | oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 154 | per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */ |
| 155 | per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 156 | asn_outmost_tag_f *outmost_tag; /* <optional, internal> */ |
| 157 | } asn_TYPE_operation_t; |
| 158 | |
| 159 | /* |
| 160 | * The definitive description of the destination language's structure. |
| 161 | */ |
| 162 | typedef struct asn_TYPE_descriptor_s { |
| 163 | const char *name; /* A name of the ASN.1 type. "" in some cases. */ |
| 164 | const char *xml_tag; /* Name used in XML tag */ |
| 165 | |
| 166 | /* |
| 167 | * Generalized functions for dealing with the specific type. |
| 168 | * May be directly invoked by applications. |
| 169 | */ |
| 170 | asn_TYPE_operation_t *op; |
| 171 | asn_constr_check_f *check_constraints; /* Constraints validator */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 173 | /*********************************************************************** |
| 174 | * Internally useful members. Not to be used by applications directly. * |
| 175 | **********************************************************************/ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 178 | * Tags that are expected to occur. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 180 | const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 181 | unsigned tags_count; /* Number of tags which are expected */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 182 | const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 183 | unsigned all_tags_count; /* Number of tags */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 184 | |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 185 | asn_oer_constraints_t *oer_constraints; /* OER constraints */ |
| 186 | asn_per_constraints_t *per_constraints; /* PER constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 187 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | /* |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 189 | * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). |
| 190 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 191 | struct asn_TYPE_member_s *elements; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 192 | unsigned elements_count; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 195 | * Additional information describing the type, used by appropriate |
| 196 | * functions above. |
| 197 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 198 | const void *specifics; |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 199 | } asn_TYPE_descriptor_t; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | |
| 201 | /* |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 202 | * This type describes an element of the constructed type, |
| 203 | * i.e. SEQUENCE, SET, CHOICE, etc. |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 204 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 205 | enum asn_TYPE_flags_e { |
Lev Walkin | 14fd3e5 | 2017-08-27 01:38:45 -0700 | [diff] [blame] | 206 | ATF_NOFLAGS, |
| 207 | ATF_POINTER = 0x01, /* Represented by the pointer */ |
| 208 | ATF_OPEN_TYPE = 0x02, /* Open Type */ |
| 209 | ATF_ANY_TYPE = 0x04 /* ANY type (deprecated!) */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 210 | }; |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 211 | typedef struct asn_TYPE_member_s { |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 212 | enum asn_TYPE_flags_e flags; /* Element's presentation flags */ |
| 213 | unsigned optional; /* Following optional members, including current */ |
| 214 | unsigned memb_offset; /* Offset of the element */ |
| 215 | ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ |
| 216 | int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ |
| 217 | asn_TYPE_descriptor_t *type; /* Member type descriptor */ |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 218 | asn_type_selector_f *type_selector; /* IoS runtime type selector */ |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 219 | asn_constr_check_f *memb_constraints; /* Constraints validator */ |
| 220 | asn_oer_constraints_t *oer_constraints; /* OER compiled constraints */ |
| 221 | asn_per_constraints_t *per_constraints; /* PER compiled constraints */ |
| 222 | int (*default_value)(int setval, void **sptr); /* DEFAULT <value> */ |
| 223 | const char *name; /* ASN.1 identifier of the element */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 224 | } asn_TYPE_member_t; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 225 | |
| 226 | /* |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 227 | * BER tag to element number mapping. |
| 228 | */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 229 | typedef struct asn_TYPE_tag2member_s { |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 230 | ber_tlv_tag_t el_tag; /* Outmost tag of the member */ |
| 231 | unsigned el_no; /* Index of the associated member, base 0 */ |
Lev Walkin | d3fbff9 | 2017-08-27 12:39:30 -0700 | [diff] [blame] | 232 | int toff_first; /* First occurence of the el_tag, relative */ |
| 233 | int toff_last; /* Last occurence of the el_tag, relative */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 234 | } asn_TYPE_tag2member_t; |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 235 | |
Lev Walkin | 4c36e30 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 236 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | * This function is a wrapper around (td)->print_struct, which prints out |
| 238 | * the contents of the target language's structure (struct_ptr) into the |
| 239 | * file pointer (stream) in human readable form. |
| 240 | * RETURN VALUES: |
| 241 | * 0: The structure is printed. |
| 242 | * -1: Problem dumping the structure. |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 243 | * (See also xer_fprint() in xer_encoder.h) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | */ |
| 245 | int asn_fprint(FILE *stream, /* Destination stream descriptor */ |
Lev Walkin | 1cbd222 | 2004-09-29 13:24:10 +0000 | [diff] [blame] | 246 | asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | const void *struct_ptr); /* Structure to be printed */ |
| 248 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 249 | #ifdef __cplusplus |
| 250 | } |
| 251 | #endif |
| 252 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | #endif /* _CONSTR_TYPE_H_ */ |