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