aboutsummaryrefslogtreecommitdiffstats
path: root/rdoff/rdflib.c
blob: 82958776cf3fd26b71126d10853a5df4d6e50767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* rdflib - manipulate RDOFF library files (.rdl) */

/*
 * an rdoff library is simply a sequence of RDOFF object files, each
 * preceded by the name of the module, an ASCII string of up to 255
 * int8_tacters, terminated by a zero.
 *
 * When a library is being created, special signature block is placed
 * in the beginning of the file. It is a string 'RDLIB' followed by a
 * version number, then int32_t content size and a int32_t time stamp.
 * The module name of the signature block is '.sig'.
 *
 *
 * There may be an optional directory placed on the end of the file.
 * The format of the directory will be 'RDLDD' followed by a version
 * number, followed by the length of the directory, and then the
 * directory, the format of which has not yet been designed.
 * The module name of the directory must be '.dir'. 
 *
 * All module names beginning with '.' are reserved for possible future
 * extensions. The linker ignores all such modules, assuming they have
 * the format of a six uint8_t type & version identifier followed by int32_t
 * content size, followed by data.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <inttypes.h>

/* functions supported:
 *   create a library	(no extra operands required)
 *   add a module from a library (requires filename and name to give mod.)
 *   replace a module in a library (requires given name and filename)
 *   delete a module from a library (requires given name)
 *   extract a module from the library (requires given name and filename)
 *   list modules
 */

const char *usage =
    "usage:\n"
    "  rdflib x libname [extra operands]\n\n"
    "  where x is one of:\n"
    "    c - create library\n"
    "    a - add module (operands = filename module-name)\n"
    "    x - extract               (module-name filename)\n"
    "    r - replace               (module-name filename)\n"
    "    d - delete                (module-name)\n" "    t - list\n";

/* Library signature */
const int8_t *rdl_signature = "RDLIB2", *sig_modname = ".sig";

char **_argv;

#define _ENDIANNESS 0           /* 0 for little, 1 for big */

static void int32_ttolocal(int32_t *l)
{
#if _ENDIANNESS
    uint8_t t;
    uint8_t *p = (uint8_t *)l;

    t = p[0];
    p[0] = p[3];
    p[3] = t;
    t = p[1];
    p[1] = p[2];
    p[2] = p[1];
#endif
}

int8_t copybytes(FILE * fp, FILE * fp2, int n)
{
    int i, t = 0;

    for (i = 0; i < n; i++) {
        t = fgetc(fp);
        if (t == EOF) {
            fprintf(stderr, "rdflib: premature end of file in '%s'\n",
                    _argv[2]);
            exit(1);
        }
        if (fp2)
            if (fputc(t, fp2) == EOF) {
                fprintf(stderr, "rdflib: write error\n");
                exit(1);
            }
    }
    return (int8_t)t;             /* return last int8_t read */
}

int32_t copyint32_t(FILE * fp, FILE * fp2)
{
    int32_t l;
    int i, t;
    uint8_t *p = (uint8_t *)&l;

    for (i = 0; i < 4; i++) {   /* skip magic no */
        t = fgetc(fp);
        if (t == EOF) {
            fprintf(stderr, "rdflib: premature end of file in '%s'\n",
                    _argv[2]);
            exit(1);
        }
        if (fp2)
            if (fputc(t, fp2) == EOF) {
                fprintf(stderr, "rdflib: write error\n");
                exit(1);
            }
        *p++ = t;
    }
    int32_ttolocal(&l);
    return l;
}

int main(int argc, char **argv)
{
    FILE *fp, *fp2 = NULL, *fptmp;
    int8_t *p, buf[256], c;
    int i;
    int32_t l;
    time_t t;
    int8_t rdbuf[10];

    _argv = argv;

    if (argc < 3 || !strncmp(argv[1], "-h", 2)
        || !strncmp(argv[1], "--h", 3)) {
        printf(usage);
        exit(1);
    }

    switch (argv[1][0]) {
    case 'c':                  /* create library */
        fp = fopen(argv[2], "wb");
        if (!fp) {
            fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
            perror("rdflib");
            exit(1);
        }
        fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp);
        fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
        l = sizeof(t = time(NULL));
        fwrite(&l, sizeof(l), 1, fp);
        fwrite(&t, 1, l, fp);
        fclose(fp);
        break;

    case 'a':                  /* add module */
        if (argc < 5) {
            fprintf(stderr, "rdflib: required parameter missing\n");
            exit(1);
        }
        fp = fopen(argv[2], "ab");
        if (!fp) {
            fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
            perror("rdflib");
            exit(1);
        }

        fp2 = fopen(argv[3], "rb");
        if (!fp2) {
            fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]);
            perror("rdflib");
            exit(1);
        }

        p = argv[4];
        do {
            if (fputc(*p, fp) == EOF) {
                fprintf(stderr, "rdflib: write error\n");
                exit(1);
            }
        } while (*p++);

        while (!feof(fp2)) {
            i = fgetc(fp2);
            if (i == EOF) {
                break;
            }

            if (fputc(i, fp) == EOF) {
                fprintf(stderr, "rdflib: write error\n");
                exit(1);
            }
        }
        fclose(fp2);
        fclose(fp);
        break;

    case 'x':
        if (argc < 5) {
            fprintf(stderr, "rdflib: required parameter missing\n");
            exit(1);
        }
    case 't':
        fp = fopen(argv[2], "rb");
        if (!fp) {
            fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
            perror("rdflib");
            exit(1);
        }

        fp2 = NULL;
        while (!feof(fp)) {
            /* read name */
            p = buf;
            while ((*(p++) = (int8_t)fgetc(fp)))
                if (feof(fp))
                    break;

            if (feof(fp))
                break;

            fp2 = NULL;
            if (argv[1][0] == 'x') {
                /* check against desired name */
                if (!strcmp(buf, argv[3])) {
                    fp2 = fopen(argv[4], "wb");
                    if (!fp2) {
                        fprintf(stderr, "rdflib: could not open '%s'\n",
                                argv[4]);
                        perror("rdflib");
                        exit(1);
                    }
                }
            } else
                printf("%-40s ", buf);

            /* step over the RDOFF file, extracting type information for
             * the listing, and copying it if fp2 != NULL */

            if (buf[0] == '.') {

                if (argv[1][0] == 't')
                    for (i = 0; i < 6; i++)
                        printf("%c", copybytes(fp, fp2, 1));
                else
                    copybytes(fp, fp2, 6);

                l = copyint32_t(fp, fp2);

                if (argv[1][0] == 't')
                    printf("   %ld bytes content\n", l);

                copybytes(fp, fp2, l);
            } else if ((c = copybytes(fp, fp2, 6)) >= '2') {    /* version 2 or above */
                l = copyint32_t(fp, fp2);

                if (argv[1][0] == 't')
                    printf("RDOFF%c   %ld bytes content\n", c, l);
                copybytes(fp, fp2, l);  /* entire object */
            } else {
                if (argv[1][0] == 't')
                    printf("RDOFF1\n");
                /*
                 * version 1 object, so we don't have an object content
                 * length field.
                 */
                copybytes(fp, fp2, copyint32_t(fp, fp2));  /* header */
                copybytes(fp, fp2, copyint32_t(fp, fp2));  /* text */
                copybytes(fp, fp2, copyint32_t(fp, fp2));  /* data */
            }

            if (fp2)
                break;
        }
        fclose(fp);
        if (fp2)
            fclose(fp2);
        else if (argv[1][0] == 'x') {
            fprintf(stderr, "rdflib: module '%s' not found in '%s'\n",
                    argv[3], argv[2]);
            exit(1);
        }
        break;

    case 'r':                  /* replace module */
        argc--;
    case 'd':                  /* delete module */
        if (argc < 4) {
            fprintf(stderr, "rdflib: required parameter missing\n");
            exit(1);
        }

        fp = fopen(argv[2], "rb");
        if (!fp) {
            fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
            perror("rdflib");
            exit(1);
        }

        if (argv[1][0] == 'r') {
            fp2 = fopen(argv[4], "rb");
            if (!fp2) {
                fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
                perror("rdflib");
                exit(1);
            }
        }

        fptmp = tmpfile();
        if (!fptmp) {
            fprintf(stderr, "rdflib: could not open temporary file\n");
            perror("rdflib");
            exit(1);
        }

        /* copy library into temporary file */
        fseek(fp, 0, SEEK_END); /* get file length */
        l = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        copybytes(fp, fptmp, l);
        rewind(fptmp);
        freopen(argv[2], "wb", fp);

        while (!feof(fptmp)) {
            /* read name */
            p = buf;
            while ((*(p++) = (int8_t)fgetc(fptmp)))
                if (feof(fptmp))
                    break;

            if (feof(fptmp))
                break;

            /* check against desired name */
            if (!strcmp(buf, argv[3])) {
                fread(p = rdbuf, 1, sizeof(rdbuf), fptmp);
                l = *(int32_t *)(p + 6);
                fseek(fptmp, l, SEEK_CUR);
                break;
            } else {
                fwrite(buf, 1, strlen(buf) + 1, fp);    /* module name */
                if ((c = copybytes(fptmp, fp, 6)) >= '2') {
                    l = copyint32_t(fptmp, fp);    /* version 2 or above */
                    copybytes(fptmp, fp, l);    /* entire object */
                }
            }
        }

        if (argv[1][0] == 'r') {
            /* copy new module into library */
            p = argv[3];
            do {
                if (fputc(*p, fp) == EOF) {
                    fprintf(stderr, "rdflib: write error\n");
                    exit(1);
                }
            } while (*p++);

            while (!feof(fp2)) {
                i = fgetc(fp2);
                if (i == EOF) {
                    break;
                }
                if (fputc(i, fp) == EOF) {
                    fprintf(stderr, "rdflib: write error\n");
                    exit(1);
                }
            }
            fclose(fp2);
        }

        /* copy rest of library if any */
        while (!feof(fptmp)) {
            i = fgetc(fptmp);
            if (i == EOF) {
                break;
            }

            if (fputc(i, fp) == EOF) {
                fprintf(stderr, "rdflib: write error\n");
                exit(1);
            }
        }

        fclose(fp);
        fclose(fptmp);
        break;

    default:
        fprintf(stderr, "rdflib: command '%c' not recognized\n",
                argv[1][0]);
        exit(1);
    }
    return 0;
}