]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/any/genber.c
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.
20 * c-examples/any/genber.c - builds an AnyTestType value and writes BER form
21 * of the value to a file called "att.ber"
23 * Shows how to build internal rep of lists and ANY values.
27 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-examples/any/Attic/genber.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
29 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
30 * Move from private repository to open source repository
32 * Revision 1.2 2001/05/05 00:59:19 rmurphy
33 * Adding darwin license headers
35 * Revision 1.1.1.1 1999/03/16 18:06:08 aram
36 * Originals from SMIME Free Library.
38 * Revision 1.5 1995/07/24 20:40:50 rj
39 * any-test.[hc] becomes any.[hc] due to to snacc's new file name generation scheme.
41 * changed `_' to `-' in file names.
43 * Revision 1.4 1995/02/18 15:17:36 rj
46 * Revision 1.3 1994/08/31 23:48:06 rj
47 * more portable .h file inclusion.
49 * Revision 1.2 1994/08/31 08:59:32 rj
50 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
54 #include <sys/types.h> /* this must be before stddef for gcc-2.3.1 */
66 main (int argc
, char *argv
[])
70 unsigned long int encodedLen
;
77 AttrValue1
**atv1Hndl
;
78 AttrValue2
**atv2Hndl
;
85 /* used to alloc part of value (Asn1Alloc & AsnListAppend) */
86 InitNibbleMem (512,512);
88 /* init id to type ANY hash table */
91 att
.intMap
= AsnListNew (sizeof (void*));
92 atv1Hndl
= (AttrValue1
**)AsnListAppend (att
.intMap
);
93 *atv1Hndl
= (AttrValue1
*) Asn1Alloc (sizeof (AttrValue1
));
94 (*atv1Hndl
)->id
= intId
; /* the id's are defined in the generated code */
96 (*atv1Hndl
)->anyDefBy
.value
= (void*) &intVal
;
98 atv1Hndl
= (AttrValue1
**)AsnListAppend (att
.intMap
);
99 *atv1Hndl
= (AttrValue1
*) Asn1Alloc (sizeof (AttrValue1
));
100 (*atv1Hndl
)->id
= boolId
;
102 (*atv1Hndl
)->anyDefBy
.value
= (void*)&boolVal
;
104 atv1Hndl
= (AttrValue1
**)AsnListAppend (att
.intMap
);
105 *atv1Hndl
= (AttrValue1
*) Asn1Alloc (sizeof (AttrValue1
));
106 (*atv1Hndl
)->id
= octsId
;
107 octsVal
.octs
= "Hi Mom";
108 octsVal
.octetLen
= strlen (octsVal
.octs
);
109 (*atv1Hndl
)->anyDefBy
.value
= (void*)&octsVal
;
111 atv1Hndl
= (AttrValue1
**)AsnListAppend (att
.intMap
);
112 *atv1Hndl
= (AttrValue1
*) Asn1Alloc (sizeof (AttrValue1
));
113 (*atv1Hndl
)->id
= bitsId
;
115 bitsVal
.bits
= (char*)&i
;
116 SetAsnBit (&bitsVal
, 0);
117 ClrAsnBit (&bitsVal
, 1);
118 SetAsnBit (&bitsVal
, 2);
119 ClrAsnBit (&bitsVal
, 3);
120 SetAsnBit (&bitsVal
, 4);
121 ClrAsnBit (&bitsVal
, 5);
122 SetAsnBit (&bitsVal
, 6);
123 ClrAsnBit (&bitsVal
, 7);
124 SetAsnBit (&bitsVal
, 8);
125 ClrAsnBit (&bitsVal
, 9);
126 (*atv1Hndl
)->anyDefBy
.value
= (void*)&bitsVal
;
128 atv1Hndl
= (AttrValue1
**)AsnListAppend (att
.intMap
);
129 *atv1Hndl
= (AttrValue1
*) Asn1Alloc (sizeof (AttrValue1
));
130 (*atv1Hndl
)->id
= realId
;
132 (*atv1Hndl
)->anyDefBy
.value
= (void*)&realVal
;
134 /* now do TSeq2 with same vals but use OID as identifier */
135 att
.oidMap
= AsnListNew (sizeof (void*));
137 atv2Hndl
= (AttrValue2
**)AsnListAppend (att
.oidMap
);
138 *atv2Hndl
= (AttrValue2
*) Asn1Alloc (sizeof (AttrValue2
));
139 (*atv2Hndl
)->id
= intOid
;
140 (*atv2Hndl
)->anyDefBy
.value
= (void*)&intVal
;
142 atv2Hndl
= (AttrValue2
**)AsnListAppend (att
.oidMap
);
143 *atv2Hndl
= (AttrValue2
*) Asn1Alloc (sizeof (AttrValue2
));
144 (*atv2Hndl
)->id
= boolOid
;
145 (*atv2Hndl
)->anyDefBy
.value
= (void*)&boolVal
;
147 atv2Hndl
= (AttrValue2
**)AsnListAppend (att
.oidMap
);
148 *atv2Hndl
= (AttrValue2
*) Asn1Alloc (sizeof (AttrValue2
));
149 (*atv2Hndl
)->id
= octsOid
;
150 (*atv2Hndl
)->anyDefBy
.value
= (void*)&octsVal
;
152 atv2Hndl
= (AttrValue2
**)AsnListAppend (att
.oidMap
);
153 *atv2Hndl
= (AttrValue2
*) Asn1Alloc (sizeof (AttrValue2
));
154 (*atv2Hndl
)->id
= bitsOid
;
155 (*atv2Hndl
)->anyDefBy
.value
= (void*)&bitsVal
;
157 atv2Hndl
= (AttrValue2
**)AsnListAppend (att
.oidMap
);
158 *atv2Hndl
= (AttrValue2
*) Asn1Alloc (sizeof (AttrValue2
));
159 (*atv2Hndl
)->id
= realOid
;
160 (*atv2Hndl
)->anyDefBy
.value
= (void*)&realVal
;
162 SBufInit (&outputBuf
,data
, dataSize
);
163 SBufResetInWriteRvsMode (&outputBuf
);
165 encodedLen
= BEncAnyTestType (&outputBuf
, &att
);
166 if ((encodedLen
<= 0) || (SBufWriteError (&outputBuf
)))
168 fprintf (stderr
, "failed encoding AnyTestType value\n");
172 outputFile
= fopen ("att.ber", "w");
179 SBufResetInReadMode (&outputBuf
);
180 for ( ; encodedLen
> 0; encodedLen
--)
181 fputc (SBufGetByte (&outputBuf
), outputFile
);
184 printf ("Wrote the following BER AnyTestType value to att.ber.\n");
185 printf ("Test it with \"def\" and \"indef\"\n");
187 PrintAnyTestType (stdout
, &att
, 0);