Fixed three possible buffer overflows
[nasm.git] / rdoff / rdlib.c
blob038b6fc422424bbd1102abc7f7b60abb2a74bcfa
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;
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 fp = fopen(filename, "rb");
73 strcpy(lastverified, filename);
75 if (!fp)
76 return (rdl_error = lastresult = 1);
78 while (!feof(fp)) {
79 i = 0;
81 while (fread(buf + i, 1, 1, fp) == 1 && i < 257 && buf[i])
82 i++;
83 if (feof(fp))
84 break;
86 if (buf[0] == '.') {
88 * A special module, eg a signature block or a directory.
89 * Format of such a module is defined to be:
90 * six char type identifier
91 * int32_t count bytes content
92 * content
93 * so we can handle it uniformaly with RDOFF2 modules.
95 fread(buf, 6, 1, fp);
96 buf[6] = 0;
97 /* Currently, nothing useful to do with signature block.. */
98 } else {
99 fread(buf, 6, 1, fp);
100 buf[6] = 0;
101 if (strncmp(buf, "RDOFF", 5)) {
102 return rdl_error = lastresult = 2;
103 } else if (buf[5] != '2') {
104 return rdl_error = lastresult = 3;
107 fread(&length, 4, 1, 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 = strdup(name);
122 lib->referenced = 0;
123 lib->next = NULL;
124 return 0;
127 void rdl_close(struct librarynode *lib)
129 if (lib->fp)
130 fclose(lib->fp);
131 free(lib->name);
134 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
136 char buf[512];
137 int i, t;
138 void *hdr;
139 rdfheaderrec *r;
140 int32_t l;
142 rdl_error = 0;
143 lib->referenced++;
145 if (!lib->fp) {
146 lib->fp = fopen(lib->name, "rb");
148 if (!lib->fp) {
149 rdl_error = 1;
150 return 0;
152 } else
153 rewind(lib->fp);
155 while (!feof(lib->fp)) {
157 * read the module name from the file, and prepend
158 * the library name and '.' to it.
160 strcpy(buf, lib->name);
162 i = strlen(lib->name);
163 buf[i++] = '.';
164 t = i;
165 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
166 i++;
168 buf[i] = 0;
170 if (feof(lib->fp))
171 break;
172 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
173 fread(&l, 4, 1, lib->fp);
174 fseek(lib->fp, l, SEEK_CUR);
175 continue;
178 * open the RDOFF module
180 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
181 rdl_error = 16 * rdf_errno;
182 return 0;
185 * read in the header, and scan for exported symbols
187 hdr = malloc(f->header_len);
188 rdfloadseg(f, RDOFF_HEADER, hdr);
190 while ((r = rdfgetheaderrec(f))) {
191 if (r->type != 3) /* not an export */
192 continue;
194 if (!strcmp(r->e.label, label)) { /* match! */
195 free(hdr); /* reset to 'just open' */
196 f->header_loc = NULL; /* state... */
197 f->header_fp = 0;
198 return 1;
202 /* find start of next module... */
203 i = f->eof_offset;
204 rdfclose(f);
205 fseek(lib->fp, i, SEEK_SET);
209 * close the file if nobody else is using it
211 lib->referenced--;
212 if (!lib->referenced) {
213 fclose(lib->fp);
214 lib->fp = NULL;
216 return 0;
219 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
221 char buf[512];
222 int i, cmod, t;
223 int32_t length;
225 lib->referenced++;
227 if (!lib->fp) {
228 lib->fp = fopen(lib->name, "rb");
229 if (!lib->fp) {
230 lib->referenced--;
231 return (rdl_error = 1);
233 } else
234 rewind(lib->fp);
236 cmod = -1;
237 while (!feof(lib->fp)) {
238 strcpy(buf, lib->name);
239 i = strlen(buf);
240 buf[i++] = '.';
241 t = i;
242 while (fread(buf + i, 1, 1, lib->fp) == 1 && i < 512 && buf[i])
243 i++;
244 buf[i] = 0;
245 if (feof(lib->fp))
246 break;
248 if (buf[t] != '.') /* special module - not counted in the numbering */
249 cmod++; /* of RDOFF modules - must be referred to by name */
251 if (cmod == moduleno) {
252 rdl_error = 16 *
253 rdfopenhere(f, lib->fp, &lib->referenced, buf);
254 lib->referenced--;
255 if (!lib->referenced) {
256 fclose(lib->fp);
257 lib->fp = NULL;
259 return rdl_error;
262 fread(buf, 6, 1, lib->fp);
263 buf[6] = 0;
264 if (buf[t] == '.') {
265 /* do nothing */
266 } else if (strncmp(buf, "RDOFF", 5)) {
267 if (!--lib->referenced) {
268 fclose(lib->fp);
269 lib->fp = NULL;
271 return rdl_error = 2;
272 } else if (buf[5] != '2') {
273 if (!--lib->referenced) {
274 fclose(lib->fp);
275 lib->fp = NULL;
277 return rdl_error = 3;
280 fread(&length, 4, 1, lib->fp);
281 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
283 if (!--lib->referenced) {
284 fclose(lib->fp);
285 lib->fp = NULL;
287 return rdl_error = 4; /* module not found */
290 void rdl_perror(const char *apname, const char *filename)
292 if (rdl_error >= 16)
293 rdfperror(apname, filename);
294 else
295 fprintf(stderr, "%s:%s:%s\n", apname, filename,
296 rdl_errors[rdl_error]);