| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <asn_internal.h> |
| 6 |
#include <OBJECT_IDENTIFIER.h> |
| 7 |
#include <OCTET_STRING.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = { |
| 15 |
(ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 16 |
}; |
| 17 |
asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = { |
| 18 |
"OBJECT IDENTIFIER", |
| 19 |
"OBJECT_IDENTIFIER", |
| 20 |
ASN__PRIMITIVE_TYPE_free, |
| 21 |
OBJECT_IDENTIFIER_print, |
| 22 |
OBJECT_IDENTIFIER_constraint, |
| 23 |
ber_decode_primitive, |
| 24 |
der_encode_primitive, |
| 25 |
OBJECT_IDENTIFIER_decode_xer, |
| 26 |
OBJECT_IDENTIFIER_encode_xer, |
| 27 |
OCTET_STRING_decode_uper, |
| 28 |
OCTET_STRING_encode_uper, |
| 29 |
0, |
| 30 |
asn_DEF_OBJECT_IDENTIFIER_tags, |
| 31 |
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 32 |
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 33 |
asn_DEF_OBJECT_IDENTIFIER_tags, |
| 34 |
sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 35 |
/ sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 36 |
0, |
| 37 |
0, 0, |
| 38 |
0 |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
int |
| 43 |
OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
| 44 |
asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 45 |
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 46 |
|
| 47 |
if(st && st->buf) { |
| 48 |
if(st->size < 1) { |
| 49 |
_ASN_CTFAIL(app_key, td, sptr, |
| 50 |
"%s: at least one numerical value " |
| 51 |
"expected (%s:%d)", |
| 52 |
td->name, __FILE__, __LINE__); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
} else { |
| 56 |
_ASN_CTFAIL(app_key, td, sptr, |
| 57 |
"%s: value not given (%s:%d)", |
| 58 |
td->name, __FILE__, __LINE__); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
int |
| 67 |
OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) { |
| 68 |
unsigned LE GCC_NOTUSED = 1; |
| 69 |
uint8_t *arcend = arcbuf + arclen; |
| 70 |
unsigned int cache = 0; |
| 71 |
unsigned char *rvbuf = (unsigned char *)rvbufp; |
| 72 |
unsigned char *rvstart = rvbuf; |
| 73 |
int inc; |
| 74 |
|
| 75 |
rvsize *= CHAR_BIT; |
| 76 |
arclen *= 7; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if(arclen > rvsize) { |
| 83 |
if(arclen > (rvsize + CHAR_BIT)) { |
| 84 |
errno = ERANGE; |
| 85 |
return -1; |
| 86 |
} else { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 101 |
if((*arcbuf & mask)) { |
| 102 |
errno = ERANGE; |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
arclen -= 7; |
| 107 |
cache = *arcbuf & 0x7f; |
| 108 |
arcbuf++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
if(rvsize == (CHAR_BIT * sizeof(unsigned long))) { |
| 114 |
unsigned long accum; |
| 115 |
|
| 116 |
for(accum = cache; arcbuf < arcend; arcbuf++) |
| 117 |
accum = (accum << 7) | (*arcbuf & ~0x80); |
| 118 |
if(accum < (unsigned)-add) { |
| 119 |
errno = ERANGE; |
| 120 |
return -1; |
| 121 |
} |
| 122 |
*(unsigned long *)rvbuf = accum + add; |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
#ifndef WORDS_BIGENDIAN |
| 127 |
if(*(unsigned char *)&LE) { |
| 128 |
|
| 129 |
rvbuf += rvsize / CHAR_BIT - 1; |
| 130 |
rvstart--; |
| 131 |
inc = -1; |
| 132 |
} else |
| 133 |
#endif |
| 134 |
inc = +1; |
| 135 |
|
| 136 |
{ |
| 137 |
int bits; |
| 138 |
|
| 139 |
|
| 140 |
for(bits = rvsize - arclen; |
| 141 |
bits > CHAR_BIT; |
| 142 |
rvbuf += inc, bits -= CHAR_BIT) |
| 143 |
*rvbuf = 0; |
| 144 |
|
| 145 |
|
| 146 |
for(; arcbuf < arcend; arcbuf++) { |
| 147 |
cache = (cache << 7) | (*arcbuf & 0x7f); |
| 148 |
bits += 7; |
| 149 |
if(bits >= CHAR_BIT) { |
| 150 |
bits -= CHAR_BIT; |
| 151 |
*rvbuf = (cache >> bits); |
| 152 |
rvbuf += inc; |
| 153 |
} |
| 154 |
} |
| 155 |
if(bits) { |
| 156 |
*rvbuf = cache; |
| 157 |
rvbuf += inc; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if(add) { |
| 162 |
for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) { |
| 163 |
int v = add + *rvbuf; |
| 164 |
if(v & (-1 << CHAR_BIT)) { |
| 165 |
*rvbuf = (unsigned char)(v + (1 << CHAR_BIT)); |
| 166 |
add = -1; |
| 167 |
} else { |
| 168 |
*rvbuf = v; |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
if(rvbuf == rvstart) { |
| 173 |
|
| 174 |
errno = ERANGE; |
| 175 |
return -1; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
ssize_t |
| 183 |
OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add, |
| 184 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 185 |
char scratch[64]; |
| 186 |
unsigned long accum; |
| 187 |
char *p; |
| 188 |
|
| 189 |
if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 190 |
&accum, sizeof(accum))) |
| 191 |
return -1; |
| 192 |
|
| 193 |
if(accum) { |
| 194 |
ssize_t len; |
| 195 |
|
| 196 |
|
| 197 |
p = scratch + sizeof(scratch); |
| 198 |
for(; accum; accum /= 10) |
| 199 |
*(--p) = (char)(accum % 10) + 0x30; |
| 200 |
|
| 201 |
len = sizeof(scratch) - (p - scratch); |
| 202 |
if(cb(p, len, app_key) < 0) |
| 203 |
return -1; |
| 204 |
return len; |
| 205 |
} else { |
| 206 |
*scratch = 0x30; |
| 207 |
if(cb(scratch, 1, app_key) < 0) |
| 208 |
return -1; |
| 209 |
return 1; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
int |
| 214 |
OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add, |
| 215 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 216 |
|
| 217 |
if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0) |
| 218 |
return -1; |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static ssize_t |
| 224 |
OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
| 225 |
ssize_t wrote_len = 0; |
| 226 |
int startn; |
| 227 |
int add = 0; |
| 228 |
int i; |
| 229 |
|
| 230 |
for(i = 0, startn = 0; i < st->size; i++) { |
| 231 |
uint8_t b = st->buf[i]; |
| 232 |
if((b & 0x80)) |
| 233 |
continue; |
| 234 |
|
| 235 |
if(startn == 0) { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if(i) { |
| 240 |
add = -80; |
| 241 |
if(cb("2", 1, app_key) < 0) return -1; |
| 242 |
} else if(b <= 39) { |
| 243 |
add = 0; |
| 244 |
if(cb("0", 1, app_key) < 0) return -1; |
| 245 |
} else if(b < 79) { |
| 246 |
add = -40; |
| 247 |
if(cb("1", 1, app_key) < 0) return -1; |
| 248 |
} else { |
| 249 |
add = -80; |
| 250 |
if(cb("2", 1, app_key) < 0) return -1; |
| 251 |
} |
| 252 |
wrote_len += 1; |
| 253 |
} |
| 254 |
|
| 255 |
if(cb(".", 1, app_key) < 0) |
| 256 |
return -1; |
| 257 |
|
| 258 |
add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn], |
| 259 |
i - startn + 1, add, cb, app_key); |
| 260 |
if(add < 0) return -1; |
| 261 |
wrote_len += 1 + add; |
| 262 |
startn = i + 1; |
| 263 |
add = 0; |
| 264 |
} |
| 265 |
|
| 266 |
return wrote_len; |
| 267 |
} |
| 268 |
|
| 269 |
static enum xer_pbd_rval |
| 270 |
OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) { |
| 271 |
OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr; |
| 272 |
const char *chunk_end = (const char *)chunk_buf + chunk_size; |
| 273 |
const char *endptr; |
| 274 |
long s_arcs[10]; |
| 275 |
long *arcs = s_arcs; |
| 276 |
int arcs_count; |
| 277 |
int ret; |
| 278 |
|
| 279 |
(void)td; |
| 280 |
|
| 281 |
arcs_count = OBJECT_IDENTIFIER_parse_arcs( |
| 282 |
(const char *)chunk_buf, chunk_size, arcs, |
| 283 |
sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr); |
| 284 |
if(arcs_count <= 0) { |
| 285 |
|
| 286 |
return XPBD_BROKEN_ENCODING; |
| 287 |
} |
| 288 |
if(endptr < chunk_end) { |
| 289 |
|
| 290 |
if(!xer_is_whitespace(endptr, chunk_end - endptr)) |
| 291 |
return XPBD_BROKEN_ENCODING; |
| 292 |
} |
| 293 |
|
| 294 |
if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) { |
| 295 |
arcs = (long *)MALLOC(arcs_count * sizeof(long)); |
| 296 |
if(!arcs) return XPBD_SYSTEM_FAILURE; |
| 297 |
ret = OBJECT_IDENTIFIER_parse_arcs( |
| 298 |
(const char *)chunk_buf, chunk_size, |
| 299 |
arcs, arcs_count, &endptr); |
| 300 |
if(ret != arcs_count) |
| 301 |
return XPBD_SYSTEM_FAILURE; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count); |
| 308 |
if(arcs != s_arcs) FREEMEM(arcs); |
| 309 |
|
| 310 |
return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED; |
| 311 |
} |
| 312 |
|
| 313 |
asn_dec_rval_t |
| 314 |
OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 315 |
asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 316 |
const void *buf_ptr, size_t size) { |
| 317 |
|
| 318 |
return xer_decode_primitive(opt_codec_ctx, td, |
| 319 |
sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname, |
| 320 |
buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode); |
| 321 |
} |
| 322 |
|
| 323 |
asn_enc_rval_t |
| 324 |
OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 325 |
int ilevel, enum xer_encoder_flags_e flags, |
| 326 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 327 |
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 328 |
asn_enc_rval_t er; |
| 329 |
|
| 330 |
(void)ilevel; |
| 331 |
(void)flags; |
| 332 |
|
| 333 |
if(!st || !st->buf) |
| 334 |
_ASN_ENCODE_FAILED; |
| 335 |
|
| 336 |
er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key); |
| 337 |
if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 338 |
|
| 339 |
_ASN_ENCODED_OK(er); |
| 340 |
} |
| 341 |
|
| 342 |
int |
| 343 |
OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr, |
| 344 |
int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
| 345 |
const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 346 |
|
| 347 |
(void)td; |
| 348 |
(void)ilevel; |
| 349 |
|
| 350 |
if(!st || !st->buf) |
| 351 |
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 352 |
|
| 353 |
|
| 354 |
if(cb("{ ", 2, app_key) < 0) |
| 355 |
return -1; |
| 356 |
|
| 357 |
if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) |
| 358 |
return -1; |
| 359 |
|
| 360 |
return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
| 361 |
} |
| 362 |
|
| 363 |
int |
| 364 |
OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 365 |
unsigned int arc_type_size, unsigned int arc_slots) { |
| 366 |
void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
| 367 |
int num_arcs = 0; |
| 368 |
int startn = 0; |
| 369 |
int add = 0; |
| 370 |
int i; |
| 371 |
|
| 372 |
if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
| 373 |
errno = EINVAL; |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
for(i = 0; i < oid->size; i++) { |
| 378 |
uint8_t b = oid->buf[i]; |
| 379 |
if((b & 0x80)) |
| 380 |
continue; |
| 381 |
|
| 382 |
if(num_arcs == 0) { |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
unsigned LE = 1; |
| 387 |
int first_arc; |
| 388 |
num_arcs++; |
| 389 |
if(!arc_slots) { num_arcs++; continue; } |
| 390 |
|
| 391 |
if(i) first_arc = 2; |
| 392 |
else if(b <= 39) first_arc = 0; |
| 393 |
else if(b < 79) first_arc = 1; |
| 394 |
else first_arc = 2; |
| 395 |
|
| 396 |
add = -40 * first_arc; |
| 397 |
memset(arcs, 0, arc_type_size); |
| 398 |
*(unsigned char *)((char *)arcs |
| 399 |
+ ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 400 |
= first_arc; |
| 401 |
arcs = ((char *)arcs) + arc_type_size; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
if(arcs < arcs_end) { |
| 406 |
if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 407 |
i - startn + 1, add, |
| 408 |
arcs, arc_type_size)) |
| 409 |
return -1; |
| 410 |
startn = i + 1; |
| 411 |
arcs = ((char *)arcs) + arc_type_size; |
| 412 |
add = 0; |
| 413 |
} |
| 414 |
num_arcs++; |
| 415 |
} |
| 416 |
|
| 417 |
return num_arcs; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
int |
| 425 |
OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) { |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
#ifdef WORDS_BIGENDIAN |
| 434 |
const unsigned isLittleEndian = 0; |
| 435 |
#else |
| 436 |
unsigned LE = 1; |
| 437 |
unsigned isLittleEndian = *(char *)&LE; |
| 438 |
#endif |
| 439 |
const uint8_t *tend, *tp; |
| 440 |
unsigned int cache; |
| 441 |
uint8_t *bp = arcbuf; |
| 442 |
int bits; |
| 443 |
uint8_t buffer[16]; |
| 444 |
|
| 445 |
if(isLittleEndian && !prepared_order) { |
| 446 |
const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1; |
| 447 |
const uint8_t *aend = (const uint8_t *)arcval; |
| 448 |
uint8_t *msb = buffer + arcval_size - 1; |
| 449 |
uint8_t *tb; |
| 450 |
for(tb = buffer; a >= aend; tb++, a--) |
| 451 |
if((*tb = *a) && (tb < msb)) |
| 452 |
msb = tb; |
| 453 |
tend = &buffer[arcval_size]; |
| 454 |
tp = msb; |
| 455 |
} else { |
| 456 |
|
| 457 |
tend = (const unsigned char *)arcval + arcval_size; |
| 458 |
for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++) |
| 459 |
if(*tp) break; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
bits = ((tend - tp) * CHAR_BIT) % 7; |
| 466 |
if(bits) { |
| 467 |
cache = *tp >> (CHAR_BIT - bits); |
| 468 |
if(cache) { |
| 469 |
*bp++ = cache | 0x80; |
| 470 |
cache = *tp++; |
| 471 |
bits = CHAR_BIT - bits; |
| 472 |
} else { |
| 473 |
bits = -bits; |
| 474 |
} |
| 475 |
} else { |
| 476 |
cache = 0; |
| 477 |
} |
| 478 |
for(; tp < tend; tp++) { |
| 479 |
cache = (cache << CHAR_BIT) + *tp; |
| 480 |
bits += CHAR_BIT; |
| 481 |
while(bits >= 7) { |
| 482 |
bits -= 7; |
| 483 |
*bp++ = 0x80 | (cache >> bits); |
| 484 |
} |
| 485 |
} |
| 486 |
if(bits) *bp++ = cache; |
| 487 |
bp[-1] &= 0x7f; |
| 488 |
|
| 489 |
return bp - arcbuf; |
| 490 |
} |
| 491 |
|
| 492 |
int |
| 493 |
OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
| 494 |
uint8_t *buf; |
| 495 |
uint8_t *bp; |
| 496 |
unsigned LE = 1; |
| 497 |
unsigned isLittleEndian = *((char *)&LE); |
| 498 |
unsigned int arc0; |
| 499 |
unsigned int arc1; |
| 500 |
unsigned size; |
| 501 |
unsigned i; |
| 502 |
|
| 503 |
if(!oid || !arcs || arc_type_size < 1 |
| 504 |
|| arc_type_size > 16 |
| 505 |
|| arc_slots < 2) { |
| 506 |
errno = EINVAL; |
| 507 |
return -1; |
| 508 |
} |
| 509 |
|
| 510 |
switch(arc_type_size) { |
| 511 |
case sizeof(char): |
| 512 |
arc0 = ((const unsigned char *)arcs)[0]; |
| 513 |
arc1 = ((const unsigned char *)arcs)[1]; |
| 514 |
break; |
| 515 |
case sizeof(short): |
| 516 |
arc0 = ((const unsigned short *)arcs)[0]; |
| 517 |
arc1 = ((const unsigned short *)arcs)[1]; |
| 518 |
break; |
| 519 |
case sizeof(int): |
| 520 |
arc0 = ((const unsigned int *)arcs)[0]; |
| 521 |
arc1 = ((const unsigned int *)arcs)[1]; |
| 522 |
break; |
| 523 |
default: |
| 524 |
arc1 = arc0 = 0; |
| 525 |
if(isLittleEndian) { |
| 526 |
const unsigned char *ps, *pe; |
| 527 |
|
| 528 |
|
| 529 |
for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 530 |
ps < pe; ps++) |
| 531 |
arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 532 |
arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
| 533 |
arc0 = *((const unsigned char *)arcs + 0); |
| 534 |
arc1 = *((const unsigned char *)arcs + arc_type_size); |
| 535 |
} else { |
| 536 |
const unsigned char *ps, *pe; |
| 537 |
|
| 538 |
|
| 539 |
for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++) |
| 540 |
arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 541 |
arc0 = *((const unsigned char *)arcs + arc_type_size - 1); |
| 542 |
arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
if(arc0 <= 1) { |
| 553 |
if(arc1 >= 39) { |
| 554 |
|
| 555 |
errno = ERANGE; |
| 556 |
return -1; |
| 557 |
} |
| 558 |
} else if(arc0 > 2) { |
| 559 |
|
| 560 |
errno = ERANGE; |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
| 577 |
bp = buf = (uint8_t *)MALLOC(size + 1); |
| 578 |
if(!buf) { |
| 579 |
|
| 580 |
return -1; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
{ |
| 588 |
uint8_t *tp; |
| 589 |
uint8_t first_value[1 + 16]; |
| 590 |
uint8_t *fv = first_value; |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
*fv++ = 0; |
| 597 |
arcs = ((const char *)arcs) + arc_type_size; |
| 598 |
if(isLittleEndian) { |
| 599 |
const uint8_t *aend = (const unsigned char *)arcs - 1; |
| 600 |
const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1; |
| 601 |
for(; a1 > aend; fv++, a1--) *fv = *a1; |
| 602 |
} else { |
| 603 |
const uint8_t *a1 = (const uint8_t *)arcs; |
| 604 |
const uint8_t *aend = a1 + arc_type_size; |
| 605 |
for(; a1 < aend; fv++, a1++) *fv = *a1; |
| 606 |
} |
| 607 |
|
| 608 |
arc0 *= 40; |
| 609 |
for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 610 |
unsigned int v = *tp; |
| 611 |
v += arc0; |
| 612 |
*tp = v; |
| 613 |
if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 614 |
else break; |
| 615 |
} |
| 616 |
|
| 617 |
assert(tp >= first_value); |
| 618 |
|
| 619 |
bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 620 |
fv - first_value, 1); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
for(arcs = ((const char *)arcs) + arc_type_size, i = 2; |
| 627 |
i < arc_slots; |
| 628 |
i++, arcs = ((const char *)arcs) + arc_type_size) { |
| 629 |
bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 630 |
arcs, arc_type_size, 0); |
| 631 |
} |
| 632 |
|
| 633 |
assert((unsigned)(bp - buf) <= size); |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
oid->size = bp - buf; |
| 639 |
bp = oid->buf; |
| 640 |
oid->buf = buf; |
| 641 |
if(bp) FREEMEM(bp); |
| 642 |
|
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
int |
| 648 |
OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length, |
| 649 |
long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) { |
| 650 |
unsigned int arcs_count = 0; |
| 651 |
const char *oid_end; |
| 652 |
long value = 0; |
| 653 |
enum { |
| 654 |
ST_SKIPSPACE, |
| 655 |
ST_WAITDIGITS, |
| 656 |
ST_DIGITS |
| 657 |
} state = ST_SKIPSPACE; |
| 658 |
|
| 659 |
if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) { |
| 660 |
if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
| 661 |
errno = EINVAL; |
| 662 |
return -1; |
| 663 |
} |
| 664 |
|
| 665 |
if(oid_txt_length == -1) |
| 666 |
oid_txt_length = strlen(oid_text); |
| 667 |
|
| 668 |
for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) { |
| 669 |
switch(*oid_text) { |
| 670 |
case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 671 |
if(state == ST_SKIPSPACE) { |
| 672 |
continue; |
| 673 |
} else { |
| 674 |
break; |
| 675 |
} |
| 676 |
case 0x2e: |
| 677 |
if(state != ST_DIGITS |
| 678 |
|| (oid_text + 1) == oid_end) { |
| 679 |
state = ST_WAITDIGITS; |
| 680 |
break; |
| 681 |
} |
| 682 |
if(arcs_count < arcs_slots) |
| 683 |
arcs[arcs_count] = value; |
| 684 |
arcs_count++; |
| 685 |
state = ST_WAITDIGITS; |
| 686 |
continue; |
| 687 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 688 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 689 |
if(state != ST_DIGITS) { |
| 690 |
state = ST_DIGITS; |
| 691 |
value = 0; |
| 692 |
} |
| 693 |
if(1) { |
| 694 |
long new_value = value * 10; |
| 695 |
if(new_value / 10 != value |
| 696 |
|| (value = new_value + (*oid_text - 0x30)) < 0) { |
| 697 |
|
| 698 |
state = ST_WAITDIGITS; |
| 699 |
break; |
| 700 |
} |
| 701 |
continue; |
| 702 |
} |
| 703 |
default: |
| 704 |
|
| 705 |
state = ST_WAITDIGITS; |
| 706 |
break; |
| 707 |
} |
| 708 |
break; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
| 713 |
|
| 714 |
|
| 715 |
switch(state) { |
| 716 |
case ST_WAITDIGITS: |
| 717 |
errno = EINVAL; |
| 718 |
return -1; |
| 719 |
case ST_DIGITS: |
| 720 |
if(arcs_count < arcs_slots) |
| 721 |
arcs[arcs_count] = value; |
| 722 |
arcs_count++; |
| 723 |
|
| 724 |
default: |
| 725 |
return arcs_count; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|