Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 2c34aad | 2005-03-10 11:50:24 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
| 6 | #ifndef _OBJECT_IDENTIFIER_H_ |
| 7 | #define _OBJECT_IDENTIFIER_H_ |
| 8 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 9 | #include <asn_application.h> |
Lev Walkin | 3256d6f | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 10 | #include <asn_codecs_prim.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 16 | typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | asn_struct_print_f OBJECT_IDENTIFIER_print; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | asn_constr_check_f OBJECT_IDENTIFIER_constraint; |
| 22 | der_type_encoder_f OBJECT_IDENTIFIER_encode_der; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 23 | xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 26 | #define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free |
| 27 | #define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive |
| 28 | #define OBJECT_IDENTIFIER_encode_der der_encode_primitive |
| 29 | #define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper |
| 30 | #define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper |
| 31 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | /********************************** |
| 33 | * Some handy conversion routines * |
| 34 | **********************************/ |
| 35 | |
| 36 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | * This function fills an (_arcs) array with OBJECT IDENTIFIER arcs |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 38 | * up to specified (_arc_slots) elements. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | * |
| 40 | * EXAMPLE: |
| 41 | * void print_arcs(OBJECT_IDENTIFIER_t *oid) { |
| 42 | * unsigned long fixed_arcs[10]; // Try with fixed space first |
| 43 | * unsigned long *arcs = fixed_arcs; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 44 | * int arc_type_size = sizeof(fixed_arcs[0]); // sizeof(long) |
| 45 | * int arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | * int count; // Real number of arcs. |
| 47 | * int i; |
| 48 | * |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 49 | * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, |
| 50 | * arc_type_size, arc_slots); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | * // If necessary, reallocate arcs array and try again. |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 52 | * if(count > arc_slots) { |
| 53 | * arc_slots = count; |
| 54 | * arcs = malloc(arc_type_size * arc_slots); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | * if(!arcs) return; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 56 | * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, |
| 57 | * arc_type_size, arc_slots); |
| 58 | * assert(count == arc_slots); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | * } |
| 60 | * |
| 61 | * // Print the contents of the arcs array. |
| 62 | * for(i = 0; i < count; i++) |
| 63 | * printf("%d\n", arcs[i]); |
| 64 | * |
| 65 | * // Avoid memory leak. |
| 66 | * if(arcs != fixed_arcs) free(arcs); |
| 67 | * } |
| 68 | * |
| 69 | * RETURN VALUES: |
| 70 | * -1/EINVAL: Invalid arguments (oid is missing) |
| 71 | * -1/ERANGE: One or more arcs have value out of array cell type range. |
| 72 | * >=0: Number of arcs contained in the OBJECT IDENTIFIER |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 73 | * |
| 74 | * WARNING: The function always returns the real number of arcs, |
| 75 | * even if there is no sufficient (_arc_slots) provided. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | */ |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 77 | int OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *_oid, |
Lev Walkin | 4a4d06e | 2006-08-10 22:09:41 +0000 | [diff] [blame] | 78 | void *_arcs, /* e.g., unsigned int arcs[N] */ |
| 79 | unsigned int _arc_type_size, /* e.g., sizeof(arcs[0]) */ |
| 80 | unsigned int _arc_slots /* e.g., N */); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * This functions initializes the OBJECT IDENTIFIER object with |
| 84 | * the given set of arcs. |
| 85 | * The minimum of two arcs must be present; some restrictions apply. |
| 86 | * RETURN VALUES: |
| 87 | * -1/EINVAL: Invalid arguments |
| 88 | * -1/ERANGE: The first two arcs do not conform to ASN.1 restrictions. |
| 89 | * -1/ENOMEM: Memory allocation failed |
| 90 | * 0: The object was initialized with new arcs. |
| 91 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 92 | int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *_oid, |
Lev Walkin | 4a4d06e | 2006-08-10 22:09:41 +0000 | [diff] [blame] | 93 | const void *_arcs, /* e.g., unsigned int arcs[N] */ |
| 94 | unsigned int _arc_type_size, /* e.g., sizeof(arcs[0]) */ |
| 95 | unsigned int _arc_slots /* e.g., N */); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 98 | * Print the specified OBJECT IDENTIFIER arc. |
| 99 | */ |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 100 | int OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 101 | int add, /* Arbitrary offset, required to process the first two arcs */ |
| 102 | asn_app_consume_bytes_f *cb, void *app_key); |
| 103 | |
| 104 | /* Same as above, but returns the number of written digits, instead of 0 */ |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 105 | ssize_t OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add, |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 106 | asn_app_consume_bytes_f *cb, void *app_key); |
| 107 | |
| 108 | /* |
| 109 | * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363"). |
| 110 | * No arc can exceed the (0..signed_long_max) range (typically, 0..2G if L32). |
| 111 | * This function is not specific to OBJECT IDENTIFIER, it may be used to parse |
| 112 | * the RELATIVE-OID data, or any other data consisting of dot-separated |
| 113 | * series of numeric values. |
| 114 | * |
| 115 | * If (oid_txt_length == -1), the strlen() will be invoked to determine the |
| 116 | * size of the (oid_text) string. |
| 117 | * |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 118 | * After return, the optional (opt_oid_text_end) is set to the character after |
| 119 | * the last parsed one. (opt_oid_text_end) is never less than (oid_text). |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 120 | * |
| 121 | * RETURN VALUES: |
| 122 | * -1: Parse error. |
| 123 | * >= 0: Number of arcs contained in the OBJECT IDENTIFIER. |
| 124 | * |
| 125 | * WARNING: The function always returns the real number of arcs, |
| 126 | * even if there is no sufficient (_arc_slots) provided. |
Lev Walkin | b9c9102 | 2005-03-10 11:32:57 +0000 | [diff] [blame] | 127 | * This is useful for (_arc_slots) value estimation. |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 128 | */ |
| 129 | int OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length, |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 130 | long arcs[], unsigned int arcs_slots, const char **opt_oid_text_end); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | * Internal functions. |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 134 | * Used by RELATIVE-OID implementation in particular. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | */ |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 136 | int OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 137 | signed int add, void *value, unsigned int value_size); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 138 | int OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 139 | const void *arcval, unsigned int arcval_size, int _prepared_order); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 141 | #ifdef __cplusplus |
| 142 | } |
| 143 | #endif |
| 144 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | #endif /* _OBJECT_IDENTIFIER_H_ */ |