]> git.saurik.com Git - apple/security.git/blame - SecuritySNACCRuntime/c++-examples/simple/example.C
Security-54.1.9.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-examples / simple / example.C
CommitLineData
bac41a7b
A
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/simple/example.C - an example of how to use C++ ASN.1-BER
20// encoders and decoders generated by snacc
21//
22// AUTHOR: Mike Sample
23// DATE: Aug 92
24//
a66d0d4a 25// $Header: /cvs/root/Security/SecuritySNACCRuntime/c++-examples/simple/Attic/example.C,v 1.1.1.1 2001/05/18 23:14:05 mb Exp $
bac41a7b
A
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:36:03 rj
37// check return value of new.
38//
39// changed `_' to `-' in file names.
40//
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.
43//
44// Revision 1.3 1994/10/08 01:27:02 rj
45// several \size_t'
46//
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.
49//
50
51#include <stddef.h>
52#include <stdlib.h>
53#include <errno.h>
54#include <fstream.h>
55#include "asn-incl.h"
56#include "p-rec.h"
57
58
59main (int argc, char *argv[])
60{
61 AsnBuf inputBuf;
62 AsnBuf outputBuf;
63 size_t encodedLen;
64 size_t decodedLen;
65 size_t dataSize;
66 ifstream dataFile;
67 PersonnelRecord pr;
68
69 if (argc != 2)
70 {
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;
74 exit (1);
75 }
76
77
78 // open the data file
79 dataFile.open (argv[1]);
80
81 if (!dataFile)
82 {
83 perror ("ifstream::open");
84 exit (1);
85 }
86
87 // get size of the data file file
88 dataFile.seekg (0, ios::end);
89 dataSize = dataFile.tellg();
90 dataFile.seekg (0);
91
92 // read data from file into contiguous block for a buffer
93#if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
94 char data[dataSize];
95#else
96 char *data = new char[dataSize];
97 if (!data)
98 return 1;
99#endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
100 dataFile.read (data, dataSize);
101 dataFile.close();
102
103 //
104 // put the BER data read from the file
105 // into buffer format, ready for reading from the
106 // beginning
107 //
108 inputBuf.InstallData (data, dataSize);
109
110 if (!pr.BDecPdu (inputBuf, decodedLen))
111 {
112 cerr << "--- ERROR - Decode routines failed, exiting..." << endl;
113 exit (1);
114 }
115
116 cerr << "decodedValue PersonnelRecord ::= " << pr << endl << endl;
117
118 //
119 // allocate a new buffer set up for writing to
120 //
121#if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
122 char outputData[dataSize + 512];
123#else
124 char *outputData = new char[dataSize + 512];
125 if (!outputData)
126 return 1;
127#endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
128 outputBuf.Init (outputData, dataSize+512);
129 outputBuf.ResetInWriteRvsMode();
130
131 if (!pr.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}