Add 64-bit integer support to ASN.1 compiler

ASN.1 INTEGERs will now compile to C int64_t or uint64_t, depending
    on whether the constraint ranges include numbers that cannot be
    represented in 32-bit ints and whether they include negative
    numbers.

    Template backend support included.  check-template is now built with
    --template, so we know we're testing it.

    Tests included.
This commit is contained in:
Nicolas Williams
2011-06-22 17:11:40 -05:00
committed by Nicolas Williams
parent 0e7437ba2e
commit 19d378f44d
20 changed files with 426 additions and 48 deletions

View File

@@ -75,6 +75,38 @@ der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
}
}
int
der_put_unsigned64 (unsigned char *p, size_t len, const uint64_t *v, size_t *size)
{
unsigned char *base = p;
uint64_t val = *v;
if (val) {
while (len > 0 && val) {
*p-- = val % 256;
val /= 256;
--len;
}
if (val != 0)
return ASN1_OVERFLOW;
else {
if(p[1] >= 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0;
}
*size = base - p;
return 0;
}
} else if (len < 1)
return ASN1_OVERFLOW;
else {
*p = 0;
*size = 1;
return 0;
}
}
int
der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
{
@@ -115,6 +147,46 @@ der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
return 0;
}
int
der_put_integer64 (unsigned char *p, size_t len, const int64_t *v, size_t *size)
{
unsigned char *base = p;
int64_t val = *v;
if(val >= 0) {
do {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = val % 256;
len--;
val /= 256;
} while(val);
if(p[1] >= 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0;
len--;
}
} else {
val = ~val;
do {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = ~(val % 256);
len--;
val /= 256;
} while(val);
if(p[1] < 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0xff;
len--;
}
}
*size = base - p;
return 0;
}
int
der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)