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 #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 #ifndef MIN
55 #define MIN(x, y) ((x) <= (y) ? (x) : (y))
56 #endif
57
58 static int Showheader;
59
60 /*
61 * eftread_showheader -- set showheader flag
62 *
63 */
64 void
eftread_showheader(int newval)65 eftread_showheader(int newval)
66 {
67 Showheader = newval;
68 }
69
70 /*
71 * eftread_fopen -- fopen an EFT file for reading
72 *
73 */
74
75 FILE *
eftread_fopen(const char * fname,char * idbuf,size_t idbufsz)76 eftread_fopen(const char *fname, char *idbuf, size_t idbufsz)
77 {
78 FILE *fp;
79 FILE *tfp;
80 struct eftheader hdr;
81 #define BUFLEN 8192
82 char buf[BUFLEN];
83 int cc;
84 uint32_t csum = 0;
85 char *ptr;
86
87 if ((ptr = strrchr(fname, '.')) == NULL || strcmp(ptr, ".eft") != 0) {
88 out(O_ERR, "%s: not a valid EFT (bad extension)", fname);
89 return (NULL);
90 }
91
92 if ((fp = fopen(fname, "r")) == NULL) {
93 out(O_ERR|O_SYS, "%s", fname);
94 return (NULL);
95 }
96
97 if (fread(&hdr, 1, sizeof (hdr), fp) < sizeof (hdr)) {
98 (void) fclose(fp);
99 out(O_ERR, "%s: not a valid EFT (too short)", fname);
100 return (NULL);
101 }
102 hdr.magic = ntohl(hdr.magic);
103 hdr.major = ntohs(hdr.major);
104 hdr.minor = ntohs(hdr.minor);
105 hdr.cmajor = ntohs(hdr.cmajor);
106 hdr.cminor = ntohs(hdr.cminor);
107 hdr.identlen = ntohl(hdr.identlen);
108 hdr.dictlen = ntohl(hdr.dictlen);
109 hdr.csum = ntohl(hdr.csum);
110
111 if (Showheader)
112 out(O_VERB, "%s: magic %x EFT version %d.%d esc version %d.%d",
113 fname, hdr.magic, hdr.major, hdr.minor,
114 hdr.cmajor, hdr.cminor);
115
116 if (hdr.magic != EFT_HDR_MAGIC) {
117 (void) fclose(fp);
118 out(O_ERR, "%s: not a valid EFT (bad magic)", fname);
119 return (NULL);
120 }
121
122 if (hdr.major != EFT_HDR_MAJOR || hdr.minor > EFT_HDR_MINOR) {
123 (void) fclose(fp);
124 out(O_ERR, "%s is version %d.%d, "
125 "this program supports up to %d.%d", fname,
126 hdr.major, hdr.minor, EFT_HDR_MAJOR, EFT_HDR_MINOR);
127 return (NULL);
128 }
129
130 bzero(idbuf, idbufsz);
131 if (hdr.identlen != 0) {
132 long npos = ftell(fp) + (long)hdr.identlen; /* after ident */
133 size_t rsz = MIN(hdr.identlen, idbufsz - 1);
134
135 if (fread(idbuf, 1, rsz, fp) != rsz)
136 out(O_DIE|O_SYS, "%s: fread", fname);
137 if (fseek(fp, npos, SEEK_SET) == -1)
138 out(O_DIE|O_SYS, "%s: fseek", fname);
139 }
140
141 if (hdr.dictlen && (hdr.dictlen < 2 || hdr.dictlen > 1000)) {
142 (void) fclose(fp);
143 out(O_ERR, "%s: bad dictlen: %d", fname, hdr.dictlen);
144 return (NULL);
145 }
146
147 /* read in dict strings */
148 if (hdr.dictlen) {
149 char *dbuf = alloca(hdr.dictlen);
150 char *dptr;
151
152 if ((cc = fread(dbuf, 1, hdr.dictlen, fp)) != hdr.dictlen)
153 out(O_DIE|O_SYS, "short fread on %s (dictlen %d)",
154 fname, hdr.dictlen);
155
156 /* work from end of string array backwards, finding names */
157 for (dptr = &dbuf[hdr.dictlen - 2]; dptr > dbuf; dptr--)
158 if (*dptr == '\0') {
159 /* found separator, record string */
160 Dicts = lut_add(Dicts,
161 (void *)stable(dptr + 1), (void *)0, NULL);
162 }
163 /* record the first string */
164 Dicts = lut_add(Dicts,
165 (void *)stable(dptr), (void *)0, NULL);
166 }
167
168 if ((tfp = tmpfile()) == NULL)
169 out(O_DIE|O_SYS, "cannot create temporary file");
170
171 while ((cc = fread(buf, 1, BUFLEN, fp)) > 0) {
172 char *ptr;
173
174 for (ptr = buf; ptr < &buf[cc]; ptr++) {
175 *ptr = ~((unsigned char)*ptr);
176 csum += (uint32_t)*ptr;
177 }
178 if (cc != fwrite(buf, 1, cc, tfp) || ferror(tfp))
179 out(O_DIE|O_SYS, "fwrite on tmpfile");
180 }
181 if (ferror(fp))
182 out(O_DIE|O_SYS, "fread on %s", fname);
183 (void) fclose(fp);
184
185 if (hdr.csum != csum) {
186 out(O_ERR, "%s: bad checksum (%x != %x)", fname,
187 hdr.csum, csum);
188 (void) fclose(tfp);
189 return (NULL);
190 }
191
192 if (Showheader) {
193 int len = strlen(hdr.comment);
194 if (len > 0 && hdr.comment[len - 1] == '\n')
195 hdr.comment[len - 1] = '\0';
196 out(O_OK, "%s:\n\t%s", fname, hdr.comment);
197 }
198
199 rewind(tfp);
200
201 return (tfp);
202 }
203