]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/tbl-example/example.c
Security-54.1.tar.gz
[apple/security.git] / SecuritySNACCRuntime / tbl-example / 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 * file: .../tbl-example/example.c - decodes and prints a given BER
21 * PersonnelRecord value and re-encodes it to the file
22 * "p-rec.out.ber". This example would be similar to your user code in
23 * that you run "mkchdr" to build a nicely named description of data
24 * structure (PersonnelRecord in this case). The table tools deal with
25 * the same data structure in a generic way and don't use/need mkchdr.
26 * You must not change the output of mkchdr otherwise the table encoder
27 * decoder, etc will not understand it.
28 *
29 * Mike Sample
30 *
31 * Copyright (C) 1993 Michael Sample
32 * and the University of British Columbia
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program and the associated libraries are distributed in the hope
40 * that they will be useful, but WITHOUT ANY WARRANTY; without even the
41 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
42 * PURPOSE. See the GNU General Public License and GNU Library General
43 * Public License for more details.
44 *
45 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/tbl-example/example.c,v 1.1.1.1 2001/05/18 23:14:10 mb Exp $
46 * $Log: example.c,v $
47 * Revision 1.1.1.1 2001/05/18 23:14:10 mb
48 * Move from private repository to open source repository
49 *
50 * Revision 1.2 2001/05/05 00:59:30 rmurphy
51 * Adding darwin license headers
52 *
53 * Revision 1.1.1.1 1999/03/16 18:06:53 aram
54 * Originals from SMIME Free Library.
55 *
56 * Revision 1.1 1997/02/15 19:33:26 rj
57 * first check-in
58 *
59 */
60
61 #include "tbl-incl.h"
62 #include "exp-buf.h"
63 #include "sbuf.h"
64
65 #include "p-rec.h" /* include the file we made with mkchdr */
66
67
68 char *outputFileNameG = "p-rec.out.ber";
69
70 void Usage PARAMS ((prg),
71 char *prg)
72 {
73 fprintf (stderr, "Usage: %s <tt file name> <p-rec ber file> \n\n", prg);
74 fprintf (stderr, "E.g. %s p-rec.tt p-rec.ber\n\n", prg);
75 fprintf (stderr, "The BER values in the file list will be decoded, printed to stdout and then re-encoded to the file \"%s\"\n", outputFileNameG);
76 }
77
78
79 int
80 main PARAMS ((argc, argv),
81 int argc _AND_
82 char **argv)
83 {
84 char *tblFileName;
85 char *berFileName;
86 TBL *tbl;
87 int i;
88 char *fileData;
89 unsigned long int fsize;
90 PersonnelRecord *val;
91 unsigned long int bytesDecoded;
92 unsigned long int bytesEncoded;
93 SBuf sb; /* use simple buffers for reading in (know sizes) */
94 ExpBuf *ebPtr; /* use expanding bufs for enc (usually don't know sizes)*/
95 GenBuf gb;
96 FILE *outputFile;
97
98
99
100 if (argc != 3)
101 {
102 Usage (argv[0]);
103 return 1;
104 }
105
106 tblFileName = argv[1];
107 berFileName = argv[2];
108
109 /* init mem pool to hold decoded val */
110 InitNibbleMem (1024, 1024);
111
112 /* read in and decode the type table */
113 tbl = LoadTblFile (tblFileName);
114 if (tbl == NULL)
115 return 1;
116
117 fileData = LoadFile (berFileName, &fsize);
118 if (fileData == NULL)
119 return 1;
120
121 SBufInstallData (&sb, fileData, fsize);
122 PutSBufInGenBuf (&sb, &gb);
123
124 fprintf (stdout, "\n\n-- decoded contents of BER PersonnelRecord file: \"%s\"--\n", berFileName);
125
126 val = TblDecode (tbl, NULL, "PersonnelRecord", &gb, &bytesDecoded);
127
128 if (val == NULL)
129 fprintf (stdout, "-- Decoding error occured somewhere -- \n");
130 else
131 TblPrintValue (tbl, NULL, "PersonnelRecord", stdout, val);
132
133 fprintf (stdout, "\n\n -- decoded %d bytes for the above value --\n\n", bytesDecoded, berFileName);
134
135 free (fileData); /* was malloc'd in LoadFile */
136
137 /*
138 * process value here
139 * (This is where the header file generated by mkchdr is
140 * useful - you can access the decoded value in a standard
141 * /easier way).
142 *
143 * Ok, well, the names "field0" etc aren't that nice
144 * but what did you expect - they aren't named in the ASN.1
145 * spec so mkchdr just makes them up. To fix this, just
146 * add field names to you ASN.1 spec - it will not change the
147 * way the values are encoded - so you're not making it
148 * incompatible with the original. (not including value notation)
149 */
150 printf ("The following printout is an example of using the\n");
151 printf ("hdr file generated by mkchdr to access the data\n");
152 printf ("returned from the table decoder. Look in \"example.c\"\n\n");
153
154
155 printf ("***** JQ GUMBY & CO Database *****************************************\n");
156 printf ("Employee Name: %s %s %s\n", val->field0->givenName->octs, val->field0->initial->octs, val->field0->familyName->octs);
157 printf ("Title: %s\n", val->title->octs);
158 printf ("Employee Number: %d\n", *val->field1);
159 printf ("Date of Hire: %s\n", val->dateOfHire->octs);
160 printf ("Name of Spouse: %s %s %s\n", val->nameOfSpouse->givenName->octs, val->nameOfSpouse->initial->octs, val->nameOfSpouse->familyName->octs);
161 printf ("Number of Children: %d\n", AsnListCount (val->children));
162 printf ("**********************************************************************\n\n");
163
164 /*
165 * finished playing with the decoded value.
166 * now re-encode the value. Using an expbuf to hold the encoded val
167 * because they can grow and in general you can predict a values
168 * encoded size (although we could assume that is would be close to
169 * the same size as the one we read in at the beginning of this prg).
170 * (note: the size of PersonnelRecord BER value we decoded may be
171 * different from the size of the re-encoded version depending on
172 * the use of indefinite or definite lengths. Both are valid BER.)
173 */
174 fprintf (stdout, "now re-encoding the PersonnelRecord value to \"%s\"\n", outputFileNameG);
175
176 ebPtr = ExpBufAllocBufAndData();
177 ExpBufResetInWriteRvsMode (ebPtr); /* set up to hold encoding (= writing) */
178
179 PutExpBufInGenBuf (ebPtr, &gb);
180
181 if (TblEncode (tbl, NULL, "PersonnelRecord", &gb, val, &bytesEncoded) < 0)
182 fprintf (stderr, "main: error encoding the PersonnelRecord\n");
183
184 /* copy ExpBuf data to file */
185 outputFile = fopen (outputFileNameG, "w");
186 if (outputFile == NULL)
187 {
188 fprintf (stderr, "error - could not open file \"%s\"\n", outputFileNameG);
189 perror ("main: fopen:");
190 }
191
192 ExpBufCopyToFile (ebPtr, outputFile);
193
194 fclose (outputFile);
195
196 /* free the encoded version */
197 ExpBufFreeBufAndDataList (ebPtr);
198
199
200 return 0;
201 } /* main */