2  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
 
   3  * Redistribution and modifications are permitted subject to BSD license.
 
   5 #ifndef _OBJECT_IDENTIFIER_H_
 
   6 #define _OBJECT_IDENTIFIER_H_
 
   8 #include <asn_application.h>
 
   9 #include <asn_codecs_prim.h>
 
  10 #include <OCTET_STRING.h>
 
  16 typedef uint32_t asn_oid_arc_t;
 
  17 #define ASN_OID_ARC_MAX (~((asn_oid_arc_t)0))
 
  19 typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t;
 
  21 extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER;
 
  22 extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER;
 
  24 asn_struct_print_f OBJECT_IDENTIFIER_print;
 
  25 asn_constr_check_f OBJECT_IDENTIFIER_constraint;
 
  26 der_type_encoder_f OBJECT_IDENTIFIER_encode_der;
 
  27 xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer;
 
  28 xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer;
 
  29 asn_random_fill_f  OBJECT_IDENTIFIER_random_fill;
 
  31 #define OBJECT_IDENTIFIER_free           ASN__PRIMITIVE_TYPE_free
 
  32 #define OBJECT_IDENTIFIER_compare        OCTET_STRING_compare
 
  33 #define OBJECT_IDENTIFIER_decode_ber     ber_decode_primitive
 
  34 #define OBJECT_IDENTIFIER_encode_der     der_encode_primitive
 
  35 #define OBJECT_IDENTIFIER_decode_oer     oer_decode_primitive
 
  36 #define OBJECT_IDENTIFIER_encode_oer     oer_encode_primitive
 
  37 #define OBJECT_IDENTIFIER_decode_uper    OCTET_STRING_decode_uper
 
  38 #define OBJECT_IDENTIFIER_encode_uper    OCTET_STRING_encode_uper
 
  40 /**********************************
 
  41  * Some handy conversion routines *
 
  42  **********************************/
 
  45  * This function fills an (arcs) array with OBJECT IDENTIFIER arcs
 
  46  * up to specified (arc_slots) elements.
 
  49  *      void print_arcs(OBJECT_IDENTIFIER_t *oid) {
 
  50  *              asn_oid_arc_t fixed_arcs[10];   // Try with fixed space first
 
  51  *              asn_oid_arc_t *arcs = fixed_arcs;
 
  52  *              size_t arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10
 
  53  *              ssize_t count;  // Real number of arcs.
 
  56  *              count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
 
  57  *              // If necessary, reallocate arcs array and try again.
 
  58  *              if(count > arc_slots) {
 
  60  *                      arcs = malloc(sizeof(asn_oid_arc_t) * arc_slots);
 
  62  *                      count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
 
  63  *                      assert(count == arc_slots);
 
  66  *              // Print the contents of the arcs array.
 
  67  *              for(i = 0; i < count; i++)
 
  68  *                      printf("%"PRIu32"\n", arcs[i]);
 
  70  *              // Avoid memory leak.
 
  71  *              if(arcs != fixed_arcs) free(arcs);
 
  75  * -1/EINVAL:   Invalid arguments (oid is missing)
 
  76  * -1/ERANGE:   One or more arcs have value out of array cell type range.
 
  77  * >=0:         Number of arcs contained in the OBJECT IDENTIFIER
 
  79  * WARNING: The function always returns the actual number of arcs,
 
  80  * even if there is no sufficient (arc_slots) provided.
 
  82 ssize_t OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid,
 
  83                                    asn_oid_arc_t *arcs, size_t arc_slots);
 
  86  * This functions initializes the OBJECT IDENTIFIER object with
 
  87  * the given set of arcs.
 
  88  * The minimum of two arcs must be present; some restrictions apply.
 
  90  * -1/EINVAL:   Invalid arguments
 
  91  * -1/ERANGE:   The first two arcs do not conform to ASN.1 restrictions.
 
  92  * -1/ENOMEM:   Memory allocation failed
 
  93  * 0:           The object was initialized with new arcs.
 
  95 int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid,
 
  96                                const asn_oid_arc_t *arcs, size_t arcs_count);
 
 100  * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363").
 
 101  * No arc can exceed the (0..ASN_OID_ARC_MAX, which is the same as UINT32_MAX).
 
 102  * This function is not specific to OBJECT IDENTIFIER, it may be used to parse
 
 103  * the RELATIVE-OID data, or any other data consisting of dot-separated
 
 104  * series of numeric values.
 
 106  * If (oid_txt_length == -1), the strlen() will be invoked to determine the
 
 107  * size of the (oid_text) string.
 
 109  * After return, the optional (opt_oid_text_end) is set to the character after
 
 110  * the last parsed one. (opt_oid_text_end) is never less than (oid_text).
 
 114  * >= 0:        Number of arcs contained in the OBJECT IDENTIFIER.
 
 116  * WARNING: The function always returns the real number of arcs,
 
 117  * even if there is no sufficient (arc_slots) provided.
 
 118  * This is useful for (arc_slots) value estimation.
 
 120 ssize_t OBJECT_IDENTIFIER_parse_arcs(const char *oid_text,
 
 121                                      ssize_t oid_txt_length,
 
 122                                      asn_oid_arc_t *arcs, size_t arcs_count,
 
 123                                      const char **opt_oid_text_end);
 
 126  * Internal functions.
 
 127  * Used by RELATIVE-OID implementation in particular.
 
 131  * Retrieve a single arc of size from the (arcbuf) buffer.
 
 133  *  -1: Failed to retrieve the value from the (arcbuf).
 
 134  *  >0: Number of bytes consumed from the (arcbuf), <= (arcbuf_len).
 
 136 ssize_t OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf,
 
 138                                          asn_oid_arc_t *ret_value);
 
 141  * Write the unterminated arc value into the (arcbuf) which has the size at
 
 142  * least (arcbuf_len).
 
 144  *   -1: (arcbuf_len) size is not sufficient to write the value.
 
 145  *  <n>: Number of bytes appended to the arcbuf (<= arcbuf_len).
 
 147 ssize_t OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len,
 
 148                                          asn_oid_arc_t arc_value);
 
 154 #endif  /* _OBJECT_IDENTIFIER_H_ */