File: example.c

package info (click to toggle)
esnacc 1.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,716 kB
  • sloc: ansic: 42,342; cpp: 13,880; yacc: 2,682; tcl: 1,587; lex: 688; sh: 573; makefile: 104
file content (124 lines) | stat: -rw-r--r-- 3,239 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// c++_examples/simple/example.C - an example of how to use  C++ ASN.1-BER
//             encoders and decoders generated by snacc
//
// AUTHOR: Mike Sample
// DATE:   Aug 92
//
// $Header: /baseline/SNACC/c-examples/simple/example.c,v 1.2 2003/12/17 19:05:03 gronej Exp $
// $Log: example.c,v $
// Revision 1.2  2003/12/17 19:05:03  gronej
// SNACC baseline merged with PER v1_7 tag
//
// Revision 1.1.2.1  2003/11/05 14:58:57  gronej
// working PER code merged with esnacc_1_6
//
// Revision 1.1.1.1  2000/08/21 20:36:07  leonberp
// First CVS Version of SNACC.
//
// Revision 1.5  1995/07/24 15:36:03  rj
// check return value of new.
//
// changed `_' to `-' in file names.
//
// Revision 1.4  1995/02/18  13:54:18  rj
// added #define HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS since not every C++ compiler provides them.
//
// Revision 1.3  1994/10/08  01:27:02  rj
// several \size_t'
//
// Revision 1.2  1994/08/31  08:56:32  rj
// first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
//

#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <fstream.h>
#include "asn-incl.h"
#include "p-rec.h"


main (int argc, char *argv[])
{
    AsnBuf  inputBuf;
    AsnBuf  outputBuf;
    size_t encodedLen;
    size_t decodedLen;
    size_t      dataSize;
    ifstream dataFile;
    PersonnelRecord pr;

    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " <BER data file name>" << endl;
        cerr << "   Decodes the given PersonnelRecord BER data file" << endl;
        cerr << "   and re-encodes it to stdout" << endl;
        exit (1);
    }


    // open the data file
    dataFile.open (argv[1]);

    if (!dataFile)
    {
        perror ("ifstream::open");
        exit (1);
    }

    // get size of the data file file
    dataFile.seekg (0, ios::end);
    dataSize = dataFile.tellg();
    dataFile.seekg (0);

    // read data from file into contiguous block for a buffer
#if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
    char data[dataSize];
#else
    char *data = new char[dataSize];
    if (!data)
	return 1;
#endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
    dataFile.read (data, dataSize);
    dataFile.close();

    //
    // put the BER data read from the file
    // into buffer format, ready for reading from the
    // beginning
    //
    inputBuf.InstallData (data, dataSize);

    if (!pr.BDecPdu (inputBuf, decodedLen))
    {
        cerr << "--- ERROR - Decode routines failed, exiting..." << endl;
        exit (1);
    }

    cerr  << "decodedValue PersonnelRecord ::= " << pr << endl << endl;

    //
    // allocate a new buffer set up for writing to
    //
#if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
    char outputData[dataSize + 512];
#else
    char *outputData = new char[dataSize + 512];
    if (!outputData)
	return 1;
#endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
    outputBuf.Init (outputData, dataSize+512);
    outputBuf.ResetInWriteRvsMode();

    if (!pr.BEncPdu (outputBuf, encodedLen))
    {
        cerr << "--- ERROR - Encode routines failed" << endl;
    }

    // write the BER value to cout
    outputBuf.ResetInReadMode();
    for (; encodedLen > 0; encodedLen--)
        cout.put (outputBuf.GetByte());

    return 0;
}