xref: /illumos-gate/usr/src/cmd/fm/eversholt/common/eftread.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 2008 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 #include <stdio.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <alloca.h>
37 #include "out.h"
38 #include "stable.h"
39 #include "lut.h"
40 #include "tree.h"
41 #include "eft.h"
42 #include "eftread.h"
43 #include "esclex.h"
44 #include "version.h"
45 #include "ptree.h"
46 
47 /* for uintX_t, htonl(), etc */
48 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <inttypes.h>
51 
52 #ifndef MIN
53 #define	MIN(x, y) ((x) <= (y) ? (x) : (y))
54 #endif
55 
56 static int Showheader;
57 
58 /*
59  * eftread_showheader -- set showheader flag
60  *
61  */
62 void
eftread_showheader(int newval)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 *
eftread_fopen(const char * fname,char * idbuf,size_t idbufsz)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