Skip to content
Snippets Groups Projects
INTEGER.c 6.05 KiB
Newer Older
Lev Walkin's avatar
Lev Walkin committed
/*-
 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
 * Redistribution and modifications are permitted subject to BSD license.
 */
Lev Walkin's avatar
Lev Walkin committed
#include <asn_internal.h>
Lev Walkin's avatar
Lev Walkin committed
#include <INTEGER.h>
#include <ber_codec_prim.h>	/* Encoder and decoder of a primitive */
Lev Walkin's avatar
Lev Walkin committed
#include <assert.h>
#include <errno.h>

/*
 * INTEGER basic type description.
 */
static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
};
asn1_TYPE_descriptor_t asn1_DEF_INTEGER = {
	"INTEGER",
	ASN__PRIMITIVE_TYPE_free,
Lev Walkin's avatar
Lev Walkin committed
	INTEGER_print,
Lev Walkin's avatar
Lev Walkin committed
	asn_generic_no_constraint,
	ber_decode_primitive,
Lev Walkin's avatar
Lev Walkin committed
	INTEGER_encode_der,
Lev Walkin's avatar
Lev Walkin committed
	0,				/* Not implemented yet */
	INTEGER_encode_xer,
Lev Walkin's avatar
Lev Walkin committed
	0, /* Use generic outmost tag fetcher */
	asn1_DEF_INTEGER_tags,
	sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
	asn1_DEF_INTEGER_tags,	/* Same as above */
	sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
Lev Walkin's avatar
Lev Walkin committed
	0, 0,	/* No members */
	0	/* No specifics */
Lev Walkin's avatar
Lev Walkin committed
};

/*
 * Encode INTEGER type using DER.
 */
Lev Walkin's avatar
Lev Walkin committed
asn_enc_rval_t
INTEGER_encode_der(asn1_TYPE_descriptor_t *td, void *sptr,
Lev Walkin's avatar
Lev Walkin committed
	int tag_mode, ber_tlv_tag_t tag,
	asn_app_consume_bytes_f *cb, void *app_key) {
	INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin's avatar
Lev Walkin committed

	ASN_DEBUG("%s %s as INTEGER (tm=%d)",
		cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkin's avatar
Lev Walkin committed

	/*
	 * Canonicalize integer in the buffer.
	 * (Remove too long sign extension, remove some first 0x00 bytes)
	 */
	if(st->buf) {
		uint8_t *buf = st->buf;
		uint8_t *end1 = buf + st->size - 1;
		int shift;

		/* Compute the number of superfluous leading bytes */
		for(; buf < end1; buf++) {
			/*
			 * If the contents octets of an integer value encoding
			 * consist of more than one octet, then the bits of the
			 * first octet and bit 8 of the second octet:
			 * a) shall not all be ones; and
			 * b) shall not all be zero.
			 */
			switch(*buf) {
			case 0x00: if((buf[1] & 0x80) == 0)
					continue;
				break;
			case 0xff: if((buf[1] & 0x80))
					continue;
				break;
			}
			break;
		}

		/* Remove leading superfluous bytes from the integer */
		shift = buf - st->buf;
		if(shift) {
			uint8_t *nb = st->buf;
			uint8_t *end;

			st->size -= shift;	/* New size, minus bad bytes */
			end = nb + st->size;

			for(; nb < end; nb++, buf++)
				*nb = *buf;
		}

	} /* if(1) */

	return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkin's avatar
Lev Walkin committed
}

/*
 * INTEGER specific human-readable output.
 */
Lev Walkin's avatar
Lev Walkin committed
static ssize_t
INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
	char scratch[32];	/* Enough for 64-bit integer */
Lev Walkin's avatar
Lev Walkin committed
	uint8_t *buf = st->buf;
	uint8_t *buf_end = st->buf + st->size;
	signed long accum;
Lev Walkin's avatar
Lev Walkin committed
	ssize_t wrote = 0;
Lev Walkin's avatar
Lev Walkin committed
	char *p;
	int ret;

Lev Walkin's avatar
Lev Walkin committed
	if(st->size == 0) {
		return (cb("0", 1, app_key) < 0) ? -1 : 1;
	}
Lev Walkin's avatar
Lev Walkin committed

Lev Walkin's avatar
Lev Walkin committed
	/*
	 * Advance buf pointer until the start of the value's body.
	 * This will make us able to process large integers using simple case,
	 * when the actual value is small
	 * (0x0000000000abcdef would yield a fine 0x00abcdef)
	 */
	/* Skip the insignificant leading bytes */
	for(; buf < buf_end-1; buf++) {
		switch(*buf) {
		case 0x00: if((buf[1] & 0x80) == 0) continue; break;
		case 0xff: if((buf[1] & 0x80) != 0) continue; break;
		}
		break;
	}

Lev Walkin's avatar
Lev Walkin committed
	/* Simple case: the integer size is small */
Lev Walkin's avatar
Lev Walkin committed
	if((size_t)(buf_end - buf) <= sizeof(accum)) {
		accum = (*buf & 0x80) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
		for(; buf < buf_end; buf++)
			accum = (accum << 8) | *buf;
		ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
		assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkin's avatar
Lev Walkin committed
		return (cb(scratch, ret, app_key) < 0) ? -1 : ret;
Lev Walkin's avatar
Lev Walkin committed
	}

	/* Output in the long xx:yy:zz... format */
Lev Walkin's avatar
Lev Walkin committed
	/* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkin's avatar
Lev Walkin committed
	for(p = scratch; buf < buf_end; buf++) {
Lev Walkin's avatar
Lev Walkin committed
		static const char *h2c = "0123456789ABCDEF";
Lev Walkin's avatar
Lev Walkin committed
		if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkin's avatar
Lev Walkin committed
			/* Flush buffer */
Lev Walkin's avatar
Lev Walkin committed
			if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkin's avatar
Lev Walkin committed
				return -1;
Lev Walkin's avatar
Lev Walkin committed
			wrote += p - scratch;
Lev Walkin's avatar
Lev Walkin committed
			p = scratch;
		}
		*p++ = h2c[*buf >> 4];
		*p++ = h2c[*buf & 0x0F];
		*p++ = ':';
	}
Lev Walkin's avatar
Lev Walkin committed
	if(p != scratch)
		p--;	/* Remove the last ':' */
Lev Walkin's avatar
Lev Walkin committed

Lev Walkin's avatar
Lev Walkin committed
	wrote += p - scratch;
	return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
}

/*
 * INTEGER specific human-readable output.
 */
int
INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
	asn_app_consume_bytes_f *cb, void *app_key) {
	const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin's avatar
Lev Walkin committed

	(void)td;
	(void)ilevel;

	if(!st && !st->buf)
		ret = cb("<absent>", 8, app_key);
	else
		ret = INTEGER__dump(st, cb, app_key);
Lev Walkin's avatar
Lev Walkin committed

	return (ret < 0) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
}

asn_enc_rval_t
INTEGER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
	int ilevel, enum xer_encoder_flags_e flags,
		asn_app_consume_bytes_f *cb, void *app_key) {
	const INTEGER_t *st = (const INTEGER_t *)sptr;
	asn_enc_rval_t er;

	(void)ilevel;
	(void)flags;
	
	if(!st && !st->buf)
		_ASN_ENCODE_FAILED;

	er.encoded = INTEGER__dump(st, cb, app_key);
	if(er.encoded < 0) _ASN_ENCODE_FAILED;

	return er;
Lev Walkin's avatar
Lev Walkin committed
}

int
asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
	uint8_t *b, *end;
	size_t size;
	long l;

	/* Sanity checking */
	if(!iptr || !iptr->buf || !lptr) {
		errno = EINVAL;
		return -1;
	}

	/* Cache the begin/end of the buffer */
	b = iptr->buf;	/* Start of the INTEGER buffer */
	size = iptr->size;
	end = b + size;	/* Where to stop */

	if(size > sizeof(long)) {
		uint8_t *end1 = end - 1;
		/*
		 * Slightly more advanced processing,
		 * able to >sizeof(long) bytes,
		 * when the actual value is small
		 * (0x0000000000abcdef would yield a fine 0x00abcdef)
		 */
		/* Skip out the insignificant leading bytes */
		for(; b < end1; b++) {
			switch(*b) {
			case 0x00: if((b[1] & 0x80) == 0) continue; break;
			case 0xff: if((b[1] & 0x80) != 0) continue; break;
			}
			break;
		}

		size = end - b;
		if(size > sizeof(long)) {
			/* Still cannot fit the long */
			errno = ERANGE;
			return -1;
		}
	}

	/* Shortcut processing of a corner case */
	if(end == b) {
		*lptr = 0;
		return 0;
	}

	/* Perform the sign initialization */
	/* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
	if((*b >> 7)) l = -1; else l = 0;

	/* Conversion engine */
	for(; b < end; b++)
		l = (l << 8) | *b;

	*lptr = l;
	return 0;
}