NASM 2.12.02rc7
[nasm.git] / rdoff / rdlib.c
blob7f8ddac78fd29d62fede660353ccd99035073350
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define RDOFF_UTILS
46 #include "rdoff.h"
47 #include "rdlib.h"
48 #include "rdlar.h"
50 /* See Texinfo documentation about new RDOFF libraries format */
52 int rdl_error = 0;
54 char *rdl_errors[5] = {
55 "no error", "could not open file", "invalid file structure",
56 "file contains modules of an unsupported RDOFF version",
57 "module not found"
60 int rdl_verify(const char *filename)
62 FILE *fp;
63 char buf[257];
64 int i;
65 int32_t length;
66 static char lastverified[256];
67 static int lastresult = -1;
69 if (lastresult != -1 && !strcmp(filename, lastverified))
70 return lastresult;
72 fp = fopen(filename, "rb");
73 strcpy(lastverified, filename);
75 if (!fp)
76 return (rdl_error = lastresult = 1);
78 while (!feof(fp)) {
79 i = 0;
81 while (fread(buf + i, 1, 1, fp) == 1 && i < 257 && buf[i])
82 i++;
83 if (feof(fp))
84 break;
86 if (buf[0] == '.') {
88 * A special module, eg a signature block or a directory.
89 * Format of such a module is defined to be:
90 * six char type identifier
91 * int32_t count bytes content
92 * content
93 * so we can handle it uniformaly with RDOFF2 modules.
95 fread(buf, 6, 1, fp);
96 buf[6] = 0;
97 /* Currently, nothing useful to do with signature block.. */
98 } else {
99 fread(buf, 6, 1, fp);
100 buf[6] = 0;
101 if (strncmp(buf, "RDOFF", 5)) {
102 fclose(fp);
103 return rdl_error = lastresult = 2;
104 } else if (buf[5] != '2') {
105 fclose(fp);
106 return rdl_error = lastresult = 3;
109 fread(&length, 4, 1, fp);
110 fseek(fp, length, SEEK_CUR); /* skip over the module */
112 fclose(fp);
113 return lastresult = 0; /* library in correct format */
116 int rdl_open(struct librarynode *lib, const char *name)
118 int i = rdl_verify(name);
119 if (i)
120 return i;
122 lib->fp = NULL;
123 lib->name = strdup(name);
124 lib->referenced = 0;
125 lib->next = NULL;
126 return 0;
129 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
131 char buf[512];
132 int i, t;
133 void *hdr;
134 rdfheaderrec *r;
135 int32_t l;
137 rdl_error = 0;
138 lib->referenced++;
140 if (!lib->fp) {
141 lib->fp = fopen(lib->name, "rb");
143 if (!lib->fp) {
144 rdl_error = 1;
145 return 0;
147 } else
148 rewind(lib->fp);
150 while (!feof(lib->fp)) {
152 * read the module name from the file, and prepend
153 * the library name and '.' to it.
155 strcpy(buf, lib->name);
157 i = strlen(lib->name);
158 buf[i++] = '.';
159 t = i;
160 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
161 i++;
163 buf[i] = 0;
165 if (feof(lib->fp))
166 break;
167 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
168 fread(&l, 4, 1, lib->fp);
169 fseek(lib->fp, l, SEEK_CUR);
170 continue;
173 * open the RDOFF module
175 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
176 rdl_error = 16 * rdf_errno;
177 return 0;
180 * read in the header, and scan for exported symbols
182 hdr = malloc(f->header_len);
183 rdfloadseg(f, RDOFF_HEADER, hdr);
185 while ((r = rdfgetheaderrec(f))) {
186 if (r->type != 3) /* not an export */
187 continue;
189 if (!strcmp(r->e.label, label)) { /* match! */
190 free(hdr); /* reset to 'just open' */
191 f->header_loc = NULL; /* state... */
192 f->header_fp = 0;
193 return 1;
197 /* find start of next module... */
198 i = f->eof_offset;
199 rdfclose(f);
200 fseek(lib->fp, i, SEEK_SET);
204 * close the file if nobody else is using it
206 lib->referenced--;
207 if (!lib->referenced) {
208 fclose(lib->fp);
209 lib->fp = NULL;
211 return 0;
214 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
216 char buf[512];
217 int i, cmod, t;
218 int32_t length;
220 lib->referenced++;
222 if (!lib->fp) {
223 lib->fp = fopen(lib->name, "rb");
224 if (!lib->fp) {
225 lib->referenced--;
226 return (rdl_error = 1);
228 } else
229 rewind(lib->fp);
231 cmod = -1;
232 while (!feof(lib->fp)) {
233 strcpy(buf, lib->name);
234 i = strlen(buf);
235 buf[i++] = '.';
236 t = i;
237 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
238 i++;
239 buf[i] = 0;
240 if (feof(lib->fp))
241 break;
243 if (buf[t] != '.') /* special module - not counted in the numbering */
244 cmod++; /* of RDOFF modules - must be referred to by name */
246 if (cmod == moduleno) {
247 rdl_error = 16 *
248 rdfopenhere(f, lib->fp, &lib->referenced, buf);
249 lib->referenced--;
250 if (!lib->referenced) {
251 fclose(lib->fp);
252 lib->fp = NULL;
254 return rdl_error;
257 fread(buf, 6, 1, lib->fp);
258 buf[6] = 0;
259 if (buf[t] == '.') {
260 /* do nothing */
261 } else if (strncmp(buf, "RDOFF", 5)) {
262 if (!--lib->referenced) {
263 fclose(lib->fp);
264 lib->fp = NULL;
266 return rdl_error = 2;
267 } else if (buf[5] != '2') {
268 if (!--lib->referenced) {
269 fclose(lib->fp);
270 lib->fp = NULL;
272 return rdl_error = 3;
275 fread(&length, 4, 1, lib->fp);
276 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
278 if (!--lib->referenced) {
279 fclose(lib->fp);
280 lib->fp = NULL;
282 return rdl_error = 4; /* module not found */
285 void rdl_perror(const char *apname, const char *filename)
287 if (rdl_error >= 16)
288 rdfperror(apname, filename);
289 else
290 fprintf(stderr, "%s:%s:%s\n", apname, filename,
291 rdl_errors[rdl_error]);