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 | */ |
| 5 | #include <OBJECT_IDENTIFIER.h> |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 6 | #include <limits.h> /* for CHAR_BIT */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | /* |
| 11 | * OBJECT IDENTIFIER basic type description. |
| 12 | */ |
| 13 | static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = { |
| 14 | (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 15 | }; |
| 16 | asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = { |
| 17 | "OBJECT IDENTIFIER", |
| 18 | OBJECT_IDENTIFIER_constraint, |
| 19 | INTEGER_decode_ber, /* Implemented in terms of INTEGER type */ |
| 20 | OBJECT_IDENTIFIER_encode_der, |
| 21 | OBJECT_IDENTIFIER_print, |
| 22 | INTEGER_free, |
| 23 | 0, /* Use generic outmost tag fetcher */ |
| 24 | asn1_DEF_OBJECT_IDENTIFIER_tags, |
| 25 | sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags) |
| 26 | / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 27 | 1, /* Single UNIVERSAL tag may be implicitly overriden */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 28 | 0, /* Always in primitive form */ |
| 29 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * Encode OBJECT IDENTIFIER type using DER. |
| 35 | */ |
| 36 | der_enc_rval_t |
| 37 | OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, |
| 38 | int tag_mode, ber_tlv_tag_t tag, |
| 39 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 40 | der_enc_rval_t erval; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 41 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
| 43 | ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)", |
| 44 | cb?"Encoding":"Estimating", sd->name, tag_mode); |
| 45 | |
| 46 | erval.encoded = der_write_tags(sd, st->size, tag_mode, tag, |
| 47 | cb, app_key); |
| 48 | ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d", |
| 49 | sd->name, (int)erval.encoded); |
| 50 | if(erval.encoded == -1) { |
| 51 | erval.failed_type = sd; |
| 52 | erval.structure_ptr = ptr; |
| 53 | return erval; |
| 54 | } |
| 55 | |
| 56 | if(cb && st->buf) { |
| 57 | ssize_t ret; |
| 58 | |
| 59 | ret = cb(st->buf, st->size, app_key); |
| 60 | if(ret == -1) { |
| 61 | erval.encoded = -1; |
| 62 | erval.failed_type = sd; |
| 63 | erval.structure_ptr = ptr; |
| 64 | return erval; |
| 65 | } |
| 66 | } else { |
| 67 | assert(st->buf || st->size == 0); |
| 68 | } |
| 69 | |
| 70 | erval.encoded += st->size; |
| 71 | |
| 72 | return erval; |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 77 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 78 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | |
| 80 | if(st && st->buf) { |
| 81 | if(st->size < 1) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 82 | _ASN_ERRLOG(app_errlog, app_key, |
| 83 | "%s: at least one numerical value expected", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | td->name); |
| 85 | return -1; |
| 86 | } |
| 87 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 88 | _ASN_ERRLOG(app_errlog, app_key, |
| 89 | "%s: value not given", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 96 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 98 | OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) { |
| 99 | unsigned LE = 1; /* Little endian (x86) */ |
| 100 | uint8_t *arcend = arcbuf + arclen; /* End of arc */ |
| 101 | void *rvstart = rvbuf; /* Original start of the value buffer */ |
| 102 | unsigned int cache = 0; /* No more than 14 significant bits */ |
| 103 | int inc; /* Return value growth direction */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 105 | rvsize *= CHAR_BIT; /* bytes to bits */ |
| 106 | arclen *= 7; /* bytes to bits */ |
| 107 | |
| 108 | /* |
| 109 | * The arc has the number of bits |
| 110 | * cannot be represented using supplied return value type. |
| 111 | */ |
| 112 | if(arclen > rvsize) { |
| 113 | if(arclen > (rvsize + CHAR_BIT)) { |
| 114 | errno = ERANGE; /* Overflow */ |
| 115 | return -1; |
| 116 | } else { |
| 117 | /* |
| 118 | * Even if the number of bits in the arc representation |
| 119 | * is higher than the width of supplied * return value |
| 120 | * type, there is still possible to fit it when there |
| 121 | * are few unused high bits in the arc value |
| 122 | * representaion. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 123 | * |
| 124 | * Moreover, there is a possibility that the |
| 125 | * number could actually fit the arc space, given |
| 126 | * that add is negative, but we don't handle |
| 127 | * such "temporary lack of precision" situation here. |
| 128 | * May be considered as a bug. |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 129 | */ |
| 130 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 131 | if((*arcbuf & mask)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | errno = ERANGE; /* Overflow */ |
| 133 | return -1; |
| 134 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 135 | /* Fool the routine computing unused bits */ |
| 136 | arclen -= 7; |
| 137 | cache = *arcbuf & 0x7f; |
| 138 | arcbuf++; |
| 139 | } |
| 140 | } |
| 141 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 142 | /* Faster path for common size */ |
| 143 | if(rvsize == (CHAR_BIT * sizeof(unsigned long))) { |
| 144 | unsigned long accum; |
| 145 | /* Gather all bits into the accumulator */ |
| 146 | for(accum = cache; arcbuf < arcend; arcbuf++) |
| 147 | accum = (accum << 7) | (*arcbuf & ~0x80); |
| 148 | if(accum < (unsigned)-add) { |
| 149 | errno = ERANGE; /* Overflow */ |
| 150 | return -1; |
| 151 | } |
| 152 | *(unsigned long *)rvbuf = accum + add; |
| 153 | return 0; |
| 154 | } |
| 155 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 156 | #ifndef WORDS_BIGENDIAN |
| 157 | if(*(unsigned char *)&LE) { /* Little endian (x86) */ |
| 158 | /* "Convert" to big endian */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 159 | (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 160 | ((unsigned char *)rvstart)--; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 161 | inc = -1; /* Descending */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 162 | } else |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 163 | #endif /* !WORDS_BIGENDIAN */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 164 | inc = +1; /* Big endian is known [at compile time] */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 165 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 166 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 167 | int bits; /* typically no more than 3-4 bits */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 168 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 169 | /* Clear the high unused bits */ |
| 170 | for(bits = rvsize - arclen; |
| 171 | bits > CHAR_BIT; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 172 | (unsigned char *)rvbuf += inc, bits -= CHAR_BIT) |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 173 | *(unsigned char *)rvbuf = 0; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 174 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 175 | /* Fill the body of a value */ |
| 176 | for(; arcbuf < arcend; arcbuf++) { |
| 177 | cache = (cache << 7) | (*arcbuf & 0x7f); |
| 178 | bits += 7; |
| 179 | if(bits >= CHAR_BIT) { |
| 180 | bits -= CHAR_BIT; |
| 181 | *(unsigned char *)rvbuf = (cache >> bits); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 182 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | if(bits) { |
| 186 | *(unsigned char *)rvbuf = cache; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 187 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | if(add) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 192 | for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) { |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 193 | int v = add + *(unsigned char *)rvbuf; |
| 194 | if(v & (-1 << CHAR_BIT)) { |
| 195 | *(unsigned char *)rvbuf |
vlm | 754284a | 2004-08-11 09:17:15 +0000 | [diff] [blame] | 196 | = (unsigned char)(v + (1 << CHAR_BIT)); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 197 | add = -1; |
| 198 | } else { |
| 199 | *(unsigned char *)rvbuf = v; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | if(rvbuf == rvstart) { |
| 204 | /* No space to carry over */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | errno = ERANGE; /* Overflow */ |
| 206 | return -1; |
| 207 | } |
| 208 | } |
| 209 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 213 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 214 | int |
| 215 | OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add, |
| 216 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 217 | char scratch[64]; /* Conservative estimate */ |
| 218 | unsigned long accum; /* Bits accumulator */ |
| 219 | char *p; /* Position in the scratch buffer */ |
| 220 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 221 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 222 | &accum, sizeof(accum))) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | return -1; |
| 224 | |
| 225 | /* Fill the scratch buffer in reverse. */ |
| 226 | p = scratch + sizeof(scratch); |
| 227 | for(; accum; accum /= 10) |
vlm | 754284a | 2004-08-11 09:17:15 +0000 | [diff] [blame] | 228 | *(--p) = (char)(accum % 10) + 0x30; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 229 | |
| 230 | return cb(p, sizeof(scratch) - (p - scratch), app_key); |
| 231 | } |
| 232 | |
| 233 | int |
| 234 | OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 235 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 236 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | int startn; |
| 238 | int add = 0; |
| 239 | int i; |
| 240 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 241 | (void)td; /* Unused argument */ |
| 242 | (void)ilevel; /* Unused argument */ |
| 243 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | if(!st || !st->buf) |
| 245 | return cb("<absent>", 8, app_key); |
| 246 | |
| 247 | /* Dump preamble */ |
| 248 | if(cb("{ ", 2, app_key)) |
| 249 | return -1; |
| 250 | |
| 251 | for(i = 0, startn = 0; i < st->size; i++) { |
| 252 | uint8_t b = st->buf[i]; |
| 253 | if((b & 0x80)) /* Continuation expected */ |
| 254 | continue; |
| 255 | |
| 256 | if(startn == 0) { |
| 257 | /* |
| 258 | * First two arcs are encoded through the backdoor. |
| 259 | */ |
| 260 | if(i) { |
| 261 | add = -80; |
| 262 | if(cb("2", 1, app_key)) return -1; |
| 263 | } else if(b <= 39) { |
| 264 | add = 0; |
| 265 | if(cb("0", 1, app_key)) return -1; |
| 266 | } else if(b < 79) { |
| 267 | add = -40; |
| 268 | if(cb("1", 1, app_key)) return -1; |
| 269 | } else { |
| 270 | add = -80; |
| 271 | if(cb("2", 1, app_key)) return -1; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if(cb(" ", 1, app_key)) /* Separate arcs */ |
| 276 | return -1; |
| 277 | |
| 278 | if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn], |
| 279 | i - startn + 1, add, |
| 280 | cb, app_key)) |
| 281 | return -1; |
| 282 | startn = i + 1; |
| 283 | add = 0; |
| 284 | } |
| 285 | |
| 286 | return cb(" }", 2, app_key); |
| 287 | } |
| 288 | |
| 289 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 290 | OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 291 | unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 292 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 293 | int num_arcs = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 294 | int startn = 0; |
| 295 | int add = 0; |
| 296 | int i; |
| 297 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 298 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 299 | errno = EINVAL; |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | for(i = 0; i < oid->size; i++) { |
| 304 | uint8_t b = oid->buf[i]; |
| 305 | if((b & 0x80)) /* Continuation expected */ |
| 306 | continue; |
| 307 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 308 | if(num_arcs == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | /* |
| 310 | * First two arcs are encoded through the backdoor. |
| 311 | */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 312 | unsigned LE = 1; /* Little endian */ |
| 313 | int first_arc; |
| 314 | num_arcs++; |
| 315 | if(!arc_slots) { num_arcs++; continue; } |
| 316 | |
| 317 | if(i) first_arc = 2; |
| 318 | else if(b <= 39) first_arc = 0; |
| 319 | else if(b < 79) first_arc = 1; |
| 320 | else first_arc = 2; |
| 321 | |
| 322 | add = -40 * first_arc; |
| 323 | memset(arcs, 0, arc_type_size); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 324 | *(unsigned char *)((char *)arcs |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 325 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 326 | = first_arc; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 327 | (char *)arcs += arc_type_size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | } |
| 329 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 330 | /* Decode, if has space */ |
| 331 | if(arcs < arcs_end) { |
| 332 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 333 | i - startn + 1, add, |
| 334 | arcs, arc_type_size)) |
| 335 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 336 | startn = i + 1; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 337 | (char *)arcs += arc_type_size; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 338 | add = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 339 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 340 | num_arcs++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 341 | } |
| 342 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 343 | return num_arcs; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 344 | } |
| 345 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Save the single value as an object identifier arc. |
| 349 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 350 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 351 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) { |
| 352 | /* |
| 353 | * The following conditions must hold: |
| 354 | * assert(arcval); |
| 355 | * assert(arcval_size > 0); |
| 356 | * assert(arcbuf); |
| 357 | */ |
| 358 | #ifdef WORDS_BIGENDIAN |
| 359 | const unsigned isLittleEndian = 0; |
| 360 | #else |
| 361 | unsigned LE = 1; |
| 362 | unsigned isLittleEndian = *(char *)&LE; |
| 363 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 364 | uint8_t *tp, *tend; |
| 365 | unsigned int cache; |
| 366 | uint8_t *bp = arcbuf; |
| 367 | int bits; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 368 | #ifdef __GNUC__ |
| 369 | uint8_t buffer[arcval_size]; |
| 370 | #else |
| 371 | uint8_t *buffer = alloca(arcval_size); |
vlm | 7a6a60e | 2004-08-11 07:41:45 +0000 | [diff] [blame] | 372 | if(!buffer) { errno = ENOMEM; return -1; } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 373 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 374 | |
| 375 | if(isLittleEndian && !prepared_order) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 376 | uint8_t *a = (unsigned char *)arcval + arcval_size - 1; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 377 | uint8_t *aend = (uint8_t *)arcval; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 378 | uint8_t *msb = buffer + arcval_size - 1; |
| 379 | for(tp = buffer; a >= aend; tp++, a--) |
| 380 | if((*tp = *a) && (tp < msb)) |
| 381 | msb = tp; |
| 382 | tend = &buffer[arcval_size]; |
| 383 | tp = msb; /* Most significant non-zero byte */ |
| 384 | } else { |
| 385 | /* Look for most significant non-zero byte */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 386 | tend = (unsigned char *)arcval + arcval_size; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 387 | for(tp = (uint8_t *)arcval; tp < tend - 1; tp++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 388 | if(*tp) break; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Split the value in 7-bits chunks. |
| 393 | */ |
| 394 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 395 | if(bits) { |
| 396 | cache = *tp >> (CHAR_BIT - bits); |
| 397 | if(cache) { |
| 398 | *bp++ = cache | 0x80; |
| 399 | cache = *tp++; |
| 400 | bits = CHAR_BIT - bits; |
| 401 | } else { |
| 402 | bits = -bits; |
| 403 | } |
| 404 | } else { |
| 405 | cache = 0; |
| 406 | } |
| 407 | for(; tp < tend; tp++) { |
| 408 | cache = (cache << CHAR_BIT) + *tp; |
| 409 | bits += CHAR_BIT; |
| 410 | while(bits >= 7) { |
| 411 | bits -= 7; |
| 412 | *bp++ = 0x80 | (cache >> bits); |
| 413 | } |
| 414 | } |
| 415 | if(bits) *bp++ = cache; |
| 416 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 417 | |
| 418 | return bp - arcbuf; |
| 419 | } |
| 420 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 421 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 422 | OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 423 | uint8_t *buf; |
| 424 | uint8_t *bp; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 425 | unsigned LE = 1; /* Little endian (x86) */ |
| 426 | unsigned isLittleEndian = *((char *)&LE); |
| 427 | unsigned int arc0; |
| 428 | unsigned int arc1; |
| 429 | unsigned size; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 430 | unsigned i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 432 | if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | errno = EINVAL; |
| 434 | return -1; |
| 435 | } |
| 436 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 437 | switch(arc_type_size) { |
| 438 | case sizeof(char): |
| 439 | arc0 = ((unsigned char *)arcs)[0]; |
| 440 | arc1 = ((unsigned char *)arcs)[1]; |
| 441 | break; |
| 442 | case sizeof(short): |
| 443 | arc0 = ((unsigned short *)arcs)[0]; |
| 444 | arc1 = ((unsigned short *)arcs)[1]; |
| 445 | break; |
| 446 | case sizeof(int): |
| 447 | arc0 = ((unsigned int *)arcs)[0]; |
| 448 | arc1 = ((unsigned int *)arcs)[1]; |
| 449 | break; |
| 450 | default: |
| 451 | arc1 = arc0 = 0; |
| 452 | if(isLittleEndian) { /* Little endian (x86) */ |
| 453 | unsigned char *ps, *pe; |
| 454 | /* If more significant bytes are present, |
| 455 | * make them > 255 quick */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 456 | for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 457 | ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 458 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 459 | arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
| 460 | arc0 = *((unsigned char *)arcs + 0); |
| 461 | arc1 = *((unsigned char *)arcs + arc_type_size); |
| 462 | } else { |
| 463 | unsigned char *ps, *pe; |
| 464 | /* If more significant bytes are present, |
| 465 | * make them > 255 quick */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 466 | for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 467 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 468 | arc0 = *((unsigned char *)arcs + arc_type_size - 1); |
| 469 | arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * The previous chapter left us with the first and the second arcs. |
| 475 | * The values are not precise (that is, they are valid only if |
| 476 | * they're less than 255), but OK for the purposes of making |
| 477 | * the sanity test below. |
| 478 | */ |
| 479 | if(arc0 <= 1) { |
| 480 | if(arc1 >= 39) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 482 | errno = ERANGE; |
| 483 | return -1; |
| 484 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 485 | } else if(arc0 > 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | /* 8.19.4: Only three values are allocated from the root node */ |
| 487 | errno = ERANGE; |
| 488 | return -1; |
| 489 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 490 | /* |
| 491 | * After above tests it is known that the value of arc0 is completely |
| 492 | * trustworthy (0..2). However, the arc1's value is still meaningless. |
| 493 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * Roughly estimate the maximum size necessary to encode these arcs. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 497 | * This estimation implicitly takes in account the following facts, |
| 498 | * that cancel each other: |
| 499 | * * the first two arcs are encoded in a single value. |
| 500 | * * the first value may require more space (+1 byte) |
| 501 | * * the value of the first arc which is in range (0..2) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 503 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 504 | bp = buf = (uint8_t *)MALLOC(size + 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 505 | if(!buf) { |
| 506 | /* ENOMEM */ |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | /* |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 511 | * Encode the first two arcs. |
| 512 | * These require special treatment. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 514 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 515 | uint8_t *tp; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 516 | #ifdef __GNUC__ |
| 517 | uint8_t first_value[1 + arc_type_size]; /* of two arcs */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 518 | uint8_t *fv = first_value; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 519 | #else |
| 520 | uint8_t *first_value = alloca(1 + arc_type_size); |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 521 | uint8_t *fv = first_value; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 522 | if(!first_value) { |
| 523 | errno = ENOMEM; |
| 524 | return -1; |
| 525 | } |
| 526 | #endif |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 527 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 528 | /* |
| 529 | * Simulate first_value = arc0 * 40 + arc1; |
| 530 | */ |
| 531 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 532 | *fv++ = 0; |
| 533 | (char *)arcs += arc_type_size; |
| 534 | if(isLittleEndian) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 535 | uint8_t *aend = (unsigned char *)arcs - 1; |
| 536 | uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 537 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | } else { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 539 | uint8_t *a1 = (uint8_t *)arcs; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 540 | uint8_t *aend = a1 + arc_type_size; |
| 541 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 542 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 543 | /* Increase the first_value by arc0 */ |
| 544 | arc0 *= 40; /* (0..80) */ |
| 545 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 546 | unsigned int v = *tp; |
| 547 | v += arc0; |
| 548 | *tp = v; |
| 549 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 550 | else break; |
| 551 | } |
| 552 | |
| 553 | assert(tp >= first_value); |
| 554 | |
| 555 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 556 | fv - first_value, 1); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Save the rest of arcs. |
| 561 | */ |
| 562 | for((char *)arcs += arc_type_size, i = 2; |
| 563 | i < arc_slots; i++, (char *)arcs += arc_type_size) { |
| 564 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 565 | arcs, arc_type_size, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 566 | } |
| 567 | |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 568 | assert((unsigned)(bp - buf) <= size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * Replace buffer. |
| 572 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 573 | oid->size = bp - buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 574 | bp = oid->buf; |
| 575 | oid->buf = buf; |
| 576 | if(bp) FREEMEM(bp); |
| 577 | |
| 578 | return 0; |
| 579 | } |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 580 | |