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 | */ |
| 5 | #ifndef _CONSTR_TYPE_H_ |
| 6 | #define _CONSTR_TYPE_H_ |
| 7 | |
| 8 | #include <asn_types.h> /* System-dependent types */ |
| 9 | #include <ber_tlv_length.h> |
| 10 | #include <ber_tlv_tag.h> |
| 11 | #include <ber_decoder.h> |
| 12 | #include <der_encoder.h> |
| 13 | #include <constraints.h> |
| 14 | |
| 15 | struct asn1_TYPE_descriptor_s; /* Forward declaration */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 16 | struct asn1_TYPE_member_s; /* Forward declaration */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Free the structure according to its specification. |
| 20 | * If (free_contents_only) is set, the wrapper structure itself (struct_ptr) |
| 21 | * will not be freed. (It may be useful in case the structure is allocated |
| 22 | * statically or arranged on the stack, yet its elements are allocated |
| 23 | * dynamically.) |
| 24 | */ |
| 25 | typedef void (asn_struct_free_f)( |
| 26 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 27 | void *struct_ptr, int free_contents_only); |
| 28 | |
| 29 | /* |
| 30 | * Print the structure according to its specification. |
| 31 | */ |
| 32 | typedef int (asn_struct_print_f)( |
| 33 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 34 | const void *struct_ptr, |
| 35 | int level, /* Indentation level */ |
| 36 | asn_app_consume_bytes_f *callback, void *app_key); |
| 37 | |
| 38 | /* |
| 39 | * Return the outmost tag of the type. |
| 40 | * If the type is untagged CHOICE, the dynamic operation is performed. |
| 41 | * NOTE: This function pointer type is only useful internally. |
| 42 | * Do not use it in your application. |
| 43 | */ |
| 44 | typedef ber_tlv_tag_t (asn_outmost_tag_f)( |
| 45 | struct asn1_TYPE_descriptor_s *type_descriptor, |
| 46 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag); |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 47 | /* The instance of the above function type; used internally. */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | asn_outmost_tag_f asn1_TYPE_outmost_tag; |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * The definitive description of the destination language's structure. |
| 53 | */ |
| 54 | typedef struct asn1_TYPE_descriptor_s { |
| 55 | char *name; /* A name of the ASN.1 type */ |
| 56 | |
| 57 | /* |
| 58 | * Generalized functions for dealing with the specific type. |
| 59 | * May be directly invoked by applications. |
| 60 | */ |
| 61 | asn_constr_check_f *check_constraints; /* Constraints validator */ |
| 62 | ber_type_decoder_f *ber_decoder; /* Free-form BER decoder */ |
| 63 | der_type_encoder_f *der_encoder; /* Canonical DER encoder */ |
| 64 | asn_struct_print_f *print_struct; /* Human readable output */ |
| 65 | asn_struct_free_f *free_struct; /* Free the structure */ |
| 66 | |
| 67 | /* |
| 68 | * Functions used internally. Should not be used by applications. |
| 69 | */ |
| 70 | asn_outmost_tag_f *outmost_tag; /* <optional, internal> */ |
| 71 | |
| 72 | /* |
| 73 | * Tags that are expected, with some of their vital properties. |
| 74 | */ |
| 75 | ber_tlv_tag_t *tags; /* At least one tag must be specified */ |
| 76 | int tags_count; /* Number of tags which are expected */ |
| 77 | int tags_impl_skip; /* Tags to skip in implicit mode */ |
| 78 | int last_tag_form; /* Acceptable form of the tag (prim, constr) */ |
| 79 | |
| 80 | /* |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 81 | * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). |
| 82 | */ |
| 83 | struct asn1_TYPE_member_s *elements; |
| 84 | int elements_count; |
| 85 | |
| 86 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | * Additional information describing the type, used by appropriate |
| 88 | * functions above. |
| 89 | */ |
| 90 | void *specifics; |
| 91 | } asn1_TYPE_descriptor_t; |
| 92 | |
| 93 | /* |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 94 | * An element of the constructed type, i.e. SEQUENCE, SET, CHOICE. |
| 95 | */ |
| 96 | typedef struct asn1_TYPE_member_s { |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 97 | enum asn1_TYPE_flags_e { |
| 98 | ATF_NOFLAGS, |
| 99 | ATF_POINTER = 0x01, /* Represented by the pointer */ |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 100 | ATF_OPEN_TYPE = 0x02, /* ANY type, without meaningful tag */ |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 101 | } flags; /* Element's presentation flags */ |
| 102 | int optional; /* Following optional members, including current */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 103 | int memb_offset; /* Offset of the element */ |
| 104 | ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ |
| 105 | int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ |
| 106 | asn1_TYPE_descriptor_t *type; /* Member type descriptor */ |
| 107 | asn_constr_check_f *memb_constraints; /* Constraints validator */ |
| 108 | char *name; /* ASN.1 identifier of the element */ |
| 109 | } asn1_TYPE_member_t; |
| 110 | |
| 111 | /* |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 112 | * BER tag to element number mapping. |
| 113 | */ |
| 114 | typedef struct asn1_TYPE_tag2member_s { |
| 115 | ber_tlv_tag_t el_tag; /* Outmost tag of the member */ |
| 116 | int el_no; /* Index of the associated member, base 0 */ |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 117 | int toff_first; /* First occurence of the el_tag, relative */ |
| 118 | int toff_last; /* Last occurence of the el_tag, relatvie */ |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 119 | } asn1_TYPE_tag2member_t; |
| 120 | |
| 121 | |
vlm | 3147308 | 2004-06-06 07:20:02 +0000 | [diff] [blame] | 122 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | * This function is a wrapper around (td)->print_struct, which prints out |
| 124 | * the contents of the target language's structure (struct_ptr) into the |
| 125 | * file pointer (stream) in human readable form. |
| 126 | * RETURN VALUES: |
| 127 | * 0: The structure is printed. |
| 128 | * -1: Problem dumping the structure. |
| 129 | */ |
| 130 | int asn_fprint(FILE *stream, /* Destination stream descriptor */ |
| 131 | asn1_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ |
| 132 | const void *struct_ptr); /* Structure to be printed */ |
| 133 | |
| 134 | #endif /* _CONSTR_TYPE_H_ */ |