]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/simple/expbuf-ex.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-examples / simple / expbuf-ex.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/expbuf_ex.c - an example of how to call C ASN.1-BER
21 * encoders and decoders generated by snacc
22 * with the ExpBuf buffer.
23 *
24 * AUTHOR: Mike Sample
25 * DATE: Mar 92
26 *
27 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-examples/simple/expbuf-ex.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
28 * $Log: expbuf-ex.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.3 2001/05/05 00:59:20 rmurphy
33 * Adding darwin license headers
34 *
35 * Revision 1.2 2000/06/08 20:06:59 dmitch
36 * Mods for X port.
37 *
38 * Revision 1.1.1.1 1999/03/16 18:06:08 aram
39 * Originals from SMIME Free Library.
40 *
41 * Revision 1.5 1995/07/24 20:44:58 rj
42 * changed `_' to `-' in file names.
43 *
44 * Revision 1.4 1995/02/18 15:12:53 rj
45 * cosmetic changes
46 *
47 * Revision 1.3 1994/08/31 23:48:29 rj
48 * more portable .h file inclusion.
49 *
50 * Revision 1.2 1994/08/31 08:59:34 rj
51 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
52 *
53 */
54
55 #include "asn-incl.h"
56
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #if HAVE_FCNTL_H
60 #include <fcntl.h>
61 #endif
62 #include <stdio.h>
63
64 #include "p-rec.h"
65 #include "exp-buf.h"
66
67 main PARAMS ((argc, argv),
68 int argc _AND_
69 char *argv[])
70 {
71 int fd;
72 ExpBuf *buf;
73 ExpBuf b;
74 ExpBuf *tmpBuf;
75 AsnLen encodedLen;
76 AsnLen decodedLen;
77 int val;
78 PersonnelRecord pr;
79 int size;
80 char *origData;
81 struct stat sbuf;
82 jmp_buf env;
83 int decodeErr;
84 AsnTag tag;
85
86
87 if (argc != 2)
88 {
89 fprintf (stderr, "Usage: %s <BER data file name>\n", argv[0]);
90 fprintf (stderr, " Decodes the given PersonnelRecord BER data file\n");
91 fprintf (stderr, " and re-encodes it to stdout\n");
92 exit (1);
93 }
94
95 fd = open (argv[1], O_RDONLY, 0);
96 if (fd < 0)
97 {
98 perror ("main: fopen");
99 exit (1);
100 }
101
102 if (fstat (fd, &sbuf) < 0)
103 {
104 perror ("main: fstat");
105 exit (1);
106 }
107
108 size = sbuf.st_size;
109 origData = (char*)malloc (size);
110 if (read (fd, origData, size) != size)
111 {
112 perror ("main: read");
113 exit (1);
114 }
115
116 close (fd);
117
118 /*
119 * the "1024" is the size in bytes of the data
120 * blk to allocate when writing to a buffer that
121 * fills up.
122 */
123 ExpBufInit (1024);
124
125 /*
126 * the first argument (512) is the number of bytes to
127 * initially allocate for the decoder to allocate from.
128 * The second argument (512) is the size in bytes to
129 * enlarge the nibble memory by when it fills up
130 */
131 InitNibbleMem (512, 512);
132
133 /*
134 * put the BER data read from the file
135 * into buffer format, ready for reading from the
136 * beginning
137 */
138 buf = &b;
139 ExpBufInstallDataInBuf (buf, origData, size);
140
141 decodedLen = 0;
142 decodeErr = FALSE;
143 if ((val = setjmp (env)) == 0)
144 {
145 BDecPersonnelRecord (&buf, &pr, &decodedLen, env);
146 }
147 else
148 {
149 decodeErr = TRUE;
150 fprintf (stderr, "ERROR - Decode routines returned %d\n",val);
151 }
152
153 if (decodeErr)
154 exit (1);
155
156 fprintf (stderr, "decodedValue PersonnelRecord ::= ");
157 PrintPersonnelRecord (stderr, &pr, 0);
158 fprintf (stderr, "\n\n");
159
160 /*
161 * allocate a new buffer set up for writing to
162 */
163 buf = ExpBufAllocBufAndData();
164
165 encodedLen = BEncPersonnelRecord (&buf, &pr);
166
167 /*
168 * Alway check for a buffer write error after encoding
169 */
170 if (ExpBufWriteError (&buf))
171 {
172 fprintf (stderr, "ERROR - buffer write error during encoding\n");
173 exit (1);
174 }
175
176
177 /*
178 * free all of the decoded value since
179 * it has been encoded into the buffer.
180 * This is much more efficient than freeing
181 * each compontent of the value individually
182 */
183 ResetNibbleMem();
184
185 /*
186 * go through buffer (s) and write encoded value
187 * to stdout
188 */
189 buf->curr = buf->dataStart;
190 for ( tmpBuf = buf; tmpBuf != NULL; tmpBuf = tmpBuf->next)
191 {
192 fwrite (tmpBuf->dataStart, tmpBuf->dataEnd - tmpBuf->dataStart, 1, stdout);
193 }
194
195 return 0;
196 }