]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/tbl-example/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  * 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. 
  31  * Copyright (C) 1993 Michael Sample 
  32  *            and the University of British Columbia 
  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. 
  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. 
  45  * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/tbl-example/example.c,v 1.1.1.1 2001/05/18 23:14:10 mb Exp $ 
  47  * Revision 1.1.1.1  2001/05/18 23:14:10  mb 
  48  * Move from private repository to open source repository 
  50  * Revision 1.2  2001/05/05 00:59:30  rmurphy 
  51  * Adding darwin license headers 
  53  * Revision 1.1.1.1  1999/03/16 18:06:53  aram 
  54  * Originals from SMIME Free Library. 
  56  * Revision 1.1  1997/02/15 19:33:26  rj 
  65 #include "p-rec.h"  /* include the file we made with mkchdr */ 
  68 char *outputFileNameG 
= "p-rec.out.ber"; 
  70 void Usage 
PARAMS ((prg
), 
  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
); 
  80 main 
PARAMS ((argc
, argv
), 
  89     unsigned long int fsize
; 
  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)*/ 
 106     tblFileName 
= argv
[1]; 
 107     berFileName 
= argv
[2]; 
 109     /* init mem pool to hold  decoded val */ 
 110     InitNibbleMem (1024, 1024); 
 112     /* read in and decode the type table */ 
 113     tbl 
= LoadTblFile (tblFileName
); 
 117     fileData 
= LoadFile (berFileName
, &fsize
); 
 118     if (fileData 
== NULL
) 
 121     SBufInstallData (&sb
, fileData
, fsize
); 
 122     PutSBufInGenBuf (&sb
, &gb
); 
 124     fprintf (stdout
, "\n\n-- decoded contents of BER PersonnelRecord file: \"%s\"--\n", berFileName
); 
 126     val 
= TblDecode (tbl
, NULL
, "PersonnelRecord", &gb
, &bytesDecoded
); 
 129         fprintf (stdout
, "-- Decoding error occured somewhere -- \n"); 
 131         TblPrintValue (tbl
, NULL
, "PersonnelRecord", stdout
, val
); 
 133     fprintf (stdout
, "\n\n -- decoded %d bytes for the above value --\n\n", bytesDecoded
, berFileName
); 
 135     free (fileData
); /* was malloc'd in LoadFile */ 
 139      * (This is where the header file generated by mkchdr is 
 140      * useful - you can access the decoded value in a standard 
 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) 
 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"); 
 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"); 
 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.) 
 174     fprintf (stdout
, "now re-encoding the PersonnelRecord value to \"%s\"\n", outputFileNameG
); 
 176     ebPtr 
= ExpBufAllocBufAndData(); 
 177     ExpBufResetInWriteRvsMode (ebPtr
); /* set up to hold encoding (= writing) */ 
 179     PutExpBufInGenBuf (ebPtr
, &gb
); 
 181     if (TblEncode (tbl
, NULL
, "PersonnelRecord", &gb
, val
, &bytesEncoded
) < 0) 
 182         fprintf (stderr
, "main: error encoding the PersonnelRecord\n"); 
 184     /* copy ExpBuf data to file */ 
 185     outputFile 
= fopen (outputFileNameG
, "w"); 
 186     if (outputFile 
== NULL
) 
 188         fprintf (stderr
, "error - could not open file \"%s\"\n", outputFileNameG
); 
 189         perror ("main: fopen:"); 
 192     ExpBufCopyToFile (ebPtr
, outputFile
); 
 196     /* free the encoded version */ 
 197     ExpBufFreeBufAndDataList (ebPtr
);