vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <RELATIVE-OID.h> |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 7 | #include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 8 | #include <limits.h> /* for CHAR_BIT */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <errno.h> |
| 11 | |
| 12 | /* |
| 13 | * RELATIVE-OID basic type description. |
| 14 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 15 | static ber_tlv_tag_t asn_DEF_RELATIVE_OID_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | (ASN_TAG_CLASS_UNIVERSAL | (13 << 2)) |
| 17 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | "RELATIVE-OID", |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 20 | "RELATIVE_OID", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 21 | ASN__PRIMITIVE_TYPE_free, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 22 | RELATIVE_OID_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | asn_generic_no_constraint, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 24 | ber_decode_primitive, |
| 25 | der_encode_primitive, |
vlm | 1f73df2 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 26 | RELATIVE_OID_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 27 | RELATIVE_OID_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 29 | asn_DEF_RELATIVE_OID_tags, |
| 30 | sizeof(asn_DEF_RELATIVE_OID_tags) |
| 31 | / sizeof(asn_DEF_RELATIVE_OID_tags[0]), |
| 32 | asn_DEF_RELATIVE_OID_tags, /* Same as above */ |
| 33 | sizeof(asn_DEF_RELATIVE_OID_tags) |
| 34 | / sizeof(asn_DEF_RELATIVE_OID_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 35 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 36 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 39 | static ssize_t |
| 40 | RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
| 41 | ssize_t wrote = 0; |
| 42 | ssize_t ret; |
| 43 | int startn; |
| 44 | int i; |
| 45 | |
| 46 | for(i = 0, startn = 0; i < st->size; i++) { |
| 47 | uint8_t b = st->buf[i]; |
| 48 | if((b & 0x80)) /* Continuation expected */ |
| 49 | continue; |
| 50 | if(startn) { |
| 51 | /* Separate arcs */ |
| 52 | if(cb(".", 1, app_key) < 0) |
| 53 | return -1; |
| 54 | wrote++; |
| 55 | } |
| 56 | |
| 57 | ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn], |
| 58 | i - startn + 1, 0, cb, app_key); |
| 59 | if(ret < 0) return -1; |
| 60 | wrote += ret; |
| 61 | |
| 62 | startn = i + 1; |
| 63 | } |
| 64 | |
| 65 | return wrote; |
| 66 | } |
| 67 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 69 | RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 71 | const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 73 | (void)td; /* Unused argument */ |
| 74 | (void)ilevel; /* Unused argument */ |
| 75 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | if(!st || !st->buf) |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 77 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | |
| 79 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 80 | if(cb("{ ", 2, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | return -1; |
| 82 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 83 | if(RELATIVE_OID__dump_body(st, cb, app_key) < 0) |
| 84 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 86 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | } |
| 88 | |
vlm | 1f73df2 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 89 | static ssize_t |
| 90 | RELATIVE_OID__xer_body_decode(void *sptr, void *chunk_buf, size_t chunk_size) { |
| 91 | RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr; |
| 92 | char *endptr; |
| 93 | long s_arcs[6]; |
| 94 | long *arcs = s_arcs; |
| 95 | int arcs_count; |
| 96 | int ret; |
| 97 | |
| 98 | arcs_count = OBJECT_IDENTIFIER_parse_arcs( |
| 99 | (const char *)chunk_buf, chunk_size, |
| 100 | arcs, 6, &endptr); |
| 101 | if(arcs_count < 0) |
| 102 | return -1; /* Expecting at least zero arcs */ |
| 103 | if(arcs_count > 6) { |
| 104 | arcs = (long *)MALLOC(arcs_count * sizeof(long)); |
| 105 | if(!arcs) return -1; |
| 106 | ret = OBJECT_IDENTIFIER_parse_arcs( |
| 107 | (const char *)chunk_buf, chunk_size, |
| 108 | arcs, arcs_count, &endptr); |
| 109 | if(ret != arcs_count) |
| 110 | return -1; /* assert?.. */ |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Convert arcs into BER representation. |
| 115 | */ |
| 116 | ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count); |
| 117 | if(ret) return -1; |
| 118 | if(arcs != s_arcs) FREEMEM(arcs); |
| 119 | |
| 120 | return endptr - (char *)chunk_buf; |
| 121 | } |
| 122 | |
| 123 | asn_dec_rval_t |
| 124 | RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 125 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 126 | void *buf_ptr, size_t size) { |
| 127 | |
| 128 | return xer_decode_primitive(opt_codec_ctx, td, |
| 129 | sptr, sizeof(RELATIVE_OID_t), opt_mname, |
| 130 | buf_ptr, size, RELATIVE_OID__xer_body_decode); |
| 131 | } |
| 132 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 133 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 134 | RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 135 | int ilevel, enum xer_encoder_flags_e flags, |
| 136 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 137 | RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr; |
| 138 | asn_enc_rval_t er; |
| 139 | |
| 140 | (void)ilevel; /* Unused argument */ |
| 141 | (void)flags; /* Unused argument */ |
| 142 | |
| 143 | if(!st || !st->buf) |
| 144 | _ASN_ENCODE_FAILED; |
| 145 | |
| 146 | er.encoded = RELATIVE_OID__dump_body(st, cb, app_key); |
| 147 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 148 | |
| 149 | return er; |
| 150 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | |
| 152 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 153 | RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid, |
| 154 | void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 155 | void *arcs_end = (char *)arcs + (arc_slots * arc_type_size); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 156 | int num_arcs = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 157 | int startn = 0; |
| 158 | int i; |
| 159 | |
| 160 | if(!roid || !roid->buf) { |
| 161 | errno = EINVAL; |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | for(i = 0; i < roid->size; i++) { |
| 166 | uint8_t b = roid->buf[i]; |
| 167 | if((b & 0x80)) /* Continuation expected */ |
| 168 | continue; |
| 169 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 170 | if(arcs < arcs_end) { |
| 171 | if(OBJECT_IDENTIFIER_get_single_arc( |
| 172 | &roid->buf[startn], |
| 173 | i - startn + 1, 0, |
| 174 | arcs, arc_type_size)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | return -1; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 176 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 177 | num_arcs++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | startn = i + 1; |
| 181 | } |
| 182 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 183 | return num_arcs; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 187 | RELATIVE_OID_set_arcs(RELATIVE_OID_t *roid, void *arcs, unsigned int arc_type_size, unsigned int arcs_slots) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | uint8_t *buf; |
| 189 | uint8_t *bp; |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 190 | unsigned int size; |
| 191 | unsigned int i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 193 | if(roid == NULL || arcs == NULL || arc_type_size < 1) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | errno = EINVAL; |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Roughly estimate the maximum size necessary to encode these arcs. |
| 200 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 201 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 202 | bp = buf = (uint8_t *)MALLOC(size + 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 203 | if(!buf) { |
| 204 | /* ENOMEM */ |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | /* |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 209 | * Encode the arcs. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | */ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 211 | for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 212 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 213 | arcs, arc_type_size, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 214 | } |
| 215 | |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 216 | assert((unsigned)(bp - buf) <= size); |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 217 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | /* |
| 219 | * Replace buffer. |
| 220 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 221 | roid->size = bp - buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 222 | bp = roid->buf; |
| 223 | roid->buf = buf; |
| 224 | if(bp) FREEMEM(bp); |
| 225 | |
| 226 | return 0; |
| 227 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 228 | |