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