annotate mpffs/cat.c @ 35:ee4c761187cf

mpffs-cat implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 16:55:19 +0000
parents
children 8256eec598dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module contains the main function and other code specific to mpffs-cat
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <ctype.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <string.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <strings.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <unistd.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "types.h"
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include "struct.h"
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 extern char *imgfile;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 extern int verbose;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 cat_chunk(chi)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 struct chunkinfo *chi;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 u8 *dp;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 size_t len;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 int c;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 dp = chi->start;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 len = chi->len;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 while (len) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 c = *dp++;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 if (!verbose || c >= ' ' && c <= '~' || c == '\n')
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 putchar(c);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 else {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 if (c & 0x80) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 putchar('M');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 putchar('-');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 c &= 0x7F;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 putchar('^');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 if (c == 0x7F)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 putchar('?');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 else
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 putchar(c + '@');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 len--;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 cat_file(headidx)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 int ent;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 struct objinfo obj;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 struct chunkinfo chi;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 obj.entryno = headidx;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 get_index_entry(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 if (obj.type != 0xF1) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 fprintf(stderr,
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 "mpffs-cat: the requested FFS object is not a regular file\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 validate_chunk(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 size_head_chunk(&obj, &chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 if (verbose)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 printf("\n--- file content:\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 cat_chunk(&chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 for (ent = obj.descend; ent != 0xFFFF; ent = obj.descend) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 obj.entryno = ent;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 get_index_entry(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 if (obj.type != 0xF4) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 fprintf(stderr,
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 "file continuation object at index %x: type %02X != expected F4\n",
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 ent, obj.type);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 validate_chunk(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 size_extra_chunk(&obj, &chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 cat_chunk(&chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 if (verbose)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 printf("\n-- end quote --\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 usage()
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 fprintf(stderr, "usage: mpffs-cat [options] ffs-image pathname\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 main(argc, argv)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 char **argv;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 extern int optind;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 int idx;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 parse_cmdline_options(argc, argv);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 if (argc - optind != 2)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 usage();
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 imgfile = argv[optind];
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 preliminaries();
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 idx = find_pathname(argv[optind+1]);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 cat_file(idx);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 exit(0);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 }