-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjavaClass_serialisation.h
335 lines (302 loc) · 8.46 KB
/
javaClass_serialisation.h
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
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/**\file
\brief serialisation for javaClass
*/
#ifndef CLASSDESC_JAVACLASS_SERIALISATION_H
#define CLASSDESC_JAVACLASS_SERIALISATION_H
#include <vector>
#include <string>
#include <pack_base.h>
#include <dump_base.h>
#include "javaClass.h"
namespace classdesc
{
template <> void dump(dump_t& buf, const string& d, const u8& a)
{dump(buf,d,a.v);}
template <> void dump(dump_t& buf, const string& d, const u4& a)
{dump(buf,d,a.v);}
template <> void dump(dump_t& buf, const string& d, const u2& a)
{dump(buf,d,a.v);}
template <> void dump(dump_t& buf, const string& d, const u1& a)
{buf << std::hex << int(a);}
}
namespace classdesc_access
{
/*
Strings and vectors are have specialised serialisation for javaClass
*/
template <>
struct access_pack<std::string>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, const std::string& a)
{
classdesc::u2 length=a.length();
pack(buf,d,length);
buf.packraw(a.c_str(),length);
}
};
template <>
struct access_unpack<std::string>
{
template <class U>
void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
{
classdesc::u2 length;
unpack(buf,d,length);
char *s=new char[length];
buf.unpackraw(s,length);
a=std::string(s,length);
delete [] s;
}
};
template <>
struct access_unpack<classdesc::u8>
{
template <class U>
void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
{
a.v=0;
for (int i=0; i<8; i++)
{
classdesc::u1 b;
unpack(buf,d,b);
a.v=(a.v<<8) | (0xFF&b);
}
}
};
template <>
struct access_pack<classdesc::u8>
{
template <class U>
void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
{
for (int i=7; i>=0; i--)
{
classdesc::u1 b = a.v >> 8*i;
pack(buf,d,b);
}
}
};
template <>
struct access_unpack<classdesc::u4>
{
template <class U>
void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
{
a.v=0;
for (int i=0; i<4; i++)
{
classdesc::u1 b;
unpack(buf,d,b);
a.v=(a.v<<8) | (0xFF&b);
}
}
};
template <>
struct access_pack<classdesc::u4>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::u4& a)
{
for (int i=3; i>=0; i--)
{
classdesc::u1 b = a.v >> 8*i;
pack(buf,d,b);
}
}
};
template <>
struct access_unpack<classdesc::u2>
{
void operator()(classdesc::unpack_t& buf, const classdesc::string& d, classdesc::u2& a)
{
classdesc::u1 b1, b2;
unpack(buf,d,b1);
unpack(buf,d,b2);
a=(b1<<8)| (0xFF & b2);
}
};
template <>
struct access_pack<classdesc::u2>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::u2& a)
{
classdesc::u1 b1=a.v>>8, b2=a.v;
pack(buf,d,b1);
pack(buf,d,b2);
}
};
template <>
struct access_pack<classdesc::cp_info>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::cp_info& a)
{
using namespace classdesc;
pack(buf,d,a.tag());
switch (a.tag())
{
case JVM_CONSTANT_Class:
case JVM_CONSTANT_String:
pack(buf,d,a.get<u2>()); break;
case JVM_CONSTANT_Fieldref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_InterfaceMethodref:
pack(buf,d,a.get<Ref>()); break;
case JVM_CONSTANT_Integer:
case JVM_CONSTANT_Float:
pack(buf,d,a.get<u4>()); break;
case JVM_CONSTANT_Long:
case JVM_CONSTANT_Double:
pack(buf,d,a.get<u8>()); break;
case JVM_CONSTANT_NameAndType:
pack(buf,d,a.get<NameAndTypeInfo>());
break;
case JVM_CONSTANT_Utf8:
pack(buf,d,a.get<std::string>()); break;
}
}
};
template <>
struct access_unpack<classdesc::cp_info>
{
void operator()(classdesc::unpack_t& buf, const classdesc::string& d, classdesc::cp_info& a)
{
using namespace classdesc;
u1 tag;
unpack(buf,d,tag);
switch (tag)
{
case JVM_CONSTANT_Class:
case JVM_CONSTANT_String:
a.unpack<u2>(buf,tag); break;
case JVM_CONSTANT_Fieldref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_InterfaceMethodref:
a.unpack<Ref>(buf,tag); break;
case JVM_CONSTANT_Integer:
case JVM_CONSTANT_Float:
a.unpack<u4>(buf,tag); break;
case JVM_CONSTANT_Long:
case JVM_CONSTANT_Double:
a.unpack<u8>(buf,tag); break;
case JVM_CONSTANT_NameAndType:
a.unpack<NameAndTypeInfo>(buf,tag);
break;
case JVM_CONSTANT_Utf8:
a.unpack<std::string>(buf,tag); break;
}
}
};
// The zeroth element is not serialised in the constant_pool
template <>
struct access_pack<classdesc::ConstantPoolVector>
{
template <class U>
void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
{
classdesc::u2 size=a.size();
pack(buf,d,size);
for (int i=1; i<size; i++)
pack(buf,d,a[i]);
}
};
// The zeroth element is not serialised in the constant_pool
template <>
struct access_unpack<classdesc::ConstantPoolVector>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, std::vector<classdesc::cp_info>& a)
{
classdesc::u2 size; unpack(buf,d,size);
a.resize(size);
for (int i=1; i<size; i++)
unpack(buf,d,a[i]);
}
};
template <>
struct access_pack<classdesc::attribute_info>
{
template<class U>
void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
{
pack(buf,d,a.attribute_name_index);
classdesc::u4 length=a.info.size();
pack(buf,d,length);
// need to code this explicitly as attribute length is u4 not u2
for (size_t i=0; i<a.info.size(); i++)
pack(buf,d,a.info[i]);
}
};
template <>
struct access_unpack<classdesc::attribute_info>
{
void operator()(classdesc::pack_t& buf, const classdesc::string& d, classdesc::attribute_info& a)
{
unpack(buf,d,a.attribute_name_index);
classdesc::u4 length;
unpack(buf,d,length);
// need to code this explicitly as attribute length is u4 not u2
a.info.resize(length);
for (size_t i=0; i<a.info.size(); i++)
unpack(buf,d,a.info[i]);
}
};
}
/* define this here to take advantage of cp_info's serialisation operators */
inline bool classdesc::cp_info::operator==(const classdesc::cp_info& x) const
{
pack_t b1, b2;
pack(b1,string(),*this);
pack(b2,string(),x);
return b1.size()==b2.size() && memcmp(b1.data(),b2.data(),b1.size())==0;
}
namespace classdesc
{
void dumpp(dump_t& buf, const string& d, cp_info& a)
{
switch (a.tag())
{
case JVM_CONSTANT_Class:
case JVM_CONSTANT_String:
dump(buf,d,a.get<u2>()); break;
case JVM_CONSTANT_Fieldref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_InterfaceMethodref:
dump(buf,d,a.get<Ref>()); break;
case JVM_CONSTANT_Integer:
dump(buf,d,a.get<int>()); break;
case JVM_CONSTANT_Float:
dump(buf,d,a.get<float>()); break;
case JVM_CONSTANT_Long:
dump(buf,d,a.get<long long>()); break;
case JVM_CONSTANT_Double:
dump(buf,d,a.get<double>()); break;
case JVM_CONSTANT_NameAndType:
dump(buf,d,a.get<NameAndTypeInfo>());
break;
case JVM_CONSTANT_Utf8:
dump(buf,d,a.get<std::string>()); break;
}
}
}
namespace classdesc
{
inline void dumpp(dump_t& targ, const string& desc,struct attribute_info& arg)
{
dump(targ,desc+".attribute_name_index",arg.attribute_name_index);
int tab=format(targ, desc+".info");
targ << std::setw(tab) << "";
for (u1 *c=&arg.info[0]; c!=&arg.info[0]+arg.info.size(); c++)
targ << " "<<std::setw(2)<<std::setfill('0')<<std::hex << int(*c);
targ<<std::setfill(' ')<<std::endl;
}
}
template <class T> void classdesc::cp_info::unpack(pack_t& t, u1 tag) {
T tmp;
::unpack(t,"",tmp);
set(tag,tmp);
}
#endif