asn1c/skeletons/INTEGER.c

1024 lines
25 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
* Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
2005-02-25 12:10:27 +00:00
* All rights reserved.
2004-06-03 03:38:44 +00:00
* Redistribution and modifications are permitted subject to BSD license.
*/
2004-09-22 16:06:28 +00:00
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
#include <INTEGER.h>
2004-10-21 11:21:25 +00:00
#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
2004-06-03 03:38:44 +00:00
#include <errno.h>
/*
* INTEGER basic type description.
*/
2004-09-29 13:26:15 +00:00
static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
2004-06-03 03:38:44 +00:00
(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
};
2004-09-29 13:26:15 +00:00
asn_TYPE_descriptor_t asn_DEF_INTEGER = {
2004-10-20 15:50:55 +00:00
"INTEGER",
2004-06-03 03:38:44 +00:00
"INTEGER",
ASN__PRIMITIVE_TYPE_free,
2004-09-22 16:06:28 +00:00
INTEGER_print,
2004-06-03 03:38:44 +00:00
asn_generic_no_constraint,
ber_decode_primitive,
2004-06-03 03:38:44 +00:00
INTEGER_encode_der,
2004-10-21 11:21:25 +00:00
INTEGER_decode_xer,
2004-09-22 16:06:28 +00:00
INTEGER_encode_xer,
2012-09-03 08:45:03 +00:00
#ifdef ASN_DISABLE_PER_SUPPORT
0,
0,
#else
2005-11-26 11:25:14 +00:00
INTEGER_decode_uper, /* Unaligned PER decoder */
2006-08-18 01:34:18 +00:00
INTEGER_encode_uper, /* Unaligned PER encoder */
2012-09-03 08:45:03 +00:00
#endif /* ASN_DISABLE_PER_SUPPORT */
2004-06-03 03:38:44 +00:00
0, /* Use generic outmost tag fetcher */
2004-09-29 13:26:15 +00:00
asn_DEF_INTEGER_tags,
sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
asn_DEF_INTEGER_tags, /* Same as above */
sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
2005-11-26 11:25:14 +00:00
0, /* No PER visible constraints */
2004-08-20 13:23:42 +00:00
0, 0, /* No members */
2004-06-05 08:17:50 +00:00
0 /* No specifics */
2004-06-03 03:38:44 +00:00
};
/*
* Encode INTEGER type using DER.
*/
2004-09-22 16:06:28 +00:00
asn_enc_rval_t
2004-09-29 13:26:15 +00:00
INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
2004-06-03 03:38:44 +00:00
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
INTEGER_t *st = (INTEGER_t *)sptr;
2004-06-03 03:38:44 +00:00
ASN_DEBUG("%s %s as INTEGER (tm=%d)",
cb?"Encoding":"Estimating", td->name, tag_mode);
2004-06-03 03:38:44 +00:00
/*
* 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);
2004-06-03 03:38:44 +00:00
}
static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
2005-02-25 12:10:27 +00:00
2004-06-03 03:38:44 +00:00
/*
* INTEGER specific human-readable output.
*/
2004-09-22 16:06:28 +00:00
static ssize_t
2005-02-25 12:10:27 +00:00
INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
2004-07-19 17:30:25 +00:00
char scratch[32]; /* Enough for 64-bit integer */
2004-06-03 03:38:44 +00:00
uint8_t *buf = st->buf;
uint8_t *buf_end = st->buf + st->size;
2013-03-28 11:38:41 +00:00
signed long value;
2004-09-22 16:06:28 +00:00
ssize_t wrote = 0;
2004-06-03 03:38:44 +00:00
char *p;
int ret;
2013-03-28 11:38:41 +00:00
if(specs && specs->field_unsigned)
ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
else
ret = asn_INTEGER2long(st, &value);
2004-07-19 17:30:25 +00:00
2004-06-03 03:38:44 +00:00
/* Simple case: the integer size is small */
2013-03-28 11:38:41 +00:00
if(ret == 0) {
2005-02-25 12:10:27 +00:00
const asn_INTEGER_enum_map_t *el;
size_t scrsize;
char *scr;
2013-03-28 11:38:41 +00:00
el = (value >= 0 || !specs || !specs->field_unsigned)
? INTEGER_map_value2enum(specs, value) : 0;
2005-02-25 12:10:27 +00:00
if(el) {
scrsize = el->enum_len + 32;
scr = (char *)alloca(scrsize);
if(plainOrXER == 0)
ret = snprintf(scr, scrsize,
2013-03-28 11:38:41 +00:00
"%ld (%s)", value, el->enum_name);
2005-02-25 12:10:27 +00:00
else
ret = snprintf(scr, scrsize,
"<%s/>", el->enum_name);
} else if(plainOrXER && specs && specs->strict_enumeration) {
ASN_DEBUG("ASN.1 forbids dealing with "
"unknown value of ENUMERATED type");
errno = EPERM;
return -1;
} else {
scrsize = sizeof(scratch);
scr = scratch;
ret = snprintf(scr, scrsize,
(specs && specs->field_unsigned)
2013-03-28 11:38:41 +00:00
?"%lu":"%ld", value);
2005-02-25 12:10:27 +00:00
}
assert(ret > 0 && (size_t)ret < scrsize);
return (cb(scr, ret, app_key) < 0) ? -1 : ret;
} else if(plainOrXER && specs && specs->strict_enumeration) {
/*
* Here and earlier, we cannot encode the ENUMERATED values
* if there is no corresponding identifier.
*/
ASN_DEBUG("ASN.1 forbids dealing with "
"unknown value of ENUMERATED type");
errno = EPERM;
return -1;
2004-06-03 03:38:44 +00:00
}
/* Output in the long xx:yy:zz... format */
2004-07-19 17:30:25 +00:00
/* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
2004-06-03 03:38:44 +00:00
for(p = scratch; buf < buf_end; buf++) {
2004-08-11 08:10:13 +00:00
static const char *h2c = "0123456789ABCDEF";
2004-07-19 17:30:25 +00:00
if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
2004-06-03 03:38:44 +00:00
/* Flush buffer */
2004-09-22 16:06:28 +00:00
if(cb(scratch, p - scratch, app_key) < 0)
2004-06-03 03:38:44 +00:00
return -1;
2004-09-22 16:06:28 +00:00
wrote += p - scratch;
2004-06-03 03:38:44 +00:00
p = scratch;
}
*p++ = h2c[*buf >> 4];
*p++ = h2c[*buf & 0x0F];
2004-09-29 13:26:15 +00:00
*p++ = 0x3a; /* ":" */
2004-06-03 03:38:44 +00:00
}
2004-07-19 17:30:25 +00:00
if(p != scratch)
2004-09-29 13:26:15 +00:00
p--; /* Remove the last ":" */
2004-06-03 03:38:44 +00:00
2004-09-22 16:06:28 +00:00
wrote += p - scratch;
return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
}
/*
* INTEGER specific human-readable output.
*/
int
2004-09-29 13:26:15 +00:00
INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
2004-09-22 16:06:28 +00:00
asn_app_consume_bytes_f *cb, void *app_key) {
const INTEGER_t *st = (const INTEGER_t *)sptr;
ssize_t ret;
2004-09-22 16:06:28 +00:00
(void)td;
(void)ilevel;
2005-11-27 13:06:56 +00:00
if(!st || !st->buf)
ret = cb("<absent>", 8, app_key);
else
2005-02-25 12:10:27 +00:00
ret = INTEGER__dump(td, st, cb, app_key, 0);
2004-09-22 16:06:28 +00:00
return (ret < 0) ? -1 : 0;
2004-09-22 16:06:28 +00:00
}
2005-02-25 12:10:27 +00:00
struct e2v_key {
const char *start;
const char *stop;
asn_INTEGER_enum_map_t *vemap;
unsigned int *evmap;
};
static int
INTEGER__compar_enum2value(const void *kp, const void *am) {
const struct e2v_key *key = (const struct e2v_key *)kp;
const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
const char *ptr, *end, *name;
/* Remap the element (sort by different criterion) */
el = key->vemap + key->evmap[el - key->vemap];
/* Compare strings */
for(ptr = key->start, end = key->stop, name = el->enum_name;
ptr < end; ptr++, name++) {
if(*ptr != *name)
return *(const unsigned char *)ptr
- *(const unsigned char *)name;
}
return name[0] ? -1 : 0;
}
static const asn_INTEGER_enum_map_t *
INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
asn_INTEGER_enum_map_t *el_found;
2005-02-25 12:10:27 +00:00
int count = specs ? specs->map_count : 0;
struct e2v_key key;
const char *lp;
if(!count) return NULL;
/* Guaranteed: assert(lstart < lstop); */
/* Figure out the tag name */
for(lstart++, lp = lstart; lp < lstop; lp++) {
switch(*lp) {
case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
case 0x2f: /* '/' */ case 0x3e: /* '>' */
break;
default:
continue;
}
break;
}
if(lp == lstop) return NULL; /* No tag found */
lstop = lp;
key.start = lstart;
key.stop = lstop;
key.vemap = specs->value2enum;
key.evmap = specs->enum2value;
el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
specs->value2enum, count, sizeof(specs->value2enum[0]),
INTEGER__compar_enum2value);
if(el_found) {
/* Remap enum2value into value2enum */
el_found = key.vemap + key.evmap[el_found - key.vemap];
}
return el_found;
2005-02-25 12:10:27 +00:00
}
static int
INTEGER__compar_value2enum(const void *kp, const void *am) {
long a = *(const long *)kp;
const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
long b = el->nat_value;
if(a < b) return -1;
else if(a == b) return 0;
else return 1;
}
const asn_INTEGER_enum_map_t *
INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
2005-02-25 12:10:27 +00:00
int count = specs ? specs->map_count : 0;
if(!count) return 0;
return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
count, sizeof(specs->value2enum[0]),
INTEGER__compar_value2enum);
}
2006-09-15 18:33:25 +00:00
static int
INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
void *p = MALLOC(min_size + 1);
if(p) {
void *b = st->buf;
st->size = 0;
st->buf = p;
FREEMEM(b);
return 0;
} else {
return -1;
}
}
2004-10-21 11:21:25 +00:00
/*
* Decode the chunk of XML text encoding INTEGER.
*/
static enum xer_pbd_rval
INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
2004-10-21 14:02:19 +00:00
INTEGER_t *st = (INTEGER_t *)sptr;
2012-09-03 02:36:47 +00:00
long dec_value;
2013-03-26 01:51:51 +00:00
long hex_value = 0;
2005-02-25 12:10:27 +00:00
const char *lp;
const char *lstart = (const char *)chunk_buf;
const char *lstop = lstart + chunk_size;
2004-10-21 11:21:25 +00:00
enum {
2012-09-03 06:06:35 +00:00
ST_LEADSPACE,
2006-09-15 18:33:25 +00:00
ST_SKIPSPHEX,
2004-10-21 11:21:25 +00:00
ST_WAITDIGITS,
ST_DIGITS,
2012-09-03 06:06:35 +00:00
ST_DIGITS_TRAILSPACE,
2006-09-15 18:33:25 +00:00
ST_HEXDIGIT1,
ST_HEXDIGIT2,
2012-09-04 21:40:57 +00:00
ST_HEXDIGITS_TRAILSPACE,
2006-09-15 18:33:25 +00:00
ST_HEXCOLON,
2012-09-03 06:06:35 +00:00
ST_END_ENUM,
ST_UNEXPECTED
} state = ST_LEADSPACE;
const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
const char *dec_value_end = 0;
2004-10-21 12:11:57 +00:00
2006-09-15 18:33:25 +00:00
if(chunk_size)
2006-10-19 02:44:08 +00:00
ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
(long)chunk_size, *lstart, lstop[-1]);
2006-09-15 18:33:25 +00:00
if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
return XPBD_SYSTEM_FAILURE;
2004-10-21 11:21:25 +00:00
/*
2005-02-25 12:10:27 +00:00
* We may have received a tag here. It will be processed inline.
* Use strtoul()-like code and serialize the result.
2004-10-21 11:21:25 +00:00
*/
2012-09-03 02:36:47 +00:00
for(lp = lstart; lp < lstop; lp++) {
2004-10-21 12:11:57 +00:00
int lv = *lp;
2004-10-21 11:21:25 +00:00
switch(lv) {
case 0x09: case 0x0a: case 0x0d: case 0x20:
2006-09-15 18:33:25 +00:00
switch(state) {
2012-09-03 06:06:35 +00:00
case ST_LEADSPACE:
case ST_DIGITS_TRAILSPACE:
2012-09-04 21:40:57 +00:00
case ST_HEXDIGITS_TRAILSPACE:
2006-09-15 18:33:25 +00:00
case ST_SKIPSPHEX:
continue;
2012-09-03 06:06:35 +00:00
case ST_DIGITS:
dec_value_end = lp;
state = ST_DIGITS_TRAILSPACE;
continue;
2012-09-04 21:40:57 +00:00
case ST_HEXCOLON:
state = ST_HEXDIGITS_TRAILSPACE;
continue;
2006-09-15 18:33:25 +00:00
default:
break;
}
2004-10-21 11:21:25 +00:00
break;
case 0x2d: /* '-' */
2012-09-03 06:06:35 +00:00
if(state == ST_LEADSPACE) {
dec_value = 0;
dec_value_start = lp;
2004-10-21 11:21:25 +00:00
state = ST_WAITDIGITS;
continue;
}
break;
case 0x2b: /* '+' */
2012-09-03 06:06:35 +00:00
if(state == ST_LEADSPACE) {
dec_value = 0;
dec_value_start = lp;
2004-10-21 11:21:25 +00:00
state = ST_WAITDIGITS;
continue;
}
break;
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
2006-09-15 18:33:25 +00:00
switch(state) {
2012-09-03 02:36:47 +00:00
case ST_DIGITS: continue;
2006-09-15 18:33:25 +00:00
case ST_SKIPSPHEX: /* Fall through */
case ST_HEXDIGIT1:
2012-09-03 02:36:47 +00:00
hex_value = (lv - 0x30) << 4;
2006-09-15 18:33:25 +00:00
state = ST_HEXDIGIT2;
continue;
case ST_HEXDIGIT2:
2012-09-03 02:36:47 +00:00
hex_value += (lv - 0x30);
2006-09-15 18:33:25 +00:00
state = ST_HEXCOLON;
2012-09-03 02:36:47 +00:00
st->buf[st->size++] = (uint8_t)hex_value;
2006-09-15 18:33:25 +00:00
continue;
case ST_HEXCOLON:
return XPBD_BROKEN_ENCODING;
2012-09-03 06:06:35 +00:00
case ST_LEADSPACE:
dec_value = 0;
dec_value_start = lp;
/* FALL THROUGH */
case ST_WAITDIGITS:
2006-09-15 18:33:25 +00:00
state = ST_DIGITS;
2012-09-03 02:36:47 +00:00
continue;
2012-09-03 06:06:35 +00:00
default:
break;
2004-10-21 11:21:25 +00:00
}
2012-09-03 06:06:35 +00:00
break;
case 0x3c: /* '<', start of XML encoded enumeration */
if(state == ST_LEADSPACE) {
2005-02-25 12:10:27 +00:00
const asn_INTEGER_enum_map_t *el;
el = INTEGER_map_enum2value(
2005-02-25 12:10:27 +00:00
(asn_INTEGER_specifics_t *)
td->specifics, lstart, lstop);
if(el) {
ASN_DEBUG("Found \"%s\" => %ld",
el->enum_name, el->nat_value);
2012-09-03 06:06:35 +00:00
dec_value = el->nat_value;
state = ST_END_ENUM;
lp = lstop - 1;
continue;
2005-02-25 12:10:27 +00:00
}
ASN_DEBUG("Unknown identifier for INTEGER");
}
return XPBD_BROKEN_ENCODING;
2006-09-15 18:33:25 +00:00
case 0x3a: /* ':' */
if(state == ST_HEXCOLON) {
/* This colon is expected */
state = ST_HEXDIGIT1;
continue;
} else if(state == ST_DIGITS) {
/* The colon here means that we have
* decoded the first two hexadecimal
* places as a decimal value.
* Switch decoding mode. */
ASN_DEBUG("INTEGER re-evaluate as hex form");
state = ST_SKIPSPHEX;
2012-09-03 06:06:35 +00:00
dec_value_start = 0;
2006-09-15 18:33:25 +00:00
lp = lstart - 1;
continue;
} else {
2011-07-20 21:17:37 +00:00
ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
2006-09-15 18:33:25 +00:00
break;
}
/* [A-Fa-f] */
case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
switch(state) {
case ST_SKIPSPHEX:
2012-09-03 06:06:35 +00:00
case ST_LEADSPACE: /* Fall through */
2006-09-15 18:33:25 +00:00
case ST_HEXDIGIT1:
2012-09-03 06:06:35 +00:00
hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
hex_value += 10;
hex_value <<= 4;
2006-09-15 18:33:25 +00:00
state = ST_HEXDIGIT2;
continue;
case ST_HEXDIGIT2:
2012-09-03 06:06:35 +00:00
hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
hex_value += 10;
st->buf[st->size++] = (uint8_t)hex_value;
2006-09-15 18:33:25 +00:00
state = ST_HEXCOLON;
continue;
case ST_DIGITS:
ASN_DEBUG("INTEGER re-evaluate as hex form");
state = ST_SKIPSPHEX;
2012-09-03 06:06:35 +00:00
dec_value_start = 0;
2006-09-15 18:33:25 +00:00
lp = lstart - 1;
continue;
default:
break;
}
break;
2004-10-21 11:21:25 +00:00
}
/* Found extra non-numeric stuff */
2012-09-03 06:06:35 +00:00
ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
2011-07-20 21:17:37 +00:00
lv, (long)(lp - lstart));
2012-09-03 06:06:35 +00:00
state = ST_UNEXPECTED;
2004-10-21 12:11:57 +00:00
break;
2004-10-21 11:21:25 +00:00
}
2006-09-15 18:33:25 +00:00
switch(state) {
2012-09-03 06:06:35 +00:00
case ST_END_ENUM:
/* Got a complete and valid enumeration encoded as a tag. */
break;
2006-09-15 18:33:25 +00:00
case ST_DIGITS:
2012-09-03 06:06:35 +00:00
dec_value_end = lstop;
/* FALL THROUGH */
2012-09-03 06:06:35 +00:00
case ST_DIGITS_TRAILSPACE:
/* The last symbol encountered was a digit. */
switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
2012-09-03 06:06:35 +00:00
case ASN_STRTOL_OK:
break;
case ASN_STRTOL_ERROR_RANGE:
return XPBD_DECODER_LIMIT;
case ASN_STRTOL_ERROR_INVAL:
case ASN_STRTOL_EXPECT_MORE:
case ASN_STRTOL_EXTRA_DATA:
2012-09-03 06:06:35 +00:00
return XPBD_BROKEN_ENCODING;
}
2006-09-15 18:33:25 +00:00
break;
case ST_HEXCOLON:
2012-09-04 21:40:57 +00:00
case ST_HEXDIGITS_TRAILSPACE:
2006-09-15 18:33:25 +00:00
st->buf[st->size] = 0; /* Just in case termination */
return XPBD_BODY_CONSUMED;
case ST_HEXDIGIT1:
case ST_HEXDIGIT2:
case ST_SKIPSPHEX:
return XPBD_BROKEN_ENCODING;
2012-09-04 20:14:29 +00:00
case ST_LEADSPACE:
/* Content not found */
return XPBD_NOT_BODY_IGNORE;
case ST_WAITDIGITS:
case ST_UNEXPECTED:
2012-09-03 06:06:35 +00:00
ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
return XPBD_BROKEN_ENCODING; /* No digits */
}
2004-10-21 11:21:25 +00:00
2012-09-03 06:06:35 +00:00
/*
* Convert the result of parsing of enumeration or a straight
* decimal value into a BER representation.
*/
if(asn_long2INTEGER(st, dec_value))
return XPBD_SYSTEM_FAILURE;
2004-10-21 11:21:25 +00:00
return XPBD_BODY_CONSUMED;
2004-10-21 11:21:25 +00:00
}
asn_dec_rval_t
INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
2005-03-10 18:52:02 +00:00
const void *buf_ptr, size_t size) {
2004-10-21 11:21:25 +00:00
return xer_decode_primitive(opt_codec_ctx, td,
2004-10-21 14:02:19 +00:00
sptr, sizeof(INTEGER_t), opt_mname,
2004-10-21 11:21:25 +00:00
buf_ptr, size, INTEGER__xer_body_decode);
}
2004-09-22 16:06:28 +00:00
asn_enc_rval_t
2004-09-29 13:26:15 +00:00
INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
2004-09-22 16:06:28 +00:00
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;
2005-11-27 13:06:56 +00:00
if(!st || !st->buf)
2004-09-22 16:06:28 +00:00
_ASN_ENCODE_FAILED;
2005-02-25 12:10:27 +00:00
er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
2004-09-22 16:06:28 +00:00
if(er.encoded < 0) _ASN_ENCODE_FAILED;
2005-11-26 11:25:14 +00:00
_ASN_ENCODED_OK(er);
}
2012-09-03 08:45:03 +00:00
#ifndef ASN_DISABLE_PER_SUPPORT
2005-11-26 11:25:14 +00:00
asn_dec_rval_t
INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
2005-11-26 11:25:14 +00:00
asn_dec_rval_t rval = { RC_OK, 0 };
INTEGER_t *st = (INTEGER_t *)*sptr;
asn_per_constraint_t *ct;
int repeat;
(void)opt_codec_ctx;
if(!st) {
st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
if(!st) _ASN_DECODE_FAILED;
}
if(!constraints) constraints = td->per_constraints;
ct = constraints ? &constraints->value : 0;
if(ct && ct->flags & APC_EXTENSIBLE) {
int inext = per_get_few_bits(pd, 1);
2006-09-18 20:05:55 +00:00
if(inext < 0) _ASN_DECODE_STARVED;
2005-11-26 11:25:14 +00:00
if(inext) ct = 0;
}
FREEMEM(st->buf);
2007-11-10 02:05:23 +00:00
st->buf = 0;
st->size = 0;
2005-11-26 11:25:14 +00:00
if(ct) {
if(ct->flags & APC_SEMI_CONSTRAINED) {
st->buf = (uint8_t *)CALLOC(1, 2);
if(!st->buf) _ASN_DECODE_FAILED;
st->size = 1;
} else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
size_t size = (ct->range_bits + 7) >> 3;
st->buf = (uint8_t *)MALLOC(1 + size + 1);
if(!st->buf) _ASN_DECODE_FAILED;
st->size = size;
}
}
/* X.691-2008/11, #13.2.2, constrained whole number */
2005-11-26 11:25:14 +00:00
if(ct && ct->flags != APC_UNCONSTRAINED) {
/* #11.5.6 */
2005-11-26 11:25:14 +00:00
ASN_DEBUG("Integer with range %d bits", ct->range_bits);
if(ct->range_bits >= 0) {
if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
_ASN_DECODE_FAILED;
if(specs && specs->field_unsigned) {
unsigned long uvalue;
if(uper_get_constrained_whole_number(pd,
&uvalue, ct->range_bits))
_ASN_DECODE_STARVED;
ASN_DEBUG("Got value %lu + low %ld",
uvalue, ct->lower_bound);
uvalue += ct->lower_bound;
if(asn_ulong2INTEGER(st, uvalue))
_ASN_DECODE_FAILED;
2007-11-06 02:35:13 +00:00
} else {
unsigned long svalue;
if(uper_get_constrained_whole_number(pd,
&svalue, ct->range_bits))
_ASN_DECODE_STARVED;
ASN_DEBUG("Got value %ld + low %ld",
svalue, ct->lower_bound);
svalue += ct->lower_bound;
if(asn_long2INTEGER(st, svalue))
_ASN_DECODE_FAILED;
2007-11-06 02:35:13 +00:00
}
2005-11-26 11:25:14 +00:00
return rval;
}
} else {
ASN_DEBUG("Decoding unconstrained integer %s", td->name);
}
/* X.691, #12.2.3, #12.2.4 */
do {
ssize_t len;
void *p;
int ret;
/* Get the PER length */
len = uper_get_length(pd, -1, &repeat);
2006-09-18 20:05:55 +00:00
if(len < 0) _ASN_DECODE_STARVED;
2005-11-26 11:25:14 +00:00
p = REALLOC(st->buf, st->size + len + 1);
if(!p) _ASN_DECODE_FAILED;
st->buf = (uint8_t *)p;
ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
2006-09-18 20:05:55 +00:00
if(ret < 0) _ASN_DECODE_STARVED;
2005-11-26 11:25:14 +00:00
st->size += len;
} while(repeat);
st->buf[st->size] = 0; /* JIC */
/* #12.2.3 */
if(ct && ct->lower_bound) {
/*
* TODO: replace by in-place arithmetics.
*/
long value;
if(asn_INTEGER2long(st, &value))
_ASN_DECODE_FAILED;
if(asn_long2INTEGER(st, value + ct->lower_bound))
_ASN_DECODE_FAILED;
}
return rval;
2004-06-03 03:38:44 +00:00
}
2006-08-18 01:34:18 +00:00
asn_enc_rval_t
INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
2006-08-18 01:34:18 +00:00
asn_enc_rval_t er;
INTEGER_t *st = (INTEGER_t *)sptr;
const uint8_t *buf;
const uint8_t *end;
asn_per_constraint_t *ct;
long value = 0;
if(!st || st->size == 0) _ASN_ENCODE_FAILED;
if(!constraints) constraints = td->per_constraints;
ct = constraints ? &constraints->value : 0;
er.encoded = 0;
if(ct) {
int inext = 0;
if(specs && specs->field_unsigned) {
unsigned long uval;
if(asn_INTEGER2ulong(st, &uval))
_ASN_ENCODE_FAILED;
/* Check proper range */
if(ct->flags & APC_SEMI_CONSTRAINED) {
if(uval < (unsigned long)ct->lower_bound)
inext = 1;
} else if(ct->range_bits >= 0) {
if(uval < (unsigned long)ct->lower_bound
|| uval > (unsigned long)ct->upper_bound)
inext = 1;
}
ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
uval, st->buf[0], st->size,
ct->lower_bound, ct->upper_bound,
inext ? "ext" : "fix");
value = uval;
} else {
if(asn_INTEGER2long(st, &value))
_ASN_ENCODE_FAILED;
/* Check proper range */
if(ct->flags & APC_SEMI_CONSTRAINED) {
if(value < ct->lower_bound)
inext = 1;
} else if(ct->range_bits >= 0) {
if(value < ct->lower_bound
|| value > ct->upper_bound)
inext = 1;
}
ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
value, st->buf[0], st->size,
ct->lower_bound, ct->upper_bound,
inext ? "ext" : "fix");
2006-08-18 01:34:18 +00:00
}
if(ct->flags & APC_EXTENSIBLE) {
if(per_put_few_bits(po, inext, 1))
_ASN_ENCODE_FAILED;
if(inext) ct = 0;
} else if(inext) {
_ASN_ENCODE_FAILED;
}
}
/* X.691-11/2008, #13.2.2, test if constrained whole number */
2006-08-18 01:34:18 +00:00
if(ct && ct->range_bits >= 0) {
/* #11.5.6 -> #11.3 */
2014-02-10 17:24:39 +00:00
ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
value, value - ct->lower_bound, ct->range_bits);
unsigned long v = value - ct->lower_bound;
if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
_ASN_ENCODE_FAILED;
2006-08-18 01:34:18 +00:00
_ASN_ENCODED_OK(er);
}
if(ct && ct->lower_bound) {
ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
/* TODO: adjust lower bound */
_ASN_ENCODE_FAILED;
}
for(buf = st->buf, end = st->buf + st->size; buf < end;) {
ssize_t mayEncode = uper_put_length(po, end - buf);
if(mayEncode < 0)
_ASN_ENCODE_FAILED;
if(per_put_many_bits(po, buf, 8 * mayEncode))
_ASN_ENCODE_FAILED;
2006-09-08 01:56:32 +00:00
buf += mayEncode;
2006-08-18 01:34:18 +00:00
}
_ASN_ENCODED_OK(er);
}
2012-09-03 08:45:03 +00:00
#endif /* ASN_DISABLE_PER_SUPPORT */
2004-06-03 03:38:44 +00:00
int
2004-09-29 13:26:15 +00:00
asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
2004-06-03 03:38:44 +00:00
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;
}
2004-10-21 11:21:25 +00:00
int
asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
uint8_t *b, *end;
unsigned long l;