1 /* ----------------------------------------------------------------------- *
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
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 * ----------------------------------------------------------------------- */
34 /* rdflib - manipulate RDOFF library files (.rdl) */
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
39 * characters, terminated by a zero.
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
43 * version number, then int32_t content size and a int32_t time stamp.
44 * The module name of the signature block is '.sig'.
47 * 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.
51 * The module name of the directory must be '.dir'.
53 * All module names beginning with '.' are reserved for possible future
54 * extensions. The linker ignores all such modules, assuming they have
55 * the format of a six uint8_t type & version identifier followed by int32_t
56 * content size, followed by data.
68 /* functions supported:
69 * create a library (no extra operands required)
70 * add a module from a library (requires filename and name to give mod.)
71 * replace a module in a library (requires given name and filename)
72 * delete a module from a library (requires given name)
73 * extract a module from the library (requires given name and filename)
79 " rdflib x libname [extra operands]\n\n"
80 " where x is one of:\n"
81 " c - create library\n"
82 " a - add module (operands = filename module-name)\n"
83 " x - extract (module-name filename)\n"
84 " r - replace (module-name filename)\n"
85 " d - delete (module-name)\n" " t - list\n";
87 /* Library signature */
88 const char *rdl_signature
= "RDLIB2", *sig_modname
= ".sig";
92 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
94 static void int32_ttolocal(int32_t *l
)
98 uint8_t *p
= (uint8_t *)l
;
107 (void)l
; /* placate optimizers */
111 static char copybytes(FILE * fp
, FILE * fp2
, int n
)
115 for (i
= 0; i
< n
; i
++) {
118 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
123 if (fputc(t
, fp2
) == EOF
) {
124 fprintf(stderr
, "rdflib: write error\n");
128 return (char)t
; /* return last char read */
131 static int32_t copyint32_t(FILE * fp
, FILE * fp2
)
135 uint8_t *p
= (uint8_t *)&l
;
137 for (i
= 0; i
< 4; i
++) { /* skip magic no */
140 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
145 if (fputc(t
, fp2
) == EOF
) {
146 fprintf(stderr
, "rdflib: write error\n");
155 int main(int argc
, char **argv
)
157 FILE *fp
, *fp2
= NULL
, *fptmp
;
158 char *p
, buf
[256], c
;
166 if (argc
< 3 || !strncmp(argv
[1], "-h", 2)
167 || !strncmp(argv
[1], "--h", 3)) {
168 fputs(usage
, stdout
);
174 switch (argv
[1][0]) {
175 case 'c': /* create library */
176 fp
= fopen(argv
[2], "wb");
178 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
182 nasm_write(sig_modname
, strlen(sig_modname
) + 1, fp
);
183 nasm_write(rdl_signature
, strlen(rdl_signature
), fp
);
185 fwriteint32_t(t
, fp
);
189 case 'a': /* add module */
191 fprintf(stderr
, "rdflib: required parameter missing\n");
194 fp
= fopen(argv
[2], "ab");
196 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
201 fp2
= fopen(argv
[3], "rb");
203 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[3]);
210 if (fputc(*p
, fp
) == EOF
) {
211 fprintf(stderr
, "rdflib: write error\n");
222 if (fputc(i
, fp
) == EOF
) {
223 fprintf(stderr
, "rdflib: write error\n");
233 fprintf(stderr
, "rdflib: required parameter missing\n");
238 fp
= fopen(argv
[2], "rb");
240 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
249 while ((*(p
++) = (char)fgetc(fp
)))
257 if (argv
[1][0] == 'x') {
258 /* check against desired name */
259 if (!strcmp(buf
, argv
[3])) {
260 fp2
= fopen(argv
[4], "wb");
262 fprintf(stderr
, "rdflib: could not open '%s'\n",
269 printf("%-40s ", buf
);
271 /* step over the RDOFF file, extracting type information for
272 * the listing, and copying it if fp2 != NULL */
276 if (argv
[1][0] == 't')
277 for (i
= 0; i
< 6; i
++)
278 printf("%c", copybytes(fp
, fp2
, 1));
280 copybytes(fp
, fp2
, 6);
282 l
= copyint32_t(fp
, fp2
);
284 if (argv
[1][0] == 't')
285 printf(" %"PRId32
" bytes content\n", l
);
287 copybytes(fp
, fp2
, l
);
288 } else if ((c
= copybytes(fp
, fp2
, 6)) >= '2') { /* version 2 or above */
289 l
= copyint32_t(fp
, fp2
);
291 if (argv
[1][0] == 't')
292 printf("RDOFF%c %"PRId32
" bytes content\n", c
, l
);
293 copybytes(fp
, fp2
, l
); /* entire object */
295 if (argv
[1][0] == 't')
298 * version 1 object, so we don't have an object content
301 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 */
312 else if (argv
[1][0] == 'x') {
313 fprintf(stderr
, "rdflib: module '%s' not found in '%s'\n",
319 case 'r': /* replace module */
323 case 'd': /* delete module */
325 fprintf(stderr
, "rdflib: required parameter missing\n");
329 fp
= fopen(argv
[2], "rb");
331 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
336 if (argv
[1][0] == 'r') {
337 fp2
= fopen(argv
[4], "rb");
339 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
347 fprintf(stderr
, "rdflib: could not open temporary file\n");
352 /* copy library into temporary file */
353 fseek(fp
, 0, SEEK_END
); /* get file length */
355 fseek(fp
, 0, SEEK_SET
);
356 copybytes(fp
, fptmp
, l
);
358 if (freopen(argv
[2], "wb", fp
) == NULL
) {
359 fprintf(stderr
, "rdflib: could not reopen '%s'\n", argv
[2]);
364 while (!feof(fptmp
)) {
367 while ((*(p
++) = (char)fgetc(fptmp
)))
374 /* check against desired name */
375 if (!strcmp(buf
, argv
[3])) {
376 if (fread(p
= rdbuf
, 1, sizeof(rdbuf
), fptmp
) < 10) {
377 nasm_fatal("short read on input");
379 l
= *(int32_t *)(p
+ 6);
380 fseek(fptmp
, l
, SEEK_CUR
);
383 nasm_write(buf
, strlen(buf
) + 1, fp
); /* module name */
384 if ((c
= copybytes(fptmp
, fp
, 6)) >= '2') {
385 l
= copyint32_t(fptmp
, fp
); /* version 2 or above */
386 copybytes(fptmp
, fp
, l
); /* entire object */
391 if (argv
[1][0] == 'r') {
392 /* copy new module into library */
395 if (fputc(*p
, fp
) == EOF
) {
396 fprintf(stderr
, "rdflib: write error\n");
406 if (fputc(i
, fp
) == EOF
) {
407 fprintf(stderr
, "rdflib: write error\n");
414 /* copy rest of library if any */
415 while (!feof(fptmp
)) {
421 if (fputc(i
, fp
) == EOF
) {
422 fprintf(stderr
, "rdflib: write error\n");
432 fprintf(stderr
, "rdflib: command '%c' not recognized\n",