How do I decode multiple concatenated PDUs when using the file memory manager?

The procedure involved in decoding multiple concatenated PDUs when using the file memory manager is similar to the procedure when using the default plain memory manager. However, the difference is that the length field of the instance of OssBuf contains not the number of bytes remaining to be decoded, but the current file offset position. Likewise, the value field holds a reference to a file and does not need to be advanced.

Starting from version 5.0.0, the current file offset position is not updated automatically by the decoder. Thus when decoding multiple concatenated PDUs, make sure to update this manually. An example that includes an illustration of just this is included in the answer to: How can I encode/decode concatenated PDUs to or from a file?

The following example illustrates the manual update of the length field of a file-marked instance of OssBuf:

             
OssBuf encodedData;
int rc, myPduNum, byteCount;
void *decodedDataPtr = NULL;
char *sourceFilename; /* Name of the file with encoded PDUs */
long remainingBytes;
. . . .
encodedData.length = 0;
encodedData.value = ossMarkObj(world, OSS_FILE,
 (void *)sourceFilename);
remainingBytes = getFileLength(sourceFilename);
myPduNum = MyASN1DataType_PDU;

while (remainingBytes > 0) {
   decodedDataPtr=NULL;
   if (ossDecode(world, &myPduNum,
            &encodedData, (void**)&decodedDataPtr)) {
      ossPrint(world, "Decoder Error: %s\n",       
            ossGetErrMsg(world));
      break;
   }
   processDecodedData(decodedDataPtr);
   ossFreePDU(world, myPduNum, decodedDataPtr);
   byteCount = ossGetBytesReadByDecoder(world);
   encodedData.length += byteCount;
   remainingBytes -= byteCount;
}

The getFileLength() function above may be defined as follows:

static long getFileLength(char *fname)
{

   FILE *encFile = NULL;
   long fsize = 0;


if (!(encFile = fopen(fname, "rb")) ||
      fseek(encFile, 0, SEEK_END) ||
      (fsize = ftell(encFile)) < 0 || fseek(encFile, 0, 
    SEEK_SET))
      fsize = 0;
   if (encFile)
      fclose(encFile);
   return fsize;
}

If the -compat decoderUpdatesInputAddress compiler option is specified or a version of the runtime prior to v5.0.0 is in use, the decoder automatically updates the current file position held in the length field of the input instance of OssBuf.


The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.



Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email: support@oss.com
Free Trial
download

Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.




Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.