
Sample C application which calls ossPrintPER three times with various flag settings (prntper.c):
/*****************************************************************************/
/* Copyright (C) 1999-2002 OSS Nokalva, Inc. All rights reserved */
/*****************************************************************************/
/* Application Program: prntper.c */
/* encodes a sample baseball card */
/* using BCAS (Baseball Card Abstract Syntax) */
/* illustrate customization of printper output */
#include <stdio.h>
#include "bcas.h" /* compiler-generated header file */
#include "printasn.h"
/* Sample ossPrintPER hook */
ossBoolean DLL_ENTRY_FDEF
ossPrintPER_SampleHook(struct ossGlobal *world,





struct PrintPerRecord *record);
/* The following encodes a card. If the card is successfully encoded, decodes it. */
int main()
{
OssBuf encodedData; 
/* length and address of encoded data */
BBCard *myCardPtr = NULL;
/* address of decoded data */
int pdu_num = 0;
struct ossGlobal w, *world = &w;
char *buf; 
/* saves address of encoded data */
long buf_length;
/* saves length of encoded data */
int retcode;
/* return code */
ossinit(world, bcas);
ossSetEncodingFlags(world, DEBUGPDU);
ossSetDecodingFlags(world, DEBUGPDU);
/* Print input to encoder */
ossPrint(world, "\nThe input to the encoder ...\n\n");
ossPrintPDU(world, BBCard_PDU, &myCard);
/* Encode a card. Return non-zero for failure. */
ossPrint(world,

"\nThe encoder's trace messages (none for time-optimized) ...\n\n");
/* Encode a card. Return non-zero for failure. */
encodedData.value = NULL;
encodedData.length = 0;
if (ossEncode(world, BBCard_PDU, &myCard, &encodedData)) {

ossPrint(world, "%s\n", ossGetErrMsg(world));

return 1;
} else {

ossPrint(world, "Card encoded\n");
/* card was successfully encoded */

buf = (char *)encodedData.value;
/* saves buffer address







* with encoded data. */

buf_length = (long) encodedData.length; /* saves the length of *







* encoded data */

/* Decode a card whose encoding is in encodedData. Return non-zero

* for failure. */

ossPrint(world, "\n\n The default ossPrintPER output\n\n");

/* Call ossPrintPER without any additional flags set*/

if (ossPrintPER(world, &pdu_num, &encodedData, NULL, 0, NULL)) {


ossPrint(world, "%s\n", ossGetErrMsg(world));


retcode = 1;

} else {


/* Restore encoded data buffer */


encodedData.length = buf_length;


encodedData.value = (unsigned char *) buf;


/* Call ossPrintPER with application hook provided */


ossPrint(world, "\n\nThe ossPrintPER output when the application");


ossPrint(world, " hook is provided\n\n");


if (ossPrintPER(world, &pdu_num, &encodedData, NULL, 0,






ossPrintPER_SampleHook)) {



ossPrint(world, "%s\n", ossGetErrMsg(world));



retcode = 1;


} else {



/* Restore encoded data buffer */



encodedData.length = buf_length;



encodedData.value = (unsigned char *) buf;



/* Call ossPrintPER with additonal flags set *



* to customize its output */



ossPrint(world, "\n\nThe customized ossPrintPER output\n\n");



if (ossPrintPER(world, &pdu_num, &encodedData, NULL,




OSS_HEXBYTES + OSS_PRINT_ABSREF + OSS_PRINT_OFFSET +




OSS_PRINT_TYPE_INFO + OSS_PRINT_COMMENTS,




NULL)) {




ossPrint(world, "%s\n", ossGetErrMsg(world));




retcode = 1;



} else {




retcode = 0;



}


}

}

/* Free encoder's output buffer*/

ossFreeBuf(world, buf);
}

/*

* Call ossterm() to free all resources

*/
ossterm(world);
return retcode;
}
ossBoolean DLL_ENTRY_FDEF ossPrintPER_SampleHook(struct ossGlobal *world,





struct PrintPerRecord *record)
{
char *constructed = "";
int index, index1;
switch (record->typeId) {

case OSS_CHOICE_BEGIN_PPR:

case OSS_SEQ_SET_BEGIN_PPR:


constructed = "constructed ";

case OSS_ASN1_TYPE_PPR:


if (record->qualifiedName) {



ossPrint(world, "Decoding of %stype \"%s\" started\n",




constructed, record->qualifiedName);


} else {



ossPrint(world, "Decoding of PDU \"%s\" started\n",




record->typeName);


}


break;

case OSS_CHOICE_FINISHED_PPR:

case OSS_SEQ_SET_FINISHED_PPR:


if (record->qualifiedName)



ossPrint(world, "Decoding of constructed type \"%s\" finished\n",




record->qualifiedName);


else



ossPrint(world, "Decoding of PDU \"%s\" finished\n",




record->typeName);


return TRUE;

case OSS_VERSION_BRACKETS_BEGIN_PPR:


ossPrint(world, "Decoding of version brackets started\n");


return TRUE;

case OSS_VERSION_BRACKETS_FINISHED_PPR:


ossPrint(world, "Decoding of version brackets finished\n");


return TRUE;

case OSS_ADDITIONS_TYPE_PPR:

default:


break;
};
if (record->decodedContent)
ossPrint(world, "(Decoded value is: %s)\n", record->decodedContent);
for (index = 0; index < record->numberOfSimplestRecord; index++) {
struct PrintPerSimplestRecord *simplest_record =

record->simplestRecords + index;
char *kind_name;
switch (simplest_record->typeId) {

case OSS_CONTENTS_TYPE_PPR:


kind_name = "contents";


break;

case OSS_LENGTH_TYPE_PPR:


kind_name = "length";


break;

case OSS_PADDING_TYPE_PPR:


kind_name = "padding";


break;

case OSS_EXTENSION_LENGTH_TYPE_PPR:


kind_name = "extension length";


break;

case OSS_EXTENSION_RANGE_TYPE_PPR:


kind_name = "extension range";


break;

case OSS_CHOICE_INDEX_PPR:


kind_name = "choice index";


break;

case OSS_PDU_PADDING_TYPE_PPR:


kind_name = "PDU padding";


break;

case OSS_REALINF_TYPE_PPR:


kind_name = "type of real number encoding";


break;

case OSS_EXPLENGTH_TYPE_PPR:


kind_name = "exponent length";


break;

case OSS_EXP_TYPE_PPR:


kind_name = "exponent";


break;

case OSS_MANTISSA_TYPE_PPR:


kind_name = "mantissa";


break;

case OSS_LENGTH_LEADING_BIT_PPR:


kind_name = "length type bit";


break;

case OSS_EXTENSION_FLAG_TYPE_PPR:


kind_name = "extension flag";


break;

case OSS_EXTENSION_COUNT_TYPE_PPR:


kind_name = "extension preamble length";


break;

case OSS_EXTENSION_PREAMBLE_TYPE_PPR:


kind_name = "extension preamble";


break;

case OSS_PREAMBLE_TYPE_PPR:


kind_name = "preamble";


break;

case OSS_TRAILING_BITS_PPR:


kind_name = "trailing bits";


break;

case OSS_EXT_COUNT_SIZE_FLAG_PPR:


kind_name = "extension preamble size flag";


break;

case OSS_EXT_COUNT_SIZE_PPR:


kind_name = "extension preamble length";


break;

case OSS_CHOICE_INDEX_SIZE_FLAG_PPR:


kind_name = "choice index size flag";


break;

case OSS_CHOICE_INDEX_LENGTH_PPR:


kind_name = "choice index length";


break;

default:


kind_name = "<unknown>";

}

ossPrint(world, "Decoded item:\n");

ossPrint(world, " TypeId: %s\n",



kind_name);

ossPrint(world, " Length of encoding: %d (counted in bits)\n",



simplest_record->length);

ossPrint(world, " Content of encoding: ");

for (index1 = 0; index1 < simplest_record->length; index1++) {


int number_of_byte_to_print =



(simplest_record->encodedBitOffset + index1) / 8;


int number_of_bit_to_print =



(simplest_record->encodedBitOffset + index1) % 8;


unsigned char bit_value =



simplest_record->encodedContent[number_of_byte_to_print] &




(0x80 >> number_of_bit_to_print);


ossPrint(world, "%d", bit_value ? 1 : 0);


/* To avoid huge output; a demoinstration only is needed */


if (index1 > 60) {



ossPrint(world, "...");



break;


}

}

ossPrint(world, "\n");
}
if (record->typeId == OSS_ASN1_TYPE_PPR) {

if (record->qualifiedName)


ossPrint(world, "Decoding of type \"%s\" finished\n",



record->qualifiedName);

else


ossPrint(world, "Decoding of PDU \"%s\" finished\n",


record->typeName);
}
return TRUE;
}
Copyright © 2010 OSS Nokalva, Inc. All Rights Reserved.