#include "asn-config.h"
#include "asn-len.h"
#include "asn-tag.h"
#include "asn-octs.h"
#include "asn-oid.h"
AsnLen
BEncAsnOid PARAMS ((b, data),
BUF_TYPE b _AND_
AsnOid *data)
{
AsnLen len;
len = BEncAsnOidContent (b, data);
len += BEncDefLen (b, len);
len += BEncTag1 (b, UNIV, PRIM, OID_TAG_CODE);
return len;
}
void
BDecAsnOid PARAMS ((b, result, bytesDecoded, env),
BUF_TYPE b _AND_
AsnOid *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 ("BDecAsnOid: ERROR - wrong tag on OBJECT IDENTIFIER.\n");
longjmp (env, -40);
}
elmtLen = BDecLen (b, bytesDecoded, env);
BDecAsnOidContent (b, tag, elmtLen, result, bytesDecoded, env);
}
void
BDecAsnOidContent PARAMS ((b, tagId, len, result, bytesDecoded, env),
BUF_TYPE b _AND_
AsnTag tagId _AND_
AsnLen len _AND_
AsnOid *result _AND_
AsnLen *bytesDecoded _AND_
jmp_buf env)
{
result->octetLen = len;
result->octs = Asn1Alloc (len);
BufCopy (result->octs, b, len);
if (BufReadError (b))
{
Asn1Error ("BDecAsnOidContent: ERROR - decoded past end of data\n");
longjmp (env, -21);
}
(*bytesDecoded) += len;
}
void
PrintAsnOid PARAMS ((f,v, indent),
FILE *f _AND_
AsnOid *v _AND_
unsigned short int indent)
{
unsigned short int firstArcNum;
unsigned long int arcNum;
int i;
fprintf (f,"{");
for (arcNum = 0, i=0; (i < v->octetLen) && (v->octs[i] & 0x80);i++)
arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
i++;
firstArcNum = arcNum/40;
if (firstArcNum > 2)
firstArcNum = 2;
fprintf (f,"%u %u", firstArcNum, arcNum - (firstArcNum * 40));
for (; i < v->octetLen; )
{
for (arcNum = 0; (i < v->octetLen) && (v->octs[i] & 0x80);i++)
arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
i++;
fprintf (f," %u", arcNum);
}
fprintf (f,"}");
}
AsnLen
EncodedOidLen PARAMS ((oid),
OID *oid)
{
AsnLen totalLen;
unsigned long headArcNum;
unsigned long tmpArcNum;
OID *tmpOid;
if (oid->next == NULL)
return 0;
headArcNum = (oid->arcNum * 40) + oid->next->arcNum;
tmpArcNum = headArcNum;
for (totalLen = 1; (tmpArcNum >>= 7) != 0; totalLen++)
;
for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
{
totalLen++;
tmpArcNum = tmpOid->arcNum;
for (; (tmpArcNum >>= 7) != 0; totalLen++)
;
}
return totalLen;
}
void
BuildEncodedOid PARAMS ((oid, result),
OID *oid _AND_
AsnOid *result)
{
unsigned long len;
unsigned long headArcNum;
unsigned long tmpArcNum;
char *buf;
int i;
OID *tmpOid;
buf = result->octs;
if (oid->next == NULL)
return;
headArcNum = (oid->arcNum * 40) + oid->next->arcNum;
tmpArcNum = headArcNum;
for (len = 0; (tmpArcNum >>= 7) != 0; len++)
;
for (i=0; i < len; i++)
*(buf++) = 0x80 | (headArcNum >> ((len-i)*7));
*(buf++) = 0x7f & headArcNum;
for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
{
tmpArcNum = tmpOid->arcNum;
for (len = 0; (tmpArcNum >>= 7) != 0; len++)
;
for (i=0; i < len; i++)
*(buf++) = 0x80 | (tmpOid->arcNum >> ((len-i)*7));
*(buf++) = 0x7f & tmpOid->arcNum;
}
}
void
UnbuildEncodedOid PARAMS ((eoid, result),
AsnOid *eoid _AND_
OID **result)
{
OID **nextOid;
OID *headOid;
int arcNum;
int i;
int firstArcNum;
int secondArcNum;
for (arcNum = 0, i=0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80);i++)
arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
i++;
firstArcNum = arcNum / 40;
if (firstArcNum > 2)
firstArcNum = 2;
secondArcNum = arcNum - (firstArcNum * 40);
headOid = (OID*)malloc (sizeof (OID));
headOid->arcNum = firstArcNum;
headOid->next = (OID*)malloc (sizeof (OID));
headOid->next->arcNum = secondArcNum;
nextOid = &headOid->next->next;
for (; i < eoid->octetLen; )
{
for (arcNum = 0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80); i++)
arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
i++;
*nextOid = (OID*)malloc (sizeof (OID));
(*nextOid)->arcNum = arcNum;
nextOid = &(*nextOid)->next;
}
*result = headOid;
}