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