Menu

[r399]: / phprpc_2.1 / dotNET / PHPSerializer.cs  Maximize  Restore  History

Download this file

976 lines (969 with data), 33.2 kB

  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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* PHPSerializer.cs
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 3.7
* LastModified: 2007-02-09
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=193
*/
using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Reflection;
public interface Serializable
{
byte[] Serialize();
void UnSerialize(byte[] ss);
}
public class UnSerializeException : Exception
{
public UnSerializeException() { }
public UnSerializeException(string message) : base(message) { }
public UnSerializeException(string message, Exception inner) : base(message, inner) { }
}
public class PHPSerializer
{
private static Hashtable __ns;
private const byte __Quote = 34;
private const byte __0 = 48;
private const byte __1 = 49;
private const byte __Colon = 58;
private const byte __Semicolon = 59;
private const byte __C = 67;
private const byte __N = 78;
private const byte __O = 79;
private const byte __R = 82;
private const byte __U = 85;
private const byte __Slash = 92;
private const byte __a = 97;
private const byte __b = 98;
private const byte __d = 100;
private const byte __i = 105;
private const byte __r = 114;
private const byte __s = 115;
private const byte __LeftB = 123;
private const byte __RightB = 125;
static PHPSerializer()
{
__ns = new Hashtable();
Assembly[] assem = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assem.Length; i++)
{
Module[] m = assem[i].GetModules();
for (int j = 0; j < m.Length; j++)
{
try
{
Type[] t = m[j].GetTypes();
for (int k = 0; k < t.Length; k++)
{
if (t[k].Namespace != null && !__ns.ContainsKey(t[k].Namespace))
{
__ns[t[k].Namespace] = assem[i].FullName;
}
}
}
catch
{
}
}
}
}
private PHPSerializer()
{
}
public static byte[] Serialize(object obj)
{
return Serialize(obj, Encoding.UTF8);
}
public static byte[] Serialize(object obj, Encoding encoding)
{
Hashtable ht = new Hashtable();
int hv = 1;
MemoryStream stream = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);
Serialize(stream, obj, ht, ref hv, encoding);
byte[] result = stream.ToArray();
stream.Close();
return result;
}
private static void Serialize(MemoryStream stream, object obj, Hashtable ht, ref int hv, Encoding encoding)
{
if (obj == null)
{
hv++;
WriteNull(stream);
}
else if (obj is Boolean)
{
hv++;
WriteBoolean(stream, ((Boolean)obj) ? __1 : __0);
}
else if ((obj is Byte) || (obj is SByte) || (obj is Int16) || (obj is UInt16) || (obj is Int32))
{
hv++;
WriteInteger(stream, Encoding.ASCII.GetBytes(obj.ToString()));
}
else if ((obj is UInt32) || (obj is Int64) || (obj is UInt64) || (obj is Decimal))
{
hv++;
WriteDouble(stream, Encoding.ASCII.GetBytes(obj.ToString()));
}
else if (obj is Single)
{
hv++;
if (Single.IsNaN((Single)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("NAN"));
}
else if (Single.IsPositiveInfinity((Single)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("INF"));
}
else if (Single.IsNegativeInfinity((Single)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("-INF"));
}
else
{
WriteDouble(stream, Encoding.ASCII.GetBytes(obj.ToString()));
}
}
else if (obj is Double)
{
hv++;
if (Double.IsNaN((Double)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("NAN"));
}
else if (Double.IsPositiveInfinity((Double)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("INF"));
}
else if (Double.IsNegativeInfinity((Double)obj))
{
WriteDouble(stream, Encoding.ASCII.GetBytes("-INF"));
}
else
{
WriteDouble(stream, Encoding.ASCII.GetBytes(obj.ToString()));
}
}
else if ((obj is Char) || (obj is String))
{
hv++;
WriteString(stream, encoding.GetBytes(obj.ToString()));
}
else if (obj is Array)
{
if (ht.ContainsKey(obj.GetHashCode()))
{
WritePointRef(stream, Encoding.ASCII.GetBytes(ht[obj.GetHashCode()].ToString()));
}
else
{
ht.Add(obj.GetHashCode(), hv++);
WriteArray(stream, obj as Array, ht, ref hv, encoding);
}
}
else if (obj is ArrayList)
{
if (ht.ContainsKey(obj.GetHashCode()))
{
WritePointRef(stream, Encoding.ASCII.GetBytes(ht[obj.GetHashCode()].ToString()));
}
else
{
ht.Add(obj.GetHashCode(), hv++);
WriteArrayList(stream, obj as ArrayList, ht, ref hv, encoding);
}
}
else if (obj is Hashtable)
{
if (ht.ContainsKey(obj.GetHashCode()))
{
WritePointRef(stream, Encoding.ASCII.GetBytes(ht[obj.GetHashCode()].ToString()));
}
else
{
ht.Add(obj.GetHashCode(), hv++);
WriteHashtable(stream, obj as Hashtable, ht, ref hv, encoding);
}
}
else
{
if (ht.ContainsKey(obj.GetHashCode()))
{
hv++;
WriteRef(stream, Encoding.ASCII.GetBytes(ht[obj.GetHashCode()].ToString()));
}
else
{
ht.Add(obj.GetHashCode(), hv++);
WriteObject(stream, obj, ht, ref hv, encoding);
}
}
}
private static void WriteNull(MemoryStream stream)
{
stream.WriteByte(__N);
stream.WriteByte(__Semicolon);
}
private static void WriteRef(MemoryStream stream, byte[] r)
{
stream.WriteByte(__r);
stream.WriteByte(__Colon);
stream.Write(r, 0, r.Length);
stream.WriteByte(__Semicolon);
}
private static void WritePointRef(MemoryStream stream, byte[] p)
{
stream.WriteByte(__R);
stream.WriteByte(__Colon);
stream.Write(p, 0, p.Length);
stream.WriteByte(__Semicolon);
}
private static void WriteBoolean(MemoryStream stream, byte b)
{
stream.WriteByte(__b);
stream.WriteByte(__Colon);
stream.WriteByte(b);
stream.WriteByte(__Semicolon);
}
private static void WriteInteger(MemoryStream stream, byte[] i)
{
stream.WriteByte(__i);
stream.WriteByte(__Colon);
stream.Write(i, 0, i.Length);
stream.WriteByte(__Semicolon);
}
private static void WriteDouble(MemoryStream stream, byte[] d)
{
stream.WriteByte(__d);
stream.WriteByte(__Colon);
stream.Write(d, 0, d.Length);
stream.WriteByte(__Semicolon);
}
private static void WriteString(MemoryStream stream, byte[] s)
{
byte[] slen = Encoding.ASCII.GetBytes(s.Length.ToString());
stream.WriteByte(__s);
stream.WriteByte(__Colon);
stream.Write(slen, 0, slen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__Quote);
stream.Write(s, 0, s.Length);
stream.WriteByte(__Quote);
stream.WriteByte(__Semicolon);
}
private static void WriteArray(MemoryStream stream, Array a, Hashtable ht, ref int hv, Encoding encoding)
{
if (a.Rank == 1)
{
int len = a.GetLength(0);
byte[] alen = Encoding.ASCII.GetBytes(len.ToString());
int lb = a.GetLowerBound(0);
int ub = a.GetUpperBound(0);
stream.WriteByte(__a);
stream.WriteByte(__Colon);
stream.Write(alen, 0, alen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
for (int i = lb; i <= ub; i++)
{
WriteInteger(stream, Encoding.ASCII.GetBytes(i.ToString()));
Serialize(stream, a.GetValue(i), ht, ref hv, encoding);
}
stream.WriteByte(__RightB);
}
else
{
WriteArray(stream, a, new int[] { 0 }, ht, ref hv, encoding);
}
}
private static void WriteArray(MemoryStream stream, Array a, int[] indices, Hashtable ht, ref int hv, Encoding encoding)
{
int n = indices.Length;
int dimension = n - 1;
int[] temp = new int[n + 1];
indices.CopyTo(temp, 0);
int len = a.GetLength(dimension);
byte[] alen = Encoding.ASCII.GetBytes(len.ToString());
int lb = a.GetLowerBound(dimension);
int ub = a.GetUpperBound(dimension);
stream.WriteByte(__a);
stream.WriteByte(__Colon);
stream.Write(alen, 0, alen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
for (int i = lb; i <= ub; i++)
{
WriteInteger(stream, Encoding.ASCII.GetBytes(i.ToString()));
if (a.Rank == n)
{
indices[n - 1] = i;
Serialize(stream, a.GetValue(indices), ht, ref hv, encoding);
}
else
{
temp[n - 1] = i;
WriteArray(stream, a, temp, ht, ref hv, encoding);
}
}
stream.WriteByte(__RightB);
}
private static void WriteArrayList(MemoryStream stream, ArrayList a, Hashtable ht, ref int hv, Encoding encoding)
{
int len = a.Count;
byte[] alen = Encoding.ASCII.GetBytes(len.ToString());
stream.WriteByte(__a);
stream.WriteByte(__Colon);
stream.Write(alen, 0, alen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
for (int i = 0; i < len; i++)
{
WriteInteger(stream, Encoding.ASCII.GetBytes(i.ToString()));
Serialize(stream, a[i], ht, ref hv, encoding);
}
stream.WriteByte(__RightB);
}
private static void WriteHashtable(MemoryStream stream, Hashtable h, Hashtable ht, ref int hv, Encoding encoding)
{
int len = h.Count;
byte[] hlen = Encoding.ASCII.GetBytes(len.ToString());
stream.WriteByte(__a);
stream.WriteByte(__Colon);
stream.Write(hlen, 0, hlen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
foreach (DictionaryEntry entry in h)
{
if ((entry.Key is Byte) || (entry.Key is SByte) || (entry.Key is Int16) || (entry.Key is UInt16) || (entry.Key is Int32))
{
WriteInteger(stream, Encoding.ASCII.GetBytes(entry.Key.ToString()));
}
else if (entry.Key is Boolean)
{
WriteInteger(stream, new byte[] { ((Boolean)entry.Key) ? __1 : __0 });
}
else
{
WriteString(stream, encoding.GetBytes(entry.Key.ToString()));
}
Serialize(stream, entry.Value, ht, ref hv, encoding);
}
stream.WriteByte(__RightB);
}
private static void WriteObject(MemoryStream stream, object obj, Hashtable ht, ref int hv, Encoding encoding)
{
Type type = obj.GetType();
if (type.IsSerializable)
{
byte[] typename = encoding.GetBytes(type.Name);
byte[] typenamelen = Encoding.ASCII.GetBytes(typename.Length.ToString());
if (obj is Serializable)
{
byte[] cs = ((Serializable)obj).Serialize();
byte[] cslen = Encoding.ASCII.GetBytes(cs.Length.ToString());
stream.WriteByte(__C);
stream.WriteByte(__Colon);
stream.Write(typenamelen, 0, typenamelen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__Quote);
stream.Write(typename, 0, typename.Length);
stream.WriteByte(__Quote);
stream.WriteByte(__Colon);
stream.Write(cslen, 0, cslen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
stream.Write(cs, 0, cs.Length);
stream.WriteByte(__RightB);
}
else
{
MethodInfo __sleep = null;
try
{
__sleep = type.GetMethod("__sleep", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase, null, new Type[0], new ParameterModifier[0]);
}
catch { }
int fl = 0;
FieldInfo[] f;
if (__sleep != null)
{
string[] fns = (string[])__sleep.Invoke(obj, null);
f = new FieldInfo[fns.Length];
for (int i = 0, len = f.Length; i < len; i++)
{
f[i] = type.GetField(fns[i], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
}
}
else
{
f = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
}
for (int i = 0, len = f.Length; i < len; i++)
{
if (f[i] != null && !(f[i].IsNotSerialized || f[i].IsInitOnly || f[i].IsLiteral))
{
fl++;
}
}
byte[] flen = Encoding.ASCII.GetBytes(fl.ToString());
stream.WriteByte(__O);
stream.WriteByte(__Colon);
stream.Write(typenamelen, 0, typenamelen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__Quote);
stream.Write(typename, 0, typename.Length);
stream.WriteByte(__Quote);
stream.WriteByte(__Colon);
stream.Write(flen, 0, flen.Length);
stream.WriteByte(__Colon);
stream.WriteByte(__LeftB);
for (int i = 0, len = f.Length; i < len; i++)
{
if (f[i] != null && !(f[i].IsNotSerialized || f[i].IsInitOnly || f[i].IsLiteral))
{
if (f[i].IsPublic)
{
WriteString(stream, encoding.GetBytes(f[i].Name));
}
else if (f[i].IsPrivate)
{
WriteString(stream, encoding.GetBytes("\0" + f[i].DeclaringType.Name + "\0" + f[i].Name));
}
else
{
WriteString(stream, encoding.GetBytes("\0*\0" + f[i].Name));
}
Serialize(stream, f[i].GetValue(obj), ht, ref hv, encoding);
}
}
stream.WriteByte(__RightB);
}
}
else
{
WriteNull(stream);
}
}
public static object ChangeType(object obj, Type destType)
{
if (obj == null)
{
return null;
}
Type sourceType = obj.GetType();
if (sourceType == destType)
{
return obj;
}
if (obj is ArrayList && destType.IsArray)
{
return ArrayListToArray(obj as ArrayList, destType.GetArrayRank(), destType.GetElementType());
}
else if (obj is ArrayList && destType == typeof(Hashtable))
{
return ArrayListToHashtable(obj as ArrayList);
}
else if ((!sourceType.IsByRef && destType.IsByRef) || (!sourceType.IsPointer && destType.IsPointer))
{
return Convert.ChangeType(obj, destType.GetElementType());
}
else
{
return Convert.ChangeType(obj, destType);
}
}
private static Hashtable ArrayListToHashtable(ArrayList a)
{
int n = a.Count;
Hashtable h = new Hashtable(n);
for (int i = 0; i < n; i++)
{
h[i] = a[i];
}
return h;
}
private static Array ArrayListToArray(ArrayList obj, int rank, Type elementType)
{
int[] lengths = new int[rank];
ArrayList al = obj;
for (int i = 0; i < rank; i++)
{
lengths[i] = al.Count;
if (al.Count > 0)
{
al = al[0] as ArrayList;
}
else
{
break;
}
}
Array result = Array.CreateInstance(elementType, lengths);
if (lengths[0] > 0)
{
ArrayListToArray(obj as ArrayList, result, new int[] { 0 }, elementType);
}
return result;
}
private static void ArrayListToArray(ArrayList source, Array dest, int[] indices, Type elementType)
{
int n = indices.Length;
int dimension = n - 1;
int[] temp = new int[n + 1];
indices.CopyTo(temp, 0);
int len = dest.GetLength(dimension);
for (int i = 0; i < len; i++)
{
if (dest.Rank == n)
{
indices[n - 1] = i;
if (source[i] == null || elementType == typeof(object) || elementType == source[i].GetType())
{
dest.SetValue(source[i], indices);
}
else
{
dest.SetValue(ChangeType(source[i], elementType), indices);
}
}
else
{
temp[n - 1] = i;
ArrayListToArray(source[i] as ArrayList, dest, temp, elementType);
}
}
}
private static object CreateInstance(Type type)
{
try
{
return Activator.CreateInstance(type);
}
catch
{
}
try
{
return Activator.CreateInstance(type, new object [] { 0 });
}
catch
{
}
try
{
return Activator.CreateInstance(type, new object [] { false });
}
catch
{
}
try
{
return Activator.CreateInstance(type, new object [] { "" });
}
catch
{
}
try
{
FieldInfo[] f = type.GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0, n = f.Length; i < n; i++)
{
if (f[i].FieldType == type)
{
return f[i].GetValue(null);
}
}
}
catch
{
}
MethodInfo[] m = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
for (int i = 0, n = m.Length; i < n; i++)
{
if (m[i].ReturnType == type)
{
try
{
return m[i].Invoke(null, null);
}
catch
{
}
try
{
return m[i].Invoke(null, new object[] { 0 });
}
catch
{
}
try
{
return m[i].Invoke(null, new object[] { false });
}
catch
{
}
try
{
return m[i].Invoke(null, new object[] { "" });
}
catch
{
}
}
}
ThrowError("Can't create the instance of " + type.FullName);
return null;
}
public static object UnSerialize(byte[] ss)
{
return UnSerialize(ss, null, Encoding.UTF8);
}
public static object UnSerialize(byte[] ss, Encoding encoding)
{
return UnSerialize(ss, null, encoding);
}
public static object UnSerialize(byte[] ss, Type type)
{
return UnSerialize(ss, type, Encoding.UTF8);
}
public static object UnSerialize(byte[] ss, Type type, Encoding encoding)
{
int hv = 1;
MemoryStream stream = new MemoryStream(ss);
stream.Seek(0, SeekOrigin.Begin);
object result = UnSerialize(stream, new Hashtable(), ref hv, new Hashtable(), encoding);
stream.Close();
if (type != null && result != null)
{
result = ChangeType(result, type);
}
return result;
}
private static object UnSerialize(MemoryStream stream, Hashtable ht, ref int hv, Hashtable rt, Encoding encoding)
{
switch (stream.ReadByte())
{
case __N: return ht[hv++] = ReadNull(stream);
case __b: return ht[hv++] = ReadBoolean(stream);
case __i: return ht[hv++] = ReadInteger(stream);
case __d: return ht[hv++] = ReadDouble(stream);
case __s: return ht[hv++] = ReadString(stream, encoding);
case __U: return ht[hv++] = ReadUnicodeString(stream);
case __r: return ht[hv++] = ReadRef(stream, ht, rt);
case __a: return ReadArray(stream, ht, ref hv, rt, encoding);
case __O: return ReadObject(stream, ht, ref hv, rt, encoding);
case __C: return ReadCustomObject(stream, ht, ref hv, encoding);
case __R: return ReadRef(stream, ht, rt);
default: ThrowError("Wrong Serialize Stream!"); return null;
}
}
private static string ReadNumber(MemoryStream stream)
{
StringBuilder sb = new StringBuilder();
int i = stream.ReadByte();
while (i != __Semicolon && i != __Colon)
{
sb.Append((char)i);
i = stream.ReadByte();
}
return sb.ToString();
}
private static object ReadNull(MemoryStream stream)
{
stream.Seek(1, SeekOrigin.Current);
return null;
}
private static bool ReadBoolean(MemoryStream stream)
{
stream.Seek(1, SeekOrigin.Current);
bool b = (stream.ReadByte() == __1);
stream.Seek(1, SeekOrigin.Current);
return b;
}
private static object ReadInteger(MemoryStream stream)
{
stream.Seek(1, SeekOrigin.Current);
string i = ReadNumber(stream);
bool neg = i.StartsWith("-");
try
{
return (neg ? (object)SByte.Parse(i) : (object)Byte.Parse(i));
}
catch
{
try
{
return (neg ? (object)Int16.Parse(i) : (object)UInt16.Parse(i));
}
catch
{
return Int32.Parse(i);
}
}
}
private static object ReadDouble(MemoryStream stream)
{
stream.Seek(1, SeekOrigin.Current);
string d = ReadNumber(stream);
if (d == "NAN") return Double.NaN;
if (d == "INF") return Double.PositiveInfinity;
if (d == "-INF") return Double.NegativeInfinity;
bool neg = d.StartsWith("-");
try
{
return UInt32.Parse(d);
}
catch
{
try
{
return (neg ? (object)Int64.Parse(d) : (object)UInt64.Parse(d));
}
catch
{
try
{
return Decimal.Parse(d);
}
catch
{
try
{
return Single.Parse(d);
}
catch
{
return Double.Parse(d);
}
}
}
}
}
private static string ReadString(MemoryStream stream, Encoding encoding)
{
stream.Seek(1, SeekOrigin.Current);
int len = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
byte[] buf = new byte[len];
stream.Read(buf, 0, len);
string s = encoding.GetString(buf);
stream.Seek(2, SeekOrigin.Current);
return s;
}
private static string ReadUnicodeString(MemoryStream stream)
{
stream.Seek(1, SeekOrigin.Current);
int l = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
StringBuilder sb = new StringBuilder(l);
int c;
for (int i = 0; i < l; i++)
{
if ((c = stream.ReadByte()) == __Slash)
{
char c1 = (char)stream.ReadByte();
char c2 = (char)stream.ReadByte();
char c3 = (char)stream.ReadByte();
char c4 = (char)stream.ReadByte();
sb.Append((char)Int32.Parse(String.Concat(c1, c2, c3, c4), System.Globalization.NumberStyles.HexNumber));
}
else
{
sb.Append((char)c);
}
}
stream.Seek(2, SeekOrigin.Current);
return sb.ToString();
}
private static object ReadRef(MemoryStream stream, Hashtable ht, Hashtable rt)
{
stream.Seek(1, SeekOrigin.Current);
int r = Int32.Parse(ReadNumber(stream));
if (rt.ContainsKey(r))
{
rt[r] = true;
}
return ht[r];
}
private static object ReadArray(MemoryStream stream, Hashtable ht, ref int hv, Hashtable rt, Encoding encoding)
{
stream.Seek(1, SeekOrigin.Current);
int n = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
Hashtable h = new Hashtable(n);
ArrayList al = new ArrayList(n);
int r = hv;
rt.Add(r, false);
long p = stream.Position;
ht[hv++] = h;
for (int i = 0; i < n; i++)
{
object key, value;
switch (stream.ReadByte())
{
case __i: key = Convert.ToInt32(ReadInteger(stream)); break;
case __s: key = ReadString(stream, encoding); break;
case __U: key = ReadUnicodeString(stream); break;
default: ThrowError("Wrong Serialize Stream!"); return null;
}
value = UnSerialize(stream, ht, ref hv, rt, encoding);
if (al != null)
{
if ((key is Int32) && (int)key == i)
{
al.Add(value);
}
else
{
al = null;
}
}
h[key] = value;
}
if (al != null)
{
ht[r] = al;
if ((bool)rt[r])
{
hv = r + 1;
stream.Position = p;
for (int i = 0; i < n; i++)
{
int key;
switch (stream.ReadByte())
{
case __i: key = Convert.ToInt32(ReadInteger(stream)); break;
default: ThrowError("Wrong Serialize Stream!"); return null;
}
al[key] = UnSerialize(stream, ht, ref hv, rt, encoding);
}
}
}
rt.Remove(r);
stream.Seek(1, SeekOrigin.Current);
return ht[r];
}
private static object ReadObject(MemoryStream stream, Hashtable ht, ref int hv, Hashtable rt, Encoding encoding)
{
stream.Seek(1, SeekOrigin.Current);
int len = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
byte[] buf = new byte[len];
stream.Read(buf, 0, len);
string cn = encoding.GetString(buf);
stream.Seek(2, SeekOrigin.Current);
int n = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
Type type = GetType(cn);
object o;
if (type != null)
{
try
{
o = CreateInstance(type);
}
catch
{
o = new Hashtable(n);
}
}
else
{
o = new Hashtable(n);
}
ht[hv++] = o;
for (int i = 0; i < n; i++)
{
string key;
switch (stream.ReadByte())
{
case __s: key = ReadString(stream, encoding); break;
case __U: key = ReadUnicodeString(stream); break;
default: ThrowError("Wrong Serialize Stream!"); return null;
}
if (key.Substring(0, 1) == "\0")
{
key = key.Substring(key.IndexOf("\0", 1) + 1);
}
if (o is Hashtable)
{
((Hashtable)o)[key] = UnSerialize(stream, ht, ref hv, rt, encoding);
}
else
{
type.InvokeMember(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetField, null, o, new object[] { UnSerialize(stream, ht, ref hv, rt, encoding) });
}
}
stream.Seek(1, SeekOrigin.Current);
MethodInfo __wakeup = null;
try
{
__wakeup = o.GetType().GetMethod("__wakeup", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase, null, new Type[0], new ParameterModifier[0]);
}
catch { }
if (__wakeup != null)
{
__wakeup.Invoke(o, null);
}
return o;
}
private static object ReadCustomObject(MemoryStream stream, Hashtable ht, ref int hv, Encoding encoding)
{
stream.Seek(1, SeekOrigin.Current);
int len = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
byte[] buf = new byte[len];
stream.Read(buf, 0, len);
string cn = encoding.GetString(buf);
stream.Seek(2, SeekOrigin.Current);
int n = Int32.Parse(ReadNumber(stream));
stream.Seek(1, SeekOrigin.Current);
Type type = GetType(cn);
object o;
if (type != null)
{
o = CreateInstance(type);
}
else
{
ThrowError(String.Concat("Type ", cn, " is undefined!"));
return null;
}
ht[hv++] = o;
if (o is Serializable)
{
byte[] b = new byte[n];
stream.Read(b, 0, n);
((Serializable)o).UnSerialize(b);
}
else
{
stream.Seek(n, SeekOrigin.Current);
}
stream.Seek(1, SeekOrigin.Current);
return o;
}
private static void ThrowError(string message)
{
throw new UnSerializeException(message);
}
private static Type GetType(string typeName)
{
Type type = Type.GetType(typeName, false, true);
if (type != null) return type;
foreach (DictionaryEntry e in __ns)
{
type = Type.GetType(String.Concat((string)e.Key, ".", typeName, ", ", (string)e.Value), false, true);
if (type != null) return type;
}
return null;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.