Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <RELATIVE-OID.h> |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 7 | #include <ber_codec_prim.h> /* Encoder and decoder of a primitive */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 8 | #include <limits.h> /* for CHAR_BIT */ |
Lev Walkin | f15320b | 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 | */ |
| 15 | static ber_tlv_tag_t asn1_DEF_RELATIVE_OID_tags[] = { |
| 16 | (ASN_TAG_CLASS_UNIVERSAL | (13 << 2)) |
| 17 | }; |
| 18 | asn1_TYPE_descriptor_t asn1_DEF_RELATIVE_OID = { |
| 19 | "RELATIVE-OID", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 20 | ASN__PRIMITIVE_TYPE_free, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | RELATIVE_OID_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | asn_generic_no_constraint, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 23 | ber_decode_primitive, |
| 24 | der_encode_primitive, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | 0, /* Not implemented yet */ |
| 26 | RELATIVE_OID_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | 0, /* Use generic outmost tag fetcher */ |
| 28 | asn1_DEF_RELATIVE_OID_tags, |
| 29 | sizeof(asn1_DEF_RELATIVE_OID_tags) |
| 30 | / sizeof(asn1_DEF_RELATIVE_OID_tags[0]), |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 31 | asn1_DEF_RELATIVE_OID_tags, /* Same as above */ |
| 32 | sizeof(asn1_DEF_RELATIVE_OID_tags) |
| 33 | / sizeof(asn1_DEF_RELATIVE_OID_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 34 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 35 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 38 | static ssize_t |
| 39 | RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
| 40 | ssize_t wrote = 0; |
| 41 | ssize_t ret; |
| 42 | int startn; |
| 43 | int i; |
| 44 | |
| 45 | for(i = 0, startn = 0; i < st->size; i++) { |
| 46 | uint8_t b = st->buf[i]; |
| 47 | if((b & 0x80)) /* Continuation expected */ |
| 48 | continue; |
| 49 | if(startn) { |
| 50 | /* Separate arcs */ |
| 51 | if(cb(".", 1, app_key) < 0) |
| 52 | return -1; |
| 53 | wrote++; |
| 54 | } |
| 55 | |
| 56 | ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn], |
| 57 | i - startn + 1, 0, cb, app_key); |
| 58 | if(ret < 0) return -1; |
| 59 | wrote += ret; |
| 60 | |
| 61 | startn = i + 1; |
| 62 | } |
| 63 | |
| 64 | return wrote; |
| 65 | } |
| 66 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | int |
| 68 | RELATIVE_OID_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 69 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 70 | const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 72 | (void)td; /* Unused argument */ |
| 73 | (void)ilevel; /* Unused argument */ |
| 74 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | if(!st || !st->buf) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 76 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 77 | |
| 78 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 79 | if(cb("{ ", 2, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 82 | if(RELATIVE_OID__dump_body(st, cb, app_key) < 0) |
| 83 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 85 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 88 | asn_enc_rval_t |
| 89 | RELATIVE_OID_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 90 | int ilevel, enum xer_encoder_flags_e flags, |
| 91 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 92 | RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr; |
| 93 | asn_enc_rval_t er; |
| 94 | |
| 95 | (void)ilevel; /* Unused argument */ |
| 96 | (void)flags; /* Unused argument */ |
| 97 | |
| 98 | if(!st || !st->buf) |
| 99 | _ASN_ENCODE_FAILED; |
| 100 | |
| 101 | er.encoded = RELATIVE_OID__dump_body(st, cb, app_key); |
| 102 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 103 | |
| 104 | return er; |
| 105 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | |
| 107 | int |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 108 | RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid, |
| 109 | void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 110 | void *arcs_end = (char *)arcs + (arc_slots * arc_type_size); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 111 | int num_arcs = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | int startn = 0; |
| 113 | int i; |
| 114 | |
| 115 | if(!roid || !roid->buf) { |
| 116 | errno = EINVAL; |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | for(i = 0; i < roid->size; i++) { |
| 121 | uint8_t b = roid->buf[i]; |
| 122 | if((b & 0x80)) /* Continuation expected */ |
| 123 | continue; |
| 124 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 125 | if(arcs < arcs_end) { |
| 126 | if(OBJECT_IDENTIFIER_get_single_arc( |
| 127 | &roid->buf[startn], |
| 128 | i - startn + 1, 0, |
| 129 | arcs, arc_type_size)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 130 | return -1; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 131 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 132 | num_arcs++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | startn = i + 1; |
| 136 | } |
| 137 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 138 | return num_arcs; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 142 | 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] | 143 | uint8_t *buf; |
| 144 | uint8_t *bp; |
Lev Walkin | 34b2a93 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 145 | unsigned int size; |
| 146 | unsigned int i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 148 | if(roid == NULL || arcs == NULL || arc_type_size < 1) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | errno = EINVAL; |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Roughly estimate the maximum size necessary to encode these arcs. |
| 155 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 156 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 157 | bp = buf = (uint8_t *)MALLOC(size + 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | if(!buf) { |
| 159 | /* ENOMEM */ |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | /* |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 164 | * Encode the arcs. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | */ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 166 | 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] | 167 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 168 | arcs, arc_type_size, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Lev Walkin | 34b2a93 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 171 | assert((unsigned)(bp - buf) <= size); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 172 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | /* |
| 174 | * Replace buffer. |
| 175 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 176 | roid->size = bp - buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | bp = roid->buf; |
| 178 | roid->buf = buf; |
| 179 | if(bp) FREEMEM(bp); |
| 180 | |
| 181 | return 0; |
| 182 | } |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 183 | |