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/genber.C - builds an AnyTestType value and writes BER form
20 // of the value to a file called "att.ber"
22 // Shows how to build internal rep of lists and ANY values.
26 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-examples/any/genber.C,v 1.1.1.1 2001/05/18 23:14:05 mb Exp $
28 // Revision 1.1.1.1 2001/05/18 23:14:05 mb
29 // Move from private repository to open source repository
31 // Revision 1.3 2001/05/05 00:59:17 rmurphy
32 // Adding darwin license headers
34 // Revision 1.2 2000/06/08 19:58:44 dmitch
37 // Revision 1.1.1.1 1999/03/16 18:05:57 aram
38 // Originals from SMIME Free Library.
40 // Revision 1.5 1995/07/24 15:33:34 rj
41 // changed `_' to `-' in file names.
43 // any-test.[hC] becomes any.[hC] due to to snacc's new file name generation scheme.
45 // check return value of new.
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.
50 // Revision 1.3 1994/10/08 01:26:22 rj
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.
66 #define APPLE_ANY_HACK 1
68 main (int argc, char *argv[])
73 size_t dataSize = 1024;
74 #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS
77 char *data = new char[dataSize];
80 #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */
88 AsnOcts octsVal ("Hi Mom");
89 OctsId octsIdVal = octsVal;
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.
103 atv1ptr = att.intMap.Append();
107 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&intVal);
109 atv1ptr->anyDefBy.value = &intVal;
111 atv1ptr = att.intMap.Append();
112 atv1ptr->id = boolId;
115 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&boolVal);
117 atv1ptr->anyDefBy.value = &boolVal;
120 atv1ptr = att.intMap.Append();
121 atv1ptr->id = octsId;
123 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&octsIdVal);
125 atv1ptr->anyDefBy.value = &octsIdVal;
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);
141 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsIdVal);
143 atv1ptr->anyDefBy.value = &bitsIdVal;
146 atv1ptr = att.intMap.Append();
147 atv1ptr->id = realId;
150 atv1ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&realVal);
152 atv1ptr->anyDefBy.value = &realVal;
155 // now do TSeq2 with same vals but use OID as identifier
156 atv2ptr = att.oidMap.Append();
157 atv2ptr->id = intOid;
159 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&intVal);
161 atv2ptr->anyDefBy.value = &intVal;
164 atv2ptr = att.oidMap.Append();
165 atv2ptr->id = boolOid;
167 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&boolVal);
169 atv2ptr->anyDefBy.value = &boolVal;
172 atv2ptr = att.oidMap.Append();
173 atv2ptr->id = octsOid;
175 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&octsVal);
177 atv2ptr->anyDefBy.value = &octsVal;
180 atv2ptr = att.oidMap.Append();
181 atv2ptr->id = bitsOid;
182 bitsVal = bitsIdVal; // copy bits
184 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsVal);
186 atv2ptr->anyDefBy.value = &bitsVal;
189 atv2ptr = att.oidMap.Append();
190 atv2ptr->id = realOid;
192 atv2ptr->anyDefBy.value = reinterpret_cast<CSM_Buffer *>(&bitsVal);
194 atv2ptr->anyDefBy.value = &bitsVal;
197 outputBuf.Init (data, dataSize);
198 outputBuf.ResetInWriteRvsMode();
200 if (!att.BEncPdu (outputBuf, encodedLen))
201 cout << "failed encoding AnyTestType value" << endl;
203 outputFile.open ("att.ber");
206 perror ("ofstream::open");
210 outputBuf.ResetInReadMode();
211 for ( ; encodedLen > 0; encodedLen--)
212 outputFile.put (outputBuf.GetByte());
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;