doc: Update changes
[nasm/avx512.git] / rdoff / rdlib.c
blob31dbdb4b7b17397818756e9ffaa384ab0bd16bd5
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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 = fopen(filename, "rb");
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 strcpy(lastverified, filename);
74 if (!fp)
75 return (rdl_error = lastresult = 1);
77 while (!feof(fp)) {
78 i = 0;
80 while (fread(buf + i, 1, 1, fp) == 1 && buf[i] && i < 257)
81 i++;
82 if (feof(fp))
83 break;
85 if (buf[0] == '.') {
87 * A special module, eg a signature block or a directory.
88 * Format of such a module is defined to be:
89 * six char type identifier
90 * int32_t count bytes content
91 * content
92 * so we can handle it uniformaly with RDOFF2 modules.
94 fread(buf, 6, 1, fp);
95 buf[6] = 0;
96 /* Currently, nothing useful to do with signature block.. */
97 } else {
98 fread(buf, 6, 1, fp);
99 buf[6] = 0;
100 if (strncmp(buf, "RDOFF", 5)) {
101 return rdl_error = lastresult = 2;
102 } else if (buf[5] != '2') {
103 return rdl_error = lastresult = 3;
106 fread(&length, 4, 1, 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 = strdup(name);
121 lib->referenced = 0;
122 lib->next = NULL;
123 return 0;
126 void rdl_close(struct librarynode *lib)
128 if (lib->fp)
129 fclose(lib->fp);
130 free(lib->name);
133 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
135 char buf[512];
136 int i, t;
137 void *hdr;
138 rdfheaderrec *r;
139 int32_t l;
141 rdl_error = 0;
142 lib->referenced++;
144 if (!lib->fp) {
145 lib->fp = fopen(lib->name, "rb");
147 if (!lib->fp) {
148 rdl_error = 1;
149 return 0;
151 } else
152 rewind(lib->fp);
154 while (!feof(lib->fp)) {
156 * read the module name from the file, and prepend
157 * the library name and '.' to it.
159 strcpy(buf, lib->name);
161 i = strlen(lib->name);
162 buf[i++] = '.';
163 t = i;
164 while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
165 i++;
167 buf[i] = 0;
169 if (feof(lib->fp))
170 break;
171 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
172 fread(&l, 4, 1, lib->fp);
173 fseek(lib->fp, l, SEEK_CUR);
174 continue;
177 * open the RDOFF module
179 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
180 rdl_error = 16 * rdf_errno;
181 return 0;
184 * read in the header, and scan for exported symbols
186 hdr = malloc(f->header_len);
187 rdfloadseg(f, RDOFF_HEADER, hdr);
189 while ((r = rdfgetheaderrec(f))) {
190 if (r->type != 3) /* not an export */
191 continue;
193 if (!strcmp(r->e.label, label)) { /* match! */
194 free(hdr); /* reset to 'just open' */
195 f->header_loc = NULL; /* state... */
196 f->header_fp = 0;
197 return 1;
201 /* find start of next module... */
202 i = f->eof_offset;
203 rdfclose(f);
204 fseek(lib->fp, i, SEEK_SET);
208 * close the file if nobody else is using it
210 lib->referenced--;
211 if (!lib->referenced) {
212 fclose(lib->fp);
213 lib->fp = NULL;
215 return 0;
218 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
220 char buf[512];
221 int i, cmod, t;
222 int32_t length;
224 lib->referenced++;
226 if (!lib->fp) {
227 lib->fp = fopen(lib->name, "rb");
228 if (!lib->fp) {
229 lib->referenced--;
230 return (rdl_error = 1);
232 } else
233 rewind(lib->fp);
235 cmod = -1;
236 while (!feof(lib->fp)) {
237 strcpy(buf, lib->name);
238 i = strlen(buf);
239 buf[i++] = '.';
240 t = i;
241 while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
242 i++;
243 buf[i] = 0;
244 if (feof(lib->fp))
245 break;
247 if (buf[t] != '.') /* special module - not counted in the numbering */
248 cmod++; /* of RDOFF modules - must be referred to by name */
250 if (cmod == moduleno) {
251 rdl_error = 16 *
252 rdfopenhere(f, lib->fp, &lib->referenced, buf);
253 lib->referenced--;
254 if (!lib->referenced) {
255 fclose(lib->fp);
256 lib->fp = NULL;
258 return rdl_error;
261 fread(buf, 6, 1, lib->fp);
262 buf[6] = 0;
263 if (buf[t] == '.') {
264 /* do nothing */
265 } else if (strncmp(buf, "RDOFF", 5)) {
266 if (!--lib->referenced) {
267 fclose(lib->fp);
268 lib->fp = NULL;
270 return rdl_error = 2;
271 } else if (buf[5] != '2') {
272 if (!--lib->referenced) {
273 fclose(lib->fp);
274 lib->fp = NULL;
276 return rdl_error = 3;
279 fread(&length, 4, 1, lib->fp);
280 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
282 if (!--lib->referenced) {
283 fclose(lib->fp);
284 lib->fp = NULL;
286 return rdl_error = 4; /* module not found */
289 void rdl_perror(const char *apname, const char *filename)
291 if (rdl_error >= 16)
292 rdfperror(apname, filename);
293 else
294 fprintf(stderr, "%s:%s:%s\n", apname, filename,
295 rdl_errors[rdl_error]);