summaryrefslogtreecommitdiff
path: root/rdoff/rdfdump.c
blob: 4d4f4df5a6fd63a4839511c064a0c9c4b54f9b7c (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *infile;

long translatelong(long in) {		/* translate from little endian to
					   local representation */
  long r;
  unsigned char *i;

  i = (unsigned char *)&in;
  r = i[3];
  r = (r << 8) + i[2];
  r = (r << 8) + i[1];
  r = (r << 8) + *i;

  return r;
}

int translateshort(short in) {
  int r;
  unsigned char *i;

  i = (unsigned char *)&in;
  r = (i[1] << 8) + *i;

  return r;
}
void print_header(long length) {
  unsigned char buf[129],t,s,l;
  long o;
  short rs;

  while (length > 0) {
    fread(&t,1,1,infile);
    switch(t) {
    case 1:		/* relocation record */
      fread(&s,1,1,infile);
      fread(&o,4,1,infile);
      fread(&l,1,1,infile);
      fread(&rs,2,1,infile);
      printf("  relocation: location (%04x:%08lx), length %d, "
	     "referred seg %04x\n",(int)s,translatelong(o),(int)l,
	     translateshort(rs));
      length -= 9;
      break;
    case 2:             /* import record */
      fread(&rs,2,1,infile);
      l = 0;
      do {
	fread(&buf[l],1,1,infile);
      } while (buf[l++]);
      printf("  import: segment %04x = %s\n",translateshort(rs),buf);
      length -= l + 3;
      break;
    case 3:             /* export record */
      fread(&s,1,1,infile);
      fread(&o,4,1,infile);
      l = 0;
      do {
	fread(&buf[l],1,1,infile);
      } while (buf[l++]);
      printf("  export: (%04x:%08lx) = %s\n",(int)s,translatelong(o),buf);
      length -= l + 6;
      break;
    case 4:		/* DLL record */
      l = 0;
      do {
	fread(&buf[l],1,1,infile);
      } while (buf[l++]);
      printf("  dll: %s\n",buf);
      length -= l + 1;
      break;
    case 5:		/* BSS reservation */
      fread(&l,4,1,infile);
      printf("  bss reservation: %08lx bytes\n",translatelong(l));
      length -= 5;
      break;
    default:
      printf("  unrecognised record (type %d)\n",(int)t);
      length --;
    }
  }
}

int main(int argc,char **argv) {
  char id[7];
  long l;
  int verbose = 0;

  puts("RDOFF Dump utility v1.1 (C) Copyright 1996 Julian R Hall");

  if (argc < 2) {
    fputs("Usage: rdfdump [-v] <filename>\n",stderr);
    exit(1);
  }

  if (! strcmp (argv[1], "-v") )
  {
    verbose = 1;
    if (argc < 3)
    {
      fputs("required parameter missing\n",stderr);
      exit(1);
    }
    argv++;
  }

  infile = fopen(argv[1],"rb");
  if (! infile) {
    fprintf(stderr,"rdfdump: Could not open %s",argv[1]);
    exit(1);
  }

  fread(id,6,1,infile);
  if (strncmp(id,"RDOFF",5)) {
    fputs("rdfdump: File does not contain valid RDOFF header\n",stderr);
    exit(1);
  }

  printf("File %s: RDOFF version %c\n\n",argv[1],id[5]);
  if (id[5] < '1' || id[5] > '1') {
    fprintf(stderr,"rdfdump: unknown RDOFF version '%c'\n",id[5]);
    exit(1);
  }

  fread(&l,4,1,infile);
  l = translatelong(l);
  printf("Header (%ld bytes):\n",l);
  print_header(l);

  fread(&l,4,1,infile);
  l = translatelong(l);
  printf("\nText segment length = %ld bytes\n",l);
  while(l--) {
    fread(id,1,1,infile);
    if (verbose) printf("  %02x",(int) (unsigned char)id[0]);
  }
  if (verbose) printf("\n\n");

  fread(&l,4,1,infile);
  l = translatelong(l);
  printf("Data segment length = %ld bytes\n",l);

  if (verbose)
  {
    while (l--) {
      fread(id,1,1,infile);
      printf("  %02x",(int) (unsigned char) id[0]);
    }
    printf("\n");
  }
  fclose(infile);
  return 0;
}