]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/simple/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/simple/genber.c - builds a PersonnelRecord value and writes BER form
21 * of the value to a file called "pr.ber"
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 $
28 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
29 * Move from private repository to open source repository
31 * Revision 1.2 2001/05/05 00:59:20 rmurphy
32 * Adding darwin license headers
34 * Revision 1.1.1.1 1999/03/16 18:06:08 aram
35 * Originals from SMIME Free Library.
37 * Revision 1.6 1995/07/24 20:45:00 rj
38 * changed `_' to `-' in file names.
40 * Revision 1.5 1995/02/18 15:12:54 rj
43 * Revision 1.4 1995/02/17 16:21:03 rj
44 * unnecessary inclusion of <sys/stdtypes.h> removed.
46 * Revision 1.3 1994/09/01 01:02:37 rj
47 * more portable .h file inclusion.
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.
69 main (int argc
, char *argv
[])
73 unsigned long int encodedLen
;
78 ChildInformation
**childHndl
;
80 /* used to alloc part of value (Asn1Alloc & AsnListAppend) */
81 InitNibbleMem (512,512);
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
);
91 pr
.title
.octs
= "The Big Cheese";
92 pr
.title
.octetLen
= strlen (pr
.title
.octs
);
94 pr
.employeeNumber
= 99999;
96 pr
.dateOfHire
.octs
= "19820104";
97 pr
.dateOfHire
.octetLen
= strlen (pr
.dateOfHire
.octs
);
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
);
109 pr
.children
= AsnListNew (sizeof (void*));
111 childHndl
= AsnListAppend (pr
.children
);
112 *childHndl
= Asn1Alloc (sizeof (ChildInformation
));
114 (*childHndl
)->dateOfBirth
.octs
= "19570310";
115 (*childHndl
)->dateOfBirth
.octetLen
= strlen ((*childHndl
)->dateOfBirth
.octs
);
116 (*childHndl
)->name
= (Name
*) Asn1Alloc (sizeof (Name
));
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
);
128 childHndl
= AsnListAppend (pr
.children
);
129 *childHndl
= Asn1Alloc (sizeof (ChildInformation
));
131 (*childHndl
)->dateOfBirth
.octs
= "19610621";
132 (*childHndl
)->dateOfBirth
.octetLen
= strlen ((*childHndl
)->dateOfBirth
.octs
);
134 (*childHndl
)->name
= (Name
*) Asn1Alloc (sizeof (Name
));
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
);
146 SBufInit (&outputBuf
,data
, dataSize
);
147 SBufResetInWriteRvsMode (&outputBuf
);
149 encodedLen
= BEncPersonnelRecord (&outputBuf
, &pr
);
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
156 if ((encodedLen
<= 0) || (SBufWriteError (&outputBuf
)))
158 fprintf (stderr
, "failed encoding PersonnelRecord value\n");
162 outputFile
= fopen ("pr.ber", "w");
169 SBufResetInReadMode (&outputBuf
);
170 for ( ; encodedLen
> 0; encodedLen
--)
171 fputc (SBufGetByte (&outputBuf
), outputFile
);
174 printf ("Wrote the following BER PersonnelRecord value to pr.ber.\n");
175 printf ("Test it with \"def\" and \"indef\"\n");
177 PrintPersonnelRecord (stdout
, &pr
, 0);