OBJECT IDENTIFIER and RELATIVE-OID API simplified
diff --git a/skeletons/OBJECT_IDENTIFIER.c b/skeletons/OBJECT_IDENTIFIER.c
index 86fee46..73693de 100644
--- a/skeletons/OBJECT_IDENTIFIER.c
+++ b/skeletons/OBJECT_IDENTIFIER.c
@@ -55,16 +55,6 @@
 	0	/* No specifics */
 };
 
-
-/*
- * Endianness check. Will be optimized out by the compiler.
- */
-static int
-little_endian() {
-    int le_check = 1;
-    return *(char *)&le_check;
-}
-
 int
 OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
 		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
@@ -88,209 +78,107 @@
 	return 0;
 }
 
-int
-OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
-	const uint8_t *arcend = arcbuf + arclen;	/* End of arc */
-	unsigned int cache = 0;	/* No more than 14 significant bits */
-	unsigned char *rvbuf = (unsigned char *)rvbufp;
-	unsigned char *rvstart = rvbuf;	/* Original start of the value buffer */
-	int inc;	/* Return value growth direction */
+static ssize_t
+OBJECT_IDENTIFIER_get_first_arcs(const uint8_t *arcbuf, size_t arcbuf_len,
+                                 asn_oid_arc_t *arc0, asn_oid_arc_t *arc1) {
+    asn_oid_arc_t value;
 
-	rvsize *= CHAR_BIT;	/* bytes to bits */
-	arclen *= 7;		/* bytes to bits */
+    ssize_t rd = OBJECT_IDENTIFIER_get_single_arc(arcbuf, arcbuf_len, &value);
+    if(rd <= 0) return rd;
 
-	assert(add <= 0);
+    if(value >= 80) {
+        *arc0 = 2;
+        *arc1 = value - 80;
+    } else if(value >= 40) {
+        *arc0 = 1;
+        *arc1 = value - 40;
+    } else {
+        *arc0 = 0;
+        *arc1 = value;
+    }
 
-	/*
-	 * The arc has the number of bits
-	 * cannot be represented using supplied return value type.
-	 */
-	if(arclen > rvsize) {
-		if(arclen > (rvsize + CHAR_BIT)) {
-			errno = ERANGE;	/* Overflow */
-			return -1;
-		} else {
-			/*
-			 * Even if the number of bits in the arc representation
-			 * is higher than the width of supplied * return value
-			 * type, there is still possible to fit it when there
-			 * are few unused high bits in the arc value
-			 * representaion.
-			 * 
-			 * Moreover, there is a possibility that the
-			 * number could actually fit the arc space, given
-			 * that add is negative, but we don't handle
-			 * such "temporary lack of precision" situation here.
-			 * May be considered as a bug.
-			 */
-			uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
-			if((*arcbuf & mask)) {
-				errno = ERANGE;	/* Overflow */
-				return -1;
-			}
-			/* Fool the routine computing unused bits */
-			arclen -= 7;
-			cache = *arcbuf & 0x7f;
-			arcbuf++;
-		}
-	}
-	/* Faster path for common size */
-	if(rvsize == (CHAR_BIT * sizeof(unsigned long))
-        && (arcend-arcbuf) <= (ssize_t)sizeof(unsigned long)) {
-		unsigned long accum;
-		/* Gather all bits into the accumulator */
-		for(accum = cache; arcbuf < arcend; arcbuf++)
-			accum = (accum << 7) | (*arcbuf & ~0x80);
-		if(accum < (unsigned)-add
-		|| accum > ULONG_MAX-(unsigned long)(-add)) {
-			errno = ERANGE;	/* Overflow */
-			return -1;
-		}
-		*(unsigned long *)(void *)rvbuf =
-			accum - (unsigned long)(-add); /* alignment OK! */
-		return 0;
-	}
-
-	if(little_endian()) {	/* Little endian (x86) */
-		/* "Convert" to big endian */
-		rvbuf += rvsize / CHAR_BIT - 1;
-		rvstart--;
-		inc = -1;	/* Descending */
-	} else {
-		inc = +1;	/* Big endian */
-        }
-
-	{
-		int bits;	/* typically no more than 3-4 bits */
-
-		/* Clear the high unused bits */
-		for(bits = rvsize - arclen;
-			bits > CHAR_BIT;
-				rvbuf += inc, bits -= CHAR_BIT)
-				*rvbuf = 0;
-
-		/* Fill the body of a value */
-		for(; arcbuf < arcend; arcbuf++) {
-			cache = (cache << 7) | (*arcbuf & 0x7f);
-			bits += 7;
-			if(bits >= CHAR_BIT) {
-				bits -= CHAR_BIT;
-				*rvbuf = (cache >> bits);
-				rvbuf += inc;
-			}
-		}
-		if(bits) {
-			*rvbuf = cache;
-			rvbuf += inc;
-		}
-	}
-
-	if(add) {
-		for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
-			int v = add + *rvbuf;
-			if(v & ((unsigned)~0 << CHAR_BIT)) {
-				*rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
-				add = -1;
-			} else {
-				*rvbuf = v;
-				break;
-			}
-		}
-		if(rvbuf == rvstart) {
-			/* No space to carry over */
-			errno = ERANGE;	/* Overflow */
-			return -1;
-		}
-	}
-
-	return 0;
+    return rd;
 }
 
 ssize_t
-OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
-		asn_app_consume_bytes_f *cb, void *app_key) {
-	char scratch[64];	/* Conservative estimate */
-	unsigned long accum;	/* Bits accumulator */
-	char *p;		/* Position in the scratch buffer */
+OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, size_t arcbuf_len,
+                                 asn_oid_arc_t *ret_value) {
+    const uint8_t *b = arcbuf;
+    const uint8_t *arcend = arcbuf + arcbuf_len; /* End of arc */
 
-	if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
-			&accum, sizeof(accum)))
-		return -1;
+    if(arcbuf == arcend) {
+        return 0;
+    } else {
+        asn_oid_arc_t accum;
+        /* Gather all bits into the accumulator */
+        for(accum = 0; b < arcend; b++) {
+            accum = (accum << 7) | (*b & ~0x80);
+            if((*b & 0x80) == 0) {
+                if(accum <= ASN_OID_ARC_MAX) {
+                    *ret_value = accum;
+                    return 1 + (b - arcbuf);
+                } else {
+                    errno = ERANGE; /* Overflow */
+                    return -1;
+                }
+            }
+        }
+        errno = EINVAL;
+        return -1;
+    }
 
-	if(accum) {
-		ssize_t len;
-
-		/* Fill the scratch buffer in reverse. */
-		p = scratch + sizeof(scratch);
-		for(; accum; accum /= 10)
-			*(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
-
-		len = sizeof(scratch) - (p - scratch);
-		if(cb(p, len, app_key) < 0)
-			return -1;
-		return len;
-	} else {
-		*scratch = 0x30;
-		if(cb(scratch, 1, app_key) < 0)
-			return -1;
-		return 1;
-	}
-}
-
-int
-OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
-		asn_app_consume_bytes_f *cb, void *app_key) {
-
-	if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
-		return -1;
-
-	return 0;
 }
 
 static ssize_t
-OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
-	ssize_t wrote_len = 0;
-	size_t startn;
-	int add = 0;
-	size_t i;
+OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st,
+                             asn_app_consume_bytes_f *cb, void *app_key) {
+    char scratch[32];
+    asn_oid_arc_t arc0, arc1;
+    size_t produced = 0;
+    size_t off = 0;
+    ssize_t rd;
+    int ret;
 
-	for(i = 0, startn = 0; i < st->size; i++) {
-		uint8_t b = st->buf[i];
-		if((b & 0x80))			/* Continuation expected */
-			continue;
+    rd = OBJECT_IDENTIFIER_get_first_arcs(st->buf, st->size, &arc0, &arc1);
+    if(rd <= 0) {
+        return -1;
+    }
 
-		if(startn == 0) {
-			/*
-			 * First two arcs are encoded through the backdoor.
-			 */
-			if(i) {
-				add = -80;
-				if(cb("2", 1, app_key) < 0) return -1;
-			} else if(b <= 39) {
-				add = 0;
-				if(cb("0", 1, app_key) < 0) return -1;
-			} else if(b < 79) {
-				add = -40;
-				if(cb("1", 1, app_key) < 0) return -1;
-			} else {
-				add = -80;
-				if(cb("2", 1, app_key) < 0) return -1;
-			}
-			wrote_len += 1;
-		}
+    ret = snprintf(scratch, sizeof(scratch), "%"PRIu32".%"PRIu32, arc0, arc1);
+    if(ret >= (ssize_t)sizeof(scratch)) {
+        return -1;
+    }
+    produced += ret;
+    if(cb(scratch, ret, app_key) < 0)
+        return -1;
 
-		if(cb(".", 1, app_key) < 0)	/* Separate arcs */
-			return -1;
+    for(off = rd; ; ) {
+        asn_oid_arc_t arc;
+        rd = OBJECT_IDENTIFIER_get_single_arc(st->buf + off, st->size - off,
+                                              &arc);
+        if(rd < 0) {
+            return -1;
+        } else if(rd == 0) {
+            /* No more arcs. */
+            break;
+        } else {
+            off += rd;
+            assert(off <= st->size);
+            ret = snprintf(scratch, sizeof(scratch), ".%" PRIu32, arc);
+            if(ret >= (ssize_t)sizeof(scratch)) {
+                return -1;
+            }
+            produced += ret;
+            if(cb(scratch, ret, app_key) < 0) return -1;
+        }
+    }
 
-		add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
-				i - startn + 1, add, cb, app_key);
-		if(add < 0) return -1;
-		wrote_len += 1 + add;
-		startn = i + 1;
-		add = 0;
-	}
+    if(off != st->size) {
+        ASN_DEBUG("Could not scan to the end of Object Identifier");
+        return -1;
+    }
 
-	return wrote_len;
+	return produced;
 }
 
 static enum xer_pbd_rval
@@ -298,38 +186,37 @@
 	OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
 	const char *chunk_end = (const char *)chunk_buf + chunk_size;
 	const char *endptr;
-	long s_arcs[10];
-	long *arcs = s_arcs;
-	int arcs_count;
-	int ret;
+	asn_oid_arc_t s_arcs[10];
+	asn_oid_arc_t *arcs = s_arcs;
+	ssize_t num_arcs;
+	ssize_t ret;
 
 	(void)td;
 
-	arcs_count = OBJECT_IDENTIFIER_parse_arcs(
-		(const char *)chunk_buf, chunk_size, arcs,
-			sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
-	if(arcs_count < 0) {
+    num_arcs = OBJECT_IDENTIFIER_parse_arcs(
+        (const char *)chunk_buf, chunk_size, arcs,
+        sizeof(s_arcs) / sizeof(s_arcs[0]), &endptr);
+    if(num_arcs < 0) {
 		/* Expecting more than zero arcs */
 		return XPBD_BROKEN_ENCODING;
-	} else if(arcs_count == 0) {
+	} else if(num_arcs == 0) {
 		return XPBD_NOT_BODY_IGNORE;
 	}
 	assert(endptr == chunk_end);
 
-	if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
-		arcs = (long *)MALLOC(arcs_count * sizeof(long));
+	if((size_t)num_arcs > sizeof(s_arcs)/sizeof(s_arcs[0])) {
+		arcs = (asn_oid_arc_t *)MALLOC(num_arcs * sizeof(asn_oid_arc_t));
 		if(!arcs) return XPBD_SYSTEM_FAILURE;
-		ret = OBJECT_IDENTIFIER_parse_arcs(
-			(const char *)chunk_buf, chunk_size,
-			arcs, arcs_count, &endptr);
-		if(ret != arcs_count)
+        ret = OBJECT_IDENTIFIER_parse_arcs((const char *)chunk_buf, chunk_size,
+                                           arcs, num_arcs, &endptr);
+        if(ret != num_arcs)
 			return XPBD_SYSTEM_FAILURE;	/* assert?.. */
 	}
 
 	/*
 	 * Convert arcs into BER representation.
 	 */
-	ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
+	ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, num_arcs);
 	if(arcs != s_arcs) FREEMEM(arcs);
 
 	return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
@@ -352,16 +239,17 @@
 	const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
 	asn_enc_rval_t er;
 
-	(void)ilevel;
-	(void)flags;
+    (void)ilevel;
+    (void)flags;
 
-	if(!st || !st->buf)
-		ASN__ENCODE_FAILED;
+    if(!st || !st->buf) {
+        ASN__ENCODE_FAILED;
+    }
 
-	er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
-	if(er.encoded < 0) ASN__ENCODE_FAILED;
+    er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
+    if(er.encoded < 0) ASN__ENCODE_FAILED;
 
-	ASN__ENCODED_OK(er);
+    ASN__ENCODED_OK(er);
 }
 
 int
@@ -379,293 +267,201 @@
 	if(cb("{ ", 2, app_key) < 0)
 		return -1;
 
-	if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
-		return -1;
+    if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) {
+        return -1;
+    }
 
-	return (cb(" }", 2, app_key) < 0) ? -1 : 0;
+    return (cb(" }", 2, app_key) < 0) ? -1 : 0;
 }
 
-int
-OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
-		unsigned int arc_type_size, unsigned int arc_slots) {
-	void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
-	int num_arcs = 0;
-	int startn = 0;
-	int add = 0;
-	size_t i;
+ssize_t
+OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *st, asn_oid_arc_t *arcs,
+                           size_t arc_slots) {
+    asn_oid_arc_t arc0, arc1;
+    size_t num_arcs = 0;
+    size_t off;
+    ssize_t rd;
 
-	if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
-		errno = EINVAL;
-		return -1;
-	}
+    if(!st || !st->buf) {
+        errno = EINVAL;
+        return -1;
+    }
 
-	for(i = 0; i < oid->size; i++) {
-		uint8_t b = oid->buf[i];
-		if((b & 0x80))			/* Continuation expected */
-			continue;
+    rd = OBJECT_IDENTIFIER_get_first_arcs(st->buf, st->size, &arc0, &arc1);
+    if(rd <= 0) {
+        return -1;
+    }
+    num_arcs = 2;
+    switch(arc_slots) {
+    default:
+    case 2:
+        arcs[1] = arc1;
+        /* Fall through */
+    case 1:
+        arcs[0] = arc0;
+        /* Fall through */
+    case 0:
+        break;
+    }
 
-		if(num_arcs == 0) {
-			/*
-			 * First two arcs are encoded through the backdoor.
-			 */
-			int first_arc;
-			num_arcs++;
-			if(!arc_slots) { num_arcs++; continue; }
+    for(off = rd; ; ) {
+        asn_oid_arc_t arc;
+        rd = OBJECT_IDENTIFIER_get_single_arc(st->buf + off, st->size - off,
+                                              &arc);
+        if(rd < 0) {
+            return -1;
+        } else if(rd == 0) {
+            /* No more arcs. */
+            break;
+        } else {
+            off += rd;
+            if(num_arcs < arc_slots) {
+                arcs[num_arcs] = arc;
+            }
+            num_arcs++;
+        }
+    }
 
-			if(i) first_arc = 2;
-			else if(b <= 39) first_arc = 0;
-			else if(b < 79)	first_arc = 1;
-			else first_arc = 2;
+    if(off != st->size) {
+        return -1;
+    }
 
-			add = -40 * first_arc;
-			memset(arcs, 0, arc_type_size);
-			*(unsigned char *)((char *)arcs
-				+ (little_endian()?0:(arc_type_size - 1)))
-					= first_arc;
-			arcs = ((char *)arcs) + arc_type_size;
-		}
-
-		/* Decode, if has space */
-		if(arcs < arcs_end) {
-			if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
-				i - startn + 1, add,
-					arcs, arc_type_size)) {
-				return -1;
-                        }
-			startn = i + 1;
-			arcs = ((char *)arcs) + arc_type_size;
-			add = 0;
-		}
-		num_arcs++;
-	}
-
-	return num_arcs;
+    return num_arcs;
 }
 
 
 /*
  * Save the single value as an object identifier arc.
  */
-int
-OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
-	/*
+ssize_t
+OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len,
+                                 asn_oid_arc_t value) {
+    /*
 	 * The following conditions must hold:
-	 * assert(arcval);
-	 * assert(arcval_size > 0);
-	 * assert(arcval_size <= 16);
 	 * assert(arcbuf);
 	 */
-	const uint8_t *tend, *tp;
-	unsigned int cache;
-	uint8_t *bp = arcbuf;
-	int bits;
-	uint8_t buffer[16];
+    uint8_t scratch[((sizeof(value) * CHAR_BIT + 6) / 7)];
+    uint8_t *scratch_end = &scratch[sizeof(scratch)-1];
+    uint8_t *b;
+    size_t result_len;
+    uint8_t mask;
 
-	if(little_endian() && !prepared_order) {
-		const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
-		const uint8_t *aend = (const uint8_t *)arcval;
-		uint8_t *msb = buffer + arcval_size - 1;
-		uint8_t *tb;
-		for(tb = buffer; a >= aend; tb++, a--)
-			if((*tb = *a) && (tb < msb))
-				msb = tb;
-		tend = &buffer[arcval_size];
-		tp = msb;	/* Most significant non-zero byte */
-	} else {
-		/* Look for most significant non-zero byte */
-		tend = (const unsigned char *)arcval + arcval_size;
-		for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
-			if(*tp) break;
-	}
+    for(b = scratch_end, mask = 0; ; mask = 0x80, b--) {
+        *b = mask | (value & 0x7f);
+        value >>= 7;
+        if(!value) {
+            break;
+        }
+    }
 
-	/*
-	 * Split the value in 7-bits chunks.
-	 */
-	bits = ((tend - tp) * CHAR_BIT) % 7;
-	if(bits) {
-		cache = *tp >> (CHAR_BIT - bits);
-		if(cache) {
-			*bp++ = cache | 0x80;
-			cache = *tp++;
-			bits = CHAR_BIT - bits;
-		} else {
-			bits = -bits;
-		}
-	} else {
-		cache = 0;
-	}
-	for(; tp < tend; tp++) {
-		cache = (cache << CHAR_BIT) + *tp;
-		bits += CHAR_BIT;
-		while(bits >= 7) {
-			bits -= 7;
-			*bp++ = 0x80 | (cache >> bits);
-		}
-	}
-	if(bits) *bp++ = cache;
-	bp[-1] &= 0x7f;	/* Clear the last bit */
+    result_len = (scratch_end - b) + 1;
 
-	return bp - arcbuf;
+    if(result_len > arcbuf_len) {
+        return -1;
+    }
+
+    memcpy(arcbuf, b, result_len);
+
+	return result_len;
 }
 
 int
-OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
-	uint8_t *buf;
-	uint8_t *bp;
-	unsigned int arc0;
-	unsigned int arc1;
-	unsigned size;
-	unsigned i;
+OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *st, const asn_oid_arc_t *arcs,
+                           size_t arc_slots) {
+    uint8_t *buf;
+    uint8_t *bp;
+    size_t wrote;
+    asn_oid_arc_t arc0;
+    asn_oid_arc_t arc1;
+    size_t size;
+    size_t i;
 
-	if(!oid || !arcs || arc_type_size < 1
-	|| arc_type_size > 16
-	|| arc_slots < 2) {
-		errno = EINVAL;
+    if(!st || !arcs || arc_slots < 2) {
+        errno = EINVAL;
 		return -1;
 	}
 
-	switch(arc_type_size) {
-	case sizeof(char):
-		arc0 = ((const unsigned char *)arcs)[0];
-		arc1 = ((const unsigned char *)arcs)[1];
-		break;
-	case sizeof(short):
-		arc0 = ((const unsigned short *)arcs)[0];
-		arc1 = ((const unsigned short *)arcs)[1];
-		break;
-	case sizeof(int):
-		arc0 = ((const unsigned int *)arcs)[0];
-		arc1 = ((const unsigned int *)arcs)[1];
-		break;
-	default:
-		arc1 = arc0 = 0;
-		if(little_endian()) {	/* Little endian (x86) */
-			const unsigned char *ps, *pe;
-			/* If more significant bytes are present,
-			 * make them > 255 quick */
-			for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
-					ps < pe; ps++)
-				arc0 |= *ps, arc1 |= *(ps + arc_type_size);
-			arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
-			arc0 = *((const unsigned char *)arcs + 0);
-			arc1 = *((const unsigned char *)arcs + arc_type_size);
-		} else {
-			const unsigned char *ps, *pe;
-			/* If more significant bytes are present,
-			 * make them > 255 quick */
-			for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
-				arc0 |= *ps, arc1 |= *(ps + arc_type_size);
-			arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
-			arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
-		}
-	}
+    arc0 = arcs[0];
+    arc1 = arcs[1];
 
-	/*
-	 * The previous chapter left us with the first and the second arcs.
-	 * The values are not precise (that is, they are valid only if
-	 * they're less than 255), but OK for the purposes of making
-	 * the sanity test below.
-	 */
 	if(arc0 <= 1) {
-		if(arc1 >= 39) {
+		if(arc1 >= 40) {
 			/* 8.19.4: At most 39 subsequent values (including 0) */
 			errno = ERANGE;
 			return -1;
 		}
-	} else if(arc0 > 2) {
-		/* 8.19.4: Only three values are allocated from the root node */
-		errno = ERANGE;
-		return -1;
-	}
-	/*
+    } else if(arc0 == 2) {
+        if(arc1 > ASN_OID_ARC_MAX - 80) {
+            errno = ERANGE;
+            return -1;
+        }
+    } else if(arc0 > 2) {
+        /* 8.19.4: Only three values are allocated from the root node */
+        errno = ERANGE;
+        return -1;
+    }
+
+    /*
 	 * After above tests it is known that the value of arc0 is completely
 	 * trustworthy (0..2). However, the arc1's value is still meaningless.
 	 */
 
-	/*
-	 * Roughly estimate the maximum size necessary to encode these arcs.
-	 * This estimation implicitly takes in account the following facts,
-	 * that cancel each other:
-	 * 	* the first two arcs are encoded in a single value.
-	 * 	* the first value may require more space (+1 byte)
-	 * 	* the value of the first arc which is in range (0..2)
-	 */
-	size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
-	bp = buf = (uint8_t *)MALLOC(size + 1);
-	if(!buf) {
-		/* ENOMEM */
-		return -1;
-	}
+    /*
+     * Roughly estimate the maximum size necessary to encode these arcs.
+     * This estimation implicitly takes in account the following facts,
+     * that cancel each other:
+     * 	* the first two arcs are encoded in a single value.
+     * 	* the first value may require more space (+1 byte)
+     * 	* the value of the first arc which is in range (0..2)
+     */
+    size = ((sizeof(asn_oid_arc_t) * CHAR_BIT + 6) / 7) * arc_slots;
+    bp = buf = (uint8_t *)MALLOC(size + 1);
+    if(!buf) {
+        /* ENOMEM */
+        return -1;
+    }
 
-	/*
-	 * Encode the first two arcs.
-	 * These require special treatment.
-	 */
-	{
-		uint8_t *tp;
-		uint8_t first_value[1 + 16];	/* of two arcs */
-		uint8_t *fv = first_value;
+    wrote = OBJECT_IDENTIFIER_set_single_arc(bp, size, arc0 * 40 + arc1);
+    if(wrote <= 0) {
+        FREEMEM(buf);
+        return -1;
+    }
+    assert(wrote <= size);
+    bp += wrote;
+    size -= wrote;
 
-		/*
-		 * Simulate first_value = arc0 * 40 + arc1;
-		 */
-		/* Copy the second (1'st) arcs[1] into the first_value */
-		*fv++ = 0;
-		arcs = ((const char *)arcs) + arc_type_size;
-		if(little_endian()) {
-			const uint8_t *aend = (const unsigned char *)arcs - 1;
-			const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
-			for(; a1 > aend; fv++, a1--) *fv = *a1;
-		} else {
-			const uint8_t *a1 = (const uint8_t *)arcs;
-			const uint8_t *aend = a1 + arc_type_size;
-			for(; a1 < aend; fv++, a1++) *fv = *a1;
-		}
-		/* Increase the first_value by arc0 */
-		arc0 *= 40;	/* (0..80) */
-		for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
-			unsigned int v = *tp;
-			v += arc0;
-			*tp = v;
-			if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
-			else break;
-		}
+    for(i = 2; i < arc_slots; i++) {
+		wrote = OBJECT_IDENTIFIER_set_single_arc(bp, size, arcs[i]);
+        if(wrote <= 0) {
+            FREEMEM(buf);
+            return -1;
+        }
+        assert(wrote <= size);
+        bp += wrote;
+        size -= wrote;
+        assert(wrote <= size);
+    }
 
-		assert(tp >= first_value);
+    assert(size >= 0);
 
-		bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
-			fv - first_value, 1);
- 	}
-
-	/*
-	 * Save the rest of arcs.
-	 */
-	for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
-		i < arc_slots;
-			i++, arcs = ((const char *)arcs) + arc_type_size) {
-		bp += OBJECT_IDENTIFIER_set_single_arc(bp,
-			arcs, arc_type_size, 0);
-	}
-
-	assert((unsigned)(bp - buf) <= size);
-
-	/*
+    /*
 	 * Replace buffer.
 	 */
-	oid->size = bp - buf;
-	bp = oid->buf;
-	oid->buf = buf;
+	st->size = bp - buf;
+	bp = st->buf;
+	st->buf = buf;
+	st->buf[st->size] = '\0';
 	if(bp) FREEMEM(bp);
 
 	return 0;
 }
 
-
-int
+ssize_t
 OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
-	long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
-	unsigned int arcs_count = 0;
-	const char *oid_end;
+                             asn_oid_arc_t *arcs, size_t arcs_count,
+                             const char **opt_oid_text_end) {
+    size_t num_arcs = 0;
+    const char *oid_end;
 	enum {
 		ST_LEADSPACE,
 		ST_TAILSPACE,
@@ -673,7 +469,7 @@
 		ST_WAITDIGITS 	/* Next character is expected to be a digit */
 	} state = ST_LEADSPACE;
 
-	if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
+	if(!oid_text || oid_txt_length < -1 || (arcs_count && !arcs)) {
 		if(opt_oid_text_end) *opt_oid_text_end = oid_text;
 		errno = EINVAL;
 		return -1;
@@ -682,32 +478,33 @@
 	if(oid_txt_length == -1)
 		oid_txt_length = strlen(oid_text);
 
-#define	_OID_CAPTURE_ARC(oid_text, oid_end)		do {	\
-	const char *endp = oid_end;				\
-	long value;						\
-	switch(asn_strtol_lim(oid_text, &endp, &value)) {	\
-	case ASN_STRTOX_EXTRA_DATA:				\
-	case ASN_STRTOX_OK:					\
-		if(arcs_count < arcs_slots)			\
-			arcs[arcs_count] = value;		\
-		arcs_count++;					\
-		oid_text = endp - 1;				\
-		break;						\
-	case ASN_STRTOX_ERROR_RANGE:				\
-		if(opt_oid_text_end)				\
-			*opt_oid_text_end = oid_text;		\
-		errno = ERANGE;					\
-		return -1;					\
-	case ASN_STRTOX_ERROR_INVAL:				\
-	case ASN_STRTOX_EXPECT_MORE:				\
-		if(opt_oid_text_end)				\
-			*opt_oid_text_end = oid_text;		\
-		errno = EINVAL;					\
-		return -1;					\
-	}							\
-  } while(0)
+#define _OID_CAPTURE_ARC(oid_text, oid_end)                       \
+    do {                                                          \
+        const char *endp = oid_end;                               \
+        unsigned long value;                                      \
+        switch(asn_strtoul_lim(oid_text, &endp, &value)) {        \
+        case ASN_STRTOX_EXTRA_DATA:                               \
+        case ASN_STRTOX_OK:                                       \
+            if(value <= ASN_OID_ARC_MAX) {                        \
+                if(num_arcs < arcs_count) arcs[num_arcs] = value; \
+                num_arcs++;                                       \
+                oid_text = endp - 1;                              \
+                break;                                            \
+            }                                                     \
+            /* Fall through */                                    \
+        case ASN_STRTOX_ERROR_RANGE:                              \
+            if(opt_oid_text_end) *opt_oid_text_end = oid_text;    \
+            errno = ERANGE;                                       \
+            return -1;                                            \
+        case ASN_STRTOX_ERROR_INVAL:                              \
+        case ASN_STRTOX_EXPECT_MORE:                              \
+            if(opt_oid_text_end) *opt_oid_text_end = oid_text;    \
+            errno = EINVAL;                                       \
+            return -1;                                            \
+        }                                                         \
+    } while(0)
 
-	for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
+    for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
 	    switch(*oid_text) {
 	    case 0x09: case 0x0a: case 0x0d: case 0x20:	/* whitespace */
 		switch(state) {
@@ -772,7 +569,7 @@
 		return -1;
 	case ST_AFTERVALUE:
 	case ST_TAILSPACE:
-		return arcs_count;
+		return num_arcs;
 	}
 
 	errno = EINVAL;	/* Broken OID */
@@ -783,9 +580,9 @@
  * Generate values from the list of interesting values, or just a random
  * value up to the upper limit.
  */
-static uint32_t
-OBJECT_IDENTIFIER__biased_random_arc(uint32_t upper_bound) {
-    static const uint16_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
+static asn_oid_arc_t
+OBJECT_IDENTIFIER__biased_random_arc(asn_oid_arc_t upper_bound) {
+    const asn_oid_arc_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
     size_t idx;
 
     switch(asn_random_between(0, 2)) {
@@ -811,7 +608,7 @@
     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
     OBJECT_IDENTIFIER_t *st;
-    uint32_t arcs[5];
+    asn_oid_arc_t arcs[5];
     size_t arcs_len = asn_random_between(2, 5);
     size_t i;
 
@@ -826,13 +623,13 @@
     }
 
     arcs[0] = asn_random_between(0, 2);
-    arcs[1] =
-        OBJECT_IDENTIFIER__biased_random_arc(arcs[0] <= 1 ? 39 : UINT_MAX);
+    arcs[1] = OBJECT_IDENTIFIER__biased_random_arc(
+        arcs[0] <= 1 ? 39 : (ASN_OID_ARC_MAX - 80));
     for(i = 2; i < arcs_len; i++) {
-        arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(UINT_MAX);
+        arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(ASN_OID_ARC_MAX);
     }
 
-    if(OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(arcs[0]), arcs_len)) {
+    if(OBJECT_IDENTIFIER_set_arcs(st, arcs, arcs_len)) {
         if(st != *sptr) {
             ASN_STRUCT_FREE(*td, st);
         }
@@ -841,5 +638,6 @@
 
     *sptr = st;
 
+    result_ok.length = st->size;
     return result_ok;
 }