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