forked from lixiuzhi/sproto_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprotoTypeDeserialize.java
executable file
·421 lines (326 loc) · 10.9 KB
/
SprotoTypeDeserialize.java
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.lxz.sproto;
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class SprotoTypeDeserialize {
private SprotoTypeReader reader;
private int begin_data_pos;
private int cur_field_pos;
private int fn;
private int tag = -1;
private int value;
public SprotoTypeDeserialize() {
}
public SprotoTypeDeserialize (byte[] data) {
this.init (data,0);
}
public SprotoTypeDeserialize(SprotoTypeReader reader) {
this.init (reader);
}
public void init(byte[] data, int offset) {
this.clear ();
this.reader = new SprotoTypeReader (data, offset, data.length);
this.init ();
}
public void init(SprotoTypeReader reader) {
this.clear ();
this.reader = reader;
this.init ();
}
private void init() {
this.fn = this.read_word ();
int header_length = SprotoTypeSize.sizeof_header + this.fn * SprotoTypeSize.sizeof_field;
this.begin_data_pos = header_length;
this.cur_field_pos = this.reader.getPosition();
if (this.reader.getLength() < header_length) {
SprotoTypeSize.error ("invalid decode header.");
}
this.reader.Seek (this.begin_data_pos);
}
private long expand64(int value) {
return value;
}
private int read_word() {
return (int)this.reader.ReadByte ()& 0xFF |
((int)this.reader.ReadByte ()& 0xFF) << 8;
}
private int read_dword() {
return (int)this.reader.ReadByte ()& 0xFF |
((int)this.reader.ReadByte ()& 0xFF) << 8 |
((int)this.reader.ReadByte ()& 0xFF) << 16|
((int)this.reader.ReadByte ()& 0xFF) << 24;
}
private int read_array_size() {
if (this.value >= 0)
SprotoTypeSize.error ("invalid array value.");
int sz = this.read_dword ();
if (sz < 0)
SprotoTypeSize.error ("error array size("+sz+")");
return sz;
}
public int read_tag() {
int pos = this.reader.getPosition();
this.reader.Seek (this.cur_field_pos);
while(this.reader.getPosition() < this.begin_data_pos){
this.tag++;
int value = this.read_word ();
if( (value & 1) == 0) {
this.cur_field_pos = this.reader.getPosition();
this.reader.Seek(pos);
this.value = value/2 - 1;
return this.tag;
}
this.tag += value/2;
}
this.reader.Seek(pos);
return -1;
}
public int read_int32()
{
return (int)read_integer();
}
public float read_float32( )
{
return (float)read_int32() * 0.001f;
}
public double read_double()
{
return (double)read_int64() * 0.001;
}
public long read_int64()
{
return read_integer();
}
public long read_integer() {
if (this.value >= 0) {
return (long)(this.value);
} else {
int sz = this.read_dword ();
if (sz == 4) {
long v = this.expand64 (this.read_dword ());
return (long)v;
} else if (sz == 8) {
int low = this.read_dword ();
int hi = this.read_dword ();
long v = (long)low | (long)hi << 32;
return (long)v;
} else {
SprotoTypeSize.error ("read invalid integer size (" + sz + ")");
}
}
return 0;
}
public List<Long> read_int64_list()
{
return read_integer_list();
}
public List<Long> read_integer_list() {
List<Long> integer_list = new ArrayList<>();
int sz = this.read_array_size ();
if (sz == 0) {
return integer_list;
}
int len = this.reader.ReadByte () & 0xFF;
sz--;
if (len == 4) {
if (sz % 4 != 0) {
SprotoTypeSize.error ("error array size("+sz+")@sizeof(Uint32)");
}
for (int i = 0; i < sz / 4; i++) {
long v = this.expand64 (this.read_dword ());
integer_list.add (v);
}
} else if (len == 8) {
if (sz % 8 != 0) {
SprotoTypeSize.error ("error array size("+sz+")@sizeof(Uint64)");
}
for (int i = 0; i < sz / 8; i++) {
int low = this.read_dword ();
int hi = this.read_dword ();
long v = (long)low | (long)hi << 32;
integer_list.add (v);
}
} else {
SprotoTypeSize.error ("error intlen("+len+")");
}
return integer_list;
}
public List<Integer> read_int32_list()
{
List<Integer> integer_list =new ArrayList<>();
int sz = this.read_array_size();
if (sz == 0)
{
return integer_list;
}
int len = this.reader.ReadByte()& 0xFF;
sz--;
if (len ==4)
{
if (sz %4 != 0)
{
SprotoTypeSize.error("error array size(" + sz + ")@sizeof(Uint32)");
}
for (int i = 0; i < sz / 4; i++)
{
long v = this.expand64(this.read_dword());
integer_list.add((int)v);
}
}
else if (len == 8)
{
if (sz %8 != 0)
{
SprotoTypeSize.error("error array size(" + sz + ")@sizeof(Uint64)");
}
for (int i = 0; i < sz /8; i++)
{
int low = this.read_dword();
int hi = this.read_dword();
long v = (long)low | (long)hi << 32;
integer_list.add((int)v);
}
}
else
{
SprotoTypeSize.error("error intlen(" + len + ")");
}
return integer_list;
}
public boolean read_boolean() {
if (this.value < 0) {
SprotoTypeSize.error ("read invalid boolean.");
return false;
} else {
return (this.value ==0)?(false):(true);
}
}
public List<Boolean> read_boolean_list() {
int sz = this.read_array_size ();
List<Boolean> boolean_list = new ArrayList<>();
for (int i = 0; i < sz; i++) {
boolean v = (this.reader.ReadByte() == (byte)0)?(false):(true);
boolean_list.add (v);
}
return boolean_list;
}
public String read_string() {
int sz = this.read_dword ();
byte[] buffer = new byte[sz];
this.reader.Read (buffer, 0, buffer.length);
try{
return new String(buffer,"utf-8");
}catch (Exception e){
}
return "";
}
public List<String> read_string_list() {
int sz = this.read_array_size();
List<String> string_list = new ArrayList<>();
for (int i = 0; sz > 0; i++) {
if (sz < SprotoTypeSize.sizeof_length) {
SprotoTypeSize.error("error array size.");
}
int hsz = this.read_dword();
sz -= (int) SprotoTypeSize.sizeof_length;
if (hsz > sz) {
SprotoTypeSize.error("error array object.");
}
byte[] buffer = new byte[hsz];
this.reader.Read(buffer, 0, buffer.length);
String v = "";
try {
v = new String(buffer, "utf-8");
} catch (Exception e) {
}
string_list.add(v);
sz -= hsz;
}
return string_list;
}
public byte[] read_bytes()
{
int sz = this.read_dword();
byte[] buffer = new byte[sz];
this.reader.Read(buffer, 0, buffer.length);
return buffer;
}
public List<byte[]> read_bytes_list()
{
int sz = this.read_array_size();
List<byte[]> data_list = new ArrayList<>();
for (int i = 0; sz > 0; i++)
{
if (sz < SprotoTypeSize.sizeof_length)
{
SprotoTypeSize.error("error array size.");
}
int hsz = this.read_dword();
sz -= SprotoTypeSize.sizeof_length;
if (hsz > sz)
{
SprotoTypeSize.error("error array object.");
}
byte[] buffer = new byte[hsz];
this.reader.Read(buffer, 0, buffer.length);
data_list.add(buffer);
sz -= hsz;
}
return data_list;
}
public <T extends SprotoTypeBase> T read_obj (Supplier<T> factory) {
int sz = (int)this.read_dword ();
SprotoTypeReader reader = new SprotoTypeReader (this.reader.buffer, this.reader.getOffset(), sz);
this.reader.Seek (this.reader.getPosition() + sz);
T obj = factory.get();
obj.init (reader);
return obj;
}
private <T extends SprotoTypeBase> int read_element(T obj, SprotoTypeReader reader, int sz) {
int read_size = 0;
if (sz < SprotoTypeSize.sizeof_length) {
SprotoTypeSize.error ("error array size.");
}
int hsz = this.read_dword ();
sz -= (int)SprotoTypeSize.sizeof_length;
read_size += (int)SprotoTypeSize.sizeof_length;
if (hsz > sz) {
SprotoTypeSize.error ("error array object.");
}
reader.Init(this.reader.buffer, this.reader.getOffset(), (int)hsz);
this.reader.Seek (this.reader.getPosition() + (int)hsz);
obj.init (reader);
read_size += hsz;
return read_size;
}
public <T extends SprotoTypeBase> List<T> read_obj_list(Supplier<T> factory){
int sz = this.read_array_size ();
List<T> obj_list = new ArrayList<>();
SprotoTypeReader reader = new SprotoTypeReader ();
for (int i = 0; sz > 0; i++) {
int read_size;
T obj = factory.get();
sz-=read_element(obj,reader, sz);
obj_list.add (obj);
}
return obj_list;
}
public void read_unknow_data() {
if (this.value < 0) {
int sz = (int)this.read_dword ();
this.reader.Seek (sz + this.reader.getPosition());
}
}
public int size() {
return this.reader.getPosition();
}
public void clear() {
this.fn = 0;
this.tag = -1;
this.value = 0;
if (this.reader != null) {
this.reader.Seek (0);
}
}
}