rbtree: add rb_search_exact()
[nasm.git] / rdoff / rdlib.c
blob320a1664dd8ad15303aa3f63cd28e1d453b7d95f
1 /* ----------------------------------------------------------------------- *
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>
43 #include "rdfutils.h"
44 #include "rdlib.h"
45 #include "rdlar.h"
47 /* See Texinfo documentation about new RDOFF libraries format */
49 int rdl_error = 0;
51 char *rdl_errors[5] = {
52 "no error", "could not open file", "invalid file structure",
53 "file contains modules of an unsupported RDOFF version",
54 "module not found"
57 int rdl_verify(const char *filename)
59 FILE *fp;
60 char buf[257];
61 int i;
62 int32_t length;
63 static char lastverified[256];
64 static int lastresult = -1;
66 if (lastresult != -1 && !strcmp(filename, lastverified))
67 return lastresult;
69 fp = fopen(filename, "rb");
70 strcpy(lastverified, filename);
72 if (!fp)
73 return (rdl_error = lastresult = 1);
75 while (!feof(fp)) {
76 i = 0;
78 while (fread(buf + i, 1, 1, fp) == 1 && i < 257 && buf[i])
79 i++;
80 if (feof(fp))
81 break;
83 if (buf[0] == '.') {
85 * A special module, eg a signature block or a directory.
86 * Format of such a module is defined to be:
87 * six char type identifier
88 * int32_t count bytes content
89 * content
90 * so we can handle it uniformaly with RDOFF2 modules.
92 nasm_read(buf, 6, fp);
93 buf[6] = 0;
94 /* Currently, nothing useful to do with signature block.. */
95 } else {
96 nasm_read(buf, 6, fp);
97 buf[6] = 0;
98 if (strncmp(buf, "RDOFF", 5)) {
99 fclose(fp);
100 return rdl_error = lastresult = 2;
101 } else if (buf[5] != '2') {
102 fclose(fp);
103 return rdl_error = lastresult = 3;
106 nasm_read(&length, 4, fp);
107 fseek(fp, length, SEEK_CUR); /* skip over the module */
109 fclose(fp);
110 return lastresult = 0; /* library in correct format */
113 int rdl_open(struct librarynode *lib, const char *name)
115 int i = rdl_verify(name);
116 if (i)
117 return i;
119 lib->fp = NULL;
120 lib->name = nasm_strdup(name);
121 lib->referenced = 0;
122 lib->next = NULL;
123 return 0;
126 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
128 char buf[512];
129 int i, t;
130 void *hdr;
131 rdfheaderrec *r;
132 int32_t l;
134 rdl_error = 0;
135 lib->referenced++;
137 if (!lib->fp) {
138 lib->fp = fopen(lib->name, "rb");
140 if (!lib->fp) {
141 rdl_error = 1;
142 return 0;
144 } else
145 rewind(lib->fp);
147 while (!feof(lib->fp)) {
149 * read the module name from the file, and prepend
150 * the library name and '.' to it.
152 strcpy(buf, lib->name);
154 i = strlen(lib->name);
155 buf[i++] = '.';
156 t = i;
157 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
158 i++;
160 buf[i] = 0;
162 if (feof(lib->fp))
163 break;
164 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
165 nasm_read(&l, 4, lib->fp);
166 fseek(lib->fp, l, SEEK_CUR);
167 continue;
170 * open the RDOFF module
172 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
173 rdl_error = 16 * rdf_errno;
174 return 0;
177 * read in the header, and scan for exported symbols
179 hdr = nasm_malloc(f->header_len);
180 rdfloadseg(f, RDOFF_HEADER, hdr);
182 while ((r = rdfgetheaderrec(f))) {
183 if (r->type != 3) /* not an export */
184 continue;
186 if (!strcmp(r->e.label, label)) { /* match! */
187 nasm_free(hdr); /* reset to 'just open' */
188 f->header_loc = NULL; /* state... */
189 f->header_fp = 0;
190 return 1;
194 /* find start of next module... */
195 i = f->eof_offset;
196 rdfclose(f);
197 fseek(lib->fp, i, SEEK_SET);
201 * close the file if nobody else is using it
203 lib->referenced--;
204 if (!lib->referenced) {
205 fclose(lib->fp);
206 lib->fp = NULL;
208 return 0;
211 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
213 char buf[512];
214 int i, cmod, t;
215 int32_t length;
217 lib->referenced++;
219 if (!lib->fp) {
220 lib->fp = fopen(lib->name, "rb");
221 if (!lib->fp) {
222 lib->referenced--;
223 return (rdl_error = 1);
225 } else
226 rewind(lib->fp);
228 cmod = -1;
229 while (!feof(lib->fp)) {
230 strcpy(buf, lib->name);
231 i = strlen(buf);
232 buf[i++] = '.';
233 t = i;
234 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
235 i++;
236 buf[i] = 0;
237 if (feof(lib->fp))
238 break;
240 if (buf[t] != '.') /* special module - not counted in the numbering */
241 cmod++; /* of RDOFF modules - must be referred to by name */
243 if (cmod == moduleno) {
244 rdl_error = 16 *
245 rdfopenhere(f, lib->fp, &lib->referenced, buf);
246 lib->referenced--;
247 if (!lib->referenced) {
248 fclose(lib->fp);
249 lib->fp = NULL;
251 return rdl_error;
254 nasm_read(buf, 6, lib->fp);
255 buf[6] = 0;
256 if (buf[t] == '.') {
257 /* do nothing */
258 } else if (strncmp(buf, "RDOFF", 5)) {
259 if (!--lib->referenced) {
260 fclose(lib->fp);
261 lib->fp = NULL;
263 return rdl_error = 2;
264 } else if (buf[5] != '2') {
265 if (!--lib->referenced) {
266 fclose(lib->fp);
267 lib->fp = NULL;
269 return rdl_error = 3;
272 nasm_read(&length, 4, lib->fp);
273 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
275 if (!--lib->referenced) {
276 fclose(lib->fp);
277 lib->fp = NULL;
279 return rdl_error = 4; /* module not found */
282 void rdl_perror(const char *apname, const char *filename)
284 if (rdl_error >= 16)
285 rdfperror(apname, filename);
286 else
287 fprintf(stderr, "%s:%s:%s\n", apname, filename,
288 rdl_errors[rdl_error]);