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/any/example.C - an example of how to use C++ ASN.1-BER
22 // AUTHOR: Mike Sample
25 // $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c++-examples/any/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:33:33 rj
37 // changed `_' to `-' in file names.
39 // any-test.[hC] becomes any.[hC] due to to snacc's new file name generation scheme.
41 // check return value of new.
43 // Revision 1.4 1995/02/18 13:53:07 rj
44 // added #define HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS since not every C++ compiler provides them.
46 // Revision 1.3 1994/10/08 01:26:21 rj
49 // Revision 1.2 1994/08/31 08:56:29 rj
50 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
61 main (int argc, char *argv[])
65 cerr << "Usage: " << argv[0] << " <BER data file name>" << endl;
66 cerr << " Decodes the given AnyTestType BER data file" << endl;
67 cerr << " and re-encodes it to stdout" << endl;
73 dataFile.open (argv[1]);
77 perror ("ifstream::open");
81 // get size of the data file file
82 dataFile.seekg (0, ios::end);
83 int dataSize = dataFile.tellg();
86 // read data from file into contiguous block for a buffer
87 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
90 char *data = new char[dataSize];
93 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
94 dataFile.read (data, dataSize);
98 // put the BER data read from the file
99 // into buffer format, ready for reading from the
103 inputBuf.InstallData ((char*)data, dataSize);
108 if (!att.BDecPdu (inputBuf, decodedLen))
110 cerr << "ERROR - Decode routines failed, exiting..." << endl;
114 cerr << "decodedValue AnyTestType ::= " << att << endl << endl;
117 // allocate a new buffer and set up for writing to
120 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
121 char outputData[dataSize + 512];
123 char *outputData = new char[dataSize + 512];
126 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
127 outputBuf.Init (outputData, dataSize+512);
128 outputBuf.ResetInWriteRvsMode();
131 if (!att.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());