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