#ifdef TTBL
#include <stdio.h>
#include "tbl-incl.h"
int TblEncodeTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
AsnLen TblEncTag PROTO ((BUF_TYPE b, TBLTag *tag));
int
TblEncode PARAMS ((tbl, modName, typeName, b, v, bytesEncoded),
TBL *tbl _AND_
char *modName _AND_
char *typeName _AND_
BUF_TYPE b _AND_
AVal *v _AND_
unsigned long int *bytesEncoded)
{
TBLModule *tblMod;
TBLTypeDef *tblTd;
int retVal;
tblTd = TblFindTypeDef (tbl, modName, typeName, &tblMod);
if (tblTd == NULL)
{
TblError ("TblEncode: Could not find a type definition with the given module and name");
return -1;
}
*bytesEncoded = 0;
retVal = TblEncodeType (tblTd->type, b, v, FALSE, bytesEncoded);
if (BufWriteError (b))
retVal = -1;
return retVal;
}
int
TblEncodeType PARAMS ((tblT, b, v, implicit, bytesEncoded),
TBLType *tblT _AND_
BUF_TYPE b _AND_
AVal *v _AND_
int implicit _AND_
unsigned long int *bytesEncoded)
{
AVal *elmtV;
AsnList *lVal;
int retVal = 0;
unsigned long int tmpBytesEncoded = 0;
unsigned int currElmt;
TBLType *listElmtType;
TBLType *structElmtType;
TBLType *choiceElmtType;
AChoiceVal *cVal;
AStructVal *sVal;
int implicitRef;
void *tmp;
switch (tblT->typeId)
{
case TBL_TYPEREF:
implicitRef = tblT->content->a.typeRef->implicit ||
(implicit &&
((tblT->tagList == NULL) || LIST_EMPTY (tblT->tagList)));
retVal = TblEncodeType (tblT->content->a.typeRef->typeDefPtr->type, b, v, implicitRef, &tmpBytesEncoded);
break;
case TBL_SEQUENCE:
case TBL_SET:
currElmt = LIST_COUNT (tblT->content->a.elmts)-1;
sVal = (AStructVal*)v;
tmp = CURR_LIST_NODE (tblT->content->a.elmts);
FOR_EACH_LIST_ELMT_RVS (structElmtType, tblT->content->a.elmts)
{
elmtV = sVal[currElmt--];
if (!(structElmtType->optional && (elmtV == NULL)))
{
retVal = TblEncodeType (structElmtType, b, elmtV, FALSE, &tmpBytesEncoded);
if (retVal < 0)
break;
}
}
SET_CURR_LIST_NODE (tblT->content->a.elmts, tmp);
break;
case TBL_SEQUENCEOF:
case TBL_SETOF:
lVal = (AsnList*)v;
listElmtType = FIRST_LIST_ELMT (tblT->content->a.elmts);
FOR_EACH_LIST_ELMT_RVS (elmtV, lVal)
{
retVal = TblEncodeType (listElmtType, b, elmtV, FALSE, &tmpBytesEncoded);
if (retVal < 0)
break;
}
break;
case TBL_CHOICE:
cVal = (AChoiceVal*) v;
choiceElmtType = (TBLType*)GetAsnListElmt (tblT->content->a.elmts, cVal->choiceId);
retVal = TblEncodeType (choiceElmtType, b, cVal->val, FALSE, &tmpBytesEncoded);
break;
case TBL_BOOLEAN:
tmpBytesEncoded += BEncAsnBoolContent (b, (AsnBool*)v);
break;
case TBL_INTEGER:
case TBL_ENUMERATED:
tmpBytesEncoded += BEncAsnIntContent (b, (AsnInt*)v);
break;
case TBL_BITSTRING:
tmpBytesEncoded += BEncAsnBitsContent (b, (AsnBits*)v);
break;
case TBL_OCTETSTRING:
tmpBytesEncoded += BEncAsnOctsContent (b, (AsnOcts*)v);
break;
case TBL_NULL:
tmpBytesEncoded += BEncAsnNullContent (b, (AsnNull*)v);
break;
case TBL_OID:
tmpBytesEncoded += BEncAsnOidContent (b, (AsnOid*)v);
break;
case TBL_REAL:
tmpBytesEncoded += BEncAsnRealContent (b, (AsnReal*)v);
break;
default:
retVal = -1;
}
if (retVal >= 0)
retVal = TblEncodeTagsAndLens (tblT, b, implicit, &tmpBytesEncoded);
(*bytesEncoded) += tmpBytesEncoded;
return retVal;
}
int
TblEncodeTagsAndLens PARAMS ((tblT, b, implicit, bytesEncoded),
TBLType *tblT _AND_
BUF_TYPE b _AND_
int implicit _AND_
unsigned long int *bytesEncoded)
{
TBLTag *tag;
FOR_EACH_LIST_ELMT_RVS (tag, tblT->tagList)
{
if (!(implicit && (tag == FIRST_LIST_ELMT (tblT->tagList))))
{
if (tag->form == CONS)
(*bytesEncoded) += BEncConsLen (b, *bytesEncoded);
else
(*bytesEncoded) += BEncDefLen (b, *bytesEncoded);
(*bytesEncoded) += TblEncTag (b, tag);
}
}
return 0;
}
AsnLen TblEncTag PARAMS ((b, tag),
BUF_TYPE b _AND_
TBLTag *tag)
{
AsnTag shifted;
unsigned char octet;
AsnLen encLen = 0;
int i;
for (i = 0; i < sizeof (AsnTag); i++)
{
shifted = (tag->encTag >> (i * 8));
octet = shifted & 0xff;
if (octet || i<sizeof(AsnTag)-2 && (shifted & 0x8000))
{
encLen++;
BufPutByteRvs (b, octet);
}
}
return encLen;
}
#endif