]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/any/example.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/any/example.c - an example of how to call C ASN.1-BER
21 * encoders and decoders generated by snacc
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 $
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:19 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 1997/02/16 20:26:15 rj
38 * check-in of a few cosmetic changes
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.
43 * changed `_' to `-' in file names.
45 * Revision 1.4 1995/02/18 15:17:35 rj
48 * Revision 1.3 1994/08/31 23:45:45 rj
49 * more portable .h file inclusion.
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.
58 #include <sys/types.h>
69 main
PARAMS ((argc
, argv
),
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");
94 fd
= open (argv
[1], O_RDONLY
, 0);
97 perror ("main: fopen");
101 if (fstat (fd
, &sbuf
) < 0)
103 perror ("main: fstat");
108 origData
= (char*)malloc (size
);
109 if (read (fd
, origData
, size
) != size
)
111 perror ("main: read");
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
122 SBufInstallData (&buf
, origData
, size
);
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
130 InitNibbleMem (512, 512);
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)
142 if ((val
= setjmp (env
)) == 0)
144 BDecAnyTestType (&buf
, &att
, &decodedLen
, env
);
148 fprintf (stderr
, "ERROR - Decode routines returned %d\n",val
);
152 fprintf (stderr
, "decodedValue AnyTestType ::= ");
153 PrintAnyTestType (stderr
, &att
, 0);
154 fprintf (stderr
, "\n\n");
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)
162 encData
= (char*) malloc (size
+ 512);
163 SBufInit (&encBuf
, encData
, size
+ 512);
164 SBufResetInWriteRvsMode (&encBuf
);
166 encodedLen
= BEncAnyTestType (&encBuf
, &att
);
167 if ((encodedLen
<= 0) || SBufWriteError (&encBuf
))
169 fprintf (stderr
, "ERROR - buffer to hold the encoded value was too small\n");
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
182 * write encoded value from encBuf
185 fwrite (SBufDataPtr (&encBuf
), SBufDataLen (&encBuf
), 1, stdout
);