doc: Update changes
[nasm.git] / rdoff / rdlib.c
blobd8f2fc7a5c0fbb63529cd2e4bd4753151d04b46e
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>
42 #include <string.h>
44 #include "rdfutils.h"
45 #include "rdlib.h"
46 #include "rdlar.h"
48 /* See Texinfo documentation about new RDOFF libraries format */
50 int rdl_error = 0;
52 char *rdl_errors[5] = {
53 "no error", "could not open file", "invalid file structure",
54 "file contains modules of an unsupported RDOFF version",
55 "module not found"
58 int rdl_verify(const char *filename)
60 FILE *fp;
61 char buf[257];
62 int i;
63 int32_t length;
64 static char lastverified[256];
65 static int lastresult = -1;
67 if (lastresult != -1 && !strcmp(filename, lastverified))
68 return lastresult;
70 fp = fopen(filename, "rb");
71 strcpy(lastverified, filename);
73 if (!fp)
74 return (rdl_error = lastresult = 1);
76 while (!feof(fp)) {
77 i = 0;
79 while (fread(buf + i, 1, 1, fp) == 1 && i < 257 && buf[i])
80 i++;
81 if (feof(fp))
82 break;
84 if (buf[0] == '.') {
86 * A special module, eg a signature block or a directory.
87 * Format of such a module is defined to be:
88 * six char type identifier
89 * int32_t count bytes content
90 * content
91 * so we can handle it uniformaly with RDOFF2 modules.
93 nasm_read(buf, 6, fp);
94 buf[6] = 0;
95 /* Currently, nothing useful to do with signature block.. */
96 } else {
97 nasm_read(buf, 6, fp);
98 buf[6] = 0;
99 if (strncmp(buf, "RDOFF", 5)) {
100 fclose(fp);
101 return rdl_error = lastresult = 2;
102 } else if (buf[5] != '2') {
103 fclose(fp);
104 return rdl_error = lastresult = 3;
107 nasm_read(&length, 4, fp);
108 fseek(fp, length, SEEK_CUR); /* skip over the module */
110 fclose(fp);
111 return lastresult = 0; /* library in correct format */
114 int rdl_open(struct librarynode *lib, const char *name)
116 int i = rdl_verify(name);
117 if (i)
118 return i;
120 lib->fp = NULL;
121 lib->name = nasm_strdup(name);
122 lib->referenced = 0;
123 lib->next = NULL;
124 return 0;
127 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
129 char buf[512];
130 int i, t;
131 void *hdr;
132 rdfheaderrec *r;
133 int32_t l;
135 rdl_error = 0;
136 lib->referenced++;
138 if (!lib->fp) {
139 lib->fp = fopen(lib->name, "rb");
141 if (!lib->fp) {
142 rdl_error = 1;
143 return 0;
145 } else
146 rewind(lib->fp);
148 while (!feof(lib->fp)) {
150 * read the module name from the file, and prepend
151 * the library name and '.' to it.
153 strcpy(buf, lib->name);
155 i = strlen(lib->name);
156 buf[i++] = '.';
157 t = i;
158 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
159 i++;
161 buf[i] = 0;
163 if (feof(lib->fp))
164 break;
165 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
166 nasm_read(&l, 4, lib->fp);
167 fseek(lib->fp, l, SEEK_CUR);
168 continue;
171 * open the RDOFF module
173 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
174 rdl_error = 16 * rdf_errno;
175 return 0;
178 * read in the header, and scan for exported symbols
180 hdr = nasm_malloc(f->header_len);
181 rdfloadseg(f, RDOFF_HEADER, hdr);
183 while ((r = rdfgetheaderrec(f))) {
184 if (r->type != 3) /* not an export */
185 continue;
187 if (!strcmp(r->e.label, label)) { /* match! */
188 nasm_free(hdr); /* reset to 'just open' */
189 f->header_loc = NULL; /* state... */
190 f->header_fp = 0;
191 return 1;
195 /* find start of next module... */
196 i = f->eof_offset;
197 rdfclose(f);
198 fseek(lib->fp, i, SEEK_SET);
202 * close the file if nobody else is using it
204 lib->referenced--;
205 if (!lib->referenced) {
206 fclose(lib->fp);
207 lib->fp = NULL;
209 return 0;
212 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
214 char buf[512];
215 int i, cmod, t;
216 int32_t length;
218 lib->referenced++;
220 if (!lib->fp) {
221 lib->fp = fopen(lib->name, "rb");
222 if (!lib->fp) {
223 lib->referenced--;
224 return (rdl_error = 1);
226 } else
227 rewind(lib->fp);
229 cmod = -1;
230 while (!feof(lib->fp)) {
231 strcpy(buf, lib->name);
232 i = strlen(buf);
233 buf[i++] = '.';
234 t = i;
235 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
236 i++;
237 buf[i] = 0;
238 if (feof(lib->fp))
239 break;
241 if (buf[t] != '.') /* special module - not counted in the numbering */
242 cmod++; /* of RDOFF modules - must be referred to by name */
244 if (cmod == moduleno) {
245 rdl_error = 16 *
246 rdfopenhere(f, lib->fp, &lib->referenced, buf);
247 lib->referenced--;
248 if (!lib->referenced) {
249 fclose(lib->fp);
250 lib->fp = NULL;
252 return rdl_error;
255 nasm_read(buf, 6, lib->fp);
256 buf[6] = 0;
257 if (buf[t] == '.') {
258 /* do nothing */
259 } else if (strncmp(buf, "RDOFF", 5)) {
260 if (!--lib->referenced) {
261 fclose(lib->fp);
262 lib->fp = NULL;
264 return rdl_error = 2;
265 } else if (buf[5] != '2') {
266 if (!--lib->referenced) {
267 fclose(lib->fp);
268 lib->fp = NULL;
270 return rdl_error = 3;
273 nasm_read(&length, 4, lib->fp);
274 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
276 if (!--lib->referenced) {
277 fclose(lib->fp);
278 lib->fp = NULL;
280 return rdl_error = 4; /* module not found */
283 void rdl_perror(const char *apname, const char *filename)
285 if (rdl_error >= 16)
286 rdfperror(apname, filename);
287 else
288 fprintf(stderr, "%s:%s:%s\n", apname, filename,
289 rdl_errors[rdl_error]);