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