-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSerializedPropertyStore.cs
executable file
·127 lines (116 loc) · 4.65 KB
/
SerializedPropertyStore.cs
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
using System;
using System.Linq;
using Securify.PropertyStore.Structures;
using System.Collections.Generic;
using System.Text;
namespace Securify.PropertyStore
{
/// <summary>
/// The Property Store Binary File Format is a sequence of Serialized Property
/// Storage structures. The sequence MUST be terminated by a Serialized Property
/// Storage structure that specifies 0x00000000 for the Storage Size field.
/// </summary>
public class SerializedPropertyStore : Structure
{
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public SerializedPropertyStore() : base()
{
PropertyStorage = new List<SerializedPropertyStorage>();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="SerializedPropertyStorage">A sequence of one or more Serialized Property Storage structures</param>
public SerializedPropertyStore(List<SerializedPropertyStorage> SerializedPropertyStorage) : base()
{
this.PropertyStorage = SerializedPropertyStorage;
}
#endregion // Constructor
#region FromByteArray
/// <summary>
/// Create a LinkTargetIDList from a given byte array
/// </summary>
/// <param name="ba">The byte array</param>
/// <returns>A LinkTargetIDList object</returns>
public static SerializedPropertyStore FromByteArray(byte[] ba)
{
SerializedPropertyStore PropertyStore = new SerializedPropertyStore();
if (ba.Length < 8)
{
throw new ArgumentException(String.Format("Size of the SerializedPropertyStore is less than 8 ({0})", ba.Length));
}
UInt32 StoreSize = BitConverter.ToUInt32(ba, 0);
if (ba.Length < StoreSize)
{
throw new ArgumentException(String.Format("Size of the SerializedPropertyStore is less than {0} ({1})", StoreSize, ba.Length));
}
ba = ba.Skip(4).ToArray();
UInt32 StorageSize = BitConverter.ToUInt32(ba, 0);
while (StorageSize > 5)
{
SerializedPropertyStorage PropertyStorage = SerializedPropertyStorage.FromByteArray(ba);
PropertyStore.PropertyStorage.Add(PropertyStorage);
ba = ba.Skip((int)StorageSize).ToArray();
StorageSize = BitConverter.ToUInt32(ba, 0);
}
return PropertyStore;
}
#endregion // FromByteArray
#region StorageSize
/// <summary>
/// Store Size (4 bytes): An unsigned integer that specifies the total size,
/// in bytes, of this structure, excluding the size of this field.
/// </summary>
public virtual UInt32 StoreSize
{
get
{
UInt32 Size = 8;
for(int i = 0; i < PropertyStorage.Count; i++)
{
Size += PropertyStorage[i].StorageSize;
}
return Size;
}
}
#endregion // StoreSize
/// <summary>
/// A sequence of one or more Serialized Property Storage structures
/// </summary>
public List<SerializedPropertyStorage> PropertyStorage { get; set; }
#region GetBytes
/// <inheritdoc />
public override byte[] GetBytes()
{
int Offset = 4;
byte[] PropertyStore = new byte[StoreSize];
Buffer.BlockCopy(BitConverter.GetBytes(StoreSize), 0, PropertyStore, 0, 4);
for (int i = 0; i < this.PropertyStorage.Count; i++)
{
SerializedPropertyStorage PropertyStorage = this.PropertyStorage[i];
Buffer.BlockCopy(PropertyStorage.GetBytes(), 0, PropertyStore, Offset, (int)PropertyStorage.StorageSize);
Offset += (int)PropertyStorage.StorageSize;
}
return PropertyStore;
}
#endregion // GetBytes
#region ToString
/// <inheritdoc />
public override String ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(base.ToString());
builder.AppendFormat("StoreSize: {0} (0x{0X})", StoreSize);
builder.AppendLine();
for (int i = 0; i < PropertyStorage.Count; i++)
{
builder.Append(PropertyStorage[i].ToString());
}
return builder.ToString();
}
#endregion // ToString
}
}