]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/any/example.c
Security-54.1.9.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-examples / any / example.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/example.c - an example of how to call C ASN.1-BER
21 * encoders and decoders generated by snacc
22 *
23 * AUTHOR: Mike Sample
24 * DATE: Mar 92
25 *
26 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-examples/any/Attic/example.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
27 * $Log: example.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:19 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 1997/02/16 20:26:15 rj
38 * check-in of a few cosmetic changes
39 *
40 * Revision 1.5 1995/07/24 20:40:19 rj
41 * any-test.[hc] becomes any.[hc] due to to snacc's new file name generation scheme.
42 *
43 * changed `_' to `-' in file names.
44 *
45 * Revision 1.4 1995/02/18 15:17:35 rj
46 * cosmetic changes
47 *
48 * Revision 1.3 1994/08/31 23:45:45 rj
49 * more portable .h file inclusion.
50 *
51 * Revision 1.2 1994/08/31 08:59:31 rj
52 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
53 *
54 */
55
56 #include "asn-incl.h"
57
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #if HAVE_FCNTL_H
61 #include <fcntl.h>
62 #endif
63 #include <stdio.h>
64
65 #include "any.h"
66
67
68
69 main PARAMS ((argc, argv),
70 int argc _AND_
71 char *argv[])
72 {
73 int fd;
74 SBuf buf;
75 SBuf encBuf;
76 char *encData;
77 AsnLen encodedLen;
78 AsnLen decodedLen;
79 int val;
80 AnyTestType att;
81 int size;
82 char *origData;
83 struct stat sbuf;
84 jmp_buf env;
85
86 if (argc != 2)
87 {
88 fprintf (stderr, "Usage: %s <BER data file name>\n", argv[0]);
89 fprintf (stderr, " Decodes the given PersonnelRecord BER data file\n");
90 fprintf (stderr, " and re-encodes it to stdout\n");
91 exit (1);
92 }
93
94 fd = open (argv[1], O_RDONLY, 0);
95 if (fd < 0)
96 {
97 perror ("main: fopen");
98 exit (1);
99 }
100
101 if (fstat (fd, &sbuf) < 0)
102 {
103 perror ("main: fstat");
104 exit (1);
105 }
106
107 size = sbuf.st_size;
108 origData = (char*)malloc (size);
109 if (read (fd, origData, size) != size)
110 {
111 perror ("main: read");
112 exit (1);
113 }
114
115 close (fd);
116
117 /*
118 * puts the given data 'origData' of 'size' bytes
119 * into an SBuf and sets the SBuf up for reading
120 * origData from the beginning
121 */
122 SBufInstallData (&buf, origData, size);
123
124 /*
125 * the first argument (512) is the number of bytes to
126 * initially allocate for the decoder to allocate from.
127 * The second argument (512) is the size in bytes to
128 * enlarge the nibble memory by when it fills up
129 */
130 InitNibbleMem (512, 512);
131
132
133 /*
134 * initialize the hash table for the
135 * the ANY type mappings.
136 * This only needs to be done once per execution
137 * (before any encoding or decoding is done)
138 */
139 InitAnyANY_TEST();
140
141 decodedLen = 0;
142 if ((val = setjmp (env)) == 0)
143 {
144 BDecAnyTestType (&buf, &att, &decodedLen, env);
145 }
146 else
147 {
148 fprintf (stderr, "ERROR - Decode routines returned %d\n",val);
149 exit (1);
150 }
151
152 fprintf (stderr, "decodedValue AnyTestType ::= ");
153 PrintAnyTestType (stderr, &att, 0);
154 fprintf (stderr, "\n\n");
155
156 /*
157 * setup a new buffer set up for writing.
158 * make sure size is big enough to hold the encoded
159 * value (may be larger than decoded value if encoding
160 * with indef lengths - so add 512 slush bytes)
161 */
162 encData = (char*) malloc (size + 512);
163 SBufInit (&encBuf, encData, size + 512);
164 SBufResetInWriteRvsMode (&encBuf);
165
166 encodedLen = BEncAnyTestType (&encBuf, &att);
167 if ((encodedLen <= 0) || SBufWriteError (&encBuf))
168 {
169 fprintf (stderr, "ERROR - buffer to hold the encoded value was too small\n");
170 exit (1);
171 }
172
173 /*
174 * free all of the decoded value since
175 * it has been encoded into the buffer.
176 * This is much more efficient than freeing
177 * each compontent of the value individually
178 */
179 ResetNibbleMem();
180
181 /*
182 * write encoded value from encBuf
183 * to stdout
184 */
185 fwrite (SBufDataPtr (&encBuf), SBufDataLen (&encBuf), 1, stdout);
186
187 return 0;
188 }