In this post, we will continue our Itevad Protocol Binary Ber Decoder. With the help of ANS.1 compiler, not only do we get the structural description of Itevad protocol itself but also the necessary converting functions used to convert between ASN.1 types and C basic data types, as well as the corresponding encoder and decoder function calls per different binary encoding ways, like Per, Ber and Xer. Let’s check the code!
/* Ber decoder for Binary Itevad Protocol */ [root@localhost Itevad2]# cat itevadBerDecoder.c /* Itevad Protocol Binary Ber Decoder Example April 1, 2012 http://daveti.blog.com Reference: Using the Open Source ASN.1 Compiler by Lev Walkin */ #include <stdio.h> #include <sys/types.h> #include "ItevadMessage.h" int main( int ac, char **av) { char buf[ 1024]; /* Temp buffer */ ItevadMessage_t *itevadMsg = NULL; /* Type to decode */ asn_dec_rval_t rval; /* Decoder return value */ FILE *fp; /* Input file handler */ size_t size; /* Number of bytes read */ char *filename; /* Input file name */ /* Require a single filename argument */ if ( ac != 2) { fprintf( stderr, "Usage: %s <file.ber>n", av[ 0]); exit(64); /* better, EX_USAGE */ } else { filename = av[ 1]; } /* Open input file as read-only binary */ fp = fopen( filename, "rb"); if ( !fp) { perror( filename); exit(66); /* better, EX_NOINPUT */ } /* Read up to the buffer size */ size = fread( buf, 1, sizeof( buf), fp); fclose( fp); if ( !size) { fprintf( stderr, "%s: Empty or brokenn", filename); exit(65); /* better, EX_DATAERR */ } /* Decode the input buffer as Itevad type */ rval = ber_decode( 0, &asn_DEF_ItevadMessage, (void **)&itevadMsg, buf, size); if ( rval.code != RC_OK) { fprintf( stderr, "%s: Broken ItevadMessage decoding at byte %ldn", filename, (long)rval.consumed); exit(65); /* better, EX_DATAERR */ } /* Print the decoded Itevad type as XML */ xer_fprint( stdout, &asn_DEF_ItevadMessage, itevadMsg); /* Decoding finished successfully */ return 0; } [root@localhost itevad2]# /* Generate and run itevadBerDecoderBin */ [root@localhost itevad2]# gcc -I. -o itevadBerDecoderBin *.c [root@localhost Itevad2]# ./itevadBerDecoderBin itevad_binary.msg <ItevadMessage> <version>1</version> <ip4Address> <address>31 31 31 31</address> <portNumber>7777</portNumber> </ip4Address> <messageBody> <messageRequest> <transactionId>65535</transactionId> <askContent>who r you?</askContent> </messageRequest> </messageBody> </ItevadMessage> [root@localhost itevad2]#