]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-examples/any/genber.C
2581f79864b4675e5cf430ef3ecbb5cb0d530d8e
[apple/security.git] / SecuritySNACCRuntime / c++-examples / any / genber.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/genber.C - builds an AnyTestType value and writes BER form
20 // of the value to a file called "att.ber"
21 //
22 // Shows how to build internal rep of lists and ANY values.
23 //
24 // MS 92
25 //
26 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-examples/any/genber.C,v 1.1.1.1 2001/05/18 23:14:05 mb Exp $
27 // $Log: genber.C,v $
28 // Revision 1.1.1.1 2001/05/18 23:14:05 mb
29 // Move from private repository to open source repository
30 //
31 // Revision 1.3 2001/05/05 00:59:17 rmurphy
32 // Adding darwin license headers
33 //
34 // Revision 1.2 2000/06/08 19:58:44 dmitch
35 // Mods for X port.
36 //
37 // Revision 1.1.1.1 1999/03/16 18:05:57 aram
38 // Originals from SMIME Free Library.
39 //
40 // Revision 1.5 1995/07/24 15:33:34 rj
41 // changed `_' to `-' in file names.
42 //
43 // any-test.[hC] becomes any.[hC] due to to snacc's new file name generation scheme.
44 //
45 // check return value of new.
46 //
47 // Revision 1.4 1995/02/18 13:54:03 rj
48 // added #define HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS since not every C++ compiler provides them.
49 //
50 // Revision 1.3 1994/10/08 01:26:22 rj
51 // several \size_t'
52 //
53 // Revision 1.2 1994/08/31 08:56:30 rj
54 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
55 //
56
57
58 #include <stddef.h>
59 #include <stdlib.h>
60 #include <errno.h>
61 #include <fstream.h>
62
63 #include "asn-incl.h"
64 #include "any.h"
65
66 #define APPLE_ANY_HACK 1
67
68 main (int argc, char *argv[])
69 {
70 ofstream outputFile;
71 AsnBuf outputBuf;
72 size_t encodedLen;
73 size_t dataSize = 1024;
74 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
75 char data[dataSize];
76 #else
77 char *data = new char[dataSize];
78 if (!data)
79 return 1;
80 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
81 AnyTestType att;
82 TSeq1 ts1;
83 TSeq2 ts2;
84 AttrValue1 *atv1ptr;
85 AttrValue2 *atv2ptr;
86 AsnInt intVal;
87 AsnBool boolVal;
88 AsnOcts octsVal ("Hi Mom");
89 OctsId octsIdVal = octsVal;
90 AsnBits bitsVal;
91 BitsId bitsIdVal (9);
92 AsnReal realVal;
93
94 // READ THIS!!!
95 // you must be really careful when setting the
96 // "value" field and "id" fields in an
97 // ANY/ANY DEFINED BY type because "value" is a
98 // "AsnType*" and will accept any
99 // pointer value. It will even encode
100 // the wrong value without complaining if you
101 // set "value" to the wrong object.
102
103 atv1ptr = att.intMap.Append();
104 atv1ptr->id = intId;
105 intVal = -99;
106 #if APPLE_ANY_HACK
107 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&intVal);
108 #else
109 atv1ptr->anyDefBy.value = &intVal;
110 #endif
111 atv1ptr = att.intMap.Append();
112 atv1ptr->id = boolId;
113 boolVal = true;
114 #if APPLE_ANY_HACK
115 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&boolVal);
116 #else
117 atv1ptr->anyDefBy.value = &boolVal;
118 #endif
119
120 atv1ptr = att.intMap.Append();
121 atv1ptr->id = octsId;
122 #if APPLE_ANY_HACK
123 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&octsIdVal);
124 #else
125 atv1ptr->anyDefBy.value = &octsIdVal;
126 #endif
127
128 atv1ptr = att.intMap.Append();
129 atv1ptr->id = bitsId;
130 bitsIdVal.SetBit (0);
131 bitsIdVal.ClrBit (1);
132 bitsIdVal.SetBit (2);
133 bitsIdVal.ClrBit (3);
134 bitsIdVal.SetBit (4);
135 bitsIdVal.ClrBit (5);
136 bitsIdVal.SetBit (6);
137 bitsIdVal.ClrBit (7);
138 bitsIdVal.SetBit (8);
139 bitsIdVal.ClrBit (9);
140 #if APPLE_ANY_HACK
141 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsIdVal);
142 #else
143 atv1ptr->anyDefBy.value = &bitsIdVal;
144 #endif
145
146 atv1ptr = att.intMap.Append();
147 atv1ptr->id = realId;
148 realVal = 108.3838;
149 #if APPLE_ANY_HACK
150 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&realVal);
151 #else
152 atv1ptr->anyDefBy.value = &realVal;
153 #endif
154
155 // now do TSeq2 with same vals but use OID as identifier
156 atv2ptr = att.oidMap.Append();
157 atv2ptr->id = intOid;
158 #if APPLE_ANY_HACK
159 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&intVal);
160 #else
161 atv2ptr->anyDefBy.value = &intVal;
162 #endif
163
164 atv2ptr = att.oidMap.Append();
165 atv2ptr->id = boolOid;
166 #if APPLE_ANY_HACK
167 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&boolVal);
168 #else
169 atv2ptr->anyDefBy.value = &boolVal;
170 #endif
171
172 atv2ptr = att.oidMap.Append();
173 atv2ptr->id = octsOid;
174 #if APPLE_ANY_HACK
175 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&octsVal);
176 #else
177 atv2ptr->anyDefBy.value = &octsVal;
178 #endif
179
180 atv2ptr = att.oidMap.Append();
181 atv2ptr->id = bitsOid;
182 bitsVal = bitsIdVal; // copy bits
183 #if APPLE_ANY_HACK
184 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsVal);
185 #else
186 atv2ptr->anyDefBy.value = &bitsVal;
187 #endif
188
189 atv2ptr = att.oidMap.Append();
190 atv2ptr->id = realOid;
191 #if APPLE_ANY_HACK
192 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsVal);
193 #else
194 atv2ptr->anyDefBy.value = &bitsVal;
195 #endif
196
197 outputBuf.Init (data, dataSize);
198 outputBuf.ResetInWriteRvsMode();
199
200 if (!att.BEncPdu (outputBuf, encodedLen))
201 cout << "failed encoding AnyTestType value" << endl;
202
203 outputFile.open ("att.ber");
204 if (!outputFile)
205 {
206 perror ("ofstream::open");
207 exit (1);
208 }
209
210 outputBuf.ResetInReadMode();
211 for ( ; encodedLen > 0; encodedLen--)
212 outputFile.put (outputBuf.GetByte());
213
214
215 cout << "Wrote the following BER AnyTestType value to att.ber." << endl;
216 cout << "Test it with \"def\" and \"indef\"" << endl;
217 //cout << att << endl;
218
219 return 0;
220 }