X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/b27841d687ec9e84983340b5581376dfb24010ea..b2b89723a2427bba8290bd6967a1ab39cbb630be:/libs/smux/include/stg/constr_TYPE.h diff --git a/libs/smux/include/stg/constr_TYPE.h b/libs/smux/include/stg/constr_TYPE.h index a9cd86dc..10bb0f36 100644 --- a/libs/smux/include/stg/constr_TYPE.h +++ b/libs/smux/include/stg/constr_TYPE.h @@ -1,6 +1,5 @@ -/*- - * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin . - * All rights reserved. +/* + * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. * Redistribution and modifications are permitted subject to BSD license. */ /* @@ -42,29 +41,77 @@ typedef struct asn_struct_ctx_s { #include /* Packet Encoding Rules decoder */ #include /* Packet Encoding Rules encoder */ #include /* Subtype constraints support */ +#include /* Random structures support */ + +#ifdef ASN_DISABLE_OER_SUPPORT +typedef void (oer_type_decoder_f)(void); +typedef void (oer_type_encoder_f)(void); +typedef void asn_oer_constraints_t; +#else +#include /* Octet Encoding Rules encoder */ +#include /* Octet Encoding Rules encoder */ +#endif /* * Free the structure according to its specification. - * If (free_contents_only) is set, the wrapper structure itself (struct_ptr) - * will not be freed. (It may be useful in case the structure is allocated - * statically or arranged on the stack, yet its elements are allocated - * dynamically.) + * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead. + * Do not use directly. */ +enum asn_struct_free_method { + ASFM_FREE_EVERYTHING, /* free(struct_ptr) and underlying members */ + ASFM_FREE_UNDERLYING, /* free underlying members */ + ASFM_FREE_UNDERLYING_AND_RESET /* FREE_UNDERLYING + memset(0) */ +}; typedef void (asn_struct_free_f)( - struct asn_TYPE_descriptor_s *type_descriptor, - void *struct_ptr, int free_contents_only); -#define ASN_STRUCT_FREE(asn_DEF, ptr) (asn_DEF).free_struct(&(asn_DEF),ptr,0) -#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \ - (asn_DEF).free_struct(&(asn_DEF),ptr,1) + const struct asn_TYPE_descriptor_s *type_descriptor, + void *struct_ptr, enum asn_struct_free_method); + +/* + * Free the structure including freeing the memory pointed to by ptr itself. + */ +#define ASN_STRUCT_FREE(asn_DEF, ptr) \ + (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING) + +/* + * Free the memory used by the members of the structure without freeing the + * the structure pointer itself. + * ZERO-OUT the structure to the safe clean state. + * (Retaining the pointer may be useful in case the structure is allocated + * statically or arranged on the stack, yet its elements are dynamic.) + */ +#define ASN_STRUCT_RESET(asn_DEF, ptr) \ + (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET) + +/* + * Free memory used by the members of the structure without freeing + * the structure pointer itself. + * (Retaining the pointer may be useful in case the structure is allocated + * statically or arranged on the stack, yet its elements are dynamic.) + * AVOID using it in the application code; + * Use a safer ASN_STRUCT_RESET() instead. + */ +#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \ + (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING) /* * Print the structure according to its specification. */ -typedef int (asn_struct_print_f)( - struct asn_TYPE_descriptor_s *type_descriptor, - const void *struct_ptr, - int level, /* Indentation level */ - asn_app_consume_bytes_f *callback, void *app_key); +typedef int(asn_struct_print_f)( + const struct asn_TYPE_descriptor_s *type_descriptor, + const void *struct_ptr, + int level, /* Indentation level */ + asn_app_consume_bytes_f *callback, void *app_key); + +/* + * Compare two structs between each other. + * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater", + * and =0 if "equal to", for some type-specific, stable definition of + * "smaller", "greater" and "equal to". + */ +typedef int (asn_struct_compare_f)( + const struct asn_TYPE_descriptor_s *type_descriptor, + const void *struct_A, + const void *struct_B); /* * Return the outmost tag of the type. @@ -78,54 +125,86 @@ typedef ber_tlv_tag_t (asn_outmost_tag_f)( /* The instance of the above function type; used internally. */ asn_outmost_tag_f asn_TYPE_outmost_tag; - +/* + * Fetch the desired type of the Open Type based on the + * Information Object Set driven constraints. + */ +typedef struct asn_type_selector_result_s { + const struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */ + unsigned presence_index; /* Associated choice variant. */ +} asn_type_selector_result_t; +typedef asn_type_selector_result_t(asn_type_selector_f)( + const struct asn_TYPE_descriptor_s *parent_type_descriptor, + const void *parent_structure_ptr); + +/* + * Generalized functions for dealing with the speciic type. + * May be directly invoked by applications. + */ +typedef struct asn_TYPE_operation_s { + asn_struct_free_f *free_struct; /* Free the structure */ + asn_struct_print_f *print_struct; /* Human readable output */ + asn_struct_compare_f *compare_struct; /* Compare two structures */ + ber_type_decoder_f *ber_decoder; /* Generic BER decoder */ + der_type_encoder_f *der_encoder; /* Canonical DER encoder */ + xer_type_decoder_f *xer_decoder; /* Generic XER decoder */ + xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ + oer_type_decoder_f *oer_decoder; /* Generic OER decoder */ + oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */ + per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */ + per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */ + asn_random_fill_f *random_fill; /* Initialize with a random value */ + asn_outmost_tag_f *outmost_tag; /* */ +} asn_TYPE_operation_t; + +/* + * A constraints tuple specifying both the OER and PER constraints. + */ +typedef struct asn_encoding_constraints_s { + const struct asn_oer_constraints_s *oer_constraints; + const struct asn_per_constraints_s *per_constraints; + asn_constr_check_f *general_constraints; +} asn_encoding_constraints_t; + /* * The definitive description of the destination language's structure. */ typedef struct asn_TYPE_descriptor_s { - const char *name; /* A name of the ASN.1 type. "" in some cases. */ - const char *xml_tag; /* Name used in XML tag */ - - /* - * Generalized functions for dealing with the specific type. - * May be directly invoked by applications. - */ - asn_struct_free_f *free_struct; /* Free the structure */ - asn_struct_print_f *print_struct; /* Human readable output */ - asn_constr_check_f *check_constraints; /* Constraints validator */ - ber_type_decoder_f *ber_decoder; /* Generic BER decoder */ - der_type_encoder_f *der_encoder; /* Canonical DER encoder */ - xer_type_decoder_f *xer_decoder; /* Generic XER decoder */ - xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */ - per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */ - per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */ - - /*********************************************************************** - * Internally useful members. Not to be used by applications directly. * - **********************************************************************/ - - /* - * Tags that are expected to occur. - */ - asn_outmost_tag_f *outmost_tag; /* */ - const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ - int tags_count; /* Number of tags which are expected */ - const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */ - int all_tags_count; /* Number of tags */ - - asn_per_constraints_t *per_constraints; /* PER compiled constraints */ - - /* - * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). - */ - struct asn_TYPE_member_s *elements; - int elements_count; - - /* - * Additional information describing the type, used by appropriate - * functions above. - */ - const void *specifics; + const char *name; /* A name of the ASN.1 type. "" in some cases. */ + const char *xml_tag; /* Name used in XML tag */ + + /* + * Generalized functions for dealing with the specific type. + * May be directly invoked by applications. + */ + asn_TYPE_operation_t *op; + + /*********************************************************************** + * Internally useful members. Not to be used by applications directly. * + **********************************************************************/ + + /* + * Tags that are expected to occur. + */ + const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */ + unsigned tags_count; /* Number of tags which are expected */ + const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */ + unsigned all_tags_count; /* Number of tags */ + + /* OER, PER, and general constraints */ + asn_encoding_constraints_t encoding_constraints; + + /* + * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE). + */ + struct asn_TYPE_member_s *elements; + unsigned elements_count; + + /* + * Additional information describing the type, used by appropriate + * functions above. + */ + const void *specifics; } asn_TYPE_descriptor_t; /* @@ -133,45 +212,46 @@ typedef struct asn_TYPE_descriptor_s { * i.e. SEQUENCE, SET, CHOICE, etc. */ enum asn_TYPE_flags_e { - ATF_NOFLAGS, - ATF_POINTER = 0x01, /* Represented by the pointer */ - ATF_OPEN_TYPE = 0x02 /* ANY type, without meaningful tag */ + ATF_NOFLAGS, + ATF_POINTER = 0x01, /* Represented by the pointer */ + ATF_OPEN_TYPE = 0x02, /* Open Type */ + ATF_ANY_TYPE = 0x04 /* ANY type (deprecated!) */ }; typedef struct asn_TYPE_member_s { - enum asn_TYPE_flags_e flags; /* Element's presentation flags */ - int optional; /* Following optional members, including current */ - int memb_offset; /* Offset of the element */ - ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ - int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ - asn_TYPE_descriptor_t *type; /* Member type descriptor */ - asn_constr_check_f *memb_constraints; /* Constraints validator */ - asn_per_constraints_t *per_constraints; /* PER compiled constraints */ - int (*default_value)(int setval, void **sptr); /* DEFAULT */ - const char *name; /* ASN.1 identifier of the element */ + enum asn_TYPE_flags_e flags; /* Element's presentation flags */ + unsigned optional; /* Following optional members, including current */ + unsigned memb_offset; /* Offset of the element */ + ber_tlv_tag_t tag; /* Outmost (most immediate) tag */ + int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */ + asn_TYPE_descriptor_t *type; /* Member type descriptor */ + asn_type_selector_f *type_selector; /* IoS runtime type selector */ + asn_encoding_constraints_t encoding_constraints; + int (*default_value_cmp)(const void *sptr); /* Compare DEFAULT */ + int (*default_value_set)(void **sptr); /* Set DEFAULT */ + const char *name; /* ASN.1 identifier of the element */ } asn_TYPE_member_t; /* * BER tag to element number mapping. */ typedef struct asn_TYPE_tag2member_s { - ber_tlv_tag_t el_tag; /* Outmost tag of the member */ - int el_no; /* Index of the associated member, base 0 */ - int toff_first; /* First occurence of the el_tag, relative */ - int toff_last; /* Last occurence of the el_tag, relatvie */ + ber_tlv_tag_t el_tag; /* Outmost tag of the member */ + unsigned el_no; /* Index of the associated member, base 0 */ + int toff_first; /* First occurence of the el_tag, relative */ + int toff_last; /* Last occurence of the el_tag, relative */ } asn_TYPE_tag2member_t; /* - * This function is a wrapper around (td)->print_struct, which prints out - * the contents of the target language's structure (struct_ptr) into the - * file pointer (stream) in human readable form. + * This function prints out the contents of the target language's structure + * (struct_ptr) into the file pointer (stream) in human readable form. * RETURN VALUES: * 0: The structure is printed. * -1: Problem dumping the structure. * (See also xer_fprint() in xer_encoder.h) */ -int asn_fprint(FILE *stream, /* Destination stream descriptor */ - asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ - const void *struct_ptr); /* Structure to be printed */ +int asn_fprint(FILE *stream, /* Destination stream descriptor */ + const asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */ + const void *struct_ptr); /* Structure to be printed */ #ifdef __cplusplus }