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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * eftread.c -- routines for reading .eft files
29 #pragma ident "%Z%%M% %I% %E% SMI"
49 /* for uintX_t, htonl(), etc */
50 #include <sys/types.h>
51 #include <netinet/in.h>
55 #define MIN(x, y) ((x) <= (y) ? (x) : (y))
58 static int Showheader
;
61 * eftread_showheader -- set showheader flag
65 eftread_showheader(int newval
)
71 * eftread_fopen -- fopen an EFT file for reading
76 eftread_fopen(const char *fname
, char *idbuf
, size_t idbufsz
)
87 if ((ptr
= strrchr(fname
, '.')) == NULL
|| strcmp(ptr
, ".eft") != 0) {
88 out(O_ERR
, "%s: not a valid EFT (bad extension)", fname
);
92 if ((fp
= fopen(fname
, "r")) == NULL
) {
93 out(O_ERR
|O_SYS
, "%s", fname
);
97 if (fread(&hdr
, 1, sizeof (hdr
), fp
) < sizeof (hdr
)) {
99 out(O_ERR
, "%s: not a valid EFT (too short)", fname
);
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
);
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
);
116 if (hdr
.magic
!= EFT_HDR_MAGIC
) {
118 out(O_ERR
, "%s: not a valid EFT (bad magic)", fname
);
122 if (hdr
.major
!= EFT_HDR_MAJOR
|| hdr
.minor
> EFT_HDR_MINOR
) {
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
);
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);
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
);
141 if (hdr
.dictlen
&& (hdr
.dictlen
< 2 || hdr
.dictlen
> 1000)) {
143 out(O_ERR
, "%s: bad dictlen: %d", fname
, hdr
.dictlen
);
147 /* read in dict strings */
149 char *dbuf
= alloca(hdr
.dictlen
);
152 if ((cc
= fread(dbuf
, 1, hdr
.dictlen
, fp
)) != hdr
.dictlen
)
153 out(O_DIE
|O_SYS
, "short fread on %s (dictlen %d)",
156 /* work from end of string array backwards, finding names */
157 for (dptr
= &dbuf
[hdr
.dictlen
- 2]; dptr
> dbuf
; dptr
--)
159 /* found separator, record string */
160 Dicts
= lut_add(Dicts
,
161 (void *)stable(dptr
+ 1), NULL
, NULL
);
163 /* record the first string */
164 Dicts
= lut_add(Dicts
,
165 (void *)stable(dptr
), NULL
, NULL
);
168 if ((tfp
= tmpfile()) == NULL
)
169 out(O_DIE
|O_SYS
, "cannot create temporary file");
171 while ((cc
= fread(buf
, 1, BUFLEN
, fp
)) > 0) {
174 for (ptr
= buf
; ptr
< &buf
[cc
]; ptr
++) {
175 *ptr
= ~((unsigned char)*ptr
);
176 csum
+= (uint32_t)*ptr
;
178 if (cc
!= fwrite(buf
, 1, cc
, tfp
) || ferror(tfp
))
179 out(O_DIE
|O_SYS
, "fwrite on tmpfile");
182 out(O_DIE
|O_SYS
, "fread on %s", fname
);
185 if (hdr
.csum
!= csum
) {
186 out(O_ERR
, "%s: bad checksum (%x != %x)", fname
,
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
);