📄 gen-enc.c
字号:
/* * compiler/back-ends/c-gen/gen-enc.c - routines for printing c encoders from type trees * * Mike Sample * 91/09/26 * Copyright (C) 1991, 1992 Michael Sample * and the University of British Columbia * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * $Header: /baseline/SNACC/compiler/back-ends/c-gen/gen-enc.c,v 1.18 2004/03/25 19:20:16 gronej Exp $ * $Log: gen-enc.c,v $ * Revision 1.18 2004/03/25 19:20:16 gronej * fixed some linux warnings * * Revision 1.17 2004/03/12 18:51:20 gronej * updated c-library to error on extension additions as it does with untagged elements * * Revision 1.16 2004/01/14 19:07:53 gronej * Updated Compiler to accept and process relative-oid's * * Revision 1.15 2003/08/04 10:35:23 colestor * Updated several improperly referenced "b" buffer parameters when dealing with * ANY load/unloads (encode/decodes). This code has never been tested in the * "C" library. * * Revision 1.14 2003/07/31 18:33:23 colestor * Updated to reflect newly added ANY processing as AsnOcts. All built code will * now compile directly. * * Revision 1.13 2003/07/30 19:01:40 colestor * (RWC)Added ANY (not ANY DEFINED BY) default to newly added * SetAnyTypeUnknown(...). (UNTESTED) * * Revision 1.12 2003/07/28 11:13:51 colestor * Changes to complete handing of the "--snacc namespace" compiler directive. * Also, updates to handle ASN.1 constant integer tag designations for C++/C. * * Revision 1.11 2003/07/07 14:53:38 nicholar * Eliminated headers and cleaned up include references * * Revision 1.10 2003/04/29 21:03:24 leonberp * integerated Deepak's changes for IOB support * * Revision 1.9 2003/02/21 18:58:03 leonberp * removed for loop from around Asn1fFree(bufs) * * Revision 1.8 2002/10/22 14:39:40 mcphersc * Added modified DER encoding snacc generated code * * Revision 1.7 2002/10/21 17:15:43 mcphersc * fixed long int * * Revision 1.6 2002/09/16 17:34:54 mcphersc * Fixed warnings * * Revision 1.5 2002/09/04 17:59:33 vracarl * got rid of c++ comments * * Revision 1.4 2002/01/18 16:13:13 vracarl * fixed code to put in missing .bitLen code * * Revision 1.3 2000/10/24 14:54:47 rwc * Updated to remove high-level warnings (level 4 on MSVC++) for an easier build. * SOME warnings persist due to difficulty in modifying the SNACC compiler to * properly build clean source; also some files are built by Lex/Yacc. * * Revision 1.2 2000/10/16 18:10:42 rwc * removed most warnings from C++-lib, some C-lib. * * Revision 1.1.1.1 2000/08/21 20:36:05 leonberp * First CVS Version of SNACC. * * Revision 1.5 1997/10/23 01:15:55 povey * Fixed bug where default values weren't checked properly before encoding * * Revision 1.4 1997/08/28 07:26:08 povey * Changes to support DER encoding/decoding * * Revision 1.3.1.1 1997/08/20 23:14:41 povey * * Revision 1.3 1995/07/25 18:42:24 rj * file name has been shortened for redundant part: c-gen/gen-c-enc -> c-gen/gen-enc. * * changed `_' to `-' in file names. * * Revision 1.2 1994/09/01 00:23:10 rj * snacc_config.h and other superfluous .h files removed. * * Revision 1.1 1994/08/28 09:48:24 rj * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog. * *//* Deepak: Formatting improved as required on 11/Feb/2003*/#include "asn-incl.h"#include "asn1module.h"#include "rules.h"#include "util.h"#include "tag-util.h"#include "snacc-util.h"#include "enc-rules.h"EncRulesType GetEncRulesType();void PrintConstraintValueCheckingCode PROTO ((FILE *src, TypeDef *td, Type *t, NamedType *nt));//static int moduleImplicitTagsG;static CRules *genEncCRulesG;static char* bufNameG = "b";extern char *valueArgNameG;char *encodedLenVarNameG = "totalLen";char *itemLenNameG = "itemLen";char *listComponentNameG = "component";char *listLenNameG = "listLen";char *returnTypeG = "AsnLen";extern char *bufTypeNameG;extern char *lenTypeNameG;extern char *tagTypeNameG;extern char *envTypeNameG;void PrintCValueInstantiation PROTO ((FILE *hdr, CRules *r, Value *v));/* non-exported prototypes */static void PrintCEncoderPrototype PROTO ((FILE *hdr, TypeDef *td));static void PrintCEncoderDeclaration PROTO ((FILE *src, TypeDef *td));static void PrintCEncoderDefine PROTO ((FILE *src, TypeDef *td));static void PrintCEncoderLocals PROTO ((FILE *src, TypeDef *td));static void PrintCElmtsEncodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *e, int level, char *varName));static void PrintCElmtEncodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, NamedType *e, int level, char *varName));static void PrintCListEncoderCode PROTO ((FILE *src, TypeDef *td, Type *t, int level, char *varName));static void PrintCChoiceEncodeCode PROTO ((FILE *src, TypeDef *td, Type *t, int level, char *varName));static void PrintCTagAndLenEncodingCode PROTO ((FILE *src, TypeDef *td, Type *t));static void PrintEocEncoders PROTO ((FILE *src, TypeDef *td, Type *t));static void PrintCLenEncodingCode PROTO ((FILE *f, int isCons, int isShort));static void PrintCTagAndLenList PROTO ((FILE *src, Type *t,TagList *tg));// Deepak:18/Apr/2003static void PrintCMacroElmtsEncodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, MacroType *mt, int level, char *varName));static void PrintCRosOperationElmtsEncodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, MacroType *mt, RosOperationMacroType *op, int level, char *varName));// Deepak: 31/Mar/2003 // Following 4 func's are similar to that in gen-dec.cstatic void PrintCEncoderTableConsType PROTO ((FILE *src, FILE *hdr, Module *m, TypeDef *td, Type *t, NamedType *nt));static void PrintCEncoderTableConsBasicType PROTO ((FILE *src, FILE *hdr, Module *m, TypeDef *td, Type *t, NamedType *nt, BasicType *bt));static void PrintCEncoderTableConsElmtTypes PROTO ((FILE *src, FILE *hdr, Module *m, TypeDef *td, NamedTypeList *e));static void PrintCEncoderTableConsElmtType PROTO ((FILE *src, FILE *hdr, Module *m, TypeDef *td, NamedType *n));void // Deepak: 25/Mar/2003PrintCTableConstraintEncoder PARAMS ((src, hdr, m, td), FILE *src _AND_ FILE *hdr _AND_ Module *m _AND_ TypeDef *td){ fprintf (hdr, "%s %s%s_TableCons(%s %s,%s *v);\n", returnTypeG, GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName, bufTypeNameG, bufNameG, td->cTypeDefInfo->cTypeName); fprintf (hdr, "\n"); fprintf (src, "%s %s%s_TableCons(%s %s,%s *v)\n", returnTypeG, GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName, bufTypeNameG, bufNameG, td->cTypeDefInfo->cTypeName); fprintf (src, "{\n"); fprintf (src, "\t%s totalLen = 0;\n", returnTypeG); fprintf (src, "\t%s itemLen = 0;\n", returnTypeG); fprintf (src, "\t%s index;\n", returnTypeG); fprintf (src, "\n"); // Check each field here PrintCEncoderTableConsType(src, hdr, m, td, td->type, NULL); fprintf (src, "\treturn totalLen;\n"); fprintf (src,"} /* %s%s_TableCons */", GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName); fprintf (src,"\n\n");} /* PrintCTableConstraintEncoder */static void // Deepak: 25/Mar/2003PrintCEncoderTableConsType PARAMS ((src, hdr, m, td, t, nt), FILE *src _AND_ FILE *hdr _AND_ Module *m _AND_ TypeDef *td _AND_ Type *t _AND_ NamedType *nt){ if(t == NULL) return; PrintCEncoderTableConsBasicType(src, hdr, m, td, t, nt, t->basicType);} /* PrintCEncoderTableConsType */static void // Deepak: 25/Mar/2003PrintCEncoderTableConsBasicType PARAMS ((src, hdr, m, td, t, nt, bt), FILE *src _AND_ FILE *hdr _AND_ Module *m _AND_ TypeDef *td _AND_ Type *t _AND_ NamedType *nt _AND_ BasicType *bt){ if(bt == NULL) return; switch (bt->choiceId) { case BASICTYPE_SEQUENCET: // Deepak: added on 29/Nov/2002 case BASICTYPE_SEQUENCE: case BASICTYPE_SET: case BASICTYPE_CHOICE: // Deepak: process the elements in sequence etc here. PrintCEncoderTableConsElmtTypes (src, hdr, m, td, bt->a.set); break; case BASICTYPE_SEQUENCEOF: case BASICTYPE_SETOF: //TypeLinkType (m, currMod, head, bt->a.setOf); break; case BASICTYPE_INTEGER: PrintConstraintValueCheckingCode(src, td, t, nt); break; case BASICTYPE_OBJECTCLASSFIELDTYPE: // if(nt->type->basicType->choiceId == 0) { char* objSetName = t->tableConstraint->objSetAssignment->objectSetName; char* typeName = nt->type->typeName; fprintf (src, "\tif(%s[index].m.%sPresent)\n", objSetName, typeName); fprintf (src, "\t{\n"); fprintf (src, "\t\titemLen = %s[index].m.encode%s(%s,%s[index].%s);\n", objSetName, typeName, bufNameG, objSetName, typeName); fprintf (src, "\t\tv->%s.octetLen = itemLen;\n", nt->fieldName); fprintf (src, "\t\tv->%s.octs = Asn1Alloc(itemLen);\n", nt->fieldName); fprintf (src, "\t\tmemcpy(v->%s.octs,b->dataStart,itemLen);\n", nt->fieldName); fprintf (src, "\t\ttotalLen += itemLen;\n"); fprintf (src, "\t}\n"); fprintf (src, "\n"); } break; default: break; }} /* PrintCEncoderTableConsBasicType */static void // Deepak: 25/Mar/2003PrintCEncoderTableConsElmtTypes PARAMS ((src, hdr, m, td, e), FILE *src _AND_ FILE *hdr _AND_ Module *m _AND_ TypeDef *td _AND_ NamedTypeList *e){ NamedType *n; FOR_EACH_LIST_ELMT (n, e) { PrintCEncoderTableConsElmtType (src, hdr, m, td, n); } } /* PrintCEncoderTableConsElmtTypes */static void // Deepak: 25/Mar/2003PrintCEncoderTableConsElmtType PARAMS ((src, hdr, m, td, n), FILE *src _AND_ FILE *hdr _AND_ Module *m _AND_ TypeDef *td _AND_ NamedType *n){ if(n->type->tableConstraint) { fprintf (src, "\t/* Check %s */\n", n->fieldName); PrintCEncoderTableConsType (src, hdr, m, td, n->type, n); }} /* PrintCEncoderTableConsElmtType */voidPrintCEncoder PARAMS ((src, hdr, r, m, td), FILE *src _AND_ FILE *hdr _AND_ CRules *r _AND_ Module *m _AND_ TypeDef *td){ enum BasicTypeChoiceId typeId; CTDI *ctdi; TagList *tags = NULL; Tag *tag; char *formStr; char *classStr; int tagLen; int stoleChoiceTags; EncRulesType* encoding; char *pszCode=NULL; ctdi = td->cTypeDefInfo; if (!ctdi->genEncodeRoutine) return; /* Generate encoders for each encoding rule required */ encoding = GetEncRules(); while (SetEncRules(*encoding)) { encoding++; /* * if is type that refs another pdu type or lib type * without generating a new type via tagging or named elmts * print define to the hdr file * (a type is a pdu by default if it is ref'd by an ANY) */ if (!IsNewType (td->type) && (!IsTypeRef (td->type) || (IsTypeRef (td->type) && (td->type->basicType->a.localTypeRef->link->cTypeDefInfo->isPdu || ((td->type->basicType->a.localTypeRef->link->anyRefs != NULL) && !LIST_EMPTY (td->type->basicType->a.localTypeRef->link->anyRefs)))))) { fprintf(hdr,"#define %s%s\t%s%s\n", GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName, GetEncRulePrefix(), td->type->cTypeRefInfo->encodeRoutineName); //fprintf(hdr,"#define %s%s(b, v, bytesDecoded, env) %s%s(b, v, bytesDecoded, env)\n", GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName, GetEncRulePrefix(), td->type->cTypeRefInfo->encodeRoutineName); return; } typeId = GetBuiltinType (td->type); /* print proto to hdr file */// fprintf (hdr,"%s %s%s PROTO ((%s b, %s *v));\n\n", lenTypeNameG, GetEncRulePrefix(), ctdi->encodeRoutineName, bufTypeNameG, ctdi->cTypeName); fprintf (hdr,"%s %s%s(%s b,%s *v);\n\n", lenTypeNameG, GetEncRulePrefix(), ctdi->encodeRoutineName, bufTypeNameG, ctdi->cTypeName); /* print routine to src file */// fprintf (src,"%s %s%s PARAMS ((b, v),\n", lenTypeNameG, GetEncRulePrefix(), ctdi->encodeRoutineName);// fprintf (src,"%s b _AND_\n",bufTypeNameG);// fprintf (src,"%s *v)\n",ctdi->cTypeName);// fprintf (src,"{\n");// fprintf (src,"\t%s l=0;\n", lenTypeNameG); //fprintf (src,"%s %s%s", lenTypeNameG, GetEncRulePrefix(), ctdi->encodeRoutineName); // Deepak: 16/Apr/2003 fprintf (src,"%s %s%s(", lenTypeNameG, GetEncRulePrefix(), ctdi->encodeRoutineName); fprintf (src,"%s b,",bufTypeNameG); fprintf (src,"%s *v)\n",ctdi->cTypeName); fprintf (src,"{\n"); fprintf (src,"\t%s l=0;\n", lenTypeNameG); PrintEocEncoders (src, td, td->type); fprintf (src,"\tl = %s%sContent (b,v);\n", GetEncRulePrefix(), ctdi->encodeRoutineName); /* encode each tag/len pair if any */ tags = GetTags (td->type, &stoleChoiceTags); if (! stoleChoiceTags) { FOR_EACH_LIST_ELMT_RVS (tag, tags) { classStr = Class2ClassStr (tag->tclass); if (tag->form == ANY_FORM) tag->form = PRIM; formStr = Form2FormStr (tag->form); tagLen = TagByteLen (tag->code); if (tag->form == CONS) fprintf (src,"\tl += %sEncConsLen(b,l);\n", GetEncRulePrefix()); else fprintf (src,"\tl += %sEncDefLen(b,l);\n", GetEncRulePrefix()); if (tag->tclass == UNIV) pszCode = DetermineCode(tag, &tagLen, 0); else pszCode = DetermineCode(tag, &tagLen, 1); fprintf (src,"\tl += %sEncTag%d(b,%s,%s,%s);\n", GetEncRulePrefix(), tagLen, classStr, formStr, pszCode); /*RWC;Code2UnivCodeStr (tag->code)); else fprintf (src,"\tl += %sEncTag%d(b,%s,%s,%d);\n", GetEncRulePrefix(), tagLen, classStr, formStr, tag->code);*/ } } fprintf (src,"\treturn l;\n"); fprintf (src,"} /* %s%s */\n\n", GetEncRulePrefix(), ctdi->encodeRoutineName); FreeTags (tags); } m = m ; r = r; /* AVOIDS warning. */} /* PrintCEncoder */voidPrintCContentEncoder PARAMS ((src, hdr, r, m, td), FILE *src _AND_ FILE *hdr _AND_ CRules *r _AND_ Module *m _AND_ TypeDef *td){ CTDI *ctdi; CTypeId rhsTypeId; /* cTypeId of the type that defined this typedef */ EncRulesType* encoding; genEncCRulesG = r; ctdi = td->cTypeDefInfo; if (!ctdi->genEncodeRoutine) return; rhsTypeId = td->type->cTypeRefInfo->cTypeId; /* Generate encoders for each encoding rule required */ encoding = GetEncRules(); while (SetEncRules(*encoding)) { encoding++; switch(rhsTypeId) { case C_ANY: fprintf (hdr, "/* ANY - Fix Me! */\n"); /* * Note - ANY's don't have the 'Content' suffix cause they * encode their tags and lengths */ fprintf(hdr, "#define %s%s\t%s%s\n", GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName, GetEncRulePrefix(), td->type->cTypeRefInfo->encodeRoutineName); /* fprintf(hdr, "#define %s%s( b, v) ", GetEncRulePrefix(), td->cTypeDefInfo->encodeRoutineName); fprintf (hdr, "%s%s (b, v)", GetEncRulePrefix(), td->type->cTypeRefInfo->encodeRoutineName); */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -