]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-examples/any/example.C
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-examples / any / example.C
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 // c++_examples/any/example.C - an example of how to use C++ ASN.1-BER
20 // for ANY Types
21 //
22 // AUTHOR: Mike Sample
23 // DATE: 92
24 //
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
29 //
30 // Revision 1.2 2001/05/05 00:59:17 rmurphy
31 // Adding darwin license headers
32 //
33 // Revision 1.1.1.1 1999/03/16 18:05:57 aram
34 // Originals from SMIME Free Library.
35 //
36 // Revision 1.5 1995/07/24 15:33:33 rj
37 // changed `_' to `-' in file names.
38 //
39 // any-test.[hC] becomes any.[hC] due to to snacc's new file name generation scheme.
40 //
41 // check return value of new.
42 //
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.
45 //
46 // Revision 1.3 1994/10/08 01:26:21 rj
47 // several \size_t'
48 //
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.
51 //
52
53 #include <stddef.h>
54 #include <stdlib.h>
55 #include <errno.h>
56 #include <fstream.h>
57
58 #include "asn-incl.h"
59 #include "any.h"
60
61 main (int argc, char *argv[])
62 {
63 if (argc != 2)
64 {
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;
68 exit (1);
69 }
70
71 ifstream dataFile;
72 // open the data file
73 dataFile.open (argv[1]);
74
75 if (!dataFile)
76 {
77 perror ("ifstream::open");
78 exit (1);
79 }
80
81 // get size of the data file file
82 dataFile.seekg (0, ios::end);
83 int dataSize = dataFile.tellg();
84 dataFile.seekg (0);
85
86 // read data from file into contiguous block for a buffer
87 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
88 char data[dataSize];
89 #else
90 char *data = new char[dataSize];
91 if (!data)
92 return 1;
93 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
94 dataFile.read (data, dataSize);
95 dataFile.close();
96
97 //
98 // put the BER data read from the file
99 // into buffer format, ready for reading from the
100 // beginning
101 //
102 AsnBuf inputBuf;
103 inputBuf.InstallData ((char*)data, dataSize);
104
105 size_t decodedLen;
106 AnyTestType att;
107
108 if (!att.BDecPdu (inputBuf, decodedLen))
109 {
110 cerr << "ERROR - Decode routines failed, exiting..." << endl;
111 exit (1);
112 }
113
114 cerr << "decodedValue AnyTestType ::= " << att << endl << endl;
115
116 //
117 // allocate a new buffer and set up for writing to
118 //
119 AsnBuf outputBuf;
120 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
121 char outputData[dataSize + 512];
122 #else
123 char *outputData = new char[dataSize + 512];
124 if (!outputData)
125 return 1;
126 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
127 outputBuf.Init (outputData, dataSize+512);
128 outputBuf.ResetInWriteRvsMode();
129
130 size_t encodedLen;
131 if (!att.BEncPdu (outputBuf, encodedLen))
132 {
133 cerr << "ERROR - Encode routines failed" << endl;
134 }
135
136 // write the BER value to cout
137 outputBuf.ResetInReadMode();
138 for ( ; encodedLen > 0; encodedLen--)
139 cout.put (outputBuf.GetByte());
140
141 return 0;
142 }