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