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