1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #ifndef _OBJECT_IDENTIFIER_H_
24 #define _OBJECT_IDENTIFIER_H_
26 #include <asn_application.h>
27 #include <asn_codecs_prim.h>
28 #include <OCTET_STRING.h>
34 typedef uint32_t asn_oid_arc_t;
35 #define ASN_OID_ARC_MAX (~((asn_oid_arc_t)0))
37 typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t;
39 extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER;
40 extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER;
42 asn_struct_print_f OBJECT_IDENTIFIER_print;
43 asn_constr_check_f OBJECT_IDENTIFIER_constraint;
44 der_type_encoder_f OBJECT_IDENTIFIER_encode_der;
45 xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer;
46 xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer;
47 asn_random_fill_f OBJECT_IDENTIFIER_random_fill;
49 #define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free
50 #define OBJECT_IDENTIFIER_compare OCTET_STRING_compare
51 #define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive
52 #define OBJECT_IDENTIFIER_encode_der der_encode_primitive
53 #define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive
54 #define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive
55 #define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper
56 #define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper
57 #define OBJECT_IDENTIFIER_decode_aper OCTET_STRING_decode_aper
58 #define OBJECT_IDENTIFIER_encode_aper OCTET_STRING_encode_aper
60 /**********************************
61 * Some handy conversion routines *
62 **********************************/
65 * This function fills an (arcs) array with OBJECT IDENTIFIER arcs
66 * up to specified (arc_slots) elements.
69 * void print_arcs(OBJECT_IDENTIFIER_t *oid) {
70 * asn_oid_arc_t fixed_arcs[10]; // Try with fixed space first
71 * asn_oid_arc_t *arcs = fixed_arcs;
72 * size_t arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10
73 * ssize_t count; // Real number of arcs.
76 * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
77 * // If necessary, reallocate arcs array and try again.
78 * if(count > arc_slots) {
80 * arcs = malloc(sizeof(asn_oid_arc_t) * arc_slots);
82 * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
83 * assert(count == arc_slots);
86 * // Print the contents of the arcs array.
87 * for(i = 0; i < count; i++)
88 * printf("%"PRIu32"\n", arcs[i]);
90 * // Avoid memory leak.
91 * if(arcs != fixed_arcs) free(arcs);
95 * -1/EINVAL: Invalid arguments (oid is missing)
96 * -1/ERANGE: One or more arcs have value out of array cell type range.
97 * >=0: Number of arcs contained in the OBJECT IDENTIFIER
99 * WARNING: The function always returns the actual number of arcs,
100 * even if there is no sufficient (arc_slots) provided.
102 ssize_t OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid,
103 asn_oid_arc_t *arcs, size_t arc_slots);
106 * This functions initializes the OBJECT IDENTIFIER object with
107 * the given set of arcs.
108 * The minimum of two arcs must be present; some restrictions apply.
110 * -1/EINVAL: Invalid arguments
111 * -1/ERANGE: The first two arcs do not conform to ASN.1 restrictions.
112 * -1/ENOMEM: Memory allocation failed
113 * 0: The object was initialized with new arcs.
115 int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid,
116 const asn_oid_arc_t *arcs, size_t arcs_count);
120 * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363").
121 * No arc can exceed the (0..ASN_OID_ARC_MAX, which is the same as UINT32_MAX).
122 * This function is not specific to OBJECT IDENTIFIER, it may be used to parse
123 * the RELATIVE-OID data, or any other data consisting of dot-separated
124 * series of numeric values.
126 * If (oid_txt_length == -1), the strlen() will be invoked to determine the
127 * size of the (oid_text) string.
129 * After return, the optional (opt_oid_text_end) is set to the character after
130 * the last parsed one. (opt_oid_text_end) is never less than (oid_text).
134 * >= 0: Number of arcs contained in the OBJECT IDENTIFIER.
136 * WARNING: The function always returns the real number of arcs,
137 * even if there is no sufficient (arc_slots) provided.
138 * This is useful for (arc_slots) value estimation.
140 ssize_t OBJECT_IDENTIFIER_parse_arcs(const char *oid_text,
141 ssize_t oid_txt_length,
142 asn_oid_arc_t *arcs, size_t arcs_count,
143 const char **opt_oid_text_end);
146 * Internal functions.
147 * Used by RELATIVE-OID implementation in particular.
151 * Retrieve a single arc of size from the (arcbuf) buffer.
153 * -1: Failed to retrieve the value from the (arcbuf).
154 * >0: Number of bytes consumed from the (arcbuf), <= (arcbuf_len).
156 ssize_t OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf,
158 asn_oid_arc_t *ret_value);
161 * Write the unterminated arc value into the (arcbuf) which has the size at
162 * least (arcbuf_len).
164 * -1: (arcbuf_len) size is not sufficient to write the value.
165 * <n>: Number of bytes appended to the arcbuf (<= arcbuf_len).
167 ssize_t OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len,
168 asn_oid_arc_t arc_value);
174 #endif /* _OBJECT_IDENTIFIER_H_ */