Formatting: kill off "stealth whitespace"
[platform/upstream/nasm.git] / rdoff / rdlib.c
1 /*
2  * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
3  */
4
5 #include "compiler.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #define RDOFF_UTILS
12
13 #include "rdoff.h"
14 #include "rdlib.h"
15 #include "rdlar.h"
16
17 /* See Texinfo documentation about new RDOFF libraries format */
18
19 int rdl_error = 0;
20
21 char *rdl_errors[5] = {
22     "no error", "could not open file", "invalid file structure",
23     "file contains modules of an unsupported RDOFF version",
24     "module not found"
25 };
26
27 int rdl_verify(const char *filename)
28 {
29     FILE *fp = fopen(filename, "rb");
30     char buf[257];
31     int i;
32     int32_t length;
33     static char lastverified[256];
34     static int lastresult = -1;
35
36     if (lastresult != -1 && !strcmp(filename, lastverified))
37         return lastresult;
38
39     strcpy(lastverified, filename);
40
41     if (!fp)
42         return (rdl_error = lastresult = 1);
43
44     while (!feof(fp)) {
45         i = 0;
46
47         while (fread(buf + i, 1, 1, fp) == 1 && buf[i] && i < 257)
48             i++;
49         if (feof(fp))
50             break;
51
52         if (buf[0] == '.') {
53             /*
54              * A special module, eg a signature block or a directory.
55              * Format of such a module is defined to be:
56              *   six char type identifier
57              *   int32_t count bytes content
58              *   content
59              * so we can handle it uniformaly with RDOFF2 modules.
60              */
61             fread(buf, 6, 1, fp);
62             buf[6] = 0;
63             /* Currently, nothing useful to do with signature block.. */
64         } else {
65             fread(buf, 6, 1, fp);
66             buf[6] = 0;
67             if (strncmp(buf, "RDOFF", 5)) {
68                 return rdl_error = lastresult = 2;
69             } else if (buf[5] != '2') {
70                 return rdl_error = lastresult = 3;
71             }
72         }
73         fread(&length, 4, 1, fp);
74         fseek(fp, length, SEEK_CUR);    /* skip over the module */
75     }
76     fclose(fp);
77     return lastresult = 0;      /* library in correct format */
78 }
79
80 int rdl_open(struct librarynode *lib, const char *name)
81 {
82     int i = rdl_verify(name);
83     if (i)
84         return i;
85
86     lib->fp = NULL;
87     lib->name = strdup(name);
88     lib->referenced = 0;
89     lib->next = NULL;
90     return 0;
91 }
92
93 void rdl_close(struct librarynode *lib)
94 {
95     if (lib->fp)
96         fclose(lib->fp);
97     free(lib->name);
98 }
99
100 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
101 {
102     char buf[512];
103     int i, t;
104     void *hdr;
105     rdfheaderrec *r;
106     int32_t l;
107
108     rdl_error = 0;
109     lib->referenced++;
110
111     if (!lib->fp) {
112         lib->fp = fopen(lib->name, "rb");
113
114         if (!lib->fp) {
115             rdl_error = 1;
116             return 0;
117         }
118     } else
119         rewind(lib->fp);
120
121     while (!feof(lib->fp)) {
122         /*
123          * read the module name from the file, and prepend
124          * the library name and '.' to it.
125          */
126         strcpy(buf, lib->name);
127
128         i = strlen(lib->name);
129         buf[i++] = '.';
130         t = i;
131         while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
132             i++;
133
134         buf[i] = 0;
135
136         if (feof(lib->fp))
137             break;
138         if (!strcmp(buf + t, ".dir")) { /* skip over directory */
139             fread(&l, 4, 1, lib->fp);
140             fseek(lib->fp, l, SEEK_CUR);
141             continue;
142         }
143         /*
144          * open the RDOFF module
145          */
146         if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
147             rdl_error = 16 * rdf_errno;
148             return 0;
149         }
150         /*
151          * read in the header, and scan for exported symbols
152          */
153         hdr = malloc(f->header_len);
154         rdfloadseg(f, RDOFF_HEADER, hdr);
155
156         while ((r = rdfgetheaderrec(f))) {
157             if (r->type != 3)   /* not an export */
158                 continue;
159
160             if (!strcmp(r->e.label, label)) {   /* match! */
161                 free(hdr);      /* reset to 'just open' */
162                 f->header_loc = NULL;   /* state... */
163                 f->header_fp = 0;
164                 return 1;
165             }
166         }
167
168         /* find start of next module... */
169         i = f->eof_offset;
170         rdfclose(f);
171         fseek(lib->fp, i, SEEK_SET);
172     }
173
174     /*
175      * close the file if nobody else is using it
176      */
177     lib->referenced--;
178     if (!lib->referenced) {
179         fclose(lib->fp);
180         lib->fp = NULL;
181     }
182     return 0;
183 }
184
185 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
186 {
187     char buf[512];
188     int i, cmod, t;
189     int32_t length;
190
191     lib->referenced++;
192
193     if (!lib->fp) {
194         lib->fp = fopen(lib->name, "rb");
195         if (!lib->fp) {
196             lib->referenced--;
197             return (rdl_error = 1);
198         }
199     } else
200         rewind(lib->fp);
201
202     cmod = -1;
203     while (!feof(lib->fp)) {
204         strcpy(buf, lib->name);
205         i = strlen(buf);
206         buf[i++] = '.';
207         t = i;
208         while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
209             i++;
210         buf[i] = 0;
211         if (feof(lib->fp))
212             break;
213
214         if (buf[t] != '.')      /* special module - not counted in the numbering */
215             cmod++;             /* of RDOFF modules - must be referred to by name */
216
217         if (cmod == moduleno) {
218             rdl_error = 16 *
219                 rdfopenhere(f, lib->fp, &lib->referenced, buf);
220             lib->referenced--;
221             if (!lib->referenced) {
222                 fclose(lib->fp);
223                 lib->fp = NULL;
224             }
225             return rdl_error;
226         }
227
228         fread(buf, 6, 1, lib->fp);
229         buf[6] = 0;
230         if (buf[t] == '.') {
231             /* do nothing */
232         } else if (strncmp(buf, "RDOFF", 5)) {
233             if (!--lib->referenced) {
234                 fclose(lib->fp);
235                 lib->fp = NULL;
236             }
237             return rdl_error = 2;
238         } else if (buf[5] != '2') {
239             if (!--lib->referenced) {
240                 fclose(lib->fp);
241                 lib->fp = NULL;
242             }
243             return rdl_error = 3;
244         }
245
246         fread(&length, 4, 1, lib->fp);
247         fseek(lib->fp, length, SEEK_CUR);       /* skip over the module */
248     }
249     if (!--lib->referenced) {
250         fclose(lib->fp);
251         lib->fp = NULL;
252     }
253     return rdl_error = 4;       /* module not found */
254 }
255
256 void rdl_perror(const char *apname, const char *filename)
257 {
258     if (rdl_error >= 16)
259         rdfperror(apname, filename);
260     else
261         fprintf(stderr, "%s:%s:%s\n", apname, filename,
262                 rdl_errors[rdl_error]);
263 }