From aconole at bytheb.org Fri Feb 2 19:24:38 2018 From: aconole at bytheb.org (Aaron Conole) Date: Fri, 02 Feb 2018 19:24:38 -0000 Subject: [esnacc-dev] [PATCH 0/2] friday fixes... Message-ID: <20180202192431.21109-1-aconole@bytheb.org> Turns out the c-library needs some additional love. This time, fixes in the minbuf library and code generation. Thanks to @m-cobbold on github for the discoveries. Aaron Conole (2): compiler: fix c-code c++ guards min-buf: Fix "encoding" put asn1specs/p-rec.asn1 | 7 ++ c-examples/simple/minbuf-ex.c | 29 ++++++- c-lib/src/min-buf.c | 1 + compiler/back-ends/c-gen/gen-code.c | 158 ++++++++++++++++-------------------- 4 files changed, 104 insertions(+), 91 deletions(-) -- 2.14.3 From aconole at bytheb.org Fri Feb 2 19:24:39 2018 From: aconole at bytheb.org (Aaron Conole) Date: Fri, 02 Feb 2018 19:24:39 -0000 Subject: [esnacc-dev] [PATCH 1/2] compiler: fix c-code c++ guards In-Reply-To: <20180202192431.21109-1-aconole@bytheb.org> References: <20180202192431.21109-1-aconole@bytheb.org> Message-ID: <20180202192431.21109-2-aconole@bytheb.org> The c++ extern was improperly being generated. This doesn't matter when using a C compiler (since __cplusplus won't be defined), but will matter when using a C++ compiler. This commit restores the standard include guard banner. Reported-at: https://github.com/esnacc/esnacc-ng/issues/45 Reported-by: m-cobbold Signed-off-by: Aaron Conole --- compiler/back-ends/c-gen/gen-code.c | 158 ++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 90 deletions(-) diff --git a/compiler/back-ends/c-gen/gen-code.c b/compiler/back-ends/c-gen/gen-code.c index abf0d32..7a224d7 100644 --- a/compiler/back-ends/c-gen/gen-code.c +++ b/compiler/back-ends/c-gen/gen-code.c @@ -162,73 +162,58 @@ PrintCCode PARAMS ((src, hdr, mods, m, r, longJmpVal, printTypes, printValues, p TypeDef *td; ValueDef *vd; - /* Deepak: suppose the asn source file is test.asn - * then the C source file name is test.c and C header file is test.h - */ + /* Deepak: suppose the asn source file is test.asn + * then the C source file name is test.c and C header file is test.h + */ PrintCSrcComment(src, m); PrintCSrcIncludes(hdr, m, mods); PrintCHdrComment(hdr, m); PrintConditionalIncludeOpen(hdr, m->cHdrFileName); - /* PIERCE TBD: Is this necessary still after Deepak's mods? - * - * Add include reference to source file - * - */ - fprintf (src, "#include \"%s\"\n", m->cHdrFileName); - fprintf (hdr,"\n\n"); - fprintf (hdr,"#ifdef __cplusplus\n"); - fprintf (hdr,"extern \"C\" {\n"); - fprintf (hdr,"#endif\n"); - // RWC; ADDED to remove warning about "unreferenced local variable" for - // variables that are hardcoded by the eSNACC compiler; depending on - // the recursed data types (e.g. in a CHOICE), the variables may not be - // used. We just ignore the warning. - fprintf (hdr,"#ifdef _WIN32\n"); - fprintf (hdr,"#pragma warning( disable : 4101 )\n"); - fprintf (hdr,"#endif\n"); + /* Add include reference to source file */ + fprintf(src, "#include \"%s\"\n", m->cHdrFileName); + fprintf(hdr, "\n\n"); + + fprintf(hdr, "#ifdef __cplusplus\n"); + fprintf(hdr, "extern \"C\" {\n"); + fprintf(hdr, "#endif\n"); + + fprintf(hdr, "#ifdef _WIN32\n"); + fprintf(hdr, "#pragma warning( disable : 4101 )\n"); + fprintf(hdr, "#endif\n"); fprintf (src,"\n\n"); - if (printValues) - { + if (printValues) { /* put value defs at beginning of .c file */ - FOR_EACH_LIST_ELMT (vd, m->valueDefs) - { + FOR_EACH_LIST_ELMT(vd, m->valueDefs) { PrintCValueDef (src, r, vd); } } - PrintCAnyCode (src, hdr, r, mods, m, printEncoders, printDecoders, - printPrinters, printFree); + PrintCAnyCode(src, hdr, r, mods, m, printEncoders, printDecoders, + printPrinters, printFree); - FOR_EACH_LIST_ELMT (td, m->typeDefs) - { + FOR_EACH_LIST_ELMT(td, m->typeDefs) { if (printTypes) PrintCTypeDef (hdr, r, m, td); /* for PDU type or types ref'd with ANY/ANY DEF BY */ - if (printEncoders && ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu)) + if (printEncoders && + ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu)) PrintCEncoder (src, hdr, r, m, td); /* for PDU type or types ref'd with ANY/ANY DEF BY */ - if (printDecoders && ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu)) + if (printDecoders && + ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu)) PrintCDecoder (src, hdr, r, m, td, &longJmpVal); if (printEncoders) - { PrintCContentEncoder (src, hdr, r, m, td); - //if (td->bHasTableConstraint) - // PrintCTableConstraintEncoder (src, hdr, m, td); // Deepak: 25/Mar/2003 - } if (printDecoders) - { PrintCContentDecoder (src, hdr, r, m, td, &longJmpVal); - //if (td->bHasTableConstraint) - // PrintCTableConstraintDecoder (src, hdr, m, td); // Deepak: 25/Mar/2003 - } if (printPrinters) PrintCPrinter (src, hdr, r, mods, m, td); @@ -237,67 +222,60 @@ PrintCCode PARAMS ((src, hdr, mods, m, r, longJmpVal, printTypes, printValues, p PrintCFree (src, hdr, r, mods, m, td); /* only print new lines for normal types */ - switch (td->type->basicType->choiceId) - { - case BASICTYPE_SEQUENCEOF: /* list types */ - case BASICTYPE_SETOF: - case BASICTYPE_CHOICE: - case BASICTYPE_SET: - case BASICTYPE_SEQUENCE: - case BASICTYPE_SEQUENCET: // Deepak: 30/Nov/2002 - case BASICTYPE_OBJECTCLASS: // Deepak: 14/Mar/2003 - fprintf (src, "\n"); - /* fall through */ - - case BASICTYPE_IMPORTTYPEREF: /* type references */ - case BASICTYPE_LOCALTYPEREF: - case BASICTYPE_BOOLEAN: /* library type */ - case BASICTYPE_REAL: /* library type */ - case BASICTYPE_OCTETSTRING: /* library type */ - case BASICTYPE_NULL: /* library type */ - case BASICTYPE_OID: /* library type */ - case BASICTYPE_RELATIVE_OID: /* library type */ - case BASICTYPE_INTEGER: /* library type */ - case BASICTYPE_BITSTRING: /* library type */ - case BASICTYPE_ENUMERATED: /* library type */ - case BASICTYPE_ANYDEFINEDBY: /* ANY types */ - case BASICTYPE_ANY: - case BASICTYPE_NUMERIC_STR: /* library type */ - case BASICTYPE_PRINTABLE_STR: /* library type */ - case BASICTYPE_IA5_STR: /* library type */ - case BASICTYPE_BMP_STR: /* library type */ - case BASICTYPE_UNIVERSAL_STR: /* library type */ - case BASICTYPE_UTF8_STR: /* library type */ - case BASICTYPE_T61_STR: /* library type */ - fprintf (hdr, "\n"); - break; - - default: - break; + switch (td->type->basicType->choiceId) { + case BASICTYPE_SEQUENCEOF: /* list types */ + case BASICTYPE_SETOF: + case BASICTYPE_CHOICE: + case BASICTYPE_SET: + case BASICTYPE_SEQUENCE: + case BASICTYPE_SEQUENCET: // Deepak: 30/Nov/2002 + case BASICTYPE_OBJECTCLASS: // Deepak: 14/Mar/2003 + fprintf(src, "\n"); + /* fall through */ + case BASICTYPE_IMPORTTYPEREF: /* type references */ + case BASICTYPE_LOCALTYPEREF: + case BASICTYPE_BOOLEAN: /* library type */ + case BASICTYPE_REAL: /* library type */ + case BASICTYPE_OCTETSTRING: /* library type */ + case BASICTYPE_NULL: /* library type */ + case BASICTYPE_OID: /* library type */ + case BASICTYPE_RELATIVE_OID: /* library type */ + case BASICTYPE_INTEGER: /* library type */ + case BASICTYPE_BITSTRING: /* library type */ + case BASICTYPE_ENUMERATED: /* library type */ + case BASICTYPE_ANYDEFINEDBY: /* ANY types */ + case BASICTYPE_ANY: + case BASICTYPE_NUMERIC_STR: /* library type */ + case BASICTYPE_PRINTABLE_STR: /* library type */ + case BASICTYPE_IA5_STR: /* library type */ + case BASICTYPE_BMP_STR: /* library type */ + case BASICTYPE_UNIVERSAL_STR: /* library type */ + case BASICTYPE_UTF8_STR: /* library type */ + case BASICTYPE_T61_STR: /* library type */ + fprintf(hdr, "\n"); + break; + + default: + break; } } - // Declare ObjectAssignment, ObjestSetAssignment, & initialize them. - if(isTableConstraintAllowed) - { - PrintCHdrObjectDeclaration_and_Init (hdr, m, r); // Deepak: 24/Mar/2003 - } + // Declare ObjectAssignment, ObjestSetAssignment, & initialize them. + if(isTableConstraintAllowed) + PrintCHdrObjectDeclaration_and_Init(hdr, m, r); - if (printValues) - { + if (printValues) { /* put value externs at end of .h file */ - FOR_EACH_LIST_ELMT (vd, m->valueDefs) - { - PrintCValueExtern (hdr, r, vd); + FOR_EACH_LIST_ELMT(vd, m->valueDefs) { + PrintCValueExtern(hdr, r, vd); } } - fprintf (hdr,"#ifdef __cplusplus\n"); - fprintf (hdr,"extern \"C\" {\n"); - fprintf (hdr,"#endif\n"); - + fprintf(hdr, "#ifdef __cplusplus\n"); + fprintf(hdr, "}\n"); + fprintf(hdr, "#endif\n"); - PrintConditionalIncludeClose (hdr, m->cHdrFileName); + PrintConditionalIncludeClose(hdr, m->cHdrFileName); } /* PrintCCode */ -- 2.14.3 From aconole at bytheb.org Fri Feb 2 19:24:40 2018 From: aconole at bytheb.org (Aaron Conole) Date: Fri, 02 Feb 2018 19:24:40 -0000 Subject: [esnacc-dev] [PATCH 2/2] min-buf: Fix "encoding" put In-Reply-To: <20180202192431.21109-1-aconole@bytheb.org> References: <20180202192431.21109-1-aconole@bytheb.org> Message-ID: <20180202192431.21109-3-aconole@bytheb.org> The min-buf encoder was previously only writing to the same regions of memory, rather than updating the buffer pointer. This has the effect of not actually encoding when using the min buffer. The fix includes a simple test case. More thorough test cases will be added. Reported-at: https://github.com/esnacc/esnacc-ng/issues/45 Reported-by: m-cobbold Signed-off-by: Aaron Conole --- asn1specs/p-rec.asn1 | 7 +++++++ c-examples/simple/minbuf-ex.c | 29 ++++++++++++++++++++++++++++- c-lib/src/min-buf.c | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/asn1specs/p-rec.asn1 b/asn1specs/p-rec.asn1 index bbe700c..2f754fd 100644 --- a/asn1specs/p-rec.asn1 +++ b/asn1specs/p-rec.asn1 @@ -53,4 +53,11 @@ EmployeeNumber ::= [APPLICATION 128] IMPLICIT INTEGER Date ::= [APPLICATION 3] IMPLICIT IA5String -- YYYYMMDD +-- Suggested simple encoding test +PDU1 ::= --snacc isPdu:"TRUE" -- SEQUENCE +{ + height INTEGER(0..255), + width INTEGER(0..255) +} + END diff --git a/c-examples/simple/minbuf-ex.c b/c-examples/simple/minbuf-ex.c index 9b8b334..5b9c769 100644 --- a/c-examples/simple/minbuf-ex.c +++ b/c-examples/simple/minbuf-ex.c @@ -65,12 +65,17 @@ main PARAMS ((argc, argv), AsnLen decodedLen; int val; PersonnelRecord pr; + PDU1 p; int size; char *origData; struct stat sbuf; jmp_buf env; int decodeErr; char *filename; + char expected_pdu_bytes[] = { 0x30, /* SEQUENCE */ + 0x06, /* LENGTH (6-bytes, def) */ + 0x02, 0x01, 0x12, /*INT - 18*/ + 0x02, 0x01, 0x30 /*INT - 48*/}; if (argc != 2) { filename = "pr.ber"; @@ -128,15 +133,37 @@ main PARAMS ((argc, argv), */ encBufSize = size + 512; encData = (char*) malloc(encBufSize); + memset(encData, 0, encBufSize); /* * set 'buffer' up for writing by setting ptr * byte after last byte of the block */ + p.height = 18; + p.width = 48; GenBufFromMinBuf(&encBuf, encData + encBufSize); - (void)BEncPersonnelRecord(&encBuf, &pr); + decodedLen = BEncPDU1(&encBuf, &p); + + size = 0; + for (int i = encBufSize - decodedLen; i < encBufSize; i++, size++) { + if (encData[i] != expected_pdu_bytes[size]) { + printf("enc cmp failed - byte: %d [0x%02x vs 0x%02x]\n", + i, encData[i], expected_pdu_bytes[size]); + exit(-1); + } + } free(encData); free(origData); return 0; } + + + + + + + + + + diff --git a/c-lib/src/min-buf.c b/c-lib/src/min-buf.c index e1d57a8..870cedd 100644 --- a/c-lib/src/min-buf.c +++ b/c-lib/src/min-buf.c @@ -60,6 +60,7 @@ MinBufPutSegRvs__(void *b, char *src, unsigned long len) { unsigned char *dst = (unsigned char *)(*(unsigned char **)b); dst -= len; + *(unsigned char **)b = dst; memcpy(dst, src, len); } -- 2.14.3