2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 // c++_examples/simple/example.C - an example of how to use C++ ASN.1-BER
20 // encoders and decoders generated by snacc
22 // AUTHOR: Mike Sample
25 // $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c++-examples/simple/example.C,v 1.1.1.1 2001/05/18 23:14:05 mb Exp $
26 // $Log: example.C,v $
27 // Revision 1.1.1.1 2001/05/18 23:14:05 mb
28 // Move from private repository to open source repository
30 // Revision 1.2 2001/05/05 00:59:17 rmurphy
31 // Adding darwin license headers
33 // Revision 1.1.1.1 1999/03/16 18:05:57 aram
34 // Originals from SMIME Free Library.
36 // Revision 1.5 1995/07/24 15:36:03 rj
37 // check return value of new.
39 // changed `_' to `-' in file names.
41 // Revision 1.4 1995/02/18 13:54:18 rj
42 // added #define HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS since not every C++ compiler provides them.
44 // Revision 1.3 1994/10/08 01:27:02 rj
47 // Revision 1.2 1994/08/31 08:56:32 rj
48 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
59 main (int argc, char *argv[])
71 cerr << "Usage: " << argv[0] << " <BER data file name>" << endl;
72 cerr << " Decodes the given PersonnelRecord BER data file" << endl;
73 cerr << " and re-encodes it to stdout" << endl;
79 dataFile.open (argv[1]);
83 perror ("ifstream::open");
87 // get size of the data file file
88 dataFile.seekg (0, ios::end);
89 dataSize = dataFile.tellg();
92 // read data from file into contiguous block for a buffer
93 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
96 char *data = new char[dataSize];
99 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
100 dataFile.read (data, dataSize);
104 // put the BER data read from the file
105 // into buffer format, ready for reading from the
108 inputBuf.InstallData (data, dataSize);
110 if (!pr.BDecPdu (inputBuf, decodedLen))
112 cerr << "--- ERROR - Decode routines failed, exiting..." << endl;
116 cerr << "decodedValue PersonnelRecord ::= " << pr << endl << endl;
119 // allocate a new buffer set up for writing to
121 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
122 char outputData[dataSize + 512];
124 char *outputData = new char[dataSize + 512];
127 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
128 outputBuf.Init (outputData, dataSize+512);
129 outputBuf.ResetInWriteRvsMode();
131 if (!pr.BEncPdu (outputBuf, encodedLen))
133 cerr << "--- ERROR - Encode routines failed" << endl;
136 // write the BER value to cout
137 outputBuf.ResetInReadMode();
138 for (; encodedLen > 0; encodedLen--)
139 cout.put (outputBuf.GetByte());