17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58a40a695Sgavinm  * Common Development and Distribution License (the "License").
68a40a695Sgavinm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*837416c3Scy  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * eftread.c -- routines for reading .eft files
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <time.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <alloca.h>
397c478bd9Sstevel@tonic-gate #include "out.h"
407c478bd9Sstevel@tonic-gate #include "stable.h"
417c478bd9Sstevel@tonic-gate #include "lut.h"
427c478bd9Sstevel@tonic-gate #include "tree.h"
437c478bd9Sstevel@tonic-gate #include "eft.h"
447c478bd9Sstevel@tonic-gate #include "eftread.h"
457c478bd9Sstevel@tonic-gate #include "esclex.h"
467c478bd9Sstevel@tonic-gate #include "version.h"
477c478bd9Sstevel@tonic-gate #include "ptree.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* for uintX_t, htonl(), etc */
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <netinet/in.h>
527c478bd9Sstevel@tonic-gate #include <inttypes.h>
537c478bd9Sstevel@tonic-gate 
54*837416c3Scy #ifndef MIN
558a40a695Sgavinm #define	MIN(x, y) ((x) <= (y) ? (x) : (y))
56*837416c3Scy #endif
578a40a695Sgavinm 
587c478bd9Sstevel@tonic-gate static int Showheader;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * eftread_showheader -- set showheader flag
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate void
eftread_showheader(int newval)657c478bd9Sstevel@tonic-gate eftread_showheader(int newval)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	Showheader = newval;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * eftread_fopen -- fopen an EFT file for reading
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate FILE *
eftread_fopen(const char * fname,char * idbuf,size_t idbufsz)768a40a695Sgavinm eftread_fopen(const char *fname, char *idbuf, size_t idbufsz)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	FILE *fp;
797c478bd9Sstevel@tonic-gate 	FILE *tfp;
807c478bd9Sstevel@tonic-gate 	struct eftheader hdr;
817c478bd9Sstevel@tonic-gate #define	BUFLEN	8192
827c478bd9Sstevel@tonic-gate 	char buf[BUFLEN];
837c478bd9Sstevel@tonic-gate 	int cc;
847c478bd9Sstevel@tonic-gate 	uint32_t csum = 0;
857c478bd9Sstevel@tonic-gate 	char *ptr;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ((ptr = strrchr(fname, '.')) == NULL || strcmp(ptr, ".eft") != 0) {
887c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s: not a valid EFT (bad extension)", fname);
897c478bd9Sstevel@tonic-gate 		return (NULL);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if ((fp = fopen(fname, "r")) == NULL) {
937c478bd9Sstevel@tonic-gate 		out(O_ERR|O_SYS, "%s", fname);
947c478bd9Sstevel@tonic-gate 		return (NULL);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (fread(&hdr, 1, sizeof (hdr), fp) < sizeof (hdr)) {
987c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
997c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s: not a valid EFT (too short)", fname);
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	hdr.magic = ntohl(hdr.magic);
1037c478bd9Sstevel@tonic-gate 	hdr.major = ntohs(hdr.major);
1047c478bd9Sstevel@tonic-gate 	hdr.minor = ntohs(hdr.minor);
1057c478bd9Sstevel@tonic-gate 	hdr.cmajor = ntohs(hdr.cmajor);
1067c478bd9Sstevel@tonic-gate 	hdr.cminor = ntohs(hdr.cminor);
1077c478bd9Sstevel@tonic-gate 	hdr.identlen = ntohl(hdr.identlen);
1087c478bd9Sstevel@tonic-gate 	hdr.dictlen = ntohl(hdr.dictlen);
1097c478bd9Sstevel@tonic-gate 	hdr.csum = ntohl(hdr.csum);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (Showheader)
1127c478bd9Sstevel@tonic-gate 		out(O_VERB, "%s: magic %x EFT version %d.%d esc version %d.%d",
1137c478bd9Sstevel@tonic-gate 		    fname, hdr.magic, hdr.major, hdr.minor,
1147c478bd9Sstevel@tonic-gate 		    hdr.cmajor, hdr.cminor);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (hdr.magic != EFT_HDR_MAGIC) {
1177c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1187c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s: not a valid EFT (bad magic)", fname);
1197c478bd9Sstevel@tonic-gate 		return (NULL);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (hdr.major != EFT_HDR_MAJOR || hdr.minor > EFT_HDR_MINOR) {
1237c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1247c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s is version %d.%d, "
1257c478bd9Sstevel@tonic-gate 		    "this program supports up to %d.%d", fname,
1267c478bd9Sstevel@tonic-gate 		    hdr.major, hdr.minor, EFT_HDR_MAJOR, EFT_HDR_MINOR);
1277c478bd9Sstevel@tonic-gate 		return (NULL);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1308a40a695Sgavinm 	bzero(idbuf, idbufsz);
1318a40a695Sgavinm 	if (hdr.identlen != 0) {
1328a40a695Sgavinm 		long npos = ftell(fp) + (long)hdr.identlen; /* after ident */
1338a40a695Sgavinm 		size_t rsz = MIN(hdr.identlen, idbufsz - 1);
1348a40a695Sgavinm 
1358a40a695Sgavinm 		if (fread(idbuf, 1, rsz, fp) != rsz)
1368a40a695Sgavinm 			out(O_DIE|O_SYS, "%s: fread", fname);
1378a40a695Sgavinm 		if (fseek(fp, npos, SEEK_SET) == -1)
1388a40a695Sgavinm 			out(O_DIE|O_SYS, "%s: fseek", fname);
1398a40a695Sgavinm 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (hdr.dictlen && (hdr.dictlen < 2 || hdr.dictlen > 1000)) {
1427c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1437c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s: bad dictlen: %d", fname, hdr.dictlen);
1447c478bd9Sstevel@tonic-gate 		return (NULL);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* read in dict strings */
1487c478bd9Sstevel@tonic-gate 	if (hdr.dictlen) {
1497c478bd9Sstevel@tonic-gate 		char *dbuf = alloca(hdr.dictlen);
1507c478bd9Sstevel@tonic-gate 		char *dptr;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if ((cc = fread(dbuf, 1, hdr.dictlen, fp)) != hdr.dictlen)
1537c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "short fread on %s (dictlen %d)",
1547c478bd9Sstevel@tonic-gate 			    fname, hdr.dictlen);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		/* work from end of string array backwards, finding names */
1577c478bd9Sstevel@tonic-gate 		for (dptr = &dbuf[hdr.dictlen - 2]; dptr > dbuf; dptr--)
1587c478bd9Sstevel@tonic-gate 			if (*dptr == '\0') {
1597c478bd9Sstevel@tonic-gate 				/* found separator, record string */
1607c478bd9Sstevel@tonic-gate 				Dicts = lut_add(Dicts,
1617c478bd9Sstevel@tonic-gate 				    (void *)stable(dptr + 1), (void *)0, NULL);
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 		/* record the first string */
1647c478bd9Sstevel@tonic-gate 		Dicts = lut_add(Dicts,
1657c478bd9Sstevel@tonic-gate 		    (void *)stable(dptr), (void *)0, NULL);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if ((tfp = tmpfile()) == NULL)
1697c478bd9Sstevel@tonic-gate 		out(O_DIE|O_SYS, "cannot create temporary file");
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	while ((cc = fread(buf, 1, BUFLEN, fp)) > 0) {
1727c478bd9Sstevel@tonic-gate 		char *ptr;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		for (ptr = buf; ptr < &buf[cc]; ptr++) {
1757c478bd9Sstevel@tonic-gate 			*ptr = ~((unsigned char)*ptr);
1767c478bd9Sstevel@tonic-gate 			csum += (uint32_t)*ptr;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		if (cc != fwrite(buf, 1, cc, tfp) || ferror(tfp))
1797c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "fwrite on tmpfile");
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (ferror(fp))
1827c478bd9Sstevel@tonic-gate 		out(O_DIE|O_SYS, "fread on %s", fname);
1837c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (hdr.csum != csum) {
1867c478bd9Sstevel@tonic-gate 		out(O_ERR, "%s: bad checksum (%x != %x)", fname,
1877c478bd9Sstevel@tonic-gate 		    hdr.csum, csum);
1887c478bd9Sstevel@tonic-gate 		(void) fclose(tfp);
1897c478bd9Sstevel@tonic-gate 		return (NULL);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (Showheader) {
1937c478bd9Sstevel@tonic-gate 		int len = strlen(hdr.comment);
1947c478bd9Sstevel@tonic-gate 		if (len > 0 && hdr.comment[len - 1] == '\n')
1957c478bd9Sstevel@tonic-gate 			hdr.comment[len - 1] = '\0';
1967c478bd9Sstevel@tonic-gate 		out(O_OK, "%s:\n\t%s", fname, hdr.comment);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	rewind(tfp);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (tfp);
2027c478bd9Sstevel@tonic-gate }
203