-
-int
-OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
- unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
- 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 */
-
- rvsize *= CHAR_BIT; /* bytes to bits */
- arclen *= 7; /* bytes to bits */
-
- /*
- * 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))) {
- unsigned long accum;
- /* Gather all bits into the accumulator */
- for(accum = cache; arcbuf < arcend; arcbuf++)
- accum = (accum << 7) | (*arcbuf & ~0x80);
- if(accum < (unsigned)-add) {
- errno = ERANGE; /* Overflow */
- return -1;
- }
- *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
- return 0;
- }
-
-#ifndef WORDS_BIGENDIAN
- if(*(unsigned char *)&LE) { /* Little endian (x86) */
- /* "Convert" to big endian */
- rvbuf += rvsize / CHAR_BIT - 1;
- rvstart--;
- inc = -1; /* Descending */
- } else
-#endif /* !WORDS_BIGENDIAN */
- inc = +1; /* Big endian is known [at compile time] */
-
- {
- 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 & (-1 << 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;