FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bytecode-viewer/plugins/java/ClassParser.java at master · GitJavaProgramming/bytecode-viewer · GitHub
GitJavaProgramming
bytecode-viewer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bytecode-viewer
/
plugins
/
java
/
ClassParser.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
347 lines (299 loc) · 11.7 KB
Breadcrumbs
bytecode-viewer
/
plugins
/
java
/
ClassParser.java
Copy path
File metadata and controls
347 lines (299 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
org
.
objectweb
.
asm
.
ClassWriter
;
import
org
.
objectweb
.
asm
.
tree
.
ClassNode
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
BytecodeViewer
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
api
.
Plugin
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
api
.
PluginConsole
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
translation
.
TranslatedStrings
;
import
java
.
io
.
ByteArrayInputStream
;
import
java
.
io
.
DataInputStream
;
import
java
.
io
.
IOException
;
import
java
.
nio
.
BufferUnderflowException
;
import
java
.
nio
.
ByteBuffer
;
import
java
.
security
.
MessageDigest
;
import
java
.
security
.
NoSuchAlgorithmException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Date
;
import
java
.
util
.
List
;
/**
* Class Parser
*
* @author Damir37
*/
public
class
ClassParser
extends
Plugin
{
private
PluginConsole
pluginConsole
;
@
Override
public
void
execute
(
List
<
ClassNode
>
list
)
{
if
(!
BytecodeViewer
.
isActiveClassActive
())
{
BytecodeViewer
.
showMessage
(
"Open A Classfile First"
);
return
;
}
ClassNode
c
=
BytecodeViewer
.
getCurrentlyOpenedClassNode
();
ClassFileParser
classFileParser
=
new
ClassFileParser
(
classNodeToByte
(
c
));
pluginConsole
=
new
PluginConsole
(
"ClassParser"
);
pluginConsole
.
setVisible
(
true
);
print
(
"Parsing class: "
+
c
.
name
+
".class"
);
print
(
"MAGIC VALUE: "
+
classFileParser
.
parseMagicValue
());
print
(
"Class version: "
+
classFileParser
.
parseVersionClass
());
print
(
"Constant pool count: "
+
classFileParser
.
parseConstantPoolCount
()
+
" If not all constants were parsed, most likely the constant is not used in the bytecode."
);
print
(
"Then use the javap utility to view the constant pool of a class file."
);
print
(
"Last modified class: "
+
classFileParser
.
parseClassModificationDate
());
print
(
"Hash sum class md5: "
+
classFileParser
.
getHash
(
"MD5"
));
print
(
"Hash sum class sha1: "
+
classFileParser
.
getHash
(
"SHA-1"
));
print
(
"Hash sum class sha256: "
+
classFileParser
.
getHash
(
"SHA-256"
));
print
(
"Hash sum class sha512: "
+
classFileParser
.
getHash
(
"SHA-512"
));
print
(
"Constant pool ->"
);
classFileParser
.
getConstantPool
().
parseConstantPool
();
if
(
classFileParser
.
getConstantPool
().
getCpList
() !=
null
&& !
classFileParser
.
getConstantPool
().
getCpList
().
isEmpty
())
{
for
(
String
s
:
classFileParser
.
getConstantPool
().
getCpList
())
{
print
(
s
);
}
}
}
private
byte
[]
classNodeToByte
(
ClassNode
classNode
)
{
ClassWriter
cw
=
new
ClassWriter
(
ClassWriter
.
COMPUTE_FRAMES
);
classNode
.
accept
(
cw
);
return
cw
.
toByteArray
();
}
public
void
print
(
String
text
)
{
pluginConsole
.
appendText
(
text
);
pluginConsole
.
repaint
();
}
private
static
class
ClassFileParser
{
private
final
ByteBuffer
buffer
;
private
final
ConstantParser
cpParser
;
public
ClassFileParser
(
byte
[]
classBytes
)
{
this
.
buffer
=
ByteBuffer
.
wrap
(
classBytes
);
cpParser
=
new
ConstantParser
(
buffer
,
parseConstantPoolCount
());
}
public
String
parseMagicValue
()
{
buffer
.
position
(
0
);
int
magicValue
=
buffer
.
getInt
();
return
"0x"
+
Integer
.
toHexString
(
magicValue
).
toUpperCase
();
}
public
ClassVersion
parseVersionClass
()
{
buffer
.
position
(
4
);
int
minor
=
buffer
.
getShort
() &
0xFFFF
;
int
major
=
buffer
.
getShort
() &
0xFFFF
;
return
ClassVersion
.
check
(
major
,
minor
);
}
public
Date
parseClassModificationDate
()
{
buffer
.
position
(
8
);
long
modificationTime
=
buffer
.
getInt
() &
0xFFFFFFFFL
;
return
new
Date
(
modificationTime
*
1000L
);
}
public
int
parseConstantPoolCount
()
{
buffer
.
position
(
8
);
return
buffer
.
getShort
() &
0xFFFF
;
}
public
String
getHash
(
String
algorithm
)
{
try
{
MessageDigest
md
=
MessageDigest
.
getInstance
(
algorithm
);
md
.
update
(
buffer
.
array
());
byte
[]
digest
=
md
.
digest
();
return
convertToHex
(
digest
);
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
return
"null"
;
}
private
String
convertToHex
(
byte
[]
bytes
)
{
StringBuilder
hexString
=
new
StringBuilder
();
for
(
byte
b
:
bytes
)
{
hexString
.
append
(
String
.
format
(
"%02X"
,
b
));
}
return
hexString
.
toString
();
}
public
ConstantParser
getConstantPool
()
{
return
cpParser
;
}
}
private
enum
ClassVersion
{
UNKNOWN
(
0
,
0
),
JAVA_1_1
(
45
,
3
),
JAVA_1_2
(
46
,
0
),
JAVA_1_3
(
47
,
0
),
JAVA_1_4
(
48
,
0
),
JAVA_5
(
49
,
0
),
JAVA_6
(
50
,
0
),
JAVA_7
(
51
,
0
),
JAVA_8
(
52
,
0
),
JAVA_9
(
53
,
0
),
JAVA_10
(
54
,
0
),
AVA_11
(
55
,
0
),
JAVA_12
(
56
,
0
),
JAVA_13
(
57
,
0
),
JAVA_14
(
58
,
0
),
JAVA_15
(
59
,
0
),
JAVA_16
(
60
,
0
),
JAVA_17
(
61
,
0
),
JAVA_18
(
62
,
0
),
JAVA_19
(
63
,
0
),
JAVA_20
(
64
,
0
),
JAVA_21
(
65
,
0
),
JAVA_22
(
66
,
0
),
JAVA_23
(
67
,
0
),
JAVA_24
(
68
,
0
),
JAVA_25
(
69
,
0
),
JAVA_26
(
70
,
0
),
JAVA_27
(
71
,
0
),
JAVA_28
(
72
,
0
),
JAVA_29
(
73
,
0
),
JAVA_30
(
74
,
0
);
public
final
int
major
;
public
final
int
minor
;
ClassVersion
(
int
major
,
int
minor
)
{
this
.
major
=
major
;
this
.
minor
=
minor
;
}
public
static
ClassVersion
check
(
int
major
,
int
minor
)
{
for
(
ClassVersion
v
:
ClassVersion
.
values
())
{
if
(
v
.
major
==
major
&&
v
.
minor
==
minor
)
return
v
;
}
return
UNKNOWN
;
}
}
private
static
class
ConstantParser
{
private
final
ByteBuffer
buffer
;
private
final
int
constantPoolCount
;
private
final
List
<
String
>
cpList
=
new
ArrayList
<
String
>();
public
ConstantParser
(
ByteBuffer
buffer
,
int
constantPoolCount
)
{
this
.
buffer
=
buffer
;
this
.
constantPoolCount
=
constantPoolCount
;
}
public
void
parseConstantPool
()
{
buffer
.
position
(
10
);
for
(
int
i
=
1
;
i
<
constantPoolCount
;
i
++)
{
int
tag
=
buffer
.
get
() &
0xFF
;
switch
(
tag
)
{
case
ConstantType
.
CONSTANT_Utf8
:
int
length
=
buffer
.
getShort
() &
0xFFFF
;
byte
[]
bytes
=
new
byte
[
length
];
buffer
.
get
(
bytes
);
String
string
=
new
String
(
bytes
);
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Utf8: "
+
string
);
break
;
case
ConstantType
.
CONSTANT_Integer
:
int
value
=
buffer
.
getInt
();
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Integer: "
+
value
);
break
;
case
ConstantType
.
CONSTANT_Float
:
float
floatValue
=
buffer
.
getFloat
();
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Float: "
+
floatValue
);
break
;
case
ConstantType
.
CONSTANT_Long
:
long
longValue
=
buffer
.
getLong
();
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Long: "
+
longValue
);
i
++;
break
;
case
ConstantType
.
CONSTANT_Double
:
double
doubleValue
=
buffer
.
getDouble
();
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Double: "
+
doubleValue
);
i
++;
break
;
case
ConstantType
.
CONSTANT_Class
:
int
nameIndex
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_Class: #"
+
nameIndex
);
break
;
case
ConstantType
.
CONSTANT_String
:
int
stringIndex
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_String: #"
+
stringIndex
);
break
;
case
ConstantType
.
CONSTANT_Fieldref
:
case
ConstantType
.
CONSTANT_Methodref
:
case
ConstantType
.
CONSTANT_InterfaceMethodref
:
int
classIndex
=
buffer
.
getShort
() &
0xFFFF
;
int
nameAndTypeIndex
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_"
+
getRefTypeName
(
tag
) +
": #"
+
classIndex
+
".#"
+
nameAndTypeIndex
);
break
;
case
ConstantType
.
CONSTANT_NameAndType
:
int
nameIndex1
=
buffer
.
getShort
() &
0xFFFF
;
int
descriptorIndex
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_NameAndType: #"
+
nameIndex1
+
":#"
+
descriptorIndex
);
break
;
case
ConstantType
.
CONSTANT_MethodHandle
:
int
referenceKind
=
buffer
.
get
() &
0xFF
;
int
referenceIndex
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_MethodHandle: "
+
referenceKind
+
":#"
+
referenceIndex
);
break
;
case
ConstantType
.
CONSTANT_MethodType
:
int
descriptorIndex1
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_MethodType: #"
+
descriptorIndex1
);
break
;
case
ConstantType
.
CONSTANT_InvokeDynamic
:
int
bootstrapMethodAttrIndex
=
buffer
.
getShort
() &
0xFFFF
;
int
nameAndTypeIndex3
=
buffer
.
getShort
() &
0xFFFF
;
cpList
.
add
(
"["
+
i
+
"] CONSTANT_InvokeDynamic: #"
+
bootstrapMethodAttrIndex
+
":#"
+
nameAndTypeIndex3
);
break
;
default
:
throw
new
IllegalArgumentException
(
"Unknown constant pool tag "
+
tag
);
}
}
}
private
String
getRefTypeName
(
int
tag
)
{
switch
(
tag
)
{
case
ConstantType
.
CONSTANT_Fieldref
:
return
"Fieldref"
;
case
ConstantType
.
CONSTANT_Methodref
:
return
"Methodref"
;
case
ConstantType
.
CONSTANT_InterfaceMethodref
:
return
"InterfaceMethodref"
;
default
:
return
"Unknown"
;
}
}
public
List
<
String
>
getCpList
()
{
return
cpList
;
}
}
private
interface
ConstantType
{
public
static
final
byte
CONSTANT_Utf8
=
1
;
public
static
final
byte
CONSTANT_Class
=
7
;
public
static
final
byte
CONSTANT_Fieldref
=
9
;
public
static
final
byte
CONSTANT_Methodref
=
10
;
public
static
final
byte
CONSTANT_InterfaceMethodref
=
11
;
public
static
final
byte
CONSTANT_String
=
8
;
public
static
final
byte
CONSTANT_Integer
=
3
;
public
static
final
byte
CONSTANT_Float
=
4
;
public
static
final
byte
CONSTANT_Long
=
5
;
public
static
final
byte
CONSTANT_Double
=
6
;
public
static
final
byte
CONSTANT_NameAndType
=
12
;
public
static
final
byte
CONSTANT_MethodHandle
=
15
;
public
static
final
byte
CONSTANT_MethodType
=
16
;
public
static
final
byte
CONSTANT_InvokeDynamic
=
18
;
}
}
Back
|
FazBrowse Home
|
New Git URL