]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/any/genber.c
Security-54.1.7.tar.gz
[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 /*
20 * c-examples/any/genber.c - builds an AnyTestType value and writes BER form
21 * of the value to a file called "att.ber"
22 *
23 * Shows how to build internal rep of lists and ANY values.
24 *
25 * MS 92
26 *
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 $
28 * $Log: genber.c,v $
29 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
30 * Move from private repository to open source repository
31 *
32 * Revision 1.2 2001/05/05 00:59:19 rmurphy
33 * Adding darwin license headers
34 *
35 * Revision 1.1.1.1 1999/03/16 18:06:08 aram
36 * Originals from SMIME Free Library.
37 *
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.
40 *
41 * changed `_' to `-' in file names.
42 *
43 * Revision 1.4 1995/02/18 15:17:36 rj
44 * cosmetic changes
45 *
46 * Revision 1.3 1994/08/31 23:48:06 rj
47 * more portable .h file inclusion.
48 *
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.
51 *
52 */
53
54 #include <sys/types.h> /* this must be before stddef for gcc-2.3.1 */
55 #include <stddef.h>
56 #include <stdlib.h>
57 #include <errno.h>
58 #include <sys/file.h>
59 #include <sys/stat.h>
60 #include <stdio.h>
61
62 #include "asn-incl.h"
63 #include "any.h"
64
65
66 main (int argc, char *argv[])
67 {
68 FILE *outputFile;
69 SBuf outputBuf;
70 unsigned long int encodedLen;
71 int dataSize = 1024;
72 int i;
73 char data[1024];
74 AnyTestType att;
75 TSeq1 ts1;
76 TSeq2 ts2;
77 AttrValue1 **atv1Hndl;
78 AttrValue2 **atv2Hndl;
79 AsnInt intVal;
80 AsnBool boolVal;
81 AsnOcts octsVal;
82 AsnBits bitsVal;
83 AsnReal realVal;
84
85 /* used to alloc part of value (Asn1Alloc & AsnListAppend) */
86 InitNibbleMem (512,512);
87
88 /* init id to type ANY hash table */
89 InitAnyANY_TEST();
90
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 */
95 intVal = -99;
96 (*atv1Hndl)->anyDefBy.value = (void*) &intVal;
97
98 atv1Hndl = (AttrValue1**)AsnListAppend (att.intMap);
99 *atv1Hndl = (AttrValue1*) Asn1Alloc (sizeof (AttrValue1));
100 (*atv1Hndl)->id = boolId;
101 boolVal = TRUE;
102 (*atv1Hndl)->anyDefBy.value = (void*)&boolVal;
103
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;
110
111 atv1Hndl = (AttrValue1**)AsnListAppend (att.intMap);
112 *atv1Hndl = (AttrValue1*) Asn1Alloc (sizeof (AttrValue1));
113 (*atv1Hndl)->id = bitsId;
114 bitsVal.bitLen = 10;
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;
127
128 atv1Hndl = (AttrValue1**)AsnListAppend (att.intMap);
129 *atv1Hndl = (AttrValue1*) Asn1Alloc (sizeof (AttrValue1));
130 (*atv1Hndl)->id = realId;
131 realVal = 108.3838;
132 (*atv1Hndl)->anyDefBy.value = (void*)&realVal;
133
134 /* now do TSeq2 with same vals but use OID as identifier */
135 att.oidMap = AsnListNew (sizeof (void*));
136
137 atv2Hndl = (AttrValue2**)AsnListAppend (att.oidMap);
138 *atv2Hndl = (AttrValue2*) Asn1Alloc (sizeof (AttrValue2));
139 (*atv2Hndl)->id = intOid;
140 (*atv2Hndl)->anyDefBy.value = (void*)&intVal;
141
142 atv2Hndl = (AttrValue2**)AsnListAppend (att.oidMap);
143 *atv2Hndl = (AttrValue2*) Asn1Alloc (sizeof (AttrValue2));
144 (*atv2Hndl)->id = boolOid;
145 (*atv2Hndl)->anyDefBy.value = (void*)&boolVal;
146
147 atv2Hndl = (AttrValue2**)AsnListAppend (att.oidMap);
148 *atv2Hndl = (AttrValue2*) Asn1Alloc (sizeof (AttrValue2));
149 (*atv2Hndl)->id = octsOid;
150 (*atv2Hndl)->anyDefBy.value = (void*)&octsVal;
151
152 atv2Hndl = (AttrValue2**)AsnListAppend (att.oidMap);
153 *atv2Hndl = (AttrValue2*) Asn1Alloc (sizeof (AttrValue2));
154 (*atv2Hndl)->id = bitsOid;
155 (*atv2Hndl)->anyDefBy.value = (void*)&bitsVal;
156
157 atv2Hndl = (AttrValue2**)AsnListAppend (att.oidMap);
158 *atv2Hndl = (AttrValue2*) Asn1Alloc (sizeof (AttrValue2));
159 (*atv2Hndl)->id = realOid;
160 (*atv2Hndl)->anyDefBy.value = (void*)&realVal;
161
162 SBufInit (&outputBuf,data, dataSize);
163 SBufResetInWriteRvsMode (&outputBuf);
164
165 encodedLen = BEncAnyTestType (&outputBuf, &att);
166 if ((encodedLen <= 0) || (SBufWriteError (&outputBuf)))
167 {
168 fprintf (stderr, "failed encoding AnyTestType value\n");
169 exit (1);
170 }
171
172 outputFile = fopen ("att.ber", "w");
173 if (!outputFile)
174 {
175 perror ("fopen:");
176 exit (1);
177 }
178
179 SBufResetInReadMode (&outputBuf);
180 for ( ; encodedLen > 0; encodedLen--)
181 fputc (SBufGetByte (&outputBuf), outputFile);
182
183
184 printf ("Wrote the following BER AnyTestType value to att.ber.\n");
185 printf ("Test it with \"def\" and \"indef\"\n");
186
187 PrintAnyTestType (stdout, &att, 0);
188 printf ("\n");
189
190 return 0;
191 }