--- /dev/null
+/*\r
+ * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.\r
+ * Redistribution and modifications are permitted subject to BSD license.\r
+ */\r
+/*\r
+ * This file contains the declaration structure called "ASN.1 Type Definition",\r
+ * which holds all information necessary for encoding and decoding routines.\r
+ * This structure even contains pointer to these encoding and decoding routines\r
+ * for each defined ASN.1 type.\r
+ */\r
+#ifndef _CONSTR_TYPE_H_\r
+#define _CONSTR_TYPE_H_\r
+\r
+#include <ber_tlv_length.h>\r
+#include <ber_tlv_tag.h>\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+struct asn_TYPE_descriptor_s; /* Forward declaration */\r
+struct asn_TYPE_member_s; /* Forward declaration */\r
+\r
+/*\r
+ * This type provides the context information for various ASN.1 routines,\r
+ * primarily ones doing decoding. A member _asn_ctx of this type must be\r
+ * included into certain target language's structures, such as compound types.\r
+ */\r
+typedef struct asn_struct_ctx_s {\r
+ short phase; /* Decoding phase */\r
+ short step; /* Elementary step of a phase */\r
+ int context; /* Other context information */\r
+ void *ptr; /* Decoder-specific stuff (stack elements) */\r
+ ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */\r
+} asn_struct_ctx_t;\r
+\r
+#include <ber_decoder.h> /* Basic Encoding Rules decoder */\r
+#include <der_encoder.h> /* Distinguished Encoding Rules encoder */\r
+#include <xer_decoder.h> /* Decoder of XER (XML, text) */\r
+#include <xer_encoder.h> /* Encoder into XER (XML, text) */\r
+#include <per_decoder.h> /* Packet Encoding Rules decoder */\r
+#include <per_encoder.h> /* Packet Encoding Rules encoder */\r
+#include <constraints.h> /* Subtype constraints support */\r
+#include <asn_random_fill.h> /* Random structures support */\r
+\r
+#ifdef ASN_DISABLE_OER_SUPPORT\r
+typedef void (oer_type_decoder_f)(void);\r
+typedef void (oer_type_encoder_f)(void);\r
+typedef void asn_oer_constraints_t;\r
+#else\r
+#include <oer_decoder.h> /* Octet Encoding Rules encoder */\r
+#include <oer_encoder.h> /* Octet Encoding Rules encoder */\r
+#endif\r
+\r
+/*\r
+ * Free the structure according to its specification.\r
+ * Use one of ASN_STRUCT_{FREE,RESET,CONTENTS_ONLY} macros instead.\r
+ * Do not use directly.\r
+ */\r
+enum asn_struct_free_method {\r
+ ASFM_FREE_EVERYTHING, /* free(struct_ptr) and underlying members */\r
+ ASFM_FREE_UNDERLYING, /* free underlying members */\r
+ ASFM_FREE_UNDERLYING_AND_RESET /* FREE_UNDERLYING + memset(0) */\r
+};\r
+typedef void (asn_struct_free_f)(\r
+ const struct asn_TYPE_descriptor_s *type_descriptor,\r
+ void *struct_ptr, enum asn_struct_free_method);\r
+\r
+/*\r
+ * Free the structure including freeing the memory pointed to by ptr itself.\r
+ */\r
+#define ASN_STRUCT_FREE(asn_DEF, ptr) \\r
+ (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_EVERYTHING)\r
+\r
+/*\r
+ * Free the memory used by the members of the structure without freeing the\r
+ * the structure pointer itself.\r
+ * ZERO-OUT the structure to the safe clean state.\r
+ * (Retaining the pointer may be useful in case the structure is allocated\r
+ * statically or arranged on the stack, yet its elements are dynamic.)\r
+ */\r
+#define ASN_STRUCT_RESET(asn_DEF, ptr) \\r
+ (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING_AND_RESET)\r
+\r
+/*\r
+ * Free memory used by the members of the structure without freeing\r
+ * the structure pointer itself.\r
+ * (Retaining the pointer may be useful in case the structure is allocated\r
+ * statically or arranged on the stack, yet its elements are dynamic.)\r
+ * AVOID using it in the application code;\r
+ * Use a safer ASN_STRUCT_RESET() instead.\r
+ */\r
+#define ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr) \\r
+ (asn_DEF).op->free_struct(&(asn_DEF), (ptr), ASFM_FREE_UNDERLYING)\r
+\r
+/*\r
+ * Print the structure according to its specification.\r
+ */\r
+typedef int(asn_struct_print_f)(\r
+ const struct asn_TYPE_descriptor_s *type_descriptor,\r
+ const void *struct_ptr,\r
+ int level, /* Indentation level */\r
+ asn_app_consume_bytes_f *callback, void *app_key);\r
+\r
+/*\r
+ * Compare two structs between each other.\r
+ * Returns <0 if struct_A is "smaller" than struct_B, >0 if "greater",\r
+ * and =0 if "equal to", for some type-specific, stable definition of\r
+ * "smaller", "greater" and "equal to".\r
+ */\r
+typedef int (asn_struct_compare_f)(\r
+ const struct asn_TYPE_descriptor_s *type_descriptor,\r
+ const void *struct_A,\r
+ const void *struct_B);\r
+\r
+/*\r
+ * Return the outmost tag of the type.\r
+ * If the type is untagged CHOICE, the dynamic operation is performed.\r
+ * NOTE: This function pointer type is only useful internally.\r
+ * Do not use it in your application.\r
+ */\r
+typedef ber_tlv_tag_t (asn_outmost_tag_f)(\r
+ const struct asn_TYPE_descriptor_s *type_descriptor,\r
+ const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag);\r
+/* The instance of the above function type; used internally. */\r
+asn_outmost_tag_f asn_TYPE_outmost_tag;\r
+\r
+/*\r
+ * Fetch the desired type of the Open Type based on the\r
+ * Information Object Set driven constraints.\r
+ */\r
+typedef struct asn_type_selector_result_s {\r
+ const struct asn_TYPE_descriptor_s *type_descriptor; /* Type encoded. */\r
+ unsigned presence_index; /* Associated choice variant. */\r
+} asn_type_selector_result_t;\r
+typedef asn_type_selector_result_t(asn_type_selector_f)(\r
+ const struct asn_TYPE_descriptor_s *parent_type_descriptor,\r
+ const void *parent_structure_ptr);\r
+\r
+/*\r
+ * Generalized functions for dealing with the speciic type.\r
+ * May be directly invoked by applications.\r
+ */\r
+typedef struct asn_TYPE_operation_s {\r
+ asn_struct_free_f *free_struct; /* Free the structure */\r
+ asn_struct_print_f *print_struct; /* Human readable output */\r
+ asn_struct_compare_f *compare_struct; /* Compare two structures */\r
+ ber_type_decoder_f *ber_decoder; /* Generic BER decoder */\r
+ der_type_encoder_f *der_encoder; /* Canonical DER encoder */\r
+ xer_type_decoder_f *xer_decoder; /* Generic XER decoder */\r
+ xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */\r
+ oer_type_decoder_f *oer_decoder; /* Generic OER decoder */\r
+ oer_type_encoder_f *oer_encoder; /* Canonical OER encoder */\r
+ per_type_decoder_f *uper_decoder; /* Unaligned PER decoder */\r
+ per_type_encoder_f *uper_encoder; /* Unaligned PER encoder */\r
+ per_type_decoder_f *aper_decoder; /* Aligned PER decoder */\r
+ per_type_encoder_f *aper_encoder; /* Aligned PER encoder */\r
+ asn_random_fill_f *random_fill; /* Initialize with a random value */\r
+ asn_outmost_tag_f *outmost_tag; /* <optional, internal> */\r
+} asn_TYPE_operation_t;\r
+\r
+/*\r
+ * A constraints tuple specifying both the OER and PER constraints.\r
+ */\r
+typedef struct asn_encoding_constraints_s {\r
+ const struct asn_oer_constraints_s *oer_constraints;\r
+ const struct asn_per_constraints_s *per_constraints;\r
+ asn_constr_check_f *general_constraints;\r
+} asn_encoding_constraints_t;\r
+\r
+/*\r
+ * The definitive description of the destination language's structure.\r
+ */\r
+typedef struct asn_TYPE_descriptor_s {\r
+ const char *name; /* A name of the ASN.1 type. "" in some cases. */\r
+ const char *xml_tag; /* Name used in XML tag */\r
+\r
+ /*\r
+ * Generalized functions for dealing with the specific type.\r
+ * May be directly invoked by applications.\r
+ */\r
+ asn_TYPE_operation_t *op;\r
+\r
+ /***********************************************************************\r
+ * Internally useful members. Not to be used by applications directly. *\r
+ **********************************************************************/\r
+\r
+ /*\r
+ * Tags that are expected to occur.\r
+ */\r
+ const ber_tlv_tag_t *tags; /* Effective tags sequence for this type */\r
+ unsigned tags_count; /* Number of tags which are expected */\r
+ const ber_tlv_tag_t *all_tags; /* Every tag for BER/containment */\r
+ unsigned all_tags_count; /* Number of tags */\r
+\r
+ /* OER, PER, and general constraints */\r
+ asn_encoding_constraints_t encoding_constraints;\r
+\r
+ /*\r
+ * An ASN.1 production type members (members of SEQUENCE, SET, CHOICE).\r
+ */\r
+ struct asn_TYPE_member_s *elements;\r
+ unsigned elements_count;\r
+\r
+ /*\r
+ * Additional information describing the type, used by appropriate\r
+ * functions above.\r
+ */\r
+ const void *specifics;\r
+} asn_TYPE_descriptor_t;\r
+\r
+/*\r
+ * This type describes an element of the constructed type,\r
+ * i.e. SEQUENCE, SET, CHOICE, etc.\r
+ */\r
+ enum asn_TYPE_flags_e {\r
+ ATF_NOFLAGS,\r
+ ATF_POINTER = 0x01, /* Represented by the pointer */\r
+ ATF_OPEN_TYPE = 0x02, /* Open Type */\r
+ ATF_ANY_TYPE = 0x04 /* ANY type (deprecated!) */\r
+ };\r
+typedef struct asn_TYPE_member_s {\r
+ enum asn_TYPE_flags_e flags; /* Element's presentation flags */\r
+ unsigned optional; /* Following optional members, including current */\r
+ unsigned memb_offset; /* Offset of the element */\r
+ ber_tlv_tag_t tag; /* Outmost (most immediate) tag */\r
+ int tag_mode; /* IMPLICIT/no/EXPLICIT tag at current level */\r
+ asn_TYPE_descriptor_t *type; /* Member type descriptor */\r
+ asn_type_selector_f *type_selector; /* IoS runtime type selector */\r
+ asn_encoding_constraints_t encoding_constraints;\r
+ int (*default_value_cmp)(const void *sptr); /* Compare DEFAULT <value> */\r
+ int (*default_value_set)(void **sptr); /* Set DEFAULT <value> */\r
+ const char *name; /* ASN.1 identifier of the element */\r
+} asn_TYPE_member_t;\r
+\r
+/*\r
+ * BER tag to element number mapping.\r
+ */\r
+typedef struct asn_TYPE_tag2member_s {\r
+ ber_tlv_tag_t el_tag; /* Outmost tag of the member */\r
+ unsigned el_no; /* Index of the associated member, base 0 */\r
+ int toff_first; /* First occurence of the el_tag, relative */\r
+ int toff_last; /* Last occurence of the el_tag, relative */\r
+} asn_TYPE_tag2member_t;\r
+\r
+/*\r
+ * This function prints out the contents of the target language's structure\r
+ * (struct_ptr) into the file pointer (stream) in human readable form.\r
+ * RETURN VALUES:\r
+ * 0: The structure is printed.\r
+ * -1: Problem dumping the structure.\r
+ * (See also xer_fprint() in xer_encoder.h)\r
+ */\r
+int asn_fprint(FILE *stream, /* Destination stream descriptor */\r
+ const asn_TYPE_descriptor_t *td, /* ASN.1 type descriptor */\r
+ const void *struct_ptr); /* Structure to be printed */\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* _CONSTR_TYPE_H_ */\r