blob: aa0d5c7084ec092f1ce420e4417e637c222df7d0 [file] [log] [blame]
Lev Walkin20696a42017-10-17 21:27:33 -07001/*
2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#ifndef _OBJECT_IDENTIFIER_H_
6#define _OBJECT_IDENTIFIER_H_
7
Lev Walkin8e8078a2004-09-26 13:10:40 +00008#include <asn_application.h>
Lev Walkin3256d6f2004-10-21 11:22:12 +00009#include <asn_codecs_prim.h>
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080010#include <OCTET_STRING.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkin21b41ac2005-07-24 09:03:44 +000012#ifdef __cplusplus
13extern "C" {
14#endif
15
Lev Walkin588bf0f2017-10-13 23:51:16 -070016typedef uint32_t asn_oid_arc_t;
17#define ASN_OID_ARC_MAX (~((asn_oid_arc_t)0))
18
Lev Walkin8e8078a2004-09-26 13:10:40 +000019typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t;
Lev Walkinf15320b2004-06-03 03:38:44 +000020
Lev Walkin5e033762004-09-29 13:26:15 +000021extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080022extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER;
Lev Walkinf15320b2004-06-03 03:38:44 +000023
Lev Walkinf15320b2004-06-03 03:38:44 +000024asn_struct_print_f OBJECT_IDENTIFIER_print;
Lev Walkina9cc46e2004-09-22 16:06:28 +000025asn_constr_check_f OBJECT_IDENTIFIER_constraint;
26der_type_encoder_f OBJECT_IDENTIFIER_encode_der;
Lev Walkin92302252004-10-23 10:16:51 +000027xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer;
Lev Walkina9cc46e2004-09-22 16:06:28 +000028xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer;
Lev Walkina5972be2017-09-29 23:15:58 -070029asn_random_fill_f OBJECT_IDENTIFIER_random_fill;
Lev Walkinf15320b2004-06-03 03:38:44 +000030
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080031#define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080032#define OBJECT_IDENTIFIER_compare OCTET_STRING_compare
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080033#define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive
34#define OBJECT_IDENTIFIER_encode_der der_encode_primitive
Lev Walkinb5b524b2017-10-13 03:14:03 -070035#define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive
36#define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080037#define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper
38#define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper
39
Lev Walkinf15320b2004-06-03 03:38:44 +000040/**********************************
41 * Some handy conversion routines *
42 **********************************/
43
44/*
Lev Walkin588bf0f2017-10-13 23:51:16 -070045 * This function fills an (arcs) array with OBJECT IDENTIFIER arcs
46 * up to specified (arc_slots) elements.
47 *
Lev Walkinf15320b2004-06-03 03:38:44 +000048 * EXAMPLE:
49 * void print_arcs(OBJECT_IDENTIFIER_t *oid) {
Lev Walkin588bf0f2017-10-13 23:51:16 -070050 * 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.
Lev Walkinf15320b2004-06-03 03:38:44 +000054 * int i;
Lev Walkin588bf0f2017-10-13 23:51:16 -070055 *
56 * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
Lev Walkinf15320b2004-06-03 03:38:44 +000057 * // If necessary, reallocate arcs array and try again.
Lev Walkin29a044b2004-06-14 07:24:36 +000058 * if(count > arc_slots) {
59 * arc_slots = count;
Lev Walkin588bf0f2017-10-13 23:51:16 -070060 * arcs = malloc(sizeof(asn_oid_arc_t) * arc_slots);
Lev Walkinf15320b2004-06-03 03:38:44 +000061 * if(!arcs) return;
Lev Walkin588bf0f2017-10-13 23:51:16 -070062 * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +000063 * assert(count == arc_slots);
Lev Walkinf15320b2004-06-03 03:38:44 +000064 * }
Lev Walkin588bf0f2017-10-13 23:51:16 -070065 *
Lev Walkinf15320b2004-06-03 03:38:44 +000066 * // Print the contents of the arcs array.
67 * for(i = 0; i < count; i++)
Lev Walkin588bf0f2017-10-13 23:51:16 -070068 * printf("%"PRIu32"\n", arcs[i]);
69 *
Lev Walkinf15320b2004-06-03 03:38:44 +000070 * // Avoid memory leak.
71 * if(arcs != fixed_arcs) free(arcs);
72 * }
Lev Walkin588bf0f2017-10-13 23:51:16 -070073 *
Lev Walkinf15320b2004-06-03 03:38:44 +000074 * RETURN VALUES:
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
Lev Walkin588bf0f2017-10-13 23:51:16 -070078 *
79 * WARNING: The function always returns the actual number of arcs,
80 * even if there is no sufficient (arc_slots) provided.
Lev Walkinf15320b2004-06-03 03:38:44 +000081 */
Lev Walkin588bf0f2017-10-13 23:51:16 -070082ssize_t OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid,
83 asn_oid_arc_t *arcs, size_t arc_slots);
Lev Walkinf15320b2004-06-03 03:38:44 +000084
85/*
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.
89 * RETURN VALUES:
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.
94 */
Lev Walkin588bf0f2017-10-13 23:51:16 -070095int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid,
96 const asn_oid_arc_t *arcs, size_t arcs_count);
Lev Walkinf15320b2004-06-03 03:38:44 +000097
Lev Walkin92302252004-10-23 10:16:51 +000098
99/*
100 * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363").
Lev Walkin588bf0f2017-10-13 23:51:16 -0700101 * No arc can exceed the (0..ASN_OID_ARC_MAX, which is the same as UINT32_MAX).
Lev Walkin92302252004-10-23 10:16:51 +0000102 * 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.
105 *
106 * If (oid_txt_length == -1), the strlen() will be invoked to determine the
107 * size of the (oid_text) string.
108 *
Lev Walkin093ba2e2005-04-04 21:10:06 +0000109 * 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).
Lev Walkin92302252004-10-23 10:16:51 +0000111 *
112 * RETURN VALUES:
113 * -1: Parse error.
114 * >= 0: Number of arcs contained in the OBJECT IDENTIFIER.
115 *
116 * WARNING: The function always returns the real number of arcs,
Lev Walkin588bf0f2017-10-13 23:51:16 -0700117 * even if there is no sufficient (arc_slots) provided.
118 * This is useful for (arc_slots) value estimation.
Lev Walkin92302252004-10-23 10:16:51 +0000119 */
Lev Walkin588bf0f2017-10-13 23:51:16 -0700120ssize_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);
Lev Walkin92302252004-10-23 10:16:51 +0000124
125/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 * Internal functions.
Lev Walkin0787ff02004-06-17 23:43:39 +0000127 * Used by RELATIVE-OID implementation in particular.
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 */
Lev Walkin588bf0f2017-10-13 23:51:16 -0700129
130/*
131 * Retrieve a single arc of size from the (arcbuf) buffer.
132 * RETURN VALUES:
133 * -1: Failed to retrieve the value from the (arcbuf).
134 * >0: Number of bytes consumed from the (arcbuf), <= (arcbuf_len).
135 */
136ssize_t OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf,
137 size_t arcbuf_len,
138 asn_oid_arc_t *ret_value);
139
140/*
141 * Write the unterminated arc value into the (arcbuf) which has the size at
142 * least (arcbuf_len).
143 * RETURN VALUES:
144 * -1: (arcbuf_len) size is not sufficient to write the value.
145 * <n>: Number of bytes appended to the arcbuf (<= arcbuf_len).
146 */
147ssize_t OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len,
148 asn_oid_arc_t arc_value);
Lev Walkinf15320b2004-06-03 03:38:44 +0000149
Lev Walkin21b41ac2005-07-24 09:03:44 +0000150#ifdef __cplusplus
151}
152#endif
153
Lev Walkinf15320b2004-06-03 03:38:44 +0000154#endif /* _OBJECT_IDENTIFIER_H_ */