1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * eftread.c -- routines for reading .eft files 27*7c478bd9Sstevel@tonic-gate * 28*7c478bd9Sstevel@tonic-gate */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #include <stdio.h> 33*7c478bd9Sstevel@tonic-gate #include <string.h> 34*7c478bd9Sstevel@tonic-gate #include <strings.h> 35*7c478bd9Sstevel@tonic-gate #include <time.h> 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate #include <errno.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <alloca.h> 40*7c478bd9Sstevel@tonic-gate #include "out.h" 41*7c478bd9Sstevel@tonic-gate #include "stable.h" 42*7c478bd9Sstevel@tonic-gate #include "lut.h" 43*7c478bd9Sstevel@tonic-gate #include "tree.h" 44*7c478bd9Sstevel@tonic-gate #include "eft.h" 45*7c478bd9Sstevel@tonic-gate #include "eftread.h" 46*7c478bd9Sstevel@tonic-gate #include "esclex.h" 47*7c478bd9Sstevel@tonic-gate #include "version.h" 48*7c478bd9Sstevel@tonic-gate #include "ptree.h" 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* for uintX_t, htonl(), etc */ 51*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 52*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 53*7c478bd9Sstevel@tonic-gate #include <inttypes.h> 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static int Showheader; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * eftread_showheader -- set showheader flag 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate void 62*7c478bd9Sstevel@tonic-gate eftread_showheader(int newval) 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate Showheader = newval; 65*7c478bd9Sstevel@tonic-gate } 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate * eftread_fopen -- fopen an EFT file for reading 69*7c478bd9Sstevel@tonic-gate * 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate FILE * 73*7c478bd9Sstevel@tonic-gate eftread_fopen(const char *fname) 74*7c478bd9Sstevel@tonic-gate { 75*7c478bd9Sstevel@tonic-gate FILE *fp; 76*7c478bd9Sstevel@tonic-gate FILE *tfp; 77*7c478bd9Sstevel@tonic-gate struct eftheader hdr; 78*7c478bd9Sstevel@tonic-gate #define BUFLEN 8192 79*7c478bd9Sstevel@tonic-gate char buf[BUFLEN]; 80*7c478bd9Sstevel@tonic-gate int cc; 81*7c478bd9Sstevel@tonic-gate uint32_t csum = 0; 82*7c478bd9Sstevel@tonic-gate char *ptr; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if ((ptr = strrchr(fname, '.')) == NULL || strcmp(ptr, ".eft") != 0) { 85*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s: not a valid EFT (bad extension)", fname); 86*7c478bd9Sstevel@tonic-gate return (NULL); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate if ((fp = fopen(fname, "r")) == NULL) { 90*7c478bd9Sstevel@tonic-gate out(O_ERR|O_SYS, "%s", fname); 91*7c478bd9Sstevel@tonic-gate return (NULL); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate if (fread(&hdr, 1, sizeof (hdr), fp) < sizeof (hdr)) { 95*7c478bd9Sstevel@tonic-gate (void) fclose(fp); 96*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s: not a valid EFT (too short)", fname); 97*7c478bd9Sstevel@tonic-gate return (NULL); 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate hdr.magic = ntohl(hdr.magic); 100*7c478bd9Sstevel@tonic-gate hdr.major = ntohs(hdr.major); 101*7c478bd9Sstevel@tonic-gate hdr.minor = ntohs(hdr.minor); 102*7c478bd9Sstevel@tonic-gate hdr.cmajor = ntohs(hdr.cmajor); 103*7c478bd9Sstevel@tonic-gate hdr.cminor = ntohs(hdr.cminor); 104*7c478bd9Sstevel@tonic-gate hdr.identlen = ntohl(hdr.identlen); 105*7c478bd9Sstevel@tonic-gate hdr.dictlen = ntohl(hdr.dictlen); 106*7c478bd9Sstevel@tonic-gate hdr.csum = ntohl(hdr.csum); 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate if (Showheader) 109*7c478bd9Sstevel@tonic-gate out(O_VERB, "%s: magic %x EFT version %d.%d esc version %d.%d", 110*7c478bd9Sstevel@tonic-gate fname, hdr.magic, hdr.major, hdr.minor, 111*7c478bd9Sstevel@tonic-gate hdr.cmajor, hdr.cminor); 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate if (hdr.magic != EFT_HDR_MAGIC) { 114*7c478bd9Sstevel@tonic-gate (void) fclose(fp); 115*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s: not a valid EFT (bad magic)", fname); 116*7c478bd9Sstevel@tonic-gate return (NULL); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate if (hdr.major != EFT_HDR_MAJOR || hdr.minor > EFT_HDR_MINOR) { 120*7c478bd9Sstevel@tonic-gate (void) fclose(fp); 121*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s is version %d.%d, " 122*7c478bd9Sstevel@tonic-gate "this program supports up to %d.%d", fname, 123*7c478bd9Sstevel@tonic-gate hdr.major, hdr.minor, EFT_HDR_MAJOR, EFT_HDR_MINOR); 124*7c478bd9Sstevel@tonic-gate return (NULL); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate /* skip over ident strings */ 128*7c478bd9Sstevel@tonic-gate if (fseek(fp, (long)hdr.identlen, SEEK_CUR) == -1) 129*7c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "%s: fseek", fname); 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if (hdr.dictlen && (hdr.dictlen < 2 || hdr.dictlen > 1000)) { 132*7c478bd9Sstevel@tonic-gate (void) fclose(fp); 133*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s: bad dictlen: %d", fname, hdr.dictlen); 134*7c478bd9Sstevel@tonic-gate return (NULL); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* read in dict strings */ 138*7c478bd9Sstevel@tonic-gate if (hdr.dictlen) { 139*7c478bd9Sstevel@tonic-gate char *dbuf = alloca(hdr.dictlen); 140*7c478bd9Sstevel@tonic-gate char *dptr; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate if ((cc = fread(dbuf, 1, hdr.dictlen, fp)) != hdr.dictlen) 143*7c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "short fread on %s (dictlen %d)", 144*7c478bd9Sstevel@tonic-gate fname, hdr.dictlen); 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate /* work from end of string array backwards, finding names */ 147*7c478bd9Sstevel@tonic-gate for (dptr = &dbuf[hdr.dictlen - 2]; dptr > dbuf; dptr--) 148*7c478bd9Sstevel@tonic-gate if (*dptr == '\0') { 149*7c478bd9Sstevel@tonic-gate /* found separator, record string */ 150*7c478bd9Sstevel@tonic-gate Dicts = lut_add(Dicts, 151*7c478bd9Sstevel@tonic-gate (void *)stable(dptr + 1), (void *)0, NULL); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate /* record the first string */ 154*7c478bd9Sstevel@tonic-gate Dicts = lut_add(Dicts, 155*7c478bd9Sstevel@tonic-gate (void *)stable(dptr), (void *)0, NULL); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate if ((tfp = tmpfile()) == NULL) 159*7c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "cannot create temporary file"); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate while ((cc = fread(buf, 1, BUFLEN, fp)) > 0) { 162*7c478bd9Sstevel@tonic-gate char *ptr; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate for (ptr = buf; ptr < &buf[cc]; ptr++) { 165*7c478bd9Sstevel@tonic-gate *ptr = ~((unsigned char)*ptr); 166*7c478bd9Sstevel@tonic-gate csum += (uint32_t)*ptr; 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate if (cc != fwrite(buf, 1, cc, tfp) || ferror(tfp)) 169*7c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "fwrite on tmpfile"); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate if (ferror(fp)) 172*7c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "fread on %s", fname); 173*7c478bd9Sstevel@tonic-gate (void) fclose(fp); 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate if (hdr.csum != csum) { 176*7c478bd9Sstevel@tonic-gate out(O_ERR, "%s: bad checksum (%x != %x)", fname, 177*7c478bd9Sstevel@tonic-gate hdr.csum, csum); 178*7c478bd9Sstevel@tonic-gate (void) fclose(tfp); 179*7c478bd9Sstevel@tonic-gate return (NULL); 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate if (Showheader) { 183*7c478bd9Sstevel@tonic-gate int len = strlen(hdr.comment); 184*7c478bd9Sstevel@tonic-gate if (len > 0 && hdr.comment[len - 1] == '\n') 185*7c478bd9Sstevel@tonic-gate hdr.comment[len - 1] = '\0'; 186*7c478bd9Sstevel@tonic-gate out(O_OK, "%s:\n\t%s", fname, hdr.comment); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate rewind(tfp); 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate return (tfp); 192*7c478bd9Sstevel@tonic-gate } 193