]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/simple/genber.c
Security-54.1.7.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-examples / simple / 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/simple/genber.c - builds a PersonnelRecord value and writes BER form
21 * of the value to a file called "pr.ber"
22 *
23 *
24 * MS 92
25 *
26 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-examples/simple/Attic/genber.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
27 * $Log: genber.c,v $
28 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
29 * Move from private repository to open source repository
30 *
31 * Revision 1.2 2001/05/05 00:59:20 rmurphy
32 * Adding darwin license headers
33 *
34 * Revision 1.1.1.1 1999/03/16 18:06:08 aram
35 * Originals from SMIME Free Library.
36 *
37 * Revision 1.6 1995/07/24 20:45:00 rj
38 * changed `_' to `-' in file names.
39 *
40 * Revision 1.5 1995/02/18 15:12:54 rj
41 * cosmetic changes
42 *
43 * Revision 1.4 1995/02/17 16:21:03 rj
44 * unnecessary inclusion of <sys/stdtypes.h> removed.
45 *
46 * Revision 1.3 1994/09/01 01:02:37 rj
47 * more portable .h file inclusion.
48 *
49 * Revision 1.2 1994/08/31 08:59:35 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 "asn-incl.h"
55
56 #include <sys/file.h>
57 #include <sys/stat.h>
58
59 #include <stddef.h>
60 #if STDC_HEADERS
61 #include <stdlib.h>
62 #endif
63 #include <errno.h>
64 #include <stdio.h>
65
66 #include "p-rec.h"
67
68
69 main (int argc, char *argv[])
70 {
71 FILE *outputFile;
72 SBuf outputBuf;
73 unsigned long int encodedLen;
74 int dataSize = 1024;
75 int i;
76 char data[1024];
77 PersonnelRecord pr;
78 ChildInformation **childHndl;
79
80 /* used to alloc part of value (Asn1Alloc & AsnListAppend) */
81 InitNibbleMem (512,512);
82
83 pr.name = Asn1Alloc (sizeof (Name));
84 pr.name->givenName.octs = "John";
85 pr.name->givenName.octetLen = strlen (pr.name->givenName.octs);
86 pr.name->initial.octs = "E";
87 pr.name->initial.octetLen = strlen (pr.name->initial.octs);
88 pr.name->familyName.octs = "Smith";
89 pr.name->familyName.octetLen = strlen (pr.name->familyName.octs);
90
91 pr.title.octs = "The Big Cheese";
92 pr.title.octetLen = strlen (pr.title.octs);
93
94 pr.employeeNumber = 99999;
95
96 pr.dateOfHire.octs = "19820104";
97 pr.dateOfHire.octetLen = strlen (pr.dateOfHire.octs);
98
99 pr.nameOfSpouse = (Name*) Asn1Alloc (sizeof (Name));
100 pr.nameOfSpouse->givenName.octs = "Mary";
101 pr.nameOfSpouse->givenName.octetLen =
102 strlen (pr.nameOfSpouse->givenName.octs);
103 pr.nameOfSpouse->initial.octs = "L";
104 pr.nameOfSpouse->initial.octetLen = strlen (pr.nameOfSpouse->initial.octs);
105 pr.nameOfSpouse->familyName.octs = "Smith";
106 pr.nameOfSpouse->familyName.octetLen =
107 strlen (pr.nameOfSpouse->familyName.octs);
108
109 pr.children = AsnListNew (sizeof (void*));
110
111 childHndl = AsnListAppend (pr.children);
112 *childHndl = Asn1Alloc (sizeof (ChildInformation));
113
114 (*childHndl)->dateOfBirth.octs = "19570310";
115 (*childHndl)->dateOfBirth.octetLen = strlen ((*childHndl)->dateOfBirth.octs);
116 (*childHndl)->name = (Name*) Asn1Alloc (sizeof (Name));
117
118 (*childHndl)->name->givenName.octs = "James";
119 (*childHndl)->name->givenName.octetLen =
120 strlen ((*childHndl)->name->givenName.octs);
121 (*childHndl)->name->initial.octs = "R";
122 (*childHndl)->name->initial.octetLen =
123 strlen ((*childHndl)->name->initial.octs);
124 (*childHndl)->name->familyName.octs = "Smith";
125 (*childHndl)->name->familyName.octetLen =
126 strlen ((*childHndl)->name->familyName.octs);
127
128 childHndl = AsnListAppend (pr.children);
129 *childHndl = Asn1Alloc (sizeof (ChildInformation));
130
131 (*childHndl)->dateOfBirth.octs = "19610621";
132 (*childHndl)->dateOfBirth.octetLen = strlen ((*childHndl)->dateOfBirth.octs);
133
134 (*childHndl)->name = (Name*) Asn1Alloc (sizeof (Name));
135
136 (*childHndl)->name->givenName.octs = "Lisa";
137 (*childHndl)->name->givenName.octetLen =
138 strlen ((*childHndl)->name->givenName.octs);
139 (*childHndl)->name->initial.octs = "M";
140 (*childHndl)->name->initial.octetLen =
141 strlen ((*childHndl)->name->initial.octs);
142 (*childHndl)->name->familyName.octs = "Smith";
143 (*childHndl)->name->familyName.octetLen =
144 strlen ((*childHndl)->name->familyName.octs);
145
146 SBufInit (&outputBuf,data, dataSize);
147 SBufResetInWriteRvsMode (&outputBuf);
148
149 encodedLen = BEncPersonnelRecord (&outputBuf, &pr);
150
151 /*
152 * after encoding a value ALWAYS check for write error
153 * in the buffer. The encode routine do not use longjmp
154 * when they enter an error state
155 */
156 if ((encodedLen <= 0) || (SBufWriteError (&outputBuf)))
157 {
158 fprintf (stderr, "failed encoding PersonnelRecord value\n");
159 exit (1);
160 }
161
162 outputFile = fopen ("pr.ber", "w");
163 if (!outputFile)
164 {
165 perror ("fopen:");
166 exit (1);
167 }
168
169 SBufResetInReadMode (&outputBuf);
170 for ( ; encodedLen > 0; encodedLen--)
171 fputc (SBufGetByte (&outputBuf), outputFile);
172
173
174 printf ("Wrote the following BER PersonnelRecord value to pr.ber.\n");
175 printf ("Test it with \"def\" and \"indef\"\n");
176
177 PrintPersonnelRecord (stdout, &pr, 0);
178 printf ("\n");
179
180 return 0;
181 }