-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpinfo.cpp
100 lines (87 loc) · 3.08 KB
/
cpinfo.cpp
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
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of SJVM the Simple Java Virtual Machine.
*
* SJVM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SJVM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SJVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CPInfo.h"
#include "FieldInfo.h"
#include "MethodInfo.h"
#include <iostream>
#define U2(var,data,index) var = data[index++] << 8; var |= data[index++];
CPInfo::CPInfo(char type) {
this->type = type;
}
CPInfo* CPInfo::read(unsigned char *data, int &index) {
switch (data[index]) {
case CONSTANT_Class:
return new ClassInfo(data,index);
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
return new FieldMethodInfo(data,index);
case CONSTANT_String:
return new StringInfo(data,index);
case CONSTANT_Integer:
case CONSTANT_Float:
return new IntegerFloatInfo(data,index);
case CONSTANT_Long:
case CONSTANT_Double:
return new LongDoubleInfo(data,index);
case CONSTANT_NameAndType:
return new NameAndTypeInfo(data,index);
case CONSTANT_Utf8:
return new UTF8Info(data,index);
}
return 0;
}
UTF8Info::UTF8Info(unsigned char *data, int &index) : CPInfo(data[index++]) {
U2(size,data,index);
bytes = new char[size+1];
for (int i = 0; i < size; i++)
bytes[i] = data[i+index];
bytes[size] = 0;
index += size;
}
UTF8Info::~UTF8Info() {
delete [] bytes;
}
StringInfo::StringInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
U2(stringIndex,data,index);
}
NameAndTypeInfo::NameAndTypeInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
U2(nameIndex,data,index);
U2(descriptorIndex,data,index);
}
LongDoubleInfo::LongDoubleInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
for (int i = 0; i < 8; i++)
this->data[i] = data[7-i+index];
index += 8;
}
IntegerFloatInfo::IntegerFloatInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
for (int i = 0; i < 4; i++)
this->data[i] = data[3-i+index];
index += 4;
}
FieldMethodInfo::FieldMethodInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
U2(classIndex,data,index);
U2(nameAndTypeIndex,data,index);
fieldRef = 0;
methodRef = 0;
}
ClassInfo::ClassInfo(unsigned char *data, int &index) : CPInfo(data[index++]) {
U2(nameIndex,data,index);
classRef = 0;
}