]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/simple/sbuf-ex.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/sbuf_ex.c - an example of how to call C ASN.1-BER
21 * encoders and decoders generated by snacc
22 * using the SBuf buffer.
27 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-examples/simple/Attic/sbuf-ex.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
29 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
30 * Move from private repository to open source repository
32 * Revision 1.2 2001/05/05 00:59:20 rmurphy
33 * Adding darwin license headers
35 * Revision 1.1.1.1 1999/03/16 18:06:09 aram
36 * Originals from SMIME Free Library.
38 * Revision 1.5 1995/07/24 20:47:00 rj
39 * changed `_' to `-' in file names.
41 * Revision 1.4 1995/02/18 15:12:56 rj
44 * Revision 1.3 1994/09/01 01:02:39 rj
45 * more portable .h file inclusion.
47 * Revision 1.2 1994/08/31 08:59:37 rj
48 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
54 #include <sys/types.h>
63 main
PARAMS ((argc
, argv
),
84 fprintf (stderr
, "Usage: %s <BER data file name>\n", argv
[0]);
85 fprintf (stderr
, " Decodes the given PersonnelRecord BER data file\n");
86 fprintf (stderr
, " and re-encodes it to stdout\n");
90 fd
= open (argv
[1], O_RDONLY
, 0);
93 perror ("main: fopen");
97 if (fstat (fd
, &sbuf
) < 0)
99 perror ("main: fstat");
104 origData
= (char*)malloc (size
);
105 if (read (fd
, origData
, size
) != size
)
107 perror ("main: read");
114 * puts the given data 'origData' of 'size' bytes
115 * into an SBuf and sets the SBuf up for reading
116 * origData from the beginning
118 SBufInstallData (&buf
, origData
, size
);
121 * the first argument (512) is the number of bytes to
122 * initially allocate for the decoder to allocate from.
123 * The second argument (512) is the size in bytes to
124 * enlarge the nibble memory by when it fills up
126 InitNibbleMem (512, 512);
131 if ((val
= setjmp (env
)) == 0)
133 BDecPersonnelRecord (&buf
, &pr
, &decodedLen
, env
);
138 fprintf (stderr
, "ERROR - Decode routines returned %d\n",val
);
144 fprintf (stderr
, "decodedValue PersonnelRecord ::= ");
145 PrintPersonnelRecord (stderr
, &pr
, 0);
146 fprintf (stderr
, "\n\n");
149 * setup a new buffer set up for writing.
150 * make sure size is big enough to hold the encoded
151 * value (may be larger than decoded value if encoding
152 * with indef lengths - so add 512 slush bytes)
154 encData
= (char*) malloc (size
+ 512);
155 SBufInit (&encBuf
, encData
, size
+ 512);
156 SBufResetInWriteRvsMode (&encBuf
);
158 encodedLen
= BEncPersonnelRecord (&encBuf
, &pr
);
160 if (SBufWriteError (&encBuf
))
162 fprintf (stderr
, "ERROR - buffer to hold the encoded value was too small\n");
167 * free all of the decoded value since
168 * it has been encoded into the buffer.
169 * This is much more efficient than freeing
170 * each compontent of the value individually
175 * write encoded value from encBuf
178 fwrite (SBufDataPtr (&encBuf
), SBufDataLen (&encBuf
), 1, stdout
);