File: dumpclass.c

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (142 lines) | stat: -rw-r--r-- 4,184 bytes parent folder | download | duplicates (2)
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
/* dumpclass.c -- display contents of a class_file
 * Written by Charles Briscoe-Smith; refer to the file LEGAL for details.
 */

/* Interface definitions */

/* Dump the contents of class to stdout in human-readable form.  */
extern void dumpclass(const class_file class);


#ifndef SEEN_dumpclass_h

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "types.h"

static void
showaccessflags(u2 access, int in_method)
{
  printf("Access flags:%s%s%s%s%s\n",
         access & ACC_public ? " public" : "",
         access & ACC_private ? " private" : "",
         access & ACC_protected ? " protected" : "",
         access & ACC_final ? " final" : "",
	 access & ACC_volatile ? " volatile" : "",
	 access & ACC_transient ? " transient" : "",
	 access & ACC_native ? " native" : "",
	 access & ACC_abstract ? " abstract" : "",
	 access & ACC_synchronised & in_method ? " synchronised" : "",
	 access & ACC_interface ? " interface" : "",
	 access & ACC_super &!in_method ? "" : " (without super flag; generated by old Sun compiler)",
	 );
}

void
dumpclass(const class_file *class)
{
  u4 count;

  printf("Version: major %d, minor %d\n", class->major_version,
					  class->minor_version);
  printf("Number of constants: %d\n", class->const_count);
  cp=(cp_info*) calloc(const_count, sizeof(cp_info));
  tag=(u2*) calloc(const_count, sizeof(u2));
  for (i1=1; i1<const_count; i1++) {
    tag[i1]=readconst(fp, &cp[i1]);
    if (tag[i1]==CONSTANT_Long
       || tag[i1]==CONSTANT_Double) {
      i1++;
    }
  }
  for (i1=0; i1<const_count; i1++) {
    u4f u4f;
    u8d u8d;
    printf("Constant %d: ", i1);
    if (i1==0) printf("reserved\n");
    else if (tag[i1-1]==CONSTANT_Long
	     || tag[i1-1]==CONSTANT_Double) printf("continuation word\n");
    else switch (tag[i1]) {
    case CONSTANT_Class:
      printf("Class: %d (%s)\n", cp[i1].u2,
	     cp[cp[i1].u2].utf8);
      break;
    case CONSTANT_Fieldref:
      printf("Fieldref: %d, %d (%s, %s, %s)\n",
             cp[i1].u2x2.i1,
             cp[i1].u2x2.i2,
	     cp[cp[cp[i1].u2x2.i1].u2].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i1].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i2].utf8);
      break;
    case CONSTANT_Methodref:
      printf("Methodref: %d, %d (%s, %s, %s)\n",
             cp[i1].u2x2.i1,
	     cp[i1].u2x2.i2,
	     cp[cp[cp[i1].u2x2.i1].u2].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i1].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i2].utf8);
      break;
    case CONSTANT_InterfaceMethodref:
      printf("InterfaceMethodref: %d, %d (%s, %s, %s)\n",
             cp[i1].u2x2.i1,
             cp[i1].u2x2.i2,
	     cp[cp[cp[i1].u2x2.i1].u2].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i1].utf8,
	     cp[cp[cp[i1].u2x2.i2].u2x2.i2].utf8);
      break;
    case CONSTANT_String:
      printf("String: %d (\"%s\")\n", cp[i1].u2,
	     cp[cp[i1].u2].utf8);
      break;
    case CONSTANT_Integer:
      printf("Integer: %ld\n", cp[i1].u4);
      break;
    case CONSTANT_Float:
      u4f.u=cp[i1].u4;
      printf("Float: %g\n", u4f.f);
      break;
    case CONSTANT_Long:
      printf("Long: %qd\n", ((u8) cp[i1].u4 << 32) + cp[i1+1].u4);
      break;
    case CONSTANT_Double:
      u8d.u=((u8) cp[i1].u4 << 32) + cp[i1+1].u4;
      printf("Double: %g\n", u8d.d);
      break;
    case CONSTANT_NameAndType:
      printf("NameAndType: %d, %d\n", cp[i1].u2x2.i1, cp[i1].u2x2.i2);
      break;
    case CONSTANT_Utf8:
      printf("Utf8: \"%s\"\n", cp[i1].utf8);
      break;
    }
  }

  access=readu2(fp);
  this=readu2(fp);
  printf("This class: %d\n", this);
  super=readu2(fp);
  printf("Superclass: %d\n", super);
  iface_count=readu2(fp);
  printf("Number of interfaces: %d:", iface_count);
  ifaces=(u2*) calloc(iface_count, sizeof(u2));
  for (i1=0; i1<iface_count; i1++) {
    ifaces[i1]=readu2(fp);
    printf(" %d", ifaces[i1]);
  }
  field_count=readu2(fp);
  printf("\nNumber of fields: %d\n", field_count);
  meth_count=readu2(fp);
  printf("Number of methods: %d\n", meth_count);
  attr_count=readu2(fp);
  printf("Number of attributes: %d\n", attr_count);
  if (feof(fp)) {
    error("%s", "premature end of file");
  }
  return 0;
}

#endif /* SEEN_readclass_h */