⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gen-enc.c

📁 asn to c编译器 源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
static voidPrintCMacroElmtsEncodeCode PARAMS ((src, td, parent, mt, level, varName),    FILE *src _AND_    TypeDef *td _AND_    Type *parent _AND_    MacroType *mt _AND_    int level _AND_    char *varName){    if (mt == NULL)    {        fprintf (src,"/* ERROR? - expected macroType for this type*/\n");        return;    }    switch (mt->choiceId)    {	// This switch case copied from do-macros.c	case MACROTYPE_ASNABSTRACTOPERATION:	case MACROTYPE_ROSOPERATION:				PrintCRosOperationElmtsEncodeCode (src, td, parent, mt, mt->a.rosOperation, level, varName);		break;		// Add code for other macro types here	default:		// Unsupported Macro Type		break;	}}  /* PrintCMacroElmtsEncodeCode */static voidPrintCRosOperationElmtsEncodeCode PARAMS ((src, td, parent, mt, op, level, varName),    FILE *src _AND_    TypeDef *td _AND_    Type *parent _AND_    MacroType *mt _AND_	RosOperationMacroType *op _AND_    int level _AND_    char *varName){	if(op->result != NULL)	{		PrintCElmtEncodeCode (src, td, parent, op->result, level, varName);	}	if(op->arguments != NULL)	{		PrintCElmtEncodeCode (src, td, parent, op->arguments, level, varName);	}	mt=mt;} /* PrintCRosOperationElmtsEncodeCode *//* * Generates code for internally defined lists * eg: * TypeX = SET { foo INTEGER, bar SEQUENCE OF INTEGER } --> * BerEncodeTypeX (b, v) * { *    ... *         listLen = 0; *         FOR_EACH_LIST_ELMT (component, v->bar) *         { *              itemLen = BerEncodeInteger (b, (int*) component); *              itemLen+= EncodeLen (b, itemLen) *              itemLen += ENCODE_TAG (b, INTEGER_TAG); *              listLen += itemLen; *         } *    ... * } */static voidPrintCListEncoderCode PARAMS ((src, td, t, level, varName),    FILE *src _AND_    TypeDef *td _AND_    Type *t _AND_    int level _AND_    char *varName){    CTRI *ctri;    char *elmtVarRef = "component";    Type *tmpType;    char *itemLenName=NULL;    ctri =  t->basicType->a.setOf->cTypeRefInfo;    if (ctri == NULL)        return;        /* IF DER, and SET OF, we need to encode in order of the DER encodings      * of the elements.  This first bit stuffs each encoded elmt into an     * array of bufs.     */    if (GetEncRulesType() == DER && 	t->basicType->choiceId == BASICTYPE_SETOF) {      /* Print extra locals */      fprintf(src, "\tEncodedElmt *bufs;\n");	  fprintf(src, "\tExpBuf	*xbufp;\n");      fprintf(src, "\tint i, len1;\n\n");      fprintf (src, "\tlistLen = 0;\n");      fprintf (src, "\t/* Encode elements and stuff them in an array */\n");      fprintf (src, "\tbufs = (EncodedElmt* ) Asn1Alloc(sizeof(EncodedElmt)*AsnListCount(%s));\n", varName);      fprintf (src, "\tfor (i=0; i < AsnListCount(%s); i++) {\n", 	       varName);      fprintf (src, "\t  xbufp = ExpBufAllocBufAndData();;\n");      fprintf (src, "\t  ExpBufResetInWriteRvsMode(xbufp);// May not need - leave in for now \n ");      fprintf (src, "\t  ExpBuftoGenBuf(xbufp, &bufs[i].b);\n   } \n");      fprintf (src, "\ti = 0;\n");      fprintf (src, "\tFOR_EACH_LIST_ELMT_RVS (component, %s)\n", varName);      fprintf (src, "\t{\n");      itemLenName = itemLenNameG;      itemLenNameG = "bufs[i].len";      bufNameG = "bufs[i].b";    } else {      fprintf (src, "\tlistLen = 0;\n");      fprintf (src, "\tFOR_EACH_LIST_ELMT_RVS(component,%s)\n", varName);      fprintf (src, "\t{\n");      bufNameG = "b";    }	  fflush (src);    PrintEocEncoders (src, td, t->basicType->a.setOf);    /*     * need extra case here for SET OF typedef not just SET OF typeref     */    switch(ctri->cTypeId)    {        case C_TYPEREF:            tmpType = GetType (t->basicType->a.setOf);             /* NOTE: ANY DEFINED BY must be directly in the parent (not ref)*/            if (tmpType->cTypeRefInfo->cTypeId != C_ANY)            {                fprintf (src, "\t%s = %s%sContent(%s,%s);\n", itemLenNameG,			 GetEncRulePrefix(), ctri->encodeRoutineName, 			 bufNameG, elmtVarRef);                break;            }            else  /* fall through */        case C_ANY:            /* ANY's enc's do tag and len so zap the Content suffix */            fprintf (src,"\t /* ANY - Fix Me! */\n");            fprintf (src,"\tSetAnyTypeUnknown(%s);\n", elmtVarRef);            //RWC;fprintf (src, "\tSetAnyTypeBy???(%s,???);\n", elmtVarRef);            fprintf (src, "\t%s = %s%s(%s,%s);\n", itemLenNameG,				GetEncRulePrefix(), ctri->encodeRoutineName,				bufNameG, elmtVarRef);            break;        default:            fprintf (src, "\t%s = %s%sContent(%s,(%s*)%s);\n", 		     itemLenNameG, GetEncRulePrefix(), 		     ctri->encodeRoutineName, bufNameG, ctri->cTypeName, 		     elmtVarRef);            break;    }    PrintCTagAndLenEncodingCode (src, td, t->basicType->a.setOf);    fprintf (src,"\n");    fprintf (src, "\t%s += %s;\n", listLenNameG, itemLenNameG);    if (GetEncRulesType() == DER && 	t->basicType->choiceId == BASICTYPE_SETOF) { 	  fprintf (src, "\t  BufResetInReadMode(bufs[i].b);\n");      fprintf (src, "\t  i++;\n");    }    fprintf (src, "\t}\n");    if (GetEncRulesType() == DER && 	t->basicType->choiceId == BASICTYPE_SETOF) {      /* Sort the encoded results in the array of bufs, then stuff everything       * in the output buf       */      fprintf (src, "\tlen1 = i;\n");      fprintf (src, "\t/* Sort elements */\n");      fprintf (src, "\tqsort(bufs, i, sizeof(EncodedElmt), EncodedElmtCmp);\n");      fprintf (src, "\tfor (i--; i>=0; i--) {\n");      fprintf (src, "\t\tchar *dst = (char *) Asn1Alloc(bufs[i].len);\n");      fprintf (src, "\t\tBufResetInReadMode(bufs[i].b);\n");      fprintf (src, "\t\tBufCopy(dst, bufs[i].b, bufs[i].len);\n");      fprintf (src, "\t\tBufPutSegRvs(b, dst, bufs[i].len);\n");      fprintf (src, "\t\tExpBufFreeBufAndData(bufs[i].b->spare);\n");      fprintf (src, "\t};\n");	  fprintf (src, "\t/* Free the EncodedElmt's */\n");	  fprintf (src, "\tAsn1Free(bufs);\n");      itemLenNameG = itemLenName;      bufNameG = "b";    }    level = level;  /* AVOIDS warning. */}  /*  PrintCListEncoderCode */static voidPrintCChoiceEncodeCode PARAMS ((src, td, t, level, varName),    FILE *src _AND_    TypeDef *td _AND_    Type *t _AND_    int level _AND_    char *varName){    NamedType *e;    CTRI *ctri;    void *tmp;    ctri =  t->cTypeRefInfo;    fprintf (src,"\tswitch(%s->%s)\n\t{\n", varName, ctri->choiceIdEnumFieldName);    FOR_EACH_LIST_ELMT (e, t->basicType->a.choice)    {        tmp = (void*)CURR_LIST_NODE (t->basicType->a.choice);        if (e->type == NULL)            continue;        ctri =  e->type->cTypeRefInfo;        if (ctri != NULL)            fprintf (src, "\t\tcase %s:\n", ctri->choiceIdSymbol);        else            fprintf (src, "\t\tcase ????:\n");        PrintCElmtEncodeCode (src, td, t, e, level+1, varName);        fprintf (src,"\tbreak;\n\n");        SET_CURR_LIST_NODE (t->basicType->a.choice, tmp);    }    fprintf (src, "\t}\n");}  /* PrintCChoiceEncodeCode *//* *  prints DecodeBerEocIfNec (b) for each constructed len *  assoc with given type */static voidPrintEocEncoders PARAMS ((src, td, t),    FILE *src _AND_    TypeDef *td _AND_    Type *t){    TagList *tl;    Tag *tag;    int stoleChoiceTags;    /*     * get all the tags on this type     */    tl = (TagList*) GetTags (t, &stoleChoiceTags);    /*     * leave choice elmt tag enc to encoding routine     */    if (!stoleChoiceTags)    {        FOR_EACH_LIST_ELMT (tag, tl)        {            if (tag->form == CONS)                fprintf (src,"\t%sEncEocIfNec(b);\n", GetEncRulePrefix());        }    }/*    consTagCount = 0;    if (!stoleChoiceTags)    {        FOR_EACH_LIST_ELMT (tag, tl)            consTagCount++;    }    if (IsPrimitiveByDefOrRef (t))        consTagCount--;    for (; consTagCount > 0; consTagCount--)        fprintf (src,"\t%sEncEocIfNec (b);\n", GetEncRulePrefix());*/    FreeTags (tl);    td = td; /*AVOIDS warning*/}  /* PrintEocEncoders *//* *  Recursively walks throught type refs printing lower lvl tags *  first (since encoding is done backwards). * */static voidPrintCTagAndLenEncodingCode PARAMS ((src, td, t),    FILE *src _AND_    TypeDef *td _AND_    Type *t){    TagList *tl;    int stoleChoiceTags;    /*     * get all the tags on this type     */    tl = (TagList*) GetTags (t, &stoleChoiceTags);    /*     * leave choice elmt tag enc to encoding routine     */    if (!stoleChoiceTags)        PrintCTagAndLenList (src, t, tl);    FreeTags (tl);    td = td; /*AVOIDS warning*/}  /* PrintCTagAndLenEncodingCode *//* * prints last tag's encoding code first */static voidPrintCTagAndLenList PARAMS ((src, t, tagList),    FILE *src _AND_    Type *t _AND_    TagList *tagList){    char *classStr;    char *formStr;    Tag *tg;    Tag *last;    int tagLen;    enum BasicTypeChoiceId typesType;    int isShort;    if ((tagList == NULL) || LIST_EMPTY (tagList))        return;    /*     * efficiency hack - use simple length (1 byte)     * encoded for type (almost) guaranteed to have     * encoded lengths of 0 <= len <= 127     */    typesType = GetBuiltinType (t);    if ((typesType == BASICTYPE_BOOLEAN) ||        (typesType == BASICTYPE_INTEGER) ||        (typesType == BASICTYPE_NULL) ||        (typesType == BASICTYPE_REAL) ||        (typesType == BASICTYPE_ENUMERATED))        isShort = 1;    else        isShort = 0;    /*     * since encoding backward encode tags backwards     */    last = (Tag*)LAST_LIST_ELMT (tagList);    FOR_EACH_LIST_ELMT_RVS (tg, tagList)    {        classStr = Class2ClassStr (tg->tclass);        if (tg->form == CONS)        {            formStr = Form2FormStr (CONS);            PrintCLenEncodingCode (src, TRUE, isShort);        }        else /* PRIM or ANY_FORM */        {            formStr = Form2FormStr (PRIM);            PrintCLenEncodingCode (src, FALSE, isShort);        }/*       GetTags sets the form bit correctly now        if (IsPrimitiveByDefOrRef (t) && (tg == last))        {            formStr = Form2FormStr (PRIM);            PrintCLenEncodingCode (src, FALSE, isShort);        }        else        {            formStr = Form2FormStr (CONS);            PrintCLenEncodingCode (src, TRUE, isShort);        }  */        fprintf (src,"\n");        if (tg->code < 31)            tagLen = 1;        else if (tg->code < 128)            tagLen = 2;        else if (tg->code < 16384)            tagLen = 3;        else if (tg->code < 2097152)            tagLen = 4;        else            tagLen = 5;        fprintf (src,"\t%s += %sEncTag%d(%s,%s,%s,%s);\n", itemLenNameG, 		 GetEncRulePrefix(), tagLen, bufNameG, classStr, formStr,          DetermineCode(tg, &tagLen, 0));		 //RWC;Code2UnivCodeStr (tg->code));    }}  /* PrintCTagAndLenList *//* * prints length encoding code.  Primitives always use * definite length and constructors get "ConsLen" * which can be configured at compile to to be indefinite * or definite.  Primitives can also be "short" (isShort is true) * in which case a fast macro is used to write the length. * Types for which isShort apply are: boolean, null and * (almost always) integer and reals */static voidPrintCLenEncodingCode PARAMS ((f, isCons, isShort),    FILE *f _AND_    int isCons _AND_    int isShort){   /* fprintf (f, "\t%sER_ENCODE_DEF_LEN (b, itemLen, itemLen);",      GetEncRulePrefix()); */    if (isCons)        fprintf (f, "\t%s += %sEncConsLen(%s,%s);", 		 itemLenNameG, GetEncRulePrefix(), bufNameG, itemLenNameG);    else    {        if (isShort)        {            fprintf (f, "\t%sEncDefLenTo127(%s,%s);\n", 		     GetEncRulePrefix(), bufNameG, itemLenNameG);            fprintf (f, "\t%s++;", itemLenNameG);        }        else            fprintf (f, "\t%s += %sEncDefLen(%s,%s);",		     itemLenNameG, GetEncRulePrefix(), bufNameG, itemLenNameG);    }}  /* PrintCLenEncodingCode */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -