2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
7 #include <OBJECT_IDENTIFIER.h>
8 #include <OCTET_STRING.h>
9 #include <limits.h> /* for CHAR_BIT */
13 * OBJECT IDENTIFIER basic type description.
15 static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
16 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
18 asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
21 ASN__PRIMITIVE_TYPE_free,
22 OBJECT_IDENTIFIER_print,
23 OBJECT_IDENTIFIER_constraint,
26 OBJECT_IDENTIFIER_decode_xer,
27 OBJECT_IDENTIFIER_encode_xer,
28 OCTET_STRING_decode_uper,
29 OCTET_STRING_encode_uper,
30 0, /* Use generic outmost tag fetcher */
31 asn_DEF_OBJECT_IDENTIFIER_tags,
32 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
33 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
34 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
35 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
36 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
37 0, /* No PER visible constraints */
38 0, 0, /* No members */
44 OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
45 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
46 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
50 ASN__CTFAIL(app_key, td, sptr,
51 "%s: at least one numerical value "
53 td->name, __FILE__, __LINE__);
57 ASN__CTFAIL(app_key, td, sptr,
58 "%s: value not given (%s:%d)",
59 td->name, __FILE__, __LINE__);
68 OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
69 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
70 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
71 unsigned int cache = 0; /* No more than 14 significant bits */
72 unsigned char *rvbuf = (unsigned char *)rvbufp;
73 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
74 int inc; /* Return value growth direction */
76 rvsize *= CHAR_BIT; /* bytes to bits */
77 arclen *= 7; /* bytes to bits */
80 * The arc has the number of bits
81 * cannot be represented using supplied return value type.
84 if(arclen > (rvsize + CHAR_BIT)) {
85 errno = ERANGE; /* Overflow */
89 * Even if the number of bits in the arc representation
90 * is higher than the width of supplied * return value
91 * type, there is still possible to fit it when there
92 * are few unused high bits in the arc value
95 * Moreover, there is a possibility that the
96 * number could actually fit the arc space, given
97 * that add is negative, but we don't handle
98 * such "temporary lack of precision" situation here.
99 * May be considered as a bug.
101 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
102 if((*arcbuf & mask)) {
103 errno = ERANGE; /* Overflow */
106 /* Fool the routine computing unused bits */
108 cache = *arcbuf & 0x7f;
113 /* Faster path for common size */
114 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
116 /* Gather all bits into the accumulator */
117 for(accum = cache; arcbuf < arcend; arcbuf++)
118 accum = (accum << 7) | (*arcbuf & ~0x80);
119 if(accum < (unsigned)-add) {
120 errno = ERANGE; /* Overflow */
123 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
127 #ifndef WORDS_BIGENDIAN
128 if(*(unsigned char *)&LE) { /* Little endian (x86) */
129 /* "Convert" to big endian */
130 rvbuf += rvsize / CHAR_BIT - 1;
132 inc = -1; /* Descending */
134 #endif /* !WORDS_BIGENDIAN */
135 inc = +1; /* Big endian is known [at compile time] */
138 int bits; /* typically no more than 3-4 bits */
140 /* Clear the high unused bits */
141 for(bits = rvsize - arclen;
143 rvbuf += inc, bits -= CHAR_BIT)
146 /* Fill the body of a value */
147 for(; arcbuf < arcend; arcbuf++) {
148 cache = (cache << 7) | (*arcbuf & 0x7f);
150 if(bits >= CHAR_BIT) {
152 *rvbuf = (cache >> bits);
163 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
164 int v = add + *rvbuf;
165 if(v & ((unsigned)~0 << CHAR_BIT)) {
166 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
173 if(rvbuf == rvstart) {
174 /* No space to carry over */
175 errno = ERANGE; /* Overflow */
184 OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
185 asn_app_consume_bytes_f *cb, void *app_key) {
186 char scratch[64]; /* Conservative estimate */
187 unsigned long accum; /* Bits accumulator */
188 char *p; /* Position in the scratch buffer */
190 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
191 &accum, sizeof(accum)))
197 /* Fill the scratch buffer in reverse. */
198 p = scratch + sizeof(scratch);
199 for(; accum; accum /= 10)
200 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
202 len = sizeof(scratch) - (p - scratch);
203 if(cb(p, len, app_key) < 0)
208 if(cb(scratch, 1, app_key) < 0)
215 OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
216 asn_app_consume_bytes_f *cb, void *app_key) {
218 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
225 OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
226 ssize_t wrote_len = 0;
231 for(i = 0, startn = 0; i < st->size; i++) {
232 uint8_t b = st->buf[i];
233 if((b & 0x80)) /* Continuation expected */
238 * First two arcs are encoded through the backdoor.
242 if(cb("2", 1, app_key) < 0) return -1;
245 if(cb("0", 1, app_key) < 0) return -1;
248 if(cb("1", 1, app_key) < 0) return -1;
251 if(cb("2", 1, app_key) < 0) return -1;
256 if(cb(".", 1, app_key) < 0) /* Separate arcs */
259 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
260 i - startn + 1, add, cb, app_key);
261 if(add < 0) return -1;
262 wrote_len += 1 + add;
270 static enum xer_pbd_rval
271 OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
272 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
273 const char *chunk_end = (const char *)chunk_buf + chunk_size;
282 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
283 (const char *)chunk_buf, chunk_size, arcs,
284 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
286 /* Expecting more than zero arcs */
287 return XPBD_BROKEN_ENCODING;
288 } else if(arcs_count == 0) {
289 return XPBD_NOT_BODY_IGNORE;
291 assert(endptr == chunk_end);
293 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
294 arcs = (long *)MALLOC(arcs_count * sizeof(long));
295 if(!arcs) return XPBD_SYSTEM_FAILURE;
296 ret = OBJECT_IDENTIFIER_parse_arcs(
297 (const char *)chunk_buf, chunk_size,
298 arcs, arcs_count, &endptr);
299 if(ret != arcs_count)
300 return XPBD_SYSTEM_FAILURE; /* assert?.. */
304 * Convert arcs into BER representation.
306 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
307 if(arcs != s_arcs) FREEMEM(arcs);
309 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
313 OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
314 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
315 const void *buf_ptr, size_t size) {
317 return xer_decode_primitive(opt_codec_ctx, td,
318 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
319 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
323 OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
324 int ilevel, enum xer_encoder_flags_e flags,
325 asn_app_consume_bytes_f *cb, void *app_key) {
326 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
335 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
336 if(er.encoded < 0) ASN__ENCODE_FAILED;
342 OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
343 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
344 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
346 (void)td; /* Unused argument */
347 (void)ilevel; /* Unused argument */
350 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
353 if(cb("{ ", 2, app_key) < 0)
356 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
359 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
363 OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
364 unsigned int arc_type_size, unsigned int arc_slots) {
365 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
371 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
376 for(i = 0; i < oid->size; i++) {
377 uint8_t b = oid->buf[i];
378 if((b & 0x80)) /* Continuation expected */
383 * First two arcs are encoded through the backdoor.
385 unsigned LE = 1; /* Little endian */
388 if(!arc_slots) { num_arcs++; continue; }
391 else if(b <= 39) first_arc = 0;
392 else if(b < 79) first_arc = 1;
395 add = -40 * first_arc;
396 memset(arcs, 0, arc_type_size);
397 *(unsigned char *)((char *)arcs
398 + ((*(char *)&LE)?0:(arc_type_size - 1)))
400 arcs = ((char *)arcs) + arc_type_size;
403 /* Decode, if has space */
404 if(arcs < arcs_end) {
405 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
407 arcs, arc_type_size))
410 arcs = ((char *)arcs) + arc_type_size;
421 * Save the single value as an object identifier arc.
424 OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
426 * The following conditions must hold:
428 * assert(arcval_size > 0);
429 * assert(arcval_size <= 16);
432 #ifdef WORDS_BIGENDIAN
433 const unsigned isLittleEndian = 0;
436 unsigned isLittleEndian = *(char *)&LE;
438 const uint8_t *tend, *tp;
440 uint8_t *bp = arcbuf;
444 if(isLittleEndian && !prepared_order) {
445 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
446 const uint8_t *aend = (const uint8_t *)arcval;
447 uint8_t *msb = buffer + arcval_size - 1;
449 for(tb = buffer; a >= aend; tb++, a--)
450 if((*tb = *a) && (tb < msb))
452 tend = &buffer[arcval_size];
453 tp = msb; /* Most significant non-zero byte */
455 /* Look for most significant non-zero byte */
456 tend = (const unsigned char *)arcval + arcval_size;
457 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
462 * Split the value in 7-bits chunks.
464 bits = ((tend - tp) * CHAR_BIT) % 7;
466 cache = *tp >> (CHAR_BIT - bits);
468 *bp++ = cache | 0x80;
470 bits = CHAR_BIT - bits;
477 for(; tp < tend; tp++) {
478 cache = (cache << CHAR_BIT) + *tp;
482 *bp++ = 0x80 | (cache >> bits);
485 if(bits) *bp++ = cache;
486 bp[-1] &= 0x7f; /* Clear the last bit */
492 OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
495 unsigned LE = 1; /* Little endian (x86) */
496 unsigned isLittleEndian = *((char *)&LE);
502 if(!oid || !arcs || arc_type_size < 1
503 || arc_type_size > 16
509 switch(arc_type_size) {
511 arc0 = ((const unsigned char *)arcs)[0];
512 arc1 = ((const unsigned char *)arcs)[1];
515 arc0 = ((const unsigned short *)arcs)[0];
516 arc1 = ((const unsigned short *)arcs)[1];
519 arc0 = ((const unsigned int *)arcs)[0];
520 arc1 = ((const unsigned int *)arcs)[1];
524 if(isLittleEndian) { /* Little endian (x86) */
525 const unsigned char *ps, *pe;
526 /* If more significant bytes are present,
527 * make them > 255 quick */
528 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
530 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
531 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
532 arc0 = *((const unsigned char *)arcs + 0);
533 arc1 = *((const unsigned char *)arcs + arc_type_size);
535 const unsigned char *ps, *pe;
536 /* If more significant bytes are present,
537 * make them > 255 quick */
538 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
539 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
540 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
541 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
546 * The previous chapter left us with the first and the second arcs.
547 * The values are not precise (that is, they are valid only if
548 * they're less than 255), but OK for the purposes of making
549 * the sanity test below.
553 /* 8.19.4: At most 39 subsequent values (including 0) */
557 } else if(arc0 > 2) {
558 /* 8.19.4: Only three values are allocated from the root node */
563 * After above tests it is known that the value of arc0 is completely
564 * trustworthy (0..2). However, the arc1's value is still meaningless.
568 * Roughly estimate the maximum size necessary to encode these arcs.
569 * This estimation implicitly takes in account the following facts,
570 * that cancel each other:
571 * * the first two arcs are encoded in a single value.
572 * * the first value may require more space (+1 byte)
573 * * the value of the first arc which is in range (0..2)
575 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
576 bp = buf = (uint8_t *)MALLOC(size + 1);
583 * Encode the first two arcs.
584 * These require special treatment.
588 uint8_t first_value[1 + 16]; /* of two arcs */
589 uint8_t *fv = first_value;
592 * Simulate first_value = arc0 * 40 + arc1;
594 /* Copy the second (1'st) arcs[1] into the first_value */
596 arcs = ((const char *)arcs) + arc_type_size;
598 const uint8_t *aend = (const unsigned char *)arcs - 1;
599 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
600 for(; a1 > aend; fv++, a1--) *fv = *a1;
602 const uint8_t *a1 = (const uint8_t *)arcs;
603 const uint8_t *aend = a1 + arc_type_size;
604 for(; a1 < aend; fv++, a1++) *fv = *a1;
606 /* Increase the first_value by arc0 */
607 arc0 *= 40; /* (0..80) */
608 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
609 unsigned int v = *tp;
612 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
616 assert(tp >= first_value);
618 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
619 fv - first_value, 1);
623 * Save the rest of arcs.
625 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
627 i++, arcs = ((const char *)arcs) + arc_type_size) {
628 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
629 arcs, arc_type_size, 0);
632 assert((unsigned)(bp - buf) <= size);
637 oid->size = bp - buf;
647 OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
648 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
649 unsigned int arcs_count = 0;
654 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
655 ST_WAITDIGITS /* Next character is expected to be a digit */
656 } state = ST_LEADSPACE;
658 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
659 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
664 if(oid_txt_length == -1)
665 oid_txt_length = strlen(oid_text);
667 #define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
668 const char *endp = oid_end; \
670 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
671 case ASN_STRTOL_EXTRA_DATA: \
672 case ASN_STRTOL_OK: \
673 if(arcs_count < arcs_slots) \
674 arcs[arcs_count] = value; \
676 oid_text = endp - 1; \
678 case ASN_STRTOL_ERROR_RANGE: \
679 if(opt_oid_text_end) \
680 *opt_oid_text_end = oid_text; \
683 case ASN_STRTOL_ERROR_INVAL: \
684 case ASN_STRTOL_EXPECT_MORE: \
685 if(opt_oid_text_end) \
686 *opt_oid_text_end = oid_text; \
692 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
694 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
700 state = ST_TAILSPACE;
703 break; /* Digits expected after ".", got whitespace */
712 *opt_oid_text_end = oid_text;
713 errno = EINVAL; /* Broken OID */
717 state = ST_WAITDIGITS;
721 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
722 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
727 *opt_oid_text_end = oid_text;
728 errno = EINVAL; /* "1. 1" => broken OID */
732 _OID_CAPTURE_ARC(oid_text, oid_end);
733 state = ST_AFTERVALUE;
738 /* Unexpected symbols */
739 state = ST_WAITDIGITS;
746 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
748 /* Finalize last arc */
751 return 0; /* No OID found in input data */
753 errno = EINVAL; /* Broken OID */
760 errno = EINVAL; /* Broken OID */