#include "asn-config.h"
#include "asn-len.h"
#include "asn-tag.h"
#include "asn-int.h"
AsnLen
BEncAsnInt PARAMS ((b, data),
BUF_TYPE b _AND_
AsnInt *data)
{
AsnLen len;
len = BEncAsnIntContent (b, data);
len += BEncDefLen (b, len);
len += BEncTag1 (b, UNIV, PRIM, INTEGER_TAG_CODE);
return len;
}
void
BDecAsnInt PARAMS ((b, result, bytesDecoded, env),
BUF_TYPE b _AND_
AsnInt *result _AND_
AsnLen *bytesDecoded _AND_
jmp_buf env)
{
AsnTag tag;
AsnLen elmtLen;
if ((tag = BDecTag (b, bytesDecoded, env)) != MAKE_TAG_ID (UNIV, PRIM, INTEGER_TAG_CODE))
{
Asn1Error ("BDecAsnInt: ERROR wrong tag on INTEGER.\n");
longjmp (env, -40);
}
elmtLen = BDecLen (b, bytesDecoded, env);
BDecAsnIntContent (b, tag, elmtLen, result, bytesDecoded, env);
}
AsnLen
BEncAsnIntContent PARAMS ((b, data),
BUF_TYPE b _AND_
AsnInt *data)
{
int len;
int i;
unsigned long int mask;
unsigned long int dataCpy;
#define INT_MASK (0x7f80 << ((sizeof(AsnInt) - 2) * 8))
dataCpy = *data;
mask = INT_MASK;
if ((long int)dataCpy < 0)
for (len = sizeof (AsnInt); len > 1; --len)
{
if ((dataCpy & mask) == mask)
mask >>= 8;
else
break;
}
else
for (len = sizeof (AsnInt); len > 1; --len)
{
if ((dataCpy & mask) == 0)
mask >>= 8;
else
break;
}
for (i = 0; i < len; i++)
{
BufPutByteRvs (b, dataCpy);
dataCpy >>= 8;
}
return len;
}
void
BDecAsnIntContent PARAMS ((b, tagId, len, result, bytesDecoded, env),
BUF_TYPE b _AND_
AsnTag tagId _AND_
AsnLen len _AND_
AsnInt *result _AND_
AsnLen *bytesDecoded _AND_
jmp_buf env)
{
int i;
long int retVal;
unsigned long int byte;
if (len > sizeof (AsnInt))
{
Asn1Error ("BDecAsnIntContent: ERROR - integer to big to decode.\n");
longjmp (env, -7);
}
byte = (unsigned long int) BufGetByte (b);
if (byte & 0x80)
retVal = (-1 << 8) | byte;
else
retVal = byte;
for (i = 1; i < len; i++)
retVal = (retVal << 8) | (unsigned long int)(BufGetByte (b));
if (BufReadError (b))
{
Asn1Error ("BDecAsnIntContent: ERROR - decoded past end of data \n");
longjmp (env, -8);
}
(*bytesDecoded) += len;
*result = retVal;
}
void
PrintAsnInt PARAMS ((f, v, indent),
FILE *f _AND_
AsnInt *v _AND_
unsigned short int indent)
{
fprintf (f,"%d", *v);
}
AsnLen
BEncUAsnInt PARAMS ((b, data),
BUF_TYPE b _AND_
UAsnInt *data)
{
AsnLen len;
len = BEncUAsnIntContent (b, data);
len += BEncDefLen (b, len);
len += BEncTag1 (b, UNIV, PRIM, INTEGER_TAG_CODE);
return len;
}
void
BDecUAsnInt PARAMS ((b, result, bytesDecoded, env),
BUF_TYPE b _AND_
UAsnInt *result _AND_
AsnLen *bytesDecoded _AND_
jmp_buf env)
{
AsnTag tag;
AsnLen elmtLen;
if ((tag = BDecTag (b, bytesDecoded, env)) != MAKE_TAG_ID (UNIV, PRIM, INTEGER_TAG_CODE))
{
Asn1Error ("BDecAsnInt: ERROR wrong tag on INTGER.\n");
longjmp (env, -40);
}
elmtLen = BDecLen (b, bytesDecoded, env);
BDecUAsnIntContent (b, tag, elmtLen, result, bytesDecoded, env);
}
AsnLen
BEncUAsnIntContent PARAMS ((b, data),
BUF_TYPE b _AND_
UAsnInt *data)
{
int len;
int i;
unsigned long int mask;
unsigned long int dataCpy;
dataCpy = *data;
mask = INT_MASK;
if ((long int)dataCpy < 0)
{
for (i = 0; i < sizeof (UAsnInt); i++)
{
BufPutByteRvs (b, dataCpy);
dataCpy >>= 8;
}
BufPutByteRvs (b, 0);
return sizeof (UAsnInt)+1;
}
else
{
for (len = sizeof (UAsnInt); len > 1; --len)
{
if ((dataCpy & mask) == 0)
mask >>= 8;
else
break;
}
for (i = 0; i < len; i++)
{
BufPutByteRvs (b, dataCpy);
dataCpy >>= 8;
}
return len;
}
}
void
BDecUAsnIntContent PARAMS ((b, tag, len, result, bytesDecoded, env),
BUF_TYPE b _AND_
AsnTag tag _AND_
AsnLen len _AND_
UAsnInt *result _AND_
AsnLen *bytesDecoded _AND_
jmp_buf env)
{
int i;
unsigned long int retVal;
retVal = (unsigned long int) BufGetByte (b);
if (len > (sizeof (UAsnInt)+1))
{
Asn1Error ("BDecUAsnIntContent: ERROR - integer to big to decode.\n");
longjmp (env, -9);
}
else if (retVal & 0x80)
{
Asn1Error ("BDecUAsnIntContent: ERROR - integer is negative.\n");
longjmp (env, -10);
}
else if ((len == (sizeof (UAsnInt)+1)) && (retVal != 0))
{
Asn1Error ("BDecUAsnIntContent: ERROR - integer is negative.\n");
longjmp (env, -11);
}
for (i = 1; i < len; i++)
retVal = (retVal << 8) | (unsigned long int)(BufGetByte (b));
if (BufReadError (b))
{
Asn1Error ("BDecUIntegerContent: ERROR - decoded past end of data\n");
longjmp (env, -12);
}
(*bytesDecoded) += len;
*result = retVal;
}
void
PrintUAsnInt PARAMS ((f, v, indent),
FILE *f _AND_
UAsnInt *v _AND_
unsigned short int indent)
{
fprintf (f, "%u", *v);
}