[esnacc-dev] [PATCH 2/2] min-buf: Fix "encoding" put

Aaron Conole aconole at bytheb.org
Fri Feb 2 19:24:40 UTC 2018


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 <aconole at bytheb.org>
---
 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



More information about the dev mailing list