X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8e80bb9cec2c90dd61f810fb1525932a434288eb..0c097ef3435d2a45c1ee4ac80f8bd3f254fb8df5:/libs/smux/include/stg/OBJECT_IDENTIFIER.h?ds=sidebyside diff --git a/libs/smux/include/stg/OBJECT_IDENTIFIER.h b/libs/smux/include/stg/OBJECT_IDENTIFIER.h index c2c6373e..aa0d5c70 100644 --- a/libs/smux/include/stg/OBJECT_IDENTIFIER.h +++ b/libs/smux/include/stg/OBJECT_IDENTIFIER.h @@ -1,6 +1,5 @@ -/*- - * Copyright (c) 2003, 2004, 2005 Lev Walkin . - * All rights reserved. +/* + * Copyright (c) 2003-2017 Lev Walkin . All rights reserved. * Redistribution and modifications are permitted subject to BSD license. */ #ifndef _OBJECT_IDENTIFIER_H_ @@ -8,70 +7,80 @@ #include #include +#include #ifdef __cplusplus extern "C" { #endif +typedef uint32_t asn_oid_arc_t; +#define ASN_OID_ARC_MAX (~((asn_oid_arc_t)0)) + typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t; extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER; +extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER; asn_struct_print_f OBJECT_IDENTIFIER_print; asn_constr_check_f OBJECT_IDENTIFIER_constraint; der_type_encoder_f OBJECT_IDENTIFIER_encode_der; xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer; xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer; +asn_random_fill_f OBJECT_IDENTIFIER_random_fill; + +#define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free +#define OBJECT_IDENTIFIER_compare OCTET_STRING_compare +#define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive +#define OBJECT_IDENTIFIER_encode_der der_encode_primitive +#define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive +#define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive +#define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper +#define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper /********************************** * Some handy conversion routines * **********************************/ /* - * This function fills an (_arcs) array with OBJECT IDENTIFIER arcs - * up to specified (_arc_slots) elements. - * + * This function fills an (arcs) array with OBJECT IDENTIFIER arcs + * up to specified (arc_slots) elements. + * * EXAMPLE: * void print_arcs(OBJECT_IDENTIFIER_t *oid) { - * unsigned long fixed_arcs[10]; // Try with fixed space first - * unsigned long *arcs = fixed_arcs; - * int arc_type_size = sizeof(fixed_arcs[0]); // sizeof(long) - * int arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10 - * int count; // Real number of arcs. + * asn_oid_arc_t fixed_arcs[10]; // Try with fixed space first + * asn_oid_arc_t *arcs = fixed_arcs; + * size_t arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10 + * ssize_t count; // Real number of arcs. * int i; - * - * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, - * arc_type_size, arc_slots); + * + * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots); * // If necessary, reallocate arcs array and try again. * if(count > arc_slots) { * arc_slots = count; - * arcs = malloc(arc_type_size * arc_slots); + * arcs = malloc(sizeof(asn_oid_arc_t) * arc_slots); * if(!arcs) return; - * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, - * arc_type_size, arc_slots); + * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots); * assert(count == arc_slots); * } - * + * * // Print the contents of the arcs array. * for(i = 0; i < count; i++) - * printf("%d\n", arcs[i]); - * + * printf("%"PRIu32"\n", arcs[i]); + * * // Avoid memory leak. * if(arcs != fixed_arcs) free(arcs); * } - * + * * RETURN VALUES: * -1/EINVAL: Invalid arguments (oid is missing) * -1/ERANGE: One or more arcs have value out of array cell type range. * >=0: Number of arcs contained in the OBJECT IDENTIFIER - * - * WARNING: The function always returns the real number of arcs, - * even if there is no sufficient (_arc_slots) provided. + * + * WARNING: The function always returns the actual number of arcs, + * even if there is no sufficient (arc_slots) provided. */ -int OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *_oid, - void *_arcs, /* e.g., unsigned int arcs[N] */ - unsigned int _arc_type_size, /* e.g., sizeof(arcs[0]) */ - unsigned int _arc_slots /* e.g., N */); +ssize_t OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, + asn_oid_arc_t *arcs, size_t arc_slots); /* * This functions initializes the OBJECT IDENTIFIER object with @@ -83,25 +92,13 @@ int OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *_oid, * -1/ENOMEM: Memory allocation failed * 0: The object was initialized with new arcs. */ -int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *_oid, - const void *_arcs, /* e.g., unsigned int arcs[N] */ - unsigned int _arc_type_size, /* e.g., sizeof(arcs[0]) */ - unsigned int _arc_slots /* e.g., N */); +int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, + const asn_oid_arc_t *arcs, size_t arcs_count); -/* - * Print the specified OBJECT IDENTIFIER arc. - */ -int OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, - int add, /* Arbitrary offset, required to process the first two arcs */ - asn_app_consume_bytes_f *cb, void *app_key); - -/* Same as above, but returns the number of written digits, instead of 0 */ -ssize_t OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add, - asn_app_consume_bytes_f *cb, void *app_key); /* * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363"). - * No arc can exceed the (0..signed_long_max) range (typically, 0..2G if L32). + * No arc can exceed the (0..ASN_OID_ARC_MAX, which is the same as UINT32_MAX). * This function is not specific to OBJECT IDENTIFIER, it may be used to parse * the RELATIVE-OID data, or any other data consisting of dot-separated * series of numeric values. @@ -117,20 +114,38 @@ ssize_t OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add, * >= 0: Number of arcs contained in the OBJECT IDENTIFIER. * * WARNING: The function always returns the real number of arcs, - * even if there is no sufficient (_arc_slots) provided. - * This is useful for (_arc_slots) value estimation. + * even if there is no sufficient (arc_slots) provided. + * This is useful for (arc_slots) value estimation. */ -int OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length, - long arcs[], unsigned int arcs_slots, const char **opt_oid_text_end); +ssize_t OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, + ssize_t oid_txt_length, + asn_oid_arc_t *arcs, size_t arcs_count, + const char **opt_oid_text_end); /* * Internal functions. * Used by RELATIVE-OID implementation in particular. */ -int OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, - signed int add, void *value, unsigned int value_size); -int OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, - const void *arcval, unsigned int arcval_size, int _prepared_order); + +/* + * Retrieve a single arc of size from the (arcbuf) buffer. + * RETURN VALUES: + * -1: Failed to retrieve the value from the (arcbuf). + * >0: Number of bytes consumed from the (arcbuf), <= (arcbuf_len). + */ +ssize_t OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, + size_t arcbuf_len, + asn_oid_arc_t *ret_value); + +/* + * Write the unterminated arc value into the (arcbuf) which has the size at + * least (arcbuf_len). + * RETURN VALUES: + * -1: (arcbuf_len) size is not sufficient to write the value. + * : Number of bytes appended to the arcbuf (<= arcbuf_len). + */ +ssize_t OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len, + asn_oid_arc_t arc_value); #ifdef __cplusplus }