blob: 06cce3c79eaaf13a2163bd1bab19c729065d57df [file] [log] [blame]
H. Peter Anvinc8578ce2009-06-28 17:33:50 -07001/* ----------------------------------------------------------------------- *
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.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
17 *
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.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000034/* rdflib - manipulate RDOFF library files (.rdl) */
35
H. Peter Anvin41bf8002002-04-30 20:58:18 +000036/*
37 * an rdoff library is simply a sequence of RDOFF object files, each
38 * preceded by the name of the module, an ASCII string of up to 255
Keith Kaniosa6dfa782007-04-13 16:47:53 +000039 * characters, terminated by a zero.
H. Peter Anvin27cf5032002-05-04 05:42:30 +000040 *
41 * When a library is being created, special signature block is placed
42 * in the beginning of the file. It is a string 'RDLIB' followed by a
Keith Kaniosb7a89542007-04-12 02:40:54 +000043 * version number, then int32_t content size and a int32_t time stamp.
H. Peter Anvin27cf5032002-05-04 05:42:30 +000044 * The module name of the signature block is '.sig'.
45 *
H. Peter Anvin41bf8002002-04-30 20:58:18 +000046 *
H. Peter Anvin225c5922002-04-30 21:06:16 +000047 * There may be an optional directory placed on the end of the file.
48 * The format of the directory will be 'RDLDD' followed by a version
49 * number, followed by the length of the directory, and then the
50 * directory, the format of which has not yet been designed.
H. Peter Anvin70653092007-10-19 14:42:29 -070051 * The module name of the directory must be '.dir'.
H. Peter Anvin41bf8002002-04-30 20:58:18 +000052 *
H. Peter Anvin27cf5032002-05-04 05:42:30 +000053 * All module names beginning with '.' are reserved for possible future
54 * extensions. The linker ignores all such modules, assuming they have
H. Peter Anvin038d8612007-04-12 16:54:50 +000055 * the format of a six uint8_t type & version identifier followed by int32_t
H. Peter Anvin27cf5032002-05-04 05:42:30 +000056 * content size, followed by data.
H. Peter Anvin41bf8002002-04-30 20:58:18 +000057 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000058
H. Peter Anvinfe501952007-10-02 21:53:51 -070059#include "compiler.h"
60
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061#include <stdio.h>
H. Peter Anvinfa969e52002-12-05 19:33:20 +000062#include <stdlib.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000063#include <errno.h>
64#include <string.h>
H. Peter Anvin038d8612007-04-12 16:54:50 +000065#include <inttypes.h>
H. Peter Anvin27cf5032002-05-04 05:42:30 +000066#include <time.h>
Keith Kanios82e15cd2007-04-12 16:23:11 +000067#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000068
69/* functions supported:
H. Peter Anvin225c5922002-04-30 21:06:16 +000070 * create a library (no extra operands required)
71 * add a module from a library (requires filename and name to give mod.)
72 * replace a module in a library (requires given name and filename)
73 * delete a module from a library (requires given name)
74 * extract a module from the library (requires given name and filename)
75 * list modules
76 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000077
H. Peter Anvin038d8612007-04-12 16:54:50 +000078const char *usage =
H. Peter Anvine2c80182005-01-15 22:15:51 +000079 "usage:\n"
80 " rdflib x libname [extra operands]\n\n"
81 " where x is one of:\n"
82 " c - create library\n"
83 " a - add module (operands = filename module-name)\n"
84 " x - extract (module-name filename)\n"
85 " r - replace (module-name filename)\n"
86 " d - delete (module-name)\n" " t - list\n";
87
H. Peter Anvin27cf5032002-05-04 05:42:30 +000088/* Library signature */
Keith Kaniosa6dfa782007-04-13 16:47:53 +000089const char *rdl_signature = "RDLIB2", *sig_modname = ".sig";
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090
Keith Kanios82e15cd2007-04-12 16:23:11 +000091char **_argv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092
H. Peter Anvine2c80182005-01-15 22:15:51 +000093#define _ENDIANNESS 0 /* 0 for little, 1 for big */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
Keith Kaniosb7a89542007-04-12 02:40:54 +000095static void int32_ttolocal(int32_t *l)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000096{
97#if _ENDIANNESS
Keith Kaniosb7a89542007-04-12 02:40:54 +000098 uint8_t t;
99 uint8_t *p = (uint8_t *)l;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100
101 t = p[0];
102 p[0] = p[3];
103 p[3] = t;
104 t = p[1];
105 p[1] = p[2];
106 p[2] = p[1];
Keith Kanios47776ac2007-04-14 01:24:14 +0000107#else
108 (void)l; /* placate optimizers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000109#endif
110}
111
H. Peter Anvin48166382014-11-25 12:34:03 -0800112static char copybytes(FILE * fp, FILE * fp2, int n)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113{
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000114 int i, t = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116 for (i = 0; i < n; i++) {
117 t = fgetc(fp);
118 if (t == EOF) {
119 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
120 _argv[2]);
121 exit(1);
122 }
123 if (fp2)
124 if (fputc(t, fp2) == EOF) {
125 fprintf(stderr, "rdflib: write error\n");
126 exit(1);
127 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000128 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000129 return (char)t; /* return last char read */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130}
131
H. Peter Anvin48166382014-11-25 12:34:03 -0800132static int32_t copyint32_t(FILE * fp, FILE * fp2)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000133{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000134 int32_t l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000135 int i, t;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000136 uint8_t *p = (uint8_t *)&l;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000137
H. Peter Anvine2c80182005-01-15 22:15:51 +0000138 for (i = 0; i < 4; i++) { /* skip magic no */
139 t = fgetc(fp);
140 if (t == EOF) {
141 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
142 _argv[2]);
143 exit(1);
144 }
145 if (fp2)
146 if (fputc(t, fp2) == EOF) {
147 fprintf(stderr, "rdflib: write error\n");
148 exit(1);
149 }
150 *p++ = t;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000151 }
Keith Kaniosb7a89542007-04-12 02:40:54 +0000152 int32_ttolocal(&l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000153 return l;
154}
155
Keith Kanios82e15cd2007-04-12 16:23:11 +0000156int main(int argc, char **argv)
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157{
H. Peter Anvin27cf5032002-05-04 05:42:30 +0000158 FILE *fp, *fp2 = NULL, *fptmp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000159 char *p, buf[256], c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000160 int i;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000161 int32_t l;
H. Peter Anvin27cf5032002-05-04 05:42:30 +0000162 time_t t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000163 char rdbuf[10];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000164
165 _argv = argv;
166
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167 if (argc < 3 || !strncmp(argv[1], "-h", 2)
168 || !strncmp(argv[1], "--h", 3)) {
Victor van den Elzen0dd450c2009-08-13 15:07:59 +0200169 fputs(usage, stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000170 exit(1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000171 }
172
H. Peter Anvine2c80182005-01-15 22:15:51 +0000173 switch (argv[1][0]) {
174 case 'c': /* create library */
175 fp = fopen(argv[2], "wb");
176 if (!fp) {
177 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
178 perror("rdflib");
179 exit(1);
180 }
181 fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp);
182 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
H. Peter Anvin2bc798a2016-02-12 21:21:57 -0800183 t = time(NULL);
184 l = sizeof(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000185 fwrite(&l, sizeof(l), 1, fp);
186 fwrite(&t, 1, l, fp);
187 fclose(fp);
188 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 case 'a': /* add module */
191 if (argc < 5) {
192 fprintf(stderr, "rdflib: required parameter missing\n");
193 exit(1);
194 }
195 fp = fopen(argv[2], "ab");
196 if (!fp) {
197 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
198 perror("rdflib");
199 exit(1);
200 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201
H. Peter Anvine2c80182005-01-15 22:15:51 +0000202 fp2 = fopen(argv[3], "rb");
203 if (!fp2) {
204 fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]);
205 perror("rdflib");
206 exit(1);
207 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209 p = argv[4];
210 do {
211 if (fputc(*p, fp) == EOF) {
212 fprintf(stderr, "rdflib: write error\n");
213 exit(1);
214 }
215 } while (*p++);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 while (!feof(fp2)) {
218 i = fgetc(fp2);
219 if (i == EOF) {
220 break;
221 }
222
223 if (fputc(i, fp) == EOF) {
224 fprintf(stderr, "rdflib: write error\n");
225 exit(1);
226 }
227 }
228 fclose(fp2);
229 fclose(fp);
230 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231
232 case 'x':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000233 if (argc < 5) {
234 fprintf(stderr, "rdflib: required parameter missing\n");
235 exit(1);
236 }
H. Peter Anvin225c5922002-04-30 21:06:16 +0000237 case 't':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 fp = fopen(argv[2], "rb");
239 if (!fp) {
240 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
241 perror("rdflib");
242 exit(1);
243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000244
H. Peter Anvine2c80182005-01-15 22:15:51 +0000245 fp2 = NULL;
246 while (!feof(fp)) {
247 /* read name */
248 p = buf;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000249 while ((*(p++) = (char)fgetc(fp)))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000250 if (feof(fp))
251 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000252
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 if (feof(fp))
254 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 fp2 = NULL;
257 if (argv[1][0] == 'x') {
258 /* check against desired name */
259 if (!strcmp(buf, argv[3])) {
260 fp2 = fopen(argv[4], "wb");
261 if (!fp2) {
262 fprintf(stderr, "rdflib: could not open '%s'\n",
263 argv[4]);
264 perror("rdflib");
265 exit(1);
266 }
267 }
268 } else
269 printf("%-40s ", buf);
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000270
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271 /* step over the RDOFF file, extracting type information for
272 * the listing, and copying it if fp2 != NULL */
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000273
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274 if (buf[0] == '.') {
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000275
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276 if (argv[1][0] == 't')
277 for (i = 0; i < 6; i++)
278 printf("%c", copybytes(fp, fp2, 1));
279 else
280 copybytes(fp, fp2, 6);
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000281
Keith Kaniosb7a89542007-04-12 02:40:54 +0000282 l = copyint32_t(fp, fp2);
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000283
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284 if (argv[1][0] == 't')
Keith Kanios47776ac2007-04-14 01:24:14 +0000285 printf(" %"PRId32" bytes content\n", l);
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000286
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 copybytes(fp, fp2, l);
288 } else if ((c = copybytes(fp, fp2, 6)) >= '2') { /* version 2 or above */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000289 l = copyint32_t(fp, fp2);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291 if (argv[1][0] == 't')
Keith Kanios47776ac2007-04-14 01:24:14 +0000292 printf("RDOFF%c %"PRId32" bytes content\n", c, l);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293 copybytes(fp, fp2, l); /* entire object */
294 } else {
295 if (argv[1][0] == 't')
296 printf("RDOFF1\n");
297 /*
298 * version 1 object, so we don't have an object content
299 * length field.
300 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000301 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* header */
302 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* text */
303 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* data */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304 }
H. Peter Anvin225c5922002-04-30 21:06:16 +0000305
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306 if (fp2)
307 break;
308 }
309 fclose(fp);
310 if (fp2)
311 fclose(fp2);
312 else if (argv[1][0] == 'x') {
313 fprintf(stderr, "rdflib: module '%s' not found in '%s'\n",
314 argv[3], argv[2]);
315 exit(1);
316 }
317 break;
H. Peter Anvin225c5922002-04-30 21:06:16 +0000318
H. Peter Anvine2c80182005-01-15 22:15:51 +0000319 case 'r': /* replace module */
320 argc--;
321 case 'd': /* delete module */
322 if (argc < 4) {
323 fprintf(stderr, "rdflib: required parameter missing\n");
324 exit(1);
325 }
H. Peter Anvin225c5922002-04-30 21:06:16 +0000326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327 fp = fopen(argv[2], "rb");
328 if (!fp) {
329 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
330 perror("rdflib");
331 exit(1);
332 }
333
334 if (argv[1][0] == 'r') {
335 fp2 = fopen(argv[4], "rb");
336 if (!fp2) {
337 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
338 perror("rdflib");
339 exit(1);
340 }
341 }
342
343 fptmp = tmpfile();
344 if (!fptmp) {
345 fprintf(stderr, "rdflib: could not open temporary file\n");
346 perror("rdflib");
347 exit(1);
348 }
349
350 /* copy library into temporary file */
351 fseek(fp, 0, SEEK_END); /* get file length */
352 l = ftell(fp);
353 fseek(fp, 0, SEEK_SET);
354 copybytes(fp, fptmp, l);
355 rewind(fptmp);
356 freopen(argv[2], "wb", fp);
357
358 while (!feof(fptmp)) {
359 /* read name */
360 p = buf;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000361 while ((*(p++) = (char)fgetc(fptmp)))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000362 if (feof(fptmp))
363 break;
364
365 if (feof(fptmp))
366 break;
367
368 /* check against desired name */
369 if (!strcmp(buf, argv[3])) {
370 fread(p = rdbuf, 1, sizeof(rdbuf), fptmp);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000371 l = *(int32_t *)(p + 6);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 fseek(fptmp, l, SEEK_CUR);
373 break;
374 } else {
375 fwrite(buf, 1, strlen(buf) + 1, fp); /* module name */
376 if ((c = copybytes(fptmp, fp, 6)) >= '2') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000377 l = copyint32_t(fptmp, fp); /* version 2 or above */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 copybytes(fptmp, fp, l); /* entire object */
379 }
380 }
381 }
382
383 if (argv[1][0] == 'r') {
384 /* copy new module into library */
385 p = argv[3];
386 do {
387 if (fputc(*p, fp) == EOF) {
388 fprintf(stderr, "rdflib: write error\n");
389 exit(1);
390 }
391 } while (*p++);
392
393 while (!feof(fp2)) {
394 i = fgetc(fp2);
395 if (i == EOF) {
396 break;
397 }
398 if (fputc(i, fp) == EOF) {
399 fprintf(stderr, "rdflib: write error\n");
400 exit(1);
401 }
402 }
403 fclose(fp2);
404 }
405
406 /* copy rest of library if any */
407 while (!feof(fptmp)) {
408 i = fgetc(fptmp);
409 if (i == EOF) {
410 break;
411 }
412
413 if (fputc(i, fp) == EOF) {
414 fprintf(stderr, "rdflib: write error\n");
415 exit(1);
416 }
417 }
418
419 fclose(fp);
420 fclose(fptmp);
421 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422
423 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 fprintf(stderr, "rdflib: command '%c' not recognized\n",
425 argv[1][0]);
426 exit(1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 }
428 return 0;
429}